3 namespace Drupal\Tests\Core\Session;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\Session\WriteSafeSessionHandler;
9 * Tests \Drupal\Core\Session\WriteSafeSessionHandler.
11 * @coversDefaultClass \Drupal\Core\Session\WriteSafeSessionHandler
14 class WriteSafeSessionHandlerTest extends UnitTestCase {
17 * The wrapped session handler.
19 * @var \SessionHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
21 protected $wrappedSessionHandler;
24 * The write safe session handler.
26 * @var \Drupal\Core\Session\WriteSafeSessionHandler
28 protected $sessionHandler;
30 protected function setUp() {
31 $this->wrappedSessionHandler = $this->getMock('SessionHandlerInterface');
32 $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler);
36 * Tests creating a WriteSafeSessionHandler with default arguments.
38 * @covers ::__construct
39 * @covers ::isSessionWritable
42 public function testConstructWriteSafeSessionHandlerDefaultArgs() {
43 $session_id = 'some-id';
44 $session_data = 'serialized-session-data';
46 $this->assertSame(TRUE, $this->sessionHandler->isSessionWritable());
48 // Writing should be enabled, return value passed to the caller by default.
49 $this->wrappedSessionHandler->expects($this->at(0))
51 ->with($session_id, $session_data)
52 ->will($this->returnValue(TRUE));
54 $this->wrappedSessionHandler->expects($this->at(1))
56 ->with($session_id, $session_data)
57 ->will($this->returnValue(FALSE));
59 $result = $this->sessionHandler->write($session_id, $session_data);
60 $this->assertSame(TRUE, $result);
62 $result = $this->sessionHandler->write($session_id, $session_data);
63 $this->assertSame(FALSE, $result);
67 * Tests creating a WriteSafeSessionHandler with session writing disabled.
69 * @covers ::__construct
70 * @covers ::isSessionWritable
73 public function testConstructWriteSafeSessionHandlerDisableWriting() {
74 $session_id = 'some-id';
75 $session_data = 'serialized-session-data';
77 // Disable writing upon construction.
78 $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
80 $this->assertSame(FALSE, $this->sessionHandler->isSessionWritable());
82 $result = $this->sessionHandler->write($session_id, $session_data);
83 $this->assertSame(TRUE, $result);
87 * Tests using setSessionWritable to enable/disable session writing.
89 * @covers ::setSessionWritable
92 public function testSetSessionWritable() {
93 $session_id = 'some-id';
94 $session_data = 'serialized-session-data';
96 $this->assertSame(TRUE, $this->sessionHandler->isSessionWritable());
98 // Disable writing after construction.
99 $this->sessionHandler->setSessionWritable(FALSE);
100 $this->assertSame(FALSE, $this->sessionHandler->isSessionWritable());
102 $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
104 $this->assertSame(FALSE, $this->sessionHandler->isSessionWritable());
106 $result = $this->sessionHandler->write($session_id, $session_data);
107 $this->assertSame(TRUE, $result);
109 // Enable writing again.
110 $this->sessionHandler->setSessionWritable(TRUE);
111 $this->assertSame(TRUE, $this->sessionHandler->isSessionWritable());
113 // Writing should be enabled, return value passed to the caller by default.
114 $this->wrappedSessionHandler->expects($this->at(0))
116 ->with($session_id, $session_data)
117 ->will($this->returnValue(TRUE));
119 $this->wrappedSessionHandler->expects($this->at(1))
121 ->with($session_id, $session_data)
122 ->will($this->returnValue(FALSE));
124 $result = $this->sessionHandler->write($session_id, $session_data);
125 $this->assertSame(TRUE, $result);
127 $result = $this->sessionHandler->write($session_id, $session_data);
128 $this->assertSame(FALSE, $result);
132 * Tests that other invocations are passed unmodified to the wrapped handler.
134 * @covers ::setSessionWritable
140 * @dataProvider providerTestOtherMethods
142 public function testOtherMethods($method, $expected_result, $args) {
143 $invocation = $this->wrappedSessionHandler->expects($this->exactly(2))
145 ->will($this->returnValue($expected_result));
147 // Set the parameter matcher.
148 call_user_func_array([$invocation, 'with'], $args);
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);
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);
163 * Provides test data for the other methods test.
168 public function providerTestOtherMethods() {
170 ['open', TRUE, ['/some/path', 'some-session-id']],
171 ['read', 'some-session-data', ['a-session-id']],
173 ['destroy', TRUE, ['old-session-id']],