1 file.test FileAdminTestCase::testFileAdminPages()

Tests file overview with different user permissions.

File

core/modules/file/tests/file.test, line 2317
Tests for file.module.

Class

FileAdminTestCase
Test file administration page functionality.

Code

function testFileAdminPages() {
  $this->backdropLogin($this->admin_user);

  // Create a file field to add files.
  $field_name = strtolower($this->randomName());
  $type_name = 'post';
  $this->createFileField($field_name, $type_name, array(), array('file_extensions' => ''));

  // Create test files.
  $files = array();
  $files['file_1'] = $this->getTestFile('text');
  $files['file_2'] = $this->getTestFile('text', 131072);
  $files['file_3'] = $this->getTestFile('text', 1310720);
  $files['file_4'] = $this->getTestFile('image', 125);
  $files['file_5'] = $this->getTestFile('image', 183);
  $files['file_6'] = $this->getTestFile('image', 1885);

  foreach ($files as $file) {
    // Create a new node with the file attached.
    $nid = $this->uploadNodeFile($file, $field_name, $type_name);
    sleep(1);
  }

  // Test that the default sort by timestamp actually fires properly.
  $files_query = db_select('file_managed', 'f')
    ->fields('f', array('fid'))
    ->orderBy('timestamp', 'DESC')
    ->execute()
    ->fetchCol();

  // Create a set of loaded file entities to use for testing.
  $files = array();
  foreach ($files_query as $fid) {
    $files[$fid] = file_load($fid);
  }

  // Add a second usage for the last file.
  $edit[$field_name . '[und][0][fid]'] = $fid;
  $this->backdropPost('node/' . $nid . '/edit', $edit, 'Save');

  $files_list = array();
  $this->backdropGet('admin/content/files');
  foreach ($this->xpath('//table/tbody/tr/td/div/input/@value') as $input) {
    $files_list[] = (string) $input;
  }
  $this->assertEqual($files_query, $files_list, 'Files correctly sorted by timestamp, by default.');

  // Test file size sort.
  $files_query = db_select('file_managed', 'f')
    ->fields('f', array('fid'))
    ->orderBy('filesize')
    ->execute()
    ->fetchCol();

  $files_list = array();
  $this->backdropGet('admin/content/files', array('query' => array('sort' => 'asc', 'order' => 'filesize')));
  foreach ($this->xpath('//table/tbody/tr/td/div/input/@value') as $input) {
    $files_list[] = $input;
  }
  $this->assertEqual($files_query, $files_list, 'Files correctly sorted by filesize.');

  // Verify edit and delete links for any file.
  $index = 0;
  foreach ($files as $file) {
    $this->assertLinkByHref('file/' . $file->fid . '/delete');
    // Verify tableselect.
    $this->assertFieldByName('bulk_form[' . $index . ']', '', 'Bulk operations found.');
    $index++;
  }

  // Verify no delete links are displayed without delete permission.
  $this->backdropLogout();
  $this->backdropLogin($this->base_user_1);
  $this->backdropGet('admin/content/files');
  $this->assertResponse(200);
  foreach ($files as $file) {
    $this->assertNoLinkByHref('file/' . $file->fid . '/delete');
  }

  // Verify delete links are displayed with delete permission added.
  $this->backdropLogout();
  $this->backdropLogin($this->base_user_2);
  $this->backdropGet('admin/content/files');
  $this->assertResponse(200);
  foreach ($files as $file) {
    $this->assertLinkByHref('file/' . $file->fid . '/delete');

    // Verify no edit links are displayed without edit permission.
    $this->assertNoLinkByHref('file/' . $file->fid . '/manage');

    // Verify use count on confirm form is correct.
    $this->backdropGet('file/' . $file->fid . '/delete');
    $count = db_query("SELECT count(fid) FROM {file_usage} WHERE fid = :fid", array(':fid' => $file->fid))->fetchField();
    if ($count == 0) {
      $text = 'This file has no known content referencing it';
    }
    else {
      $singular = 'This file is referenced by one piece of content.';
      $plural = "This file is referenced by @count pieces of content.";
      $text = format_plural($count, $singular, $plural);
    }
    $this->assertText($text, 'Correct file usage counts appear on confirm page: ', $count);
    // Test the cancel link on the confirm form.
    $this->clickLink('Cancel');
  }

  // Verify edit links are displayed with edit permission added.
  $this->backdropLogout();
  $this->backdropLogin($this->base_user_3);
  $this->backdropGet('admin/content/files');
  $this->assertResponse(200);
  foreach ($files as $file) {
    $this->assertLinkByHref('file/' . $file->fid . '/manage');
  }

  // Verify changing a filename changes the database value.
  $parts = explode('.', $file->uri);
  $extension = array_pop($parts);
  $new_filename = 'changedme' . $extension;
  $edit = array();
  $edit['filename'] = $new_filename;
  $this->backdropPost('file/' . $file->fid . '/manage', $edit, 'Save');
  $this->assertText($new_filename, 'File name changed in database.');

  // Verify uploading a new file replaces the existing file.
  // Test the file has been updated by checking its file size.
  $edit = array();
  $test_file = $this->getTestFile('text', 131072); // 128KB
  $edit['files[replace_upload]'] = backdrop_realpath($test_file->uri);
  $this->backdropPost('file/' . $file->fid . '/manage', $edit, 'Save');
  $this->assertText(format_size(131072), 'File size matches new upload.');
  // Do it twice to be sure.
  $edit = array();
  $test_file = $this->getTestFile('text', 1310720); // 1.2MB
  $edit['files[replace_upload]'] = backdrop_realpath($test_file->uri);
  $this->backdropPost('file/' . $file->fid . '/manage', $edit, 'Save');
  $this->assertText(format_size(1310720), 'File size matches new upload.');
}