You are here

public function SelectionTest::testSelectionHandlerRelationship in Zircon Profile 8

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

Tests the selection handler with a relationship.

File

core/modules/field/src/Tests/EntityReference/Views/SelectionTest.php, line 114
Contains \Drupal\field\Tests\EntityReference\Views\SelectionTest.

Class

SelectionTest
Tests entity reference selection handler.

Namespace

Drupal\field\Tests\EntityReference\Views

Code

public function testSelectionHandlerRelationship() {

  // Add a relationship to the view.
  $view = Views::getView('test_entity_reference');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')
    ->setOption('relationships', array(
    'test_relationship' => array(
      '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', array(
    'uid' => array(
      '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();

  // Get values from the selection handler.
  $handler = $this->container
    ->get('plugin.manager.entity_reference_selection')
    ->getSelectionHandler($this->field);
  $result = $handler
    ->getReferenceableEntities();
  $this
    ->assertResults($result);
}