1 session.test SessionTestCase::testEmptyAnonymousSession()

Test that empty anonymous sessions are destroyed.

File

core/modules/simpletest/tests/session.test, line 129
Provides SimpleTests for core session handling functionality.

Class

SessionTestCase

Code

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

  // The same behavior is expected when caching is enabled.
  config_set('system.core', 'cache', 1);
  $this->backdropGet('');
  $this->assertSessionCookie(FALSE);
  $this->assertSessionEmpty(TRUE);
  $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'MISS', 'Page was not cached.');

  // Start a new session by setting a message.
  $this->backdropGet('session-test/set-message');
  $this->assertSessionCookie(TRUE);
  $this->assertTrue($this->backdropGetHeader('Set-Cookie'), 'New session was started.');

  // Display the message, during the same request the session is destroyed
  // and the session cookie is unset.
  $this->backdropGet('');
  $this->assertSessionCookie(FALSE);
  $this->assertSessionEmpty(FALSE);
  $this->assertFalse($this->backdropGetHeader('X-Backdrop-Cache'), 'Caching was bypassed.');
  $this->assertText(t('This is a dummy message.'), 'Message was displayed.');
  $this->assertTrue(preg_match('/SESS\w+=deleted/', $this->backdropGetHeader('Set-Cookie')), 'Session cookie was deleted.');

  // Verify that session was destroyed.
  $this->backdropGet('');
  $this->assertSessionCookie(FALSE);
  $this->assertSessionEmpty(TRUE);
  $this->assertNoText(t('This is a dummy message.'), 'Message was not cached.');
  $this->assertEqual($this->backdropGetHeader('X-Backdrop-Cache'), 'HIT', 'Page was cached.');
  $this->assertFalse($this->backdropGetHeader('Set-Cookie'), 'New session was not started.');

  // Verify that no session is created if backdrop_save_session(FALSE) is called.
  $this->backdropGet('session-test/set-message-but-dont-save');
  $this->assertSessionCookie(FALSE);
  $this->assertSessionEmpty(TRUE);

  // Verify that no message is displayed.
  $this->backdropGet('');
  $this->assertSessionCookie(FALSE);
  $this->assertSessionEmpty(TRUE);
  $this->assertNoText(t('This is a dummy message.'), 'The message was not saved.');
}