5 * Helper module for the file tests.
7 * The caller is must call file_test_reset() to initializing this module before
8 * calling file_test_get_calls() or file_test_set_return().
11 use Drupal\file\Entity\File;
13 const FILE_URL_TEST_CDN_1 = 'http://cdn1.example.com';
14 const FILE_URL_TEST_CDN_2 = 'http://cdn2.example.com';
17 * Reset/initialize the history of calls to the file_* hooks.
19 * @see file_test_get_calls()
20 * @see file_test_reset()
22 function file_test_reset() {
23 // Keep track of calls to these hooks
34 \Drupal::state()->set('file_test.results', $results);
36 // These hooks will return these values, see file_test_set_return().
41 \Drupal::state()->set('file_test.return', $return);
45 * Get the arguments passed to invocation of a given hook since
46 * file_test_reset() was last called.
49 * One of the hook_file_* operations: 'load', 'validate', 'download',
50 * 'insert', 'update', 'copy', 'move', 'delete'.
53 * Array of the parameters passed to each call.
55 * @see _file_test_log_call()
56 * @see file_test_reset()
58 function file_test_get_calls($op) {
59 $results = \Drupal::state()->get('file_test.results') ?: [];
64 * Get an array with the calls for all hooks.
67 * An array keyed by hook name ('load', 'validate', 'download', 'insert',
68 * 'update', 'copy', 'move', 'delete') with values being arrays of parameters
69 * passed to each call.
71 function file_test_get_all_calls() {
72 return \Drupal::state()->get('file_test.results') ?: [];
76 * Store the values passed to a hook invocation.
79 * One of the hook_file_* operations: 'load', 'validate', 'download',
80 * 'insert', 'update', 'copy', 'move', 'delete'.
82 * Values passed to hook.
84 * @see file_test_get_calls()
85 * @see file_test_reset()
87 function _file_test_log_call($op, $args) {
88 if (\Drupal::state()->get('file_test.count_hook_invocations', TRUE)) {
89 $results = \Drupal::state()->get('file_test.results') ?: [];
90 $results[$op][] = $args;
91 \Drupal::state()->set('file_test.results', $results);
96 * Load the appropriate return value.
99 * One of the hook_file_[validate,download] operations.
102 * Value set by file_test_set_return().
104 * @see file_test_set_return()
105 * @see file_test_reset()
107 function _file_test_get_return($op) {
108 $return = \Drupal::state()->get('file_test.return') ?: [$op => NULL];
113 * Assign a return value for a given operation.
116 * One of the hook_file_[validate,download] operations.
117 * @param mixed $value
118 * Value for the hook to return.
120 * @see _file_test_get_return()
121 * @see file_test_reset()
123 function file_test_set_return($op, $value) {
124 $return = \Drupal::state()->get('file_test.return') ?: [];
125 $return[$op] = $value;
126 \Drupal::state()->set('file_test.return', $return);
130 * Implements hook_ENTITY_TYPE_load() for file entities.
132 function file_test_file_load($files) {
133 foreach ($files as $file) {
134 _file_test_log_call('load', [$file->id()]);
135 // Assign a value on the object so that we can test that the $file is passed
137 $file->file_test['loaded'] = TRUE;
142 * Implements hook_file_validate().
144 function file_test_file_validate(File $file) {
145 _file_test_log_call('validate', [$file->id()]);
146 return _file_test_get_return('validate');
150 * Implements hook_file_download().
152 function file_test_file_download($uri) {
153 if (\Drupal::state()->get('file_test.allow_all', FALSE)) {
154 $files = entity_load_multiple_by_properties('file', ['uri' => $uri]);
155 $file = reset($files);
156 return file_get_content_headers($file);
158 _file_test_log_call('download', [$uri]);
159 return _file_test_get_return('download');
163 * Implements hook_ENTITY_TYPE_insert() for file entities.
165 function file_test_file_insert(File $file) {
166 _file_test_log_call('insert', [$file->id()]);
170 * Implements hook_ENTITY_TYPE_update() for file entities.
172 function file_test_file_update(File $file) {
173 _file_test_log_call('update', [$file->id()]);
177 * Implements hook_file_copy().
179 function file_test_file_copy(File $file, $source) {
180 _file_test_log_call('copy', [$file->id(), $source->id()]);
184 * Implements hook_file_move().
186 function file_test_file_move(File $file, File $source) {
187 _file_test_log_call('move', [$file->id(), $source->id()]);
191 * Implements hook_ENTITY_TYPE_predelete() for file entities.
193 function file_test_file_predelete(File $file) {
194 _file_test_log_call('delete', [$file->id()]);
198 * Implements hook_file_url_alter().
200 function file_test_file_url_alter(&$uri) {
201 // Only run this hook when this variable is set. Otherwise, we'd have to add
202 // another hidden test module just for this hook.
203 $alter_mode = \Drupal::state()->get('file_test.hook_file_url_alter');
207 // Test alteration of file URLs to use a CDN.
208 elseif ($alter_mode == 'cdn') {
209 $cdn_extensions = ['css', 'js', 'gif', 'jpg', 'jpeg', 'png'];
211 // Most CDNs don't support private file transfers without a lot of hassle,
212 // so don't support this in the common case.
213 $schemes = ['public'];
215 $scheme = file_uri_scheme($uri);
217 // Only serve shipped files and public created files from the CDN.
218 if (!$scheme || in_array($scheme, $schemes)) {
223 // Public created files.
225 $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
226 $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
229 // Clean up Windows paths.
230 $path = str_replace('\\', '/', $path);
232 // Serve files with one of the CDN extensions from CDN 1, all others from
234 $pathinfo = pathinfo($path);
235 if (array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $cdn_extensions)) {
236 $uri = FILE_URL_TEST_CDN_1 . '/' . $path;
239 $uri = FILE_URL_TEST_CDN_2 . '/' . $path;
243 // Test alteration of file URLs to use root-relative URLs.
244 elseif ($alter_mode == 'root-relative') {
245 // Only serve shipped files and public created files with root-relative
247 $scheme = file_uri_scheme($uri);
248 if (!$scheme || $scheme == 'public') {
253 // Public created files.
255 $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
256 $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
259 // Clean up Windows paths.
260 $path = str_replace('\\', '/', $path);
262 // Generate a root-relative URL.
263 $uri = base_path() . '/' . $path;
266 // Test alteration of file URLs to use protocol-relative URLs.
267 elseif ($alter_mode == 'protocol-relative') {
268 // Only serve shipped files and public created files with protocol-relative
270 $scheme = file_uri_scheme($uri);
271 if (!$scheme || $scheme == 'public') {
276 // Public created files.
278 $wrapper = \Drupal::service('stream_wrapper_manager')->getViaScheme($scheme);
279 $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
282 // Clean up Windows paths.
283 $path = str_replace('\\', '/', $path);
285 // Generate a protocol-relative URL.
286 $uri = '/' . base_path() . '/' . $path;
292 * Implements hook_file_mimetype_mapping_alter().
294 function file_test_file_mimetype_mapping_alter(&$mapping) {
296 $mapping['mimetypes']['file_test_mimetype_1'] = 'madeup/file_test_1';
297 $mapping['mimetypes']['file_test_mimetype_2'] = 'madeup/file_test_2';
298 $mapping['mimetypes']['file_test_mimetype_3'] = 'madeup/doc';
299 $mapping['extensions']['file_test_1'] = 'file_test_mimetype_1';
300 $mapping['extensions']['file_test_2'] = 'file_test_mimetype_2';
301 $mapping['extensions']['file_test_3'] = 'file_test_mimetype_2';
302 // Override existing mapping.
303 $mapping['extensions']['doc'] = 'file_test_mimetype_3';
307 * Helper validator that returns the $errors parameter.
309 function file_test_validator(File $file, $errors) {
314 * Helper function for testing file_scan_directory().
316 * Each time the function is called the file is stored in a static variable.
317 * When the function is called with no $filepath parameter, the results are
320 * @param string|null $filepath
323 * If $filepath is NULL, an array of all previous $filepath parameters
325 function file_test_file_scan_callback($filepath = NULL) {
326 $files = &drupal_static(__FUNCTION__, []);
327 if (isset($filepath)) {
328 $files[] = $filepath;
336 * Reset static variables used by file_test_file_scan_callback().
338 function file_test_file_scan_callback_reset() {
339 drupal_static_reset('file_test_file_scan_callback');
343 * Implements hook_entity_info_alter().
345 function file_test_entity_type_alter(&$entity_types) {
346 if (\Drupal::state()->get('file_test_alternate_access_handler', FALSE)) {
347 /** @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
348 $entity_types['file']
349 ->setAccessClass('Drupal\file_test\FileTestAccessControlHandler');