View source
<?php
namespace Drupal\Tests\Core\Session;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Session\WriteSafeSessionHandler;
class WriteSafeSessionHandlerTest extends UnitTestCase {
protected $wrappedSessionHandler;
protected $sessionHandler;
protected function setUp() : void {
$this->wrappedSessionHandler = $this
->createMock('SessionHandlerInterface');
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler);
}
public function testConstructWriteSafeSessionHandlerDefaultArgs() {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this
->assertTrue($this->sessionHandler
->isSessionWritable());
$this->wrappedSessionHandler
->expects($this
->exactly(2))
->method('write')
->with($session_id, $session_data)
->willReturnOnConsecutiveCalls(TRUE, FALSE);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertTrue($result);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertFalse($result);
}
public function testConstructWriteSafeSessionHandlerDisableWriting() {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
$this
->assertFalse($this->sessionHandler
->isSessionWritable());
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertTrue($result);
}
public function testSetSessionWritable() {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this
->assertTrue($this->sessionHandler
->isSessionWritable());
$this->sessionHandler
->setSessionWritable(FALSE);
$this
->assertFalse($this->sessionHandler
->isSessionWritable());
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
$this
->assertFalse($this->sessionHandler
->isSessionWritable());
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertTrue($result);
$this->sessionHandler
->setSessionWritable(TRUE);
$this
->assertTrue($this->sessionHandler
->isSessionWritable());
$this->wrappedSessionHandler
->expects($this
->exactly(2))
->method('write')
->with($session_id, $session_data)
->willReturnOnConsecutiveCalls(TRUE, FALSE);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertTrue($result);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertFalse($result);
}
public function testOtherMethods($method, $expected_result, $args) {
$invocation = $this->wrappedSessionHandler
->expects($this
->exactly(2))
->method($method)
->will($this
->returnValue($expected_result));
call_user_func_array([
$invocation,
'with',
], $args);
$this
->assertTrue($this->sessionHandler
->isSessionWritable());
$actual_result = call_user_func_array([
$this->sessionHandler,
$method,
], $args);
$this
->assertSame($expected_result, $actual_result);
$this->sessionHandler
->setSessionWritable(FALSE);
$this
->assertFalse($this->sessionHandler
->isSessionWritable());
$actual_result = call_user_func_array([
$this->sessionHandler,
$method,
], $args);
$this
->assertSame($expected_result, $actual_result);
}
public function providerTestOtherMethods() {
return [
[
'open',
TRUE,
[
'/some/path',
'some-session-id',
],
],
[
'read',
'some-session-data',
[
'a-session-id',
],
],
[
'close',
TRUE,
[],
],
[
'destroy',
TRUE,
[
'old-session-id',
],
],
[
'gc',
TRUE,
[
42,
],
],
];
}
}