You are here

public function AuthcachePolicyTestCase::testDefaultExclusionRules in Authenticated User Page Caching (Authcache) 7.2

Test builtin standard cache exclusion rules.

File

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

Class

AuthcachePolicyTestCase
Test cache policy rules, i.e. exclusion and cancelation.

Code

public function testDefaultExclusionRules() {

  // Basic rule 1: Never cache when no backend is available.
  $hook_boot = $this->stubbackend
    ->hook('boot', FALSE);
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded(t('No active cache backend.'));
  $this
    ->assertStub($hook_boot, HookStub::once());
  $this->stubbackend
    ->hook('boot', NULL);

  // Basic rule 2: Never cache when request method is not GET / HEAD.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));

  // For anonymous user.
  $this
    ->resetTestVariables();
  $edit['authcache_test_form_comment'] = $this
    ->randomName(16);
  $this
    ->authcachePost('authcache-test-form', drupal_anonymous_user(), $edit, t('Save configuration'));
  $this
    ->assertAuthcacheExcluded(t('Only GET and HEAD requests allowed. Method for this request is: @method.', array(
    '@method' => 'POST',
  )));

  // For authenticated user.
  $this
    ->resetTestVariables();
  $edit['authcache_test_form_comment'] = $this
    ->randomName(16);
  $this
    ->authcachePost('authcache-test-form', $this->plainUser, $edit, t('Save configuration'));
  $this
    ->assertAuthcacheExcluded(t('Only GET and HEAD requests allowed. Method for this request is: @method.', array(
    '@method' => 'POST',
  )));

  // Basic rule 3: Never cache when Drupal core page caching is turned on.
  variable_set('cache', TRUE);
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded(t('Drupal core page caching for anonymous users active.'));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertAuthcacheExcluded(t('Drupal core page caching for anonymous users active.'));
  variable_del('cache');

  // Basic rule 4: Do not cache when request came in via a whitelisted
  // frontcontroller. Test with an empty list (removing the default entry
  // pointing at index.php).
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_frontcontroller_whitelist' => array(),
  ));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded(t('Not invoked using an allowed front controller.'));

  // Page caching rules.
  //
  // Default configuration, no roles enabled, ensure that pages are not
  // cached for any user.
  $this
    ->setupConfig();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Enable caching for anonymous users.
  $this
    ->setupConfig(array(
    'authcache_roles' => array(
      DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
    ),
  ));
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Pages under /cart are excluded by the default rule-set, ensure that this
  // is respected.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->plainUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Enable caching for authenticated users (without additional role).
  $this
    ->setupConfig(array(
    'authcache_roles' => array(
      DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
    ),
  ));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Pages under /cart are excluded by the default rule-set, ensure that this
  // is respected.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->plainUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Enable caching for role of webUser.
  $roles = $this->webUser->roles;
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
  $this
    ->assertEqual(1, count($roles));
  $rid = reset($roles);
  $this
    ->setupConfig(array(
    'authcache_roles' => array(
      $rid => $rid,
    ),
  ));

  // Even when the role of web-user is enabled, no caching will happen unless
  // web-users role is explicitely added to a page caching ruleset.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $this
    ->assertAuthcacheExcluded(t('Account does not match any page caching rule.'));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Pages under /cart are excluded by the default rule-set, ensure that this
  // is respected.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->plainUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Enable caching for all roles.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));

  // Even when the role of web-user is enabled, no caching will happen unless
  // web-users role is explicitely added to a page caching ruleset.
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Pages under /cart are excluded by the default rule-set, ensure that this
  // is respected.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->plainUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->webUser);
  $this
    ->assertAuthcacheExcluded();
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->adminUser);
  $this
    ->assertAuthcacheExcluded();

  // Account exclusion rule 1: Do not allow superuser.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled for superuser'));

  // ... unless explicitely requested.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_su' => 1,
  ));
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());

  // Account exclusion rule 2: User has at least one non-cacheable role.
  $this
    ->setupConfig(array(
    'authcache_roles' => array(
      DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
      DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
    ),
  ));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $user_roles = $this->webUser->roles;
  unset($user_roles[DRUPAL_AUTHENTICATED_RID]);
  $this
    ->assertEqual(1, count($user_roles));
  $rid = reset($user_roles);
  $roles = implode(', ', array_intersect_key(user_roles(), $user_roles));
  $this
    ->assertAuthcacheExcluded(t('Account has non-cachable role @roles', array(
    '@roles' => $roles,
  )));

  // Account exclusion rule 3: User does not match any page caching rule.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $this
    ->assertAuthcacheExcluded(t('Account does not match any page caching rule.'));
}