Issue #2459155 by alexpott, pfrenssen, hussainweb, neclimdul: Remove REQUEST_TIME from bootstrap.php

8.0.x
webchick 2015-04-06 23:53:19 -07:00
parent cc32fa51ed
commit 81a6979058
18 changed files with 92 additions and 38 deletions

View File

@ -94,7 +94,7 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
$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 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
$this->cache[$cid] = (object) array(
'cid' => $cid,
'data' => serialize($data),
'created' => REQUEST_TIME,
'created' => $this->getRequestTime(),
'expire' => $expire,
'tags' => $tags,
);
@ -155,7 +155,7 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
*/
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 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
*/
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 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
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 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
*/
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 @@ class MemoryBackend implements CacheBackendInterface, CacheTagsInvalidatorInterf
$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.
*/

View File

@ -144,7 +144,7 @@ class AliasManager implements AliasManagerInterface, CacheDecoratorInterface {
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 @@ class AliasManager implements AliasManagerInterface, CacheDecoratorInterface {
}
$this->whitelist->clear();
}
/**
* Wrapper method for REQUEST_TIME constant.
*
* @return int
*/
protected function getRequestTime() {
return defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME'];
}
}

View File

@ -571,7 +571,7 @@ class Renderer implements RendererInterface {
$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,

View File

@ -354,7 +354,7 @@ class MigrateExecutableTest extends MigrateTestCase {
$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();

View File

@ -132,7 +132,7 @@ class PrivateTempStore {
$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);

View File

@ -10,6 +10,7 @@ namespace Drupal\user;
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 @@ class SharedTempStore {
$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 @@ class SharedTempStore {
$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);

View File

@ -10,6 +10,7 @@ namespace Drupal\user;
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 @@ class SharedTempStoreFactory {
// 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);
}
}

View File

@ -76,7 +76,7 @@ class TempStoreDatabaseTest extends KernelTestBase {
*/
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.

View File

@ -9,6 +9,7 @@ namespace Drupal\Tests\user\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\user\PrivateTempStore;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
@ -80,13 +81,15 @@ class PrivateTempStoreTest extends UnitTestCase {
->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.

View File

@ -9,6 +9,8 @@ namespace Drupal\Tests\user\Unit;
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 @@ class SharedTempStoreTest extends UnitTestCase {
$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.

View File

@ -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:

View File

@ -101,7 +101,7 @@ class ViewUIObjectTest extends UnitTestCase {
$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 @@ class ViewUIObjectTest extends UnitTestCase {
$lock = (object) array(
'owner' => 1,
'data' => array(),
'updated' => REQUEST_TIME,
'updated' => (int) $_SERVER['REQUEST_TIME'],
);
$view_ui->lock = $lock;
$this->assertFalse($view_ui->isLocked());

View File

@ -121,7 +121,7 @@ class CacheCollectorTest extends UnitTestCase {
$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 @@ class CacheCollectorTest extends UnitTestCase {
$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 @@ class CacheCollectorTest extends UnitTestCase {
->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 @@ class CacheCollectorTest extends UnitTestCase {
->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 @@ class CacheCollectorTest extends UnitTestCase {
$cache = (object) array(
'data' => array($key => $value),
'created' => REQUEST_TIME,
'created' => (int) $_SERVER['REQUEST_TIME'],
);
$this->cacheBackend->expects($this->at(0))
->method('get')

View File

@ -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));

View File

@ -207,7 +207,7 @@ class AliasManagerTest extends UnitTestCase {
// 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 @@ class AliasManagerTest extends UnitTestCase {
// 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 @@ class AliasManagerTest extends UnitTestCase {
);
$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 @@ class AliasManagerTest extends UnitTestCase {
);
$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 @@ class AliasManagerTest extends UnitTestCase {
);
$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();
}

View File

@ -616,7 +616,7 @@ class RendererTest extends RendererTestBase {
public function providerTestRenderCacheMaxAge() {
return [
[0, FALSE, NULL],
[60, TRUE, REQUEST_TIME + 60],
[60, TRUE, (int) $_SERVER['REQUEST_TIME'] + 60],
[Cache::PERMANENT, TRUE, -1],
];
}

View File

@ -173,6 +173,8 @@ class RendererTestBase extends UnitTestCase {
*/
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);
}

View File

@ -81,9 +81,6 @@ $dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots)
$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()