file.test 47.3 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Tests for file.module.
6
7
8
9
10
11
12
13
14
 */

/**
 * This class provides methods specifically for testing File's field handling.
 */
class FileFieldTestCase extends DrupalWebTestCase {
  protected $admin_user;

  function setUp() {
15
    parent::setUp('file', 'file_module_test');
16
    $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'));
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Get a sample file of the specified type.
   */
  function getTestFile($type_name, $size = NULL) {
    // Get a file to upload.
    $file = current($this->drupalGetTestFiles($type_name, $size));

    // Add a filesize property to files as would be read by file_load().
    $file->filesize = filesize($file->uri);

    return $file;
  }

33
34
35
36
37
38
39
  /**
   * Get the fid of the last inserted file.
   */
  function getLastFileId() {
    return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
  }

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  /**
   * Create a new file field.
   *
   * @param $name
   *   The name of the new field (all lowercase), exclude the "field_" prefix.
   * @param $type_name
   *   The node type that this field will be added to.
   * @param $field_settings
   *   A list of field settings that will be added to the defaults.
   * @param $instance_settings
   *   A list of instance settings that will be added to the instance defaults.
   * @param $widget_settings
   *   A list of widget settings that will be added to the widget defaults.
   */
  function createFileField($name, $type_name, $field_settings = array(), $instance_settings = array(), $widget_settings = array()) {
    $field = array(
      'field_name' => $name,
      'type' => 'file',
      'settings' => array(),
      'cardinality' => !empty($field_settings['cardinality']) ? $field_settings['cardinality'] : 1,
    );
    $field['settings'] = array_merge($field['settings'], $field_settings);
    field_create_field($field);

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    $this->attachFileField($name, 'node', $type_name, $instance_settings, $widget_settings);
  }

  /**
   * Attach a file field to an entity.
   *
   * @param $name
   *   The name of the new field (all lowercase), exclude the "field_" prefix.
   * @param $entity_type
   *   The entity type this field will be added to.
   * @param $bundle
   *   The bundle this field will be added to.
   * @param $field_settings
   *   A list of field settings that will be added to the defaults.
   * @param $instance_settings
   *   A list of instance settings that will be added to the instance defaults.
   * @param $widget_settings
   *   A list of widget settings that will be added to the widget defaults.
   */
  function attachFileField($name, $entity_type, $bundle, $instance_settings = array(), $widget_settings = array()) {
84
    $instance = array(
85
      'field_name' => $name,
86
      'label' => $name,
87
88
      'entity_type' => $entity_type,
      'bundle' => $bundle,
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      'required' => !empty($instance_settings['required']),
      'settings' => array(),
      'widget' => array(
        'type' => 'file_generic',
        'settings' => array(),
      ),
    );
    $instance['settings'] = array_merge($instance['settings'], $instance_settings);
    $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);
    field_create_instance($instance);
  }

  /**
   * Update an existing file field with new settings.
   */
  function updateFileField($name, $type_name, $instance_settings = array(), $widget_settings = array()) {
105
    $instance = field_info_instance('node', $name, $type_name);
106
107
108
109
110
111
112
113
114
115
    $instance['settings'] = array_merge($instance['settings'], $instance_settings);
    $instance['widget']['settings'] = array_merge($instance['widget']['settings'], $widget_settings);

    field_update_instance($instance);
  }

  /**
   * Upload a file to a node.
   */
  function uploadNodeFile($file, $field_name, $nid_or_type, $new_revision = TRUE) {
116
    $langcode = LANGUAGE_NONE;
117
    $edit = array(
118
      "title" => $this->randomName(),
119
120
121
122
      'revision' => (string) (int) $new_revision,
    );

    if (is_numeric($nid_or_type)) {
123
      $nid = $nid_or_type;
124
125
    }
    else {
126
127
128
129
130
131
132
      // Add a new node.
      $node = $this->drupalCreateNode(array('type' => $nid_or_type));
      $nid = $node->nid;
      // Save at least one revision to better simulate a real site.
      $this->drupalCreateNode(get_object_vars($node));
      $node = node_load($nid, NULL, TRUE);
      $this->assertNotEqual($nid, $node->vid, t('Node revision exists.'));
133
134
    }

135
136
137
138
139
    // Attach a file to the node.
    $edit['files[' . $field_name . '_' . $langcode . '_0]'] = drupal_realpath($file->uri);
    $this->drupalPost("node/$nid/edit", $edit, t('Save'));

    return $nid;
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  }

  /**
   * Remove a file from a node.
   *
   * Note that if replacing a file, it must first be removed then added again.
   */
  function removeNodeFile($nid, $new_revision = TRUE) {
    $edit = array(
      'revision' => (string) (int) $new_revision,
    );

    $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
    $this->drupalPost(NULL, $edit, t('Save'));
  }

  /**
   * Replace a file within a node.
   */
  function replaceNodeFile($file, $field_name, $nid, $new_revision = TRUE) {
    $edit = array(
161
      'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($file->uri),
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
      'revision' => (string) (int) $new_revision,
    );

    $this->drupalPost('node/' . $nid . '/edit', array(), t('Remove'));
    $this->drupalPost(NULL, $edit, t('Save'));
  }

  /**
   * Assert that a file exists physically on disk.
   */
  function assertFileExists($file, $message = NULL) {
    $message = isset($message) ? $message : t('File %file exists on the disk.', array('%file' => $file->uri));
    $this->assertTrue(is_file($file->uri), $message);
  }

  /**
   * Assert that a file exists in the database.
   */
  function assertFileEntryExists($file, $message = NULL) {
181
    entity_get_controller('file')->resetCache();
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
    $db_file = file_load($file->fid);
    $message = isset($message) ? $message : t('File %file exists in database at the correct path.', array('%file' => $file->uri));
    $this->assertEqual($db_file->uri, $file->uri, $message);
  }

  /**
   * Assert that a file does not exist on disk.
   */
  function assertFileNotExists($file, $message = NULL) {
    $message = isset($message) ? $message : t('File %file exists on the disk.', array('%file' => $file->uri));
    $this->assertFalse(is_file($file->uri), $message);
  }

  /**
   * Assert that a file does not exist in the database.
   */
  function assertFileEntryNotExists($file, $message) {
199
    entity_get_controller('file')->resetCache();
200
201
202
    $message = isset($message) ? $message : t('File %file exists in database at the correct path.', array('%file' => $file->uri));
    $this->assertFalse(file_load($file->fid), $message);
  }
203
204
205
206
207
208
209
210

  /**
   * Assert that a file's status is set to permanent in the database.
   */
  function assertFileIsPermanent($file, $message = NULL) {
    $message = isset($message) ? $message : t('File %file is permanent.', array('%file' => $file->uri));
    $this->assertTrue($file->status == FILE_STATUS_PERMANENT, $message);
  }
211
212
}

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
 * Test class for testing the 'managed_file' element type on its own, not as part of a file field.
 *
 * @todo Create a FileTestCase base class and move FileFieldTestCase methods
 *   that aren't related to fields into it.
 */
class FileManagedFileElementTestCase extends FileFieldTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Managed file element test',
      'description' => 'Tests the managed_file element type.',
      'group' => 'File',
    );
  }

  /**
   * Tests the managed_file element type.
   */
  function testManagedFile() {
    // Perform the tests with all permutations of $form['#tree'] and
    // $element['#extended'].
    foreach (array(0, 1) as $tree) {
      foreach (array(0, 1) as $extended) {
        $test_file = $this->getTestFile('text');
        $path = 'file/test/' . $tree . '/' . $extended;
        $input_base_name = $tree ? 'nested_file' : 'file';

        // Submit without a file.
        $this->drupalPost($path, array(), t('Save'));
        $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submitted without a file.'));

        // Submit a new file, without using the Upload button.
        $last_fid_prior = $this->getLastFileId();
        $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
        $this->drupalPost($path, $edit, t('Save'));
        $last_fid = $this->getLastFileId();
        $this->assertTrue($last_fid > $last_fid_prior, t('New file got saved.'));
        $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Submit handler has correct file info.'));

        // Submit no new input, but with a default file.
        $this->drupalPost($path . '/' . $last_fid, array(), t('Save'));
        $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Empty submission did not change an existing file.'));

256
        // Now, test the Upload and Remove buttons, with and without Ajax.
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
        foreach (array(FALSE, TRUE) as $ajax) {
          // Upload, then Submit.
          $last_fid_prior = $this->getLastFileId();
          $this->drupalGet($path);
          $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
          if ($ajax) {
            $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
          }
          else {
            $this->drupalPost(NULL, $edit, t('Upload'));
          }
          $last_fid = $this->getLastFileId();
          $this->assertTrue($last_fid > $last_fid_prior, t('New file got uploaded.'));
          $this->drupalPost(NULL, array(), t('Save'));
          $this->assertRaw(t('The file id is %fid.', array('%fid' => $last_fid)), t('Submit handler has correct file info.'));

          // Remove, then Submit.
          $this->drupalGet($path . '/' . $last_fid);
          if ($ajax) {
            $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
          }
          else {
            $this->drupalPost(NULL, array(), t('Remove'));
          }
          $this->drupalPost(NULL, array(), t('Save'));
          $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submission after file removal was successful.'));

          // Upload, then Remove, then Submit.
          $this->drupalGet($path);
          $edit = array('files[' . $input_base_name . ']' => drupal_realpath($test_file->uri));
          if ($ajax) {
            $this->drupalPostAJAX(NULL, $edit, $input_base_name . '_upload_button');
            $this->drupalPostAJAX(NULL, array(), $input_base_name . '_remove_button');
          }
          else {
            $this->drupalPost(NULL, $edit, t('Upload'));
            $this->drupalPost(NULL, array(), t('Remove'));
          }
          $this->drupalPost(NULL, array(), t('Save'));
          $this->assertRaw(t('The file id is %fid.', array('%fid' => 0)), t('Submission after file upload and removal was successful.'));
        }
      }
    }
  }
}
302
303

/**
304
 * Test class to test file field widget, single and multi-valued, with and without Ajax, with public and private files.
305
306
307
308
309
 */
class FileFieldWidgetTestCase extends FileFieldTestCase {
  public static function getInfo() {
    return array(
      'name' => 'File field widget test',
310
      'description' => 'Tests the file field widget, single and multi-valued, with and without AJAX, with public and private files.',
311
312
313
314
315
      'group' => 'File',
    );
  }

  /**
316
   * Tests upload and remove buttons, with and without Ajax, for a single-valued File field.
317
   */
318
  function testSingleValuedWidget() {
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
    // Use 'page' instead of 'article', so that the 'article' image field does
    // not conflict with this test. If in the future the 'page' type gets its
    // own default file or image field, this test can be made more robust by
    // using a custom node type.
    $type_name = 'page';
    $field_name = strtolower($this->randomName());
    $this->createFileField($field_name, $type_name);
    $field = field_info_field($field_name);
    $instance = field_info_instance('node', $field_name, $type_name);

    $test_file = $this->getTestFile('text');

    foreach (array('nojs', 'js') as $type) {
      // Create a new node with the uploaded file and ensure it got uploaded
      // successfully.
334
335
      // @todo This only tests a 'nojs' submission, because drupalPostAJAX()
      //   does not yet support file uploads.
336
337
338
      $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
      $node = node_load($nid, NULL, TRUE);
      $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
339
      $this->assertFileExists($node_file, t('New file saved to disk on node creation.'));
340
341

      // Ensure the file can be downloaded.
342
      $this->drupalGet(file_create_url($node_file->uri));
343
      $this->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the shipped file.'));
344
345
346

      // Ensure the edit page has a remove button instead of an upload button.
      $this->drupalGet("node/$nid/edit");
347
348
      $this->assertNoFieldByXPath('//input[@type="submit"]', t('Upload'), t('Node with file does not display the "Upload" button.'));
      $this->assertFieldByXpath('//input[@type="submit"]', t('Remove'), t('Node with file displays the "Remove" button.'));
349
350
351
352
353
354
355
356

      // "Click" the remove button (emulating either a nojs or js submission).
      switch ($type) {
        case 'nojs':
          $this->drupalPost(NULL, array(), t('Remove'));
          break;
        case 'js':
          $button = $this->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
357
          $this->drupalPostAJAX(NULL, array(), array((string) $button[0]['name'] => (string) $button[0]['value']));
358
359
360
361
          break;
      }

      // Ensure the page now has an upload button instead of a remove button.
362
363
      $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), t('After clicking the "Remove" button, it is no longer displayed.'));
      $this->assertFieldByXpath('//input[@type="submit"]', t('Upload'), t('After clicking the "Remove" button, the "Upload" button is displayed.'));
364
365
366
367

      // Save the node and ensure it does not have the file.
      $this->drupalPost(NULL, array(), t('Save'));
      $node = node_load($nid, NULL, TRUE);
368
      $this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), t('File was successfully removed from the node.'));
369
    }
370
  }
371

372
  /**
373
   * Tests upload and remove buttons, with and without Ajax, for a multi-valued File field.
374
375
376
377
378
379
380
381
   */
  function testMultiValuedWidget() {
    // Use 'page' instead of 'article', so that the 'article' image field does
    // not conflict with this test. If in the future the 'page' type gets its
    // own default file or image field, this test can be made more robust by
    // using a custom node type.
    $type_name = 'page';
    $field_name = strtolower($this->randomName());
382
    $this->createFileField($field_name, $type_name, array('cardinality' => 3));
383
384
385
386
387
388
389
390
391
    $field = field_info_field($field_name);
    $instance = field_info_instance('node', $field_name, $type_name);

    $test_file = $this->getTestFile('text');

    foreach (array('nojs', 'js') as $type) {
      // Visit the node creation form, and upload 3 files. Since the field has
      // cardinality of 3, ensure the "Upload" button is displayed until after
      // the 3rd file, and after that, isn't displayed.
392
      // @todo This is only testing a non-Ajax upload, because drupalPostAJAX()
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
      //   does not yet emulate jQuery's file upload.
      $this->drupalGet("node/add/$type_name");
      for ($delta = 0; $delta < 3; $delta++) {
        $edit = array('files[' . $field_name . '_' . LANGUAGE_NONE . '_' . $delta . ']' => drupal_realpath($test_file->uri));
        // If the Upload button doesn't exist, drupalPost() will automatically
        // fail with an assertion message.
        $this->drupalPost(NULL, $edit, t('Upload'));
      }
      $this->assertNoFieldByXpath('//input[@type="submit"]', t('Upload'), t('After uploading 3 files, the "Upload" button is no longer displayed.'));

      // Test clicking each "Remove" button. For extra robustness, test them out
      // of sequential order. They are 0-indexed, and get renumbered after each
      // iteration, so array(1, 1, 0) means:
      // - First remove the 2nd file.
      // - Then remove what is then the 2nd file (was originally the 3rd file).
      // - Then remove the first file.
      $num_expected_remove_buttons = 3;
      foreach (array(1, 1, 0) as $delta) {
        // Ensure we have the expected number of Remove buttons, and that they
        // are numbered sequentially.
        $buttons = $this->xpath('//input[@type="submit" and @value="Remove"]');
        $this->assertTrue(is_array($buttons) && count($buttons) === $num_expected_remove_buttons, t('There are %n "Remove" buttons displayed (JSMode=%type).', array('%n' => $num_expected_remove_buttons, '%type' => $type)));
        foreach ($buttons as $i => $button) {
          $this->assertIdentical((string) $button['name'], $field_name . '_' . LANGUAGE_NONE . '_' . $i . '_remove_button');
        }

        // "Click" the remove button (emulating either a nojs or js submission).
        $button_name = $field_name . '_' . LANGUAGE_NONE . '_' . $delta . '_remove_button';
        switch ($type) {
          case 'nojs':
            // drupalPost() takes a $submit parameter that is the value of the
            // button whose click we want to emulate. Since we have multiple
            // buttons with the value "Remove", and want to control which one we
            // use, we change the value of the other ones to something else.
            // Since non-clicked buttons aren't included in the submitted POST
            // data, and since drupalPost() will result in $this being updated
            // with a newly rebuilt form, this doesn't cause problems.
            foreach ($buttons as $button) {
              if ($button['name'] != $button_name) {
                $button['value'] = 'DUMMY';
              }
            }
            $this->drupalPost(NULL, array(), t('Remove'));
            break;
          case 'js':
            // drupalPostAJAX() lets us target the button precisely, so we don't
            // require the workaround used above for nojs.
            $this->drupalPostAJAX(NULL, array(), array($button_name => t('Remove')));
            break;
        }
        $num_expected_remove_buttons--;

        // Ensure we have a single Upload button, and that it is numbered
        // sequentially after the Remove buttons.
        $buttons = $this->xpath('//input[@type="submit" and @value="Upload"]');
        $this->assertTrue(is_array($buttons) && count($buttons) == 1 && ((string) $buttons[0]['name'] === ($field_name . '_' . LANGUAGE_NONE . '_' . $num_expected_remove_buttons . '_upload_button')), t('After removing a file, an "Upload" button is displayed (JSMode=%type).'));
      }

      // Ensure the page now has no Remove buttons.
      $this->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), t('After removing all files, there is no "Remove" button displayed.', array('%n' => $num_expected_remove_buttons, '%type' => $type)));
453

454
455
456
457
458
459
460
      // Save the node and ensure it does not have any files.
      $this->drupalPost(NULL, array('title' => $this->randomName()), t('Save'));
      $matches = array();
      preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
      $nid = $matches[1];
      $node = node_load($nid, NULL, TRUE);
      $this->assertTrue(empty($node->{$field_name}[LANGUAGE_NONE][0]['fid']), t('Node was successfully saved without any files.'));
461
    }
462
  }
463

464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
  /**
   * Tests a file field with a "Private files" upload destination setting.
   */
  function testPrivateFileSetting() {
    // Use 'page' instead of 'article', so that the 'article' image field does
    // not conflict with this test. If in the future the 'page' type gets its
    // own default file or image field, this test can be made more robust by
    // using a custom node type.
    $type_name = 'page';
    $field_name = strtolower($this->randomName());
    $this->createFileField($field_name, $type_name);
    $field = field_info_field($field_name);
    $instance = field_info_instance('node', $field_name, $type_name);

    $test_file = $this->getTestFile('text');

    // Change the field setting to make its files private, and upload a file.
481
482
483
484
485
    $edit = array('field[settings][uri_scheme]' => 'private');
    $this->drupalPost("admin/structure/types/manage/$type_name/fields/$field_name", $edit, t('Save settings'));
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $node = node_load($nid, NULL, TRUE);
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
486
    $this->assertFileExists($node_file, t('New file saved to disk on node creation.'));
487
488

    // Ensure the private file is available to the user who uploaded it.
489
    $this->drupalGet(file_create_url($node_file->uri));
490
    $this->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the shipped file.'));
491

492
493
494
    // Ensure we can't change 'uri_scheme' field settings while there are some
    // entities with uploaded files.
    $this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
495
    $this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and @disabled="disabled"]', 'public', t('Upload destination setting disabled.'));
496

497
498
499
    // Delete node and confirm that setting could be changed.
    node_delete($nid);
    $this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_name");
500
    $this->assertFieldByXpath('//input[@id="edit-field-settings-uri-scheme-public" and not(@disabled)]', 'public', t('Upload destination setting enabled.'));
501
  }
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

  /**
   * Tests that download restrictions on private files work on comments.
   */
  function testPrivateFileComment() {
    $user = $this->drupalCreateUser(array('access comments'));

    // Remove access comments permission from anon user.
    $edit = array(
      '1[access comments]' => FALSE,
    );
    $this->drupalPost('admin/people/permissions', $edit, t('Save permissions'));

    // Create a new field.
    $edit = array(
517
518
519
520
      'fields[_add_new_field][label]' => $label = $this->randomName(),
      'fields[_add_new_field][field_name]' => $name = strtolower($this->randomName()),
      'fields[_add_new_field][type]' => 'file',
      'fields[_add_new_field][widget_type]' => 'file_generic',
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
    );
    $this->drupalPost('admin/structure/types/manage/article/comment/fields', $edit, t('Save'));
    $edit = array('field[settings][uri_scheme]' => 'private');
    $this->drupalPost(NULL, $edit, t('Save field settings'));
    $this->drupalPost(NULL, array(), t('Save settings'));

    // Create node.
    $text_file = $this->getTestFile('text');
    $edit = array(
      'title' => $this->randomName(),
    );
    $this->drupalPost('node/add/article', $edit, t('Save'));

    // Add a comment with a file.
    $text_file = $this->getTestFile('text');
    $edit = array(
537
      'files[field_' . $name . '_' . LANGUAGE_NONE . '_' . 0 . ']' => drupal_realpath($text_file->uri),
538
539
540
541
542
543
544
545
546
547
      'comment_body[' . LANGUAGE_NONE . '][0][value]' => $comment_body = $this->randomName(),
    );
    $this->drupalPost(NULL, $edit, t('Save'));

    // Get the comment ID.
    preg_match('/comment-([0-9]+)/', $this->getUrl(), $matches);
    $cid = $matches[1];

    // Log in as normal user.
    $this->drupalLogin($user);
548

549
550
551
552
553
554
555
556
    $comment = comment_load($cid);
    $comment_file = (object) $comment->{'field_' . $name}[LANGUAGE_NONE][0];
    $this->assertFileExists($comment_file, t('New file saved to disk on node creation.'));
    // Test authenticated file download.
    $url = file_create_url($comment_file->uri);
    $this->assertNotEqual($url, NULL, t('Confirmed that the URL is valid'));
    $this->drupalGet(file_create_url($comment_file->uri));
    $this->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the shipped file.'));
557

558
559
560
561
562
563
    // Test anonymous file download.
    $this->drupalLogout();
    $this->drupalGet(file_create_url($comment_file->uri));
    $this->assertResponse(403, t('Confirmed that access is denied for the file without the needed permission.'));
  }

564
565
}

566
567
568
569
/**
 * Test class to test file handling with node revisions.
 */
class FileFieldRevisionTestCase extends FileFieldTestCase {
570
  public static function getInfo() {
571
    return array(
572
573
574
      'name' => 'File field revision test',
      'description' => 'Test creating and deleting revisions with files attached.',
      'group' => 'File',
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
    );
  }

  /**
   * Test creating multiple revisions of a node and managing the attached files.
   *
   * Expected behaviors:
   *  - Adding a new revision will make another entry in the field table, but
   *    the original file will not be duplicated.
   *  - Deleting a revision should not delete the original file if the file
   *    is in use by another revision.
   *  - When the last revision that uses a file is deleted, the original file
   *    should be deleted also.
   */
  function testRevisions() {
    $type_name = 'article';
591
    $field_name = strtolower($this->randomName());
592
593
    $this->createFileField($field_name, $type_name);
    $field = field_info_field($field_name);
594
    $instance = field_info_instance('node', $field_name, $type_name);
595

596
597
598
    // Attach the same fields to users.
    $this->attachFileField($field_name, 'user', 'user');

599
600
601
602
603
604
605
    $test_file = $this->getTestFile('text');

    // Create a new node with the uploaded file.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);

    // Check that the file exists on disk and in the database.
    $node = node_load($nid, NULL, TRUE);
606
    $node_file_r1 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
607
    $node_vid_r1 = $node->vid;
608
609
610
    $this->assertFileExists($node_file_r1, t('New file saved to disk on node creation.'));
    $this->assertFileEntryExists($node_file_r1, t('File entry exists in database on node creation.'));
    $this->assertFileIsPermanent($node_file_r1, t('File is permanent.'));
611
612
613
614

    // Upload another file to the same node in a new revision.
    $this->replaceNodeFile($test_file, $field_name, $nid);
    $node = node_load($nid, NULL, TRUE);
615
    $node_file_r2 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
616
    $node_vid_r2 = $node->vid;
617
618
619
    $this->assertFileExists($node_file_r2, t('Replacement file exists on disk after creating new revision.'));
    $this->assertFileEntryExists($node_file_r2, t('Replacement file entry exists in database after creating new revision.'));
    $this->assertFileIsPermanent($node_file_r2, t('Replacement file is permanent.'));
620
621
622

    // Check that the original file is still in place on the first revision.
    $node = node_load($nid, $node_vid_r1, TRUE);
623
624
625
626
    $this->assertEqual($node_file_r1, (object) $node->{$field_name}[LANGUAGE_NONE][0], t('Original file still in place after replacing file in new revision.'));
    $this->assertFileExists($node_file_r1, t('Original file still in place after replacing file in new revision.'));
    $this->assertFileEntryExists($node_file_r1, t('Original file entry still in place after replacing file in new revision'));
    $this->assertFileIsPermanent($node_file_r1, t('Original file is still permanent.'));
627
628
629
630
631

    // Save a new version of the node without any changes.
    // Check that the file is still the same as the previous revision.
    $this->drupalPost('node/' . $nid . '/edit', array('revision' => '1'), t('Save'));
    $node = node_load($nid, NULL, TRUE);
632
    $node_file_r3 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
633
    $node_vid_r3 = $node->vid;
634
635
    $this->assertEqual($node_file_r2, $node_file_r3, t('Previous revision file still in place after creating a new revision without a new file.'));
    $this->assertFileIsPermanent($node_file_r3, t('New revision file is permanent.'));
636
637
638
639

    // Revert to the first revision and check that the original file is active.
    $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r1 . '/revert', array(), t('Revert'));
    $node = node_load($nid, NULL, TRUE);
640
    $node_file_r4 = (object) $node->{$field_name}[LANGUAGE_NONE][0];
641
    $node_vid_r4 = $node->vid;
642
643
    $this->assertEqual($node_file_r1, $node_file_r4, t('Original revision file still in place after reverting to the original revision.'));
    $this->assertFileIsPermanent($node_file_r4, t('Original revision file still permanent after reverting to the original revision.'));
644
645
646
647

    // Delete the second revision and check that the file is kept (since it is
    // still being used by the third revision).
    $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r2 . '/delete', array(), t('Delete'));
648
649
650
    $this->assertFileExists($node_file_r3, t('Second file is still available after deleting second revision, since it is being used by the third revision.'));
    $this->assertFileEntryExists($node_file_r3, t('Second file entry is still available after deleting second revision, since it is being used by the third revision.'));
    $this->assertFileIsPermanent($node_file_r3, t('Second file entry is still permanent after deleting second revision, since it is being used by the third revision.'));
651

652
653
    // Attach the second file to a user.
    $user = $this->drupalCreateUser();
654
    $edit = (array) $user;
655
656
657
658
659
    $edit[$field_name][LANGUAGE_NONE][0] = (array) $node_file_r3;
    user_save($user, $edit);
    $this->drupalGet('user/' . $user->uid . '/edit');

    // Delete the third revision and check that the file is not deleted yet.
660
    $this->drupalPost('node/' . $nid . '/revisions/' . $node_vid_r3 . '/delete', array(), t('Delete'));
661
662
663
    $this->assertFileExists($node_file_r3, t('Second file is still available after deleting third revision, since it is being used by the user.'));
    $this->assertFileEntryExists($node_file_r3, t('Second file entry is still available after deleting third revision, since it is being used by the user.'));
    $this->assertFileIsPermanent($node_file_r3, t('Second file entry is still permanent after deleting third revision, since it is being used by the user.'));
664
665
666

    // Delete the user and check that the file is also deleted.
    user_delete($user->uid);
667
668
669
670
    // TODO: This seems like a bug in File API. Clearing the stat cache should
    // not be necessary here. The file really is deleted, but stream wrappers
    // doesn't seem to think so unless we clear the PHP file stat() cache.
    clearstatcache();
671
672
    $this->assertFileNotExists($node_file_r3, t('Second file is now deleted after deleting third revision, since it is no longer being used by any other nodes.'));
    $this->assertFileEntryNotExists($node_file_r3, t('Second file entry is now deleted after deleting third revision, since it is no longer being used by any other nodes.'));
673
674
675

    // Delete the entire node and check that the original file is deleted.
    $this->drupalPost('node/' . $nid . '/delete', array(), t('Delete'));
676
677
    $this->assertFileNotExists($node_file_r1, t('Original file is deleted after deleting the entire node with two revisions remaining.'));
    $this->assertFileEntryNotExists($node_file_r1, t('Original file entry is deleted after deleting the entire node with two revisions remaining.'));
678
679
680
681
682
683
684
  }
}

/**
 * Test class to check that formatters are working properly.
 */
class FileFieldDisplayTestCase extends FileFieldTestCase {
685
  public static function getInfo() {
686
    return array(
687
688
689
      'name' => 'File field display tests',
      'description' => 'Test the display of file fields in node and views.',
      'group' => 'File',
690
691
692
693
694
695
696
    );
  }

  /**
   * Test normal formatter display on node display.
   */
  function testNodeDisplay() {
697
    $field_name = strtolower($this->randomName());
698
699
700
701
702
    $type_name = 'article';
    $field_settings = array(
      'display_field' => '1',
      'display_default' => '1',
    );
703
    $instance_settings = array(
704
705
      'description_field' => '1',
    );
706
    $widget_settings = array();
707
708
    $this->createFileField($field_name, $type_name, $field_settings, $instance_settings, $widget_settings);
    $field = field_info_field($field_name);
709
    $instance = field_info_instance('node', $field_name, $type_name);
710
711
712
713
714
715
716
717
718

    $test_file = $this->getTestFile('text');

    // Create a new node with the uploaded file.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $this->drupalGet('node/' . $nid . '/edit');

    // Check that the default formatter is displaying with the file name.
    $node = node_load($nid, NULL, TRUE);
719
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
720
    $default_output = theme('file_link', array('file' => $node_file));
721
    $this->assertRaw($default_output, t('Default formatter displaying correctly on full node view.'));
722
723

    // Turn the "display" option off and check that the file is no longer displayed.
724
    $edit = array($field_name . '[' . LANGUAGE_NONE . '][0][display]' => FALSE);
725
726
    $this->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));

727
    $this->assertNoRaw($default_output, t('Field is hidden when "display" option is unchecked.'));
728
729
730
731
732
733
734
735
736
737
738

  }
}

/**
 * Test class to check for various validations.
 */
class FileFieldValidateTestCase extends FileFieldTestCase {
  protected $field;
  protected $node_type;

739
  public static function getInfo() {
740
    return array(
741
742
743
      'name' => 'File field validation tests',
      'description' => 'Tests validation functions such as file type, max file size, max size per node, and required.',
      'group' => 'File',
744
745
746
747
748
749
750
751
    );
  }

  /**
   * Test required property on file fields.
   */
  function testRequired() {
    $type_name = 'article';
752
    $field_name = strtolower($this->randomName());
753
754
    $this->createFileField($field_name, $type_name, array(), array('required' => '1'));
    $field = field_info_field($field_name);
755
    $instance = field_info_instance('node', $field_name, $type_name);
756
757
758
759

    $test_file = $this->getTestFile('text');

    // Try to post a new node without uploading a file.
760
    $langcode = LANGUAGE_NONE;
761
    $edit = array("title" => $this->randomName());
762
    $this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
763
    $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), t('Node save failed when required file field was empty.'));
764
765
766

    // Create a new node with the uploaded file.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
767
768
    $this->assertTrue($nid !== FALSE, t('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array('@test_file' => $test_file->uri, '@field_name' => $field_name, '@type_name' => $type_name)));

769
770
    $node = node_load($nid, NULL, TRUE);

771
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
772
773
    $this->assertFileExists($node_file, t('File exists after uploading to the required field.'));
    $this->assertFileEntryExists($node_file, t('File entry exists after uploading to the required field.'));
774
775
776
777
778
779

    // Try again with a multiple value field.
    field_delete_field($field_name);
    $this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED), array('required' => '1'));

    // Try to post a new node without uploading a file in the multivalue field.
780
    $edit = array('title' => $this->randomName());
781
    $this->drupalPost('node/add/' . $type_name, $edit, t('Save'));
782
    $this->assertRaw(t('!title field is required.', array('!title' => $instance['label'])), t('Node save failed when required multiple value file field was empty.'));
783
784
785
786

    // Create a new node with the uploaded file into the multivalue field.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $node = node_load($nid, NULL, TRUE);
787
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
788
789
    $this->assertFileExists($node_file, t('File exists after uploading to the required multiple value field.'));
    $this->assertFileEntryExists($node_file, t('File entry exists after uploading to the required multipel value field.'));
790
791
792
793
794
795
796
797
798
799

    // Remove our file field.
    field_delete_field($field_name);
  }

  /**
   * Test the max file size validator.
   */
  function testFileMaxSize() {
    $type_name = 'article';
800
    $field_name = strtolower($this->randomName());
801
802
    $this->createFileField($field_name, $type_name, array(), array('required' => '1'));
    $field = field_info_field($field_name);
803
    $instance = field_info_instance('node', $field_name, $type_name);
804
805
806
807
808
809
810
811
812
813
814
815
816
817

    $small_file = $this->getTestFile('text', 131072); // 128KB.
    $large_file = $this->getTestFile('text', 1310720); // 1.2MB

    // Test uploading both a large and small file with different increments.
    $sizes = array(
      '1M' => 1048576,
      '1024K' => 1048576,
      '1048576' => 1048576,
    );

    foreach ($sizes as $max_filesize => $file_limit) {
      // Set the max file upload size.
      $this->updateFileField($field_name, $type_name, array('max_filesize' => $max_filesize));
818
      $instance = field_info_instance('node', $field_name, $type_name);
819
820
821
822

      // Create a new node with the small file, which should pass.
      $nid = $this->uploadNodeFile($small_file, $field_name, $type_name);
      $node = node_load($nid, NULL, TRUE);
823
      $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
824
825
      $this->assertFileExists($node_file, t('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
      $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array('%filesize' => format_size($small_file->filesize), '%maxsize' => $max_filesize)));
826
827
828
829

      // Check that uploading the large file fails (1M limit).
      $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
      $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)));
830
      $this->assertRaw($error_message, t('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', array('%filesize' => format_size($large_file->filesize), '%maxsize' => $max_filesize)));
831
832
833
834
835
836
837
838
    }

    // Turn off the max filesize.
    $this->updateFileField($field_name, $type_name, array('max_filesize' => ''));

    // Upload the big file successfully.
    $nid = $this->uploadNodeFile($large_file, $field_name, $type_name);
    $node = node_load($nid, NULL, TRUE);
839
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
840
841
    $this->assertFileExists($node_file, t('File exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
    $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) with no max limit.', array('%filesize' => format_size($large_file->filesize))));
842
843
844
845
846
847
848
849
850
851

    // Remove our file field.
    field_delete_field($field_name);
  }

  /**
   * Test the file extension, do additional checks if mimedetect is installed.
   */
  function testFileExtension() {
    $type_name = 'article';
852
    $field_name = strtolower($this->randomName());
853
854
    $this->createFileField($field_name, $type_name);
    $field = field_info_field($field_name);
855
    $instance = field_info_instance('node', $field_name, $type_name);
856
857

    $test_file = $this->getTestFile('image');
858
    list(, $test_file_extension) = explode('.', $test_file->filename);
859
860
861
862
863
864
865

    // Disable extension checking.
    $this->updateFileField($field_name, $type_name, array('file_extensions' => ''));

    // Check that the file can be uploaded with no extension checking.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $node = node_load($nid, NULL, TRUE);
866
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
867
868
    $this->assertFileExists($node_file, t('File exists after uploading a file with no extension checking.'));
    $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file with no extension checking.'));
869
870
871
872
873
874
875

    // Enable extension checking for text files.
    $this->updateFileField($field_name, $type_name, array('file_extensions' => 'txt'));

    // Check that the file with the wrong extension cannot be uploaded.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $error_message = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => 'txt'));
876
    $this->assertRaw($error_message, t('Node save failed when file uploaded with the wrong extension.'));
877
878

    // Enable extension checking for text and image files.
879
    $this->updateFileField($field_name, $type_name, array('file_extensions' => "txt $test_file_extension"));
880
881
882
883

    // Check that the file can be uploaded with extension checking.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
    $node = node_load($nid, NULL, TRUE);
884
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
885
886
    $this->assertFileExists($node_file, t('File exists after uploading a file with extension checking.'));
    $this->assertFileEntryExists($node_file, t('File entry exists after uploading a file with extension checking.'));
887
888
889
890
891
892
893
894
895
896

    // Remove our file field.
    field_delete_field($field_name);
  }
}

/**
 * Test class to check that files are uploaded to proper locations.
 */
class FileFieldPathTestCase extends FileFieldTestCase {
897
  public static function getInfo() {
898
    return array(
899
900
901
      'name' => 'File field file path tests',
      'description' => 'Test that files are uploaded to the proper location with token support.',
      'group' => 'File',
902
903
904
905
906
907
908
    );
  }

  /**
   * Test normal formatter display on node display.
   */
  function testUploadPath() {
909
    $field_name = strtolower($this->randomName());
910
911
912
913
914
915
916
917
918
    $type_name = 'article';
    $field = $this->createFileField($field_name, $type_name);
    $test_file = $this->getTestFile('text');

    // Create a new node.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);

    // Check that the file was uploaded to the file root.
    $node = node_load($nid, NULL, TRUE);
919
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
920
    $this->assertPathMatch('public://' . $test_file->filename, $node_file->uri, t('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
921
922
923
924
925
926
927
928
929

    // Change the path to contain multiple subdirectories.
    $field = $this->updateFileField($field_name, $type_name, array('file_directory' => 'foo/bar/baz'));

    // Upload a new file into the subdirectories.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);

    // Check that the file was uploaded into the subdirectory.
    $node = node_load($nid, NULL, TRUE);
930
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
931
    $this->assertPathMatch('public://foo/bar/baz/' . $test_file->filename, $node_file->uri, t('The file %file was uploaded to the correct path.', array('%file' => $node_file->uri)));
932
933
934

    // Check the path when used with tokens.
    // Change the path to contain multiple token directories.
935
    $field = $this->updateFileField($field_name, $type_name, array('file_directory' => '[current-user:uid]/[current-user:name]'));
936
937
938
939
940
941

    // Upload a new file into the token subdirectories.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);

    // Check that the file was uploaded into the subdirectory.
    $node = node_load($nid, NULL, TRUE);
942
    $node_file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
943
944
    // Do token replacement using the same user which uploaded the file, not
    // the user running the test case.
945
946
    $data = array('user' => $this->admin_user);
    $subdirectory = token_replace('[user:uid]/[user:name]', $data);
947
    $this->assertPathMatch('public://' . $subdirectory . '/' . $test_file->filename, $node_file->uri, t('The file %file was uploaded to the correct path with token replacements.', array('%file' => $node_file->uri)));
948
949
950
951
  }

  /**
   * A loose assertion to check that a file is uploaded to the right location.
952
   *
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
   * @param $expected_path
   *   The location where the file is expected to be uploaded. Duplicate file
   *   names to not need to be taken into account.
   * @param $actual_path
   *   Where the file was actually uploaded.
   * @param $message
   *   The message to display with this assertion.
   */
  function assertPathMatch($expected_path, $actual_path, $message) {
    // Strip off the extension of the expected path to allow for _0, _1, etc.
    // suffixes when the file hits a duplicate name.
    $pos = strrpos($expected_path, '.');
    $base_path = substr($expected_path, 0, $pos);
    $extension = substr($expected_path, $pos + 1);

    $result = preg_match('/' . preg_quote($base_path, '/') . '(_[0-9]+)?\.' . preg_quote($extension, '/') . '/', $actual_path);
    $this->assertTrue($result, $message);
  }
}
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007

/**
 * Test file token replacement in strings.
 */
class FileTokenReplaceTestCase extends FileFieldTestCase {
  public static function getInfo() {
    return array(
      'name' => 'File token replacement',
      'description' => 'Generates text using placeholders for dummy content to check file token replacement.',
      'group' => 'File',
    );
  }

  /**
   * Creates a file, then tests the tokens generated from it.
   */
  function testFileTokenReplacement() {
    global $language;
    $url_options = array(
      'absolute' => TRUE,
      'language' => $language,
    );

    // Create file field.
    $type_name = 'article';
    $field_name = 'field_' . strtolower($this->randomName());
    $this->createFileField($field_name, $type_name);
    $field = field_info_field($field_name);
    $instance = field_info_instance('node', $field_name, $type_name);

    $test_file = $this->getTestFile('text');

    // Create a new node with the uploaded file.
    $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);

    // Load the node and the file.
1008
    $node = node_load($nid, NULL, TRUE);
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
    $file = (object) $node->{$field_name}[LANGUAGE_NONE][0];
    $file->description = 'File description.';

    // Generate and test sanitized tokens.
    $tests = array();
    $tests['[file:fid]'] = $file->fid;
    $tests['[file:name]'] = check_plain($file->filename);
    $tests['[file:description]'] = filter_xss($file->description);
    $tests['[file:path]'] = filter_xss($file->uri);
    $tests['[file:mime]'] = filter_xss($file->filemime);
    $tests['[file:size]'] = format_size($file->filesize);
    $tests['[file:url]'] = url(file_create_url($file->uri), $url_options);
    $tests['[file:timestamp]'] = format_date($file->timestamp, 'medium', '', NULL, $language->language);
    $tests['[file:timestamp:short]'] = format_date($file->timestamp, 'short', '', NULL, $language->language);
    $tests['[file:owner]'] = $this->admin_user->name;
1024
    $tests['[file:owner:uid]'] = $file->uid;
1025
1026

    // Test to make sure that we generated something for each token.
1027
    $this->assertFalse(in_array(0, array_map('strlen', $tests)), t('No empty tokens generated.'));
1028
1029
1030

    foreach ($tests as $input => $expected) {
      $output = token_replace($input, array('file' => $file), array('language' => $language));
1031
      $this->assertEqual($output, $expected, t('Sanitized file token %token replaced.', array('%token' => $input)));
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
    }

    // Generate and test unsanitized tokens.
    $tests['[file:name]'] = $file->filename;
    $tests['[file:description]'] = $file->description;
    $tests['[file:path]'] = $file->uri;
    $tests['[file:mime]'] = $file->filemime;
    $tests['[file:size]'] = format_size($file->filesize);

    foreach ($tests as $input => $expected) {
      $output = token_replace($input, array('file' => $file), array('language' => $language, 'sanitize' => FALSE));
1043
      $this->assertEqual($output, $expected, t('Unsanitized file token %token replaced.', array('%token' => $input)));
1044
1045
1046
    }
  }
}