You are here

protected function sessionCookieLifetimeTestCase::assertSessionExpiry in Session cookie lifetime 7

Assert session expiry time, based on the previous request.

Parameters

int $session_lifetime: Session lifetime in seconds.

string $message: Assert text.

2 calls to sessionCookieLifetimeTestCase::assertSessionExpiry()
sessionCookieLifetimeTestCase::testLifetimeBrowserSession in tests/session_cookie_lifetime.test
Test Browser session lifetime session cookie functionality.
sessionCookieLifetimeTestCase::testLifetimeTimebased in tests/session_cookie_lifetime.test
Test time based session cookie functionality.

File

tests/session_cookie_lifetime.test, line 67

Class

sessionCookieLifetimeTestCase

Code

protected function assertSessionExpiry($session_lifetime, $message = '') {
  $cookie_string = $this
    ->drupalGetHeader('set-cookie', TRUE);

  // If session lifetime is 0, there should be no "expires" tag in the header.
  if ($session_lifetime == 0) {
    $found_expires_string = strpos($cookie_string, ' expires=');
    $this
      ->assertFalse($found_expires_string, $message);
  }
  else {
    $matches = array();
    preg_match('/expires=([^;]*)/', $cookie_string, $matches);
    $expires_string = $matches[1];
    $cookie_timestamp = strtotime($expires_string);
    $current_timestamp = time();
    $php_max_execution_time = ini_get('max_execution_time');

    // Compensate with the maximum php execution time.
    $maximum_cookie_timestamp = $current_timestamp + $session_lifetime;
    $minimum_cookie_timestamp = $current_timestamp - $php_max_execution_time + $session_lifetime;
    $valid_timestamp = $maximum_cookie_timestamp >= $cookie_timestamp && $minimum_cookie_timestamp <= $cookie_timestamp;
    $this
      ->assertTrue($valid_timestamp, $message);
  }
}