function SessionTest::testEmptyAnonymousSession

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testEmptyAnonymousSession()
  2. 10 core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testEmptyAnonymousSession()
  3. 9 core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testEmptyAnonymousSession()
  4. main core/modules/system/tests/src/Functional/Session/SessionTest.php \Drupal\Tests\system\Functional\Session\SessionTest::testEmptyAnonymousSession()

Test that empty anonymous sessions are destroyed.

File

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

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->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
  // Start a new session by setting a message.
  $this->drupalGet('session-test/set-message');
  $this->assertSessionCookie(TRUE);
  $this->assertNotEmpty($this->drupalGetHeader('Set-Cookie'), 'New session was started.');
  // 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);
  $this->assertNull($this->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
  $this->assertText(t('This is a dummy message.'), 'Message was displayed.');
  $this->assertRegExp('/SESS\\w+=deleted/', $this->drupalGetHeader('Set-Cookie'), 'Session cookie was 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->assertNoText(t('This is a dummy message.'), 'Message was not cached.');
  $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
  $this->assertNull($this->drupalGetHeader('Set-Cookie'), 'New session was not started.');
  // 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->assertNoText(t('This is a dummy message.'), 'The message was not saved.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.