You are here

public function WriteSafeSessionHandlerTest::testSetSessionWritable in Drupal 9

Same name and namespace in other branches
  1. 8 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 87

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
    ->assertTrue($this->sessionHandler
    ->isSessionWritable());

  // Disable writing after construction.
  $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);

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

  // Writing should be enabled, return value passed to the caller by default.
  $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);
}