public function MultifieldFileUsageTestCase::testFileUsage in Multifield 7
Same name and namespace in other branches
- 7.2 tests/MultifieldFileUsageTestCase.test \MultifieldFileUsageTestCase::testFileUsage()
File
- tests/
MultifieldFileUsageTestCase.test, line 18
Class
Code
public function testFileUsage() {
// Add the test node type.
$node_type = $this
->drupalCreateContentType();
// Get the test files.
$text_files = $this
->drupalGetTestFiles('text');
$text_file_1 = file_save($text_files[0]);
$text_file_2 = file_save($text_files[1]);
$image_files = $this
->drupalGetTestFiles('image');
$image_file_1 = file_save($image_files[0]);
$image_file_2 = file_save($image_files[1]);
$multifield_field = array(
'field_name' => 'test_file_usage',
'type' => 'multifield',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
);
$multifield_field = field_create_field($multifield_field);
$file_field = array(
'field_name' => 'field_file',
'type' => 'file',
);
$file_field = field_create_field($file_field);
$file_field_instance = array(
'field_name' => $file_field['field_name'],
'entity_type' => 'multifield',
'bundle' => $multifield_field['field_name'],
);
field_create_instance($file_field_instance);
$image_field = array(
'field_name' => 'field_image',
'type' => 'image',
);
$image_field = field_create_field($image_field);
$image_field_instance = array(
'field_name' => $image_field['field_name'],
'entity_type' => 'multifield',
'bundle' => $multifield_field['field_name'],
);
field_create_instance($image_field_instance);
$multifield_field_instance = array(
'field_name' => $multifield_field['field_name'],
'entity_type' => 'node',
'bundle' => $node_type->type,
);
field_create_instance($multifield_field_instance);
$node = new stdClass();
$node->type = $node_type->type;
$node->language = LANGUAGE_NONE;
$node->test_file_usage[LANGUAGE_NONE][0]['field_file'][LANGUAGE_NONE][0]['fid'] = $text_file_1->fid;
$node->test_file_usage[LANGUAGE_NONE][1]['field_file'][LANGUAGE_NONE][0]['fid'] = $text_file_1->fid;
node_save($node);
$first_revision_id = $node->vid;
$id1 = $node->test_file_usage[LANGUAGE_NONE][0]['id'];
$id2 = $node->test_file_usage[LANGUAGE_NONE][1]['id'];
$this
->assertFileUsage($text_file_1, 'file', 'multifield', $id1, 1);
$this
->assertFileUsage($text_file_1, 'file', 'multifield', $id2, 1);
$this
->assertFileUsage($text_file_1, 'file', 'node', $node->nid, 2);
$this
->assertNoFileUsage($text_file_2);
$this
->assertNoFileUsage($image_file_1);
$node->test_file_usage[LANGUAGE_NONE][0]['field_file'][LANGUAGE_NONE][0]['fid'] = $text_file_2->fid;
$node->test_file_usage[LANGUAGE_NONE][0]['field_image'][LANGUAGE_NONE][0]['fid'] = $image_file_1->fid;
$node->test_file_usage[LANGUAGE_NONE][1]['field_file'][LANGUAGE_NONE][0] = array();
$node->test_file_usage[LANGUAGE_NONE][1]['field_image'][LANGUAGE_NONE][0]['fid'] = $image_file_1->fid;
node_save($node);
$this
->assertNoFileUsage($text_file_1);
$this
->assertFileUsage($text_file_2, 'file', 'multifield', $id1, 1);
$this
->assertFileUsage($text_file_2, 'file', 'multifield', $id2, 0);
$this
->assertFileUsage($text_file_2, 'file', 'node', $node->nid, 1);
$this
->assertFileUsage($image_file_1, 'file', 'multifield', $id1, 1);
$this
->assertFileUsage($image_file_1, 'file', 'multifield', $id2, 1);
$this
->assertFileUsage($image_file_1, 'file', 'node', $node->nid, 2);
// Test saving a revision of the node to ensure previous usages are
// preserved.
$node->revision = TRUE;
$node->test_file_usage[LANGUAGE_NONE][0]['field_file'][LANGUAGE_NONE][0]['fid'] = $text_file_2->fid;
$node->test_file_usage[LANGUAGE_NONE][0]['field_image'][LANGUAGE_NONE][0]['fid'] = $image_file_1->fid;
$node->test_file_usage[LANGUAGE_NONE][1] = array();
node_save($node);
$this
->assertNoFileUsage($text_file_1);
$this
->assertFileUsage($text_file_2, 'file', 'multifield', $id1, 2);
$this
->assertFileUsage($text_file_2, 'file', 'multifield', $id2, 0);
$this
->assertFileUsage($text_file_2, 'file', 'node', $node->nid, 2);
$this
->assertFileUsage($image_file_1, 'file', 'multifield', $id1, 2);
$this
->assertFileUsage($image_file_1, 'file', 'multifield', $id2, 1);
$this
->assertFileUsage($image_file_1, 'file', 'node', $node->nid, 3);
// Set the multifield to empty and delete the old revision.
$node->revision = TRUE;
$node->test_file_usage = array();
node_save($node);
node_revision_delete($first_revision_id);
$this
->assertNoFileUsage($text_file_1);
$this
->assertFileUsage($text_file_2, 'file', 'multifield', $id1, 1);
$this
->assertFileUsage($text_file_2, 'file', 'multifield', $id2, 0);
$this
->assertFileUsage($text_file_2, 'file', 'node', $node->nid, 1);
$this
->assertFileUsage($image_file_1, 'file', 'multifield', $id1, 1);
$this
->assertFileUsage($image_file_1, 'file', 'multifield', $id2, 0);
$this
->assertFileUsage($image_file_1, 'file', 'node', $node->nid, 1);
// Delete the node and test that all usages have been removed.
node_delete($node->nid);
$this
->assertNoFileUsage($text_file_1);
$this
->assertNoFileUsage($text_file_2);
$this
->assertNoFileUsage($image_file_1);
}