3 namespace Drupal\Tests\views\Functional\Plugin;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
9 use Drupal\Tests\views\Functional\ViewTestBase;
10 use Drupal\views\Views;
13 * Tests the entity reference display plugin.
17 * @see \Drupal\views\Plugin\views\display\EntityReference
19 class DisplayEntityReferenceTest extends ViewTestBase {
21 use EntityReferenceTestTrait;
24 * Views used by this test.
28 public static $testViews = ['test_display_entity_reference'];
35 public static $modules = ['entity_test', 'field', 'views_ui'];
38 * The used field name in the test.
45 * The used entity reference field name in the test.
49 protected $entityRefFieldName;
54 * @var \Drupal\field\Entity\FieldStorageConfig
56 protected $fieldStorage;
61 * @var \Drupal\field\Entity\FieldConfig
68 protected function setUp($import_test_views = TRUE) {
69 parent::setUp($import_test_views);
71 $this->drupalLogin($this->drupalCreateUser(['administer views']));
73 // Create the text field.
74 $this->fieldName = 'field_test_entity_ref_display';
75 $this->fieldStorage = FieldStorageConfig::create([
76 'field_name' => $this->fieldName,
77 'entity_type' => 'entity_test',
80 $this->fieldStorage->save();
82 // Create an instance of the text field on the content type.
83 $this->field = FieldConfig::create([
84 'field_storage' => $this->fieldStorage,
85 'bundle' => 'entity_test',
89 // Add an entity reference field to reference the same base table.
90 $this->entityRefFieldName = 'field_test_entity_ref_entity_ref';
91 $this->createEntityReferenceField('entity_test', 'entity_test', $this->entityRefFieldName, NULL, 'entity_test');
93 // Create some entities to search. Add a common string to the name and
94 // the text field in two entities so we can test that we can search in both.
95 for ($i = 0; $i < 5; $i++) {
97 'bundle' => 'entity_test',
98 'name' => 'name' . $i,
99 $this->fieldName => 'text',
102 'bundle' => 'entity_test',
104 $this->fieldName => 'text' . $i,
108 'bundle' => 'entity_test',
110 $this->fieldName => 'tex',
113 'bundle' => 'entity_test',
115 $this->fieldName => 'TEX',
118 'bundle' => 'entity_test',
120 $this->fieldName => 'sometext',
125 * Tests the entity reference display plugin.
127 public function testEntityReferenceDisplay() {
128 // Test that the 'title' settings are not shown.
129 $this->drupalGet('admin/structure/views/view/test_display_entity_reference/edit/entity_reference_1');
130 $this->assertSession()->linkByHrefNotExists('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/title');
132 // Add the new field to the fields.
133 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/field', ['name[entity_test__' . $this->fieldName . '.' . $this->fieldName . ']' => TRUE], t('Add and configure fields'));
134 $this->drupalPostForm(NULL, [], t('Apply'));
136 // Test that the right fields are shown on the display settings form.
137 $this->drupalGet('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options');
138 $this->assertText('Test entity: Name');
139 $this->assertText('Test entity: ' . $this->field->label());
141 // Add the new field to the search fields.
142 $this->drupalPostForm(NULL, ['style_options[search_fields][' . $this->fieldName . ']' => $this->fieldName], t('Apply'));
143 $this->drupalPostForm(NULL, [], t('Save'));
145 $view = Views::getView('test_display_entity_reference');
146 $view->setDisplay('entity_reference_1');
148 // Add the required settings to test a search operation.
151 'match_operator' => 'CONTAINS',
155 $view->display_handler->setOption('entity_reference_options', $options);
157 $this->executeView($view);
159 // Test that we have searched in both fields.
160 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
163 // Test the 'CONTAINS' match_operator.
164 $view = Views::getView('test_display_entity_reference');
165 $view->setDisplay('entity_reference_1');
168 'match_operator' => 'CONTAINS',
172 $view->display_handler->setOption('entity_reference_options', $options);
173 $this->executeView($view);
174 $this->assertEqual(count($view->result), 13, 'Search returned thirteen rows');
177 // Test the 'STARTS_WITH' match_operator.
178 $view = Views::getView('test_display_entity_reference');
179 $view->setDisplay('entity_reference_1');
182 'match_operator' => 'STARTS_WITH',
186 $view->display_handler->setOption('entity_reference_options', $options);
187 $this->executeView($view);
188 $this->assertEqual(count($view->result), 12, 'Search returned twelve rows');
191 // Test the '=' match_operator.
192 $view = Views::getView('test_display_entity_reference');
193 $view->setDisplay('entity_reference_1');
196 'match_operator' => '=',
200 $view->display_handler->setOption('entity_reference_options', $options);
201 $this->executeView($view);
202 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
205 // Add a relationship and a field using that relationship.
206 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/relationship', ['name[entity_test.user_id]' => TRUE], t('Add and configure relationships'));
207 $this->drupalPostForm(NULL, [], t('Apply'));
209 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/field', ['name[users_field_data.uid]' => TRUE], t('Add and configure fields'));
210 $this->drupalPostForm(NULL, [], t('Apply'));
212 // Add the new field to the search fields.
213 $this->drupalPostForm('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options', ['style_options[search_fields][uid]' => 'uid'], t('Apply'));
214 $this->drupalPostForm(NULL, [], t('Save'));
216 // Test that the search still works with the related field.
217 $view = Views::getView('test_display_entity_reference');
218 $view->setDisplay('entity_reference_1');
220 // Add the required settings to test a search operation.
223 'match_operator' => 'CONTAINS',
227 $view->display_handler->setOption('entity_reference_options', $options);
229 $this->executeView($view);
231 // Run validation when using a relationship to the same base table.
232 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
235 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/relationship', ['name[entity_test__field_test_entity_ref_entity_ref.field_test_entity_ref_entity_ref]' => TRUE], t('Add and configure relationships'));
236 $this->drupalPostForm(NULL, [], t('Apply'));
238 $this->drupalPostForm(NULL, [], t('Save'));
240 // Test that the search still works with the related field.
241 $view = Views::getView('test_display_entity_reference');
242 $view->setDisplay('entity_reference_1');
244 // Add IDs to trigger validation.
247 'match_operator' => 'CONTAINS',
251 $view->display_handler->setOption('entity_reference_options', $options);
253 $this->executeView($view);
255 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
257 // Test that the render() return empty array for empty result.
258 $view = Views::getView('test_display_entity_reference');
259 $view->setDisplay('entity_reference_1');
260 $render = $view->display_handler->render();
261 $this->assertSame([], $render, 'Render returned empty array');