function SelectionTest::testSelectionHandler

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\SelectionTest::testSelectionHandler()
  2. 8.9.x core/modules/field/tests/src/Kernel/EntityReference/Views/SelectionTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\SelectionTest::testSelectionHandler()
  3. 11.x 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 97

Class

SelectionTest
Tests entity reference selection handler.

Namespace

Drupal\Tests\field\Kernel\EntityReference\Views

Code

public function testSelectionHandler() : void {
    // 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());
}

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