diff --git a/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php b/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php index ef85d6faf2371..68cd0eb8cc1e9 100644 --- a/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php +++ b/app/code/Magento/PageCache/Model/Layout/LayoutPlugin.php @@ -20,22 +20,31 @@ class LayoutPlugin */ protected $response; + /** + * @var \Magento\Framework\App\MaintenanceMode + */ + private $maintenanceMode; + /** * Constructor * * @param \Magento\Framework\App\ResponseInterface $response - * @param \Magento\PageCache\Model\Config $config + * @param \Magento\PageCache\Model\Config $config + * @param \Magento\Framework\App\MaintenanceMode $maintenanceMode */ public function __construct( \Magento\Framework\App\ResponseInterface $response, - \Magento\PageCache\Model\Config $config + \Magento\PageCache\Model\Config $config, + \Magento\Framework\App\MaintenanceMode $maintenanceMode ) { $this->response = $response; $this->config = $config; + $this->maintenanceMode = $maintenanceMode; } /** * Set appropriate Cache-Control headers + * * We have to set public headers in order to tell Varnish and Builtin app that page should be cached * * @param \Magento\Framework\View\Layout $subject @@ -44,7 +53,7 @@ public function __construct( */ public function afterGenerateXml(\Magento\Framework\View\Layout $subject, $result) { - if ($subject->isCacheable() && $this->config->isEnabled()) { + if ($subject->isCacheable() && !$this->maintenanceMode->isOn() && $this->config->isEnabled()) { $this->response->setPublicHeaders($this->config->getTtl()); } return $result; @@ -68,6 +77,7 @@ public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result) if ($isVarnish && $isEsiBlock) { continue; } + // phpcs:ignore $tags = array_merge($tags, $block->getIdentities()); } } diff --git a/app/code/Magento/PageCache/Observer/SwitchPageCacheOnMaintenance.php b/app/code/Magento/PageCache/Observer/SwitchPageCacheOnMaintenance.php deleted file mode 100644 index 7017da27eee93..0000000000000 --- a/app/code/Magento/PageCache/Observer/SwitchPageCacheOnMaintenance.php +++ /dev/null @@ -1,108 +0,0 @@ -cacheManager = $cacheManager; - $this->pageCacheStateStorage = $pageCacheStateStorage; - } - - /** - * Switches Full Page Cache. - * - * Depending on enabling or disabling Maintenance Mode it turns off or restores Full Page Cache state. - * - * @param Observer $observer - * @return void - */ - public function execute(Observer $observer): void - { - if ($observer->getData('isOn')) { - $this->pageCacheStateStorage->save($this->isFullPageCacheEnabled()); - $this->turnOffFullPageCache(); - } else { - $this->restoreFullPageCacheState(); - } - } - - /** - * Turns off Full Page Cache. - * - * @return void - */ - private function turnOffFullPageCache(): void - { - if (!$this->isFullPageCacheEnabled()) { - return; - } - - $this->cacheManager->clean([PageCacheType::TYPE_IDENTIFIER]); - $this->cacheManager->setEnabled([PageCacheType::TYPE_IDENTIFIER], false); - } - - /** - * Full Page Cache state. - * - * @return bool - */ - private function isFullPageCacheEnabled(): bool - { - $cacheStatus = $this->cacheManager->getStatus(); - - if (!array_key_exists(PageCacheType::TYPE_IDENTIFIER, $cacheStatus)) { - return false; - } - - return (bool)$cacheStatus[PageCacheType::TYPE_IDENTIFIER]; - } - - /** - * Restores Full Page Cache state. - * - * Returns FPC to previous state that was before maintenance mode turning on. - * - * @return void - */ - private function restoreFullPageCacheState(): void - { - $storedPageCacheState = $this->pageCacheStateStorage->isEnabled(); - $this->pageCacheStateStorage->flush(); - - if ($storedPageCacheState) { - $this->cacheManager->setEnabled([PageCacheType::TYPE_IDENTIFIER], true); - } - } -} diff --git a/app/code/Magento/PageCache/Observer/SwitchPageCacheOnMaintenance/PageCacheState.php b/app/code/Magento/PageCache/Observer/SwitchPageCacheOnMaintenance/PageCacheState.php index e4cadf728f2ea..5c52aa055ef16 100644 --- a/app/code/Magento/PageCache/Observer/SwitchPageCacheOnMaintenance/PageCacheState.php +++ b/app/code/Magento/PageCache/Observer/SwitchPageCacheOnMaintenance/PageCacheState.php @@ -14,6 +14,8 @@ /** * Page Cache state. + * + * @deprecated Originally used by now removed observer SwitchPageCacheOnMaintenance */ class PageCacheState { diff --git a/app/code/Magento/PageCache/Test/Unit/Model/Layout/LayoutPluginTest.php b/app/code/Magento/PageCache/Test/Unit/Model/Layout/LayoutPluginTest.php index 6c39fe1e7979c..e2bc7f237ab0a 100644 --- a/app/code/Magento/PageCache/Test/Unit/Model/Layout/LayoutPluginTest.php +++ b/app/code/Magento/PageCache/Test/Unit/Model/Layout/LayoutPluginTest.php @@ -27,6 +27,11 @@ class LayoutPluginTest extends \PHPUnit\Framework\TestCase */ protected $configMock; + /** + * @var \Magento\Framework\App\MaintenanceMode|\PHPUnit\Framework\MockObject\MockObject + */ + private $maintenanceModeMock; + protected function setUp() { $this->layoutMock = $this->getMockForAbstractClass( @@ -40,27 +45,33 @@ protected function setUp() ); $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class); $this->configMock = $this->createMock(\Magento\PageCache\Model\Config::class); + $this->maintenanceModeMock = $this->createMock(\Magento\Framework\App\MaintenanceMode::class); $this->model = new \Magento\PageCache\Model\Layout\LayoutPlugin( $this->responseMock, - $this->configMock + $this->configMock, + $this->maintenanceModeMock ); } /** * @param $cacheState * @param $layoutIsCacheable + * @param $maintenanceModeIsEnabled + * * @dataProvider afterGenerateXmlDataProvider */ - public function testAfterGenerateXml($cacheState, $layoutIsCacheable) + public function testAfterGenerateXml($cacheState, $layoutIsCacheable, $maintenanceModeIsEnabled) { $maxAge = 180; $result = 'test'; $this->layoutMock->expects($this->once())->method('isCacheable')->will($this->returnValue($layoutIsCacheable)); $this->configMock->expects($this->any())->method('isEnabled')->will($this->returnValue($cacheState)); + $this->maintenanceModeMock->expects($this->any())->method('isOn') + ->will($this->returnValue($maintenanceModeIsEnabled)); - if ($layoutIsCacheable && $cacheState) { + if ($layoutIsCacheable && $cacheState && !$maintenanceModeIsEnabled) { $this->configMock->expects($this->once())->method('getTtl')->will($this->returnValue($maxAge)); $this->responseMock->expects($this->once())->method('setPublicHeaders')->with($maxAge); } else { @@ -76,10 +87,11 @@ public function testAfterGenerateXml($cacheState, $layoutIsCacheable) public function afterGenerateXmlDataProvider() { return [ - 'Full_cache state is true, Layout is cache-able' => [true, true], - 'Full_cache state is true, Layout is not cache-able' => [true, false], - 'Full_cache state is false, Layout is not cache-able' => [false, false], - 'Full_cache state is false, Layout is cache-able' => [false, true] + 'Full_cache state is true, Layout is cache-able' => [true, true, false], + 'Full_cache state is true, Layout is not cache-able' => [true, false, false], + 'Full_cache state is false, Layout is not cache-able' => [false, false, false], + 'Full_cache state is false, Layout is cache-able' => [false, true, false], + 'Full_cache state is true, Layout is cache-able, Maintenance mode is enabled' => [true, true, true], ]; } diff --git a/app/code/Magento/PageCache/Test/Unit/Observer/SwitchPageCacheOnMaintenanceTest.php b/app/code/Magento/PageCache/Test/Unit/Observer/SwitchPageCacheOnMaintenanceTest.php deleted file mode 100644 index 2dbb815c70925..0000000000000 --- a/app/code/Magento/PageCache/Test/Unit/Observer/SwitchPageCacheOnMaintenanceTest.php +++ /dev/null @@ -1,164 +0,0 @@ -cacheManager = $this->createMock(Manager::class); - $this->pageCacheStateStorage = $this->createMock(PageCacheState::class); - $this->observer = $this->createMock(Observer::class); - - $this->model = $objectManager->getObject(SwitchPageCacheOnMaintenance::class, [ - 'cacheManager' => $this->cacheManager, - 'pageCacheStateStorage' => $this->pageCacheStateStorage, - ]); - } - - /** - * Tests execute when setting maintenance mode to on. - * - * @param array $cacheStatus - * @param bool $cacheState - * @param int $flushCacheCalls - * @return void - * @dataProvider enablingPageCacheStateProvider - */ - public function testExecuteWhileMaintenanceEnabling( - array $cacheStatus, - bool $cacheState, - int $flushCacheCalls - ): void { - $this->observer->method('getData') - ->with('isOn') - ->willReturn(true); - $this->cacheManager->method('getStatus') - ->willReturn($cacheStatus); - - // Page Cache state will be stored. - $this->pageCacheStateStorage->expects($this->once()) - ->method('save') - ->with($cacheState); - - // Page Cache will be cleaned and disabled - $this->cacheManager->expects($this->exactly($flushCacheCalls)) - ->method('clean') - ->with([PageCacheType::TYPE_IDENTIFIER]); - $this->cacheManager->expects($this->exactly($flushCacheCalls)) - ->method('setEnabled') - ->with([PageCacheType::TYPE_IDENTIFIER], false); - - $this->model->execute($this->observer); - } - - /** - * Tests execute when setting Maintenance Mode to off. - * - * @param bool $storedCacheState - * @param int $enableCacheCalls - * @return void - * @dataProvider disablingPageCacheStateProvider - */ - public function testExecuteWhileMaintenanceDisabling(bool $storedCacheState, int $enableCacheCalls): void - { - $this->observer->method('getData') - ->with('isOn') - ->willReturn(false); - - $this->pageCacheStateStorage->method('isEnabled') - ->willReturn($storedCacheState); - - // Nullify Page Cache state. - $this->pageCacheStateStorage->expects($this->once()) - ->method('flush'); - - // Page Cache will be enabled. - $this->cacheManager->expects($this->exactly($enableCacheCalls)) - ->method('setEnabled') - ->with([PageCacheType::TYPE_IDENTIFIER]); - - $this->model->execute($this->observer); - } - - /** - * Page Cache state data provider. - * - * @return array - */ - public function enablingPageCacheStateProvider(): array - { - return [ - 'page_cache_is_enable' => [ - 'cache_status' => [PageCacheType::TYPE_IDENTIFIER => 1], - 'cache_state' => true, - 'flush_cache_calls' => 1, - ], - 'page_cache_is_missing_in_system' => [ - 'cache_status' => [], - 'cache_state' => false, - 'flush_cache_calls' => 0, - ], - 'page_cache_is_disable' => [ - 'cache_status' => [PageCacheType::TYPE_IDENTIFIER => 0], - 'cache_state' => false, - 'flush_cache_calls' => 0, - ], - ]; - } - - /** - * Page Cache state data provider. - * - * @return array - */ - public function disablingPageCacheStateProvider(): array - { - return [ - ['stored_cache_state' => true, 'enable_cache_calls' => 1], - ['stored_cache_state' => false, 'enable_cache_calls' => 0], - ]; - } -} diff --git a/app/code/Magento/PageCache/etc/events.xml b/app/code/Magento/PageCache/etc/events.xml index 3f0a2532ae60a..7584f5f36d69c 100644 --- a/app/code/Magento/PageCache/etc/events.xml +++ b/app/code/Magento/PageCache/etc/events.xml @@ -57,7 +57,4 @@ - - -