/** * @param \Magento\Framework\Controller\ResultInterface $subject * @param callable $proceed * @param ResponseHttp $response * @return \Magento\Framework\Controller\ResultInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundRenderResult(\Magento\Framework\Controller\ResultInterface $subject, \Closure $proceed, ResponseHttp $response) { $result = $proceed($response); $usePlugin = $this->registry->registry('use_page_cache_plugin'); if (!$usePlugin || !$this->config->isEnabled() || $this->config->getType() != \Magento\PageCache\Model\Config::BUILT_IN) { return $result; } if ($this->state->getMode() == \Magento\Framework\App\State::MODE_DEVELOPER) { $cacheControlHeader = $response->getHeader('Cache-Control'); if ($cacheControlHeader instanceof \Zend\Http\Header\HeaderInterface) { $response->setHeader('X-Magento-Cache-Control', $cacheControlHeader->getFieldValue()); } $response->setHeader('X-Magento-Cache-Debug', 'MISS', true); } $tagsHeader = $response->getHeader('X-Magento-Tags'); $tags = []; if ($tagsHeader) { $tags = explode(',', $tagsHeader->getFieldValue()); $response->clearHeader('X-Magento-Tags'); } $tags = array_unique(array_merge($tags, [\Magento\PageCache\Model\Cache\Type::CACHE_TAG])); $response->setHeader('X-Magento-Tags', implode(',', $tags)); $this->kernel->process($response); return $result; }
/** * @param \Magento\Framework\App\FrontControllerInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return \Magento\Framework\Controller\ResultInterface|\Magento\Framework\App\Response\Http * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundDispatch(\Magento\Framework\App\FrontControllerInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request) { if (!$this->config->isEnabled() || $this->config->getType() != \Magento\PageCache\Model\Config::BUILT_IN) { return $proceed($request); } $this->version->process(); $result = $this->kernel->load(); if ($result === false) { $result = $proceed($request); if ($result instanceof ResponseHttp) { $this->addDebugHeaders($result); $this->kernel->process($result); } } else { $this->addDebugHeader($result, 'X-Magento-Cache-Debug', 'HIT', true); } return $result; }
/** * @param \Magento\Framework\App\FrontControllerInterface $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * @return false|\Magento\Framework\App\Response\Http */ public function aroundDispatch(\Magento\Framework\App\FrontControllerInterface $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request) { if ($this->config->getType() == \Magento\PageCache\Model\Config::BUILT_IN && $this->config->isEnabled()) { $this->version->process(); $response = $this->kernel->load(); if ($response === false) { $response = $proceed($request); $cacheControl = $response->getHeader('Cache-Control')['value']; $this->addDebugHeader($response, 'X-Magento-Cache-Control', $cacheControl); $this->kernel->process($response); $this->addDebugHeader($response, 'X-Magento-Cache-Debug', 'MISS'); } else { $this->addDebugHeader($response, 'X-Magento-Cache-Debug', 'HIT'); } } else { return $response = $proceed($request); } return $response; }
/** * @dataProvider processNotSaveCacheProvider * @param string $cacheControlHeader * @param int $httpCode * @param bool $isGet * @param bool $overrideHeaders */ public function testProcessNotSaveCache($cacheControlHeader, $httpCode, $isGet, $overrideHeaders) { $header = \Zend\Http\Header\CacheControl::fromString("Cache-Control: {$cacheControlHeader}"); $this->responseMock->expects($this->once())->method('getHeader')->with('Cache-Control')->will($this->returnValue($header)); $this->responseMock->expects($this->any())->method('getHttpResponseCode')->will($this->returnValue($httpCode)); $this->requestMock->expects($this->any())->method('isGet')->will($this->returnValue($isGet)); if ($overrideHeaders) { $this->responseMock->expects($this->once())->method('setNoCacheHeaders'); } $this->fullPageCacheMock->expects($this->never())->method('save'); $this->kernel->process($this->responseMock); }
public function testAroundResultWithPluginDeveloperMode() { $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->willReturn(true); $this->state->expects($this->once())->method('getMode')->willReturn(\Magento\Framework\App\State::MODE_DEVELOPER); $this->header->expects($this->any())->method('getFieldValue')->willReturnOnConsecutiveCalls('test', 'tag,tag2'); $this->response->expects($this->any())->method('setHeader')->withConsecutive(['X-Magento-Cache-Control', 'test'], ['X-Magento-Cache-Debug', 'MISS', true], ['X-Magento-Tags', 'tag,tag2,' . \Magento\PageCache\Model\Cache\Type::CACHE_TAG]); $this->response->expects($this->once())->method('clearHeader')->with('X-Magento-Tags'); $this->registry->expects($this->once())->method('registry')->with('use_page_cache_plugin')->will($this->returnValue(true)); $this->kernel->expects($this->once())->method('process')->with($this->response); $result = call_user_func($this->closure); $this->assertSame($result, $this->plugin->aroundRenderResult($this->subject, $this->closure, $this->response)); }
/** * @dataProvider dataProvider */ public function testAroundDispatchReturnsCache($state) { $this->configMock->expects($this->once())->method('getType')->will($this->returnValue(\Magento\PageCache\Model\Config::BUILT_IN)); $this->configMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->versionMock->expects($this->once())->method('process'); $this->kernelMock->expects($this->once())->method('load')->will($this->returnValue($this->responseMock)); $this->stateMock->expects($this->any())->method('getMode')->will($this->returnValue($state)); if ($state == \Magento\Framework\App\State::MODE_DEVELOPER) { $this->responseMock->expects($this->once())->method('setHeader')->with('X-Magento-Cache-Debug'); } else { $this->responseMock->expects($this->never())->method('setHeader'); } $this->plugin->aroundDispatch($this->frontControllerMock, $this->closure, $this->requestMock); }