3 namespace Drupal\Tests\field\Functional\EntityReference\Views;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\views\Views;
8 use Drupal\field\Entity\FieldStorageConfig;
11 * Tests entity reference selection handler.
13 * @group entity_reference
15 class SelectionTest extends BrowserTestBase {
17 public static $modules = ['node', 'views', 'entity_reference_test', 'entity_test'];
24 protected $nodes = [];
27 * The entity reference field to test.
29 * @var \Drupal\Core\Field\FieldDefinitionInterface
36 protected function setUp() {
40 $type = $this->drupalCreateContentType()->id();
41 $node1 = $this->drupalCreateNode(['type' => $type]);
42 $node2 = $this->drupalCreateNode(['type' => $type]);
43 $node3 = $this->drupalCreateNode();
45 foreach ([$node1, $node2, $node3] as $node) {
46 $this->nodes[$node->getType()][$node->id()] = $node->label();
50 $field_storage = FieldStorageConfig::create([
51 'field_name' => 'test_field',
52 'entity_type' => 'entity_test',
53 'translatable' => FALSE,
55 'target_type' => 'node',
57 'type' => 'entity_reference',
60 $field_storage->save();
61 $field = FieldConfig::create([
62 'field_storage' => $field_storage,
63 'bundle' => 'test_bundle',
66 'handler_settings' => [
68 'view_name' => 'test_entity_reference',
69 'display_name' => 'entity_reference_1',
76 $this->field = $field;
80 * Confirm the expected results are returned.
82 * @param array $result
83 * Query results keyed by node type and nid.
85 protected function assertResults(array $result) {
87 foreach ($result as $node_type => $values) {
88 foreach ($values as $nid => $label) {
89 if (!$success = $this->nodes[$node_type][$nid] == trim(strip_tags($label))) {
90 // There was some error, so break.
95 $this->assertTrue($success, 'Views selection handler returned expected values.');
99 * Tests the selection handler.
101 public function testSelectionHandler() {
102 // Get values from selection handler.
103 $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field);
104 $result = $handler->getReferenceableEntities();
105 $this->assertResults($result);
109 * Tests the selection handler with a relationship.
111 public function testSelectionHandlerRelationship() {
112 // Add a relationship to the view.
113 $view = Views::getView('test_entity_reference');
115 $view->displayHandlers->get('default')->setOption('relationships', [
116 'test_relationship' => [
118 'table' => 'node_field_data',
123 // Add a filter depending on the relationship to the test view.
124 $view->displayHandlers->get('default')->setOption('filters', [
127 'table' => 'users_field_data',
129 'relationship' => 'test_relationship',
133 // Set view to distinct so only one row per node is returned.
134 $query_options = $view->display_handler->getOption('query');
135 $query_options['options']['distinct'] = TRUE;
136 $view->display_handler->setOption('query', $query_options);
139 // Get values from the selection handler.
140 $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($this->field);
141 $result = $handler->getReferenceableEntities();
142 $this->assertResults($result);