|
| 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\ObjectManagerInterface; |
| 11 | +use Magento\TestFramework\Fixture\Config as ConfigFixture; |
| 12 | +use Magento\TestFramework\Fixture\DataFixture; |
| 13 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 14 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\Customer\Model\Session; |
| 17 | +use Magento\Framework\App\Http\Context; |
| 18 | +use Magento\Framework\Stdlib\CookieManagerInterface; |
| 19 | +use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory; |
| 20 | +use Magento\Customer\Test\Fixture\Customer as CustomerFixture; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +/** |
| 24 | + * Integration test for \Magento\PageCache\Model\App\Request\Http\IdentifierForSave |
| 25 | + * |
| 26 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 27 | + */ |
| 28 | +class IdentifierForSaveTest extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var ObjectManagerInterface |
| 32 | + */ |
| 33 | + private $objectManager; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var IdentifierForSave |
| 37 | + */ |
| 38 | + private $identifierForSave; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var DataFixtureStorage |
| 42 | + */ |
| 43 | + private $fixtures; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var Context |
| 47 | + */ |
| 48 | + private $context; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var CookieManagerInterface |
| 52 | + */ |
| 53 | + private $cookieManager; |
| 54 | + |
| 55 | + /** |
| 56 | + * @var CookieMetadataFactory |
| 57 | + */ |
| 58 | + private $cookieMetadataFactory; |
| 59 | + |
| 60 | + /** |
| 61 | + * @var string |
| 62 | + */ |
| 63 | + private const COOKIE_VARY_STRING = 'X-Magento-Vary'; |
| 64 | + |
| 65 | + protected function setUp(): void |
| 66 | + { |
| 67 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 68 | + $this->identifierForSave = $this->objectManager->get(IdentifierForSave::class); |
| 69 | + $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 70 | + $this->context = $this->objectManager->get(Context::class); |
| 71 | + $this->cookieManager = $this->objectManager->get(CookieManagerInterface::class); |
| 72 | + $this->cookieMetadataFactory = $this->objectManager->get(CookieMetadataFactory::class); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test that cache identifier properly handles logged-in customers |
| 77 | + */ |
| 78 | + #[ |
| 79 | + ConfigFixture('system/full_page_cache/caching_application', '1', 'store'), |
| 80 | + ConfigFixture('system/full_page_cache/enabled', '1', 'store'), |
| 81 | + DataFixture(CustomerFixture::class, as: 'customer') |
| 82 | + ] |
| 83 | + public function testAfterGetValueWithLoggedInCustomer() |
| 84 | + { |
| 85 | + // Get customer and login |
| 86 | + $customer = $this->fixtures->get('customer'); |
| 87 | + $customerSession = $this->objectManager->get(Session::class); |
| 88 | + $customerSession->loginById($customer->getId()); |
| 89 | + |
| 90 | + // Get cache identifiers |
| 91 | + $result = $this->identifierForSave->getValue(); |
| 92 | + |
| 93 | + // Verify that both cache keys are not empty and contain customer context |
| 94 | + $this->assertNotEmpty($result, 'Cache identifier for save should not be empty for logged-in user'); |
| 95 | + |
| 96 | + // Test scenario: Simulate context vary string being empty but cookie vary string present |
| 97 | + // Get the current vary string from context |
| 98 | + $originalVaryString = $this->context->getVaryString(); |
| 99 | + $this->assertNotEmpty($originalVaryString, 'Context vary string should not be empty for logged-in user'); |
| 100 | + |
| 101 | + // Set the vary cookie to simulate a previous request |
| 102 | + $cookieMetadata = $this->cookieMetadataFactory->createSensitiveCookieMetadata()->setPath('/'); |
| 103 | + $this->cookieManager->setSensitiveCookie( |
| 104 | + self::COOKIE_VARY_STRING, |
| 105 | + $originalVaryString, |
| 106 | + $cookieMetadata |
| 107 | + ); |
| 108 | + |
| 109 | + // Clear the context vary string to simulate depersonalization |
| 110 | + $this->context->_resetState(); |
| 111 | + |
| 112 | + // Verify context vary string is now empty |
| 113 | + $this->assertEmpty($this->context->getVaryString(), 'Context vary string should be empty after reset'); |
| 114 | + |
| 115 | + // Get cache identifiers again - should still work due to cookie fallback |
| 116 | + $resultWithEmptyContext = $this->identifierForSave->getValue(); |
| 117 | + |
| 118 | + // Both should still generate valid cache keys due to cookie fallback |
| 119 | + $this->assertNotEmpty( |
| 120 | + $resultWithEmptyContext, |
| 121 | + 'Cache identifier for save should work with empty context due to cookie fallback' |
| 122 | + ); |
| 123 | + |
| 124 | + // Both cache key should be same even after context vary string is empty because it use cookie vary string |
| 125 | + $this->assertEquals($result, $resultWithEmptyContext); |
| 126 | + |
| 127 | + // Clean up |
| 128 | + $this->cookieManager->deleteCookie(self::COOKIE_VARY_STRING, $cookieMetadata); |
| 129 | + } |
| 130 | +} |
0 commit comments