function EditorFileUsageTest::testFileUsageDeleted in Editor 7
Tests usage tracking when deleting content or files.
File
- ./
editor.test, line 1208 - Tests for Editor module.
Class
- EditorFileUsageTest
- 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 = '<img src="awesome-llama.jpg" data-entity-type="file" data-entity-id="' . $fid . '" />';
$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['editor']['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.'));
}