- <?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().
- */
-
-
- define('FILE_URL_TEST_CDN_1', 'http://cdn1.example.com');
- define('FILE_URL_TEST_CDN_2', 'http://cdn2.example.com');
-
-
- * Implements hook_menu().
- */
- function file_test_menu() {
- $items['file-test/upload'] = array(
- 'title' => 'Upload test',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array('_file_test_form'),
- 'access callback' => TRUE,
- 'type' => MENU_CALLBACK,
- );
- return $items;
- }
-
- * Implements hook_stream_wrappers().
- */
- function file_test_stream_wrappers() {
- return array(
- 'dummy' => array(
- 'name' => t('Dummy files'),
- 'class' => 'BackdropDummyStreamWrapper',
- 'description' => t('Dummy wrapper for simpletest.'),
- ),
- 'dummy-remote' => array(
- 'name' => t('Dummy files (remote)'),
- 'class' => 'BackdropDummyRemoteStreamWrapper',
- 'description' => t('Dummy wrapper for simpletest (remote).'),
- ),
- );
- }
-
- * Form to test file uploads.
- */
- function _file_test_form($form, &$form_state) {
- $form['file_test_upload'] = array(
- '#type' => 'file',
- '#title' => t('Upload a file'),
- );
- $form['file_test_replace'] = array(
- '#type' => 'select',
- '#title' => t('Replace existing image'),
- '#options' => array(
- FILE_EXISTS_RENAME => t('Appends number until name is unique'),
- FILE_EXISTS_REPLACE => t('Replace the existing file'),
- FILE_EXISTS_ERROR => t('Fail with an error'),
- ),
- '#default_value' => FILE_EXISTS_RENAME,
- );
- $form['file_subdirectory'] = array(
- '#type' => 'textfield',
- '#title' => t('Subdirectory for test file'),
- '#default_value' => '',
- );
-
- $form['extensions'] = array(
- '#type' => 'textfield',
- '#title' => t('Allowed extensions.'),
- '#default_value' => '',
- );
-
- $form['allow_all_extensions'] = array(
- '#type' => 'radios',
- '#options' => array(
- 'false' => 'No',
- 'empty_array' => 'Empty array',
- 'empty_string' => 'Empty string',
- ),
- '#default_value' => 'false',
- );
-
- $form['is_image_file'] = array(
- '#type' => 'checkbox',
- '#title' => t('Is this an image file?'),
- '#default_value' => TRUE,
- );
-
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit'),
- );
- return $form;
- }
-
- * Process the upload.
- */
- function _file_test_form_submit(&$form, &$form_state) {
-
-
- if (!empty($form_state['values']['file_subdirectory'])) {
- $destination = 'temporary://' . $form_state['values']['file_subdirectory'];
- file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
- }
- else {
- $destination = FALSE;
- }
-
-
- $validators = array();
- if ($form_state['values']['is_image_file']) {
- $validators['file_validate_is_image'] = array();
- }
-
- $allow = $form_state['values']['allow_all_extensions'];
- if ($allow === 'empty_array') {
- $validators['file_validate_extensions'] = array();
- }
- elseif ($allow === 'empty_string') {
- $validators['file_validate_extensions'] = array('');
- }
- elseif (!empty($form_state['values']['extensions'])) {
- $validators['file_validate_extensions'] = array($form_state['values']['extensions']);
- }
-
- $file = file_save_upload('file_test_upload', $validators, $destination, $form_state['values']['file_test_replace']);
- if ($file) {
- $form_state['values']['file_test_upload'] = $file;
- backdrop_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->uri)));
- backdrop_set_message(t('File name is @filename.', array('@filename' => $file->filename)));
- backdrop_set_message(t('File MIME type is @mimetype.', array('@mimetype' => $file->filemime)));
- backdrop_set_message(t('You WIN!'));
- }
- elseif ($file === FALSE) {
- backdrop_set_message(t('Epic upload FAIL!'), 'error');
- }
- }
-
-
- * Reset/initialize the history of calls to the file_* hooks.
- *
- * @see file_test_get_calls()
- * @see file_test_reset()
- */
- function file_test_reset() {
-
- $results = array(
- 'load' => array(),
- 'validate' => array(),
- 'download' => array(),
- 'insert' => array(),
- 'update' => array(),
- 'copy' => array(),
- 'move' => array(),
- 'delete' => array(),
- );
- state_set('file_test_results', $results);
-
-
- $return = array(
- 'validate' => array(),
- 'download' => NULL,
- );
- state_set('file_test_return', $return);
- }
-
- * Get the arguments passed to invocation of a given hook since
- * file_test_reset() was last called.
- *
- * @param $op
- * One of the hook_file_* operations: 'load', 'validate', 'download',
- * 'insert', 'update', 'copy', 'move', 'delete'.
- *
- * @return
- * Array of the parameters passed to each call.
- *
- * @see _file_test_log_call()
- * @see file_test_reset()
- */
- function file_test_get_calls($op) {
- $results = state_get('file_test_results', array());
- return $results[$op];
- }
-
- * Get an array with the calls for all hooks.
- *
- * @return
- * 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 state_get('file_test_results', array());
- }
-
- * Store the values passed to a hook invocation.
- *
- * @param $op
- * One of the hook_file_* operations: 'load', 'validate', 'download',
- * 'insert', 'update', 'copy', 'move', 'delete'.
- * @param $args
- * Values passed to hook.
- *
- * @see file_test_get_calls()
- * @see file_test_reset()
- */
- function _file_test_log_call($op, $args) {
- $results = state_get('file_test_results', array());
- $results[$op][] = $args;
- state_set('file_test_results', $results);
- }
-
- * Load the appropriate return value.
- *
- * @param $op
- * One of the hook_file_[validate,download] operations.
- *
- * @return
- * Value set by file_test_set_return().
- *
- * @see file_test_set_return()
- * @see file_test_reset()
- */
- function _file_test_get_return($op) {
- $return = state_get('file_test_return', array($op => NULL));
- return $return[$op];
- }
-
- * Assign a return value for a given operation.
- *
- * @param $op
- * One of the hook_file_[validate,download] operations.
- * @param $value
- * Value for the hook to return.
- *
- * @see _file_test_get_return()
- * @see file_test_reset()
- */
- function file_test_set_return($op, $value) {
- $return = state_get('file_test_return', array());
- $return[$op] = $value;
- state_set('file_test_return', $return);
- }
-
- * Implements hook_file_load().
- */
- function file_test_file_load($files) {
- foreach ($files as $file) {
- _file_test_log_call('load', array($file->fid));
-
-
- $file->file_test['loaded'] = TRUE;
- }
- }
-
- * Implements hook_file_validate().
- */
- function file_test_file_validate(File $file) {
- _file_test_log_call('validate', array($file->fid));
- return _file_test_get_return('validate');
- }
-
- * Implements hook_file_download().
- */
- function file_test_file_download($uri) {
- _file_test_log_call('download', array($uri));
- return _file_test_get_return('download');
- }
-
- * Implements hook_file_insert().
- */
- function file_test_file_insert(File $file) {
- _file_test_log_call('insert', array($file->fid));
- }
-
- * Implements hook_file_update().
- */
- function file_test_file_update(File $file) {
- _file_test_log_call('update', array($file->fid));
- }
-
- * Implements hook_file_copy().
- */
- function file_test_file_copy(File $file, $source) {
- _file_test_log_call('copy', array($file->fid, $source->fid));
- }
-
- * Implements hook_file_move().
- */
- function file_test_file_move(File $file, File $source) {
- _file_test_log_call('move', array($file->fid, $source->fid));
- }
-
- * Implements hook_file_predelete().
- */
- function file_test_file_predelete(File $file) {
- _file_test_log_call('delete', array($file->fid));
- }
-
- * Implements hook_file_url_alter().
- */
- function file_test_file_url_alter(&$uri) {
-
-
- $alter_mode = state_get('file_test_hook_file_url_alter', FALSE);
- if (!$alter_mode) {
- return;
- }
-
- elseif ($alter_mode == 'cdn') {
- $cdn_extensions = array('css', 'js', 'gif', 'jpg', 'jpeg', 'png');
-
-
-
- $schemes = array('public');
-
- $scheme = file_uri_scheme($uri);
-
-
- if (!$scheme || in_array($scheme, $schemes)) {
-
- if (!$scheme) {
- $path = $uri;
- }
-
- else {
- $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
- $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
- }
-
-
- $path = str_replace('\\', '/', $path);
-
-
-
- $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;
- }
- }
- }
-
- elseif ($alter_mode == 'root-relative') {
-
-
- $scheme = file_uri_scheme($uri);
- if (!$scheme || $scheme == 'public') {
-
- if (!$scheme) {
- $path = $uri;
- }
-
- else {
- $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
- $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
- }
-
-
- $path = str_replace('\\', '/', $path);
-
-
- $uri = base_path() . '/' . $path;
- }
- }
-
- elseif ($alter_mode == 'protocol-relative') {
-
-
- $scheme = file_uri_scheme($uri);
- if (!$scheme || $scheme == 'public') {
-
- if (!$scheme) {
- $path = $uri;
- }
-
- else {
- $wrapper = file_stream_wrapper_get_instance_by_scheme($scheme);
- $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($uri);
- }
-
-
- $path = str_replace('\\', '/', $path);
-
-
- $uri = '/' . base_path() . '/' . $path;
- }
- }
- }
-
- * Implements hook_file_mimetype_mapping_alter().
- */
- function file_test_file_mimetype_mapping_alter(&$mapping) {
-
- $mapping['mimetypes']['file_test_mimetype_1'] = 'made_up/file_test_1';
- $mapping['mimetypes']['file_test_mimetype_2'] = 'made_up/file_test_2';
- $mapping['mimetypes']['file_test_mimetype_3'] = 'made_up/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';
-
- $mapping['extensions']['doc'] = 'file_test_mimetype_3';
- }
-
- * Implements hook_autoload_info().
- */
- function file_test_autoload_info() {
- return array(
- 'BackdropDummyStreamWrapper' => 'file_test.stream_wrappers.inc',
- 'BackdropDummyRemoteStreamWrapper' => 'file_test.stream_wrappers.inc',
- );
- }