public function AuthcacheCookiesTestCase::testCookieHooks in Authenticated User Page Caching (Authcache) 7.2
Verify that hook_authcache_cookie is called on every request.
File
- ./
authcache.test, line 1818 - Tests for system.module.
Class
- AuthcacheCookiesTestCase
- Test cookie management.
Code
public function testCookieHooks() {
$this
->setupConfig(array(
'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
));
// Test for cacheable anonymous user on cacheable page.
$this
->resetTestVariables();
$this
->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
$cookie_uid = variable_get('authcache_test_authcache_cookie');
$this
->assertEqual(0, $cookie_uid, t('hook_authcache_cookie called for anonymous user'));
list($alter_cookies, $alter_cookie_uid) = variable_get('authcache_test_authcache_cookie_alter');
$this
->assertEqual(0, $alter_cookie_uid, t('hook_authcache_cookie_alter called for anonymous user'));
// Test for cacheable authenticated user on cacheable page.
$this
->resetTestVariables();
$this
->authcacheGet('authcache-test-page-one', $this->plainUser);
$cookie_uid = variable_get('authcache_test_authcache_cookie');
$this
->assertEqual($this->plainUser->uid, $cookie_uid, t('hook_authcache_cookie called for anonymous user'));
list($alter_cookies, $alter_cookie_uid) = variable_get('authcache_test_authcache_cookie_alter');
$this
->assertEqual($this->plainUser->uid, $alter_cookie_uid, t('hook_authcache_cookie_alter called for plain user'));
// Ensure that this even works for superuser.
$this
->resetTestVariables();
$this
->authcacheGet('authcache-test-page-one', $this->superUser);
$cookie_uid = variable_get('authcache_test_authcache_cookie');
$this
->assertEqual($this->superUser->uid, $cookie_uid, t('hook_authcache_cookie called for anonymous user'));
list($alter_cookies, $alter_cookie_uid) = variable_get('authcache_test_authcache_cookie_alter');
$this
->assertEqual($this->superUser->uid, $alter_cookie_uid, t('hook_authcache_cookie_alter called for super user'));
// Ensure that cookies set via hook_authcache_cookie show up in
// hook_authcache_cookie_alter.
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie', array(
$cookie_name => array(
'present' => TRUE,
'value' => $cookie_value,
'lifetime' => 3600,
'path' => $cookie_path,
),
));
$this
->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
list($alter_cookies, $alter_cookie_uid) = variable_get('authcache_test_authcache_cookie_alter');
$this
->assertEqual($cookie_value, $alter_cookies[$cookie_name]['value']);
$this
->assertEqual($cookie_path, $alter_cookies[$cookie_name]['path']);
// Test whether cookies set using hook_authcache_cookie are present on the
// response.
//
// With lifetime.
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie', array(
$cookie_name => array(
'present' => TRUE,
'value' => $cookie_value,
'lifetime' => 3600,
'path' => $cookie_path,
),
));
$now = time();
$this
->drupalGet('authcache-test-page-one');
$response_cookies = $this
->extractSetCookies($this->headers);
$this
->assertEqual($response_cookies[$cookie_name]['value'], $cookie_value);
$timestamp = strtotime($response_cookies[$cookie_name]['Expires']);
$this
->assertTrue($timestamp >= $now - 3600 * 0.1);
$this
->assertTrue($timestamp <= $now + 3600 * 1.1);
$this
->assertEqual($response_cookies[$cookie_name]['Path'], $cookie_path);
// No lifetime (delete when browser window closes).
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie', array(
$cookie_name => array(
'present' => TRUE,
'value' => $cookie_value,
'lifetime' => 0,
'path' => $cookie_path,
),
));
$this
->drupalGet('authcache-test-page-one');
$response_cookies = $this
->extractSetCookies($this->headers);
$this
->assertEqual($response_cookies[$cookie_name]['value'], $cookie_value);
$this
->assertTrue(!isset($response_cookies[$cookie_name]['Expires']));
$this
->assertEqual($response_cookies[$cookie_name]['Path'], $cookie_path);
// HTTP only flag.
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie', array(
$cookie_name => array(
'present' => TRUE,
'value' => $cookie_value,
'path' => $cookie_path,
'httponly' => TRUE,
),
));
$this
->drupalGet('authcache-test-page-one');
$response_cookies = $this
->extractSetCookies($this->headers);
$this
->assertEqual($cookie_value, $response_cookies[$cookie_name]['value']);
$this
->assertTrue($response_cookies[$cookie_name]['HttpOnly']);
// Secure flag.
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie', array(
$cookie_name => array(
'present' => TRUE,
'value' => $cookie_value,
'path' => $cookie_path,
'secure' => TRUE,
),
));
$this
->drupalGet('authcache-test-page-one');
$response_cookies = $this
->extractSetCookies($this->headers);
$this
->assertEqual($cookie_value, $response_cookies[$cookie_name]['value']);
$this
->assertTrue($response_cookies[$cookie_name]['Secure']);
// SameSite attribute.
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie', array(
$cookie_name => array(
'present' => TRUE,
'value' => $cookie_value,
'path' => $cookie_path,
'samesite' => 'Strict',
),
));
$this
->drupalGet('authcache-test-page-one');
$response_cookies = $this
->extractSetCookies($this->headers);
$this
->assertEqual($cookie_value, $response_cookies[$cookie_name]['value']);
$this
->assertEqual('Strict', $response_cookies[$cookie_name]['SameSite']);
// Ensure that cookies can be injected using the alter-hook.
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie_alter', array(
$cookie_name,
array(
'present' => TRUE,
'value' => $cookie_value,
'path' => $cookie_path,
),
));
$this
->drupalGet('authcache-test-page-one');
$response_cookies = $this
->extractSetCookies($this->headers);
$this
->assertEqual($response_cookies[$cookie_name]['value'], $cookie_value);
$this
->assertEqual($response_cookies[$cookie_name]['Path'], $cookie_path);
// Ensure that cookies can be deleted from the alter-hook.
$this
->resetTestVariables();
$cookie_name = $this
->randomName(8);
$cookie_value = $this
->randomName(16);
$cookie_path = $this
->randomName(8);
variable_set('authcache_test_cookie', array(
$cookie_name => array(
'present' => TRUE,
'value' => $cookie_value,
'path' => $cookie_path,
),
));
variable_set('authcache_test_cookie_alter', array(
$cookie_name,
array(
'present' => FALSE,
'value' => $cookie_value,
'path' => $cookie_path,
),
));
$this
->drupalGet('authcache-test-page-one');
$response_cookies = $this
->extractSetCookies($this->headers);
$this
->assertFalse(isset($response_cookies[$cookie_name]));
}