You are here

public function SelectionTest::testSelectionHandler in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\SelectionTest::testSelectionHandler()

Tests the selection handler.

File

core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php, line 87

Class

SelectionTest
Tests entity reference selection handler.

Namespace

Drupal\Tests\field\Kernel\EntityReference\Views

Code

public function testSelectionHandler() {

  // Tests the selection handler.
  $this
    ->assertResults($this->selectionHandler
    ->getReferenceableEntities());

  // Add a relationship to the view.
  $view = Views::getView('test_entity_reference');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')
    ->setOption('relationships', [
    'test_relationship' => [
      'id' => 'uid',
      'table' => 'node_field_data',
      'field' => 'uid',
    ],
  ]);

  // Add a filter depending on the relationship to the test view.
  $view->displayHandlers
    ->get('default')
    ->setOption('filters', [
    'uid' => [
      'id' => 'uid',
      'table' => 'users_field_data',
      'field' => 'uid',
      'relationship' => 'test_relationship',
    ],
  ]);

  // Set view to distinct so only one row per node is returned.
  $query_options = $view->display_handler
    ->getOption('query');
  $query_options['options']['distinct'] = TRUE;
  $view->display_handler
    ->setOption('query', $query_options);
  $view
    ->save();

  // Tests the selection handler with a relationship.
  $this
    ->assertResults($this->selectionHandler
    ->getReferenceableEntities());
}