You are here

function SessionTest::testEmptyAnonymousSession in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Session/SessionTest.php \Drupal\system\Tests\Session\SessionTest::testEmptyAnonymousSession()

Test that empty anonymous sessions are destroyed.

File

core/modules/system/src/Tests/Session/SessionTest.php, line 155
Contains \Drupal\system\Tests\Session\SessionTest.

Class

SessionTest
Drupal session handling tests.

Namespace

Drupal\system\Tests\Session

Code

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
    ->assertTrue($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
    ->assertFalse($this
    ->drupalGetHeader('X-Drupal-Cache'), 'Caching was bypassed.');
  $this
    ->assertText(t('This is a dummy message.'), 'Message was displayed.');
  $this
    ->assertTrue(preg_match('/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
    ->assertFalse($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-dont-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.');
}