Security update for Core, with self-updated composer
[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 Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\RequestStack;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Cache\Context\SessionCacheContext
12  * @group Cache
13  */
14 class SessionCacheContextTest extends UnitTestCase {
15
16   /**
17    * The request.
18    *
19    * @var \Symfony\Component\HttpFoundation\Request
20    */
21   protected $request;
22
23   /**
24    * The request stack.
25    *
26    * @var \Symfony\Component\HttpFoundation\RequestStack
27    */
28   protected $requestStack;
29
30   /**
31    * The session object.
32    *
33    * @var \Symfony\Component\HttpFoundation\Session\SessionInterface|\PHPUnit_Framework_MockObject_MockObject
34    */
35   protected $session;
36
37   public function setUp() {
38     $this->request = new Request();
39
40     $this->requestStack = new RequestStack();
41     $this->requestStack->push($this->request);
42
43     $this->session = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Session\SessionInterface')
44       ->getMock();
45   }
46
47   /**
48    * @covers ::getContext
49    */
50   public function testSameContextForSameSession() {
51     $this->request->setSession($this->session);
52     $cache_context = new SessionCacheContext($this->requestStack);
53
54     $session_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
55     $this->session->expects($this->exactly(2))
56       ->method('getId')
57       ->will($this->returnValue($session_id));
58
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');
63   }
64
65   /**
66    * @covers ::getContext
67    */
68   public function testDifferentContextForDifferentSession() {
69     $this->request->setSession($this->session);
70     $cache_context = new SessionCacheContext($this->requestStack);
71
72     $session1_id = 'pjH_8aSoofyCDQiuVYXJcbfyr-CPtkUY';
73     $this->session->expects($this->at(0))
74       ->method('getId')
75       ->will($this->returnValue($session1_id));
76
77     $session2_id = 'aSebeZ52bbM6SvADurQP89SFnEpxY6j8';
78     $this->session->expects($this->at(1))
79       ->method('getId')
80       ->will($this->returnValue($session2_id));
81
82     $context1 = $cache_context->getContext();
83     $context2 = $cache_context->getContext();
84     $this->assertNotEquals($context1, $context2);
85
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');
88   }
89
90   /**
91    * @covers ::getContext
92    */
93   public function testContextWithoutSessionInRequest() {
94     $cache_context = new SessionCacheContext($this->requestStack);
95
96     $this->assertSame('none', $cache_context->getContext());
97   }
98
99 }