From aa1ba0ef90109fe084a8d736ed4ca08b32a4f40b Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Wed, 13 Jun 2018 13:56:47 +1200 Subject: [PATCH] Fix inverted condition Remove unnecessary yml block Deprecate HTTP::set_cache_age() --- _config/requestprocessors.yml | 9 --------- src/Control/HTTP.php | 5 ++++- .../Middleware/HTTPCacheControlMiddleware.php | 2 +- tests/php/Control/HTTPTest.php | 19 ++++++++----------- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/_config/requestprocessors.yml b/_config/requestprocessors.yml index 056f93e7e..c76471ccd 100644 --- a/_config/requestprocessors.yml +++ b/_config/requestprocessors.yml @@ -47,12 +47,3 @@ SilverStripe\Core\Injector\Injector: properties: ForceSSL: false ForceWWW: false ---- -Name: httpcache-dev -Only: - environment: dev ---- -SilverStripe\Core\Injector\Injector: - SilverStripe\Control\Middleware\HTTPCacheControlMiddleware: - Properties: - Enabled: false diff --git a/src/Control/HTTP.php b/src/Control/HTTP.php index ee981bc3b..e8023f1e7 100644 --- a/src/Control/HTTP.php +++ b/src/Control/HTTP.php @@ -9,6 +9,7 @@ use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Convert; use InvalidArgumentException; use finfo; +use SilverStripe\Dev\Deprecation; /** * A class with HTTP-related helpers. Like Debug, this is more a bundle of methods than a class. @@ -356,12 +357,14 @@ class HTTP /** * Set the maximum age of this page in web caches, in seconds. * + * @deprecated 4.2..5.0 Use HTTPCacheControlMiddleware::singleton()->setMaxAge($age) instead * @param int $age */ public static function set_cache_age($age) { + Deprecation::notice('5.0', 'Use HTTPCacheControlMiddleware::singleton()->setMaxAge($age) instead'); self::$cache_age = $age; - HTTPCacheControlMiddleware::singleton()->setMaxAge(self::$cache_age); + HTTPCacheControlMiddleware::singleton()->setMaxAge($age); } /** diff --git a/src/Control/Middleware/HTTPCacheControlMiddleware.php b/src/Control/Middleware/HTTPCacheControlMiddleware.php index c7c5c06d3..54adc19f6 100644 --- a/src/Control/Middleware/HTTPCacheControlMiddleware.php +++ b/src/Control/Middleware/HTTPCacheControlMiddleware.php @@ -499,7 +499,7 @@ class HTTPCacheControlMiddleware implements HTTPMiddleware, Resettable public function publicCache($force = false) { // Only execute this if its forcing level is high enough - if (!$this->applyChangeLevel(self::LEVEL_PUBLIC, $force)) { + if ($this->applyChangeLevel(self::LEVEL_PUBLIC, $force)) { $this->setState(self::STATE_PUBLIC); } return $this; diff --git a/tests/php/Control/HTTPTest.php b/tests/php/Control/HTTPTest.php index 28609c956..a99bf6311 100644 --- a/tests/php/Control/HTTPTest.php +++ b/tests/php/Control/HTTPTest.php @@ -2,6 +2,7 @@ namespace SilverStripe\Control\Tests; +use PHPUnit_Framework_Error_Warning; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Control\HTTP; @@ -9,8 +10,6 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; use SilverStripe\Core\Config\Config; -use SilverStripe\Core\Injector\Injector; -use SilverStripe\Core\Kernel; use SilverStripe\Dev\FunctionalTest; /** @@ -33,18 +32,16 @@ class HTTPTest extends FunctionalTest $body = "

Mysite

"; $response = new HTTPResponse($body, 200); HTTPCacheControlMiddleware::singleton()->publicCache(); - HTTP::set_cache_age(30); + HTTPCacheControlMiddleware::singleton()->setMaxAge(30); HTTP::add_cache_headers($response); $this->assertNotEmpty($response->getHeader('Cache-Control')); - /** @var Kernel $kernel */ - $kernel = Injector::inst()->get(Kernel::class); // Ensure cache headers are set correctly when disabled via config (e.g. when dev) Config::modify()->set(HTTP::class, 'disable_http_cache', true); HTTPCacheControlMiddleware::reset(); HTTPCacheControlMiddleware::singleton()->publicCache(); - HTTP::set_cache_age(30); + HTTPCacheControlMiddleware::singleton()->setMaxAge(30); $response = new HTTPResponse($body, 200); HTTP::add_cache_headers($response); $this->assertContains('no-cache', $response->getHeader('Cache-Control')); @@ -55,7 +52,7 @@ class HTTPTest extends FunctionalTest Config::modify()->remove(HTTP::class, 'disable_http_cache'); HTTPCacheControlMiddleware::reset(); HTTPCacheControlMiddleware::singleton()->publicCache(); - HTTP::set_cache_age(30); + HTTPCacheControlMiddleware::singleton()->setMaxAge(30); $response = new HTTPResponse($body, 200); HTTP::add_cache_headers($response); $this->assertContains('max-age=30', $response->getHeader('Cache-Control')); @@ -69,14 +66,14 @@ class HTTPTest extends FunctionalTest ); HTTPCacheControlMiddleware::reset(); HTTPCacheControlMiddleware::singleton()->publicCache(); - HTTP::set_cache_age(30); + HTTPCacheControlMiddleware::singleton()->setMaxAge(30); $response = new HTTPResponse($body, 200); foreach ($headers as $name => $value) { $response->addHeader($name, $value); } // Expect a warning if the header is already set - $this->expectException( - \PHPUnit_Framework_Error_Warning::class, + $this->expectException(PHPUnit_Framework_Error_Warning::class); + $this->expectExceptionMessage( 'Cache-Control header has already been set. ' . 'Please use HTTPCacheControl API to set caching options instead.' ); @@ -88,7 +85,7 @@ class HTTPTest extends FunctionalTest { $body = "

Mysite

"; $response = new HTTPResponse($body, 200); - HTTP::set_cache_age(30); + HTTPCacheControlMiddleware::singleton()->setMaxAge(30); HTTP::add_cache_headers($response); $v = $response->getHeader('Vary');