daad14896e47b44a08bc38ed6daec0c2f00817bc
[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\field\Tests\EntityReference\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     // 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'));
131
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());
136
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'));
140
141     $view = Views::getView('test_display_entity_reference');
142     $view->setDisplay('entity_reference_1');
143
144     // Add the required settings to test a search operation.
145     $options = [
146       'match' => '1',
147       'match_operator' => 'CONTAINS',
148       'limit' => 0,
149       'ids' => NULL,
150     ];
151     $view->display_handler->setOption('entity_reference_options', $options);
152
153     $this->executeView($view);
154
155     // Test that we have searched in both fields.
156     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
157     $view->destroy();
158
159     // Test the 'CONTAINS' match_operator.
160     $view = Views::getView('test_display_entity_reference');
161     $view->setDisplay('entity_reference_1');
162     $options = [
163       'match' => 'tex',
164       'match_operator' => 'CONTAINS',
165       'limit' => 0,
166       'ids' => NULL,
167     ];
168     $view->display_handler->setOption('entity_reference_options', $options);
169     $this->executeView($view);
170     $this->assertEqual(count($view->result), 13, 'Search returned thirteen rows');
171     $view->destroy();
172
173     // Test the 'STARTS_WITH' match_operator.
174     $view = Views::getView('test_display_entity_reference');
175     $view->setDisplay('entity_reference_1');
176     $options = [
177       'match' => 'tex',
178       'match_operator' => 'STARTS_WITH',
179       'limit' => 0,
180       'ids' => NULL,
181     ];
182     $view->display_handler->setOption('entity_reference_options', $options);
183     $this->executeView($view);
184     $this->assertEqual(count($view->result), 12, 'Search returned twelve rows');
185     $view->destroy();
186
187     // Test the '=' match_operator.
188     $view = Views::getView('test_display_entity_reference');
189     $view->setDisplay('entity_reference_1');
190     $options = [
191       'match' => 'tex',
192       'match_operator' => '=',
193       'limit' => 0,
194       'ids' => NULL,
195     ];
196     $view->display_handler->setOption('entity_reference_options', $options);
197     $this->executeView($view);
198     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
199     $view->destroy();
200
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'));
204
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'));
207
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'));
211
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');
215
216     // Add the required settings to test a search operation.
217     $options = [
218       'match' => '2',
219       'match_operator' => 'CONTAINS',
220       'limit' => 0,
221       'ids' => NULL,
222     ];
223     $view->display_handler->setOption('entity_reference_options', $options);
224
225     $this->executeView($view);
226
227     // Run validation when using a relationship to the same base table.
228     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
229     $view->destroy();
230
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'));
233
234     $this->drupalPostForm(NULL, [], t('Save'));
235
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');
239
240     // Add IDs to trigger validation.
241     $options = [
242       'match' => '1',
243       'match_operator' => 'CONTAINS',
244       'limit' => 0,
245       'ids' => [1, 2],
246     ];
247     $view->display_handler->setOption('entity_reference_options', $options);
248
249     $this->executeView($view);
250
251     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
252
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');
258   }
259
260 }