You are here

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

Ensure that cookies are set only if necessary on repeating requests.

File

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

Class

AuthcacheCookiesTestCase
Test cookie management.

Code

public function testCookieOnSubsequentRequests() {

  // With lifetime.
  $cookie_name = 'Drupal.authcache_test.' . $this
    ->randomName(8);
  $cookie_value = $this
    ->randomName(16);
  url('', array(
    'prefix' => &$prefix,
  ));
  $cookie_path = (empty($prefix) ? '' : $prefix) . '/';
  $this
    ->resetTestVariables();
  variable_set('authcache_test_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'lifetime' => 3600,
      'path' => $cookie_path,
    ),
  ));

  // On first request, we expect that the cookie is set.
  $this
    ->drupalGet('authcache-test-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertEqual($response_cookies[$cookie_name]['value'], $cookie_value);
  $this
    ->assertEqual($response_cookies[$cookie_name]['Path'], $cookie_path);
  $this
    ->resetTestVariables();
  variable_set('authcache_test_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'lifetime' => 3600,
      'path' => $cookie_path,
    ),
  ));

  // On subsequent request, cookie should not be set again on response.
  $this
    ->drupalGet('authcache-test-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertFalse(isset($response_cookies[$cookie_name]));
  $cookie_value = $this
    ->randomName(16);
  $this
    ->resetTestVariables();
  variable_set('authcache_test_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'lifetime' => 3600,
      'path' => $cookie_path,
    ),
  ));

  // When the value changes, however cookie should be present again on
  // subsequent request.
  $this
    ->drupalGet('authcache-test-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertEqual($response_cookies[$cookie_name]['value'], $cookie_value);
  $this
    ->assertEqual($response_cookies[$cookie_name]['Path'], $cookie_path);
  $this
    ->resetTestVariables();
  variable_set('authcache_test_add_cookie', array(
    $cookie_name => array(
      'present' => TRUE,
      'value' => $cookie_value,
      'lifetime' => 3600,
      'path' => $cookie_path,
    ),
  ));

  // On subsequent request, cookie should not be set again on response.
  $this
    ->drupalGet('authcache-test-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertFalse(isset($response_cookies[$cookie_name]));
  $this
    ->resetTestVariables();
  variable_set('authcache_test_add_cookie', array(
    $cookie_name => array(
      'present' => FALSE,
    ),
  ));

  // Cookies can be removed by setting the 'present' key to false on the
  // cookie record.
  $this
    ->drupalGet('authcache-test-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertEqual($response_cookies[$cookie_name]['value'], 'deleted');
  $this
    ->resetTestVariables();
  variable_set('authcache_test_add_cookie', array(
    $cookie_name => array(
      'present' => FALSE,
    ),
  ));

  // On subsequent request, delete cookie header should not be set again.
  $this
    ->drupalGet('authcache-test-add-cookie');
  $response_cookies = $this
    ->extractSetCookies($this->headers);
  $this
    ->assertFalse(isset($response_cookies[$cookie_name]));
}