You are here

public function SessionTest::testEmptyAnonymousSession 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::testEmptyAnonymousSession()
  2. 9 core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testEmptyAnonymousSession()

Tests that empty anonymous sessions are destroyed.

File

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

Class

SessionTest
Drupal session handling tests.

Namespace

Drupal\Tests\system\Functional\Session

Code

public function testEmptyAnonymousSession() {

  // Disable the dynamic_page_cache module; it'd cause session_test's debug
  // output (that is added in
  // SessionTestSubscriber::onKernelResponseSessionTest()) to not be added.
  $this->container
    ->get('module_installer')
    ->uninstall([
    'dynamic_page_cache',
  ]);

  // Verify that no session is automatically created for anonymous user when
  // page caching is disabled.
  $this->container
    ->get('module_installer')
    ->uninstall([
    'page_cache',
  ]);
  $this
    ->drupalGet('');
  $this
    ->assertSessionCookie(FALSE);
  $this
    ->assertSessionEmpty(TRUE);

  // The same behavior is expected when caching is enabled.
  $this->container
    ->get('module_installer')
    ->install([
    'page_cache',
  ]);
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->save();
  $this
    ->drupalGet('');
  $this
    ->assertSessionCookie(FALSE);

  // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages.
  // $this->assertSessionEmpty(TRUE);
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache', 'MISS');

  // Start a new session by setting a message.
  $this
    ->drupalGet('session-test/set-message');
  $this
    ->assertSessionCookie(TRUE);
  $this
    ->assertNotNull($this
    ->getSession()
    ->getResponseHeader('Set-Cookie'));

  // Display the message, during the same request the session is destroyed
  // and the session cookie is unset.
  $this
    ->drupalGet('');
  $this
    ->assertSessionCookie(FALSE);
  $this
    ->assertSessionEmpty(FALSE);

  // Verify that caching was bypassed.
  $this
    ->assertSession()
    ->responseHeaderDoesNotExist('X-Drupal-Cache');
  $this
    ->assertSession()
    ->pageTextContains('This is a dummy message.');

  // Verify that session cookie was deleted.
  $this
    ->assertSession()
    ->responseHeaderMatches('Set-Cookie', '/SESS\\w+=deleted/');

  // Verify that session was destroyed.
  $this
    ->drupalGet('');
  $this
    ->assertSessionCookie(FALSE);

  // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages.
  // $this->assertSessionEmpty(TRUE);
  $this
    ->assertSession()
    ->pageTextNotContains('This is a dummy message.');
  $this
    ->assertSession()
    ->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  $this
    ->assertSession()
    ->responseHeaderDoesNotExist('Set-Cookie');

  // Verify that no session is created if drupal_save_session(FALSE) is called.
  $this
    ->drupalGet('session-test/set-message-but-do-not-save');
  $this
    ->assertSessionCookie(FALSE);
  $this
    ->assertSessionEmpty(TRUE);

  // Verify that no message is displayed.
  $this
    ->drupalGet('');
  $this
    ->assertSessionCookie(FALSE);

  // @todo Reinstate when REQUEST and RESPONSE events fire for cached pages.
  // $this->assertSessionEmpty(TRUE);
  $this
    ->assertSession()
    ->pageTextNotContains('This is a dummy message.');
}