Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Functional / Plugin / DisplayEntityReferenceTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Plugin;
4
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;
11
12 /**
13  * Tests the entity reference display plugin.
14  *
15  * @group views
16  *
17  * @see \Drupal\views\Plugin\views\display\EntityReference
18  */
19 class DisplayEntityReferenceTest extends ViewTestBase {
20
21   use EntityReferenceTestTrait;
22
23   /**
24    * Views used by this test.
25    *
26    * @var array
27    */
28   public static $testViews = ['test_display_entity_reference'];
29
30   /**
31    * Modules to enable.
32    *
33    * @var array
34    */
35   public static $modules = ['entity_test', 'field', 'views_ui'];
36
37   /**
38    * The used field name in the test.
39    *
40    * @var string
41    */
42   protected $fieldName;
43
44   /**
45    * The used entity reference field name in the test.
46    *
47    * @var string
48    */
49   protected $entityRefFieldName;
50
51   /**
52    * The field storage.
53    *
54    * @var \Drupal\field\Entity\FieldStorageConfig
55    */
56   protected $fieldStorage;
57
58   /**
59    * The field config.
60    *
61    * @var \Drupal\field\Entity\FieldConfig
62    */
63   protected $field;
64
65   /**
66    * {@inheritdoc}
67    */
68   protected function setUp($import_test_views = TRUE) {
69     parent::setUp($import_test_views);
70
71     $this->drupalLogin($this->drupalCreateUser(['administer views']));
72
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',
78       'type' => 'text',
79     ]);
80     $this->fieldStorage->save();
81
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',
86     ]);
87     $this->field->save();
88
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');
92
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++) {
96       EntityTest::create([
97         'bundle' => 'entity_test',
98         'name' => 'name' . $i,
99         $this->fieldName => 'text',
100       ])->save();
101       EntityTest::create([
102         'bundle' => 'entity_test',
103         'name' => 'name',
104         $this->fieldName => 'text' . $i,
105       ])->save();
106     }
107     EntityTest::create([
108       'bundle' => 'entity_test',
109       'name' => 'name',
110       $this->fieldName => 'tex',
111     ])->save();
112     EntityTest::create([
113       'bundle' => 'entity_test',
114       'name' => 'name',
115       $this->fieldName => 'TEX',
116     ])->save();
117     EntityTest::create([
118       'bundle' => 'entity_test',
119       'name' => 'name',
120       $this->fieldName => 'sometext',
121     ])->save();
122   }
123
124   /**
125    * Tests the entity reference display plugin.
126    */
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');
131
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'));
135
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());
140
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'));
144
145     $view = Views::getView('test_display_entity_reference');
146     $view->setDisplay('entity_reference_1');
147
148     // Add the required settings to test a search operation.
149     $options = [
150       'match' => '1',
151       'match_operator' => 'CONTAINS',
152       'limit' => 0,
153       'ids' => NULL,
154     ];
155     $view->display_handler->setOption('entity_reference_options', $options);
156
157     $this->executeView($view);
158
159     // Test that we have searched in both fields.
160     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
161     $view->destroy();
162
163     // Test the 'CONTAINS' match_operator.
164     $view = Views::getView('test_display_entity_reference');
165     $view->setDisplay('entity_reference_1');
166     $options = [
167       'match' => 'tex',
168       'match_operator' => 'CONTAINS',
169       'limit' => 0,
170       'ids' => NULL,
171     ];
172     $view->display_handler->setOption('entity_reference_options', $options);
173     $this->executeView($view);
174     $this->assertEqual(count($view->result), 13, 'Search returned thirteen rows');
175     $view->destroy();
176
177     // Test the 'STARTS_WITH' match_operator.
178     $view = Views::getView('test_display_entity_reference');
179     $view->setDisplay('entity_reference_1');
180     $options = [
181       'match' => 'tex',
182       'match_operator' => 'STARTS_WITH',
183       'limit' => 0,
184       'ids' => NULL,
185     ];
186     $view->display_handler->setOption('entity_reference_options', $options);
187     $this->executeView($view);
188     $this->assertEqual(count($view->result), 12, 'Search returned twelve rows');
189     $view->destroy();
190
191     // Test the '=' match_operator.
192     $view = Views::getView('test_display_entity_reference');
193     $view->setDisplay('entity_reference_1');
194     $options = [
195       'match' => 'tex',
196       'match_operator' => '=',
197       'limit' => 0,
198       'ids' => NULL,
199     ];
200     $view->display_handler->setOption('entity_reference_options', $options);
201     $this->executeView($view);
202     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
203     $view->destroy();
204
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'));
208
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'));
211
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'));
215
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');
219
220     // Add the required settings to test a search operation.
221     $options = [
222       'match' => '2',
223       'match_operator' => 'CONTAINS',
224       'limit' => 0,
225       'ids' => NULL,
226     ];
227     $view->display_handler->setOption('entity_reference_options', $options);
228
229     $this->executeView($view);
230
231     // Run validation when using a relationship to the same base table.
232     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
233     $view->destroy();
234
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'));
237
238     $this->drupalPostForm(NULL, [], t('Save'));
239
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');
243
244     // Add IDs to trigger validation.
245     $options = [
246       'match' => '1',
247       'match_operator' => 'CONTAINS',
248       'limit' => 0,
249       'ids' => [1, 2],
250     ];
251     $view->display_handler->setOption('entity_reference_options', $options);
252
253     $this->executeView($view);
254
255     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
256
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');
262   }
263
264 }