You are here

public function AuthcacheVarnishTestCase::testGetKeyMenuCallback in Authenticated User Page Caching (Authcache) 7.2

Cover authcache_varnish_get_key menu callback.

File

modules/authcache_varnish/authcache_varnish.test, line 107
Test cases for authcache_varnish module.

Class

AuthcacheVarnishTestCase
Cover authcache_varnish module.

Code

public function testGetKeyMenuCallback() {

  // Disable request validation for this test.
  variable_set('authcache_varnish_header', FALSE);
  variable_set('authcache_varnish_validate_reverse_proxy_address', FALSE);
  $user = $this
    ->drupalCreateUser();

  // Work around #1873606
  user_save($user, array(
    'roles' => array(
      DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
    ),
  ));

  // Test key retrieval for anonymous user, caching disabled.
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must not be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');

  // Test key retrieval for anonymous user, caching disabled, custom key ttl.
  variable_set('authcache_key_lifetime', 42);
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must not be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');
  variable_del('authcache_key_lifetime');
  $this
    ->drupalLogin($user);

  // Test key retrieval for authenticated user, caching disabled.
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must not be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');

  // Test key retrieval for authenticated user, caching disabled, custom key
  // ttl.
  variable_set('authcache_key_lifetime', 42);
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertFalse($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must not be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');
  variable_del('authcache_key_lifetime');
  $this
    ->drupalLogout();
  variable_set('authcache_roles', array(
    DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
    DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
  ));

  // Test key retrieval for anonymous user, caching enabled.
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertTrue($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');

  // Test key retrieval for anonymous user, caching enabled, custom key ttl.
  variable_set('authcache_key_lifetime', 42);
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertTrue($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');
  variable_del('authcache_key_lifetime');
  $this
    ->drupalLogin($user);

  // Test key retrieval for authenticated user, caching enabled.
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertTrue($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');

  // Test key retrieval for authenticated user, caching enabled, custom key
  // ttl.
  variable_set('authcache_key_lifetime', 42);
  $result = $this
    ->drupalGet('authcache-varnish-get-key');
  $this
    ->assertResponse(200);
  $this
    ->assertEqual('', $result);
  $this
    ->assertVary('X-Authcache-Key-CID');
  $this
    ->assertTrue($this
    ->drupalGetHeader('X-Authcache-Key'), 'X-Authcache-Key header must be present on response.');
  $this
    ->assertCacheControl('public');
  $this
    ->assertCacheControl('max-age=' . authcache_key_lifetime());
  $this
    ->assertNoCacheControl('must-revalidate');
  $this
    ->assertNoCacheControl('no-cache');
  variable_del('authcache_key_lifetime');
  $this
    ->drupalLogout();
}