1 file.test FileUsageTestCase::testFileUsagePage()

Create a file and verify its usage information.

File

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

Class

FileUsageTestCase
Tests the file usage page.

Code

function testFileUsagePage() {
  $image_field = 'field_image';
  $image = $this->getTestFile('image');

  // Create a node, save it, then edit it to upload a file.
  $edit = array(
    "files[" . $image_field . "_" . LANGUAGE_NONE . "_0]" => backdrop_realpath($image->uri),
  );
  $node = $this->backdropCreateNode(array('type' => 'post'));
  $this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));

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

  // View the file's usage page.
  $this->backdropGet('file/' . $file->fid . '/usage');

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

  // Verify that the entity type and use count information is also present.
  $expected_values = array(
    'type' => 'node',
    'count' => 1,
  );
  foreach ($expected_values as $name => $value) {
    $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)));
  }

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

  // Reload the page and verify that the expected values are present.
  $this->backdropGet('file/' . $file->fid . '/usage');
  $expected_values['count'] = 3;
  foreach ($expected_values as $name => $value) {
    $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)));
  }

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

  // Reload the page.
  $this->backdropGet('file/' . $file->fid . '/usage');

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

  // Verify that the entity type and use count information is also present.
  $expected_values = array(
    'type' => 'imaginary',
    'count' => 1,
  );
  foreach ($expected_values as $name => $value) {
    $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)));
  }
}