You are here

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

Test builtin standard cache cancelation rules.

File

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

Class

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

Code

public function testDefaultCancelationRules() {

  // CSRF-protected forms.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-form', drupal_anonymous_user());
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-form', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Form with CSRF protection on page. Enable authcache_form or implement custom code capable of handling CSRF protected forms.'));

  // Forms with value elements.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-form-value-no-cache', 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-form-value-no-cache', $this->plainUser);
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-form-value-cache', drupal_anonymous_user());
  $this
    ->assertAuthcacheCanceled(t('Value element %name contained in the cacheable form %form_id. Please enable a suitable Authcache integration module for that form or file a support request.', array(
    '%name' => 'authcache_test_form_value',
    '%form_id' => 'authcache_test_form_value',
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-form-value-cache', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Value element %name contained in the cacheable form %form_id. Please enable a suitable Authcache integration module for that form or file a support request.', array(
    '%name' => 'authcache_test_form_value',
    '%form_id' => 'authcache_test_form_value',
  )));

  // Pages with tabs.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-tab', drupal_anonymous_user());
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-tab', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Tabs on page. Enable authcache_menu or implement custom code capable of detecting when caching tabs is acceptable.'));

  // Pages with action links.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $save_stub = $this->stubbackend
    ->hook('authcache_backend_cache_save');
  $this
    ->authcacheGet('authcache-test-action', drupal_anonymous_user());
  $this
    ->assertAuthcacheNotCanceled();
  $this
    ->assertAuthcacheNotExcluded();
  $this
    ->assertStub($save_stub, HookStub::any());
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-action', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Action links on page. Enable authcache_menu or implement custom code capable of detecting when caching local actions is acceptable.'));

  // Pages using drupal_goto.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-redirect', drupal_anonymous_user());
  $this
    ->assertAuthcacheCanceled(t('Redirecting to @destination', array(
    '@destination' => url('user', array(
      'absolute' => TRUE,
    )),
  )));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-redirect', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Redirecting to @destination', array(
    '@destination' => url('user', array(
      'absolute' => TRUE,
    )),
  )));

  // Status messages on page.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  variable_set('authcache_test_status_message', TRUE);
  $this
    ->authcacheGet('authcache-test-page-one', drupal_anonymous_user());
  $this
    ->assertAuthcacheCanceled(t('Status message on page'));
  $this
    ->resetTestVariables();
  variable_set('authcache_test_status_message', TRUE);
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Status message on page'));

  // Content-types.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $allowed_mimetypes = preg_split('/(\\r\\n?|\\n)/', AUTHCACHE_MIMETYPE_DEFAULT, -1, PREG_SPLIT_NO_EMPTY);
  foreach ($allowed_mimetypes as $mime_type) {
    $this
      ->resetTestVariables();
    $save_stub = $this->stubbackend
      ->hook('authcache_backend_cache_save');
    variable_set('authcache_test_content_type', $mime_type);
    $this
      ->authcacheGet('authcache-test-content-type', drupal_anonymous_user());
    $this
      ->assertAuthcacheNotCanceled();
    $this
      ->assertAuthcacheNotExcluded();
    $this
      ->assertStub($save_stub, HookStub::any());
    $this
      ->resetTestVariables();
    variable_set('authcache_test_content_type', $mime_type);
    $save_stub = $this->stubbackend
      ->hook('authcache_backend_cache_save');
    $this
      ->authcacheGet('authcache-test-content-type', $this->plainUser);
    $this
      ->assertAuthcacheNotCanceled();
    $this
      ->assertAuthcacheNotExcluded();
    $this
      ->assertStub($save_stub, HookStub::any());
  }
  $mime_type = 'application/octet-stream';
  $this
    ->resetTestVariables();
  variable_set('authcache_test_content_type', $mime_type);
  $this
    ->authcacheGet('authcache-test-content-type', drupal_anonymous_user());
  $this
    ->assertAuthcacheCanceled(t('Only cache allowed HTTP content types (HTML, JS, etc)'));
  $this
    ->resetTestVariables();
  variable_set('authcache_test_content_type', $mime_type);
  $this
    ->authcacheGet('authcache-test-content-type', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Only cache allowed HTTP content types (HTML, JS, etc)'));

  // Headers unexpectedly sent.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  variable_set('authcache_test_headers_sent', array(
    'Content-Type' => 'text/html',
  ));
  $this
    ->authcacheGet('authcache-test-headers-sent', drupal_anonymous_user());
  $this
    ->assertAuthcacheCanceled(t('Private file transfers or headers were unexpectedly sent'));
  $this
    ->resetTestVariables();
  variable_set('authcache_test_headers_sent', array(
    'Content-Type' => 'text/html',
  ));
  $this
    ->authcacheGet('authcache-test-headers-sent', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Private file transfers or headers were unexpectedly sent'));

  // Private file transfer.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $files = $this
    ->drupalGetTestFiles('html');
  $uri = reset($files)->uri;
  $headers = array(
    'Content-Type' => 'text/html',
  );
  $params = array(
    $uri,
    $headers,
  );
  $this
    ->resetTestVariables();
  variable_set('authcache_test_file_transfer', $params);
  $this
    ->authcacheGet('authcache-test-file-transfer', drupal_anonymous_user());
  $this
    ->assertAuthcacheCanceled(t('Private file transfers or headers were unexpectedly sent'));
  $this
    ->resetTestVariables();
  variable_set('authcache_test_file_transfer', $params);
  $this
    ->authcacheGet('authcache-test-file-transfer', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Private file transfers or headers were unexpectedly sent'));

  // Redirect using location header.
  $this
    ->setupConfig(array(
    'authcache_roles' => drupal_map_assoc(array_keys(user_roles())),
  ));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-redirect-location', drupal_anonymous_user());
  $this
    ->assertAuthcacheCanceled(t('Location header detected'));
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-redirect-location', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('Location header detected'));

  // hook_exit called during bootstrap.
  $this
    ->resetTestVariables();
  variable_set('authcache_test_early_exit', TRUE);
  $this
    ->authcacheGet('authcache-test-page-one', $this->plainUser);
  $this
    ->assertResponse(200);

  // Changed Vary header.
  $this
    ->resetTestVariables();
  $this
    ->authcacheGet('authcache-test-change-vary', $this->plainUser);
  $this
    ->assertAuthcacheCanceled(t('The Vary header was modified during the request'));

  // FIXME: Figure out how to properly test for thrown errors from
  // simpletest.
}