session_cookie_lifetime.test in Session cookie lifetime 7
File
tests/session_cookie_lifetime.test
View source
<?php
class sessionCookieLifetimeTestCase extends DrupalWebTestCase {
protected $adminUser;
public static function getInfo() {
return array(
'name' => 'Session cookie lifetime',
'description' => 'Setting custom session cookie lifetime functionality',
'group' => 'Session',
);
}
public function setUp() {
parent::setUp('session_cookie_lifetime');
$this->adminUser = $this
->drupalCreateUser(array(
'administer site configuration',
));
}
public function testLifetimeBrowserSession() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/system/session_cookie_lifetime');
$post = array(
'session_cookie_lifetime_type' => 'browser_session',
);
$this
->drupalPost('admin/config/system/session_cookie_lifetime', $post, t('Save configuration'));
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$this
->assertSessionExpiry(0, t('Expires tag not found'));
}
public function testLifetimeTimebased() {
$this
->drupalLogin($this->adminUser);
$number_of_days = rand(10, 1000);
$post = array(
'session_cookie_lifetime_type' => 'time',
'session_cookie_lifetime_amount' => $number_of_days,
'session_cookie_lifetime_multiplier' => 60 * 60 * 24,
);
$this
->drupalPost('admin/config/system/session_cookie_lifetime', $post, t('Save configuration'));
$this
->drupalLogout();
$this
->drupalLogin($this->adminUser);
$expiry_time = $number_of_days * 60 * 60 * 24;
$this
->assertSessionExpiry($expiry_time, t('Expires tag is set to !amount days', array(
'!amount' => $number_of_days,
)));
}
protected function assertSessionExpiry($session_lifetime, $message = '') {
$cookie_string = $this
->drupalGetHeader('set-cookie', TRUE);
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');
$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);
}
}
}