Skip to content

Instantly share code, notes, and snippets.

@sherakama
Last active August 3, 2017 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sherakama/60de8073403fec0af2d21caba06346f5 to your computer and use it in GitHub Desktop.
Save sherakama/60de8073403fec0af2d21caba06346f5 to your computer and use it in GitHub Desktop.
Patch test
diff --git a/core/modules/page_cache/src/StackMiddleware/PageCache.php b/core/modules/page_cache/src/StackMiddleware/PageCache.php
index 6eb4742..ba495fd 100644
--- a/core/modules/page_cache/src/StackMiddleware/PageCache.php
+++ b/core/modules/page_cache/src/StackMiddleware/PageCache.php
@@ -283,10 +283,15 @@ protected function storeResponse(Request $request, Response $response) {
$expire = $request_time + $cache_ttl_4xx;
}
}
- else {
- $date = $response->getExpires()->getTimestamp();
+ // The getExpires method could return NULL if Expires header is not set, so
+ // the returned value needs to be checked before calling getTimestamp.
+ elseif ($expires = $response->getExpires()) {
+ $date = $expires->getTimestamp();
$expire = ($date > $request_time) ? $date : Cache::PERMANENT;
}
+ else {
+ $expire = Cache::PERMANENT;
+ }
if ($expire === Cache::PERMANENT || $expire > $request_time) {
$tags = $response->getCacheableMetadata()->getCacheTags();
diff --git a/core/modules/page_cache/src/Tests/PageCacheStoreResponseTest.php b/core/modules/page_cache/src/Tests/PageCacheStoreResponseTest.php
new file mode 100644
index 0000000..9b212f6
--- /dev/null
+++ b/core/modules/page_cache/src/Tests/PageCacheStoreResponseTest.php
@@ -0,0 +1,119 @@
+<?php
+
+namespace Drupal\page_cache\Tests;
+
+use Drupal\Core\Cache\CacheBackendInterface;
+use Drupal\Core\Cache\CacheableResponse;
+use Drupal\Core\PageCache\RequestPolicyInterface;
+use Drupal\Core\PageCache\ResponsePolicyInterface;
+use Drupal\Tests\UnitTestCase;
+use Drupal\page_cache\StackMiddleware\PageCache;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpKernel\HttpKernelInterface;
+
+/**
+ * @coversDefaultClass \Drupal\page_cache\StackMiddleware\PageCache
+ * @group page_cache
+ */
+class PageCacheStoreResponseTest extends UnitTestCase {
+
+ /**
+ * The cache backend used in the test.
+ *
+ * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $cache;
+
+ /**
+ * The mocked HTTP kernel.
+ *
+ * @var \Symfony\Component\HttpKernel\HttpKernelInterface|PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $kernel;
+
+ /**
+ * The accessible reflection of the tested protected method.
+ *
+ * @var \ReflectionMethod
+ */
+ protected $method;
+
+ /**
+ * The mocked request policy.
+ *
+ * @var \Drupal\Core\PageCache\RequestPolicyInterface|PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $requestPolicy;
+
+ /**
+ * The mocked response policy.
+ *
+ * @var \Drupal\Core\PageCache\ResponsePolicyInterface|PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $responsePolicy;
+
+ /**
+ * The tested page cache middleware.
+ *
+ * @var \Drupal\page_cache\StackMiddleware\PageCache
+ */
+ protected $pageCache;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUp() {
+ $this->cache = $this->getMock(CacheBackendInterface::class);
+ $this->kernel = $this->getMock(HttpKernelInterface::class);
+ $this->requestPolicy = $this->getMock(RequestPolicyInterface::class);
+ $this->responsePolicy = $this->getMock(ResponsePolicyInterface::class);
+
+ $this->pageCache = new PageCache(
+ $this->kernel,
+ $this->cache,
+ $this->requestPolicy,
+ $this->responsePolicy
+ );
+
+ $this->method = new \ReflectionMethod(PageCache::class, 'storeResponse');
+ $this->method->setAccessible(TRUE);
+ }
+
+ /**
+ * Tests that storeResponse method works in general without any data manipulations.
+ */
+ public function testSimpleInvocation() {
+ $request = new Request();
+ $response = new CacheableResponse();
+
+ // Expect backend cache to be set.
+ $this->cache->expects($this->once())->method('set');
+
+ $this->assertTrue($this->method->invoke($this->pageCache, $request, $response));
+ }
+
+ /**
+ * Tests handling of NULL value returned from Response::getExpires.
+ */
+ public function testResponseWithoutExpiresHeader() {
+ $request = new Request();
+
+ $response = $this->getMock(CacheableResponse::class, ['getExpires']);
+ $response->expects($this->any())
+ ->method('getExpires')
+ ->will($this->onConsecutiveCalls(
+ NULL,
+ new \DateTime('-1 day'),
+ new \DateTime(),
+ new \DateTime('1 day')
+ ));
+
+ // Expect backend cache to be set.
+ $this->cache->expects($this->exactly(4))->method('set');
+
+ for ($i = 0; $i < 4; $i++) {
+ $this->assertTrue($this->method->invoke($this->pageCache, $request, $response));
+ }
+ }
+
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment