Commit 81a69790 authored by webchick's avatar webchick

Issue #2459155 by alexpott, pfrenssen, hussainweb, neclimdul: Remove...

Issue #2459155 by alexpott, pfrenssen, hussainweb, neclimdul: Remove REQUEST_TIME from bootstrap.php
parent cc32fa51
......@@ -94,7 +94,7 @@ protected function prepareItem($cache, $allow_invalid) {
$prepared->data = unserialize($prepared->data);
// Check expire time.
$prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= REQUEST_TIME;
$prepared->valid = $prepared->expire == Cache::PERMANENT || $prepared->expire >= $this->getRequestTime();
if (!$allow_invalid && !$prepared->valid) {
return FALSE;
......@@ -114,7 +114,7 @@ public function set($cid, $data, $expire = Cache::PERMANENT, array $tags = array
$this->cache[$cid] = (object) array(
'cid' => $cid,
'data' => serialize($data),
'created' => REQUEST_TIME,
'created' => $this->getRequestTime(),
'expire' => $expire,
'tags' => $tags,
);
......@@ -155,7 +155,7 @@ public function deleteAll() {
*/
public function invalidate($cid) {
if (isset($this->cache[$cid])) {
$this->cache[$cid]->expire = REQUEST_TIME - 1;
$this->cache[$cid]->expire = $this->getRequestTime() - 1;
}
}
......@@ -164,7 +164,7 @@ public function invalidate($cid) {
*/
public function invalidateMultiple(array $cids) {
foreach ($cids as $cid) {
$this->cache[$cid]->expire = REQUEST_TIME - 1;
$this->cache[$cid]->expire = $this->getRequestTime() - 1;
}
}
......@@ -174,7 +174,7 @@ public function invalidateMultiple(array $cids) {
public function invalidateTags(array $tags) {
foreach ($this->cache as $cid => $item) {
if (array_intersect($tags, $item->tags)) {
$this->cache[$cid]->expire = REQUEST_TIME - 1;
$this->cache[$cid]->expire = $this->getRequestTime() - 1;
}
}
}
......@@ -184,7 +184,7 @@ public function invalidateTags(array $tags) {
*/
public function invalidateAll() {
foreach ($this->cache as $cid => $item) {
$this->cache[$cid]->expire = REQUEST_TIME - 1;
$this->cache[$cid]->expire = $this->getRequestTime() - 1;
}
}
......@@ -201,6 +201,15 @@ public function removeBin() {
$this->cache = [];
}
/**
* Wrapper method for REQUEST_TIME constant.
*
* @return int
*/
protected function getRequestTime() {
return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
}
/**
* Prevents data stored in memory backends from being serialized.
*/
......
......@@ -144,7 +144,7 @@ public function writeCache() {
if (!empty($path_lookups)) {
$twenty_four_hours = 60 * 60 * 24;
$this->cache->set($this->cacheKey, $path_lookups, REQUEST_TIME + $twenty_four_hours);
$this->cache->set($this->cacheKey, $path_lookups, $this->getRequestTime() + $twenty_four_hours);
}
}
}
......@@ -286,4 +286,13 @@ protected function pathAliasWhitelistRebuild($path = NULL) {
}
$this->whitelist->clear();
}
/**
* Wrapper method for REQUEST_TIME constant.
*
* @return int
*/
protected function getRequestTime() {
return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
}
}
......@@ -571,7 +571,7 @@ protected function cacheSet(array &$elements, $pre_bubbling_cid) {
$data = $this->getCacheableRenderArray($elements);
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';
$expire = ($elements['#cache']['max-age'] === Cache::PERMANENT) ? Cache::PERMANENT : REQUEST_TIME + $elements['#cache']['max-age'];
$expire = ($elements['#cache']['max-age'] === Cache::PERMANENT) ? Cache::PERMANENT : (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME') + $elements['#cache']['max-age'];
$cache = $this->cacheFactory->get($bin);
// Two-tier caching: detect different CID post-bubbling, create redirect,
......
......@@ -354,7 +354,7 @@ public function testTimeOptionExceeded() {
$this->executable->setTimeElapsed(1);
$this->assertTrue($this->executable->timeOptionExceeded());
// Assert time limit not exceeded.
$this->executable->limit = array('unit' => 'seconds', 'value' => (REQUEST_TIME - 3600));
$this->executable->limit = array('unit' => 'seconds', 'value' => (int) $_SERVER['REQUEST_TIME'] - 3600);
$this->assertFalse($this->executable->timeOptionExceeded());
// Assert no time limit.
$this->executable->limit = array();
......
......@@ -132,7 +132,7 @@ public function set($key, $value) {
$value = (object) array(
'owner' => $this->getOwner(),
'data' => $value,
'updated' => REQUEST_TIME,
'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'),
);
$this->storage->setWithExpire($key, $value, $this->expire);
$this->lockBackend->release($key);
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Stores and retrieves temporary data for a given owner.
......@@ -56,6 +57,13 @@ class SharedTempStore {
*/
protected $lockBackend;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The owner key to store along with the data (e.g. a user or session ID).
*
......@@ -83,13 +91,16 @@ class SharedTempStore {
* The lock object used for this data.
* @param mixed $owner
* The owner key to store along with the data (e.g. a user or session ID).
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param int $expire
* The time to live for items, in seconds.
*/
public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lockBackend, $owner, $expire = 604800) {
public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lockBackend, $owner, RequestStack $request_stack, $expire = 604800) {
$this->storage = $storage;
$this->lockBackend = $lockBackend;
$this->owner = $owner;
$this->requestStack = $request_stack;
$this->expire = $expire;
}
......@@ -140,7 +151,7 @@ public function setIfNotExists($key, $value) {
$value = (object) array(
'owner' => $this->owner,
'data' => $value,
'updated' => REQUEST_TIME,
'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'),
);
return $this->storage->setWithExpireIfNotExists($key, $value, $this->expire);
}
......@@ -195,7 +206,7 @@ public function set($key, $value) {
$value = (object) array(
'owner' => $this->owner,
'data' => $value,
'updated' => REQUEST_TIME,
'updated' => (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME'),
);
$this->storage->setWithExpire($key, $value, $this->expire);
$this->lockBackend->release($key);
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Creates a shared temporary storage for a collection.
......@@ -30,6 +31,13 @@ class SharedTempStoreFactory {
*/
protected $lockBackend;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The time to live for items in seconds.
*
......@@ -44,12 +52,15 @@ class SharedTempStoreFactory {
* The connection object used for this data.
* @param \Drupal\Core\Lock\LockBackendInterface $lockBackend
* The lock object used for this data.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param int $expire
* The time to live for items, in seconds.
*/
function __construct(KeyValueExpirableFactoryInterface $storage_factory, LockBackendInterface $lockBackend, $expire = 604800) {
function __construct(KeyValueExpirableFactoryInterface $storage_factory, LockBackendInterface $lockBackend, RequestStack $request_stack, $expire = 604800) {
$this->storageFactory = $storage_factory;
$this->lockBackend = $lockBackend;
$this->requestStack = $request_stack;
$this->expire = $expire;
}
......@@ -76,7 +87,7 @@ function get($collection, $owner = NULL) {
// Store the data for this collection in the database.
$storage = $this->storageFactory->get("user.shared_tempstore.$collection");
return new SharedTempStore($storage, $this->lockBackend, $owner, $this->expire);
return new SharedTempStore($storage, $this->lockBackend, $owner, $this->requestStack, $this->expire);
}
}
......@@ -76,7 +76,7 @@ protected function setUp() {
*/
public function testUserTempStore() {
// Create a key/value collection.
$factory = new SharedTempStoreFactory(new KeyValueExpirableFactory(\Drupal::getContainer()), new DatabaseLockBackend(Database::getConnection()));
$factory = new SharedTempStoreFactory(new KeyValueExpirableFactory(\Drupal::getContainer()), new DatabaseLockBackend(Database::getConnection()), $this->container->get('request_stack'));
$collection = $this->randomMachineName();
// Create two mock users.
......
......@@ -9,6 +9,7 @@
use Drupal\Tests\UnitTestCase;
use Drupal\user\PrivateTempStore;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
......@@ -80,13 +81,15 @@ protected function setUp() {
->willReturn(1);
$this->requestStack = new RequestStack();
$request = Request::createFromGlobals();
$this->requestStack->push($request);
$this->tempStore = new PrivateTempStore($this->keyValue, $this->lock, $this->currentUser, $this->requestStack, 604800);
$this->ownObject = (object) array(
'data' => 'test_data',
'owner' => $this->currentUser->id(),
'updated' => REQUEST_TIME,
'updated' => (int) $request->server->get('REQUEST_TIME'),
);
// Clone the object but change the owner.
......
......@@ -9,6 +9,8 @@
use Drupal\Tests\UnitTestCase;
use Drupal\user\SharedTempStore;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @coversDefaultClass \Drupal\user\SharedTempStore
......@@ -44,6 +46,13 @@ class SharedTempStoreTest extends UnitTestCase {
*/
protected $owner = 1;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* A tempstore object belonging to the owner.
*
......@@ -66,13 +75,16 @@ protected function setUp() {
$this->keyValue = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface');
$this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
$this->requestStack = new RequestStack();
$request = Request::createFromGlobals();
$this->requestStack->push($request);
$this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, 604800);
$this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800);
$this->ownObject = (object) array(
'data' => 'test_data',
'owner' => $this->owner,
'updated' => REQUEST_TIME,
'updated' => (int) $request->server->get('REQUEST_TIME'),
);
// Clone the object but change the owner.
......
......@@ -50,7 +50,7 @@ services:
- { name: backend_overridable }
user.shared_tempstore:
class: Drupal\user\SharedTempStoreFactory
arguments: ['@keyvalue.expirable', '@lock', '%user.tempstore.expire%']
arguments: ['@keyvalue.expirable', '@lock', '@request_stack', '%user.tempstore.expire%']
tags:
- { name: backend_overridable }
user.permissions:
......
......@@ -101,7 +101,7 @@ public function testIsLocked() {
$lock = (object) array(
'owner' => 2,
'data' => array(),
'updated' => REQUEST_TIME,
'updated' => (int) $_SERVER['REQUEST_TIME'],
);
$view_ui->lock = $lock;
$this->assertTrue($view_ui->isLocked());
......@@ -110,7 +110,7 @@ public function testIsLocked() {
$lock = (object) array(
'owner' => 1,
'data' => array(),
'updated' => REQUEST_TIME,
'updated' => (int) $_SERVER['REQUEST_TIME'],
);
$view_ui->lock = $lock;
$this->assertFalse($view_ui->isLocked());
......
......@@ -121,7 +121,7 @@ public function testGetFromCache() {
$cache = (object) array(
'data' => array($key => $value),
'created' => REQUEST_TIME,
'created' => (int) $_SERVER['REQUEST_TIME'],
);
$this->cacheBackend->expects($this->once())
->method('get')
......@@ -229,7 +229,7 @@ public function testUpdateCacheInvalidatedConflict() {
$cache = (object) array(
'data' => array($key => $value),
'created' => REQUEST_TIME,
'created' => (int) $_SERVER['REQUEST_TIME'],
);
$this->cacheBackend->expects($this->at(0))
->method('get')
......@@ -250,7 +250,7 @@ public function testUpdateCacheInvalidatedConflict() {
->will($this->returnValue(TRUE));
$cache = (object) array(
'data' => array($key => $value),
'created' => REQUEST_TIME + 1,
'created' => (int) $_SERVER['REQUEST_TIME'] + 1,
);
$this->cacheBackend->expects($this->at(0))
->method('get')
......@@ -286,7 +286,7 @@ public function testUpdateCacheMerge() {
->will($this->returnValue(TRUE));
$cache = (object) array(
'data' => array('other key' => 'other value'),
'created' => REQUEST_TIME + 1,
'created' => (int) $_SERVER['REQUEST_TIME'] + 1,
);
$this->cacheBackend->expects($this->at(0))
->method('get')
......@@ -312,7 +312,7 @@ public function testUpdateCacheDelete() {
$cache = (object) array(
'data' => array($key => $value),
'created' => REQUEST_TIME,
'created' => (int) $_SERVER['REQUEST_TIME'],
);
$this->cacheBackend->expects($this->at(0))
->method('get')
......
......@@ -44,8 +44,8 @@ class ChainedFastBackendTest extends UnitTestCase {
public function testGetDoesntHitConsistentBackend() {
$consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$timestamp_cid = ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo';
// Use REQUEST_TIME because that is what we will be comparing against.
$timestamp_item = (object) array('cid' => $timestamp_cid, 'data' => REQUEST_TIME - 60);
// Use the request time because that is what we will be comparing against.
$timestamp_item = (object) array('cid' => $timestamp_cid, 'data' => (int) $_SERVER['REQUEST_TIME'] - 60);
$consistent_cache->expects($this->once())
->method('get')->with($timestamp_cid)
->will($this->returnValue($timestamp_item));
......
......@@ -207,7 +207,7 @@ public function testGetAliasByPathNoMatch() {
// This needs to write out the cache.
$this->cache->expects($this->once())
->method('set')
->with($this->cacheKey, array($language->getId() => array($path)), REQUEST_TIME + (60 * 60 * 24));
->with($this->cacheKey, array($language->getId() => array($path)), (int) $_SERVER['REQUEST_TIME'] + (60 * 60 * 24));
$this->aliasManager->writeCache();
}
......@@ -245,7 +245,7 @@ public function testGetAliasByPathMatch() {
// This needs to write out the cache.
$this->cache->expects($this->once())
->method('set')
->with($this->cacheKey, array($language->getId() => array($path)), REQUEST_TIME + (60 * 60 * 24));
->with($this->cacheKey, array($language->getId() => array($path)), (int) $_SERVER['REQUEST_TIME'] + (60 * 60 * 24));
$this->aliasManager->writeCache();
}
......@@ -346,7 +346,7 @@ public function testGetAliasByPathCachedMissLanguage() {
);
$this->cache->expects($this->once())
->method('set')
->with($this->cacheKey, $expected_new_cache, REQUEST_TIME + (60 * 60 * 24));
->with($this->cacheKey, $expected_new_cache, (int) $_SERVER['REQUEST_TIME'] + (60 * 60 * 24));
$this->aliasManager->writeCache();
}
......@@ -447,7 +447,7 @@ public function testGetAliasByPathUncachedMissNoAlias() {
);
$this->cache->expects($this->once())
->method('set')
->with($this->cacheKey, $expected_new_cache, REQUEST_TIME + (60 * 60 * 24));
->with($this->cacheKey, $expected_new_cache, (int) $_SERVER['REQUEST_TIME'] + (60 * 60 * 24));
$this->aliasManager->writeCache();
}
......@@ -539,7 +539,7 @@ public function testGetAliasByPathUncachedMissWithAlias() {
);
$this->cache->expects($this->once())
->method('set')
->with($this->cacheKey, $expected_new_cache, REQUEST_TIME + (60 * 60 * 24));
->with($this->cacheKey, $expected_new_cache, (int) $_SERVER['REQUEST_TIME'] + (60 * 60 * 24));
$this->aliasManager->writeCache();
}
......
......@@ -616,7 +616,7 @@ public function testRenderCacheMaxAge($max_age, $is_render_cached, $render_cache
public function providerTestRenderCacheMaxAge() {
return [
[0, FALSE, NULL],
[60, TRUE, REQUEST_TIME + 60],
[60, TRUE, (int) $_SERVER['REQUEST_TIME'] + 60],
[Cache::PERMANENT, TRUE, -1],
];
}
......
......@@ -173,6 +173,8 @@ protected function setupMemoryCache() {
*/
protected function setUpRequest($method = 'GET') {
$request = Request::create('/', $method);
// Ensure that the request time is set as expected.
$request->server->set('REQUEST_TIME', (int) $_SERVER['REQUEST_TIME']);
$this->requestStack->push($request);
}
......
......@@ -81,9 +81,6 @@ function drupal_phpunit_register_extension_dirs(Composer\Autoload\ClassLoader $l
$dirs = array_reduce($dirs, 'array_merge', array());
drupal_phpunit_register_extension_dirs($loader, $dirs);
// Look into removing these later.
define('REQUEST_TIME', (int) $_SERVER['REQUEST_TIME']);
// Set sane locale settings, to ensure consistent string, dates, times and
// numbers handling.
// @see \Drupal\Core\DrupalKernel::bootEnvironment()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment