You are here

authcache_test.module in Authenticated User Page Caching (Authcache) 7.2

Mock module to aid in testing authcache.module.

File

tests/authcache_test.module
View source
<?php

/**
 * @file
 * Mock module to aid in testing authcache.module.
 */

/**
 * Implements hook_menu().
 */
function authcache_test_menu() {
  $item['authcache-test-page-one'] = array(
    'title' => 'Test page one',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
  );
  $item['authcache-test-page-two'] = array(
    'title' => 'Test page one',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
  );
  $item['authcache-test-403'] = array(
    'title' => 'Test page one',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => FALSE,
  );
  $item['cart/authcache-test-default-nocache'] = array(
    'title' => 'Test page within a default nocache rule',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
  );
  $item['authcache-test-form'] = array(
    'title' => 'Test form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'authcache_test_form',
    ),
    'access callback' => TRUE,
  );
  $item['authcache-test-form-value-no-cache'] = array(
    'title' => 'Test form with value not cached',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'authcache_test_form_value',
      'no',
    ),
    'access callback' => TRUE,
  );
  $item['authcache-test-form-value-cache'] = array(
    'title' => 'Test form with value cached',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'authcache_test_form_value',
      'yes',
    ),
    'access callback' => TRUE,
  );
  $item['authcache-test-action'] = array(
    'title' => 'Test site with action link',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
  );
  $item['authcache-test-action/action'] = array(
    'title' => 'Action link',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
    'type' => MENU_LOCAL_ACTION,
  );
  $item['authcache-test-tab'] = array(
    'title' => 'Test site with action link',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
    'type' => MENU_NORMAL_ITEM,
  );
  $item['authcache-test-tab/first'] = array(
    'title' => 'First task',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $item['authcache-test-tab/second'] = array(
    'title' => 'Second task',
    'page callback' => 'authcache_test_empty_page',
    'access callback' => TRUE,
    'type' => MENU_LOCAL_TASK,
  );
  $item['authcache-test-redirect'] = array(
    'title' => 'Redirect',
    'page callback' => 'authcache_test_redirect',
    'access callback' => TRUE,
  );
  $item['authcache-test-content-type'] = array(
    'title' => 'Blank with content-type',
    'page callback' => 'authcache_test_content_type',
    'access callback' => TRUE,
  );
  $item['authcache-test-headers-sent'] = array(
    'title' => 'Headers sent',
    'page callback' => 'authcache_test_headers_sent',
    'access callback' => TRUE,
  );
  $item['authcache-test-file-transfer'] = array(
    'title' => 'Private file transfer',
    'page callback' => 'authcache_test_file_transfer',
    'access callback' => TRUE,
  );
  $item['authcache-test-php-error'] = array(
    'title' => 'Page with PHP error',
    'page callback' => 'authcache_test_php_error',
    'access callback' => TRUE,
  );
  $item['authcache-test-redirect-location'] = array(
    'title' => 'Redirect',
    'page callback' => 'authcache_test_redirect_location',
    'access callback' => TRUE,
  );
  $item['authcache-test-php-error'] = array(
    'title' => 'PHP Error',
    'page callback' => 'authcache_test_php_error',
    'access callback' => TRUE,
  );
  $item['authcache-test-change-vary'] = array(
    'title' => 'Change Vary',
    'page callback' => 'authcache_test_change_vary',
    'access callback' => TRUE,
  );
  $item['authcache-test-get-key'] = array(
    'title' => 'Return authcache key',
    'page callback' => 'authcache_test_get_key',
    'access callback' => TRUE,
  );
  $item['authcache-test-add-cookie'] = array(
    'title' => 'Add cookie to response',
    'page callback' => 'authcache_test_add_cookie',
    'access callback' => TRUE,
  );
  $item['authcache-test-session-counter'] = array(
    'title' => 'Increment counter stored in a session',
    'page callback' => 'authcache_test_session_counter',
    'access callback' => TRUE,
  );
  $item['authcache-test-session-clear'] = array(
    'title' => 'Clear session',
    'page callback' => 'authcache_test_session_clear',
    'access callback' => TRUE,
  );
  return $item;
}

/**
 * Form API callback for test form.
 */
function authcache_test_form($form, &$form_state) {
  $form['authcache_test_form_comment'] = array(
    '#type' => 'textarea',
  );
  return system_settings_form($form, $form_state);
}

/**
 * Form API callback for test from with a value element.
 */
function authcache_test_form_value($form, &$form_state, $cached) {
  $form['authcache_test_form_value'] = array(
    '#type' => 'value',
    '#value' => '42',
  );
  $form_state['cache'] = filter_var($cached, FILTER_VALIDATE_BOOLEAN);

  // Disable CSRF protection on this form in order to avoid interference with
  // other tests.
  $form['#token'] = FALSE;
  return system_settings_form($form, $form_state);
}

/**
 * Page build callback.
 */
function authcache_test_empty_page() {
  return '';
}

/**
 * Redirect.
 */
function authcache_test_redirect() {

  // Redirect to a page which is not cached.
  drupal_goto('user');
}

/**
 * Content type.
 */
function authcache_test_content_type() {
  drupal_add_http_header('Content-Type', variable_get('authcache_test_content_type'));
  print 'mostly harmless';
  drupal_exit();
}

/**
 * Headers sent.
 */
function authcache_test_headers_sent() {
  foreach (variable_get('authcache_test_headers_sent') as $header => $value) {
    drupal_add_http_header($header, $value);
  }
  while (ob_get_level()) {
    ob_end_flush();
  }
  print 'mostly harmless';
  drupal_exit();
}

/**
 * Private file transfer.
 */
function authcache_test_file_transfer() {
  list($uri, $headers) = variable_get('authcache_test_file_transfer');
  file_transfer($uri, $headers);
}

/**
 * Redirect.
 */
function authcache_test_redirect_location() {

  // Redirect to a page which is not cached.
  drupal_add_http_header('Content-Type', 'text/html');
  header('Location: ' . url('user', array(
    'absolute' => TRUE,
  )));
  drupal_exit();
}

/**
 * PHP Error.
 */
function authcache_test_php_error() {
  list($message, $type) = variable_get('authcache_test_php_error');
  drupal_add_http_header('Content-Type', 'text/html');

  // @ignore security_trigger_error
  trigger_error($message, $type);
  drupal_exit();
}

/**
 * Change vary.
 */
function authcache_test_change_vary() {

  // Specify a custom Vary-header.
  drupal_add_http_header('Vary', 'Shoe-Size');
  return '42';
}

/**
 * Return authcache key for currently logged in user.
 */
function authcache_test_get_key() {
  print authcache_key();
  drupal_exit();
}

/**
 * Add a cookie to the response.
 */
function authcache_test_add_cookie() {
  authcache_add_cookie(variable_get('authcache_test_add_cookie'));
  drupal_exit();
}

/**
 * Open a session for anonymous users.
 */
function authcache_test_session_counter() {
  if (empty($_SESSION['authcache-test-count'])) {
    $_SESSION['authcache-test-count'] = 0;
  }
  print ++$_SESSION['authcache-test-count'];
  drupal_exit();
}

/**
 * Clear the session.
 */
function authcache_test_session_clear() {
  unset($_SESSION);
  drupal_exit();
}

/**
 * Implements hook_page_build().
 */
function authcache_test_page_build(&$page) {
  if (variable_get('authcache_test_status_message', FALSE)) {

    // @ignore i18n_8
    drupal_set_message('status message during page build');
  }
}

/**
 * Implements hook_exit().
 */
function authcache_test_exit() {
  if (variable_get('authcache_test_status_message_exit', FALSE)) {

    // @ignore i18n_8
    drupal_set_message('status message during exit hook');
  }
}

/**
 * Implements hook_boot().
 */
function authcache_test_boot() {
  if (variable_get('authcache_test_early_exit', FALSE)) {
    module_invoke_all('exit', 'http://example.com/redirect/to/landing-page');
    exit;
  }
}

/**
 * Implements hook_authcache_request_exclude().
 */
function authcache_test_authcache_request_exclude() {
  if ($reason = variable_get('authcache_test_authcache_request_exclude')) {
    return t('Trigger request exclude: @reason', array(
      '@reason' => $reason,
    ));
  }
}

/**
 * Implements hook_authcache_account_exclude().
 */
function authcache_test_authcache_account_exclude($account) {
  if ($reason = variable_get('authcache_test_authcache_account_exclude')) {
    return t('Trigger account exclude for uid=@uid: @reason', array(
      '@uid' => $account->uid,
      '@reason' => $reason,
    ));
  }
}

/**
 * Implements hook_authcache_excluded().
 */
function authcache_test_authcache_excluded($reason) {

  // Do not record anything resulting from test user login/logout
  if (variable_get('authcache_test_record')) {
    variable_set('authcache_test_authcache_excluded', $reason);
  }
}

/**
 * Implements hook_authcache_cancel().
 */
function authcache_test_authcache_cancel() {
  if ($reason = variable_get('authcache_test_authcache_cancel')) {
    return t('Trigger cancelation: @reason', array(
      '@reason' => $reason,
    ));
  }
}

/**
 * Implements hook_authcache_canceled().
 */
function authcache_test_authcache_canceled($reason) {

  // Do not record anything resulting from test user login/logout
  if (variable_get('authcache_test_record')) {
    variable_set('authcache_test_authcache_canceled', $reason);
  }
}

/**
 * Implements hook_authcache_preclude().
 */
function authcache_test_authcache_preclude() {
  if ($reason = variable_get('authcache_test_authcache_preclude')) {
    return t('Trigger preclusion: @reason', array(
      '@reason' => $reason,
    ));
  }
}

/**
 * Implements hook_authcache_precluded().
 */
function authcache_test_authcache_precluded($reason) {

  // Do not record anything resulting from test user login/logout
  if (variable_get('authcache_test_record')) {
    variable_set('authcache_test_authcache_precluded', $reason);
  }
}

/**
 * Implements hook_authcache_key_properties().
 */
function authcache_test_authcache_key_properties() {
  return variable_get('authcache_test_authcache_key_properties');
}

/**
 * Implements hook_authcache_key_properties_alter().
 */
function authcache_test_authcache_key_properties_alter(&$properties) {
}

/**
 * Implements hook_authcache_backend_key_set().
 */
function authcache_test_authcache_backend_key_set($key, $lifetime, $has_session) {
  return HookStub::record(__FUNCTION__, func_get_args());
}

/**
 * Implements hook_authcache_cookie().
 */
function authcache_test_authcache_cookie($account) {

  // Do not record anything resulting from test user login/logout
  if (variable_get('authcache_test_record')) {
    variable_set('authcache_test_authcache_cookie', $account->uid);
  }
  if ($cookie = variable_get('authcache_test_cookie')) {
    return $cookie;
  }
}

/**
 * Implements hook_authcache_cookie_alter().
 */
function authcache_test_authcache_cookie_alter(&$cookies, $account) {

  // Do not record anything resulting from test user login/logout
  if (variable_get('authcache_test_record')) {
    variable_set('authcache_test_authcache_cookie_alter', array(
      $cookies,
      $account->uid,
    ));
  }
  if ($cookierec = variable_get('authcache_test_cookie_alter')) {
    list($name, $value) = $cookierec;
    $cookies[$name] = $value;
  }
}

/**
 * Test key generator for anonymous users.
 */
function authcache_test_key_generator() {
  return variable_get('authcache_test_key_generator_key');
}

Functions

Namesort descending Description
authcache_test_add_cookie Add a cookie to the response.
authcache_test_authcache_account_exclude Implements hook_authcache_account_exclude().
authcache_test_authcache_backend_key_set Implements hook_authcache_backend_key_set().
authcache_test_authcache_cancel Implements hook_authcache_cancel().
authcache_test_authcache_canceled Implements hook_authcache_canceled().
authcache_test_authcache_cookie Implements hook_authcache_cookie().
authcache_test_authcache_cookie_alter Implements hook_authcache_cookie_alter().
authcache_test_authcache_excluded Implements hook_authcache_excluded().
authcache_test_authcache_key_properties Implements hook_authcache_key_properties().
authcache_test_authcache_key_properties_alter Implements hook_authcache_key_properties_alter().
authcache_test_authcache_preclude Implements hook_authcache_preclude().
authcache_test_authcache_precluded Implements hook_authcache_precluded().
authcache_test_authcache_request_exclude Implements hook_authcache_request_exclude().
authcache_test_boot Implements hook_boot().
authcache_test_change_vary Change vary.
authcache_test_content_type Content type.
authcache_test_empty_page Page build callback.
authcache_test_exit Implements hook_exit().
authcache_test_file_transfer Private file transfer.
authcache_test_form Form API callback for test form.
authcache_test_form_value Form API callback for test from with a value element.
authcache_test_get_key Return authcache key for currently logged in user.
authcache_test_headers_sent Headers sent.
authcache_test_key_generator Test key generator for anonymous users.
authcache_test_menu Implements hook_menu().
authcache_test_page_build Implements hook_page_build().
authcache_test_php_error PHP Error.
authcache_test_redirect Redirect.
authcache_test_redirect_location Redirect.
authcache_test_session_clear Clear the session.
authcache_test_session_counter Open a session for anonymous users.