You are here

public function SessionTest::testDataPersistence in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testDataPersistence()
  2. 9 core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testDataPersistence()

Tests data persistence via the session_test module callbacks.

File

core/modules/system/tests/src/Functional/Session/SessionTest.php, line 85

Class

SessionTest
Drupal session handling tests.

Namespace

Drupal\Tests\system\Functional\Session

Code

public function testDataPersistence() {
  $user = $this
    ->drupalCreateUser([]);

  // Enable sessions.
  $this
    ->sessionReset($user
    ->id());
  $this
    ->drupalLogin($user);
  $value_1 = $this
    ->randomMachineName();

  // Verify that the session value is stored.
  $this
    ->drupalGet('session-test/set/' . $value_1);
  $this
    ->assertSession()
    ->pageTextContains($value_1);

  // Verify that the session correctly returned the stored data for an
  // authenticated user.
  $this
    ->drupalGet('session-test/get');
  $this
    ->assertSession()
    ->pageTextContains($value_1);

  // Attempt to write over val_1. If drupal_save_session(FALSE) is working.
  // properly, val_1 will still be set.
  $value_2 = $this
    ->randomMachineName();

  // Verify that the session value is correctly passed to
  // session-test/no-set.
  $this
    ->drupalGet('session-test/no-set/' . $value_2);
  $session = $this
    ->getSession();
  $this
    ->assertSession()
    ->pageTextContains($value_2);

  // Verify that the session data is not saved for drupal_save_session(FALSE).
  $this
    ->drupalGet('session-test/get');
  $this
    ->assertSession()
    ->pageTextContains($value_1);

  // Switch browser cookie to anonymous user, then back to user 1.
  $session_cookie_name = $this
    ->getSessionName();
  $session_cookie_value = $session
    ->getCookie($session_cookie_name);
  $session
    ->restart();
  $this
    ->initFrontPage();

  // Session restart always resets all the cookies by design, so we need to
  // add the old session cookie again.
  $session
    ->setCookie($session_cookie_name, $session_cookie_value);

  // Verify that the session data persists through browser close.
  $this
    ->drupalGet('session-test/get');
  $this
    ->assertSession()
    ->pageTextContains($value_1);
  $this->mink
    ->setDefaultSessionName('default');

  // Logout the user and make sure the stored value no longer persists.
  $this
    ->drupalLogout();
  $this
    ->sessionReset();

  // Verify that after logout, previous user's session data is not available.
  $this
    ->drupalGet('session-test/get');
  $this
    ->assertSession()
    ->pageTextNotContains($value_1);

  // Now try to store some data as an anonymous user.
  $value_3 = $this
    ->randomMachineName();

  // Verify that session data is stored for anonymous user.
  $this
    ->drupalGet('session-test/set/' . $value_3);
  $this
    ->assertSession()
    ->pageTextContains($value_3);

  // Verify that session correctly returns the stored data for an anonymous
  // user.
  $this
    ->drupalGet('session-test/get');
  $this
    ->assertSession()
    ->pageTextContains($value_3);

  // Try to store data when drupal_save_session(FALSE).
  $value_4 = $this
    ->randomMachineName();

  // Verify that the session value is correctly passed to session-test/no-set.
  $this
    ->drupalGet('session-test/no-set/' . $value_4);
  $this
    ->assertSession()
    ->pageTextContains($value_4);

  // Verify that the session data is not saved for drupal_save_session(FALSE).
  $this
    ->drupalGet('session-test/get');
  $this
    ->assertSession()
    ->pageTextContains($value_3);

  // Login, the data should persist.
  $this
    ->drupalLogin($user);
  $this
    ->sessionReset($user
    ->id());

  // Verify that the session persists for an authenticated user after
  // logging out and then back in.
  $this
    ->drupalGet('session-test/get');
  $this
    ->assertSession()
    ->pageTextNotContains($value_1);

  // Change session and create another user.
  $user2 = $this
    ->drupalCreateUser([]);
  $this
    ->sessionReset($user2
    ->id());
  $this
    ->drupalLogin($user2);
}