View source  
  <?php
namespace Drupal\Tests\field\Kernel\EntityReference\Views;
use Drupal\Component\Utility\Html;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\views\Views;
class SelectionTest extends KernelTestBase {
  use EntityReferenceTestTrait;
  use NodeCreationTrait;
  
  protected static $modules = [
    'entity_reference_test',
    'entity_test',
    'field',
    'filter',
    'node',
    'system',
    'user',
    'views',
  ];
  
  protected $nodes = [];
  
  protected $selectionHandler;
  
  protected function setUp() : void {
    parent::setUp();
    $this
      ->installConfig([
      'entity_reference_test',
      'filter',
    ]);
    $this
      ->installEntitySchema('user');
    $this
      ->installEntitySchema('node');
    
    $type = strtolower($this
      ->randomMachineName());
    NodeType::create([
      'type' => $type,
    ])
      ->save();
    $node1 = $this
      ->createNode([
      'type' => $type,
    ]);
    $node2 = $this
      ->createNode([
      'type' => $type,
    ]);
    $node3 = $this
      ->createNode();
    foreach ([
      $node1,
      $node2,
      $node3,
    ] as $node) {
      $this->nodes[$node
        ->id()] = $node;
    }
    
    $handler_settings = [
      'view' => [
        'view_name' => 'test_entity_reference',
        'display_name' => 'entity_reference_1',
      ],
    ];
    $this
      ->createEntityReferenceField('entity_test', 'test_bundle', 'test_field', $this
      ->randomString(), 'node', 'views', $handler_settings);
    $field_config = FieldConfig::loadByName('entity_test', 'test_bundle', 'test_field');
    $this->selectionHandler = $this->container
      ->get('plugin.manager.entity_reference_selection')
      ->getSelectionHandler($field_config);
  }
  
  public function testSelectionHandler() {
    
    $this
      ->assertResults($this->selectionHandler
      ->getReferenceableEntities());
    
    $view = Views::getView('test_entity_reference');
    $view
      ->setDisplay();
    $view->displayHandlers
      ->get('default')
      ->setOption('relationships', [
      'test_relationship' => [
        'id' => 'uid',
        'table' => 'node_field_data',
        'field' => 'uid',
      ],
    ]);
    
    $view->displayHandlers
      ->get('default')
      ->setOption('filters', [
      'uid' => [
        'id' => 'uid',
        'table' => 'users_field_data',
        'field' => 'uid',
        'relationship' => 'test_relationship',
      ],
    ]);
    
    $query_options = $view->display_handler
      ->getOption('query');
    $query_options['options']['distinct'] = TRUE;
    $view->display_handler
      ->setOption('query', $query_options);
    $view
      ->save();
    
    $this
      ->assertResults($this->selectionHandler
      ->getReferenceableEntities());
  }
  
  public function testAnchorTagStripping() {
    $filtered_rendered_results_formatted = [];
    foreach ($this->selectionHandler
      ->getReferenceableEntities() as $subresults) {
      $filtered_rendered_results_formatted += $subresults;
    }
    
    $expected = [
      1 => '<span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[1]
        ->label()) . '</span></span>',
      2 => '<span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[2]
        ->label()) . '</span></span>',
      3 => '<span class="views-field views-field-title"><span class="field-content">' . Html::escape($this->nodes[3]
        ->label()) . '</span></span>',
    ];
    $this
      ->assertEquals($expected, $filtered_rendered_results_formatted, 'Anchor tag stripping has failed.');
  }
  
  protected function assertResults(array $result) {
    foreach ($result as $node_type => $values) {
      foreach ($values as $nid => $label) {
        $this
          ->assertSame($node_type, $this->nodes[$nid]
          ->bundle());
        $this
          ->assertSame(trim(strip_tags($label)), Html::escape($this->nodes[$nid]
          ->label()));
      }
    }
  }
}