You are here

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

Test custom page caching rules (variable: authcache_pagecaching).

File

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

Class

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

Code

public function testCustomPagecaching() {

  // Page caching rules empty.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_pagecaching' => array(),
  ));

  // Never allow caching when no page caching rules are configured.
  $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);

  // Page caching rules: Allow all roles on default page set.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_pagecaching' => array(
      array(
        'option' => 0,
        'pages' => AUTHCACHE_NOCACHE_DEFAULT,
        'noadmin' => 1,
        'roles' => array(
          'custom' => TRUE,
          'roles' => drupal_map_assoc(array_keys(user_roles())),
        ),
      ),
    ),
  ));

  // Allow page caching for all except superuser.
  $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();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $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->adminUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $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(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(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->adminUser);
  $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->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Deny page caching on admin pages.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('admin', $this->adminUser);
  $this
    ->assertAuthcacheExcluded(t('Not caching admin pages (by page ruleset #@number)', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('admin', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Page caching rules: Allow all roles on default page set, including admin
  // pages.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_pagecaching' => array(
      array(
        'option' => 0,
        'pages' => AUTHCACHE_NOCACHE_DEFAULT,
        'noadmin' => 0,
        'roles' => array(
          'custom' => TRUE,
          'roles' => drupal_map_assoc(array_keys(user_roles())),
        ),
      ),
    ),
  ));

  // Allow page caching for all except superuser.
  $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();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $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->adminUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $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(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(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->adminUser);
  $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->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Deny page caching on admin pages.
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('admin/config', $this->adminUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('admin/config', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Page caching rules: Simple blacklist.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_pagecaching' => array(
      array(
        'option' => 0,
        'pages' => 'authcache-test-page-two',
        'noadmin' => 1,
        'roles' => array(
          'custom' => TRUE,
          'roles' => drupal_map_assoc(array_keys(user_roles())),
        ),
      ),
    ),
  ));

  // Allow page caching for all except superuser.
  $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();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $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->adminUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Allow page caching for all except superuser.
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', 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('cart/authcache-test-default-nocache', $this->plainUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->webUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->adminUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('cart/authcache-test-default-nocache', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Deny page caching on excluded pages.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->plainUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->webUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->adminUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Page caching rules: Simple whitelist.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_pagecaching' => array(
      array(
        'option' => 1,
        'pages' => 'authcache-test-page-one',
        'noadmin' => 1,
        'roles' => array(
          'custom' => TRUE,
          'roles' => drupal_map_assoc(array_keys(user_roles())),
        ),
      ),
    ),
  ));

  // Allow page caching for all except superuser.
  $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();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $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->adminUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Deny page caching for pages not in the whitelist.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', drupal_anonymous_user());
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->plainUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->webUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->adminUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 1,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Combination of page caching rules: Standard rules for anonymous and
  // authenticated users, more restrictions for privileged users.
  $privileged_roles = user_roles();
  unset($privileged_roles[DRUPAL_ANONYMOUS_RID]);
  unset($privileged_roles[DRUPAL_AUTHENTICATED_RID]);
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_pagecaching' => array(
      array(
        'option' => 0,
        'pages' => AUTHCACHE_NOCACHE_DEFAULT,
        'noadmin' => 1,
        'roles' => array(
          'custom' => TRUE,
          'roles' => array(
            DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID,
            DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
          ),
        ),
      ),
      array(
        'option' => 0,
        'pages' => AUTHCACHE_NOCACHE_DEFAULT . "\nauthcache-test-page-two\n",
        'noadmin' => 1,
        'roles' => array(
          'custom' => TRUE,
          'roles' => drupal_map_assoc(array_keys($privileged_roles)),
        ),
      ),
    ),
  ));

  // Allow page caching for all except superuser.
  $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();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-one', $this->webUser);
  $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->adminUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-one', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Allow page caching for anonymous and authenticated roles but not for
  // special roles on second page.
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-page-two', 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-two', $this->plainUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->webUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 2,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->adminUser);
  $this
    ->assertAuthcacheExcluded(t('Caching disabled by path list of page ruleset #@number', array(
    '@number' => 2,
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-page-two', $this->superUser);
  $this
    ->assertAuthcacheExcluded();

  // Page caching rules: PHP.
  module_enable(array(
    'php',
  ));
  $this
    ->assertTrue(module_exists('php'));
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
    'authcache_pagecaching' => array(
      array(
        'option' => 2,
        'pages' => 'return variable_get(\'authcache_test_pagecaching_allow\');',
        'noadmin' => 1,
        'roles' => array(
          'custom' => TRUE,
          'roles' => drupal_map_assoc(array_keys(user_roles())),
        ),
      ),
    ),
  ));

  // Allow page caching based on custom PHP code.
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  variable_set('authcache_test_pagecaching_allow', TRUE);
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());

  // Allow page caching based on custom PHP code.
  $this
    ->resetTestVariables();
  variable_set('authcache_test_pagecaching_allow', FALSE);
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheNotExcluded(t('Caching disabled by PHP rule of page ruleset #@number', array(
    '@number' => 1,
  )));
}