file_test.module
Same filename in other branches
File
-
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().
*/
declare (strict_types=1);
use Drupal\file\Entity\File;
// cspell:ignore garply tarz
const FILE_URL_TEST_CDN_1 = 'http://cdn1.example.com';
const FILE_URL_TEST_CDN_2 = 'http://cdn2.example.com';
/**
* 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 = [
'load' => [],
'validate' => [],
'download' => [],
'insert' => [],
'update' => [],
'copy' => [],
'move' => [],
'delete' => [],
];
\Drupal::state()->set('file_test.results', $results);
// These hooks will return these values, see file_test_set_return().
$return = [
'validate' => [],
'download' => NULL,
];
\Drupal::state()->set('file_test.return', $return);
}
/**
* Gets the arguments passed to a given hook invocation.
*
* Arguments are gathered since file_test_reset() was last called.
*
* @param string $op
* One of the hook_file_* operations: 'load', 'validate', 'download',
* 'insert', 'update', 'copy', 'move', 'delete'.
*
* @return array
* 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', []);
return $results[$op];
}
/**
* Get an array with the calls for all hooks.
*
* @return array
* 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', []);
}
/**
* Store the values passed to a hook invocation.
*
* @param string $op
* One of the hook_file_* operations: 'load', 'validate', 'download',
* 'insert', 'update', 'copy', 'move', 'delete'.
* @param array $args
* Values passed to hook.
*
* @see file_test_get_calls()
* @see file_test_reset()
*/
function _file_test_log_call($op, $args) {
if (\Drupal::state()->get('file_test.count_hook_invocations', TRUE)) {
$results = \Drupal::state()->get('file_test.results', []);
$results[$op][] = $args;
\Drupal::state()->set('file_test.results', $results);
}
}
/**
* Load the appropriate return value.
*
* @param string $op
* One of the hook_file_[validate,download] operations.
*
* @return mixed
* 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', [
$op => NULL,
]);
return $return[$op];
}
/**
* Assign a return value for a given operation.
*
* @param string $op
* One of the hook_file_[validate,download] operations.
* @param mixed $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', []);
$return[$op] = $value;
\Drupal::state()->set('file_test.return', $return);
}
/**
* Helper validator that returns the $errors parameter.
*/
function file_test_validator(File $file, $errors) {
return $errors;
}
/**
* Helper function for testing FileSystemInterface::scanDirectory().
*
* 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 string|null $filepath
* File path
* @param bool $reset
* (optional) If to reset the internal memory cache. If TRUE is passed, the
* first parameter has no effect. Defaults to FALSE.
*
* @return array
* If $filepath is NULL, an array of all previous $filepath parameters
*/
function file_test_file_scan_callback($filepath = NULL, $reset = FALSE) {
static $files = [];
if ($reset) {
$files = [];
}
elseif ($filepath) {
$files[] = $filepath;
}
return $files;
}
/**
* Reset static variables used by file_test_file_scan_callback().
*/
function file_test_file_scan_callback_reset() {
file_test_file_scan_callback(NULL, TRUE);
}
Functions
Title | Deprecated | Summary |
---|---|---|
file_test_file_scan_callback | Helper function for testing FileSystemInterface::scanDirectory(). | |
file_test_file_scan_callback_reset | Reset static variables used by file_test_file_scan_callback(). | |
file_test_get_all_calls | Get an array with the calls for all hooks. | |
file_test_get_calls | Gets the arguments passed to a given hook invocation. | |
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_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
Title | Deprecated | Summary |
---|---|---|
FILE_URL_TEST_CDN_1 | ||
FILE_URL_TEST_CDN_2 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.