file_test.module 12 KB

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