setSessionWritable example

/** * Tests session writing and regeneration. * * @covers \Drupal\Core\Session\WriteSafeSessionHandler::setSessionWritable * @covers \Drupal\Core\Session\WriteSafeSessionHandler::isSessionWritable * @covers \Drupal\Core\Session\SessionManager::regenerate */
  public function testSessionSaveRegenerate() {
    $session_handler = $this->container->get('session_handler.write_safe');
    $this->assertTrue($session_handler->isSessionWritable(), 'session_handler->isSessionWritable() initially returns TRUE.');
    $session_handler->setSessionWritable(FALSE);
    $this->assertFalse($session_handler->isSessionWritable(), '$session_handler->isSessionWritable() returns FALSE after disabling.');
    $session_handler->setSessionWritable(TRUE);
    $this->assertTrue($session_handler->isSessionWritable(), '$session_handler->isSessionWritable() returns TRUE after enabling.');

    // Test session hardening code from SA-2008-044.     $user = $this->drupalCreateUser();

    // Enable sessions.     $this->sessionReset();

    // Make sure the session cookie is set as HttpOnly. We can only test this in
/** * Turns off session saving and then tries to save a value anyway. * * @param string $test_value * A session value. * * @return string * A notification message. */
  public function noSet($test_value) {
    \Drupal::service('session_handler.write_safe')->setSessionWritable(FALSE);
    $this->set($test_value);
    return ['#markup' => $this->t('session saving was disabled, and then %val was set', ['%val' => $test_value])];
  }

  /** * Sets a message to me displayed on the following page. * * @return string * A notification message. */
  public function setMessage() {
    


  /** * {@inheritdoc} */
  public function switchTo(AccountInterface $account) {
    // Prevent session information from being saved and push previous account.     if (!isset($this->originalSessionSaving)) {
      // Ensure that only the first session saving status is saved.       $this->originalSessionSaving = $this->writeSafeHandler->isSessionWritable();
    }
    $this->writeSafeHandler->setSessionWritable(FALSE);
    array_push($this->accountStack, $this->currentUser->getAccount());
    $this->currentUser->setAccount($account);
    return $this;
  }

  /** * {@inheritdoc} */
  public function switchBack() {
    // Restore the previous account from the stack.     if (!empty($this->accountStack)) {
      

  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);
    
Home | Imprint | This part of the site doesn't use cookies.