file_test.module

Helper module for the file tests.

The caller is must call file_test_reset() to initializing this module before calling file_test_get_calls() or file_test_set_return().

File

drupal/core/modules/file/tests/file_test/file_test.module
View source
<?php

/**
 * @file
 * Helper module for the file tests.
 *
 * The caller is must call file_test_reset() to initializing this module before
 * calling file_test_get_calls() or file_test_set_return().
 */
use Drupal\file\Plugin\Core\Entity\File;
const FILE_URL_TEST_CDN_1 = 'http://cdn1.example.com';
const FILE_URL_TEST_CDN_2 = 'http://cdn2.example.com';

/**
 * Implements hook_stream_wrappers().
 */
function file_test_stream_wrappers() {
  return array(
    'dummy' => array(
      'name' => t('Dummy files'),
      'class' => 'Drupal\\file_test\\DummyStreamWrapper',
      'description' => t('Dummy wrapper for simpletest.'),
    ),
    'dummy-remote' => array(
      'name' => t('Dummy files (remote)'),
      'class' => 'Drupal\\file_test\\DummyRemoteStreamWrapper',
      'description' => t('Dummy wrapper for simpletest (remote).'),
    ),
    'dummy-readonly' => array(
      'name' => t('Dummy files (readonly)'),
      'class' => 'Drupal\\file_test\\DummyReadOnlyStreamWrapper',
      'description' => t('Dummy wrapper for simpletest (readonly).'),
    ),
  );
}

/**
 * Reset/initialize the history of calls to the file_* hooks.
 *
 * @see file_test_get_calls()
 * @see file_test_reset()
 */
function file_test_reset() {

  // Keep track of calls to these hooks
  $results = array(
    'load' => array(),
    'validate' => array(),
    'download' => array(),
    'insert' => array(),
    'update' => array(),
    'copy' => array(),
    'move' => array(),
    'delete' => array(),
  );
  Drupal::state()
    ->set('file_test.results', $results);

  // These hooks will return these values, see file_test_set_return().
  $return = array(
    'validate' => array(),
    'download' => NULL,
  );
  Drupal::state()
    ->set('file_test.return', $return);
}

/**
 * Get the arguments passed to invocation of a given hook since
 * file_test_reset() was last called.
 *
 * @param $op
 *   One of the hook_file_* operations: 'load', 'validate', 'download',
 *   'insert', 'update', 'copy', 'move', 'delete'.
 *
 * @return
 *   Array of the parameters passed to each call.
 *
 * @see _file_test_log_call()
 * @see file_test_reset()
 */
function file_test_get_calls($op) {
  $results = Drupal::state()
    ->get('file_test.results') ?: array();
  return $results[$op];
}

/**
 * Get an array with the calls for all hooks.
 *
 * @return
 *   An array keyed by hook name ('load', 'validate', 'download', 'insert',
 *   'update', 'copy', 'move', 'delete') with values being arrays of parameters
 *   passed to each call.
 */
function file_test_get_all_calls() {
  return Drupal::state()
    ->get('file_test.results') ?: array();
}

/**
 * Store the values passed to a hook invocation.
 *
 * @param $op
 *   One of the hook_file_* operations: 'load', 'validate', 'download',
 *   'insert', 'update', 'copy', 'move', 'delete'.
 * @param $args
 *   Values passed to hook.
 *
 * @see file_test_get_calls()
 * @see file_test_reset()
 */
function _file_test_log_call($op, $args) {
  $results = Drupal::state()
    ->get('file_test.results') ?: array();
  $results[$op][] = $args;
  Drupal::state()
    ->set('file_test.results', $results);
}

/**
 * Load the appropriate return value.
 *
 * @param $op
 *   One of the hook_file_[validate,download] operations.
 *
 * @return
 *   Value set by file_test_set_return().
 *
 * @see file_test_set_return()
 * @see file_test_reset()
 */
function _file_test_get_return($op) {
  $return = Drupal::state()
    ->get('file_test.return') ?: array(
    $op => NULL,
  );
  return $return[$op];
}

/**
 * Assign a return value for a given operation.
 *
 * @param $op
 *   One of the hook_file_[validate,download] operations.
 * @param $value
 *   Value for the hook to return.
 *
 * @see _file_test_get_return()
 * @see file_test_reset()
 */
function file_test_set_return($op, $value) {
  $return = Drupal::state()
    ->get('file_test.return') ?: array();
  $return[$op] = $value;
  Drupal::state()
    ->set('file_test.return', $return);
}

/**
 * Implements hook_file_load().
 */
function file_test_file_load($files) {
  foreach ($files as $file) {
    _file_test_log_call('load', array(
      $file->fid,
    ));

    // Assign a value on the object so that we can test that the $file is passed
    // by reference.
    $file->file_test['loaded'] = TRUE;
  }
}

/**
 * Implements hook_file_validate().
 */
function file_test_file_validate(File $file) {
  _file_test_log_call('validate', array(
    $file->fid,
  ));
  return _file_test_get_return('validate');
}

/**
 * Implements hook_file_download().
 */
function file_test_file_download($uri) {
  _file_test_log_call('download', array(
    $uri,
  ));
  return _file_test_get_return('download');
}

/**
 * Implements hook_file_insert().
 */
function file_test_file_insert(File $file) {
  _file_test_log_call('insert', array(
    $file->fid,
  ));
}

/**
 * Implements hook_file_update().
 */
function file_test_file_update(File $file) {
  _file_test_log_call('update', array(
    $file->fid,
  ));
}

/**
 * Implements hook_file_copy().
 */
function file_test_file_copy(File $file, $source) {
  _file_test_log_call('copy', array(
    $file->fid,
    $source->fid,
  ));
}

/**
 * Implements hook_file_move().
 */
function file_test_file_move(File $file, File $source) {
  _file_test_log_call('move', array(
    $file->fid,
    $source->fid,
  ));
}

/**
 * Implements hook_file_predelete().
 */
function file_test_file_predelete(File $file) {
  _file_test_log_call('delete', array(
    $file->fid,
  ));
}

/**
 * Implements hook_file_url_alter().
 */
function file_test_file_url_alter(&$uri) {

  // Only run this hook when this variable is set. Otherwise, we'd have to add
  // another hidden test module just for this hook.
  $alter_mode = Drupal::state()
    ->get('file_test.hook_file_url_alter');
  if (!$alter_mode) {
    return;
  }
  elseif ($alter_mode == 'cdn') {
    $cdn_extensions = array(
      'css',
      'js',
      'gif',
      'jpg',
      'jpeg',
      'png',
    );

    // Most CDNs don't support private file transfers without a lot of hassle,
    // so don't support this in the common case.
    $schemes = array(
      'public',
    );
    $scheme = file_uri_scheme($uri);

    // Only serve shipped files and public created files from the CDN.
    if (!$scheme || in_array($scheme, $schemes)) {

      // Shipped files.
      if (!$scheme) {
        $path = $uri;
      }
      else {
        $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
        $path = $wrapper
          ->getDirectoryPath() . '/' . file_uri_target($uri);
      }

      // Clean up Windows paths.
      $path = str_replace('\\', '/', $path);

      // Serve files with one of the CDN extensions from CDN 1, all others from
      // CDN 2.
      $pathinfo = pathinfo($path);
      if (array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $cdn_extensions)) {
        $uri = FILE_URL_TEST_CDN_1 . '/' . $path;
      }
      else {
        $uri = FILE_URL_TEST_CDN_2 . '/' . $path;
      }
    }
  }
  elseif ($alter_mode == 'root-relative') {

    // Only serve shipped files and public created files with root-relative
    // URLs.
    $scheme = file_uri_scheme($uri);
    if (!$scheme || $scheme == 'public') {

      // Shipped files.
      if (!$scheme) {
        $path = $uri;
      }
      else {
        $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
        $path = $wrapper
          ->getDirectoryPath() . '/' . file_uri_target($uri);
      }

      // Clean up Windows paths.
      $path = str_replace('\\', '/', $path);

      // Generate a root-relative URL.
      $uri = base_path() . '/' . $path;
    }
  }
  elseif ($alter_mode == 'protocol-relative') {

    // Only serve shipped files and public created files with protocol-relative
    // URLs.
    $scheme = file_uri_scheme($uri);
    if (!$scheme || $scheme == 'public') {

      // Shipped files.
      if (!$scheme) {
        $path = $uri;
      }
      else {
        $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
        $path = $wrapper
          ->getDirectoryPath() . '/' . file_uri_target($uri);
      }

      // Clean up Windows paths.
      $path = str_replace('\\', '/', $path);

      // Generate a protocol-relative URL.
      $uri = '/' . base_path() . '/' . $path;
    }
  }
}

/**
 * Implements hook_file_mimetype_mapping_alter().
 */
function file_test_file_mimetype_mapping_alter(&$mapping) {

  // Add new mappings.
  $mapping['mimetypes']['file_test_mimetype_1'] = 'madeup/file_test_1';
  $mapping['mimetypes']['file_test_mimetype_2'] = 'madeup/file_test_2';
  $mapping['mimetypes']['file_test_mimetype_3'] = 'madeup/doc';
  $mapping['extensions']['file_test_1'] = 'file_test_mimetype_1';
  $mapping['extensions']['file_test_2'] = 'file_test_mimetype_2';
  $mapping['extensions']['file_test_3'] = 'file_test_mimetype_2';

  // Override existing mapping.
  $mapping['extensions']['doc'] = 'file_test_mimetype_3';
}

/**
 * Helper validator that returns the $errors parameter.
 */
function file_test_validator(File $file, $errors) {
  return $errors;
}

/**
 * Helper function for testing file_scan_directory().
 *
 * Each time the function is called the file is stored in a static variable.
 * When the function is called with no $filepath parameter, the results are
 * returned.
 *
 * @param $filepath
 *   File path
 * @return
 *   If $filepath is NULL, an array of all previous $filepath parameters
 */
function file_test_file_scan_callback($filepath = NULL) {
  $files =& drupal_static(__FUNCTION__, array());
  if (isset($filepath)) {
    $files[] = $filepath;
  }
  else {
    return $files;
  }
}

/**
 * Reset static variables used by file_test_file_scan_callback().
 */
function file_test_file_scan_callback_reset() {
  drupal_static_reset('file_test_file_scan_callback');
}

Functions

Namesort descending Description
file_test_file_copy Implements hook_file_copy().
file_test_file_download Implements hook_file_download().
file_test_file_insert Implements hook_file_insert().
file_test_file_load Implements hook_file_load().
file_test_file_mimetype_mapping_alter Implements hook_file_mimetype_mapping_alter().
file_test_file_move Implements hook_file_move().
file_test_file_predelete Implements hook_file_predelete().
file_test_file_scan_callback Helper function for testing file_scan_directory().
file_test_file_scan_callback_reset Reset static variables used by file_test_file_scan_callback().
file_test_file_update Implements hook_file_update().
file_test_file_url_alter Implements hook_file_url_alter().
file_test_file_validate Implements hook_file_validate().
file_test_get_all_calls Get an array with the calls for all hooks.
file_test_get_calls Get the arguments passed to invocation of a given hook since file_test_reset() was last called.
file_test_reset Reset/initialize the history of calls to the file_* hooks.
file_test_set_return Assign a return value for a given operation.
file_test_stream_wrappers Implements hook_stream_wrappers().
file_test_validator Helper validator that returns the $errors parameter.
_file_test_get_return Load the appropriate return value.
_file_test_log_call Store the values passed to a hook invocation.

Constants