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-7.x/modules/simpletest/tests/file_test.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Helper module for the file tests.
  5. *
  6. * The caller is must call file_test_reset() to initializing this module before
  7. * calling file_test_get_calls() or file_test_set_return().
  8. */
  9. define('FILE_URL_TEST_CDN_1', 'http://cdn1.example.com');
  10. define('FILE_URL_TEST_CDN_2', 'http://cdn2.example.com');
  11. /**
  12. * Implements hook_menu().
  13. */
  14. function file_test_menu() {
  15. $items['file-test/upload'] = array(
  16. 'title' => 'Upload test',
  17. 'page callback' => 'drupal_get_form',
  18. 'page arguments' => array('_file_test_form'),
  19. 'access arguments' => array('access content'),
  20. 'type' => MENU_CALLBACK,
  21. );
  22. return $items;
  23. }
  24. /**
  25. * Implements hook_stream_wrappers().
  26. */
  27. function file_test_stream_wrappers() {
  28. return array(
  29. 'dummy' => array(
  30. 'name' => t('Dummy files'),
  31. 'class' => 'DrupalDummyStreamWrapper',
  32. 'description' => t('Dummy wrapper for simpletest.'),
  33. ),
  34. 'dummy-remote' => array(
  35. 'name' => t('Dummy files (remote)'),
  36. 'class' => 'DrupalDummyRemoteStreamWrapper',
  37. 'description' => t('Dummy wrapper for simpletest (remote).'),
  38. ),
  39. );
  40. }
  41. /**
  42. * Form to test file uploads.
  43. */
  44. function _file_test_form($form, &$form_state) {
  45. $form['file_test_upload'] = array(
  46. '#type' => 'file',
  47. '#title' => t('Upload a file'),
  48. );
  49. $form['file_test_replace'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Replace existing image'),
  52. '#options' => array(
  53. FILE_EXISTS_RENAME => t('Appends number until name is unique'),
  54. FILE_EXISTS_REPLACE => t('Replace the existing file'),
  55. FILE_EXISTS_ERROR => t('Fail with an error'),
  56. ),
  57. '#default_value' => FILE_EXISTS_RENAME,
  58. );
  59. $form['file_subdir'] = array(
  60. '#type' => 'textfield',
  61. '#title' => t('Subdirectory for test file'),
  62. '#default_value' => '',
  63. );
  64. $form['extensions'] = array(
  65. '#type' => 'textfield',
  66. '#title' => t('Allowed extensions.'),
  67. '#default_value' => '',
  68. );
  69. $form['allow_all_extensions'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Allow all extensions?'),
  72. '#default_value' => FALSE,
  73. );
  74. $form['is_image_file'] = array(
  75. '#type' => 'checkbox',
  76. '#title' => t('Is this an image file?'),
  77. '#default_value' => TRUE,
  78. );
  79. $form['submit'] = array(
  80. '#type' => 'submit',
  81. '#value' => t('Submit'),
  82. );
  83. return $form;
  84. }
  85. /**
  86. * Process the upload.
  87. */
  88. function _file_test_form_submit(&$form, &$form_state) {
  89. // Process the upload and perform validation. Note: we're using the
  90. // form value for the $replace parameter.
  91. if (!empty($form_state['values']['file_subdir'])) {
  92. $destination = 'temporary://' . $form_state['values']['file_subdir'];
  93. file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
  94. }
  95. else {
  96. $destination = FALSE;
  97. }
  98. // Setup validators.
  99. $validators = array();
  100. if ($form_state['values']['is_image_file']) {
  101. $validators['file_validate_is_image'] = array();
  102. }
  103. if ($form_state['values']['allow_all_extensions']) {
  104. $validators['file_validate_extensions'] = array();
  105. }
  106. elseif (!empty($form_state['values']['extensions'])) {
  107. $validators['file_validate_extensions'] = array($form_state['values']['extensions']);
  108. }
  109. $file = file_save_upload('file_test_upload', $validators, $destination, $form_state['values']['file_test_replace']);
  110. if ($file) {
  111. $form_state['values']['file_test_upload'] = $file;
  112. drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->uri)));
  113. drupal_set_message(t('File name is @filename.', array('@filename' => $file->filename)));
  114. drupal_set_message(t('File MIME type is @mimetype.', array('@mimetype' => $file->filemime)));
  115. drupal_set_message(t('You WIN!'));
  116. }
  117. elseif ($file === FALSE) {
  118. drupal_set_message(t('Epic upload FAIL!'), 'error');
  119. }
  120. }
  121. /**
  122. * Reset/initialize the history of calls to the file_* hooks.
  123. *
  124. * @see file_test_get_calls()
  125. * @see file_test_reset()
  126. */
  127. function file_test_reset() {
  128. // Keep track of calls to these hooks
  129. $results = array(
  130. 'load' => array(),
  131. 'validate' => array(),
  132. 'download' => array(),
  133. 'insert' => array(),
  134. 'update' => array(),
  135. 'copy' => array(),
  136. 'move' => array(),
  137. 'delete' => array(),
  138. );
  139. variable_set('file_test_results', $results);
  140. // These hooks will return these values, see file_test_set_return().
  141. $return = array(
  142. 'validate' => array(),
  143. 'download' => NULL,
  144. );
  145. variable_set('file_test_return', $return);
  146. }
  147. /**
  148. * Get the arguments passed to invocation of a given hook since
  149. * file_test_reset() was last called.
  150. *
  151. * @param $op
  152. * One of the hook_file_* operations: 'load', 'validate', 'download',
  153. * 'insert', 'update', 'copy', 'move', 'delete'.
  154. *
  155. * @return
  156. * Array of the parameters passed to each call.
  157. *
  158. * @see _file_test_log_call()
  159. * @see file_test_reset()
  160. */
  161. function file_test_get_calls($op) {
  162. $results = variable_get('file_test_results', array());
  163. return $results[$op];
  164. }
  165. /**
  166. * Get an array with the calls for all hooks.
  167. *
  168. * @return
  169. * An array keyed by hook name ('load', 'validate', 'download', 'insert',
  170. * 'update', 'copy', 'move', 'delete') with values being arrays of parameters
  171. * passed to each call.
  172. */
  173. function file_test_get_all_calls() {
  174. return variable_get('file_test_results', array());
  175. }
  176. /**
  177. * Store the values passed to a hook invocation.
  178. *
  179. * @param $op
  180. * One of the hook_file_* operations: 'load', 'validate', 'download',
  181. * 'insert', 'update', 'copy', 'move', 'delete'.
  182. * @param $args
  183. * Values passed to hook.
  184. *
  185. * @see file_test_get_calls()
  186. * @see file_test_reset()
  187. */
  188. function _file_test_log_call($op, $args) {
  189. $results = variable_get('file_test_results', array());
  190. $results[$op][] = $args;
  191. variable_set('file_test_results', $results);
  192. }
  193. /**
  194. * Load the appropriate return value.
  195. *
  196. * @param $op
  197. * One of the hook_file_[validate,download] operations.
  198. *
  199. * @return
  200. * Value set by file_test_set_return().
  201. *
  202. * @see file_test_set_return()
  203. * @see file_test_reset()
  204. */
  205. function _file_test_get_return($op) {
  206. $return = variable_get('file_test_return', array($op => NULL));
  207. return $return[$op];
  208. }
  209. /**
  210. * Assign a return value for a given operation.
  211. *
  212. * @param $op
  213. * One of the hook_file_[validate,download] operations.
  214. * @param $value
  215. * Value for the hook to return.
  216. *
  217. * @see _file_test_get_return()
  218. * @see file_test_reset()
  219. */
  220. function file_test_set_return($op, $value) {
  221. $return = variable_get('file_test_return', array());
  222. $return[$op] = $value;
  223. variable_set('file_test_return', $return);
  224. }
  225. /**
  226. * Implements hook_file_load().
  227. */
  228. function file_test_file_load($files) {
  229. foreach ($files as $file) {
  230. _file_test_log_call('load', array($file));
  231. // Assign a value on the object so that we can test that the $file is passed
  232. // by reference.
  233. $file->file_test['loaded'] = TRUE;
  234. }
  235. }
  236. /**
  237. * Implements hook_file_validate().
  238. */
  239. function file_test_file_validate($file) {
  240. _file_test_log_call('validate', array($file));
  241. return _file_test_get_return('validate');
  242. }
  243. /**
  244. * Implements hook_file_download().
  245. */
  246. function file_test_file_download($uri) {
  247. _file_test_log_call('download', array($uri));
  248. return _file_test_get_return('download');
  249. }
  250. /**
  251. * Implements hook_file_insert().
  252. */
  253. function file_test_file_insert($file) {
  254. _file_test_log_call('insert', array($file));
  255. }
  256. /**
  257. * Implements hook_file_update().
  258. */
  259. function file_test_file_update($file) {
  260. _file_test_log_call('update', array($file));
  261. }
  262. /**
  263. * Implements hook_file_copy().
  264. */
  265. function file_test_file_copy($file, $source) {
  266. _file_test_log_call('copy', array($file, $source));
  267. }
  268. /**
  269. * Implements hook_file_move().
  270. */
  271. function file_test_file_move($file, $source) {
  272. _file_test_log_call('move', array($file, $source));
  273. }
  274. /**
  275. * Implements hook_file_delete().
  276. */
  277. function file_test_file_delete($file) {
  278. _file_test_log_call('delete', array($file));
  279. }
  280. /**
  281. * Implements hook_file_url_alter().
  282. */
  283. function file_test_file_url_alter(&$uri) {
  284. // Only run this hook when this variable is set. Otherwise, we'd have to add
  285. // another hidden test module just for this hook.
  286. $alter_mode = variable_get('file_test_hook_file_url_alter', FALSE);
  287. if (!$alter_mode) {
  288. return;
  289. }
  290. // Test alteration of file URLs to use a CDN.
  291. elseif ($alter_mode == 'cdn') {
  292. $cdn_extensions = array('css', 'js', 'gif', 'jpg', 'jpeg', 'png');
  293. // Most CDNs don't support private file transfers without a lot of hassle,
  294. // so don't support this in the common case.
  295. $schemes = array('public');
  296. $scheme = file_uri_scheme($uri);
  297. // Only serve shipped files and public created files from the CDN.
  298. if (!$scheme || in_array($scheme, $schemes)) {
  299. // Shipped files.
  300. if (!$scheme) {
  301. $path = $uri;
  302. }
  303. // Public created files.
  304. else {
  305. $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
  306. $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
  307. }
  308. // Clean up Windows paths.
  309. $path = str_replace('\\', '/', $path);
  310. // Serve files with one of the CDN extensions from CDN 1, all others from
  311. // CDN 2.
  312. $pathinfo = pathinfo($path);
  313. if (array_key_exists('extension', $pathinfo) && in_array($pathinfo['extension'], $cdn_extensions)) {
  314. $uri = FILE_URL_TEST_CDN_1 . '/' . $path;
  315. }
  316. else {
  317. $uri = FILE_URL_TEST_CDN_2 . '/' . $path;
  318. }
  319. }
  320. }
  321. // Test alteration of file URLs to use root-relative URLs.
  322. elseif ($alter_mode == 'root-relative') {
  323. // Only serve shipped files and public created files with root-relative
  324. // URLs.
  325. $scheme = file_uri_scheme($uri);
  326. if (!$scheme || $scheme == 'public') {
  327. // Shipped files.
  328. if (!$scheme) {
  329. $path = $uri;
  330. }
  331. // Public created files.
  332. else {
  333. $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
  334. $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
  335. }
  336. // Clean up Windows paths.
  337. $path = str_replace('\\', '/', $path);
  338. // Generate a root-relative URL.
  339. $uri = base_path() . '/' . $path;
  340. }
  341. }
  342. // Test alteration of file URLs to use protocol-relative URLs.
  343. elseif ($alter_mode == 'protocol-relative') {
  344. // Only serve shipped files and public created files with protocol-relative
  345. // URLs.
  346. $scheme = file_uri_scheme($uri);
  347. if (!$scheme || $scheme == 'public') {
  348. // Shipped files.
  349. if (!$scheme) {
  350. $path = $uri;
  351. }
  352. // Public created files.
  353. else {
  354. $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
  355. $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
  356. }
  357. // Clean up Windows paths.
  358. $path = str_replace('\\', '/', $path);
  359. // Generate a protocol-relative URL.
  360. $uri = '/' . base_path() . '/' . $path;
  361. }
  362. }
  363. }
  364. /**
  365. * Implements hook_file_mimetype_mapping_alter().
  366. */
  367. function file_test_file_mimetype_mapping_alter(&$mapping) {
  368. // Add new mappings.
  369. $mapping['mimetypes']['file_test_mimetype_1'] = 'madeup/file_test_1';
  370. $mapping['mimetypes']['file_test_mimetype_2'] = 'madeup/file_test_2';
  371. $mapping['mimetypes']['file_test_mimetype_3'] = 'madeup/doc';
  372. $mapping['extensions']['file_test_1'] = 'file_test_mimetype_1';
  373. $mapping['extensions']['file_test_2'] = 'file_test_mimetype_2';
  374. $mapping['extensions']['file_test_3'] = 'file_test_mimetype_2';
  375. // Override existing mapping.
  376. $mapping['extensions']['doc'] = 'file_test_mimetype_3';
  377. }
  378. /**
  379. * Helper class for testing the stream wrapper registry.
  380. *
  381. * Dummy stream wrapper implementation (dummy://).
  382. */
  383. class DrupalDummyStreamWrapper extends DrupalLocalStreamWrapper {
  384. function getDirectoryPath() {
  385. return variable_get('stream_public_path', 'sites/default/files');
  386. }
  387. /**
  388. * Override getInternalUri().
  389. *
  390. * Return a dummy path for testing.
  391. */
  392. function getInternalUri() {
  393. return '/dummy/example.txt';
  394. }
  395. /**
  396. * Override getExternalUrl().
  397. *
  398. * Return the HTML URI of a public file.
  399. */
  400. function getExternalUrl() {
  401. return '/dummy/example.txt';
  402. }
  403. }
  404. /**
  405. * Helper class for testing the stream wrapper registry.
  406. *
  407. * Dummy remote stream wrapper implementation (dummy-remote://).
  408. *
  409. * Basically just the public scheme but not returning a local file for realpath.
  410. */
  411. class DrupalDummyRemoteStreamWrapper extends DrupalPublicStreamWrapper {
  412. function realpath() {
  413. return FALSE;
  414. }
  415. }