function RelationshipNodeFileDataTest::testViewsHandlerRelationshipFileToNode

Same name in other branches
  1. 11.x core/modules/file/tests/src/Kernel/Views/RelationshipNodeFileDataTest.php \Drupal\Tests\file\Kernel\Views\RelationshipNodeFileDataTest::testViewsHandlerRelationshipFileToNode()

Tests using the views file_to_node relationship.

File

core/modules/file/tests/src/Kernel/Views/RelationshipNodeFileDataTest.php, line 70

Class

RelationshipNodeFileDataTest
Tests file on node relationship handler.

Namespace

Drupal\Tests\file\Kernel\Views

Code

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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.