You are here

public function RelationshipTest::testRelationshipQuery in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/RelationshipTest.php \Drupal\Tests\views\Kernel\Plugin\RelationshipTest::testRelationshipQuery()
  2. 10 core/modules/views/tests/src/Kernel/Plugin/RelationshipTest.php \Drupal\Tests\views\Kernel\Plugin\RelationshipTest::testRelationshipQuery()

Tests the query result of a view with a relationship.

File

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

Class

RelationshipTest
Tests the base relationship handler.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testRelationshipQuery() {
  $connection = Database::getConnection();

  // Set the first entry to have the admin as author.
  $connection
    ->update('views_test_data')
    ->fields([
    'uid' => 1,
  ])
    ->condition('id', 1)
    ->execute();
  $connection
    ->update('views_test_data')
    ->fields([
    'uid' => 2,
  ])
    ->condition('id', 1, '<>')
    ->execute();
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')
    ->overrideOption('relationships', [
    'uid' => [
      'id' => 'uid',
      'table' => 'views_test_data',
      'field' => 'uid',
    ],
  ]);
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'uid' => [
      'id' => 'uid',
      'table' => 'users_field_data',
      'field' => 'uid',
      'relationship' => 'uid',
    ],
  ]);
  $fields = $view->displayHandlers
    ->get('default')
    ->getOption('fields');
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', $fields + [
    'uid' => [
      'id' => 'uid',
      'table' => 'users_field_data',
      'field' => 'uid',
      'relationship' => 'uid',
    ],
  ]);
  $view
    ->initHandlers();

  // Check for all beatles created by admin.
  $view->filter['uid']->value = [
    1,
  ];
  $this
    ->executeView($view);
  $expected_result = [
    [
      'name' => 'John',
      'uid' => 1,
    ],
  ];
  $this
    ->assertIdenticalResultset($view, $expected_result, $this->columnMap);
  $view
    ->destroy();

  // Check for all beatles created by another user, which so doesn't exist.
  $view
    ->initHandlers();
  $view->filter['uid']->value = [
    3,
  ];
  $this
    ->executeView($view);
  $expected_result = [];
  $this
    ->assertIdenticalResultset($view, $expected_result, $this->columnMap);
  $view
    ->destroy();

  // Set the relationship to required, so only results authored by the admin
  // should return.
  $view
    ->initHandlers();
  $view->relationship['uid']->options['required'] = TRUE;
  $this
    ->executeView($view);
  $expected_result = [
    [
      'name' => 'John',
      'uid' => 1,
    ],
  ];
  $this
    ->assertIdenticalResultset($view, $expected_result, $this->columnMap);
  $view
    ->destroy();

  // Set the relationship to optional should cause to return all beatles.
  $view
    ->initHandlers();
  $view->relationship['uid']->options['required'] = FALSE;
  $this
    ->executeView($view);
  $expected_result = $this
    ->dataSet();

  // Alter the expected result to contain the right uids.
  foreach ($expected_result as &$row) {

    // Only John has an existing author.
    if ($row['name'] == 'John') {
      $row['uid'] = 1;
    }
    else {

      // The LEFT join should set an empty {users}.uid field.
      $row['uid'] = NULL;
    }
  }
  $this
    ->assertIdenticalResultset($view, $expected_result, $this->columnMap);
}