You are here

function FileEntityUsageTestCase::testFileEntityUsagePage in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.test \FileEntityUsageTestCase::testFileEntityUsagePage()

Create a file and verify its usage information.

File

./file_entity.test, line 826
Test integration for the file_entity module.

Class

FileEntityUsageTestCase
Tests the file usage page.

Code

function testFileEntityUsagePage() {
  $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]" => drupal_realpath($image->uri),
  );
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $this
    ->drupalPost('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
    ->drupalGet('file/' . $file->fid . '/usage');

  // Verify that a link to the entity is available.
  $this
    ->assertLink($node->title);
  $this
    ->assertLinkByHref('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
    ->drupalGet('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
    ->drupalGet('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('(entity not loaded)', '"(entity not loaded)" notice 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,
    )));
  }
}