3 namespace Drupal\Tests\Core\Cache\Context;
5 use Drupal\Core\Cache\Context\SessionCacheContext;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\HttpFoundation\RequestStack;
10 * @coversDefaultClass \Drupal\Core\Cache\Context\SessionCacheContext
13 class SessionCacheContextTest extends \PHPUnit_Framework_TestCase {
18 * @var \Symfony\Component\HttpFoundation\RequestStack
20 protected $requestStack;
25 * @var \Symfony\Component\HttpFoundation\Session\SessionInterface|\PHPUnit_Framework_MockObject_MockObject
30 * The session cache context.
32 * @var \Drupal\Core\Cache\Context\SessionCacheContext
34 protected $cacheContext;
36 public function setUp() {
37 $request = new Request();
39 $this->requestStack = new RequestStack();
40 $this->requestStack->push($request);
42 $this->session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
43 $request->setSession($this->session);
45 $this->cacheContext = new SessionCacheContext($this->requestStack);
49 * @covers ::getContext
51 public function testSameContextForSameSession() {
52 $session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
53 $this->session->expects($this->exactly(2))
55 ->will($this->returnValue($session_id));
57 $context1 = $this->cacheContext->getContext();
58 $context2 = $this->cacheContext->getContext();
59 $this->assertSame($context1, $context2);
60 $this->assertSame(FALSE, strpos($context1, $session_id), 'Session ID not contained in cache context');
64 * @covers ::getContext
66 public function testDifferentContextForDifferentSession() {
67 $session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY';
68 $this->session->expects($this->at(0))
70 ->will($this->returnValue($session1_id));
72 $session2_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
73 $this->session->expects($this->at(1))
75 ->will($this->returnValue($session2_id));
77 $context1 = $this->cacheContext->getContext();
78 $context2 = $this->cacheContext->getContext();
79 $this->assertNotEquals($context1, $context2);
81 $this->assertSame(FALSE, strpos($context1, $session1_id), 'Session ID not contained in cache context');
82 $this->assertSame(FALSE, strpos($context2, $session2_id), 'Session ID not contained in cache context');