diff --git a/core/lib/Drupal/Core/Cache/CacheItem.php b/core/lib/Drupal/Core/Cache/CacheItem.php index b9d1704ffc..e0fcb86253 100644 --- a/core/lib/Drupal/Core/Cache/CacheItem.php +++ b/core/lib/Drupal/Core/Cache/CacheItem.php @@ -132,7 +132,7 @@ public function isExpired() { return FALSE; } else { - return time() >= $_SERVER['REQUEST_TIME']; + return \Drupal::time()->getCurrentTime() >= $this->expire; } } diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheItemTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheItemTest.php new file mode 100644 index 0000000000..956208a76b --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Cache/CacheItemTest.php @@ -0,0 +1,81 @@ + 'owls']); + $this->assertSame('foo', $cache_item->cid); + $this->assertSame('foo', $cache_item->getKey()); + $this->assertSame(0, $cache_item->created); + $this->assertSame(0, $cache_item->getCreationTimestamp()); + $this->assertSame(['animals' => 'owls'], $cache_item->data); + $this->assertSame(['animals' => 'owls'], $cache_item->getData()); + $this->assertSame(CacheBackendInterface::CACHE_PERMANENT, $cache_item->expire); + $this->assertSame(CacheBackendInterface::CACHE_PERMANENT, $cache_item->getExpirationTimestamp()); + $this->assertSame([], $cache_item->tags); + $this->assertSame([], $cache_item->getTags()); + $this->assertFalse($cache_item->valid); + $this->assertFalse($cache_item->isValid()); + } + + /** + * Tests the isExpired method. + * + * @param int $expire + * The expiration time. + * @param bool $expected + * A boolean to indicate if the item should be expired. + * + * @covers ::isExpired + * @dataProvider provideExpirationData + */ + public function testExpiration($expire, $expected) { + $time_service = $this->prophesize(TimeInterface::class); + $time_service->getCurrentTime()->willReturn(200); + + $container = new ContainerBuilder(); + $container->set('datetime.time', $time_service->reveal()); + \Drupal::setContainer($container); + + $cache_item = new CacheItem('bar', 'More owls', $expire); + $this->assertSame($expected, $cache_item->isExpired()); + } + + /** + * Provides test data. + * + * @return array + */ + public function provideExpirationData() { + return [ + [CacheBackendInterface::CACHE_PERMANENT, FALSE], + [CacheBackendInterface::CACHE_PERMANENT, FALSE], + [10, TRUE], + [1000, FALSE], + ]; + } + +}