From 8fbf45e1a553846ea068c700c9138c4281801f86 Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Fri, 7 Nov 2025 20:34:32 -0500 Subject: [PATCH] AC-14999: FPC not work when login by @rogerdz --- .../App/Request/Http/IdentifierForSave.php | 3 +- .../Request/Http/IdentifierForSaveTest.php | 130 ++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 dev/tests/integration/testsuite/Magento/PageCache/Model/App/Request/Http/IdentifierForSaveTest.php diff --git a/app/code/Magento/PageCache/Model/App/Request/Http/IdentifierForSave.php b/app/code/Magento/PageCache/Model/App/Request/Http/IdentifierForSave.php index cbb3c34db6cb..bcd5291ad6da 100644 --- a/app/code/Magento/PageCache/Model/App/Request/Http/IdentifierForSave.php +++ b/app/code/Magento/PageCache/Model/App/Request/Http/IdentifierForSave.php @@ -49,7 +49,8 @@ public function getValue() $data = [ $this->request->isSecure(), preg_replace($pattern, $replace, (string)$this->request->getUriString()), - $this->context->getVaryString() + $this->request->get(\Magento\Framework\App\Response\Http::COOKIE_VARY_STRING) + ?: $this->context->getVaryString() ]; $data = $this->identifierStoreReader->getPageTagsWithStoreCacheTags($data); diff --git a/dev/tests/integration/testsuite/Magento/PageCache/Model/App/Request/Http/IdentifierForSaveTest.php b/dev/tests/integration/testsuite/Magento/PageCache/Model/App/Request/Http/IdentifierForSaveTest.php new file mode 100644 index 000000000000..def999243b3d --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/PageCache/Model/App/Request/Http/IdentifierForSaveTest.php @@ -0,0 +1,130 @@ +objectManager = Bootstrap::getObjectManager(); + $this->identifierForSave = $this->objectManager->get(IdentifierForSave::class); + $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage(); + $this->context = $this->objectManager->get(Context::class); + $this->cookieManager = $this->objectManager->get(CookieManagerInterface::class); + $this->cookieMetadataFactory = $this->objectManager->get(CookieMetadataFactory::class); + } + + /** + * Test that cache identifier properly handles logged-in customers + */ + #[ + ConfigFixture('system/full_page_cache/caching_application', '1', 'store'), + ConfigFixture('system/full_page_cache/enabled', '1', 'store'), + DataFixture(CustomerFixture::class, as: 'customer') + ] + public function testAfterGetValueWithLoggedInCustomer() + { + // Get customer and login + $customer = $this->fixtures->get('customer'); + $customerSession = $this->objectManager->get(Session::class); + $customerSession->loginById($customer->getId()); + + // Get cache identifiers + $result = $this->identifierForSave->getValue(); + + // Verify that both cache keys are not empty and contain customer context + $this->assertNotEmpty($result, 'Cache identifier for save should not be empty for logged-in user'); + + // Test scenario: Simulate context vary string being empty but cookie vary string present + // Get the current vary string from context + $originalVaryString = $this->context->getVaryString(); + $this->assertNotEmpty($originalVaryString, 'Context vary string should not be empty for logged-in user'); + + // Set the vary cookie to simulate a previous request + $cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/'); + $this->cookieManager->setSensitiveCookie( + self::COOKIE_VARY_STRING, + $originalVaryString, + $cookieMetadata + ); + + // Clear the context vary string to simulate depersonalization + $this->context->_resetState(); + + // Verify context vary string is now empty + $this->assertEmpty($this->context->getVaryString(), 'Context vary string should be empty after reset'); + + // Get cache identifiers again - should still work due to cookie fallback + $resultWithEmptyContext = $this->identifierForSave->getValue(); + + // Both should still generate valid cache keys due to cookie fallback + $this->assertNotEmpty( + $resultWithEmptyContext, + 'Cache identifier for save should work with empty context due to cookie fallback' + ); + + // Both cache key should be same even after context vary string is empty because it use cookie vary string + $this->assertEquals($result, $resultWithEmptyContext); + + // Clean up + $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata); + } +}