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() {
$this->wrappedSessionHandler = $this
->createMock('SessionHandlerInterface');
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler);
}
public function testConstructWriteSafeSessionHandlerDefaultArgs() {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this
->assertSame(TRUE, $this->sessionHandler
->isSessionWritable());
$this->wrappedSessionHandler
->expects($this
->at(0))
->method('write')
->with($session_id, $session_data)
->will($this
->returnValue(TRUE));
$this->wrappedSessionHandler
->expects($this
->at(1))
->method('write')
->with($session_id, $session_data)
->will($this
->returnValue(FALSE));
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertSame(TRUE, $result);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertSame(FALSE, $result);
}
public function testConstructWriteSafeSessionHandlerDisableWriting() {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
$this
->assertSame(FALSE, $this->sessionHandler
->isSessionWritable());
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertSame(TRUE, $result);
}
public function testSetSessionWritable() {
$session_id = 'some-id';
$session_data = 'serialized-session-data';
$this
->assertSame(TRUE, $this->sessionHandler
->isSessionWritable());
$this->sessionHandler
->setSessionWritable(FALSE);
$this
->assertSame(FALSE, $this->sessionHandler
->isSessionWritable());
$this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
$this
->assertSame(FALSE, $this->sessionHandler
->isSessionWritable());
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertSame(TRUE, $result);
$this->sessionHandler
->setSessionWritable(TRUE);
$this
->assertSame(TRUE, $this->sessionHandler
->isSessionWritable());
$this->wrappedSessionHandler
->expects($this
->at(0))
->method('write')
->with($session_id, $session_data)
->will($this
->returnValue(TRUE));
$this->wrappedSessionHandler
->expects($this
->at(1))
->method('write')
->with($session_id, $session_data)
->will($this
->returnValue(FALSE));
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertSame(TRUE, $result);
$result = $this->sessionHandler
->write($session_id, $session_data);
$this
->assertSame(FALSE, $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
->assertSame(TRUE, $this->sessionHandler
->isSessionWritable());
$actual_result = call_user_func_array([
$this->sessionHandler,
$method,
], $args);
$this
->assertSame($expected_result, $actual_result);
$this->sessionHandler
->setSessionWritable(FALSE);
$this
->assertSame(FALSE, $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,
],
],
];
}
}