You are here

public function MediaFileUsageTest::testFileUsageIncrementingDelete in D7 Media 7

Tests the behavior of node and file deletion.

File

test/media.file.usage.test, line 231
Tests for the file usage in entity fields with the Media filter markup.

Class

MediaFileUsageTest
@file Tests for the file usage in entity fields with the Media filter markup.

Code

public function testFileUsageIncrementingDelete() {

  // Create a node with file markup in the body field with a new file.
  $files = $this
    ->drupalGetTestFiles('image');
  $file = file_save(file_uri_to_object($files[1]->uri));
  $fid = $file->fid;
  $file_uses = file_usage_list($file);
  $this
    ->assertEqual(empty($file_uses), TRUE, t('Created a new file with zero uses.'));

  // Create a new node with file markup.
  $nid = $this
    ->createNode($fid);
  $file_uses = file_usage_list($file);
  $this
    ->assertEqual($file_uses['media']['node'][$nid], 1, t('Incremented file usage on node save.'));

  // 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);
  $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.'));
  $deleted = file_delete($file);
  $this
    ->assertTrue($deleted, t('File can be deleted with no usage.'));
  $file = file_load($fid);
  $this
    ->assertTrue(empty($file), t('File no longer exists after delete.'));
}