Skip to content
file_test.module 9.63 KiB
Newer Older
<?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';
 */
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.
 */
function file_test_reset() {
  // Keep track of calls to these hooks
    '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().
  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'.
 *   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();
/**
 * 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'.
 *
 * @see file_test_get_calls()
 * @see file_test_reset()
  $results = Drupal::state()->get('file_test.results') ?: array();
  Drupal::state()->set('file_test.results', $results);
 *   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()
  $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();
  Drupal::state()->set('file_test.return', $return);
function file_test_file_load($files) {
  foreach ($files as $file) {
    // Assign a value on the object so that we can test that the $file is passed
    // by reference.
    $file->file_test['loaded'] = TRUE;
  }
function file_test_file_validate(File $file) {
  _file_test_log_call('validate', array($file->fid));
function file_test_file_download($uri) {
  _file_test_log_call('download', array($uri));
function file_test_file_insert(File $file) {
  _file_test_log_call('insert', array($file->fid));
function file_test_file_update(File $file) {
  _file_test_log_call('update', array($file->fid));
function file_test_file_copy(File $file, $source) {
  _file_test_log_call('copy', array($file->fid, $source->fid));
function file_test_file_move(File $file, File $source) {
  _file_test_log_call('move', array($file->fid, $source->fid));
function file_test_file_predelete(File $file) {
  _file_test_log_call('delete', array($file->fid));
 */
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');
  // Test alteration of file URLs to use a CDN.
  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;
      }
      // Public created files.
      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;
      }
  }
  // Test alteration of file URLs to use root-relative URLs.
  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;
      }
      // Public created files.
      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;
  }
  // Test alteration of file URLs to use protocol-relative URLs.
  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;
      }
      // Public created files.
      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');
}