function SessionTest::testEmptyAnonymousSession

Test that empty anonymous sessions are destroyed.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Session/SessionTest.php, line 145
Definition of Drupal\system\Tests\Session\SessionTest.

Class

SessionTest

Namespace

Drupal\system\Tests\Session

Code

function testEmptyAnonymousSession() {

  // Verify that no session is automatically created for anonymous user.
  $this
    ->drupalGet('');
  $this
    ->assertSessionCookie(FALSE);
  $this
    ->assertSessionEmpty(TRUE);

  // The same behavior is expected when caching is enabled.
  $config = config('system.performance');
  $config
    ->set('cache.page.use_internal', 1);
  $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.');
}