Project

General

Profile

Paste
Download (64.8 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / file_entity / file_entity.test @ 1f142f4f

1
<?php
2

    
3
/**
4
 * @file
5
 * Test integration for the file_entity module.
6
 */
7

    
8
class FileEntityTestHelper extends DrupalWebTestCase {
9
  function setUp() {
10
    $modules = func_get_args();
11
    if (isset($modules[0]) && is_array($modules[0])) {
12
      $modules = $modules[0];
13
    }
14
    $modules[] = 'file_entity';
15
    parent::setUp($modules);
16
  }
17

    
18
  /**
19
   * Retrieves a sample file of the specified type.
20
   */
21
  function getTestFile($type_name, $size = NULL) {
22
    // Get a file to upload.
23
    $file = current($this->drupalGetTestFiles($type_name, $size));
24

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

    
28
    return $file;
29
  }
30

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

    
38
  /**
39
   * Get a file from the database based on its filename.
40
   *
41
   * @param $filename
42
   *   A file filename, usually generated by $this->randomName().
43
   * @param $reset
44
   *   (optional) Whether to reset the internal file_load() cache.
45
   *
46
   * @return
47
   *   A file object matching $filename.
48
   */
49
  function getFileByFilename($filename, $reset = FALSE) {
50
    $files = file_load_multiple(array(), array('filename' => $filename), $reset);
51
    // Load the first file returned from the database.
52
    $returned_file = reset($files);
53
    return $returned_file;
54
  }
55

    
56
  protected function createFileEntity($settings = array()) {
57
    // Populate defaults array.
58
    $settings += array(
59
      'filepath' => 'Файл для тестирования ' . $this->randomName(), // Prefix with non-latin characters to ensure that all file-related tests work with international filenames.
60
      'filemime' => 'text/plain',
61
      'uid' => 1,
62
      'timestamp' => REQUEST_TIME,
63
      'status' => FILE_STATUS_PERMANENT,
64
      'contents' => "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.",
65
      'scheme' => file_default_scheme(),
66
      'type' => NULL,
67
    );
68

    
69
    $filepath = $settings['scheme'] . '://' . $settings['filepath'];
70

    
71
    file_put_contents($filepath, $settings['contents']);
72
    $this->assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file');
73

    
74
    $file = new stdClass();
75
    $file->uri = $filepath;
76
    $file->filename = drupal_basename($file->uri);
77
    $file->filemime = $settings['filemime'];
78
    $file->uid = $settings['uid'];
79
    $file->timestamp = $settings['timestamp'];
80
    $file->filesize = filesize($file->uri);
81
    $file->status = $settings['status'];
82
    $file->type = $settings['type'];
83

    
84
    // The file type is used as a bundle key, and therefore, must not be NULL.
85
    if (!isset($file->type)) {
86
      $file->type = FILE_TYPE_NONE;
87
    }
88

    
89
    // If the file isn't already assigned a real type, determine what type should
90
    // be assigned to it.
91
    if ($file->type === FILE_TYPE_NONE) {
92
      $type = file_get_type($file);
93
      if (isset($type)) {
94
        $file->type = $type;
95
      }
96
    }
97

    
98
    // Write the record directly rather than calling file_save() so we don't
99
    // invoke the hooks.
100
    $this->assertNotIdentical(drupal_write_record('file_managed', $file), FALSE, t('The file was added to the database.'), 'Create test file');
101

    
102
    return $file;
103
  }
104

    
105
  protected function createFileType($overrides = array()) {
106
    $type = new stdClass();
107
    $type->type = 'test';
108
    $type->label = "Test";
109
    $type->description = '';
110
    $type->mimetypes = array('image/jpeg', 'image/gif', 'image/png', 'image/tiff');
111

    
112
    foreach ($overrides as $k => $v) {
113
      $type->$k = $v;
114
    }
115

    
116
    file_type_save($type);
117
    return $type;
118
  }
119

    
120
  /**
121
   * Overrides DrupalWebTestCase::drupalGetToken() to support the hash salt.
122
   *
123
   * @todo Remove when http://drupal.org/node/1555862 is fixed in core.
124
   */
125
  protected function drupalGetToken($value = '') {
126
    $private_key = drupal_get_private_key();
127
    return drupal_hmac_base64($value, $this->session_id . $private_key . drupal_get_hash_salt());
128
  }
129
}
130

    
131
/**
132
 * Tests file type classification functionality.
133
 */
134
class FileEntityFileTypeClassificationTestCase extends DrupalWebTestCase {
135
  public static function getInfo() {
136
    return array(
137
      'name' => 'File entity classification',
138
      'description' => 'Test existing file entity classification functionality.',
139
      'group' => 'File entity',
140
    );
141
  }
142

    
143
  function setUp() {
144
    parent::setUp();
145
  }
146

    
147
  /**
148
   * Test that existing files are properly classified by file type.
149
   */
150
  function testFileTypeClassification() {
151
    // Get test text and image files.
152
    $file = current($this->drupalGetTestFiles('text'));
153
    $text_file = file_save($file);
154
    $file = current($this->drupalGetTestFiles('image'));
155
    $image_file = file_save($file);
156

    
157
    // Enable file entity which adds adds a file type property to files and
158
    // queues up existing files for classification.
159
    module_enable(array('file_entity'));
160

    
161
    // Existing files have yet to be classified and should have an undefined
162
    // file type.
163
    $file_type = $this->getFileType($text_file);
164
    $this->assertEqual($file_type['type'], 'undefined', t('The text file has an undefined file type.'));
165
    $file_type = $this->getFileType($image_file);
166
    $this->assertEqual($file_type['type'], 'undefined', t('The image file has an undefined file type.'));
167

    
168
    // The classification queue is processed during cron runs. Run cron to
169
    // trigger the classification process.
170
    $this->cronRun();
171

    
172
    // The classification process should assign a file type to any file whose
173
    // MIME type is assigned to a file type. Check to see if each file was
174
    // assigned a proper file type.
175
    $file_type = $this->getFileType($text_file);
176
    $this->assertEqual($file_type['type'], 'document', t('The text file was properly assigned the Document file type.'));
177
    $file_type = $this->getFileType($image_file);
178
    $this->assertEqual($file_type['type'], 'image', t('The image file was properly assigned the Image file type.'));
179
  }
180

    
181
  /**
182
   * Get the file type of a given file.
183
   *
184
   * @param $file
185
   *   A file object.
186
   *
187
   * @return
188
   *   The file's file type as a string.
189
   */
190
  function getFileType($file) {
191
    $type = db_select('file_managed', 'fm')
192
      ->fields('fm', array('type'))
193
      ->condition('fid', $file->fid, '=')
194
      ->execute()
195
      ->fetchAssoc();
196

    
197
    return $type;
198
  }
199
}
200

    
201
/**
202
 * Tests basic file entity functionality.
203
 */
204
class FileEntityUnitTestCase extends FileEntityTestHelper {
205
  public static function getInfo() {
206
    return array(
207
      'name' => 'File entity unit tests',
208
      'description' => 'Test basic file entity functionality.',
209
      'group' => 'File entity',
210
    );
211
  }
212

    
213
  /**
214
   * Regression tests for core issue http://drupal.org/node/1239376.
215
   */
216
  function testMimeTypeMappings() {
217
    $tests = array(
218
      'public://test.ogg' => 'audio/ogg',
219
      'public://test.mkv' => 'video/x-m4v',
220
      'public://test.mka' => 'audio/x-matroska',
221
      'public://test.mkv' => 'video/x-matroska',
222
      'public://test.webp' => 'image/webp',
223
    );
224
    foreach ($tests as $input => $expected) {
225
      $this->assertEqual(file_get_mimetype($input), $expected);
226
    }
227
  }
228

    
229
  /**
230
   * Tests basic file entity properties.
231
   */
232
  function testFileEntity() {
233
    // Save a raw file, turning it into a file entity.
234
    $file = $this->getTestFile('text');
235
    $file->uid = 1;
236
    $file->status = FILE_STATUS_PERMANENT;
237
    file_save($file);
238

    
239
    // Test entity ID, revision ID, and bundle.
240
    $ids = entity_extract_ids('file', $file);
241
    $this->assertIdentical($ids, array($file->fid, NULL, 'document'));
242

    
243
    // Test the entity URI callback.
244
    $uri = entity_uri('file', $file);
245
    $this->assertEqual($uri['path'], "file/{$file->fid}");
246
  }
247

    
248
  /**
249
   * Tests storing image height and width as file metadata.
250
   */
251
  function testImageDimensions() {
252
    // Test hook_file_insert().
253
    $file = current($this->drupalGetTestFiles('image'));
254
    $image_file = file_save($file);
255
    $this->assertTrue(isset($image_file->metadata['height']), 'Image height retrieved on file_save() for an image file.');
256
    $this->assertTrue(isset($image_file->metadata['width']), 'Image width retrieved on file_save() for an image file.');
257

    
258
    $file = current($this->drupalGetTestFiles('text'));
259
    $text_file = file_save($file);
260
    $this->assertFalse(isset($text_file->metadata['height']), 'No image height retrieved on file_save() for an text file.');
261
    $this->assertFalse(isset($text_file->metadata['width']), 'No image width retrieved on file_save() for an text file.');
262

    
263
    // Test hook_file_load().
264
    // Clear the cache and load fresh files objects to test file_load behavior.
265
    entity_get_controller('file')->resetCache();
266

    
267
    $file = file_load($image_file->fid);
268
    $this->assertTrue(isset($file->metadata['height']), 'Image dimensions retrieved on file_load() for an image file.');
269
    $this->assertTrue(isset($file->metadata['width']), 'Image dimensions retrieved on file_load() for an image file.');
270

    
271
    $this->assertEqual($file->metadata['height'], $image_file->metadata['height'], 'Loaded image height is equal to saved image height.');
272
    $this->assertEqual($file->metadata['width'], $image_file->metadata['width'], 'Loaded image width is equal to saved image width.');
273

    
274
    $file = file_load($text_file->fid);
275
    $this->assertFalse(isset($file->metadata['height']), 'No image height retrieved on file_load() for an text file.');
276
    $this->assertFalse(isset($file->metadata['width']), 'No image width retrieved on file_load() for an text file.');
277

    
278
    // Test hook_file_update().
279
    // Load the first image file and resize it.
280
    $height = $image_file->metadata['width'] / 2;
281
    $width = $image_file->metadata['height'] / 2;
282
    $image = image_load($image_file->uri);
283
    image_resize($image, $width, $height);
284
    image_save($image);
285
    file_save($image_file);
286

    
287
    $this->assertEqual($image_file->metadata['height'], $height, 'Image file height updated by file_save().');
288
    $this->assertEqual($image_file->metadata['width'], $width, 'Image file width updated by file_save().');
289

    
290
    // Clear the cache and reload the file.
291
    entity_get_controller('file')->resetCache();
292

    
293
    $file = file_load($image_file->fid);
294
    $this->assertEqual($file->metadata['height'], $height, 'Updated image height retrieved by file_load().');
295
    $this->assertEqual($file->metadata['width'], $width, 'Updated image width retrieved by file_load().');
296

    
297
    // Verify that the image dimension metadata is removed on file deletion.
298
    file_delete($file, TRUE);
299
    $this->assertFalse(db_query('SELECT COUNT(*) FROM {file_metadata} WHERE fid = :fid', array(':fid' => 'fid'))->fetchField(), 'Row deleted in {file_metadata} on file_delete().');
300
  }
301
}
302

    
303
/**
304
 * Tests editing existing file entities.
305
 */
306
class FileEntityEditTestCase extends FileEntityTestHelper {
307
  protected $web_user;
308
  protected $admin_user;
309

    
310
  public static function getInfo() {
311
    return array(
312
      'name' => 'File entity edit',
313
      'description' => 'Create a file and test file edit functionality.',
314
      'group' => 'File entity',
315
    );
316
  }
317

    
318
  function setUp() {
319
    parent::setUp();
320

    
321
    $this->web_user = $this->drupalCreateUser(array('edit own document files', 'create files'));
322
    $this->admin_user = $this->drupalCreateUser(array('bypass file access', 'administer files'));
323
  }
324

    
325
  /**
326
   * Check file edit functionality.
327
   */
328
  function testFileEntityEdit() {
329
    $this->drupalLogin($this->web_user);
330

    
331
    $test_file = $this->getTestFile('text');
332
    $name_key = "filename";
333

    
334
    // Create file to edit.
335
    $edit = array();
336
    $edit['files[upload]'] = drupal_realpath($test_file->uri);
337
    $this->drupalPost('file/add', $edit, t('Next'));
338
    if ($this->xpath('//input[@name="scheme"]')) {
339
      $this->drupalPost(NULL, array(), t('Next'));
340
    }
341

    
342
    // Check that the file exists in the database.
343
    $file = $this->getFileByFilename($test_file->filename);
344
    $this->assertTrue($file, t('File found in database.'));
345

    
346
    // Check that "edit" link points to correct page.
347
    $this->clickLink(t('Edit'));
348
    $edit_url = url("file/$file->fid/edit", array('absolute' => TRUE));
349
    $actual_url = $this->getURL();
350
    $this->assertEqual($edit_url, $actual_url, t('On edit page.'));
351

    
352
    // Check that the name field is displayed with the correct value.
353
    $active = '<span class="element-invisible">' . t('(active tab)') . '</span>';
354
    $link_text = t('!local-task-title!active', array('!local-task-title' => t('Edit'), '!active' => $active));
355
    $this->assertText(strip_tags($link_text), 0, t('Edit tab found and marked active.'));
356
    $this->assertFieldByName($name_key, $file->filename, t('Name field displayed.'));
357

    
358
    // The user does not have "delete" permissions so no delete button should be found.
359
    $this->assertNoFieldByName('op', t('Delete'), 'Delete button not found.');
360

    
361
    // Edit the content of the file.
362
    $edit = array();
363
    $edit[$name_key] = $this->randomName(8);
364
    // Stay on the current page, without reloading.
365
    $this->drupalPost(NULL, $edit, t('Save'));
366

    
367
    // Check that the name field is displayed with the updated values.
368
    $this->assertText($edit[$name_key], t('Name displayed.'));
369
  }
370

    
371
  /**
372
   * Check changing file associated user fields.
373
   */
374
  function testFileEntityAssociatedUser() {
375
    $this->drupalLogin($this->admin_user);
376

    
377
    // Create file to edit.
378
    $test_file = $this->getTestFile('text');
379
    $name_key = "filename";
380
    $edit = array();
381
    $edit['files[upload]'] = drupal_realpath($test_file->uri);
382
    $this->drupalPost('file/add', $edit, t('Next'));
383

    
384
    // Check that the file was associated with the currently logged in user.
385
    $file = $this->getFileByFilename($test_file->filename);
386
    $this->assertIdentical($file->uid, $this->admin_user->uid, 'File associated with admin user.');
387

    
388
    // Try to change the 'associated user' field to an invalid user name.
389
    $edit = array(
390
      'name' => 'invalid-name',
391
    );
392
    $this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
393
    $this->assertText('The username invalid-name does not exist.');
394

    
395
    // Change the associated user field to an empty string, which should assign
396
    // association to the anonymous user (uid 0).
397
    $edit['name'] = '';
398
    $this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
399
    $file = file_load($file->fid);
400
    $this->assertIdentical($file->uid, '0', 'File associated with anonymous user.');
401

    
402
    // Change the associated user field to another user's name (that is not
403
    // logged in).
404
    $edit['name'] = $this->web_user->name;
405
    $this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
406
    $file = file_load($file->fid);
407
    $this->assertIdentical($file->uid, $this->web_user->uid, 'File associated with normal user.');
408

    
409
    // Check that normal users cannot change the associated user information.
410
    $this->drupalLogin($this->web_user);
411
    $this->drupalGet('file/' . $file->fid . '/edit');
412
    $this->assertNoFieldByName('name');
413
  }
414
}
415

    
416
/**
417
 * Tests creating new file entities through the file upload wizard.
418
 */
419
class FileEntityUploadWizardTestCase extends FileEntityTestHelper {
420
  public static function getInfo() {
421
    return array(
422
      'name' => 'File entity upload wizard',
423
      'description' => 'Upload a file using the multi-step wizard.',
424
      'group' => 'File entity',
425
      'dependencies' => array('token'),
426
    );
427
  }
428

    
429
  function setUp() {
430
    parent::setUp('token');
431

    
432
    // Disable the private file system which is automatically enabled by
433
    // DrupalTestCase so we can test the upload wizard correctly.
434
    variable_del('file_private_path');
435

    
436
    $web_user = $this->drupalCreateUser(array('create files', 'view own private files'));
437
    $this->drupalLogin($web_user);
438
  }
439

    
440
  /**
441
   * Test the basic file upload wizard functionality.
442
   */
443
  function testFileEntityUploadWizardBasic() {
444
    $test_file = $this->getTestFile('text');
445

    
446
    // Step 1: Upload a basic document file.
447
    $edit = array();
448
    $edit['files[upload]'] = drupal_realpath($test_file->uri);
449
    $this->drupalPost('file/add', $edit, t('Next'));
450

    
451
    // Check that the file exists in the database.
452
    $fid = $this->getLastFileId();
453
    $file = file_load($fid);
454
    $this->assertTrue($file, t('File found in database.'));
455

    
456
    // Check that the document file has been uploaded.
457
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Document', '%name' => $file->filename)), t('Document file uploaded.'));
458
  }
459

    
460
  /**
461
   * Test the file upload wizard type step.
462
   */
463
  function testFileEntityUploadWizardTypes() {
464
    $test_file = $this->getTestFile('text');
465

    
466
    // Create multiple file types with the same mime types.
467
    $this->createFileType(array('type' => 'document1', 'label' => 'Document 1', 'mimetypes' => array('text/plain')));
468
    $this->createFileType(array('type' => 'document2', 'label' => 'Document 2', 'mimetypes' => array('text/plain')));
469

    
470
    // Step 1: Upload a basic document file.
471
    $edit = array();
472
    $edit['files[upload]'] = drupal_realpath($test_file->uri);
473
    $this->drupalPost('file/add', $edit, t('Next'));
474

    
475
    // Step 2: File type selection.
476
    $edit = array();
477
    $edit['type'] = 'document2';
478
    $this->drupalPost(NULL, $edit, t('Next'));
479

    
480
    // Check that the file exists in the database.
481
    $fid = $this->getLastFileId();
482
    $file = file_load($fid);
483
    $this->assertTrue($file, t('File found in database.'));
484

    
485
    // Check that the document file has been uploaded.
486
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Document 2', '%name' => $file->filename)), t('Document 2 file uploaded.'));
487
  }
488

    
489
  /**
490
   * Test the file upload wizard scheme step.
491
   */
492
  function testFileEntityUploadWizardSchemes() {
493
    $test_file = $this->getTestFile('text');
494

    
495
    // Enable the private file system.
496
    variable_set('file_private_path', $this->private_files_directory);
497

    
498
    // Step 1: Upload a basic document file.
499
    $edit = array();
500
    $edit['files[upload]'] = drupal_realpath($test_file->uri);
501
    $this->drupalPost('file/add', $edit, t('Next'));
502

    
503
    // Step 3: Scheme selection.
504
    $edit = array();
505
    $edit['scheme'] = 'private';
506
    $this->drupalPost(NULL, $edit, t('Next'));
507

    
508
    // Check that the file exists in the database.
509
    $fid = $this->getLastFileId();
510
    $file = file_load($fid);
511
    $this->assertTrue($file, t('File found in database.'));
512

    
513
    // Check that the document file has been uploaded.
514
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Document', '%name' => $file->filename)), t('Document file uploaded.'));
515
  }
516

    
517
  /**
518
   * Test the file upload wizard field step.
519
   */
520
  function testFileEntityUploadWizardFields() {
521
    $test_file = $this->getTestFile('image');
522
    $filename = $this->randomName();
523
    $alt = $this->randomName();
524
    $title = $this->randomName();
525

    
526
    // Step 1: Upload a basic image file.
527
    $edit = array();
528
    $edit['files[upload]'] = drupal_realpath($test_file->uri);
529
    $this->drupalPost('file/add', $edit, t('Next'));
530

    
531
    // Step 4: Attached fields.
532
    $edit = array();
533
    $edit['filename'] = $filename;
534
    $edit['field_file_image_alt_text[' . LANGUAGE_NONE . '][0][value]'] = $alt;
535
    $edit['field_file_image_title_text[' . LANGUAGE_NONE . '][0][value]'] = $title;
536
    $this->drupalPost(NULL, $edit, t('Save'));
537

    
538
    // Check that the file exists in the database.
539
    $fid = $this->getLastFileId();
540
    $file = file_load($fid);
541
    $this->assertTrue($file, t('File found in database.'));
542

    
543
    // Check that the image file has been uploaded.
544
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Image', '%name' => $filename)), t('Image file uploaded.'));
545

    
546
    // Check that the alt and title text was loaded from the fields.
547
    $this->assertEqual($file->alt, $alt, t('Alt text was stored as file metadata.'));
548
    $this->assertEqual($file->title, $title, t('Title text was stored as file metadata.'));
549
  }
550

    
551
  /**
552
   * Test skipping each of the file upload wizard steps.
553
   */
554
  function testFileEntityUploadWizardStepSkipping() {
555
    $test_file = $this->getTestFile('image');
556
    $filename = $this->randomName();
557

    
558
    // Ensure that the file is affected by every step.
559
    variable_set('file_private_path', $this->private_files_directory);
560

    
561
    $this->createFileType(array('type' => 'image1', 'label' => 'Image 1', 'mimetypes' => array('image/jpeg', 'image/gif', 'image/png', 'image/tiff')));
562
    $this->createFileType(array('type' => 'image2', 'label' => 'Image 2', 'mimetypes' => array('image/jpeg', 'image/gif', 'image/png', 'image/tiff')));
563

    
564
    $field_name = drupal_strtolower($this->randomName() . '_field_name');
565
    $field = array('field_name' => $field_name, 'type' => 'text');
566
    field_create_field($field);
567
    $instance = array(
568
      'field_name' => $field_name,
569
      'entity_type' => 'file',
570
      'bundle' => 'image2',
571
      'label' => $this->randomName() . '_label',
572
    );
573
    field_create_instance($instance);
574

    
575
    // Test skipping each upload wizard step.
576
    foreach (array('types', 'schemes', 'fields') as $step) {
577
      // Step to skip.
578
      switch ($step) {
579
        case 'types':
580
          variable_set('file_entity_file_upload_wizard_skip_file_type', TRUE);
581
          break;
582
        case 'schemes':
583
          variable_set('file_entity_file_upload_wizard_skip_scheme', TRUE);
584
          break;
585
        case 'fields':
586
          variable_set('file_entity_file_upload_wizard_skip_fields', TRUE);
587
          break;
588
      }
589

    
590
      // Step 1: Upload a basic image file.
591
      $edit = array();
592
      $edit['files[upload]'] = drupal_realpath($test_file->uri);
593
      $this->drupalPost('file/add', $edit, t('Next'));
594

    
595
      // Step 2: File type selection.
596
      if ($step != 'types') {
597
        $edit = array();
598
        $edit['type'] = 'image2';
599
        $this->drupalPost(NULL, $edit, t('Next'));
600
      }
601

    
602
      // Step 3: Scheme selection.
603
      if ($step != 'schemes') {
604
        $edit = array();
605
        $edit['scheme'] = 'private';
606
        $this->drupalPost(NULL, $edit, t('Next'));
607
      }
608

    
609
      // Step 4: Attached fields.
610
      if ($step != 'fields') {
611
        // Skipping file type selection essentially skips this step as well
612
        // because the file will not be assigned a type so no fields will be
613
        // available.
614
        if ($step != 'types') {
615
          $edit = array();
616
          $edit['filename'] = $filename;
617
          $edit[$field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName();
618
          $this->drupalPost(NULL, $edit, t('Save'));
619
        }
620
      }
621

    
622
      // Check that the file exists in the database.
623
      $fid = $this->getLastFileId();
624
      $file = file_load($fid);
625
      $this->assertTrue($file, t('File found in database.'));
626

    
627
      // Determine the file's file type.
628
      $type = file_type_load($file->type);
629

    
630
      // Check that the image file has been uploaded.
631
      $this->assertRaw(t('!type %name was uploaded.', array('!type' => $type->label, '%name' => $file->filename)), t('Image file uploaded.'));
632

    
633
      // Reset 'skip' variables.
634
      variable_del('file_entity_file_upload_wizard_skip_file_type');
635
      variable_del('file_entity_file_upload_wizard_skip_scheme');
636
      variable_del('file_entity_file_upload_wizard_skip_fields');
637
    }
638
  }
639
}
640

    
641
/**
642
 * Test file administration page functionality.
643
 */
644
class FileEntityAdminTestCase extends FileEntityTestHelper {
645
  public static function getInfo() {
646
    return array(
647
      'name' => 'File administration',
648
      'description' => 'Test file administration page functionality.',
649
      'group' => 'File entity',
650
    );
651
  }
652

    
653
  function setUp() {
654
    parent::setUp();
655

    
656
    // Remove the "view files" permission which is set
657
    // by default for all users so we can test this permission
658
    // correctly.
659
    $roles = user_roles();
660
    foreach ($roles as $rid => $role) {
661
      user_role_revoke_permissions($rid, array('view files'));
662
    }
663

    
664
    $this->admin_user = $this->drupalCreateUser(array('administer files', 'bypass file access'));
665
    $this->base_user_1 = $this->drupalCreateUser(array('administer files'));
666
    $this->base_user_2 = $this->drupalCreateUser(array('administer files', 'view own private files'));
667
    $this->base_user_3 = $this->drupalCreateUser(array('administer files', 'view private files'));
668
    $this->base_user_4 = $this->drupalCreateUser(array('administer files', 'edit any document files', 'delete any document files', 'edit any image files', 'delete any image files'));
669
  }
670

    
671
  /**
672
   * Tests that the table sorting works on the files admin pages.
673
   */
674
  function testFilesAdminSort() {
675
    $this->drupalLogin($this->admin_user);
676
    $i = 0;
677
    foreach (array('dd', 'aa', 'DD', 'bb', 'cc', 'CC', 'AA', 'BB') as $prefix) {
678
      $this->createFileEntity(array('filepath' => $prefix . $this->randomName(6), 'timestamp' => $i));
679
      $i++;
680
    }
681

    
682
    // Test that the default sort by file_managed.timestamp DESC actually fires properly.
683
    $files_query = db_select('file_managed', 'fm')
684
      ->fields('fm', array('fid'))
685
      ->orderBy('timestamp', 'DESC')
686
      ->execute()
687
      ->fetchCol();
688

    
689
    $files_form = array();
690
    $this->drupalGet('admin/content/file');
691
    foreach ($this->xpath('//table/tbody/tr/td/div/input/@value') as $input) {
692
      $files_form[] = $input;
693
    }
694
    $this->assertEqual($files_query, $files_form, 'Files are sorted in the form according to the default query.');
695

    
696
    // Compare the rendered HTML node list to a query for the files ordered by
697
    // filename to account for possible database-dependent sort order.
698
    $files_query = db_select('file_managed', 'fm')
699
      ->fields('fm', array('fid'))
700
      ->orderBy('filename')
701
      ->execute()
702
      ->fetchCol();
703

    
704
    $files_form = array();
705
    $this->drupalGet('admin/content/file', array('query' => array('sort' => 'asc', 'order' => 'Title')));
706
    foreach ($this->xpath('//table/tbody/tr/td/div/input/@value') as $input) {
707
      $files_form[] = $input;
708
    }
709
    $this->assertEqual($files_query, $files_form, 'Files are sorted in the form the same as they are in the query.');
710
  }
711

    
712
  /**
713
   * Tests files overview with different user permissions.
714
   */
715
  function testFilesAdminPages() {
716
    $this->drupalLogin($this->admin_user);
717

    
718
    $files['public_image'] = $this->createFileEntity(array('scheme' => 'public', 'uid' => $this->base_user_1->uid, 'type' => 'image'));
719
    $files['public_document'] = $this->createFileEntity(array('scheme' => 'public', 'uid' => $this->base_user_2->uid, 'type' => 'document'));
720
    $files['private_image'] = $this->createFileEntity(array('scheme' => 'private', 'uid' => $this->base_user_1->uid, 'type' => 'image'));
721
    $files['private_document'] = $this->createFileEntity(array('scheme' => 'private', 'uid' => $this->base_user_2->uid, 'type' => 'document'));
722

    
723
    // Verify view, usage, edit, and delete links for any file.
724
    $this->drupalGet('admin/content/file');
725
    $this->assertResponse(200);
726
    foreach ($files as $file) {
727
      $this->assertLinkByHref('file/' . $file->fid);
728
      $this->assertLinkByHref('file/' . $file->fid . '/usage');
729
      $this->assertLinkByHref('file/' . $file->fid . '/edit');
730
      $this->assertLinkByHref('file/' . $file->fid . '/delete');
731
      // Verify tableselect.
732
      $this->assertFieldByName('files[' . $file->fid . ']', '', t('Tableselect found.'));
733
    }
734

    
735
    // Verify no operation links are displayed for regular users.
736
    $this->drupalLogout();
737
    $this->drupalLogin($this->base_user_1);
738
    $this->drupalGet('admin/content/file');
739
    $this->assertResponse(200);
740
    $this->assertLinkByHref('file/' . $files['public_image']->fid);
741
    $this->assertLinkByHref('file/' . $files['public_document']->fid);
742
    $this->assertNoLinkByHref('file/' . $files['public_image']->fid . '/edit');
743
    $this->assertNoLinkByHref('file/' . $files['public_image']->fid . '/delete');
744
    $this->assertNoLinkByHref('file/' . $files['public_document']->fid . '/edit');
745
    $this->assertNoLinkByHref('file/' . $files['public_document']->fid . '/delete');
746

    
747
    // Verify no tableselect.
748
    $this->assertNoFieldByName('files[' . $files['public_image']->fid . ']', '', t('No tableselect found.'));
749

    
750
    // Verify private file is displayed with permission.
751
    $this->drupalLogout();
752
    $this->drupalLogin($this->base_user_2);
753
    $this->drupalGet('admin/content/file');
754
    $this->assertResponse(200);
755
    $this->assertLinkByHref('file/' . $files['private_document']->fid);
756
    // Verify no operation links are displayed.
757
    $this->assertNoLinkByHref('file/' . $files['private_document']->fid . '/edit');
758
    $this->assertNoLinkByHref('file/' . $files['private_document']->fid . '/delete');
759

    
760
    // Verify user cannot see private file of other users.
761
    $this->assertNoLinkByHref('file/' . $files['private_image']->fid);
762
    $this->assertNoLinkByHref('file/' . $files['private_image']->fid . '/edit');
763
    $this->assertNoLinkByHref('file/' . $files['private_image']->fid . '/delete');
764

    
765
    // Verify no tableselect.
766
    $this->assertNoFieldByName('files[' . $files['private_document']->fid . ']', '', t('No tableselect found.'));
767

    
768
    // Verify private file is displayed with permission.
769
    $this->drupalLogout();
770
    $this->drupalLogin($this->base_user_3);
771
    $this->drupalGet('admin/content/file');
772
    $this->assertResponse(200);
773

    
774
    // Verify user can see private file of other users.
775
    $this->assertLinkByHref('file/' . $files['private_document']->fid);
776
    $this->assertLinkByHref('file/' . $files['private_image']->fid);
777

    
778
    // Verify operation links are displayed for users with appropriate permission.
779
    $this->drupalLogout();
780
    $this->drupalLogin($this->base_user_4);
781
    $this->drupalGet('admin/content/file');
782
    $this->assertResponse(200);
783
    foreach ($files as $file) {
784
      $this->assertLinkByHref('file/' . $file->fid);
785
      $this->assertLinkByHref('file/' . $file->fid . '/usage');
786
      $this->assertLinkByHref('file/' . $file->fid . '/edit');
787
      $this->assertLinkByHref('file/' . $file->fid . '/delete');
788
    }
789

    
790
    // Verify file access can be bypassed.
791
    $this->drupalLogout();
792
    $this->drupalLogin($this->admin_user);
793
    $this->drupalGet('admin/content/file');
794
    $this->assertResponse(200);
795
    foreach ($files as $file) {
796
      $this->assertLinkByHref('file/' . $file->fid);
797
      $this->assertLinkByHref('file/' . $file->fid . '/usage');
798
      $this->assertLinkByHref('file/' . $file->fid . '/edit');
799
      $this->assertLinkByHref('file/' . $file->fid . '/delete');
800
    }
801
  }
802
}
803

    
804
/**
805
 * Tests the file usage page.
806
 */
807
class FileEntityUsageTestCase extends FileEntityTestHelper {
808
  public static function getInfo() {
809
    return array(
810
      'name' => 'File entity usage',
811
      'description' => 'Create a file and verify its usage.',
812
      'group' => 'File entity',
813
    );
814
  }
815

    
816
  function setUp() {
817
    parent::setUp();
818

    
819
    $web_user = $this->drupalCreateUser(array('create files', 'bypass file access', 'edit own article content'));
820
    $this->drupalLogin($web_user);
821
  }
822

    
823
  /**
824
   * Create a file and verify its usage information.
825
   */
826
  function testFileEntityUsagePage() {
827
    $image_field = 'field_image';
828
    $image = $this->getTestFile('image');
829

    
830
    // Create a node, save it, then edit it to upload a file.
831
    $edit = array(
832
      "files[" . $image_field . "_" . LANGUAGE_NONE . "_0]" => drupal_realpath($image->uri),
833
    );
834
    $node = $this->drupalCreateNode(array('type' => 'article'));
835
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
836

    
837
    // Load the uploaded file.
838
    $fid = $this->getLastFileId();
839
    $file = file_load($fid);
840

    
841
    // View the file's usage page.
842
    $this->drupalGet('file/' . $file->fid . '/usage');
843

    
844
    // Verify that a link to the entity is available.
845
    $this->assertLink($node->title);
846
    $this->assertLinkByHref('node/' . $node->nid);
847

    
848
    // Verify that the entity type and use count information is also present.
849
    $expected_values = array(
850
      'type' => 'node',
851
      'count' => 1,
852
    );
853
    foreach ($expected_values as $name => $value) {
854
      $this->assertTrue($this->xpath('//table/tbody/tr/td[normalize-space(text())=:text]', array(':text' => $value)), t('File usage @name was found in the table.', array('@name' => $name)));
855
    }
856

    
857
    // Add a reference to the file from the same entity but registered by a
858
    // different module to ensure that the usage count is incremented and no
859
    // additional table rows are created.
860
    file_usage_add($file, 'example_module', 'node', $node->nid, 2);
861

    
862
    // Reload the page and verify that the expected values are present.
863
    $this->drupalGet('file/' . $file->fid . '/usage');
864
    $expected_values['count'] = 3;
865
    foreach ($expected_values as $name => $value) {
866
      $this->assertTrue($this->xpath('//table/tbody/tr/td[normalize-space(text())=:text]', array(':text' => $value)), t('File usage @name was found in the table.', array('@name' => $name)));
867
    }
868

    
869
    // Add a reference to the file from an entity that doesn't exist to ensure
870
    // that this case is handled.
871
    file_usage_add($file, 'test_module', 'imaginary', 1);
872

    
873
    // Reload the page.
874
    $this->drupalGet('file/' . $file->fid . '/usage');
875

    
876
    // Verify that the module name is used in place of a link to the entity.
877
    $this->assertNoLink('test_module');
878
    $this->assertRaw('test_module', 'Module name used in place of link to the entity.');
879

    
880
    // Verify that the entity type and use count information is also present.
881
    $expected_values = array(
882
      'type' => 'imaginary',
883
      'count' => 1,
884
    );
885
    foreach ($expected_values as $name => $value) {
886
      $this->assertTrue($this->xpath('//table/tbody/tr/td[normalize-space(text())=:text]', array(':text' => $value)), t('File usage @name was found in the table.', array('@name' => $name)));
887
    }
888
  }
889
}
890

    
891
/**
892
 * Tests image alt and title text.
893
 */
894
class FileEntityAltTitleTestCase extends FileEntityTestHelper {
895
  public static function getInfo() {
896
    return array(
897
      'name' => 'File entity alt and title text',
898
      'description' => 'Create an image file with alt and title text.',
899
      'group' => 'File entity',
900
      'dependencies' => array('token'),
901
    );
902
  }
903

    
904
  function setUp() {
905
    parent::setUp('token');
906

    
907
    $web_user = $this->drupalCreateUser(array('create files', 'edit own image files'));
908
    $this->drupalLogin($web_user);
909
  }
910

    
911
  /**
912
   * Create an "image" file and verify its associated alt and title text.
913
   */
914
  function testFileEntityAltTitle() {
915
    $test_file = $this->getTestFile('image');
916
    $alt_field_name = 'field_file_image_alt_text'; // Name of the default alt text field added to the image file type.
917
    $title_field_name = 'field_file_image_title_text'; // Name of the default title text field added to the image file type.
918

    
919
    // Create a file.
920
    $edit = array();
921
    $edit['files[upload]'] = drupal_realpath($test_file->uri);
922
    $this->drupalPost('file/add', $edit, t('Next'));
923

    
924
    // Step 2: Scheme selection.
925
    if ($this->xpath('//input[@name="scheme"]')) {
926
      $this->drupalPost(NULL, array(), t('Next'));
927
    }
928

    
929
    // Step 3: Attached fields.
930
    $alt = 'Quote&quot; Amp&amp; ' . 'Файл для тестирования ' . $this->randomName(); // Generate alt text containing HTML entities, spaces and non-latin characters.
931
    $title = 'Quote&quot; Amp&amp; ' . 'Файл для тестирования ' . $this->randomName(); // Generate title text containing HTML entities, spaces and non-latin characters.
932

    
933
    $edit = array();
934
    $edit[$alt_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $alt;
935
    $edit[$title_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $title;
936
    $this->drupalPost(NULL, $edit, t('Save'));
937

    
938
    // Check that the image file has been uploaded.
939
    $this->assertRaw(t('!type %name was uploaded.', array('!type' => 'Image', '%name' => $test_file->filename)), t('Image file uploaded.'));
940

    
941
    // Check that the file exists in the database.
942
    $file = $this->getFileByFilename($test_file->filename);
943
    $this->assertTrue($file, t('File found in database.'));
944

    
945
    // Check that the alt and title text was loaded from the fields.
946
    $this->assertEqual($file->alt, $alt, t('Alt text was stored as file metadata.'));
947
    $this->assertEqual($file->title, $title, t('Title text was stored as file metadata.'));
948

    
949
    // Verify that the alt and title text is present on the page.
950
    $image_info = array(
951
      'path' => $file->uri,
952
      'alt' => $alt,
953
      'title' => $title,
954
      'width' => $file->width,
955
      'height' => $file->height,
956
    );
957
    $default_output = theme('image', $image_info);
958
    $this->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');
959

    
960
    // Verify that the alt and title text can be edited.
961
    $new_alt = $this->randomName();
962
    $new_title = $this->randomName();
963

    
964
    $edit = array();
965
    $edit[$alt_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $new_alt;
966
    $edit[$title_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $new_title;
967
    $this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
968

    
969
    $image_info = array(
970
      'path' => $file->uri,
971
      'alt' => $new_alt,
972
      'title' => $new_title,
973
      'width' => $file->width,
974
      'height' => $file->height,
975
    );
976
    $default_output = theme('image', $image_info);
977
    $this->assertRaw($default_output, 'Image displayed using updated alt and title attributes.');
978
  }
979
}
980

    
981
/**
982
 * Tests replacing the file associated with a file entity.
983
 */
984
class FileEntityReplaceTestCase extends FileEntityTestHelper {
985
  public static function getInfo() {
986
    return array(
987
      'name' => 'File replacement',
988
      'description' => 'Test file replace functionality.',
989
      'group' => 'File entity',
990
    );
991
  }
992

    
993
  function setUp() {
994
    parent::setUp();
995
  }
996

    
997
  /**
998
   * @todo Test image dimensions for an image field are reset when a file is replaced.
999
   * @todo Test image styles are cleared when an image is updated.
1000
   */
1001
  function testReplaceFile() {
1002
    // Select the first text test file to use.
1003
    $file = $this->createFileEntity(array('type' => 'document'));
1004

    
1005
    // Create a user with file edit permissions.
1006
    $user = $this->drupalCreateUser(array('edit any document files'));
1007
    $this->drupalLogin($user);
1008

    
1009
    // Test that the Upload widget appears for a local file.
1010
    $this->drupalGet('file/' . $file->fid . '/edit');
1011
    $this->assertFieldByName('files[replace_upload]');
1012

    
1013
    // Test that file saves without uploading a file.
1014
    $this->drupalPost(NULL, array(), t('Save'));
1015
    $this->assertText(t('Document @file has been updated.', array('@file' => $file->filename)), 'File was updated without file upload.');
1016

    
1017
    // Get a text file to use as a replacement.
1018
    $original = clone $file;
1019
    $replacement = $this->getTestFile('text');
1020

    
1021
    // Test that the file saves when uploading a replacement file.
1022
    $edit = array();
1023
    $edit['files[replace_upload]'] = drupal_realpath($replacement->uri);
1024
    $this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
1025
    $this->assertText(t('Document @file has been updated.', array('@file' => $file->filename)), 'File was updated with file upload.');
1026

    
1027
    // Re-load the file from the database.
1028
    $file = file_load($file->fid);
1029

    
1030
    // Test how file properties changed after the file has been replaced.
1031
    $this->assertEqual($file->filename, $original->filename, 'Updated file name did not change.');
1032
    $this->assertNotEqual($file->filesize, $original->filesize, 'Updated file size changed from previous file.');
1033
    $this->assertEqual($file->filesize, $replacement->filesize, 'Updated file size matches uploaded file.');
1034
    $this->assertEqual(file_get_contents($file->uri), file_get_contents($replacement->uri), 'Updated file contents matches uploaded file.');
1035
    $this->assertFalse(entity_load('file', FALSE, array('status' => 0)), 'Temporary file used for replacement was deleted.');
1036

    
1037
    // Get an image file.
1038
    $image = $this->getTestFile('image');
1039
    $edit['files[replace_upload]'] = drupal_realpath($image->uri);
1040

    
1041
    // Test that validation works by uploading a non-text file as a replacement.
1042
    $this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
1043
    $this->assertRaw(t('The specified file %file could not be uploaded. Only files with the following extensions are allowed:', array('%file' => $image->filename)), 'File validation works, upload failed correctly.');
1044

    
1045
    // Create a non-local file record.
1046
    $file2 = new stdClass();
1047
    $file2->uri = 'oembed://' . $this->randomName();
1048
    $file2->filename = drupal_basename($file2->uri);
1049
    $file2->filemime = 'image/oembed';
1050
    $file2->type = 'image';
1051
    $file2->uid = 1;
1052
    $file2->timestamp = REQUEST_TIME;
1053
    $file2->filesize = 0;
1054
    $file2->status = 0;
1055
    // Write the record directly rather than calling file_save() so we don't
1056
    // invoke the hooks.
1057
    $this->assertTrue(drupal_write_record('file_managed', $file2), 'Non-local file was added to the database.');
1058

    
1059
    // Test that Upload widget does not appear for non-local file.
1060
    $this->drupalGet('file/' . $file2->fid . '/edit');
1061
    $this->assertNoFieldByName('files[replace_upload]');
1062
  }
1063
}
1064

    
1065
/**
1066
 * Tests file entity tokens.
1067
 */
1068
class FileEntityTokenTestCase extends FileEntityTestHelper {
1069
  public static function getInfo() {
1070
    return array(
1071
      'name' => 'File entity tokens',
1072
      'description' => 'Test the file entity tokens.',
1073
      'group' => 'File entity',
1074
    );
1075
  }
1076

    
1077
  function setUp() {
1078
    parent::setUp();
1079
  }
1080

    
1081
  function testFileEntityTokens() {
1082
    $file = $this->createFileEntity(array('type' => 'document'));
1083
    $tokens = array(
1084
      'type' => 'Document',
1085
      'type:name' => 'Document',
1086
      'type:machine-name' => 'document',
1087
      'type:count' => 1,
1088
    );
1089
    $this->assertTokens('file', array('file' => $file), $tokens);
1090

    
1091
    $file = $this->createFileEntity(array('type' => 'image'));
1092
    $tokens = array(
1093
      'type' => 'Image',
1094
      'type:name' => 'Image',
1095
      'type:machine-name' => 'image',
1096
      'type:count' => 1,
1097
    );
1098
    $this->assertTokens('file', array('file' => $file), $tokens);
1099
  }
1100

    
1101
  function assertTokens($type, array $data, array $tokens, array $options = array()) {
1102
    $token_input = drupal_map_assoc(array_keys($tokens));
1103
    $values = token_generate($type, $token_input, $data, $options);
1104
    foreach ($tokens as $token => $expected) {
1105
      if (!isset($expected)) {
1106
        $this->assertTrue(!isset($values[$token]), t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
1107
      }
1108
      elseif (!isset($values[$token])) {
1109
        $this->fail(t("Token value for [@type:@token] was not generated.", array('@type' => $type, '@token' => $token)));
1110
      }
1111
      elseif (!empty($options['regex'])) {
1112
        $this->assertTrue(preg_match('/^' . $expected . '$/', $values[$token]), t("Token value for [@type:@token] was '@actual', matching regular expression pattern '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
1113
      }
1114
      else {
1115
        $this->assertIdentical($values[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $values[$token], '@expected' => $expected)));
1116
      }
1117
    }
1118

    
1119
    return $values;
1120
  }
1121
}
1122

    
1123
/**
1124
 * Tests adding support for bundles to the core 'file' entity.
1125
 */
1126
class FileEntityTypeTestCase extends FileEntityTestHelper {
1127
  public static function getInfo() {
1128
    return array(
1129
      'name' => 'File entity types',
1130
      'description' => 'Test the file entity types.',
1131
      'group' => 'File entity',
1132
    );
1133
  }
1134

    
1135
  function setUp() {
1136
    parent::setUp();
1137
  }
1138

    
1139
  /**
1140
   * Test admin pages access and functionality.
1141
   */
1142
  function testAdminPages() {
1143
    // Create a user with file type administration access.
1144
    $user = $this->drupalCreateUser(array('administer file types'));
1145
    $this->drupalLogin($user);
1146

    
1147
    $this->drupalGet('admin/structure/file-types');
1148
    $this->assertResponse(200, 'File types admin page is accessible');
1149
  }
1150

    
1151
  /**
1152
   * Test creating a new type. Basic CRUD.
1153
   */
1154
  function testCreate() {
1155
    $type_machine_type = 'foo';
1156
    $type_machine_label = 'foobar';
1157
    $type = $this->createFileType(array('type' => $type_machine_type, 'label' => $type_machine_label));
1158
    $loaded_type = file_type_load($type_machine_type);
1159
    $this->assertEqual($loaded_type->label, $type_machine_label, "Was able to create a type and retreive it.");
1160
  }
1161

    
1162
  /**
1163
   * Test file types CRUD UI.
1164
   */
1165
  function testTypesCrudUi() {
1166
    $this->drupalGet('admin/structure/file-types');
1167
    $this->assertResponse(403, 'File types UI page is not accessible to unauthorized users.');
1168

    
1169
    $user = $this->drupalCreateUser(array('administer file types'));
1170
    $this->drupalLogin($user);
1171

    
1172
    $this->drupalGet('admin/structure/file-types');
1173
    $this->assertResponse(200, 'File types UI page is accessible to users with adequate permission.');
1174

    
1175
    // Create new file type.
1176
    $edit = array(
1177
      'label' => t('Test type'),
1178
      'type' => 'test_type',
1179
      'description' => t('This is dummy file type used just for testing.'),
1180
      'mimetypes' => 'image/png',
1181
    );
1182
    $this->drupalGet('admin/structure/file-types/add');
1183
    $this->drupalPost(NULL, $edit, t('Save'));
1184
    $this->assertText(t('The file type @type has been updated.', array('@type' => $edit['label'])), 'New file type successfully created.');
1185
    $this->assertText($edit['label'], 'New file type created: label found.');
1186
    $this->assertText($edit['description'], 'New file type created: description found.');
1187
    $this->assertFieldByXPath("//table//tr[1]//td[7]", t('Normal'), 'Newly created file type is stored in DB.');
1188
    $this->assertLink(t('disable'), 0, 'Able to disable newly created file type.');
1189
    $this->assertLink(t('delete'), 0, 'Able to delete newly created file type.');
1190
    $this->assertLinkByHref('admin/structure/file-types/manage/' . $edit['type'] . '/disable', 0, 'Disable link points to disable confirmation page.');
1191
    $this->assertLinkByHref('admin/structure/file-types/manage/' . $edit['type'] . '/delete', 0, 'Delete link points to delete confirmation page.');
1192

    
1193
    // Edit file type.
1194
    $this->drupalGet('admin/structure/file-types/manage/' . $edit['type'] . '/edit');
1195
    $this->assertRaw(t('Save'), 'Save button found on edit page.');
1196
    $this->assertRaw(t('Delete'), 'Delete button found on edit page.');
1197
    $this->assertRaw($edit['label'], 'Label found on file type edit page');
1198
    $this->assertText($edit['description'], 'Description found on file type edit page');
1199
    $this->assertText($edit['mimetypes'], 'Mime-type configuration found on file type edit page');
1200
    $this->assertText(t('Mimetype List'), 'Mimetype list present on edit form.');
1201

    
1202
    // Modify file type.
1203
    $edit['label'] = t('New type label');
1204
    $this->drupalPost(NULL, array('label' => $edit['label']), t('Save'));
1205
    $this->assertText(t('The file type @type has been updated.', array('@type' => $edit['label'])), 'File type was modified.');
1206
    $this->assertText($edit['label'], 'Modified label found on file types list.');
1207

    
1208
    // Disable and re-enable file type.
1209
    $this->drupalGet('admin/structure/file-types/manage/' . $edit['type'] . '/disable');
1210
    $this->assertText(t('Are you sure you want to disable the file type @type?', array('@type' => $edit['label'])), 'Disable confirmation page found.');
1211
    $this->drupalPost(NULL, array(), t('Disable'));
1212
    $this->assertText(t('The file type @type has been disabled.', array('@type' => $edit['label'])), 'Disable confirmation message found.');
1213
    $this->assertFieldByXPath("//table//tr[5]//td[1]", $edit['label'], 'Disabled type moved to the tail of the list.');
1214
    $this->assertLink(t('enable'), 0, 'Able to re-enable newly created file type.');
1215
    $this->assertLinkByHref('admin/structure/file-types/manage/' . $edit['type'] . '/enable', 0, 'Enable link points to enable confirmation page.');
1216
    $this->drupalGet('admin/structure/file-types/manage/' . $edit['type'] . '/enable');
1217
    $this->assertText(t('Are you sure you want to enable the file type @type?', array('@type' => $edit['label'])), 'Enable confirmation page found.');
1218
    $this->drupalPost(NULL, array(), t('Enable'));
1219
    $this->assertText(t('The file type @type has been enabled.', array('@type' => $edit['label'])), 'Enable confirmation message found.');
1220
    $this->assertFieldByXPath("//table//tr[1]//td[1]", $edit['label'], 'Enabled type moved to the top of the list.');
1221

    
1222
    // Delete newly created type.
1223
    $this->drupalGet('admin/structure/file-types/manage/' . $edit['type'] . '/delete');
1224
    $this->assertText(t('Are you sure you want to delete the file type @type?', array('@type' => $edit['label'])), 'Delete confirmation page found.');
1225
    $this->drupalPost(NULL, array(), t('Delete'));
1226
    $this->assertText(t('The file type @type has been deleted.', array('@type' => $edit['label'])), 'Delete confirmation message found.');
1227
    $this->drupalGet('admin/structure/file-types');
1228
    $this->assertNoText($edit['label'], 'File type successfully deleted.');
1229

    
1230
    // Edit exported file type.
1231
    $this->drupalGet('admin/structure/file-types/manage/image/edit');
1232
    $this->assertRaw(t('Image'), 'Label found on file type edit page');
1233
    $this->assertText("image/*", 'Mime-type configuration found on file type edit page');
1234
    $this->drupalPost(NULL, array('label' => t('Funky images')), t('Save'));
1235
    $this->assertText(t('The file type @type has been updated.', array('@type' => t('Funky images'))), 'File type was modified.');
1236
    $this->assertText(t('Funky image'), 'Modified label found on file types list.');
1237
    $this->assertFieldByXPath("//table//tr[1]//td[7]", t('Overridden'), 'Modified type overrides configuration from code.');
1238
    $this->assertLink(t('revert'), 0, 'Able to revert overridden file type.');
1239
    $this->assertLinkByHref('admin/structure/file-types/manage/image/revert', 0, 'Revert link points to revert confirmation page.');
1240

    
1241
    // Revert file type.
1242
    $this->drupalGet('admin/structure/file-types/manage/image/revert');
1243
    $this->assertText(t('Are you sure you want to revert the file type @type?', array('@type' => t('Funky images'))), 'Revert confirmation page found.');
1244
    $this->drupalPost(NULL, array(), t('Revert'));
1245
    $this->assertText(t('The file type @type has been reverted.', array('@type' => t('Funky images'))), 'Revert confirmation message found.');
1246
    $this->assertText(t('Image'), 'Reverted file type found in list.');
1247
    $this->assertFieldByXPath("//table//tr[1]//td[7]", t('Default'), 'Reverted file type shows correct state.');
1248
  }
1249
}
1250

    
1251
/**
1252
 * Tests the file entity access API.
1253
 */
1254
class FileEntityAccessTestCase extends FileEntityTestHelper {
1255

    
1256
  public static function getInfo() {
1257
    return array(
1258
      'name' => 'File entity access',
1259
      'description' => 'Test the access aspects of file entity.',
1260
      'group' => 'File entity',
1261
    );
1262
  }
1263

    
1264
  function setUp() {
1265
    parent::setUp();
1266

    
1267
    // Remove the "view files" permission which is set by default for all users
1268
    // so we can test this permission correctly.
1269
    $roles = user_roles();
1270
    foreach ($roles as $rid => $role) {
1271
      user_role_revoke_permissions($rid, array('view files'));
1272
    }
1273
  }
1274

    
1275
  /**
1276
   * Runs basic tests for file_entity_access function.
1277
   */
1278
  function testFileEntityAccess() {
1279
    $file = $this->createFileEntity(array('type' => 'image'));
1280

    
1281
    // Ensures user with 'bypass file access' permission can do everything.
1282
    $web_user = $this->drupalCreateUser(array('bypass file access'));
1283
    $this->assertFileEntityAccess(array('create' => TRUE), NULL, $web_user);
1284
    $this->assertFileEntityAccess(array('view' => TRUE, 'download' => TRUE, 'update' => TRUE, 'delete' => TRUE), $file, $web_user);
1285

    
1286
    // A user with 'administer files' should not access CRUD operations.
1287
    $web_user = $this->drupalCreateUser(array('administer files'));
1288
    $this->assertFileEntityAccess(array('view' => FALSE, 'download' => FALSE, 'update' => FALSE, 'delete' => FALSE), $file, $web_user);
1289

    
1290
    // User cannot 'view files'.
1291
    $web_user = $this->drupalCreateUser(array('create files'));
1292
    $this->assertFileEntityAccess(array('view' => FALSE), $file, $web_user);
1293
    // But can upload new ones.
1294
    $this->assertFileEntityAccess(array('create' => TRUE), NULL, $web_user);
1295

    
1296
    // User can view own files but no other files.
1297
    $web_user = $this->drupalCreateUser(array('create files', 'view own files'));
1298
    $this->assertFileEntityAccess(array('view' => FALSE), $file, $web_user);
1299
    $file->uid = $web_user->uid;
1300
    $this->assertFileEntityAccess(array('view' => TRUE), $file, $web_user);
1301

    
1302
    // User can download own files but no other files.
1303
    $web_user = $this->drupalCreateUser(array('create files', 'download own image files'));
1304
    $this->assertFileEntityAccess(array('download' => FALSE), $file, $web_user);
1305
    $file->uid = $web_user->uid;
1306
    $this->assertFileEntityAccess(array('download' => TRUE), $file, $web_user);
1307

    
1308
    // User can update own files but no other files.
1309
    $web_user = $this->drupalCreateUser(array('create files', 'view own files', 'edit own image files'));
1310
    $this->assertFileEntityAccess(array('update' => FALSE), $file, $web_user);
1311
    $file->uid = $web_user->uid;
1312
    $this->assertFileEntityAccess(array('update' => TRUE), $file, $web_user);
1313

    
1314
    // User can delete own files but no other files.
1315
    $web_user = $this->drupalCreateUser(array('create files', 'view own files', 'edit own image files', 'delete own image files'));
1316
    $this->assertFileEntityAccess(array('delete' => FALSE), $file, $web_user);
1317
    $file->uid = $web_user->uid;
1318
    $this->assertFileEntityAccess(array('delete' => TRUE), $file, $web_user);
1319

    
1320
    // User can view any file.
1321
    $web_user = $this->drupalCreateUser(array('create files', 'view files'));
1322
    $this->assertFileEntityAccess(array('view' => TRUE), $file, $web_user);
1323

    
1324
    // User can download any file.
1325
    $web_user = $this->drupalCreateUser(array('create files', 'download any image files'));
1326
    $this->assertFileEntityAccess(array('download' => TRUE), $file, $web_user);
1327

    
1328
    // User can edit any file.
1329
    $web_user = $this->drupalCreateUser(array('create files', 'view files', 'edit any image files'));
1330
    $this->assertFileEntityAccess(array('update' => TRUE), $file, $web_user);
1331

    
1332
    // User can delete any file.
1333
    $web_user = $this->drupalCreateUser(array('create files', 'view files', 'edit any image files', 'delete any image files'));
1334
    $this->assertFileEntityAccess(array('delete' => TRUE), $file, $web_user);
1335
  }
1336

    
1337
  /**
1338
   * Tests page access.
1339
   *
1340
   * Verifies the privileges required to access the following pages:
1341
   *  file/add
1342
   *  file/%/view
1343
   *  file/%/download
1344
   *  file/%/edit
1345
   *  file/%/usage
1346
   *  file/%/delete
1347
   */
1348
  function testFileEntityPageAccess() {
1349
    // Test creating files without permission.
1350
    $web_user = $this->drupalCreateUser();
1351
    $this->drupalLogin($web_user);
1352
    $this->drupalGet('file/add');
1353
    $this->assertResponse(403, 'Users without access can not access the file add page');
1354

    
1355
    // Test creating files with permission.
1356
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1357
      'create files' => TRUE,
1358
    ));
1359
    $this->drupalGet('file/add');
1360
    $this->assertResponse(200, 'Users with access can access the file add page');
1361

    
1362
    $file = $this->createFileEntity(array('type' => 'document','uid' => $web_user->uid));
1363

    
1364
    // Test viewing own files without permission.
1365
    $this->drupalGet("file/{$file->fid}/view");
1366
    $this->assertResponse(403, 'Users without access can not view their own files');
1367

    
1368
    // Test viewing own files with permission.
1369
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1370
      'view own files' => TRUE,
1371
    ));
1372
    $this->drupalGet("file/{$file->fid}/view");
1373
    $this->assertResponse(200, 'Users with access can view their own files');
1374

    
1375
    // Test viewing any files without permission.
1376
    $file->uid = 1;
1377
    file_save($file);
1378
    $this->drupalGet("file/{$file->fid}/view");
1379
    $this->assertResponse(403, 'Users with access can not view any file');
1380

    
1381
    // Test viewing any files with permission.
1382
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1383
      'view files' => TRUE,
1384
    ));
1385
    $this->drupalGet("file/{$file->fid}/view");
1386
    $this->assertResponse(200, 'Users with access can view any file');
1387

    
1388
    // Test downloading own files without permission.
1389
    $file->uid = $web_user->uid;
1390
    file_save($file);
1391
    $url = "file/{$file->fid}/download";
1392
    $this->drupalGet($url, array('query' => array('token' => file_entity_get_download_token($file))));
1393
    $this->assertResponse(403, 'Users without access can not download their own files');
1394

    
1395
    // Test downloading own files with permission.
1396
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1397
      'download own document files' => TRUE,
1398
    ));
1399
    $this->drupalGet($url, array('query' => array('token' => file_entity_get_download_token($file))));
1400
    $this->assertResponse(200, 'Users with access can download their own files');
1401

    
1402
    // Test downloading any files without permission.
1403
    $file->uid = 1;
1404
    file_save($file);
1405
    $url = "file/{$file->fid}/download";
1406
    $this->drupalGet($url, array('query' => array('token' => file_entity_get_download_token($file))));
1407
    $this->assertResponse(403, 'Users without access can not download any file');
1408

    
1409
    // Test downloading any files with permission.
1410
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1411
      'download any document files' => TRUE,
1412
    ));
1413
    $this->drupalGet($url, array('query' => array('token' => file_entity_get_download_token($file))));
1414
    $this->assertResponse(200, 'Users with access can download any file');
1415

    
1416
    // Test downloading files with an invalid token.
1417
    $this->drupalGet($url, array('query' => array('token' => 'invalid-token')));
1418
    $this->assertResponse(403, 'Cannot download file with an invalid token.');
1419

    
1420
    // Test downloading files without a token.
1421
    $this->drupalGet($url);
1422
    $this->assertResponse(403, 'Cannot download file without a token.');
1423
    variable_set('file_entity_allow_insecure_download', TRUE);
1424

    
1425
    // Test downloading files with permission but without a token when insecure
1426
    // downloads are enabled.
1427
    $this->drupalGet($url);
1428
    $this->assertResponse(200, 'Users with access can download the file without a token when file_entity_allow_insecure_download is set.');
1429

    
1430
    // Tests editing own files without permission.
1431
    $file->uid = $web_user->uid;
1432
    file_save($file);
1433
    $this->drupalGet("file/{$file->fid}/edit");
1434
    $this->assertResponse(403, 'Users without access can not edit own files');
1435

    
1436
    // Tests checking the usage of their own files without permission.
1437
    $this->drupalGet("file/{$file->fid}/usage");
1438
    $this->assertResponse(403, 'Users without access can not check the usage of their own files');
1439

    
1440
    // Tests editing own files with permission.
1441
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1442
      'edit own document files' => TRUE,
1443
    ));
1444
    $this->drupalGet("file/{$file->fid}/edit");
1445
    $this->assertResponse(200, 'Users with access can edit own files');
1446

    
1447
    // Tests checking the usage of their own files without permission.
1448
    $this->drupalGet("file/{$file->fid}/usage");
1449
    $this->assertResponse(200, 'Users with access can check the usage of their own files');
1450

    
1451
    // Tests editing any files without permission.
1452
    $file->uid = 1;
1453
    file_save($file);
1454
    $this->drupalGet("file/{$file->fid}/edit");
1455
    $this->assertResponse(403, 'Users without access can not edit any file');
1456

    
1457
    // Tests checking the usage of any files without permission.
1458
    $this->drupalGet("file/{$file->fid}/usage");
1459
    $this->assertResponse(403, 'Users without access can not check the usage of any file');
1460

    
1461
    // Tests editing any files with permission.
1462
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1463
      'edit any document files' => TRUE,
1464
    ));
1465
    $this->drupalGet("file/{$file->fid}/edit");
1466
    $this->assertResponse(200, 'Users with access can edit any file');
1467

    
1468
    // Tests checking the usage of any files with permission.
1469
    $this->drupalGet("file/{$file->fid}/usage");
1470
    $this->assertResponse(200, 'Users with access can check the usage of any file');
1471

    
1472
    // Tests deleting own files without permission.
1473
    $file->uid = $web_user->uid;
1474
    file_save($file);
1475
    $this->drupalGet("file/{$file->fid}/delete");
1476
    $this->assertResponse(403, 'Users without access can not delete their own files');
1477

    
1478
    // Tests deleting own files with permission.
1479
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1480
      'delete own document files' => TRUE,
1481
    ));
1482
    $this->drupalGet("file/{$file->fid}/delete");
1483
    $this->assertResponse(200, 'Users with access can delete their own files');
1484

    
1485
    // Tests deleting any files without permission.
1486
    $file->uid = 1;
1487
    file_save($file);
1488
    $this->drupalGet("file/{$file->fid}/delete");
1489
    $this->assertResponse(403, 'Users without access can not delete any file');
1490

    
1491
    // Tests deleting any files with permission.
1492
    user_role_change_permissions(DRUPAL_AUTHENTICATED_RID, array(
1493
      'delete any document files' => TRUE,
1494
    ));
1495
    $this->drupalGet("file/{$file->fid}/delete");
1496
    $this->assertResponse(200, 'Users with access can delete any file');
1497
  }
1498

    
1499
  /**
1500
   * Test to see if we have access to download private files when granted the permissions.
1501
   */
1502
  function testFileEntityPrivateDownloadAccess() {
1503
    foreach ($this->getPrivateDownloadAccessCases() as $case) {
1504
      // Create users and login only if non-anonymous.
1505
      $authenticated_user = !is_null($case['permissions']);
1506
      if ($authenticated_user) {
1507
        $account = $this->drupalCreateUser($case['permissions']);
1508
        $this->drupalLogin($account);
1509
      }
1510

    
1511
      // Create private, permanent files owned by this user only he's an owner.
1512
      if (!empty($case['owner'])) {
1513
        $file = $this->createFileEntity(array('type' => 'document', 'uid' => $account->uid, 'scheme' => 'private'));
1514

    
1515
        // Check if the physical file is there.
1516
        $arguments = array('%name' => $file->filename, '%username' => $account->name, '%uri' => $file->uri);
1517
        $this->assertTrue(is_file($file->uri), format_string('File %name owned by %username successfully created at %uri.', $arguments));
1518
        $url = file_create_url($file->uri);
1519
        $message_file_info = ' ' . format_string('File %uri was checked.', array('%uri' => $file->uri));
1520
      }
1521

    
1522
      // Try to download the file.
1523
      $this->drupalGet($url);
1524
      $this->assertResponse($case['expect'], $case['message'] . $message_file_info);
1525

    
1526
      // Logout authenticated users.
1527
      if ($authenticated_user) {
1528
        $this->drupalLogout();
1529
      }
1530
    }
1531
  }
1532

    
1533
  /**
1534
   * Asserts file_entity_access correctly grants or denies access.
1535
   */
1536
  function assertFileEntityAccess($ops, $file, $account) {
1537
    drupal_static_reset('file_entity_access');
1538
    foreach ($ops as $op => $result) {
1539
      $msg = t("file_entity_access returns @result with operation '@op'.", array('@result' => $result ? 'true' : 'false', '@op' => $op));
1540
      $this->assertEqual($result, file_entity_access($op, $file, $account), $msg);
1541
    }
1542
  }
1543

    
1544
  /**
1545
   * Helper for testFileEntityPrivateDownloadAccess() test.
1546
   *
1547
   * Defines several cases for accesing private files.
1548
   *
1549
   * @return array
1550
   *   Array of associative arrays, each one having the next keys:
1551
   *   - "message" string with the assertion message.
1552
   *   - "permissions" array of permissions or NULL for anonymous user.
1553
   *   - "expect" expected HTTP response code.
1554
   *   - "owner" Optional boolean indicating if the user is a file owner.
1555
   */
1556
  function getPrivateDownloadAccessCases() {
1557
    return array(
1558
      array(
1559
        'message' => "File owners cannot download their own files unless they are granted the 'view own private files' permission.",
1560
        'permissions' => array(),
1561
        'expect' => 403,
1562
        'owner' => TRUE,
1563
      ),
1564
      array(
1565
        'message' => "File owners can download their own files as they have been granted the 'view own private files' permission.",
1566
        'permissions' => array('view own private files'),
1567
        'expect' => 200,
1568
        'owner' => TRUE,
1569
      ),
1570
      array(
1571
        'message' => "Anonymous users cannot download private files.",
1572
        'permissions' => NULL,
1573
        'expect' => 403,
1574
      ),
1575
      array(
1576
        'message' => "Authenticated users cannot download each other's private files.",
1577
        'permissions' => array(),
1578
        'expect' => 403,
1579
      ),
1580
      array(
1581
        'message' => "Users who can view public files are not able to download private files.",
1582
        'permissions' => array('view files'),
1583
        'expect' => 403,
1584
      ),
1585
      array(
1586
        'message' => "Users who bypass file access can download any file.",
1587
        'permissions' => array('bypass file access'),
1588
        'expect' => 200,
1589
      ),
1590
    );
1591
  }
1592
}
1593

    
1594
/**
1595
 * Tests overriding file attributes.
1596
 */
1597
class FileEntityAttributeOverrideTestCase extends FileEntityTestHelper {
1598

    
1599
  public static function getInfo() {
1600
    return array(
1601
      'name' => 'File entity attribute override',
1602
      'description' => 'Test overriding file entity attributes.',
1603
      'group' => 'File entity',
1604
    );
1605
  }
1606

    
1607
  /**
1608
   * Test to see if file attributes can be overridden.
1609
   */
1610
  function testFileEntityFileAttributeOverrides() {
1611
    $overrides = array(
1612
      'width' => 40,
1613
      'height' => 20,
1614
      'alt' => $this->randomName(),
1615
      'title' => $this->randomName(),
1616

    
1617
    );
1618

    
1619
    // Create an image file entity for testing.
1620
    $file = $this->createFileEntity(array('type' => 'image'));
1621

    
1622
    // Override a variety of attributes.
1623
    foreach ($overrides as $override => $value) {
1624
      $file->override['attributes'][$override] = $value;
1625
    }
1626

    
1627
    // Build just the file portion of a file entity.
1628
    $build = file_view_file($file, 'full');
1629

    
1630
    // Verify that all of the overrides replaced the attributes.
1631
    foreach ($overrides as $attribute => $expected_value) {
1632
      $this->assertEqual($build['#file']->$attribute, $expected_value, format_string('The %attribute was overridden correctly.', array('%attribute' => $attribute)));
1633
    }
1634
  }
1635
}