|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All rights reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\PageCache\Model\App; |
| 9 | + |
| 10 | +use Magento\Framework\App\Cache\StateInterface; |
| 11 | +use Magento\Framework\App\PageCache\Identifier; |
| 12 | +use Magento\Framework\App\Request\Http; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\PageCache\Model\App\Request\Http\IdentifierForSave; |
| 15 | +use Magento\PageCache\Model\Cache\Type; |
| 16 | +use Magento\Store\Model\StoreManager; |
| 17 | +use Magento\Store\Test\Fixture\Group as StoreGroupFixture; |
| 18 | +use Magento\Store\Test\Fixture\Store as StoreFixture; |
| 19 | +use Magento\Store\Test\Fixture\Website as WebsiteFixture; |
| 20 | +use Magento\TestFramework\Fixture\Config as ConfigFixture; |
| 21 | +use Magento\TestFramework\Fixture\DataFixture; |
| 22 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 23 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 24 | +use Magento\TestFramework\Fixture\DbIsolation; |
| 25 | +use Magento\TestFramework\Helper\Bootstrap; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | + |
| 28 | +/** |
| 29 | + * Integration test for \Magento\PageCache\Model\App\CacheIdentifierPlugin |
| 30 | + */ |
| 31 | +class CacheIdentifierPluginTest extends TestCase |
| 32 | +{ |
| 33 | + /** |
| 34 | + * @var ObjectManagerInterface |
| 35 | + */ |
| 36 | + private $objectManager; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Http |
| 40 | + */ |
| 41 | + private $request; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Identifier |
| 45 | + */ |
| 46 | + private $identifier; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var IdentifierForSave |
| 50 | + */ |
| 51 | + private $identifierForSave; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var DataFixtureStorage |
| 55 | + */ |
| 56 | + private $fixtures; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var StateInterface |
| 60 | + */ |
| 61 | + private $cacheState; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var bool |
| 65 | + */ |
| 66 | + private $originalCacheState; |
| 67 | + |
| 68 | + protected function setUp(): void |
| 69 | + { |
| 70 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 71 | + $this->request = $this->objectManager->get(Http::class); |
| 72 | + $this->identifier = $this->objectManager->get(Identifier::class); |
| 73 | + $this->identifierForSave = $this->objectManager->get(IdentifierForSave::class); |
| 74 | + $this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage(); |
| 75 | + $this->cacheState = $this->objectManager->get(StateInterface::class); |
| 76 | + |
| 77 | + // Store original cache state |
| 78 | + $this->originalCacheState = $this->cacheState->isEnabled(Type::TYPE_IDENTIFIER); |
| 79 | + |
| 80 | + // Enable the cache type |
| 81 | + $this->cacheState->setEnabled(Type::TYPE_IDENTIFIER, true); |
| 82 | + } |
| 83 | + |
| 84 | + protected function tearDown(): void |
| 85 | + { |
| 86 | + // Revert cache state to original |
| 87 | + if (isset($this->cacheState) && isset($this->originalCacheState)) { |
| 88 | + $this->cacheState->setEnabled(Type::TYPE_IDENTIFIER, $this->originalCacheState); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test that cache identifier includes run type and run code |
| 94 | + */ |
| 95 | + #[ |
| 96 | + DbIsolation(false), |
| 97 | + ConfigFixture('system/full_page_cache/caching_application', '1', 'store'), |
| 98 | + ConfigFixture('system/full_page_cache/enabled', '1', 'store'), |
| 99 | + DataFixture(WebsiteFixture::class, as: 'website'), |
| 100 | + DataFixture(StoreGroupFixture::class, ['website_id' => '$website.id$'], 'store_group'), |
| 101 | + DataFixture(StoreFixture::class, ['store_group_id' => '$store_group.id$'], 'store') |
| 102 | + ] |
| 103 | + public function testAfterGetValueWithRunTypeAndCode() |
| 104 | + { |
| 105 | + $storeCode = $this->fixtures->get('store')->getCode(); |
| 106 | + $serverParams = [ |
| 107 | + StoreManager::PARAM_RUN_TYPE => 'store', |
| 108 | + StoreManager::PARAM_RUN_CODE => $storeCode |
| 109 | + ]; |
| 110 | + $this->request->setServer(new \Laminas\Stdlib\Parameters($serverParams)); |
| 111 | + |
| 112 | + $result = $this->identifier->getValue(); |
| 113 | + $this->assertNotEmpty($result); |
| 114 | + $this->assertStringContainsString('MAGE_RUN_TYPE=store', $result); |
| 115 | + $this->assertStringContainsString('MAGE_RUN_CODE=' . $storeCode, $result); |
| 116 | + |
| 117 | + $resultForSave = $this->identifierForSave->getValue(); |
| 118 | + $this->assertNotEmpty($resultForSave); |
| 119 | + $this->assertStringContainsString('MAGE_RUN_TYPE=store', $resultForSave); |
| 120 | + $this->assertStringContainsString('MAGE_RUN_CODE=' . $storeCode, $resultForSave); |
| 121 | + } |
| 122 | +} |
0 commit comments