3 namespace Drupal\Tests\Core\Cache\Context;
5 use Drupal\Core\Cache\Context\SessionCacheContext;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\RequestStack;
11 * @coversDefaultClass \Drupal\Core\Cache\Context\SessionCacheContext
14 class SessionCacheContextTest extends UnitTestCase {
19 * @var \Symfony\Component\HttpFoundation\Request
26 * @var \Symfony\Component\HttpFoundation\RequestStack
28 protected $requestStack;
33 * @var \Symfony\Component\HttpFoundation\Session\SessionInterface|\PHPUnit_Framework_MockObject_MockObject
37 public function setUp() {
38 $this->request = new Request();
40 $this->requestStack = new RequestStack();
41 $this->requestStack->push($this->request);
43 $this->session = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Session\SessionInterface')
48 * @covers ::getContext
50 public function testSameContextForSameSession() {
51 $this->request->setSession($this->session);
52 $cache_context = new SessionCacheContext($this->requestStack);
54 $session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
55 $this->session->expects($this->exactly(2))
57 ->will($this->returnValue($session_id));
59 $context1 = $cache_context->getContext();
60 $context2 = $cache_context->getContext();
61 $this->assertSame($context1, $context2);
62 $this->assertSame(FALSE, strpos($context1, $session_id), 'Session ID not contained in cache context');
66 * @covers ::getContext
68 public function testDifferentContextForDifferentSession() {
69 $this->request->setSession($this->session);
70 $cache_context = new SessionCacheContext($this->requestStack);
72 $session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY';
73 $this->session->expects($this->at(0))
75 ->will($this->returnValue($session1_id));
77 $session2_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
78 $this->session->expects($this->at(1))
80 ->will($this->returnValue($session2_id));
82 $context1 = $cache_context->getContext();
83 $context2 = $cache_context->getContext();
84 $this->assertNotEquals($context1, $context2);
86 $this->assertSame(FALSE, strpos($context1, $session1_id), 'Session ID not contained in cache context');
87 $this->assertSame(FALSE, strpos($context2, $session2_id), 'Session ID not contained in cache context');
91 * @covers ::getContext
93 public function testContextWithoutSessionInRequest() {
94 $cache_context = new SessionCacheContext($this->requestStack);
96 $this->assertSame('none', $cache_context->getContext());