diff --git a/Model/Layout/LayoutPlugin.php b/Model/Layout/LayoutPlugin.php index 6447582b..9bf4dd8e 100644 --- a/Model/Layout/LayoutPlugin.php +++ b/Model/Layout/LayoutPlugin.php @@ -77,14 +77,16 @@ public function afterGenerateElements(\Magento\Framework\View\Layout $subject): } } - /* - * Surface the cacheability of a page. This may expose things like page blocks being set to - * cacheable = false which makes the whole page uncacheable - */ - if ($subject->isCacheable()) { - $this->response->setHeader("fastly-page-cacheable", "YES"); - } else { - $this->response->setHeader("fastly-page-cacheable", "NO"); + if (empty($this->response->getHeader('fastly-page-cacheable'))) { + /* + * Surface the cacheability of a page. This may expose things like page blocks being set to + * cacheable = false which makes the whole page uncacheable + */ + if ($subject->isCacheable()) { + $this->response->setHeader("fastly-page-cacheable", "YES"); + } else { + $this->response->setHeader("fastly-page-cacheable", "NO"); + } } } diff --git a/Test/Unit/Model/Layout/LayoutPluginTest.php b/Test/Unit/Model/Layout/LayoutPluginTest.php index caa23ea9..a542418b 100644 --- a/Test/Unit/Model/Layout/LayoutPluginTest.php +++ b/Test/Unit/Model/Layout/LayoutPluginTest.php @@ -112,6 +112,7 @@ public function testAfterGenerateElements($cacheState, $layoutIsCacheable, $cach $staleErrorTtl = 0, $cacheControl = 'max-age=86400, public, s-maxage=86400'): void { $headerName = 'cache-control'; + $cacheableHeaderName = 'fastly-page-cacheable'; $this->layoutMock->expects($this->any() )->method('isCacheable' @@ -132,11 +133,18 @@ public function testAfterGenerateElements($cacheState, $layoutIsCacheable, $cach if ($layoutIsCacheable && $cacheState && $cacheType == \Fastly\Cdn\Model\Config::FASTLY && $ttl > 0) { if (!empty($cacheControl)) { $cacheControlHeader = new GenericHeader($headerName, $cacheControl); - - $this->responseMock->expects($this->once() - )->method('getHeader' - )->with($headerName - )->will($this->returnValue($cacheControlHeader)); + $cacheableHeader = new GenericHeader($cacheableHeaderName, 'YES'); + + $this->responseMock->expects($this->exactly(2)) + ->method('getHeader') + ->withConsecutive( + [$headerName], + [$cacheableHeaderName] + ) + ->willReturnOnConsecutiveCalls( + $this->returnValue($cacheControlHeader), + $this->returnValue($cacheableHeader) + ); $this->configMock->expects($this->once() )->method('getStaleTtl' @@ -157,19 +165,27 @@ public function testAfterGenerateElements($cacheState, $layoutIsCacheable, $cach $value = sprintf(', stale-while-revalidate=%s', $staleTtl); } - $this->responseMock->expects($this->exactly(2) + // change to once since 'fastly-page-cacheable' is already set in this mock, so method won't set it again + $this->responseMock->expects($this->once() )->method('setHeader'); //->with($headerName, $cacheControl . $value, true); } else { - $this->responseMock->expects($this->once() - )->method('getHeader' - )->with($headerName - )->will($this->returnValue(false)); - - $this->responseMock->expects($this->once() - )->method('setHeader'); + $this->responseMock->expects($this->exactly(2)) + ->method('getHeader') + ->withConsecutive( + [$headerName], + [$cacheableHeaderName] + ) + ->willReturnOnConsecutiveCalls( + $this->returnValue(false), + $this->returnValue('NO') + ); + + // Removed because 'cache-control' is not set in this case and 'fastly-page-cacheable' is already set + // in mock, so second setHeader is skipped + //$this->responseMock->expects($this->once())->method('setHeader'); } } else { $this->responseMock->expects($this->once()