diff options
author | Alex Pott | 2017-10-12 13:16:44 (GMT) |
---|---|---|
committer | Alex Pott | 2017-10-12 13:16:44 (GMT) |
commit | 0c0c63e9af3a657426c2343c2846769f50674043 (patch) | |
tree | c52bd1912619756d3619d5e3d9a7395af381e75b | |
parent | a151cef151bb4d3a44ff17fec04633297ffe1095 (diff) |
Issue #2881348 by benjifisher, guncha25, Jo Fitzgerald, anya_m, Dinesh18, pritish.kumar, andypost, alexpott, James Nesbitt, larowlan, znerol, dawehner: SessionCacheContext calls getId() on null
-rw-r--r-- | core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php | 7 | ||||
-rw-r--r-- | core/tests/Drupal/Tests/Core/Cache/Context/SessionCacheContextTest.php | 47 |
2 files changed, 35 insertions, 19 deletions
diff --git a/core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php b/core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php index c8b1027..bccd0f0 100644 --- a/core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php +++ b/core/lib/Drupal/Core/Cache/Context/SessionCacheContext.php @@ -22,8 +22,11 @@ class SessionCacheContext extends RequestStackCacheContextBase { * {@inheritdoc} */ public function getContext() { - $sid = $this->requestStack->getCurrentRequest()->getSession()->getId(); - return Crypt::hashBase64($sid); + $request = $this->requestStack->getCurrentRequest(); + if ($request->hasSession()) { + return Crypt::hashBase64($request->getSession()->getId()); + } + return 'none'; } } diff --git a/core/tests/Drupal/Tests/Core/Cache/Context/SessionCacheContextTest.php b/core/tests/Drupal/Tests/Core/Cache/Context/SessionCacheContextTest.php index 9538bd2..a8cf6fb 100644 --- a/core/tests/Drupal/Tests/Core/Cache/Context/SessionCacheContextTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/Context/SessionCacheContextTest.php @@ -14,6 +14,13 @@ use Symfony\Component\HttpFoundation\RequestStack; class SessionCacheContextTest extends UnitTestCase { /** + * The request. + * + * @var \Symfony\Component\HttpFoundation\Request + */ + protected $request; + + /** * The request stack. * * @var \Symfony\Component\HttpFoundation\RequestStack @@ -27,36 +34,30 @@ class SessionCacheContextTest extends UnitTestCase { */ protected $session; - /** - * The session cache context. - * - * @var \Drupal\Core\Cache\Context\SessionCacheContext - */ - protected $cacheContext; - public function setUp() { - $request = new Request(); + $this->request = new Request(); $this->requestStack = new RequestStack(); - $this->requestStack->push($request); - - $this->session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface'); - $request->setSession($this->session); + $this->requestStack->push($this->request); - $this->cacheContext = new SessionCacheContext($this->requestStack); + $this->session = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Session\SessionInterface') + ->getMock(); } /** * @covers ::getContext */ public function testSameContextForSameSession() { + $this->request->setSession($this->session); + $cache_context = new SessionCacheContext($this->requestStack); + $session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8'; $this->session->expects($this->exactly(2)) ->method('getId') ->will($this->returnValue($session_id)); - $context1 = $this->cacheContext->getContext(); - $context2 = $this->cacheContext->getContext(); + $context1 = $cache_context->getContext(); + $context2 = $cache_context->getContext(); $this->assertSame($context1, $context2); $this->assertSame(FALSE, strpos($context1, $session_id), 'Session ID not contained in cache context'); } @@ -65,6 +66,9 @@ class SessionCacheContextTest extends UnitTestCase { * @covers ::getContext */ public function testDifferentContextForDifferentSession() { + $this->request->setSession($this->session); + $cache_context = new SessionCacheContext($this->requestStack); + $session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY'; $this->session->expects($this->at(0)) ->method('getId') @@ -75,12 +79,21 @@ class SessionCacheContextTest extends UnitTestCase { ->method('getId') ->will($this->returnValue($session2_id)); - $context1 = $this->cacheContext->getContext(); - $context2 = $this->cacheContext->getContext(); + $context1 = $cache_context->getContext(); + $context2 = $cache_context->getContext(); $this->assertNotEquals($context1, $context2); $this->assertSame(FALSE, strpos($context1, $session1_id), 'Session ID not contained in cache context'); $this->assertSame(FALSE, strpos($context2, $session2_id), 'Session ID not contained in cache context'); } + /** + * @covers ::getContext + */ + public function testContextWithoutSessionInRequest() { + $cache_context = new SessionCacheContext($this->requestStack); + + $this->assertSame('none', $cache_context->getContext()); + } + } |