Newer
Older
<?php
/**
* @file
* Helper module for the file tests.
Angie Byron
committed
*
* The caller is must call file_test_reset() to initializing this module before
* calling file_test_get_calls() or file_test_set_return().
*/
Angie Byron
committed
use Drupal\file\Plugin\Core\Entity\File;
Dries Buytaert
committed
const FILE_URL_TEST_CDN_1 = 'http://cdn1.example.com';
const FILE_URL_TEST_CDN_2 = 'http://cdn2.example.com';
Dries Buytaert
committed
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements hook_stream_wrappers().
Dries Buytaert
committed
*/
function file_test_stream_wrappers() {
return array(
'dummy' => array(
'name' => t('Dummy files'),
'class' => 'Drupal\file_test\DummyStreamWrapper',
Dries Buytaert
committed
'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).'),
),
Dries Buytaert
committed
);
}
/**
* Reset/initialize the history of calls to the file_* hooks.
Angie Byron
committed
*
* @see file_test_get_calls()
Dries Buytaert
committed
* @see file_test_reset()
*/
function file_test_reset() {
// Keep track of calls to these hooks
Angie Byron
committed
$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);
Dries Buytaert
committed
// These hooks will return these values, see file_test_set_return().
Angie Byron
committed
$return = array(
'validate' => array(),
'download' => NULL,
);
Drupal::state()->set('file_test.return', $return);
}
/**
Angie Byron
committed
* Get the arguments passed to invocation of a given hook since
* file_test_reset() was last called.
Angie Byron
committed
* @param $op
* One of the hook_file_* operations: 'load', 'validate', 'download',
Dries Buytaert
committed
* 'insert', 'update', 'copy', 'move', 'delete'.
Dries Buytaert
committed
*
Angie Byron
committed
* Array of the parameters passed to each call.
Dries Buytaert
committed
*
* @see _file_test_log_call()
* @see file_test_reset()
*/
function file_test_get_calls($op) {
$results = Drupal::state()->get('file_test.results') ?: array();
Angie Byron
committed
return $results[$op];
}
/**
* Get an array with the calls for all hooks.
*
* @return
Dries Buytaert
committed
* 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();
Angie Byron
committed
/**
* Store the values passed to a hook invocation.
*
* @param $op
* One of the hook_file_* operations: 'load', 'validate', 'download',
Dries Buytaert
committed
* 'insert', 'update', 'copy', 'move', 'delete'.
Angie Byron
committed
* @param $args
* Values passed to hook.
Dries Buytaert
committed
*
* @see file_test_get_calls()
* @see file_test_reset()
Angie Byron
committed
*/
function _file_test_log_call($op, $args) {
$results = Drupal::state()->get('file_test.results') ?: array();
Angie Byron
committed
$results[$op][] = $args;
Drupal::state()->set('file_test.results', $results);
Angie Byron
committed
}
/**
* Load the appropriate return value.
*
* @param $op
Dries Buytaert
committed
* One of the hook_file_[validate,download] operations.
Dries Buytaert
committed
*
Angie Byron
committed
* @return
* Value set by file_test_set_return().
Dries Buytaert
committed
*
* @see file_test_set_return()
* @see file_test_reset()
Angie Byron
committed
*/
function _file_test_get_return($op) {
$return = Drupal::state()->get('file_test.return') ?: array($op => NULL);
Angie Byron
committed
return $return[$op];
}
/**
* Assign a return value for a given operation.
*
* @param $op
Dries Buytaert
committed
* One of the hook_file_[validate,download] operations.
Angie Byron
committed
* @param $value
* Value for the hook to return.
Dries Buytaert
committed
*
* @see _file_test_get_return()
* @see file_test_reset()
Angie Byron
committed
*/
function file_test_set_return($op, $value) {
$return = Drupal::state()->get('file_test.return') ?: array();
Angie Byron
committed
$return[$op] = $value;
Drupal::state()->set('file_test.return', $return);
}
/**
Dries Buytaert
committed
* Implements hook_file_load().
*/
Dries Buytaert
committed
function file_test_file_load($files) {
foreach ($files as $file) {
Dries Buytaert
committed
_file_test_log_call('load', array($file->fid));
Dries Buytaert
committed
// Assign a value on the object so that we can test that the $file is passed
// by reference.
$file->file_test['loaded'] = TRUE;
}
}
/**
Dries Buytaert
committed
* Implements hook_file_validate().
*/
Dries Buytaert
committed
function file_test_file_validate(File $file) {
_file_test_log_call('validate', array($file->fid));
Angie Byron
committed
return _file_test_get_return('validate');
}
/**
Dries Buytaert
committed
* Implements hook_file_download().
*/
Dries Buytaert
committed
function file_test_file_download($uri) {
_file_test_log_call('download', array($uri));
Angie Byron
committed
return _file_test_get_return('download');
}
/**
Dries Buytaert
committed
* Implements hook_file_insert().
*/
Dries Buytaert
committed
function file_test_file_insert(File $file) {
_file_test_log_call('insert', array($file->fid));
}
/**
Dries Buytaert
committed
* Implements hook_file_update().
*/
Dries Buytaert
committed
function file_test_file_update(File $file) {
_file_test_log_call('update', array($file->fid));
}
/**
Dries Buytaert
committed
* Implements hook_file_copy().
*/
Dries Buytaert
committed
function file_test_file_copy(File $file, $source) {
_file_test_log_call('copy', array($file->fid, $source->fid));
}
/**
Dries Buytaert
committed
* Implements hook_file_move().
*/
Dries Buytaert
committed
function file_test_file_move(File $file, File $source) {
_file_test_log_call('move', array($file->fid, $source->fid));
}
/**
catch
committed
* Implements hook_file_predelete().
*/
Dries Buytaert
committed
function file_test_file_predelete(File $file) {
_file_test_log_call('delete', array($file->fid));
Dries Buytaert
committed
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements hook_file_url_alter().
Dries Buytaert
committed
*/
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) {
Dries Buytaert
committed
return;
}
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// 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;
}
Dries Buytaert
committed
}
}
// 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;
Dries Buytaert
committed
}
}
// 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;
Dries Buytaert
committed
}
}
}
Dries Buytaert
committed
/**
Dries Buytaert
committed
* Implements hook_file_mimetype_mapping_alter().
Dries Buytaert
committed
*/
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';
}
Dries Buytaert
committed
/**
* Helper validator that returns the $errors parameter.
Dries Buytaert
committed
*/
function file_test_validator(File $file, $errors) {
return $errors;
Dries Buytaert
committed
}
/**
* 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');
}