class RelationshipNodeFileDataTest
Same name and namespace in other branches
- 11.x core/modules/file/tests/src/Kernel/Views/RelationshipNodeFileDataTest.php \Drupal\Tests\file\Kernel\Views\RelationshipNodeFileDataTest
Tests file on node relationship handler.
@group file
Hierarchy
- class \Drupal\Tests\file\Kernel\Views\RelationshipNodeFileDataTest
Expanded class hierarchy of RelationshipNodeFileDataTest
File
-
core/
modules/ file/ tests/ src/ Kernel/ Views/ RelationshipNodeFileDataTest.php, line 22
Namespace
Drupal\Tests\file\Kernel\ViewsView source
class RelationshipNodeFileDataTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'field',
'file',
'file_test_views',
'node',
'text',
];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = [
'test_file_to_node',
'test_node_to_file',
];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this->installSchema('file', 'file_usage');
$this->installEntitySchema('node');
$this->installEntitySchema('file');
$this->installEntitySchema('user');
$this->installConfig([
'node',
'field',
'file_test_views',
]);
// Create the node file field and instance.
FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'node_file',
'type' => 'file',
'translatable' => '0',
])->save();
FieldConfig::create([
'label' => 'Node File',
'description' => '',
'field_name' => 'node_file',
'entity_type' => 'node',
'bundle' => 'file_test',
'required' => 0,
])->save();
ViewTestData::createTestViews(static::class, [
'file_test_views',
]);
}
/**
* Tests using the views file_to_node relationship.
*/
public function testViewsHandlerRelationshipFileToNode() : void {
$file1 = File::create([
'filename' => 'image-test.jpg',
'uri' => "public://image-test.jpg",
'filemime' => 'image/jpeg',
'created' => 1,
'changed' => 1,
'status' => FileInterface::STATUS_PERMANENT,
]);
$file1->enforceIsNew();
file_put_contents($file1->getFileUri(), file_get_contents('core/tests/fixtures/files/image-1.png'));
$file1->save();
$file2 = File::create([
'filename' => 'image-test-2.jpg',
'uri' => "public://image-test-2.jpg",
'filemime' => 'image/jpeg',
'created' => 1,
'changed' => 1,
'status' => FileInterface::STATUS_PERMANENT,
]);
$file2->enforceIsNew();
file_put_contents($file2->getFileUri(), file_get_contents('core/tests/fixtures/files/image-1.png'));
$file2->save();
$node1 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::PUBLISHED,
]);
$node1->save();
$node2 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::PUBLISHED,
'node_file' => [
'target_id' => $file2->id(),
],
]);
$node2->save();
$view = Views::getView('test_file_to_node');
$this->executeView($view);
// We should only see a single file, the one on the user account. The other
// account's UUID, nor the other unlinked file, should appear in the
// results.
$expected_result = [
[
'fid' => $file2->id(),
'nid' => $node2->id(),
],
];
$column_map = [
'fid' => 'fid',
'nid' => 'nid',
];
$this->assertIdenticalResultset($view, $expected_result, $column_map);
}
/**
* Tests using the views node_to_file relationship.
*/
public function testViewsHandlerRelationshipNodeToFile() : void {
$file1 = File::create([
'filename' => 'image-test.jpg',
'uri' => "public://image-test.jpg",
'filemime' => 'image/jpeg',
'created' => 1,
'changed' => 1,
'status' => FileInterface::STATUS_PERMANENT,
]);
$file1->enforceIsNew();
file_put_contents($file1->getFileUri(), file_get_contents('core/tests/fixtures/files/image-1.png'));
$file1->save();
$file2 = File::create([
'filename' => 'image-test-2.jpg',
'uri' => "public://image-test-2.jpg",
'filemime' => 'image/jpeg',
'created' => 1,
'changed' => 1,
'status' => FileInterface::STATUS_PERMANENT,
]);
$file2->enforceIsNew();
file_put_contents($file2->getFileUri(), file_get_contents('core/tests/fixtures/files/image-1.png'));
$file2->save();
$node1 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::PUBLISHED,
]);
$node1->save();
$node2 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::PUBLISHED,
'node_file' => [
'target_id' => $file2->id(),
],
]);
$node2->save();
$view = Views::getView('test_node_to_file');
$this->executeView($view);
// We should only see a single file, the one on the user account. The other
// account's UUID, nor the other unlinked file, should appear in the
// results.
$expected_result = [
[
'fid' => $file2->id(),
'nid' => $node2->id(),
],
];
$column_map = [
'fid' => 'fid',
'nid' => 'nid',
];
$this->assertIdenticalResultset($view, $expected_result, $column_map);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
RelationshipNodeFileDataTest::$modules | protected static | property | Modules to install. |
RelationshipNodeFileDataTest::$testViews | public static | property | Views used by this test. |
RelationshipNodeFileDataTest::setUp | protected | function | |
RelationshipNodeFileDataTest::testViewsHandlerRelationshipFileToNode | public | function | Tests using the views file_to_node relationship. |
RelationshipNodeFileDataTest::testViewsHandlerRelationshipNodeToFile | public | function | Tests using the views node_to_file relationship. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.