You are here

public function RelationshipTest::testRelationshipRender in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/RelationshipTest.php \Drupal\views\Tests\Handler\RelationshipTest::testRelationshipRender()

Tests rendering of a view with a relationship.

File

core/modules/views/src/Tests/Handler/RelationshipTest.php, line 140
Contains \Drupal\views\Tests\Handler\RelationshipTest.

Class

RelationshipTest
Tests the base relationship handler.

Namespace

Drupal\views\Tests\Handler

Code

public function testRelationshipRender() {
  $author1 = $this
    ->createUser();
  db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 1", [
    ':uid' => $author1
      ->id(),
  ]);
  $author2 = $this
    ->createUser();
  db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 2", [
    ':uid' => $author2
      ->id(),
  ]);

  // Set uid to non-existing author uid for row 3.
  db_query("UPDATE {views_test_data} SET uid = :uid WHERE id = 3", [
    ':uid' => $author2
      ->id() + 123,
  ]);
  $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
    ->assertEqual(1, count($this
    ->xpath($xpath, [
    ':id' => 1,
    ':author' => $author1
      ->getUsername(),
  ])));
  $this
    ->assertEqual(1, count($this
    ->xpath($xpath, [
    ':id' => 2,
    ':author' => $author2
      ->getUsername(),
  ])));
  $this
    ->assertEqual(1, count($this
    ->xpath($xpath, [
    ':id' => 3,
    ':author' => '',
  ])));
}