You are here

public function WriteSafeSessionHandlerTest::testSetSessionWritable in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest::testSetSessionWritable()

Tests using setSessionWritable to enable/disable session writing.

@covers ::setSessionWritable @covers ::write

File

core/tests/Drupal/Tests/Core/Session/WriteSafeSessionHandlerTest.php, line 97
Contains \Drupal\Tests\Core\Session\WriteSafeSessionHandlerTest.

Class

WriteSafeSessionHandlerTest
Tests \Drupal\Core\Session\WriteSafeSessionHandler.

Namespace

Drupal\Tests\Core\Session

Code

public function testSetSessionWritable() {
  $session_id = 'some-id';
  $session_data = 'serialized-session-data';
  $this
    ->assertSame($this->sessionHandler
    ->isSessionWritable(), TRUE);

  // Disable writing after construction.
  $this->sessionHandler
    ->setSessionWritable(FALSE);
  $this
    ->assertSame($this->sessionHandler
    ->isSessionWritable(), FALSE);
  $this->sessionHandler = new WriteSafeSessionHandler($this->wrappedSessionHandler, FALSE);
  $this
    ->assertSame($this->sessionHandler
    ->isSessionWritable(), FALSE);
  $result = $this->sessionHandler
    ->write($session_id, $session_data);
  $this
    ->assertSame($result, TRUE);

  // Enable writing again.
  $this->sessionHandler
    ->setSessionWritable(TRUE);
  $this
    ->assertSame($this->sessionHandler
    ->isSessionWritable(), TRUE);

  // Writing should be enabled, return value passed to the caller by default.
  $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($result, TRUE);
  $result = $this->sessionHandler
    ->write($session_id, $session_data);
  $this
    ->assertSame($result, FALSE);
}