Skip to content

Commit a7e62fa

Browse files
committed
AC-14999: FPC not work when login
Test coverage for the fix. Use cookie vary string while generating cache key
1 parent 090bbfe commit a7e62fa

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All rights reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageCache\Model\App\Request\Http;
9+
10+
use Magento\Framework\App\Request\Http as HttpRequest;
11+
use Magento\Framework\App\Response\Http as HttpResponse;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Store\Model\StoreManager;
14+
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
15+
use Magento\Store\Test\Fixture\Store as StoreFixture;
16+
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
17+
use Magento\TestFramework\Fixture\Config as ConfigFixture;
18+
use Magento\TestFramework\Fixture\DataFixture;
19+
use Magento\TestFramework\Fixture\DataFixtureStorage;
20+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
21+
use Magento\TestFramework\Fixture\DbIsolation;
22+
use Magento\TestFramework\Helper\Bootstrap;
23+
use PHPUnit\Framework\TestCase;
24+
use Magento\Customer\Model\Session;
25+
use Magento\Framework\App\Http\Context;
26+
use Magento\Framework\Stdlib\CookieManagerInterface;
27+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
28+
use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
29+
30+
/**
31+
* Integration test for \Magento\PageCache\Model\App\Request\Http\IdentifierForSave
32+
*/
33+
class IdentifierForSaveTest extends TestCase
34+
{
35+
/**
36+
* @var ObjectManagerInterface
37+
*/
38+
private $objectManager;
39+
40+
/**
41+
* @var HttpRequest
42+
*/
43+
private $request;
44+
45+
/**
46+
* @var IdentifierForSave
47+
*/
48+
private $identifierForSave;
49+
50+
/**
51+
* @var DataFixtureStorage
52+
*/
53+
private $fixtures;
54+
55+
/*
56+
* @var Context
57+
*/
58+
private $context;
59+
60+
/*
61+
* @var CookieManagerInterface
62+
*/
63+
private $cookieManager;
64+
65+
/**
66+
* @var CookieMetadataFactory
67+
*/
68+
private $cookieMetadataFactory;
69+
70+
protected function setUp(): void
71+
{
72+
$this->objectManager = Bootstrap::getObjectManager();
73+
$this->request = $this->objectManager->get(HttpRequest::class);
74+
$this->identifierForSave = $this->objectManager->get(IdentifierForSave::class);
75+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
76+
$this->context = $this->objectManager->get(Context::class);
77+
$this->cookieManager = $this->objectManager->get(CookieManagerInterface::class);
78+
$this->cookieMetadataFactory = $this->objectManager->get(CookieMetadataFactory::class);
79+
}
80+
81+
/**
82+
* Test that cache identifier properly handles logged-in customers
83+
*/
84+
#[
85+
DbIsolation(false),
86+
ConfigFixture('system/full_page_cache/caching_application', '1', 'store'),
87+
ConfigFixture('system/full_page_cache/enabled', '1', 'store'),
88+
DataFixture(WebsiteFixture::class, as: 'website'),
89+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website.id$'], 'store_group'),
90+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group.id$'], 'store'),
91+
DataFixture(CustomerFixture::class, as: 'customer')
92+
]
93+
public function testAfterGetValueWithLoggedInCustomer()
94+
{
95+
$storeCode = $this->fixtures->get('store')->getCode();
96+
$serverParams = [
97+
StoreManager::PARAM_RUN_TYPE => 'store',
98+
StoreManager::PARAM_RUN_CODE => $storeCode
99+
];
100+
$this->request->setServer(new \Laminas\Stdlib\Parameters($serverParams));
101+
102+
// Get customer and login
103+
$customer = $this->fixtures->get('customer');
104+
$customerSession = $this->objectManager->get(Session::class);
105+
$customerSession->loginById($customer->getId());
106+
107+
// Get cache identifiers
108+
$result = $this->identifierForSave->getValue();
109+
110+
// Verify that both cache keys are not empty and contain customer context
111+
$this->assertNotEmpty($result, 'Cache identifier for save should not be empty for logged-in user');
112+
113+
// Test scenario: Simulate context vary string being empty but cookie vary string present
114+
// Get the current vary string from context
115+
$originalVaryString = $this->context->getVaryString();
116+
$this->assertNotEmpty($originalVaryString, 'Context vary string should not be empty for logged-in user');
117+
118+
// Set the vary cookie to simulate a previous request
119+
$cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/');
120+
$this->cookieManager->setSensitiveCookie(
121+
HttpResponse::COOKIE_VARY_STRING,
122+
$originalVaryString,
123+
$cookieMetadata
124+
);
125+
126+
// Clear the context vary string to simulate depersonalization
127+
$this->context->_resetState();
128+
129+
// Verify context vary string is now empty
130+
$this->assertEmpty($this->context->getVaryString(), 'Context vary string should be empty after reset');
131+
132+
// Get cache identifiers again - should still work due to cookie fallback
133+
$resultWithEmptyContext = $this->identifierForSave->getValue();
134+
135+
// Both should still generate valid cache keys due to cookie fallback
136+
$this->assertNotEmpty(
137+
$resultWithEmptyContext,
138+
'Cache identifier for save should work with empty context due to cookie fallback'
139+
);
140+
141+
// Both cache key should be same even after context vary string is empty because it use cookie vary string
142+
$this->assertEquals($result, $resultWithEmptyContext);
143+
144+
// Clean up
145+
$this->cookieManager->deleteCookie(
146+
HttpResponse::COOKIE_VARY_STRING,
147+
$cookieMetadata
148+
);
149+
}
150+
}

0 commit comments

Comments
 (0)