1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests for file.module.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Provides methods specifically for testing File module's field handling.
|
10
|
*/
|
11
|
class FileFieldTestCase extends DrupalWebTestCase {
|
12
|
protected $admin_user;
|
13
|
|
14
|
function setUp() {
|
15
|
// Since this is a base class for many test cases, support the same
|
16
|
// flexibility that DrupalWebTestCase::setUp() has for the modules to be
|
17
|
// passed in as either an array or a variable number of string arguments.
|
18
|
$modules = func_get_args();
|
19
|
if (isset($modules[0]) && is_array($modules[0])) {
|
20
|
$modules = $modules[0];
|
21
|
}
|
22
|
$modules[] = 'file';
|
23
|
$modules[] = 'file_module_test';
|
24
|
parent::setUp($modules);
|
25
|
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer users', 'administer permissions', 'administer content types', 'administer nodes', 'bypass node access'));
|
26
|
$this->drupalLogin($this->admin_user);
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Retrieves a sample file of the specified type.
|
31
|
*/
|
32
|
function getTestFile($type_name, $size = NULL) {
|
33
|
// Get a file to upload.
|
34
|
$file = current($this->drupalGetTestFiles($type_name, $size));
|
35
|
|
36
|
// Add a filesize property to files as would be read by file_load().
|
37
|
$file->filesize = filesize($file->uri);
|
38
|
|
39
|
return $file;
|
40
|
}
|
41
|
|
42
|
/**
|
43
|
* Retrieves the fid of the last inserted file.
|
44
|
*/
|
45
|
function getLastFileId() {
|
46
|
return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Creates a new file field.
|
51
|
*
|
52
|
* @param $name
|
53
|
* The name of the new field (all lowercase), exclude the "field_" prefix.
|
54
|
* @param $type_name
|
55
|
* The node type that this field will be added to.
|
56
|
* @param $field_settings
|
57
|
* A list of field settings that will be added to the defaults.
|
58
|
* @param $instance_settings
|
59
|
* A list of instance settings that will be added to the instance defaults.
|
60
|
* @param $widget_settings
|
61
|
* A list of widget settings that will be added to the widget defaults.
|
62
|
*/
|
63
|
function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
|
64
|
$field = array(
|
65
|
'field_name' => $name,
|
66
|
'type' => 'file',
|
67
|
'settings' => array(),
|
68
|
'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
|
69
|
);
|
70
|
$field['settings'] = array_merge($field['settings'], $field_settings);
|
71
|
field_create_field($field);
|
72
|
|
73
|
$this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings);
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Attaches a file field to an entity.
|
78
|
*
|
79
|
* @param $name
|
80
|
* The name of the new field (all lowercase), exclude the "field_" prefix.
|
81
|
* @param $entity_type
|
82
|
* The entity type this field will be added to.
|
83
|
* @param $bundle
|
84
|
* The bundle this field will be added to.
|
85
|
* @param $field_settings
|
86
|
* A list of field settings that will be added to the defaults.
|
87
|
* @param $instance_settings
|
88
|
* A list of instance settings that will be added to the instance defaults.
|
89
|
* @param $widget_settings
|
90
|
* A list of widget settings that will be added to the widget defaults.
|
91
|
*/
|
92
|
function attachFileField($name, $entity_type, $bundle, $instance_settings = array(), $widget_settings = array()) {
|
93
|
$instance = array(
|
94
|
'field_name' => $name,
|
95
|
'label' => $name,
|
96
|
'entity_type' => $entity_type,
|
97
|
'bundle' => $bundle,
|
98
|
'required' => !empty($instance_settings['required']),
|
99
|
'settings' => array(),
|
100
|
'widget' => array(
|
101
|
'type' => 'file_generic',
|
102
|
'settings' => array(),
|
103
|
),
|
104
|
);
|
105
|
$instance['settings'] = array_merge($instance['settings'], $instance_settings);
|
106
|
$instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
|
107
|
field_create_instance($instance);
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Updates an existing file field with new settings.
|
112
|
*/
|
113
|
function updateFileField($name, $type_name, $instance_settings = array(), $widget_settings = array()) {
|
114
|
$instance = field_info_instance('node', $name, $type_name);
|
115
|
$instance['settings'] = array_merge($instance['settings'], $instance_settings);
|
116
|
$instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
|
117
|
|
118
|
field_update_instance($instance);
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Uploads a file to a node.
|
123
|
*/
|
124
|
function uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE, $extras = array()) {
|
125
|
$langcode = LANGUAGE_NONE;
|
126
|
$edit = array(
|
127
|
"title" => $this->randomName(),
|
128
|
'revision' => (string) (int) $new_revision,
|
129
|
);
|
130
|
|
131
|
if (is_numeric($nid_or_type)) {
|
132
|
$nid = $nid_or_type;
|
133
|
}
|
134
|
else {
|
135
|
// Add a new node.
|
136
|
$extras['type'] = $nid_or_type;
|
137
|
$node = $this->drupalCreateNode($extras);
|
138
|
$nid = $node->nid;
|
139
|
// Save at least one revision to better simulate a real site.
|
140
|
$this->drupalCreateNode(get_object_vars($node));
|
141
|
$node = node_load($nid, NULL, TRUE);
|
142
|
$this->assertNotEqual($nid, $node->vid, 'Node revision exists.');
|
143
|
}
|
144
|
|
145
|
// Attach a file to the node.
|
146
|
$edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
|
147
|
$this->drupalPost("node/$nid/edit", $edit, t('Save'));
|
148
|
|
149
|
return $nid;
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* Removes a file from a node.
|
154
|
*
|
155
|
* Note that if replacing a file, it must first be removed then added again.
|
156
|
*/
|
157
|
function removeNodeFile($nid, $new_revision = TRUE) {
|
158
|
$edit = array(
|
159
|
'revision' => (string) (int) $new_revision,
|
160
|
);
|
161
|
|
162
|
$this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
|
163
|
$this->drupalPost(NULL, $edit, t('Save'));
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Replaces a file within a node.
|
168
|
*/
|
169
|
function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
|
170
|
$edit = array(
|
171
|
'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($file->uri),
|
172
|
'revision' => (string) (int) $new_revision,
|
173
|
);
|
174
|
|
175
|
$this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
|
176
|
$this->drupalPost(NULL, $edit, t('Save'));
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* Asserts that a file exists physically on disk.
|
181
|
*/
|
182
|
function assertFileExists($file, $message = NULL) {
|
183
|
$message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->uri));
|
184
|
$this->assertTrue(is_file($file->uri), $message);
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* Asserts that a file exists in the database.
|
189
|
*/
|
190
|
function assertFileEntryExists($file, $message = NULL) {
|
191
|
entity_get_controller('file')->resetCache();
|
192
|
$db_file = file_load($file->fid);
|
193
|
$message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->uri));
|
194
|
$this->assertEqual($db_file->uri, $file->uri, $message);
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Asserts that a file does not exist on disk.
|
199
|
*/
|
200
|
function assertFileNotExists($file, $message = NULL) {
|
201
|
$message = isset($message) ? $message : format_string('File %file exists on the disk.', array('%file' => $file->uri));
|
202
|
$this->assertFalse(is_file($file->uri), $message);
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Asserts that a file does not exist in the database.
|
207
|
*/
|
208
|
function assertFileEntryNotExists($file, $message) {
|
209
|
entity_get_controller('file')->resetCache();
|
210
|
$message = isset($message) ? $message : format_string('File %file exists in database at the correct path.', array('%file' => $file->uri));
|
211
|
$this->assertFalse(file_load($file->fid), $message);
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Asserts that a file's status is set to permanent in the database.
|
216
|
*/
|
217
|
function assertFileIsPermanent($file, $message = NULL) {
|
218
|
$message = isset($message) ? $message : format_string('File %file is permanent.', array('%file' => $file->uri));
|
219
|
$this->assertTrue($file->status == FILE_STATUS_PERMANENT, $message);
|
220
|
}
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Tests adding a file to a non-node entity.
|
225
|
*/
|
226
|
class FileTaxonomyTermTestCase extends DrupalWebTestCase {
|
227
|
protected $admin_user;
|
228
|
|
229
|
public static function getInfo() {
|
230
|
return array(
|
231
|
'name' => 'Taxonomy term file test',
|
232
|
'description' => 'Tests adding a file to a non-node entity.',
|
233
|
'group' => 'File',
|
234
|
);
|
235
|
}
|
236
|
|
237
|
public function setUp() {
|
238
|
$modules[] = 'file';
|
239
|
$modules[] = 'taxonomy';
|
240
|
parent::setUp($modules);
|
241
|
$this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer taxonomy'));
|
242
|
$this->drupalLogin($this->admin_user);
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Creates a file field and attaches it to the "Tags" taxonomy vocabulary.
|
247
|
*
|
248
|
* @param $name
|
249
|
* The field name of the file field to create.
|
250
|
* @param $uri_scheme
|
251
|
* The URI scheme to use for the file field (for example, "private" to
|
252
|
* create a field that stores private files or "public" to create a field
|
253
|
* that stores public files).
|
254
|
*/
|
255
|
protected function createAttachFileField($name, $uri_scheme) {
|
256
|
$field = array(
|
257
|
'field_name' => $name,
|
258
|
'type' => 'file',
|
259
|
'settings' => array(
|
260
|
'uri_scheme' => $uri_scheme,
|
261
|
),
|
262
|
'cardinality' => 1,
|
263
|
);
|
264
|
field_create_field($field);
|
265
|
// Attach an instance of it.
|
266
|
$instance = array(
|
267
|
'field_name' => $name,
|
268
|
'label' => 'File',
|
269
|
'entity_type' => 'taxonomy_term',
|
270
|
'bundle' => 'tags',
|
271
|
'required' => FALSE,
|
272
|
'settings' => array(),
|
273
|
'widget' => array(
|
274
|
'type' => 'file_generic',
|
275
|
'settings' => array(),
|
276
|
),
|
277
|
);
|
278
|
field_create_instance($instance);
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
* Tests that a public file can be attached to a taxonomy term.
|
283
|
*
|
284
|
* This is a regression test for https://www.drupal.org/node/2305017.
|
285
|
*/
|
286
|
public function testTermFilePublic() {
|
287
|
$this->_testTermFile('public');
|
288
|
}
|
289
|
|
290
|
/**
|
291
|
* Tests that a private file can be attached to a taxonomy term.
|
292
|
*
|
293
|
* This is a regression test for https://www.drupal.org/node/2305017.
|
294
|
*/
|
295
|
public function testTermFilePrivate() {
|
296
|
$this->_testTermFile('private');
|
297
|
}
|
298
|
|
299
|
/**
|
300
|
* Runs tests for attaching a file field to a taxonomy term.
|
301
|
*
|
302
|
* @param $uri_scheme
|
303
|
* The URI scheme to use for the file field, either "public" or "private".
|
304
|
*/
|
305
|
protected function _testTermFile($uri_scheme) {
|
306
|
$field_name = strtolower($this->randomName());
|
307
|
$this->createAttachFileField($field_name, $uri_scheme);
|
308
|
// Get a file to upload.
|
309
|
$file = current($this->drupalGetTestFiles('text'));
|
310
|
// Add a filesize property to files as would be read by file_load().
|
311
|
$file->filesize = filesize($file->uri);
|
312
|
$langcode = LANGUAGE_NONE;
|
313
|
$edit = array(
|
314
|
"name" => $this->randomName(),
|
315
|
);
|
316
|
// Attach a file to the term.
|
317
|
$edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
|
318
|
$this->drupalPost("admin/structure/taxonomy/tags/add", $edit, t('Save'));
|
319
|
// Find the term ID we just created.
|
320
|
$tid = db_query_range('SELECT tid FROM {taxonomy_term_data} ORDER BY tid DESC', 0, 1)->fetchField();
|
321
|
$terms = entity_load('taxonomy_term', array($tid));
|
322
|
$term = $terms[$tid];
|
323
|
$fid = $term->{$field_name}[LANGUAGE_NONE][0]['fid'];
|
324
|
// Check that the uploaded file is present on the edit form.
|
325
|
$this->drupalGet("taxonomy/term/$tid/edit");
|
326
|
$file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
|
327
|
$this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
|
328
|
// Edit the term and change name without changing the file.
|
329
|
$edit = array(
|
330
|
"name" => $this->randomName(),
|
331
|
);
|
332
|
$this->drupalPost("taxonomy/term/$tid/edit", $edit, t('Save'));
|
333
|
// Check that the uploaded file is still present on the edit form.
|
334
|
$this->drupalGet("taxonomy/term/$tid/edit");
|
335
|
$file_input_name = $field_name . '[' . LANGUAGE_NONE . '][0][fid]';
|
336
|
$this->assertFieldByXpath('//input[@type="hidden" and @name="' . $file_input_name . '"]', $fid, 'File is attached on edit form.');
|
337
|
// Load term while resetting the cache.
|
338
|
$terms = entity_load('taxonomy_term', array($tid), array(), TRUE);
|
339
|
$term = $terms[$tid];
|
340
|
$this->assertTrue(!empty($term->{$field_name}[LANGUAGE_NONE]), 'Term has attached files.');
|
341
|
$this->assertEqual($term->{$field_name}[LANGUAGE_NONE][0]['fid'], $fid, 'Same File ID is attached to the term.');
|
342
|
}
|
343
|
}
|
344
|
|
345
|
/**
|
346
|
* Tests the 'managed_file' element type.
|
347
|
*
|
348
|
* @todo Create a FileTestCase base class and move FileFieldTestCase methods
|
349
|
* that aren't related to fields into it.
|
350
|
*/
|
351
|
class FileManagedFileElementTestCase extends FileFieldTestCase {
|
352
|
public static function getInfo() {
|
353
|
return array(
|
354
|
'name' => 'Managed file element test',
|
355
|
'description' => 'Tests the managed_file element type.',
|
356
|
'group' => 'File',
|
357
|
);
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* Tests the managed_file element type.
|
362
|
*/
|
363
|
function testManagedFile() {
|
364
|
// Check that $element['#size'] is passed to the child upload element.
|
365
|
$this->drupalGet('file/test');
|
366
|
$this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
|
367
|
|
368
|
// Perform the tests with all permutations of $form['#tree'] and
|
369
|
// $element['#extended'].
|
370
|
foreach (array(0, 1) as $tree) {
|
371
|
foreach (array(0, 1) as $extended) {
|
372
|
$test_file = $this->getTestFile('text');
|
373
|
$path = 'file/test/' . $tree . '/' . $extended;
|
374
|
$input_base_name = $tree ? 'nested_file' : 'file';
|
375
|
|
376
|
// Submit without a file.
|
377
|
$this->drupalPost($path, array(), t('Save'));
|
378
|
$this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submitted without a file.');
|
379
|
|
380
|
// Submit a new file, without using the Upload button.
|
381
|
$last_fid_prior = $this->getLastFileId();
|
382
|
$edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
|
383
|
$this->drupalPost($path, $edit, t('Save'));
|
384
|
$last_fid = $this->getLastFileId();
|
385
|
$this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
|
386
|
$this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
|
387
|
|
388
|
// Submit no new input, but with a default file.
|
389
|
$this->drupalPost($path . '/' . $last_fid, array(), t('Save'));
|
390
|
$this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Empty submission did not change an existing file.');
|
391
|
|
392
|
// Now, test the Upload and Remove buttons, with and without Ajax.
|
393
|
foreach (array(FALSE, TRUE) as $ajax) {
|
394
|
// Upload, then Submit.
|
395
|
$last_fid_prior = $this->getLastFileId();
|
396
|
$this->drupalGet($path);
|
397
|
$edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
|
398
|
if ($ajax) {
|
399
|
$this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
|
400
|
}
|
401
|
else {
|
402
|
$this->drupalPost(NULL, $edit, t('Upload'));
|
403
|
}
|
404
|
$last_fid = $this->getLastFileId();
|
405
|
$this->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
|
406
|
$this->drupalPost(NULL, array(), t('Save'));
|
407
|
$this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), 'Submit handler has correct file info.');
|
408
|
|
409
|
// Remove, then Submit.
|
410
|
$this->drupalGet($path . '/' . $last_fid);
|
411
|
if ($ajax) {
|
412
|
$this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
|
413
|
}
|
414
|
else {
|
415
|
$this->drupalPost(NULL, array(), t('Remove'));
|
416
|
}
|
417
|
$this->drupalPost(NULL, array(), t('Save'));
|
418
|
$this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file removal was successful.');
|
419
|
|
420
|
// Upload, then Remove, then Submit.
|
421
|
$this->drupalGet($path);
|
422
|
$edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
|
423
|
if ($ajax) {
|
424
|
$this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
|
425
|
$this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
|
426
|
}
|
427
|
else {
|
428
|
$this->drupalPost(NULL, $edit, t('Upload'));
|
429
|
$this->drupalPost(NULL, array(), t('Remove'));
|
430
|
}
|
431
|
$this->drupalPost(NULL, array(), t('Save'));
|
432
|
$this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), 'Submission after file upload and removal was successful.');
|
433
|
}
|
434
|
}
|
435
|
}
|
436
|
}
|
437
|
}
|
438
|
|
439
|
/**
|
440
|
* Tests file field widget.
|
441
|
*/
|
442
|
class FileFieldWidgetTestCase extends FileFieldTestCase {
|
443
|
public static function getInfo() {
|
444
|
return array(
|
445
|
'name' => 'File field widget test',
|
446
|
'description' => 'Tests the file field widget, single and multi-valued, with and without AJAX, with public and private files.',
|
447
|
'group' => 'File',
|
448
|
);
|
449
|
}
|
450
|
|
451
|
/**
|
452
|
* Tests upload and remove buttons for a single-valued File field.
|
453
|
*/
|
454
|
function testSingleValuedWidget() {
|
455
|
// Use 'page' instead of 'article', so that the 'article' image field does
|
456
|
// not conflict with this test. If in the future the 'page' type gets its
|
457
|
// own default file or image field, this test can be made more robust by
|
458
|
// using a custom node type.
|
459
|
$type_name = 'page';
|
460
|
$field_name = strtolower($this->randomName());
|
461
|
$this->createFileField($field_name, $type_name);
|
462
|
$field = field_info_field($field_name);
|
463
|
$instance = field_info_instance('node', $field_name, $type_name);
|
464
|
|
465
|
$test_file = $this->getTestFile('text');
|
466
|
|
467
|
foreach (array('nojs', 'js') as $type) {
|
468
|
// Create a new node with the uploaded file and ensure it got uploaded
|
469
|
// successfully.
|
470
|
// @todo This only tests a 'nojs' submission, because drupalPostAJAX()
|
471
|
// does not yet support file uploads.
|
472
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
473
|
$node = node_load($nid, NULL, TRUE);
|
474
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
475
|
$this->assertFileExists($node_file, 'New file saved to disk on node creation.');
|
476
|
|
477
|
// Ensure the file can be downloaded.
|
478
|
$this->drupalGet(file_create_url($node_file->uri));
|
479
|
$this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
|
480
|
|
481
|
// Ensure the edit page has a remove button instead of an upload button.
|
482
|
$this->drupalGet("node/$nid/edit");
|
483
|
$this->assertNoFieldByXPath('//input[@type="submit"]', t('Upload'), 'Node with file does not display the "Upload" button.');
|
484
|
$this->assertFieldByXpath('//input[@type="submit"]', t('Remove'), 'Node with file displays the "Remove" button.');
|
485
|
|
486
|
// "Click" the remove button (emulating either a nojs or js submission).
|
487
|
switch ($type) {
|
488
|
case 'nojs':
|
489
|
$this->drupalPost(NULL, array(), t('Remove'));
|
490
|
break;
|
491
|
case 'js':
|
492
|
$button = $this->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
|
493
|
$this->drupalPostAJAX(NULL, array(), array((string) $button[0]['name'] => (string) $button[0]['value']));
|
494
|
break;
|
495
|
}
|
496
|
|
497
|
// Ensure the page now has an upload button instead of a remove button.
|
498
|
$this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), 'After clicking the "Remove" button, it is no longer displayed.');
|
499
|
$this->assertFieldByXpath('//input[@type="submit"]', t('Upload'), 'After clicking the "Remove" button, the "Upload" button is displayed.');
|
500
|
|
501
|
// Save the node and ensure it does not have the file.
|
502
|
$this->drupalPost(NULL, array(), t('Save'));
|
503
|
$node = node_load($nid, NULL, TRUE);
|
504
|
$this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), 'File was successfully removed from the node.');
|
505
|
}
|
506
|
}
|
507
|
|
508
|
/**
|
509
|
* Tests upload and remove buttons for multiple multi-valued File fields.
|
510
|
*/
|
511
|
function testMultiValuedWidget() {
|
512
|
// Use 'page' instead of 'article', so that the 'article' image field does
|
513
|
// not conflict with this test. If in the future the 'page' type gets its
|
514
|
// own default file or image field, this test can be made more robust by
|
515
|
// using a custom node type.
|
516
|
$type_name = 'page';
|
517
|
$field_name = strtolower($this->randomName());
|
518
|
$field_name2 = strtolower($this->randomName());
|
519
|
$this->createFileField($field_name, $type_name, array('cardinality' => 3));
|
520
|
$this->createFileField($field_name2, $type_name, array('cardinality' => 3));
|
521
|
|
522
|
$field = field_info_field($field_name);
|
523
|
$instance = field_info_instance('node', $field_name, $type_name);
|
524
|
|
525
|
$field2 = field_info_field($field_name2);
|
526
|
$instance2 = field_info_instance('node', $field_name2, $type_name);
|
527
|
|
528
|
$test_file = $this->getTestFile('text');
|
529
|
|
530
|
foreach (array('nojs', 'js') as $type) {
|
531
|
// Visit the node creation form, and upload 3 files for each field. Since
|
532
|
// the field has cardinality of 3, ensure the "Upload" button is displayed
|
533
|
// until after the 3rd file, and after that, isn't displayed. Because
|
534
|
// SimpleTest triggers the last button with a given name, so upload to the
|
535
|
// second field first.
|
536
|
// @todo This is only testing a non-Ajax upload, because drupalPostAJAX()
|
537
|
// does not yet emulate jQuery's file upload.
|
538
|
//
|
539
|
$this->drupalGet("node/add/$type_name");
|
540
|
foreach (array($field_name2, $field_name) as $each_field_name) {
|
541
|
for ($delta = 0; $delta < 3; $delta++) {
|
542
|
$edit = array('files[' . $each_field_name . '_' . LANGUAGE_NONE . '_' . $delta . ']' => drupal_realpath($test_file->uri));
|
543
|
// If the Upload button doesn't exist, drupalPost() will automatically
|
544
|
// fail with an assertion message.
|
545
|
$this->drupalPost(NULL, $edit, t('Upload'));
|
546
|
}
|
547
|
}
|
548
|
$this->assertNoFieldByXpath('//input[@type="submit"]', t('Upload'), 'After uploading 3 files for each field, the "Upload" button is no longer displayed.');
|
549
|
|
550
|
$num_expected_remove_buttons = 6;
|
551
|
|
552
|
foreach (array($field_name, $field_name2) as $current_field_name) {
|
553
|
// How many uploaded files for the current field are remaining.
|
554
|
$remaining = 3;
|
555
|
// Test clicking each "Remove" button. For extra robustness, test them out
|
556
|
// of sequential order. They are 0-indexed, and get renumbered after each
|
557
|
// iteration, so array(1, 1, 0) means:
|
558
|
// - First remove the 2nd file.
|
559
|
// - Then remove what is then the 2nd file (was originally the 3rd file).
|
560
|
// - Then remove the first file.
|
561
|
foreach (array(1,1,0) as $delta) {
|
562
|
// Ensure we have the expected number of Remove buttons, and that they
|
563
|
// are numbered sequentially.
|
564
|
$buttons = $this->xpath('//input[@type="submit" and @value="Remove"]');
|
565
|
$this->assertTrue(is_array($buttons) && count($buttons) === $num_expected_remove_buttons, format_string('There are %n "Remove" buttons displayed (JSMode=%type).', array('%n' => $num_expected_remove_buttons, '%type' => $type)));
|
566
|
foreach ($buttons as $i => $button) {
|
567
|
$key = $i >= $remaining ? $i - $remaining : $i;
|
568
|
$check_field_name = $field_name2;
|
569
|
if ($current_field_name == $field_name && $i < $remaining) {
|
570
|
$check_field_name = $field_name;
|
571
|
}
|
572
|
|
573
|
$this->assertIdentical((string) $button['name'], $check_field_name . '_' . LANGUAGE_NONE . '_' . $key. '_remove_button');
|
574
|
}
|
575
|
|
576
|
// "Click" the remove button (emulating either a nojs or js submission).
|
577
|
$button_name = $current_field_name . '_' . LANGUAGE_NONE . '_' . $delta . '_remove_button';
|
578
|
switch ($type) {
|
579
|
case 'nojs':
|
580
|
// drupalPost() takes a $submit parameter that is the value of the
|
581
|
// button whose click we want to emulate. Since we have multiple
|
582
|
// buttons with the value "Remove", and want to control which one we
|
583
|
// use, we change the value of the other ones to something else.
|
584
|
// Since non-clicked buttons aren't included in the submitted POST
|
585
|
// data, and since drupalPost() will result in $this being updated
|
586
|
// with a newly rebuilt form, this doesn't cause problems.
|
587
|
foreach ($buttons as $button) {
|
588
|
if ($button['name'] != $button_name) {
|
589
|
$button['value'] = 'DUMMY';
|
590
|
}
|
591
|
}
|
592
|
$this->drupalPost(NULL, array(), t('Remove'));
|
593
|
break;
|
594
|
case 'js':
|
595
|
// drupalPostAJAX() lets us target the button precisely, so we don't
|
596
|
// require the workaround used above for nojs.
|
597
|
$this->drupalPostAJAX(NULL, array(), array($button_name => t('Remove')));
|
598
|
break;
|
599
|
}
|
600
|
$num_expected_remove_buttons--;
|
601
|
$remaining--;
|
602
|
|
603
|
// Ensure an "Upload" button for the current field is displayed with the
|
604
|
// correct name.
|
605
|
$upload_button_name = $current_field_name . '_' . LANGUAGE_NONE . '_' . $remaining . '_upload_button';
|
606
|
$buttons = $this->xpath('//input[@type="submit" and @value="Upload" and @name=:name]', array(':name' => $upload_button_name));
|
607
|
$this->assertTrue(is_array($buttons) && count($buttons) == 1, format_string('The upload button is displayed with the correct name (JSMode=%type).', array('%type' => $type)));
|
608
|
|
609
|
// Ensure only at most one button per field is displayed.
|
610
|
$buttons = $this->xpath('//input[@type="submit" and @value="Upload"]');
|
611
|
$expected = $current_field_name == $field_name ? 1 : 2;
|
612
|
$this->assertTrue(is_array($buttons) && count($buttons) == $expected, format_string('After removing a file, only one "Upload" button for each possible field is displayed (JSMode=%type).', array('%type' => $type)));
|
613
|
}
|
614
|
}
|
615
|
|
616
|
// Ensure the page now has no Remove buttons.
|
617
|
$this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), format_string('After removing all files, there is no "Remove" button displayed (JSMode=%type).', array('%type' => $type)));
|
618
|
|
619
|
// Save the node and ensure it does not have any files.
|
620
|
$this->drupalPost(NULL, array('title' => $this->randomName()), t('Save'));
|
621
|
$matches = array();
|
622
|
preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
|
623
|
$nid = $matches[1];
|
624
|
$node = node_load($nid, NULL, TRUE);
|
625
|
$this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), 'Node was successfully saved without any files.');
|
626
|
}
|
627
|
}
|
628
|
|
629
|
/**
|
630
|
* Tests a file field with a "Private files" upload destination setting.
|
631
|
*/
|
632
|
function testPrivateFileSetting() {
|
633
|
// Use 'page' instead of 'article', so that the 'article' image field does
|
634
|
// not conflict with this test. If in the future the 'page' type gets its
|
635
|
// own default file or image field, this test can be made more robust by
|
636
|
// using a custom node type.
|
637
|
$type_name = 'page';
|
638
|
$field_name = strtolower($this->randomName());
|
639
|
$this->createFileField($field_name, $type_name);
|
640
|
$field = field_info_field($field_name);
|
641
|
$instance = field_info_instance('node', $field_name, $type_name);
|
642
|
|
643
|
$test_file = $this->getTestFile('text');
|
644
|
|
645
|
// Change the field setting to make its files private, and upload a file.
|
646
|
$edit = array('field[settings][uri_scheme]' => 'private');
|
647
|
$this->drupalPost("admin/structure/types/manage/$type_name/fields/$field_name", $edit, t('Save settings'));
|
648
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
649
|
$node = node_load($nid, NULL, TRUE);
|
650
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
651
|
$this->assertFileExists($node_file, 'New file saved to disk on node creation.');
|
652
|
|
653
|
// Ensure the private file is available to the user who uploaded it.
|
654
|
$this->drupalGet(file_create_url($node_file->uri));
|
655
|
$this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
|
656
|
|
657
|
// Ensure we can't change 'uri_scheme' field settings while there are some
|
658
|
// entities with uploaded files.
|
659
|
$this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
|
660
|
$this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and @disabled="disabled"]', 'public', 'Upload destination setting disabled.');
|
661
|
|
662
|
// Delete node and confirm that setting could be changed.
|
663
|
node_delete($nid);
|
664
|
$this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
|
665
|
$this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and not(@disabled)]', 'public', 'Upload destination setting enabled.');
|
666
|
}
|
667
|
|
668
|
/**
|
669
|
* Tests that download restrictions on private files work on comments.
|
670
|
*/
|
671
|
function testPrivateFileComment() {
|
672
|
$user = $this->drupalCreateUser(array('access comments'));
|
673
|
|
674
|
// Remove access comments permission from anon user.
|
675
|
$edit = array(
|
676
|
DRUPAL_ANONYMOUS_RID . '[access comments]' => FALSE,
|
677
|
);
|
678
|
$this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));
|
679
|
|
680
|
// Create a new field.
|
681
|
$edit = array(
|
682
|
'fields[_add_new_field][label]' => $label = $this->randomName(),
|
683
|
'fields[_add_new_field][field_name]' => $name = strtolower($this->randomName()),
|
684
|
'fields[_add_new_field][type]' => 'file',
|
685
|
'fields[_add_new_field][widget_type]' => 'file_generic',
|
686
|
);
|
687
|
$this->drupalPost('admin/structure/types/manage/article/comment/fields', $edit, t('Save'));
|
688
|
$edit = array('field[settings][uri_scheme]' => 'private');
|
689
|
$this->drupalPost(NULL, $edit, t('Save field settings'));
|
690
|
$this->drupalPost(NULL, array(), t('Save settings'));
|
691
|
|
692
|
// Create node.
|
693
|
$text_file = $this->getTestFile('text');
|
694
|
$edit = array(
|
695
|
'title' => $this->randomName(),
|
696
|
);
|
697
|
$this->drupalPost('node/add/article', $edit, t('Save'));
|
698
|
$node = $this->drupalGetNodeByTitle($edit['title']);
|
699
|
|
700
|
// Add a comment with a file.
|
701
|
$text_file = $this->getTestFile('text');
|
702
|
$edit = array(
|
703
|
'files[field_' . $name . '_' . LANGUAGE_NONE . '_' . 0 . ']' => drupal_realpath($text_file->uri),
|
704
|
'comment_body[' . LANGUAGE_NONE . '][0][value]' => $comment_body = $this->randomName(),
|
705
|
);
|
706
|
$this->drupalPost(NULL, $edit, t('Save'));
|
707
|
|
708
|
// Get the comment ID.
|
709
|
preg_match('/comment-([0-9]+)/', $this->getUrl(), $matches);
|
710
|
$cid = $matches[1];
|
711
|
|
712
|
// Log in as normal user.
|
713
|
$this->drupalLogin($user);
|
714
|
|
715
|
$comment = comment_load($cid);
|
716
|
$comment_file = (object) $comment->{'field_' . $name}[LANGUAGE_NONE][0];
|
717
|
$this->assertFileExists($comment_file, 'New file saved to disk on node creation.');
|
718
|
// Test authenticated file download.
|
719
|
$url = file_create_url($comment_file->uri);
|
720
|
$this->assertNotEqual($url, NULL, 'Confirmed that the URL is valid');
|
721
|
$this->drupalGet(file_create_url($comment_file->uri));
|
722
|
$this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
|
723
|
|
724
|
// Test anonymous file download.
|
725
|
$this->drupalLogout();
|
726
|
$this->drupalGet(file_create_url($comment_file->uri));
|
727
|
$this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
|
728
|
|
729
|
// Unpublishes node.
|
730
|
$this->drupalLogin($this->admin_user);
|
731
|
$edit = array(
|
732
|
'status' => FALSE,
|
733
|
);
|
734
|
$this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
|
735
|
|
736
|
// Ensures normal user can no longer download the file.
|
737
|
$this->drupalLogin($user);
|
738
|
$this->drupalGet(file_create_url($comment_file->uri));
|
739
|
$this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
|
740
|
}
|
741
|
|
742
|
}
|
743
|
|
744
|
/**
|
745
|
* Tests file handling with node revisions.
|
746
|
*/
|
747
|
class FileFieldRevisionTestCase extends FileFieldTestCase {
|
748
|
public static function getInfo() {
|
749
|
return array(
|
750
|
'name' => 'File field revision test',
|
751
|
'description' => 'Test creating and deleting revisions with files attached.',
|
752
|
'group' => 'File',
|
753
|
);
|
754
|
}
|
755
|
|
756
|
/**
|
757
|
* Tests creating multiple revisions of a node and managing attached files.
|
758
|
*
|
759
|
* Expected behaviors:
|
760
|
* - Adding a new revision will make another entry in the field table, but
|
761
|
* the original file will not be duplicated.
|
762
|
* - Deleting a revision should not delete the original file if the file
|
763
|
* is in use by another revision.
|
764
|
* - When the last revision that uses a file is deleted, the original file
|
765
|
* should be deleted also.
|
766
|
*/
|
767
|
function testRevisions() {
|
768
|
$type_name = 'article';
|
769
|
$field_name = strtolower($this->randomName());
|
770
|
$this->createFileField($field_name, $type_name);
|
771
|
$field = field_info_field($field_name);
|
772
|
$instance = field_info_instance('node', $field_name, $type_name);
|
773
|
|
774
|
// Attach the same fields to users.
|
775
|
$this->attachFileField($field_name, 'user', 'user');
|
776
|
|
777
|
$test_file = $this->getTestFile('text');
|
778
|
|
779
|
// Create a new node with the uploaded file.
|
780
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
781
|
|
782
|
// Check that the file exists on disk and in the database.
|
783
|
$node = node_load($nid, NULL, TRUE);
|
784
|
$node_file_r1 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
785
|
$node_vid_r1 = $node->vid;
|
786
|
$this->assertFileExists($node_file_r1, 'New file saved to disk on node creation.');
|
787
|
$this->assertFileEntryExists($node_file_r1, 'File entry exists in database on node creation.');
|
788
|
$this->assertFileIsPermanent($node_file_r1, 'File is permanent.');
|
789
|
|
790
|
// Upload another file to the same node in a new revision.
|
791
|
$this->replaceNodeFile($test_file, $field_name, $nid);
|
792
|
$node = node_load($nid, NULL, TRUE);
|
793
|
$node_file_r2 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
794
|
$node_vid_r2 = $node->vid;
|
795
|
$this->assertFileExists($node_file_r2, 'Replacement file exists on disk after creating new revision.');
|
796
|
$this->assertFileEntryExists($node_file_r2, 'Replacement file entry exists in database after creating new revision.');
|
797
|
$this->assertFileIsPermanent($node_file_r2, 'Replacement file is permanent.');
|
798
|
|
799
|
// Check that the original file is still in place on the first revision.
|
800
|
$node = node_load($nid, $node_vid_r1, TRUE);
|
801
|
$this->assertEqual($node_file_r1, (object) $node->{$field_name}[LANGUAGE_NONE][0], 'Original file still in place after replacing file in new revision.');
|
802
|
$this->assertFileExists($node_file_r1, 'Original file still in place after replacing file in new revision.');
|
803
|
$this->assertFileEntryExists($node_file_r1, 'Original file entry still in place after replacing file in new revision');
|
804
|
$this->assertFileIsPermanent($node_file_r1, 'Original file is still permanent.');
|
805
|
|
806
|
// Save a new version of the node without any changes.
|
807
|
// Check that the file is still the same as the previous revision.
|
808
|
$this->drupalPost('node/' . $nid . '/edit', array('revision' => '1'), t('Save'));
|
809
|
$node = node_load($nid, NULL, TRUE);
|
810
|
$node_file_r3 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
811
|
$node_vid_r3 = $node->vid;
|
812
|
$this->assertEqual($node_file_r2, $node_file_r3, 'Previous revision file still in place after creating a new revision without a new file.');
|
813
|
$this->assertFileIsPermanent($node_file_r3, 'New revision file is permanent.');
|
814
|
|
815
|
// Revert to the first revision and check that the original file is active.
|
816
|
$this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert'));
|
817
|
$node = node_load($nid, NULL, TRUE);
|
818
|
$node_file_r4 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
819
|
$node_vid_r4 = $node->vid;
|
820
|
$this->assertEqual($node_file_r1, $node_file_r4, 'Original revision file still in place after reverting to the original revision.');
|
821
|
$this->assertFileIsPermanent($node_file_r4, 'Original revision file still permanent after reverting to the original revision.');
|
822
|
|
823
|
// Delete the second revision and check that the file is kept (since it is
|
824
|
// still being used by the third revision).
|
825
|
$this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', array(), t('Delete'));
|
826
|
$this->assertFileExists($node_file_r3, 'Second file is still available after deleting second revision, since it is being used by the third revision.');
|
827
|
$this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting second revision, since it is being used by the third revision.');
|
828
|
$this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting second revision, since it is being used by the third revision.');
|
829
|
|
830
|
// Attach the second file to a user.
|
831
|
$user = $this->drupalCreateUser();
|
832
|
$edit = (array) $user;
|
833
|
$edit[$field_name][LANGUAGE_NONE][0] = (array) $node_file_r3;
|
834
|
user_save($user, $edit);
|
835
|
$this->drupalGet('user/' . $user->uid . '/edit');
|
836
|
|
837
|
// Delete the third revision and check that the file is not deleted yet.
|
838
|
$this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', array(), t('Delete'));
|
839
|
$this->assertFileExists($node_file_r3, 'Second file is still available after deleting third revision, since it is being used by the user.');
|
840
|
$this->assertFileEntryExists($node_file_r3, 'Second file entry is still available after deleting third revision, since it is being used by the user.');
|
841
|
$this->assertFileIsPermanent($node_file_r3, 'Second file entry is still permanent after deleting third revision, since it is being used by the user.');
|
842
|
|
843
|
// Delete the user and check that the file is also deleted.
|
844
|
user_delete($user->uid);
|
845
|
// TODO: This seems like a bug in File API. Clearing the stat cache should
|
846
|
// not be necessary here. The file really is deleted, but stream wrappers
|
847
|
// doesn't seem to think so unless we clear the PHP file stat() cache.
|
848
|
clearstatcache();
|
849
|
$this->assertFileNotExists($node_file_r3, 'Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
|
850
|
$this->assertFileEntryNotExists($node_file_r3, 'Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.');
|
851
|
|
852
|
// Delete the entire node and check that the original file is deleted.
|
853
|
$this->drupalPost('node/' . $nid . '/delete', array(), t('Delete'));
|
854
|
$this->assertFileNotExists($node_file_r1, 'Original file is deleted after deleting the entire node with two revisions remaining.');
|
855
|
$this->assertFileEntryNotExists($node_file_r1, 'Original file entry is deleted after deleting the entire node with two revisions remaining.');
|
856
|
}
|
857
|
}
|
858
|
|
859
|
/**
|
860
|
* Tests that formatters are working properly.
|
861
|
*/
|
862
|
class FileFieldDisplayTestCase extends FileFieldTestCase {
|
863
|
public static function getInfo() {
|
864
|
return array(
|
865
|
'name' => 'File field display tests',
|
866
|
'description' => 'Test the display of file fields in node and views.',
|
867
|
'group' => 'File',
|
868
|
);
|
869
|
}
|
870
|
|
871
|
/**
|
872
|
* Tests normal formatter display on node display.
|
873
|
*/
|
874
|
function testNodeDisplay() {
|
875
|
$field_name = strtolower($this->randomName());
|
876
|
$type_name = 'article';
|
877
|
$field_settings = array(
|
878
|
'display_field' => '1',
|
879
|
'display_default' => '1',
|
880
|
);
|
881
|
$instance_settings = array(
|
882
|
'description_field' => '1',
|
883
|
);
|
884
|
$widget_settings = array();
|
885
|
$this->createFileField($field_name, $type_name, $field_settings, $instance_settings, $widget_settings);
|
886
|
$field = field_info_field($field_name);
|
887
|
$instance = field_info_instance('node', $field_name, $type_name);
|
888
|
|
889
|
// Create a new node *without* the file field set, and check that the field
|
890
|
// is not shown for each node display.
|
891
|
$node = $this->drupalCreateNode(array('type' => $type_name));
|
892
|
$file_formatters = array('file_default', 'file_table', 'file_url_plain', 'hidden');
|
893
|
foreach ($file_formatters as $formatter) {
|
894
|
$edit = array(
|
895
|
"fields[$field_name][type]" => $formatter,
|
896
|
);
|
897
|
$this->drupalPost("admin/structure/types/manage/$type_name/display", $edit, t('Save'));
|
898
|
$this->drupalGet('node/' . $node->nid);
|
899
|
$this->assertNoText($field_name, format_string('Field label is hidden when no file attached for formatter %formatter', array('%formatter' => $formatter)));
|
900
|
}
|
901
|
|
902
|
$test_file = $this->getTestFile('text');
|
903
|
|
904
|
// Create a new node with the uploaded file.
|
905
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
906
|
$this->drupalGet('node/' . $nid . '/edit');
|
907
|
|
908
|
// Check that the default formatter is displaying with the file name.
|
909
|
$node = node_load($nid, NULL, TRUE);
|
910
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
911
|
$default_output = theme('file_link', array('file' => $node_file));
|
912
|
$this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
|
913
|
|
914
|
// Turn the "display" option off and check that the file is no longer displayed.
|
915
|
$edit = array($field_name . '[' . LANGUAGE_NONE . '][0][display]' => FALSE);
|
916
|
$this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
|
917
|
|
918
|
$this->assertNoRaw($default_output, 'Field is hidden when "display" option is unchecked.');
|
919
|
|
920
|
}
|
921
|
}
|
922
|
|
923
|
/**
|
924
|
* Tests various validations.
|
925
|
*/
|
926
|
class FileFieldValidateTestCase extends FileFieldTestCase {
|
927
|
protected $field;
|
928
|
protected $node_type;
|
929
|
|
930
|
public static function getInfo() {
|
931
|
return array(
|
932
|
'name' => 'File field validation tests',
|
933
|
'description' => 'Tests validation functions such as file type, max file size, max size per node, and required.',
|
934
|
'group' => 'File',
|
935
|
);
|
936
|
}
|
937
|
|
938
|
/**
|
939
|
* Tests the required property on file fields.
|
940
|
*/
|
941
|
function testRequired() {
|
942
|
$type_name = 'article';
|
943
|
$field_name = strtolower($this->randomName());
|
944
|
$this->createFileField($field_name, $type_name, array(), array('required' => '1'));
|
945
|
$field = field_info_field($field_name);
|
946
|
$instance = field_info_instance('node', $field_name, $type_name);
|
947
|
|
948
|
$test_file = $this->getTestFile('text');
|
949
|
|
950
|
// Try to post a new node without uploading a file.
|
951
|
$langcode = LANGUAGE_NONE;
|
952
|
$edit = array("title" => $this->randomName());
|
953
|
$this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
|
954
|
$this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), 'Node save failed when required file field was empty.');
|
955
|
|
956
|
// Create a new node with the uploaded file.
|
957
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
958
|
$this->assertTrue($nid !== FALSE, format_string('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array('@test_file' => $test_file->uri, '@field_name' => $field_name, '@type_name' => $type_name)));
|
959
|
|
960
|
$node = node_load($nid, NULL, TRUE);
|
961
|
|
962
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
963
|
$this->assertFileExists($node_file, 'File exists after uploading to the required field.');
|
964
|
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
|
965
|
|
966
|
// Try again with a multiple value field.
|
967
|
field_delete_field($field_name);
|
968
|
$this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED), array('required' => '1'));
|
969
|
|
970
|
// Try to post a new node without uploading a file in the multivalue field.
|
971
|
$edit = array('title' => $this->randomName());
|
972
|
$this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
|
973
|
$this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), 'Node save failed when required multiple value file field was empty.');
|
974
|
|
975
|
// Create a new node with the uploaded file into the multivalue field.
|
976
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
977
|
$node = node_load($nid, NULL, TRUE);
|
978
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
979
|
$this->assertFileExists($node_file, 'File exists after uploading to the required multiple value field.');
|
980
|
$this->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multipel value field.');
|
981
|
|
982
|
// Remove our file field.
|
983
|
field_delete_field($field_name);
|
984
|
}
|
985
|
|
986
|
/**
|
987
|
* Tests the max file size validator.
|
988
|
*/
|
989
|
function testFileMaxSize() {
|
990
|
$type_name = 'article';
|
991
|
$field_name = strtolower($this->randomName());
|
992
|
$this->createFileField($field_name, $type_name, array(), array('required' => '1'));
|
993
|
$field = field_info_field($field_name);
|
994
|
$instance = field_info_instance('node', $field_name, $type_name);
|
995
|
|
996
|
$small_file = $this->getTestFile('text', 131072); // 128KB.
|
997
|
$large_file = $this->getTestFile('text', 1310720); // 1.2MB
|
998
|
|
999
|
// Test uploading both a large and small file with different increments.
|
1000
|
$sizes = array(
|
1001
|
'1M' => 1048576,
|
1002
|
'1024K' => 1048576,
|
1003
|
'1048576' => 1048576,
|
1004
|
);
|
1005
|
|
1006
|
foreach ($sizes as $max_filesize => $file_limit) {
|
1007
|
// Set the max file upload size.
|
1008
|
$this->updateFileField($field_name, $type_name, array('max_filesize' => $max_filesize));
|
1009
|
$instance = field_info_instance('node', $field_name, $type_name);
|
1010
|
|
1011
|
// Create a new node with the small file, which should pass.
|
1012
|
$nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
|
1013
|
$node = node_load($nid, NULL, TRUE);
|
1014
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1015
|
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
|
1016
|
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
|
1017
|
|
1018
|
// Check that uploading the large file fails (1M limit).
|
1019
|
$nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
|
1020
|
$error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', array('%filesize' => format_size($large_file->filesize), '%maxsize' => format_size($file_limit)));
|
1021
|
$this->assertRaw($error_message, format_string('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', array('%filesize' => format_size($large_file->filesize), '%maxsize' => $max_filesize)));
|
1022
|
}
|
1023
|
|
1024
|
// Turn off the max filesize.
|
1025
|
$this->updateFileField($field_name, $type_name, array('max_filesize' => ''));
|
1026
|
|
1027
|
// Upload the big file successfully.
|
1028
|
$nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
|
1029
|
$node = node_load($nid, NULL, TRUE);
|
1030
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1031
|
$this->assertFileExists($node_file, format_string('File exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
|
1032
|
$this->assertFileEntryExists($node_file, format_string('File entry exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
|
1033
|
|
1034
|
// Remove our file field.
|
1035
|
field_delete_field($field_name);
|
1036
|
}
|
1037
|
|
1038
|
/**
|
1039
|
* Tests file extension checking.
|
1040
|
*/
|
1041
|
function testFileExtension() {
|
1042
|
$type_name = 'article';
|
1043
|
$field_name = strtolower($this->randomName());
|
1044
|
$this->createFileField($field_name, $type_name);
|
1045
|
$field = field_info_field($field_name);
|
1046
|
$instance = field_info_instance('node', $field_name, $type_name);
|
1047
|
|
1048
|
$test_file = $this->getTestFile('image');
|
1049
|
list(, $test_file_extension) = explode('.', $test_file->filename);
|
1050
|
|
1051
|
// Disable extension checking.
|
1052
|
$this->updateFileField($field_name, $type_name, array('file_extensions' => ''));
|
1053
|
|
1054
|
// Check that the file can be uploaded with no extension checking.
|
1055
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
1056
|
$node = node_load($nid, NULL, TRUE);
|
1057
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1058
|
$this->assertFileExists($node_file, 'File exists after uploading a file with no extension checking.');
|
1059
|
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
|
1060
|
|
1061
|
// Enable extension checking for text files.
|
1062
|
$this->updateFileField($field_name, $type_name, array('file_extensions' => 'txt'));
|
1063
|
|
1064
|
// Check that the file with the wrong extension cannot be uploaded.
|
1065
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
1066
|
$error_message = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => 'txt'));
|
1067
|
$this->assertRaw($error_message, 'Node save failed when file uploaded with the wrong extension.');
|
1068
|
|
1069
|
// Enable extension checking for text and image files.
|
1070
|
$this->updateFileField($field_name, $type_name, array('file_extensions' => "txt $test_file_extension"));
|
1071
|
|
1072
|
// Check that the file can be uploaded with extension checking.
|
1073
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
1074
|
$node = node_load($nid, NULL, TRUE);
|
1075
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1076
|
$this->assertFileExists($node_file, 'File exists after uploading a file with extension checking.');
|
1077
|
$this->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
|
1078
|
|
1079
|
// Remove our file field.
|
1080
|
field_delete_field($field_name);
|
1081
|
}
|
1082
|
}
|
1083
|
|
1084
|
/**
|
1085
|
* Tests that files are uploaded to proper locations.
|
1086
|
*/
|
1087
|
class FileFieldPathTestCase extends FileFieldTestCase {
|
1088
|
public static function getInfo() {
|
1089
|
return array(
|
1090
|
'name' => 'File field file path tests',
|
1091
|
'description' => 'Test that files are uploaded to the proper location with token support.',
|
1092
|
'group' => 'File',
|
1093
|
);
|
1094
|
}
|
1095
|
|
1096
|
/**
|
1097
|
* Tests the normal formatter display on node display.
|
1098
|
*/
|
1099
|
function testUploadPath() {
|
1100
|
$field_name = strtolower($this->randomName());
|
1101
|
$type_name = 'article';
|
1102
|
$field = $this->createFileField($field_name, $type_name);
|
1103
|
$test_file = $this->getTestFile('text');
|
1104
|
|
1105
|
// Create a new node.
|
1106
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
1107
|
|
1108
|
// Check that the file was uploaded to the file root.
|
1109
|
$node = node_load($nid, NULL, TRUE);
|
1110
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1111
|
$this->assertPathMatch('public://' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
|
1112
|
|
1113
|
// Change the path to contain multiple subdirectories.
|
1114
|
$field = $this->updateFileField($field_name, $type_name, array('file_directory' => 'foo/bar/baz'));
|
1115
|
|
1116
|
// Upload a new file into the subdirectories.
|
1117
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
1118
|
|
1119
|
// Check that the file was uploaded into the subdirectory.
|
1120
|
$node = node_load($nid, NULL, TRUE);
|
1121
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1122
|
$this->assertPathMatch('public://foo/bar/baz/' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
|
1123
|
|
1124
|
// Check the path when used with tokens.
|
1125
|
// Change the path to contain multiple token directories.
|
1126
|
$field = $this->updateFileField($field_name, $type_name, array('file_directory' => '[current-user:uid]/[current-user:name]'));
|
1127
|
|
1128
|
// Upload a new file into the token subdirectories.
|
1129
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
1130
|
|
1131
|
// Check that the file was uploaded into the subdirectory.
|
1132
|
$node = node_load($nid, NULL, TRUE);
|
1133
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1134
|
// Do token replacement using the same user which uploaded the file, not
|
1135
|
// the user running the test case.
|
1136
|
$data = array('user' => $this->admin_user);
|
1137
|
$subdirectory = token_replace('[user:uid]/[user:name]', $data);
|
1138
|
$this->assertPathMatch('public://' . $subdirectory . '/' . $test_file->filename, $node_file->uri, format_string('The file %file was uploaded to the correct path with token replacements.', array('%file' => $node_file->uri)));
|
1139
|
}
|
1140
|
|
1141
|
/**
|
1142
|
* Asserts that a file is uploaded to the right location.
|
1143
|
*
|
1144
|
* @param $expected_path
|
1145
|
* The location where the file is expected to be uploaded. Duplicate file
|
1146
|
* names to not need to be taken into account.
|
1147
|
* @param $actual_path
|
1148
|
* Where the file was actually uploaded.
|
1149
|
* @param $message
|
1150
|
* The message to display with this assertion.
|
1151
|
*/
|
1152
|
function assertPathMatch($expected_path, $actual_path, $message) {
|
1153
|
// Strip off the extension of the expected path to allow for _0, _1, etc.
|
1154
|
// suffixes when the file hits a duplicate name.
|
1155
|
$pos = strrpos($expected_path, '.');
|
1156
|
$base_path = substr($expected_path, 0, $pos);
|
1157
|
$extension = substr($expected_path, $pos + 1);
|
1158
|
|
1159
|
$result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path);
|
1160
|
$this->assertTrue($result, $message);
|
1161
|
}
|
1162
|
}
|
1163
|
|
1164
|
/**
|
1165
|
* Tests the file token replacement in strings.
|
1166
|
*/
|
1167
|
class FileTokenReplaceTestCase extends FileFieldTestCase {
|
1168
|
public static function getInfo() {
|
1169
|
return array(
|
1170
|
'name' => 'File token replacement',
|
1171
|
'description' => 'Generates text using placeholders for dummy content to check file token replacement.',
|
1172
|
'group' => 'File',
|
1173
|
);
|
1174
|
}
|
1175
|
|
1176
|
/**
|
1177
|
* Creates a file, then tests the tokens generated from it.
|
1178
|
*/
|
1179
|
function testFileTokenReplacement() {
|
1180
|
global $language;
|
1181
|
$url_options = array(
|
1182
|
'absolute' => TRUE,
|
1183
|
'language' => $language,
|
1184
|
);
|
1185
|
|
1186
|
// Create file field.
|
1187
|
$type_name = 'article';
|
1188
|
$field_name = 'field_' . strtolower($this->randomName());
|
1189
|
$this->createFileField($field_name, $type_name);
|
1190
|
$field = field_info_field($field_name);
|
1191
|
$instance = field_info_instance('node', $field_name, $type_name);
|
1192
|
|
1193
|
$test_file = $this->getTestFile('text');
|
1194
|
// Coping a file to test uploads with non-latin filenames.
|
1195
|
$filename = drupal_dirname($test_file->uri) . '/текстовый файл.txt';
|
1196
|
$test_file = file_copy($test_file, $filename);
|
1197
|
|
1198
|
// Create a new node with the uploaded file.
|
1199
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
|
1200
|
|
1201
|
// Load the node and the file.
|
1202
|
$node = node_load($nid, NULL, TRUE);
|
1203
|
$file = file_load($node->{$field_name}[LANGUAGE_NONE][0]['fid']);
|
1204
|
|
1205
|
// Generate and test sanitized tokens.
|
1206
|
$tests = array();
|
1207
|
$tests['[file:fid]'] = $file->fid;
|
1208
|
$tests['[file:name]'] = check_plain($file->filename);
|
1209
|
$tests['[file:path]'] = check_plain($file->uri);
|
1210
|
$tests['[file:mime]'] = check_plain($file->filemime);
|
1211
|
$tests['[file:size]'] = format_size($file->filesize);
|
1212
|
$tests['[file:url]'] = check_plain(file_create_url($file->uri));
|
1213
|
$tests['[file:timestamp]'] = format_date($file->timestamp, 'medium', '', NULL, $language->language);
|
1214
|
$tests['[file:timestamp:short]'] = format_date($file->timestamp, 'short', '', NULL, $language->language);
|
1215
|
$tests['[file:owner]'] = check_plain(format_username($this->admin_user));
|
1216
|
$tests['[file:owner:uid]'] = $file->uid;
|
1217
|
|
1218
|
// Test to make sure that we generated something for each token.
|
1219
|
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
|
1220
|
|
1221
|
foreach ($tests as $input => $expected) {
|
1222
|
$output = token_replace($input, array('file' => $file), array('language' => $language));
|
1223
|
$this->assertEqual($output, $expected, format_string('Sanitized file token %token replaced.', array('%token' => $input)));
|
1224
|
}
|
1225
|
|
1226
|
// Generate and test unsanitized tokens.
|
1227
|
$tests['[file:name]'] = $file->filename;
|
1228
|
$tests['[file:path]'] = $file->uri;
|
1229
|
$tests['[file:mime]'] = $file->filemime;
|
1230
|
$tests['[file:size]'] = format_size($file->filesize);
|
1231
|
|
1232
|
foreach ($tests as $input => $expected) {
|
1233
|
$output = token_replace($input, array('file' => $file), array('language' => $language, 'sanitize' => FALSE));
|
1234
|
$this->assertEqual($output, $expected, format_string('Unsanitized file token %token replaced.', array('%token' => $input)));
|
1235
|
}
|
1236
|
}
|
1237
|
}
|
1238
|
|
1239
|
/**
|
1240
|
* Tests file access on private nodes.
|
1241
|
*/
|
1242
|
class FilePrivateTestCase extends FileFieldTestCase {
|
1243
|
public static function getInfo() {
|
1244
|
return array(
|
1245
|
'name' => 'Private file test',
|
1246
|
'description' => 'Uploads a test to a private node and checks access.',
|
1247
|
'group' => 'File',
|
1248
|
);
|
1249
|
}
|
1250
|
|
1251
|
function setUp() {
|
1252
|
parent::setUp(array('node_access_test', 'field_test'));
|
1253
|
node_access_rebuild();
|
1254
|
variable_set('node_access_test_private', TRUE);
|
1255
|
}
|
1256
|
|
1257
|
/**
|
1258
|
* Tests file access for file uploaded to a private node.
|
1259
|
*/
|
1260
|
function testPrivateFile() {
|
1261
|
// Use 'page' instead of 'article', so that the 'article' image field does
|
1262
|
// not conflict with this test. If in the future the 'page' type gets its
|
1263
|
// own default file or image field, this test can be made more robust by
|
1264
|
// using a custom node type.
|
1265
|
$type_name = 'page';
|
1266
|
$field_name = strtolower($this->randomName());
|
1267
|
$this->createFileField($field_name, $type_name, array('uri_scheme' => 'private'));
|
1268
|
|
1269
|
// Create a field with no view access - see field_test_field_access().
|
1270
|
$no_access_field_name = 'field_no_view_access';
|
1271
|
$this->createFileField($no_access_field_name, $type_name, array('uri_scheme' => 'private'));
|
1272
|
|
1273
|
$test_file = $this->getTestFile('text');
|
1274
|
$nid = $this->uploadNodeFile($test_file, $field_name, $type_name, TRUE, array('private' => TRUE));
|
1275
|
$node = node_load($nid, NULL, TRUE);
|
1276
|
$node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
|
1277
|
// Ensure the file can be downloaded.
|
1278
|
$this->drupalGet(file_create_url($node_file->uri));
|
1279
|
$this->assertResponse(200, 'Confirmed that the generated URL is correct by downloading the shipped file.');
|
1280
|
$this->drupalLogOut();
|
1281
|
$this->drupalGet(file_create_url($node_file->uri));
|
1282
|
$this->assertResponse(403, 'Confirmed that access is denied for the file without the needed permission.');
|
1283
|
|
1284
|
// Test with the field that should deny access through field access.
|
1285
|
$this->drupalLogin($this->admin_user);
|
1286
|
$nid = $this->uploadNodeFile($test_file, $no_access_field_name, $type_name, TRUE, array('private' => TRUE));
|
1287
|
$node = node_load($nid, NULL, TRUE);
|
1288
|
$node_file = (object) $node->{$no_access_field_name}[LANGUAGE_NONE][0];
|
1289
|
// Ensure the file cannot be downloaded.
|
1290
|
$this->drupalGet(file_create_url($node_file->uri));
|
1291
|
$this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission.');
|
1292
|
|
1293
|
// Attempt to reuse the existing file when creating a new node, and confirm
|
1294
|
// that access is still denied.
|
1295
|
$edit = array();
|
1296
|
$edit['title'] = $this->randomName(8);
|
1297
|
$edit[$field_name . '[' . LANGUAGE_NONE . '][0][fid]'] = $node_file->fid;
|
1298
|
$this->drupalPost('node/add/page', $edit, t('Save'));
|
1299
|
$new_node = $this->drupalGetNodeByTitle($edit['title']);
|
1300
|
$this->assertTrue(!empty($new_node), 'Node was created.');
|
1301
|
$this->assertUrl('node/' . $new_node->nid);
|
1302
|
$this->assertNoRaw($node_file->filename, 'File without view field access permission does not appear after attempting to attach it to a new node.');
|
1303
|
$this->drupalGet(file_create_url($node_file->uri));
|
1304
|
$this->assertResponse(403, 'Confirmed that access is denied for the file without view field access permission after attempting to attach it to a new node.');
|
1305
|
}
|
1306
|
}
|