You are here

public function AuthcacheCookiesTestCase::testCookieFunction in Authenticated User Page Caching (Authcache) 7.2

Tests for authcache_add_cookie function.

File

./authcache.test, line 2013
Tests for system.module.

Class

AuthcacheCookiesTestCase
Test cookie management.

Code

public function testCookieFunction() {

  // With lifetime.
  $this
    ->resetTestVariables();
  $cookie_name = $this
    ->randomName(8);
  $cookie_value = $this
    ->randomName(16);
  $cookie_path = $this
    ->randomName(8);
  variable_set('authcache_test_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'lifetime' => 3600,
      'path' => $cookie_path,
    ),
  ));
  $now = time();
  $this
    ->drupalGet('authcache-test-add-cookie');
  $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_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'lifetime' => 0,
      'path' => $cookie_path,
    ),
  ));
  $this
    ->drupalGet('authcache-test-add-cookie');
  $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_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'path' => $cookie_path,
      'httponly' => TRUE,
    ),
  ));
  $this
    ->drupalGet('authcache-test-add-cookie');
  $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_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'path' => $cookie_path,
      'secure' => TRUE,
    ),
  ));
  $this
    ->drupalGet('authcache-test-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertEqual($cookie_value, $response_cookies[$cookie_name]['value']);
  $this
    ->assertTrue($response_cookies[$cookie_name]['Secure']);

  // 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_add_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-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertFalse(isset($response_cookies[$cookie_name]));
}