Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Cache / Context / SessionCacheContextTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Cache\Context;
4
5 use Drupal\Core\Cache\Context\SessionCacheContext;
6 use Symfony\Component\HttpFoundation\Request;
7 use Symfony\Component\HttpFoundation\RequestStack;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Cache\Context\SessionCacheContext
11  * @group Cache
12  */
13 class SessionCacheContextTest extends \PHPUnit_Framework_TestCase {
14
15   /**
16    * The request stack.
17    *
18    * @var \Symfony\Component\HttpFoundation\RequestStack
19    */
20   protected $requestStack;
21
22   /**
23    * The session object.
24    *
25    * @var \Symfony\Component\HttpFoundation\Session\SessionInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $session;
28
29   /**
30    * The session cache context.
31    *
32    * @var \Drupal\Core\Cache\Context\SessionCacheContext
33    */
34   protected $cacheContext;
35
36   public function setUp() {
37     $request = new Request();
38
39     $this->requestStack = new RequestStack();
40     $this->requestStack->push($request);
41
42     $this->session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
43     $request->setSession($this->session);
44
45     $this->cacheContext = new SessionCacheContext($this->requestStack);
46   }
47
48   /**
49    * @covers ::getContext
50    */
51   public function testSameContextForSameSession() {
52     $session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
53     $this->session->expects($this->exactly(2))
54       ->method('getId')
55       ->will($this->returnValue($session_id));
56
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');
61   }
62
63   /**
64    * @covers ::getContext
65    */
66   public function testDifferentContextForDifferentSession() {
67     $session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY';
68     $this->session->expects($this->at(0))
69       ->method('getId')
70       ->will($this->returnValue($session1_id));
71
72     $session2_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
73     $this->session->expects($this->at(1))
74       ->method('getId')
75       ->will($this->returnValue($session2_id));
76
77     $context1 = $this->cacheContext->getContext();
78     $context2 = $this->cacheContext->getContext();
79     $this->assertNotEquals($context1, $context2);
80
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');
83   }
84
85 }