diff --git a/src/services/CacheRequestService.php b/src/services/CacheRequestService.php index b4640b7e..2728246f 100755 --- a/src/services/CacheRequestService.php +++ b/src/services/CacheRequestService.php @@ -468,8 +468,9 @@ public function getCachedResponse(SiteUriModel $siteUri): ?Response } $response = $event->response; + $response->content = $content; $this->addCraftHeaders($response); - $this->prepareResponse($response, $siteUri, $content, $encoded); + $this->prepareResponse($response, $siteUri, $encoded); $this->appendServedByComment($response, $siteUri, $encoded); if ($this->hasEventHandlers(self::EVENT_AFTER_GET_RESPONSE)) { @@ -502,7 +503,8 @@ public function saveAndPrepareResponse(?Response $response, SiteUriModel $siteUr $content = Blitz::$plugin->generateCache->save($response->content, $siteUri); if ($content) { - $this->prepareResponse($response, $siteUri, $content); + $response->content = $content; + $this->prepareResponse($response, $siteUri); } } @@ -668,10 +670,8 @@ private function addCraftHeaders(Response $response): void * * @since 3.12.0 */ - private function prepareResponse(Response $response, SiteUriModel $siteUri, string $content, bool $encoded = false): void + private function prepareResponse(Response $response, SiteUriModel $siteUri, bool $encoded = false): void { - $response->content = $content; - $cacheControlHeader = Blitz::$plugin->settings->cacheControlHeader; if ($this->getIsExpiredSiteUri($siteUri)) { diff --git a/tests/TESTS.md b/tests/TESTS.md index b00ced4b..8f7b7d91 100644 --- a/tests/TESTS.md +++ b/tests/TESTS.md @@ -4,17 +4,21 @@ This document outlines the test specification for the Blitz plugin. --- +## Feature Tests + +### [GenerateCache](pest/Feature/GenerateCacheTest.php) + +_Tests the saving of cached values, element cache records and element query records._ + +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved with output comments. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved without output comments. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved with output comments when file extension is ".html". +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Cached value is saved without output comments when file extension is not `.html`. + ## Interface Tests ### [WebResponse](pest/Interface/WebResponseTest.php) _Tests that cached web responses contain the correct headers and comments._ -![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains the default cache control header when the page is not cacheable. -![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains the cache control header when the page is cacheable. -![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains the expired cache control header and the cache is refreshed when the page is expired. -![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response adds the powered by header. -![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response contains output comments when enabled. -![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response does not contain output comments when disabled. ![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response with mime type has headers and does not contain output comments. -![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Response is encoded when compression is enabled. diff --git a/tests/pest/Interface/WebResponseTest.php b/tests/pest/Interface/WebResponseTest.php index e85d0324..590b79a8 100644 --- a/tests/pest/Interface/WebResponseTest.php +++ b/tests/pest/Interface/WebResponseTest.php @@ -12,6 +12,7 @@ beforeEach(function() { Blitz::$plugin->cacheStorage->deleteAll(); + Blitz::$plugin->generateCache->options->outputComments = null; Blitz::$plugin->set('refreshCache', Mockery::mock(RefreshCacheService::class . '[refresh]')); }); @@ -60,25 +61,27 @@ ->toContainEqual('Blitz'); }); -test('Response contains output comments when enabled', function() { - foreach ([true, SettingsModel::OUTPUT_COMMENTS_SERVED] as $value) { - Blitz::$plugin->settings->outputComments = $value; - $response = sendRequest(); +test('Response contains output comments when enabled', function(bool|int $value) { + Blitz::$plugin->settings->outputComments = $value; + $response = sendRequest(); - expect($response->content) - ->toContain('Cached by Blitz'); - } -}); + expect($response->content) + ->toContain('Cached by Blitz'); +})->with([ + 'true' => true, + 'SettingsModel::OUTPUT_COMMENTS_CACHED' => SettingsModel::OUTPUT_COMMENTS_CACHED, +]); -test('Response does not contain output comments when disabled', function() { - foreach ([false, SettingsModel::OUTPUT_COMMENTS_CACHED] as $value) { - Blitz::$plugin->settings->outputComments = $value; - $response = sendRequest(); +test('Response does not contain output comments when disabled', function(bool|int $value) { + Blitz::$plugin->settings->outputComments = $value; + $response = sendRequest(); - expect($response->content) - ->not()->toContain('Served by Blitz on'); - } -}); + expect($response->content) + ->not()->toContain('Cached by Blitz'); +})->with([ + 'false' => false, + 'SettingsModel::OUTPUT_COMMENTS_SERVED' => SettingsModel::OUTPUT_COMMENTS_SERVED, +]); test('Response with mime type has headers and does not contain output comments', function() { $output = createOutput();