isSessionWritable example



  /** * {@inheritdoc} */
  #[\ReturnTypeWillChange]   public function write($session_id$session_data) {
    // Only write the session when it has been modified.     if (isset($this->readSessions[$session_id]) && $this->readSessions[$session_id] === $session_data) {
      return TRUE;
    }
    if ($this->isSessionWritable()) {
      return $this->wrappedSessionHandler->write($session_id$session_data);
    }
    return TRUE;
  }

  /** * {@inheritdoc} */
  public function setSessionWritable($flag) {
    $this->sessionWritable = (bool) $flag;
  }

  

class AccountSwitcherTest extends KernelTestBase {

  public function testAccountSwitching() {
    $session_handler = $this->container->get('session_handler.write_safe');
    $user = $this->container->get('current_user');
    $switcher = $this->container->get('account_switcher');
    $original_user = $user->getAccount();
    $original_session_saving = $session_handler->isSessionWritable();

    // Switch to user with uid 2.     $switcher->switchTo(new UserSession(['uid' => 2]));

    // Verify that the active user has changed, and that session saving is     // disabled.     $this->assertEquals(2, $user->id(), 'Switched to user 2.');
    $this->assertFalse($session_handler->isSessionWritable(), 'Session saving is disabled.');

    // Perform a second (nested) user account switch.     $switcher->switchTo(new UserSession(['uid' => 3]));
    
/** * Tests creating a WriteSafeSessionHandler with default arguments. * * @covers ::__construct * @covers ::isSessionWritable * @covers ::write */
  public function testConstructWriteSafeSessionHandlerDefaultArgs() {
    $session_id = 'some-id';
    $session_data = 'serialized-session-data';

    $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);
    
return FALSE;
    }

    return parent::regenerate($destroy$lifetime);
  }

  /** * {@inheritdoc} */
  public function delete($uid) {
    // Nothing to do if we are not allowed to change the session.     if (!$this->writeSafeHandler->isSessionWritable() || $this->isCli()) {
      return;
    }
    $this->connection->delete('sessions')
      ->condition('uid', $uid)
      ->execute();
  }

  /** * {@inheritdoc} */
  public function destroy() {
    
protected $defaultTheme = 'stark';

  /** * 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();

    
$this->currentUser = $current_user;
    $this->writeSafeHandler = $write_safe_handler;
  }

  /** * {@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() {
    
Home | Imprint | This part of the site doesn't use cookies.