function EntityEmbedFileUsageTest::testFileUsage in Entity Embed 7
Same name and namespace in other branches
- 7.2 entity_embed.test \EntityEmbedFileUsageTest::testFileUsage()
Tests usage tracking of embedded files.
File
- ./
entity_embed.test, line 554 - Test integration for the entity_embed module.
Class
- EntityEmbedFileUsageTest
- Tests file usage.
Code
function testFileUsage() {
// Create a file.
$files = $this
->drupalGetTestFiles('image');
$file = file_save($files[0]);
$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.'));
// Create a new revision of the node that has the file on it.
$node = node_load($nid);
$node->revision = TRUE;
node_save($node);
$node = node_load($nid);
$file_uses = file_usage_list($file);
$revisions = count(node_revision_list($node));
// Keep track of this VID to test deletion later on.
$delete_one = $node->vid;
// Verify that there are two revisions of the node.
// File should now be used twice.
$this
->assertEqual($revisions, 2, t('Node save created a second revision'));
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 2, t('File usage incremented with a new node revision.'));
// Create a new revision of the node that has the file on it.
// Embed two instances of the file.
$node = node_load($nid);
$content = '';
$content .= '<drupal-entity data-entity-type="file" data-entity-id="' . $fid . '" data-view-mode="full"></drupal-entity>';
$content .= '<drupal-entity data-entity-type="file" data-entity-id="' . $fid . '" data-view-mode="full"></drupal-entity>';
$node->body[LANGUAGE_NONE][0]['value'] = $content;
$node->revision = TRUE;
node_save($node);
$node = node_load($nid);
$file_uses = file_usage_list($file);
$revisions = count(node_revision_list($node));
// Keep track of this VID to test deletion later on.
$delete_two = $node->vid;
// Verify that there are three revisions of the node.
// File should now be used four times.
$this
->assertEqual($revisions, 3, t('Node save created a third revision.'));
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 4, t('File usage incremented with multiple files and a new node revision.'));
// Create a new revision of the node that has the file on it.
// Remove all embedded files.
$node = node_load($nid);
$node->body[LANGUAGE_NONE][0]['value'] = '';
$node->revision = TRUE;
node_save($node);
$node = node_load($nid);
$file_uses = file_usage_list($file);
$revisions = count(node_revision_list($node));
// Keep track of this VID to test deletion later on.
$delete_zero = $node->vid;
// Verify that there are four revisions of the node.
// File should continue to be used four times.
$this
->assertEqual($revisions, 4, t('Node save created a fourth revision.'));
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 4, t('File usage is unchanged with a new revision of the node that does not contain any embedded files.'));
// Create a new revision of the node that has the file on it.
// Embed one instance of the file.
$node = node_load($nid);
$node->body[LANGUAGE_NONE][0]['value'] = '<drupal-entity data-entity-type="file" data-entity-id="' . $fid . '" data-view-mode="full"></drupal-entity>';
$node->revision = TRUE;
node_save($node);
$node = node_load($nid);
$file_uses = file_usage_list($file);
$revisions = count(node_revision_list($node));
// Verify that there are five revisions of the node.
// File should now be used five times.
$this
->assertEqual($revisions, 5, t('Node save created a new revision.'));
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 5, t('File usage incremented with a single file on a new node revision.'));
// Delete a revision that has one embedded file. File usage will be 4.
node_revision_delete($delete_one);
$file_uses = file_usage_list($file);
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 4, t('Deleting revision with file decreases file usage.'));
// Delete a revision that has no embedded files. File usage will continue to
// be 4.
node_revision_delete($delete_zero);
$file_uses = file_usage_list($file);
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 4, t('Deleting revision without a file does not change file usage.'));
// Delete a revision that has two embedded files. File usage will be 2.
node_revision_delete($delete_two);
$file_uses = file_usage_list($file);
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 2, t('Deleting revision with file decreases file usage'));
// Create a new revision of the node that has the file on it.
// Embed two instances of the file.
$node = node_load($nid);
$content = '';
$content .= '<drupal-entity data-entity-type="file" data-entity-id="' . $fid . '" data-view-mode="full"></drupal-entity>';
$content .= '<drupal-entity data-entity-type="file" data-entity-id="' . $fid . '" data-view-mode="full"></drupal-entity>';
$node->body[LANGUAGE_NONE][0]['value'] = $content;
$node->revision = TRUE;
node_save($node);
$file_uses = file_usage_list($file);
// File should now be used four times.
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 4, t('File usage incremented with files on a new node revision.'));
// Remove one instance of the embedded file and re-save the current revision
// of the node.
$node = node_load($nid);
$node->body[LANGUAGE_NONE][0]['value'] = '<drupal-entity data-entity-type="file" data-entity-id="' . $fid . '" data-view-mode="full"></drupal-entity>';
$saved_vid = $node->vid;
node_save($node);
$node = node_load($nid);
$file_uses = file_usage_list($file);
// Verify that the current revision was used.
// File should now be used three times.
$this
->assertEqual($node->vid, $saved_vid, t('Resaved node revision does not create new revision.'));
$this
->assertEqual($file_uses['entity_embed']['node'][$nid], 3, t('Resaved node revision with fewer files reduces file usage.'));
// Delete the node.
// The file should now be unused.
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('Deleting the node removes all file uses.'));
}