You are here

function EntityEmbedFileUsageTest::testFileUsageDeleted in Entity Embed 7.2

Same name and namespace in other branches
  1. 7 entity_embed.test \EntityEmbedFileUsageTest::testFileUsageDeleted()

Tests usage tracking when deleting content or files.

File

./entity_embed.test, line 710
Test integration for the entity_embed module.

Class

EntityEmbedFileUsageTest
Tests file usage.

Code

function testFileUsageDeleted() {

  // Create a file.
  $files = $this
    ->drupalGetTestFiles('image');
  $file = file_save($files[1]);
  $fid = $file->fid;

  // The file should start without any usage.
  $file_uses = file_usage_list($file);
  $this
    ->assertEqual(empty($file_uses), TRUE, t('Created a new file without any usage.'));

  // Create a node with an embedded file.
  $content = '<drupal-entity data-entity-type="file" data-entity-id="' . $fid . '" data-view-mode="full"></drupal-entity>';
  $settings = array();
  $settings['body'] = array(
    LANGUAGE_NONE => array(
      array(
        'value' => $content,
        'format' => 'custom_format',
      ),
    ),
  );
  $node = $this
    ->drupalCreateNode($settings);
  $nid = $node->nid;

  // Verify that file usage increased.
  $file_uses = file_usage_list($file);
  $this
    ->assertEqual($file_uses['entity_embed']['node'][$nid], 1, t('File usage increases after embedding a file in a new node.'));

  // Try to delete the file. file_delete() should return file_usage().
  $deleted = file_delete($file);
  $this
    ->assertTrue(is_array($deleted), t('File cannot be deleted while in use by a node.'));

  // Delete the node.
  node_delete($nid);
  $node = node_load($nid);
  $file_uses = file_usage_list($file);

  // Verify that the node was deleted and the file is now unused.
  $this
    ->assertEqual(empty($node), TRUE, t('Node has been deleted.'));
  $this
    ->assertEqual(empty($file_uses), TRUE, t('File has zero usage after node is deleted.'));

  // Delete the file.
  // Verify that the file can be deleted when it is unused.
  $deleted = file_delete($file);
  $this
    ->assertTrue($deleted, t('File can be deleted with no usage.'));

  // Verify that the file was deleted.
  $file = file_load($fid);
  $this
    ->assertTrue(empty($file), t('File no longer exists after delete.'));
}