1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
<?php
// $Id$
/**
* @file
* Helper module for the file tests.
*/
/**
* Implementation of hook_menu().
*/
function file_test_menu() {
$items['file-test/upload'] = array(
'title' => t('Upload test'),
'page callback' => 'drupal_get_form',
'page arguments' => array('_file_test_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Form to test file uploads.
*/
function _file_test_form(&$form_state) {
$form['#attributes'] = array('enctype' => 'multipart/form-data');
$form['file_test_upload'] = array(
'#type' => 'file',
'#title' => t('Upload an image'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Process the upload.
*/
function _file_test_form_submit(&$form, &$form_state) {
// Validate the uploaded picture.
$file = file_save_upload('file_test_upload', array('file_validate_is_image' => array()));
if ($file) {
$form_state['values']['file_test_upload'] = $file;
drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->filepath)));
}
else {
drupal_set_message(t('Epic upload FAIL!'), 'error');
}
}
/**
* Reset/initialize the history of calls to the file_* hooks.
*/
function file_test_reset() {
// Keep track of calls to these hooks
$GLOBALS['file_test_results'] = array(
'load' => array(),
'validate' => array(),
'download' => array(),
'references' => array(),
'status' => array(),
'insert' => array(),
'update' => array(),
'copy' => array(),
'move' => array(),
'delete' => array(),
);
// These hooks will return these values.
$GLOBALS['file_test_hook_return'] = array(
'validate' => NULL,
'download' => NULL,
'references' => NULL,
);
}
/**
* Get the values passed to a the hook calls for a given operation.
*
* @param $op One of the hook_file_* operations.
* @returns Array of the parameters passed to each call.
*/
function file_test_get_calls($op) {
return $GLOBALS['file_test_results'][$op];
}
/**
* Implementation of hook_file_load().
*/
function file_test_file_load(&$file) {
$GLOBALS['file_test_results']['load'][] = func_get_args();
// Assign a value on the object so that we can test that the $file is passed
// by reference.
$file->file_test['loaded'] = TRUE;
}
/**
* Implementation of hook_file_validate().
*/
function file_test_file_validate(&$file) {
$GLOBALS['file_test_results']['validate'][] = func_get_args();
return $GLOBALS['file_test_hook_return']['validate'];
}
/**
* Implementation of hook_file_status().
*/
function file_test_file_status(&$file) {
$GLOBALS['file_test_results']['status'][] = func_get_args();
}
/**
* Implementation of hook_file_download().
*/
function file_test_file_download(&$file) {
$GLOBALS['file_test_results']['download'][] = func_get_args();
return $GLOBALS['file_test_hook_return']['download'];
}
/**
* Implementation of hook_file_references().
*/
function file_test_file_references(&$file) {
$GLOBALS['file_test_results']['references'][] = func_get_args();
return $GLOBALS['file_test_hook_return']['references'];
}
/**
* Implementation of hook_file_insert().
*/
function file_test_file_insert(&$file) {
$GLOBALS['file_test_results']['insert'][] = func_get_args();
}
/**
* Implementation of hook_file_update().
*/
function file_test_file_update(&$file) {
$GLOBALS['file_test_results']['update'][] = func_get_args();
}
/**
* Implemenation of hook_file_copy().
*/
function file_test_file_copy(&$file, &$source) {
$GLOBALS['file_test_results']['copy'][] = func_get_args();
}
/**
* Implemenation of hook_file_move().
*/
function file_test_file_move(&$file, &$source) {
$GLOBALS['file_test_results']['move'][] = func_get_args();
}
/**
* Implementation of hook_file_delete().
*/
function file_test_file_delete(&$file) {
$GLOBALS['file_test_results']['delete'][] = func_get_args();
}
|