Commit 0c0c63e9 authored by alexpott's avatar alexpott

Issue #2881348 by benjifisher, guncha25, Jo Fitzgerald, anya_m, Dinesh18,...

Issue #2881348 by benjifisher, guncha25, Jo Fitzgerald, anya_m, Dinesh18, pritish.kumar, andypost, alexpott, James Nesbitt, larowlan, znerol, dawehner: SessionCacheContext calls getId() on null
parent a151cef1
...@@ -22,8 +22,11 @@ public static function getLabel() { ...@@ -22,8 +22,11 @@ public static function getLabel() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getContext() { public function getContext() {
$sid = $this->requestStack->getCurrentRequest()->getSession()->getId(); $request = $this->requestStack->getCurrentRequest();
return Crypt::hashBase64($sid); if ($request->hasSession()) {
return Crypt::hashBase64($request->getSession()->getId());
}
return 'none';
} }
} }
...@@ -13,6 +13,13 @@ ...@@ -13,6 +13,13 @@
*/ */
class SessionCacheContextTest extends UnitTestCase { class SessionCacheContextTest extends UnitTestCase {
/**
* The request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/** /**
* The request stack. * The request stack.
* *
...@@ -27,36 +34,30 @@ class SessionCacheContextTest extends UnitTestCase { ...@@ -27,36 +34,30 @@ class SessionCacheContextTest extends UnitTestCase {
*/ */
protected $session; protected $session;
/**
* The session cache context.
*
* @var \Drupal\Core\Cache\Context\SessionCacheContext
*/
protected $cacheContext;
public function setUp() { public function setUp() {
$request = new Request(); $this->request = new Request();
$this->requestStack = new RequestStack(); $this->requestStack = new RequestStack();
$this->requestStack->push($request); $this->requestStack->push($this->request);
$this->session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
$request->setSession($this->session);
$this->cacheContext = new SessionCacheContext($this->requestStack); $this->session = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Session\SessionInterface')
->getMock();
} }
/** /**
* @covers ::getContext * @covers ::getContext
*/ */
public function testSameContextForSameSession() { public function testSameContextForSameSession() {
$this->request->setSession($this->session);
$cache_context = new SessionCacheContext($this->requestStack);
$session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8'; $session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
$this->session->expects($this->exactly(2)) $this->session->expects($this->exactly(2))
->method('getId') ->method('getId')
->will($this->returnValue($session_id)); ->will($this->returnValue($session_id));
$context1 = $this->cacheContext->getContext(); $context1 = $cache_context->getContext();
$context2 = $this->cacheContext->getContext(); $context2 = $cache_context->getContext();
$this->assertSame($context1, $context2); $this->assertSame($context1, $context2);
$this->assertSame(FALSE, strpos($context1, $session_id), 'Session ID not contained in cache context'); $this->assertSame(FALSE, strpos($context1, $session_id), 'Session ID not contained in cache context');
} }
...@@ -65,6 +66,9 @@ public function testSameContextForSameSession() { ...@@ -65,6 +66,9 @@ public function testSameContextForSameSession() {
* @covers ::getContext * @covers ::getContext
*/ */
public function testDifferentContextForDifferentSession() { public function testDifferentContextForDifferentSession() {
$this->request->setSession($this->session);
$cache_context = new SessionCacheContext($this->requestStack);
$session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY'; $session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY';
$this->session->expects($this->at(0)) $this->session->expects($this->at(0))
->method('getId') ->method('getId')
...@@ -75,12 +79,21 @@ public function testDifferentContextForDifferentSession() { ...@@ -75,12 +79,21 @@ public function testDifferentContextForDifferentSession() {
->method('getId') ->method('getId')
->will($this->returnValue($session2_id)); ->will($this->returnValue($session2_id));
$context1 = $this->cacheContext->getContext(); $context1 = $cache_context->getContext();
$context2 = $this->cacheContext->getContext(); $context2 = $cache_context->getContext();
$this->assertNotEquals($context1, $context2); $this->assertNotEquals($context1, $context2);
$this->assertSame(FALSE, strpos($context1, $session1_id), 'Session ID not contained in cache context'); $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'); $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());
}
} }
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