Unverified Commit 590a5e79 authored by alexpott's avatar alexpott

Issue #2860341 by heddn, juampynr, seanB, RumyanaRuseva, ephod, stephsem23:...

Issue #2860341 by heddn, juampynr, seanB, RumyanaRuseva, ephod, stephsem23: PrivateTempStore->getOwner Attempts to access possibly unset Request Session

(cherry picked from commit 7ae9670d)
parent 33fcce3c
...@@ -123,6 +123,7 @@ public function set($key, $value) { ...@@ -123,6 +123,7 @@ public function set($key, $value) {
if ($this->currentUser->isAnonymous()) { if ($this->currentUser->isAnonymous()) {
// @todo when https://www.drupal.org/node/2865991 is resolved, use force // @todo when https://www.drupal.org/node/2865991 is resolved, use force
// start session API rather than setting an arbitrary value directly. // start session API rather than setting an arbitrary value directly.
$this->startSession();
$this->requestStack $this->requestStack
->getCurrentRequest() ->getCurrentRequest()
->getSession() ->getSession()
...@@ -219,7 +220,34 @@ protected function createkey($key) { ...@@ -219,7 +220,34 @@ protected function createkey($key) {
* The owner. * The owner.
*/ */
protected function getOwner() { protected function getOwner() {
return $this->currentUser->id() ?: $this->requestStack->getCurrentRequest()->getSession()->getId(); $owner = $this->currentUser->id();
if ($this->currentUser->isAnonymous()) {
$this->startSession();
$owner = $this->requestStack->getCurrentRequest()->getSession()->getId();
}
return $owner;
}
/**
* Start session because it is required for a private temp store.
*
* Ensures that an anonymous user has a session created for them, as
* otherwise subsequent page loads will not be able to retrieve their
* tempstore data.
*
* @todo when https://www.drupal.org/node/2865991 is resolved, use force
* start session API.
*/
protected function startSession() {
$has_session = $this->requestStack
->getCurrentRequest()
->hasSession();
if (!$has_session) {
/** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
$session = \Drupal::service('session');
$this->requestStack->getCurrentRequest()->setSession($session);
$session->start();
}
} }
} }
...@@ -19,6 +19,13 @@ class AnonymousPrivateTempStoreTest extends KernelTestBase { ...@@ -19,6 +19,13 @@ class AnonymousPrivateTempStoreTest extends KernelTestBase {
*/ */
public static $modules = ['system']; public static $modules = ['system'];
/**
* The private temp store.
*
* @var \Drupal\Core\TempStore\PrivateTempStore
*/
protected $tempStore;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -29,30 +36,35 @@ protected function setUp() { ...@@ -29,30 +36,35 @@ protected function setUp() {
// full Drupal environment. // full Drupal environment.
$this->installSchema('system', ['key_value_expire']); $this->installSchema('system', ['key_value_expire']);
$session = $this->container->get('session');
$request = Request::create('/'); $request = Request::create('/');
$request->setSession($session);
$stack = $this->container->get('request_stack'); $stack = $this->container->get('request_stack');
$stack->pop(); $stack->pop();
$stack->push($request); $stack->push($request);
$this->tempStore = $this->container->get('tempstore.private')->get('anonymous_private_temp_store');
}
/**
* Tests anonymous can get without a previous set.
*/
public function testAnonymousCanUsePrivateTempStoreGet() {
$actual = $this->tempStore->get('foo');
$this->assertNull($actual);
} }
/** /**
* Tests anonymous can use the PrivateTempStore. * Tests anonymous can use the PrivateTempStore.
*/ */
public function testAnonymousCanUsePrivateTempStore() { public function testAnonymousCanUsePrivateTempStoreSet() {
$temp_store = $this->container->get('tempstore.private')->get('anonymous_private_temp_store'); $this->tempStore->set('foo', 'bar');
$temp_store->set('foo', 'bar'); $metadata1 = $this->tempStore->getMetadata('foo');
$metadata1 = $temp_store->getMetadata('foo');
$this->assertEquals('bar', $temp_store->get('foo')); $this->assertEquals('bar', $this->tempStore->get('foo'));
$this->assertNotEmpty($metadata1->owner); $this->assertNotEmpty($metadata1->owner);
$temp_store->set('foo', 'bar2'); $this->tempStore->set('foo', 'bar2');
$metadata2 = $temp_store->getMetadata('foo'); $metadata2 = $this->tempStore->getMetadata('foo');
$this->assertEquals('bar2', $temp_store->get('foo')); $this->assertEquals('bar2', $this->tempStore->get('foo'));
$this->assertNotEmpty($metadata2->owner); $this->assertNotEmpty($metadata2->owner);
$this->assertEquals($metadata2->owner, $metadata1->owner); $this->assertEquals($metadata2->owner, $metadata1->owner);
} }
......
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