Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Session / WriteSafeSessionHandlerTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Session;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\Session\WriteSafeSessionHandler;
7
8 /**
9  * Tests \Drupal\Core\Session\WriteSafeSessionHandler.
10  *
11  * @coversDefaultClass \Drupal\Core\Session\WriteSafeSessionHandler
12  * @group Session
13  */
14 class WriteSafeSessionHandlerTest extends UnitTestCase {
15
16   /**
17    * The wrapped session handler.
18    *
19    * @var \SessionHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
20    */
21   protected $wrappedSessionHandler;
22
23   /**
24    * The write safe session handler.
25    *
26    * @var \Drupal\Core\Session\WriteSafeSessionHandler
27    */
28   protected $sessionHandler;
29
30   protected function setUp() {
31     $this->wrappedSessionHandler = $this->getMock('SessionHandlerInterface');
32     $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler);
33   }
34
35   /**
36    * Tests creating a WriteSafeSessionHandler with default arguments.
37    *
38    * @covers ::__construct
39    * @covers ::isSessionWritable
40    * @covers ::write
41    */
42   public function testConstructWriteSafeSessionHandlerDefaultArgs() {
43     $session_id = 'some-id';
44     $session_data = 'serialized-session-data';
45
46     $this->assertSame(TRUE, $this->sessionHandler->isSessionWritable());
47
48     // Writing should be enabled, return value passed to the caller by default.
49     $this->wrappedSessionHandler->expects($this->at(0))
50       ->method('write')
51       ->with($session_id, $session_data)
52       ->will($this->returnValue(TRUE));
53
54     $this->wrappedSessionHandler->expects($this->at(1))
55       ->method('write')
56       ->with($session_id, $session_data)
57       ->will($this->returnValue(FALSE));
58
59     $result = $this->sessionHandler->write($session_id, $session_data);
60     $this->assertSame(TRUE, $result);
61
62     $result = $this->sessionHandler->write($session_id, $session_data);
63     $this->assertSame(FALSE, $result);
64   }
65
66   /**
67    * Tests creating a WriteSafeSessionHandler with session writing disabled.
68    *
69    * @covers ::__construct
70    * @covers ::isSessionWritable
71    * @covers ::write
72    */
73   public function testConstructWriteSafeSessionHandlerDisableWriting() {
74     $session_id = 'some-id';
75     $session_data = 'serialized-session-data';
76
77     // Disable writing upon construction.
78     $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
79
80     $this->assertSame(FALSE, $this->sessionHandler->isSessionWritable());
81
82     $result = $this->sessionHandler->write($session_id, $session_data);
83     $this->assertSame(TRUE, $result);
84   }
85
86   /**
87    * Tests using setSessionWritable to enable/disable session writing.
88    *
89    * @covers ::setSessionWritable
90    * @covers ::write
91    */
92   public function testSetSessionWritable() {
93     $session_id = 'some-id';
94     $session_data = 'serialized-session-data';
95
96     $this->assertSame(TRUE, $this->sessionHandler->isSessionWritable());
97
98     // Disable writing after construction.
99     $this->sessionHandler->setSessionWritable(FALSE);
100     $this->assertSame(FALSE, $this->sessionHandler->isSessionWritable());
101
102     $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
103
104     $this->assertSame(FALSE, $this->sessionHandler->isSessionWritable());
105
106     $result = $this->sessionHandler->write($session_id, $session_data);
107     $this->assertSame(TRUE, $result);
108
109     // Enable writing again.
110     $this->sessionHandler->setSessionWritable(TRUE);
111     $this->assertSame(TRUE, $this->sessionHandler->isSessionWritable());
112
113     // Writing should be enabled, return value passed to the caller by default.
114     $this->wrappedSessionHandler->expects($this->at(0))
115       ->method('write')
116       ->with($session_id, $session_data)
117       ->will($this->returnValue(TRUE));
118
119     $this->wrappedSessionHandler->expects($this->at(1))
120       ->method('write')
121       ->with($session_id, $session_data)
122       ->will($this->returnValue(FALSE));
123
124     $result = $this->sessionHandler->write($session_id, $session_data);
125     $this->assertSame(TRUE, $result);
126
127     $result = $this->sessionHandler->write($session_id, $session_data);
128     $this->assertSame(FALSE, $result);
129   }
130
131   /**
132    * Tests that other invocations are passed unmodified to the wrapped handler.
133    *
134    * @covers ::setSessionWritable
135    * @covers ::open
136    * @covers ::read
137    * @covers ::close
138    * @covers ::destroy
139    * @covers ::gc
140    * @dataProvider providerTestOtherMethods
141    */
142   public function testOtherMethods($method, $expected_result, $args) {
143     $invocation = $this->wrappedSessionHandler->expects($this->exactly(2))
144       ->method($method)
145       ->will($this->returnValue($expected_result));
146
147     // Set the parameter matcher.
148     call_user_func_array([$invocation, 'with'], $args);
149
150     // Test with writable session.
151     $this->assertSame(TRUE, $this->sessionHandler->isSessionWritable());
152     $actual_result = call_user_func_array([$this->sessionHandler, $method], $args);
153     $this->assertSame($expected_result, $actual_result);
154
155     // Test with non-writable session.
156     $this->sessionHandler->setSessionWritable(FALSE);
157     $this->assertSame(FALSE, $this->sessionHandler->isSessionWritable());
158     $actual_result = call_user_func_array([$this->sessionHandler, $method], $args);
159     $this->assertSame($expected_result, $actual_result);
160   }
161
162   /**
163    * Provides test data for the other methods test.
164    *
165    * @return array
166    *   Test data.
167    */
168   public function providerTestOtherMethods() {
169     return [
170       ['open', TRUE, ['/some/path', 'some-session-id']],
171       ['read', 'some-session-data', ['a-session-id']],
172       ['close', TRUE, []],
173       ['destroy', TRUE, ['old-session-id']],
174       ['gc', TRUE, [42]],
175     ];
176   }
177
178 }