diff --git a/core/lib/Drupal/Core/TempStore/SharedTempStore.php b/core/lib/Drupal/Core/TempStore/SharedTempStore.php index 064ae1c1b2..aeaa2d811b 100644 --- a/core/lib/Drupal/Core/TempStore/SharedTempStore.php +++ b/core/lib/Drupal/Core/TempStore/SharedTempStore.php @@ -4,6 +4,7 @@ use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface; use Drupal\Core\Lock\LockBackendInterface; +use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\HttpFoundation\RequestStack; /** @@ -74,6 +75,13 @@ class SharedTempStore { */ protected $expire; + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountProxyInterface + */ + protected $currentUser; + /** * Constructs a new object for accessing data from a key/value store. * @@ -87,14 +95,28 @@ class SharedTempStore { * 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 \Drupal\Core\Session\AccountProxyInterface|null $current_user + * The current user. * @param int $expire * The time to live for items, in seconds. */ - public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, $owner, RequestStack $request_stack, $expire = 604800) { + public function __construct(KeyValueStoreExpirableInterface $storage, LockBackendInterface $lock_backend, $owner, RequestStack $request_stack, $current_user = NULL, $expire = 604800) { $this->storage = $storage; $this->lockBackend = $lock_backend; $this->owner = $owner; $this->requestStack = $request_stack; + if ($current_user instanceof AccountProxyInterface) { + $this->currentUser = $current_user; + } + else { + @trigger_error('@todo', E_USER_DEPRECATED); + $this->currentUser = \Drupal::currentUser(); + if (is_int($current_user)) { + // If the $current_user argument is numeric then this object has been + // instantiated with the old constructor signature. + $expire = $current_user; + } + } $this->expire = $expire; } @@ -287,7 +309,7 @@ public function deleteIfOwner($key) { * This method should be called when a value is set. */ protected function ensureAnonymousSession() { - if (\Drupal::currentUser()->isAnonymous()) { + if ($this->currentUser->isAnonymous()) { $this->requestStack->getCurrentRequest()->getSession()->set('core.tempstore.shared.owner', $this->owner); } } diff --git a/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php b/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php index 438e8beb57..29745a1769 100644 --- a/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php +++ b/core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php @@ -76,8 +76,8 @@ public function __construct(KeyValueExpirableFactoryInterface $storage_factory, public function get($collection, $owner = NULL) { // Use the currently authenticated user ID or the active user ID unless // the owner is overridden. + $account = \Drupal::currentUser(); if (!isset($owner)) { - $account = \Drupal::currentUser(); $owner = $account->id(); if ($account->isAnonymous()) { $has_session = $this->requestStack @@ -95,7 +95,7 @@ public function get($collection, $owner = NULL) { // Store the data for this collection in the database. $storage = $this->storageFactory->get("tempstore.shared.$collection"); - return new SharedTempStore($storage, $this->lockBackend, $owner, $this->requestStack, $this->expire); + return new SharedTempStore($storage, $this->lockBackend, $owner, $this->requestStack, $account, $this->expire); } } diff --git a/core/modules/user/tests/src/Kernel/TempStoreDatabaseTest.php b/core/modules/user/tests/src/Kernel/TempStoreDatabaseTest.php index 51a3b9f8e7..9e4ea6ed70 100644 --- a/core/modules/user/tests/src/Kernel/TempStoreDatabaseTest.php +++ b/core/modules/user/tests/src/Kernel/TempStoreDatabaseTest.php @@ -73,8 +73,12 @@ protected function setUp() { * @expectedDeprecation \Drupal\user\SharedTempStore is scheduled for removal in Drupal 9.0.0. Use \Drupal\Core\TempStore\SharedTempStore instead. See https://www.drupal.org/node/2935639. */ public function testUserTempStore() { + $session = \Drupal::service('session'); + $session->start(); + $request_stack = $this->container->get('request_stack'); + $request_stack->getCurrentRequest()->setSession($session); // Create a key/value collection. - $factory = new SharedTempStoreFactory(new KeyValueExpirableFactory(\Drupal::getContainer()), new DatabaseLockBackend(Database::getConnection()), $this->container->get('request_stack')); + $factory = new SharedTempStoreFactory(new KeyValueExpirableFactory(\Drupal::getContainer()), new DatabaseLockBackend(Database::getConnection()), $request_stack); $collection = $this->randomMachineName(); // Create two mock users. diff --git a/core/modules/user/tests/src/Unit/SharedTempStoreTest.php b/core/modules/user/tests/src/Unit/SharedTempStoreTest.php index 6b5d441968..321636979e 100644 --- a/core/modules/user/tests/src/Unit/SharedTempStoreTest.php +++ b/core/modules/user/tests/src/Unit/SharedTempStoreTest.php @@ -2,11 +2,13 @@ namespace Drupal\Tests\user\Unit; +use Drupal\Core\Session\AccountProxyInterface; use Drupal\Tests\UnitTestCase; use Drupal\user\SharedTempStore; use Drupal\user\TempStoreException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * @coversDefaultClass \Drupal\user\SharedTempStore @@ -76,9 +78,12 @@ protected function setUp() { $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); $this->requestStack = new RequestStack(); $request = Request::createFromGlobals(); + $session = $this->getMock(SessionInterface::class); + $request->setSession($session); $this->requestStack->push($request); + $current_user = $this->getMock(AccountProxyInterface::class); - $this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800); + $this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, $current_user, 604800); $this->ownObject = (object) [ 'data' => 'test_data', diff --git a/core/tests/Drupal/KernelTests/Core/TempStore/TempStoreDatabaseTest.php b/core/tests/Drupal/KernelTests/Core/TempStore/TempStoreDatabaseTest.php index 830432f8cc..a936a431a3 100644 --- a/core/tests/Drupal/KernelTests/Core/TempStore/TempStoreDatabaseTest.php +++ b/core/tests/Drupal/KernelTests/Core/TempStore/TempStoreDatabaseTest.php @@ -64,7 +64,11 @@ protected function setUp() { public function testSharedTempStore() { // Create a key/value collection. $database = Database::getConnection(); - $factory = new SharedTempStoreFactory(new KeyValueExpirableFactory(\Drupal::getContainer()), new DatabaseLockBackend($database), $this->container->get('request_stack')); + $session = \Drupal::service('session'); + $session->start(); + $request_stack = $this->container->get('request_stack'); + $request_stack->getCurrentRequest()->setSession($session); + $factory = new SharedTempStoreFactory(new KeyValueExpirableFactory(\Drupal::getContainer()), new DatabaseLockBackend($database), $request_stack); $collection = $this->randomMachineName(); // Create two mock users. diff --git a/core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php b/core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php index 55a78b9a25..f8edcd066c 100644 --- a/core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php +++ b/core/tests/Drupal/Tests/Core/TempStore/SharedTempStoreTest.php @@ -2,11 +2,14 @@ namespace Drupal\Tests\Core\TempStore; +use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Session\AccountProxyInterface; use Drupal\Tests\UnitTestCase; use Drupal\Core\TempStore\SharedTempStore; use Drupal\Core\TempStore\TempStoreException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * @coversDefaultClass \Drupal\Core\TempStore\SharedTempStore @@ -73,9 +76,12 @@ protected function setUp() { $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); $this->requestStack = new RequestStack(); $request = Request::createFromGlobals(); + $session = $this->getMock(SessionInterface::class); + $request->setSession($session); $this->requestStack->push($request); + $current_user = $this->getMock(AccountProxyInterface::class); - $this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, 604800); + $this->tempStore = new SharedTempStore($this->keyValue, $this->lock, $this->owner, $this->requestStack, $current_user, 604800); $this->ownObject = (object) [ 'data' => 'test_data', @@ -350,4 +356,25 @@ public function testDeleteIfOwner() { $this->assertFalse($this->tempStore->deleteIfOwner('test_3')); } + /** + * @group legacy + * @expectedDeprecation @todo + */ + public function testLegacyConstructor() { + $container = new ContainerBuilder(); + $current_user = $this->getMock(AccountProxyInterface::class); + $container->set('current_user', $current_user); + \Drupal::setContainer($container); + $store = new SharedTempStore($this->keyValue, $this->lock, 2, $this->requestStack, 1000); + $reflection_class = new \ReflectionClass(SharedTempStore::class); + + $current_user_property = $reflection_class->getProperty('currentUser'); + $current_user_property->setAccessible(TRUE); + $this->assertSame($current_user, $current_user_property->getValue($store)); + + $expire_property = $reflection_class->getProperty('expire'); + $expire_property->setAccessible(TRUE); + $this->assertSame(1000, $expire_property->getValue($store)); + } + }