function RelationshipTest::testRelationshipRender

Same name in other branches
  1. 8.9.x core/modules/views/tests/src/Kernel/Plugin/RelationshipTest.php \Drupal\Tests\views\Kernel\Plugin\RelationshipTest::testRelationshipRender()
  2. 10 core/modules/views/tests/src/Kernel/Plugin/RelationshipTest.php \Drupal\Tests\views\Kernel\Plugin\RelationshipTest::testRelationshipRender()
  3. 11.x core/modules/views/tests/src/Kernel/Plugin/RelationshipTest.php \Drupal\Tests\views\Kernel\Plugin\RelationshipTest::testRelationshipRender()

Tests rendering of a view with a relationship.

File

core/modules/views/tests/src/Kernel/Plugin/RelationshipTest.php, line 146

Class

RelationshipTest
Tests the base relationship handler.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testRelationshipRender() {
    $connection = Database::getConnection();
    $author1 = $this->createUser();
    $connection->update('views_test_data')
        ->fields([
        'uid' => $author1->id(),
    ])
        ->condition('id', 1)
        ->execute();
    $author2 = $this->createUser();
    $connection->update('views_test_data')
        ->fields([
        'uid' => $author2->id(),
    ])
        ->condition('id', 2)
        ->execute();
    // Set uid to non-existing author uid for row 3.
    $connection->update('views_test_data')
        ->fields([
        'uid' => $author2->id() + 123,
    ])
        ->condition('id', 3)
        ->execute();
    $view = Views::getView('test_view');
    // Add a relationship for authors.
    $view->getDisplay()
        ->overrideOption('relationships', [
        'uid' => [
            'id' => 'uid',
            'table' => 'views_test_data',
            'field' => 'uid',
        ],
    ]);
    // Add fields for {views_test_data}.id and author name.
    $view->getDisplay()
        ->overrideOption('fields', [
        'id' => [
            'id' => 'id',
            'table' => 'views_test_data',
            'field' => 'id',
        ],
        'author' => [
            'id' => 'author',
            'table' => 'users_field_data',
            'field' => 'name',
            'relationship' => 'uid',
        ],
    ]);
    // Render the view.
    $output = $view->preview();
    $html = $this->container
        ->get('renderer')
        ->renderRoot($output);
    $this->setRawContent($html);
    // Check that the output contains correct values.
    $xpath = '//div[@class="views-row" and div[@class="views-field views-field-id"]=:id and div[@class="views-field views-field-author"]=:author]';
    $this->assertCount(1, $this->xpath($xpath, [
        ':id' => 1,
        ':author' => $author1->getAccountName(),
    ]));
    $this->assertCount(1, $this->xpath($xpath, [
        ':id' => 2,
        ':author' => $author2->getAccountName(),
    ]));
    $this->assertCount(1, $this->xpath($xpath, [
        ':id' => 3,
        ':author' => '',
    ]));
}

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