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\field\Tests\EntityReference\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 // Add the new field to the fields.
129 $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'));
130 $this->drupalPostForm(NULL, [], t('Apply'));
132 // Test that the right fields are shown on the display settings form.
133 $this->drupalGet('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options');
134 $this->assertText('Test entity: Name');
135 $this->assertText('Test entity: ' . $this->field->label());
137 // Add the new field to the search fields.
138 $this->drupalPostForm(NULL, ['style_options[search_fields][' . $this->fieldName . ']' => $this->fieldName], t('Apply'));
139 $this->drupalPostForm(NULL, [], t('Save'));
141 $view = Views::getView('test_display_entity_reference');
142 $view->setDisplay('entity_reference_1');
144 // Add the required settings to test a search operation.
147 'match_operator' => 'CONTAINS',
151 $view->display_handler->setOption('entity_reference_options', $options);
153 $this->executeView($view);
155 // Test that we have searched in both fields.
156 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
159 // Test the 'CONTAINS' match_operator.
160 $view = Views::getView('test_display_entity_reference');
161 $view->setDisplay('entity_reference_1');
164 'match_operator' => 'CONTAINS',
168 $view->display_handler->setOption('entity_reference_options', $options);
169 $this->executeView($view);
170 $this->assertEqual(count($view->result), 13, 'Search returned thirteen rows');
173 // Test the 'STARTS_WITH' match_operator.
174 $view = Views::getView('test_display_entity_reference');
175 $view->setDisplay('entity_reference_1');
178 'match_operator' => 'STARTS_WITH',
182 $view->display_handler->setOption('entity_reference_options', $options);
183 $this->executeView($view);
184 $this->assertEqual(count($view->result), 12, 'Search returned twelve rows');
187 // Test the '=' match_operator.
188 $view = Views::getView('test_display_entity_reference');
189 $view->setDisplay('entity_reference_1');
192 'match_operator' => '=',
196 $view->display_handler->setOption('entity_reference_options', $options);
197 $this->executeView($view);
198 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
201 // Add a relationship and a field using that relationship.
202 $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'));
203 $this->drupalPostForm(NULL, [], t('Apply'));
205 $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'));
206 $this->drupalPostForm(NULL, [], t('Apply'));
208 // Add the new field to the search fields.
209 $this->drupalPostForm('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options', ['style_options[search_fields][uid]' => 'uid'], t('Apply'));
210 $this->drupalPostForm(NULL, [], t('Save'));
212 // Test that the search still works with the ralated field.
213 $view = Views::getView('test_display_entity_reference');
214 $view->setDisplay('entity_reference_1');
216 // Add the required settings to test a search operation.
219 'match_operator' => 'CONTAINS',
223 $view->display_handler->setOption('entity_reference_options', $options);
225 $this->executeView($view);
227 // Run validation when using a relationship to the same base table.
228 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
231 $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'));
232 $this->drupalPostForm(NULL, [], t('Apply'));
234 $this->drupalPostForm(NULL, [], t('Save'));
236 // Test that the search still works with the related field.
237 $view = Views::getView('test_display_entity_reference');
238 $view->setDisplay('entity_reference_1');
240 // Add IDs to trigger validation.
243 'match_operator' => 'CONTAINS',
247 $view->display_handler->setOption('entity_reference_options', $options);
249 $this->executeView($view);
251 $this->assertEqual(count($view->result), 2, 'Search returned two rows');
253 // Test that the render() return empty array for empty result.
254 $view = Views::getView('test_display_entity_reference');
255 $view->setDisplay('entity_reference_1');
256 $render = $view->display_handler->render();
257 $this->assertSame([], $render, 'Render returned empty array');