Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\field\Tests\EntityReference;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
6 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@0
|
8 use Drupal\field_ui\Tests\FieldUiTestTrait;
|
Chris@0
|
9 use Drupal\node\Entity\Node;
|
Chris@0
|
10 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
11 use Drupal\taxonomy\Entity\Vocabulary;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Tests for the administrative UI.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @group entity_reference
|
Chris@0
|
17 */
|
Chris@0
|
18 class EntityReferenceAdminTest extends WebTestBase {
|
Chris@0
|
19
|
Chris@0
|
20 use FieldUiTestTrait;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Modules to install.
|
Chris@0
|
24 *
|
Chris@0
|
25 * Enable path module to ensure that the selection handler does not fail for
|
Chris@0
|
26 * entities with a path field.
|
Chris@0
|
27 * Enable views_ui module to see the no_view_help text.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var array
|
Chris@0
|
30 */
|
Chris@0
|
31 public static $modules = ['node', 'field_ui', 'path', 'taxonomy', 'block', 'views_ui'];
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * The name of the content type created for testing purposes.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var string
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $type;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 protected function setUp() {
|
Chris@0
|
44 parent::setUp();
|
Chris@0
|
45 $this->drupalPlaceBlock('system_breadcrumb_block');
|
Chris@0
|
46
|
Chris@0
|
47 // Create a content type, with underscores.
|
Chris@0
|
48 $type_name = strtolower($this->randomMachineName(8)) . '_test';
|
Chris@0
|
49 $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
|
Chris@0
|
50 $this->type = $type->id();
|
Chris@0
|
51
|
Chris@0
|
52 // Create test user.
|
Chris@0
|
53 $admin_user = $this->drupalCreateUser([
|
Chris@0
|
54 'access content',
|
Chris@0
|
55 'administer node fields',
|
Chris@0
|
56 'administer node display',
|
Chris@0
|
57 'administer views',
|
Chris@0
|
58 'create ' . $type_name . ' content',
|
Chris@0
|
59 'edit own ' . $type_name . ' content',
|
Chris@0
|
60 ]);
|
Chris@0
|
61 $this->drupalLogin($admin_user);
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Tests the Entity Reference Admin UI.
|
Chris@0
|
66 */
|
Chris@0
|
67 public function testFieldAdminHandler() {
|
Chris@0
|
68 $bundle_path = 'admin/structure/types/manage/' . $this->type;
|
Chris@0
|
69
|
Chris@0
|
70 // First step: 'Add new field' on the 'Manage fields' page.
|
Chris@0
|
71 $this->drupalGet($bundle_path . '/fields/add-field');
|
Chris@0
|
72
|
Chris@0
|
73 // Check if the commonly referenced entity types appear in the list.
|
Chris@0
|
74 $this->assertOption('edit-new-storage-type', 'field_ui:entity_reference:node');
|
Chris@0
|
75 $this->assertOption('edit-new-storage-type', 'field_ui:entity_reference:user');
|
Chris@0
|
76
|
Chris@0
|
77 $this->drupalPostForm(NULL, [
|
Chris@0
|
78 'label' => 'Test label',
|
Chris@0
|
79 'field_name' => 'test',
|
Chris@0
|
80 'new_storage_type' => 'entity_reference',
|
Chris@0
|
81 ], t('Save and continue'));
|
Chris@0
|
82
|
Chris@0
|
83 // Node should be selected by default.
|
Chris@0
|
84 $this->assertFieldByName('settings[target_type]', 'node');
|
Chris@0
|
85
|
Chris@0
|
86 // Check that all entity types can be referenced.
|
Chris@0
|
87 $this->assertFieldSelectOptions('settings[target_type]', array_keys(\Drupal::entityManager()->getDefinitions()));
|
Chris@0
|
88
|
Chris@0
|
89 // Second step: 'Field settings' form.
|
Chris@0
|
90 $this->drupalPostForm(NULL, [], t('Save field settings'));
|
Chris@0
|
91
|
Chris@0
|
92 // The base handler should be selected by default.
|
Chris@0
|
93 $this->assertFieldByName('settings[handler]', 'default:node');
|
Chris@0
|
94
|
Chris@0
|
95 // The base handler settings should be displayed.
|
Chris@0
|
96 $entity_type_id = 'node';
|
Chris@0
|
97 $bundles = $this->container->get('entity_type.bundle.info')->getBundleInfo($entity_type_id);
|
Chris@0
|
98 foreach ($bundles as $bundle_name => $bundle_info) {
|
Chris@0
|
99 $this->assertFieldByName('settings[handler_settings][target_bundles][' . $bundle_name . ']');
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 reset($bundles);
|
Chris@0
|
103
|
Chris@0
|
104 // Test the sort settings.
|
Chris@0
|
105 // Option 0: no sort.
|
Chris@0
|
106 $this->assertFieldByName('settings[handler_settings][sort][field]', '_none');
|
Chris@0
|
107 $this->assertNoFieldByName('settings[handler_settings][sort][direction]');
|
Chris@0
|
108 // Option 1: sort by field.
|
Chris@0
|
109 $this->drupalPostAjaxForm(NULL, ['settings[handler_settings][sort][field]' => 'nid'], 'settings[handler_settings][sort][field]');
|
Chris@0
|
110 $this->assertFieldByName('settings[handler_settings][sort][direction]', 'ASC');
|
Chris@0
|
111
|
Chris@0
|
112 // Test that a non-translatable base field is a sort option.
|
Chris@0
|
113 $this->assertFieldByXPath("//select[@name='settings[handler_settings][sort][field]']/option[@value='nid']");
|
Chris@0
|
114 // Test that a translatable base field is a sort option.
|
Chris@0
|
115 $this->assertFieldByXPath("//select[@name='settings[handler_settings][sort][field]']/option[@value='title']");
|
Chris@0
|
116 // Test that a configurable field is a sort option.
|
Chris@0
|
117 $this->assertFieldByXPath("//select[@name='settings[handler_settings][sort][field]']/option[@value='body.value']");
|
Chris@0
|
118
|
Chris@0
|
119 // Set back to no sort.
|
Chris@0
|
120 $this->drupalPostAjaxForm(NULL, ['settings[handler_settings][sort][field]' => '_none'], 'settings[handler_settings][sort][field]');
|
Chris@0
|
121 $this->assertNoFieldByName('settings[handler_settings][sort][direction]');
|
Chris@0
|
122
|
Chris@0
|
123 // Third step: confirm.
|
Chris@0
|
124 $this->drupalPostForm(NULL, [
|
Chris@0
|
125 'required' => '1',
|
Chris@0
|
126 'settings[handler_settings][target_bundles][' . key($bundles) . ']' => key($bundles),
|
Chris@0
|
127 ], t('Save settings'));
|
Chris@0
|
128
|
Chris@0
|
129 // Check that the field appears in the overview form.
|
Chris@0
|
130 $this->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="field-test"]/td[1]', 'Test label', 'Field was created and appears in the overview page.');
|
Chris@0
|
131
|
Chris@0
|
132 // Check that the field settings form can be submitted again, even when the
|
Chris@0
|
133 // field is required.
|
Chris@0
|
134 // The first 'Edit' link is for the Body field.
|
Chris@0
|
135 $this->clickLink(t('Edit'), 1);
|
Chris@0
|
136 $this->drupalPostForm(NULL, [], t('Save settings'));
|
Chris@0
|
137
|
Chris@0
|
138 // Switch the target type to 'taxonomy_term' and check that the settings
|
Chris@0
|
139 // specific to its selection handler are displayed.
|
Chris@0
|
140 $field_name = 'node.' . $this->type . '.field_test';
|
Chris@0
|
141 $edit = [
|
Chris@0
|
142 'settings[target_type]' => 'taxonomy_term',
|
Chris@0
|
143 ];
|
Chris@0
|
144 $this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
|
Chris@0
|
145 $this->drupalGet($bundle_path . '/fields/' . $field_name);
|
Chris@0
|
146 $this->assertFieldByName('settings[handler_settings][auto_create]');
|
Chris@0
|
147
|
Chris@0
|
148 // Switch the target type to 'user' and check that the settings specific to
|
Chris@0
|
149 // its selection handler are displayed.
|
Chris@0
|
150 $field_name = 'node.' . $this->type . '.field_test';
|
Chris@0
|
151 $edit = [
|
Chris@0
|
152 'settings[target_type]' => 'user',
|
Chris@0
|
153 ];
|
Chris@0
|
154 $this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
|
Chris@0
|
155 $this->drupalGet($bundle_path . '/fields/' . $field_name);
|
Chris@0
|
156 $this->assertFieldByName('settings[handler_settings][filter][type]', '_none');
|
Chris@0
|
157
|
Chris@0
|
158 // Switch the target type to 'node'.
|
Chris@0
|
159 $field_name = 'node.' . $this->type . '.field_test';
|
Chris@0
|
160 $edit = [
|
Chris@0
|
161 'settings[target_type]' => 'node',
|
Chris@0
|
162 ];
|
Chris@0
|
163 $this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
|
Chris@0
|
164
|
Chris@0
|
165 // Try to select the views handler.
|
Chris@0
|
166 $edit = [
|
Chris@0
|
167 'settings[handler]' => 'views',
|
Chris@0
|
168 ];
|
Chris@0
|
169 $this->drupalPostAjaxForm($bundle_path . '/fields/' . $field_name, $edit, 'settings[handler]');
|
Chris@0
|
170 $this->assertRaw(t('No eligible views were found. <a href=":create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href=":existing">existing view</a>.', [
|
Chris@0
|
171 ':create' => \Drupal::url('views_ui.add'),
|
Chris@0
|
172 ':existing' => \Drupal::url('entity.view.collection'),
|
Chris@0
|
173 ]));
|
Chris@0
|
174 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
175 // If no eligible view is available we should see a message.
|
Chris@0
|
176 $this->assertText('The views entity selection mode requires a view.');
|
Chris@0
|
177
|
Chris@0
|
178 // Enable the entity_reference_test module which creates an eligible view.
|
Chris@0
|
179 $this->container->get('module_installer')->install(['entity_reference_test']);
|
Chris@0
|
180 $this->resetAll();
|
Chris@0
|
181 $this->drupalGet($bundle_path . '/fields/' . $field_name);
|
Chris@0
|
182 $this->drupalPostAjaxForm($bundle_path . '/fields/' . $field_name, $edit, 'settings[handler]');
|
Chris@0
|
183 $edit = [
|
Chris@0
|
184 'settings[handler_settings][view][view_and_display]' => 'test_entity_reference:entity_reference_1',
|
Chris@0
|
185 ];
|
Chris@0
|
186 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
187 $this->assertResponse(200);
|
Chris@0
|
188
|
Chris@0
|
189 // Switch the target type to 'entity_test'.
|
Chris@0
|
190 $edit = [
|
Chris@0
|
191 'settings[target_type]' => 'entity_test',
|
Chris@0
|
192 ];
|
Chris@0
|
193 $this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings'));
|
Chris@0
|
194 $this->drupalGet($bundle_path . '/fields/' . $field_name);
|
Chris@0
|
195 $edit = [
|
Chris@0
|
196 'settings[handler]' => 'views',
|
Chris@0
|
197 ];
|
Chris@0
|
198 $this->drupalPostAjaxForm($bundle_path . '/fields/' . $field_name, $edit, 'settings[handler]');
|
Chris@0
|
199 $edit = [
|
Chris@0
|
200 'required' => FALSE,
|
Chris@0
|
201 'settings[handler_settings][view][view_and_display]' => 'test_entity_reference_entity_test:entity_reference_1',
|
Chris@0
|
202 ];
|
Chris@0
|
203 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
204 $this->assertResponse(200);
|
Chris@0
|
205
|
Chris@0
|
206 // Create a new view and display it as a entity reference.
|
Chris@0
|
207 $edit = [
|
Chris@0
|
208 'id' => 'node_test_view',
|
Chris@0
|
209 'label' => 'Node Test View',
|
Chris@0
|
210 'show[wizard_key]' => 'node',
|
Chris@0
|
211 'show[sort]' => 'none',
|
Chris@0
|
212 'page[create]' => 1,
|
Chris@0
|
213 'page[title]' => 'Test Node View',
|
Chris@0
|
214 'page[path]' => 'test/node/view',
|
Chris@0
|
215 'page[style][style_plugin]' => 'default',
|
Chris@0
|
216 'page[style][row_plugin]' => 'fields',
|
Chris@0
|
217 ];
|
Chris@0
|
218 $this->drupalPostForm('admin/structure/views/add', $edit, t('Save and edit'));
|
Chris@0
|
219 $this->drupalPostForm(NULL, [], t('Duplicate as Entity Reference'));
|
Chris@0
|
220 $this->clickLink(t('Settings'));
|
Chris@0
|
221 $edit = [
|
Chris@0
|
222 'style_options[search_fields][title]' => 'title',
|
Chris@0
|
223 ];
|
Chris@0
|
224 $this->drupalPostForm(NULL, $edit, t('Apply'));
|
Chris@0
|
225
|
Chris@0
|
226 // Set sort to NID ascending.
|
Chris@0
|
227 $edit = [
|
Chris@0
|
228 'name[node_field_data.nid]' => 1,
|
Chris@0
|
229 ];
|
Chris@0
|
230 $this->drupalPostForm('admin/structure/views/nojs/add-handler/node_test_view/entity_reference_1/sort', $edit, t('Add and configure sort criteria'));
|
Chris@0
|
231 $this->drupalPostForm(NULL, NULL, t('Apply'));
|
Chris@0
|
232
|
Chris@0
|
233 $this->drupalPostForm('admin/structure/views/view/node_test_view/edit/entity_reference_1', [], t('Save'));
|
Chris@0
|
234 $this->clickLink(t('Settings'));
|
Chris@0
|
235
|
Chris@0
|
236 // Create a test entity reference field.
|
Chris@0
|
237 $field_name = 'test_entity_ref_field';
|
Chris@0
|
238 $edit = [
|
Chris@0
|
239 'new_storage_type' => 'field_ui:entity_reference:node',
|
Chris@0
|
240 'label' => 'Test Entity Reference Field',
|
Chris@0
|
241 'field_name' => $field_name,
|
Chris@0
|
242 ];
|
Chris@0
|
243 $this->drupalPostForm($bundle_path . '/fields/add-field', $edit, t('Save and continue'));
|
Chris@0
|
244
|
Chris@0
|
245 // Set to unlimited.
|
Chris@0
|
246 $edit = [
|
Chris@0
|
247 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
Chris@0
|
248 ];
|
Chris@0
|
249 $this->drupalPostForm(NULL, $edit, t('Save field settings'));
|
Chris@0
|
250
|
Chris@0
|
251 // Add the view to the test field.
|
Chris@0
|
252 $edit = [
|
Chris@0
|
253 'settings[handler]' => 'views',
|
Chris@0
|
254 ];
|
Chris@0
|
255 $this->drupalPostAjaxForm(NULL, $edit, 'settings[handler]');
|
Chris@0
|
256 $edit = [
|
Chris@0
|
257 'required' => FALSE,
|
Chris@0
|
258 'settings[handler_settings][view][view_and_display]' => 'node_test_view:entity_reference_1',
|
Chris@0
|
259 ];
|
Chris@0
|
260 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
261
|
Chris@0
|
262 // Create nodes.
|
Chris@0
|
263 $node1 = Node::create([
|
Chris@0
|
264 'type' => $this->type,
|
Chris@0
|
265 'title' => 'Foo Node',
|
Chris@0
|
266 ]);
|
Chris@0
|
267 $node1->save();
|
Chris@0
|
268 $node2 = Node::create([
|
Chris@0
|
269 'type' => $this->type,
|
Chris@0
|
270 'title' => 'Foo Node',
|
Chris@0
|
271 ]);
|
Chris@0
|
272 $node2->save();
|
Chris@0
|
273
|
Chris@0
|
274 // Try to add a new node and fill the entity reference field.
|
Chris@0
|
275 $this->drupalGet('node/add/' . $this->type);
|
Chris@0
|
276 $result = $this->xpath('//input[@name="field_test_entity_ref_field[0][target_id]" and contains(@data-autocomplete-path, "/entity_reference_autocomplete/node/views/")]');
|
Chris@0
|
277 $target_url = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']);
|
Chris@0
|
278 $this->drupalGet($target_url, ['query' => ['q' => 'Foo']]);
|
Chris@0
|
279 $this->assertRaw($node1->getTitle() . ' (' . $node1->id() . ')');
|
Chris@0
|
280 $this->assertRaw($node2->getTitle() . ' (' . $node2->id() . ')');
|
Chris@0
|
281
|
Chris@0
|
282 // Try to add a new node, fill the entity reference field and submit the
|
Chris@0
|
283 // form.
|
Chris@0
|
284 $this->drupalPostForm('node/add/' . $this->type, [], t('Add another item'));
|
Chris@0
|
285 $edit = [
|
Chris@0
|
286 'title[0][value]' => 'Example',
|
Chris@0
|
287 'field_test_entity_ref_field[0][target_id]' => 'Foo Node (' . $node1->id() . ')',
|
Chris@0
|
288 'field_test_entity_ref_field[1][target_id]' => 'Foo Node (' . $node2->id() . ')',
|
Chris@0
|
289 ];
|
Chris@0
|
290 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
291 $this->assertResponse(200);
|
Chris@0
|
292
|
Chris@0
|
293 $edit = [
|
Chris@0
|
294 'title[0][value]' => 'Example',
|
Chris@0
|
295 'field_test_entity_ref_field[0][target_id]' => 'Test'
|
Chris@0
|
296 ];
|
Chris@0
|
297 $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
|
Chris@0
|
298
|
Chris@0
|
299 // Assert that entity reference autocomplete field is validated.
|
Chris@0
|
300 $this->assertText(t('There are no entities matching "@entity"', ['@entity' => 'Test']));
|
Chris@0
|
301
|
Chris@0
|
302 $edit = [
|
Chris@0
|
303 'title[0][value]' => 'Test',
|
Chris@0
|
304 'field_test_entity_ref_field[0][target_id]' => $node1->getTitle()
|
Chris@0
|
305 ];
|
Chris@0
|
306 $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
|
Chris@0
|
307
|
Chris@0
|
308 // Assert the results multiple times to avoid sorting problem of nodes with
|
Chris@0
|
309 // the same title.
|
Chris@0
|
310 $this->assertText(t('Multiple entities match this reference;'));
|
Chris@0
|
311 $this->assertText(t("@node1", ['@node1' => $node1->getTitle() . ' (' . $node1->id() . ')']));
|
Chris@0
|
312 $this->assertText(t("@node2", ['@node2' => $node2->getTitle() . ' (' . $node2->id() . ')']));
|
Chris@0
|
313 $this->assertText(t('Specify the one you want by appending the id in parentheses, like "@example".', ['@example' => $node2->getTitle() . ' (' . $node2->id() . ')']));
|
Chris@0
|
314
|
Chris@0
|
315 $edit = [
|
Chris@0
|
316 'title[0][value]' => 'Test',
|
Chris@0
|
317 'field_test_entity_ref_field[0][target_id]' => $node1->getTitle() . ' (' . $node1->id() . ')'
|
Chris@0
|
318 ];
|
Chris@0
|
319 $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save'));
|
Chris@0
|
320 $this->assertLink($node1->getTitle());
|
Chris@0
|
321
|
Chris@0
|
322 // Tests adding default values to autocomplete widgets.
|
Chris@0
|
323 Vocabulary::create(['vid' => 'tags', 'name' => 'tags'])->save();
|
Chris@0
|
324 $taxonomy_term_field_name = $this->createEntityReferenceField('taxonomy_term', ['tags']);
|
Chris@0
|
325 $field_path = 'node.' . $this->type . '.field_' . $taxonomy_term_field_name;
|
Chris@0
|
326 $this->drupalGet($bundle_path . '/fields/' . $field_path . '/storage');
|
Chris@0
|
327 $edit = [
|
Chris@0
|
328 'cardinality' => -1,
|
Chris@0
|
329 ];
|
Chris@0
|
330 $this->drupalPostForm(NULL, $edit, t('Save field settings'));
|
Chris@0
|
331 $this->drupalGet($bundle_path . '/fields/' . $field_path);
|
Chris@0
|
332 $term_name = $this->randomString();
|
Chris@0
|
333 $edit = [
|
Chris@0
|
334 // This must be set before new entities will be auto-created.
|
Chris@0
|
335 'settings[handler_settings][auto_create]' => 1,
|
Chris@0
|
336 ];
|
Chris@0
|
337 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
338 $this->drupalGet($bundle_path . '/fields/' . $field_path);
|
Chris@0
|
339 $edit = [
|
Chris@0
|
340 // A term that doesn't yet exist.
|
Chris@0
|
341 'default_value_input[field_' . $taxonomy_term_field_name . '][0][target_id]' => $term_name,
|
Chris@0
|
342 ];
|
Chris@0
|
343 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
344 // The term should now exist.
|
Chris@0
|
345 $term = taxonomy_term_load_multiple_by_name($term_name, 'tags')[1];
|
Chris@0
|
346 $this->assertIdentical(1, count($term), 'Taxonomy term was auto created when set as field default.');
|
Chris@0
|
347 }
|
Chris@0
|
348
|
Chris@0
|
349 /**
|
Chris@0
|
350 * Tests the formatters for the Entity References.
|
Chris@0
|
351 */
|
Chris@0
|
352 public function testAvailableFormatters() {
|
Chris@0
|
353 // Create a new vocabulary.
|
Chris@0
|
354 Vocabulary::create(['vid' => 'tags', 'name' => 'tags'])->save();
|
Chris@0
|
355
|
Chris@0
|
356 // Create entity reference field with taxonomy term as a target.
|
Chris@0
|
357 $taxonomy_term_field_name = $this->createEntityReferenceField('taxonomy_term', ['tags']);
|
Chris@0
|
358
|
Chris@0
|
359 // Create entity reference field with user as a target.
|
Chris@0
|
360 $user_field_name = $this->createEntityReferenceField('user');
|
Chris@0
|
361
|
Chris@0
|
362 // Create entity reference field with node as a target.
|
Chris@0
|
363 $node_field_name = $this->createEntityReferenceField('node', [$this->type]);
|
Chris@0
|
364
|
Chris@0
|
365 // Create entity reference field with date format as a target.
|
Chris@0
|
366 $date_format_field_name = $this->createEntityReferenceField('date_format');
|
Chris@0
|
367
|
Chris@0
|
368 // Display all newly created Entity Reference configuration.
|
Chris@0
|
369 $this->drupalGet('admin/structure/types/manage/' . $this->type . '/display');
|
Chris@0
|
370
|
Chris@0
|
371 // Check for Taxonomy Term select box values.
|
Chris@0
|
372 // Test if Taxonomy Term Entity Reference Field has the correct formatters.
|
Chris@0
|
373 $this->assertFieldSelectOptions('fields[field_' . $taxonomy_term_field_name . '][type]', [
|
Chris@0
|
374 'entity_reference_label',
|
Chris@0
|
375 'entity_reference_entity_id',
|
Chris@0
|
376 'entity_reference_rss_category',
|
Chris@0
|
377 'entity_reference_entity_view',
|
Chris@0
|
378 ]);
|
Chris@0
|
379
|
Chris@0
|
380 // Test if User Reference Field has the correct formatters.
|
Chris@0
|
381 // Author should be available for this field.
|
Chris@0
|
382 // RSS Category should not be available for this field.
|
Chris@0
|
383 $this->assertFieldSelectOptions('fields[field_' . $user_field_name . '][type]', [
|
Chris@0
|
384 'author',
|
Chris@0
|
385 'entity_reference_entity_id',
|
Chris@0
|
386 'entity_reference_entity_view',
|
Chris@0
|
387 'entity_reference_label',
|
Chris@0
|
388 ]);
|
Chris@0
|
389
|
Chris@0
|
390 // Test if Node Entity Reference Field has the correct formatters.
|
Chris@0
|
391 // RSS Category should not be available for this field.
|
Chris@0
|
392 $this->assertFieldSelectOptions('fields[field_' . $node_field_name . '][type]', [
|
Chris@0
|
393 'entity_reference_label',
|
Chris@0
|
394 'entity_reference_entity_id',
|
Chris@0
|
395 'entity_reference_entity_view',
|
Chris@0
|
396 ]);
|
Chris@0
|
397
|
Chris@0
|
398 // Test if Date Format Reference Field has the correct formatters.
|
Chris@0
|
399 // RSS Category & Entity View should not be available for this field.
|
Chris@0
|
400 // This could be any field without a ViewBuilder.
|
Chris@0
|
401 $this->assertFieldSelectOptions('fields[field_' . $date_format_field_name . '][type]', [
|
Chris@0
|
402 'entity_reference_label',
|
Chris@0
|
403 'entity_reference_entity_id',
|
Chris@0
|
404 ]);
|
Chris@0
|
405 }
|
Chris@0
|
406
|
Chris@0
|
407 /**
|
Chris@0
|
408 * Tests field settings for an entity reference field when the field has
|
Chris@0
|
409 * multiple target bundles and is set to auto-create the target entity.
|
Chris@0
|
410 */
|
Chris@0
|
411 public function testMultipleTargetBundles() {
|
Chris@0
|
412 /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */
|
Chris@0
|
413 $vocabularies = [];
|
Chris@0
|
414 for ($i = 0; $i < 2; $i++) {
|
Chris@0
|
415 $vid = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
416 $vocabularies[$i] = Vocabulary::create([
|
Chris@0
|
417 'name' => $this->randomString(),
|
Chris@0
|
418 'vid' => $vid,
|
Chris@0
|
419 ]);
|
Chris@0
|
420 $vocabularies[$i]->save();
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 // Create a new field pointing to the first vocabulary.
|
Chris@0
|
424 $field_name = $this->createEntityReferenceField('taxonomy_term', [$vocabularies[0]->id()]);
|
Chris@0
|
425 $field_name = "field_$field_name";
|
Chris@0
|
426 $field_id = 'node.' . $this->type . '.' . $field_name;
|
Chris@0
|
427 $path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $field_id;
|
Chris@0
|
428
|
Chris@0
|
429 $this->drupalGet($path);
|
Chris@0
|
430
|
Chris@0
|
431 // Expect that there's no 'auto_create_bundle' selected.
|
Chris@0
|
432 $this->assertNoFieldByName('settings[handler_settings][auto_create_bundle]');
|
Chris@0
|
433
|
Chris@0
|
434 $edit = [
|
Chris@0
|
435 'settings[handler_settings][target_bundles][' . $vocabularies[1]->id() . ']' => TRUE,
|
Chris@0
|
436 ];
|
Chris@0
|
437 // Enable the second vocabulary as a target bundle.
|
Chris@0
|
438 $this->drupalPostAjaxForm($path, $edit, key($edit));
|
Chris@0
|
439 // Expect a select element with the two vocabularies as options.
|
Chris@0
|
440 $this->assertFieldByXPath("//select[@name='settings[handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[0]->id() . "']");
|
Chris@0
|
441 $this->assertFieldByXPath("//select[@name='settings[handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[1]->id() . "']");
|
Chris@0
|
442
|
Chris@0
|
443 $edit = [
|
Chris@0
|
444 'settings[handler_settings][auto_create]' => TRUE,
|
Chris@0
|
445 'settings[handler_settings][auto_create_bundle]' => $vocabularies[1]->id(),
|
Chris@0
|
446 ];
|
Chris@0
|
447 $this->drupalPostForm(NULL, $edit, t('Save settings'));
|
Chris@0
|
448
|
Chris@0
|
449 /** @var \Drupal\field\Entity\FieldConfig $field_config */
|
Chris@0
|
450 $field_config = FieldConfig::load($field_id);
|
Chris@0
|
451 // Expect that the target bundle has been saved in the backend.
|
Chris@0
|
452 $this->assertEqual($field_config->getSetting('handler_settings')['auto_create_bundle'], $vocabularies[1]->id());
|
Chris@0
|
453
|
Chris@0
|
454 // Delete the other bundle. Field config should not be affected.
|
Chris@0
|
455 $vocabularies[0]->delete();
|
Chris@0
|
456 $field_config = FieldConfig::load($field_id);
|
Chris@0
|
457 $this->assertTrue($field_config->getSetting('handler_settings')['auto_create']);
|
Chris@0
|
458 $this->assertIdentical($field_config->getSetting('handler_settings')['auto_create_bundle'], $vocabularies[1]->id());
|
Chris@0
|
459
|
Chris@0
|
460 // Delete the bundle set for entity auto-creation. Auto-created settings
|
Chris@0
|
461 // should be reset (no auto-creation).
|
Chris@0
|
462 $vocabularies[1]->delete();
|
Chris@0
|
463 $field_config = FieldConfig::load($field_id);
|
Chris@0
|
464 $this->assertFalse($field_config->getSetting('handler_settings')['auto_create']);
|
Chris@0
|
465 $this->assertFalse(isset($field_config->getSetting('handler_settings')['auto_create_bundle']));
|
Chris@0
|
466 }
|
Chris@0
|
467
|
Chris@0
|
468 /**
|
Chris@0
|
469 * Creates a new Entity Reference fields with a given target type.
|
Chris@0
|
470 *
|
Chris@0
|
471 * @param string $target_type
|
Chris@0
|
472 * The name of the target type
|
Chris@0
|
473 * @param string[] $bundles
|
Chris@0
|
474 * A list of bundle IDs. Defaults to [].
|
Chris@0
|
475 *
|
Chris@0
|
476 * @return string
|
Chris@0
|
477 * Returns the generated field name
|
Chris@0
|
478 */
|
Chris@0
|
479 protected function createEntityReferenceField($target_type, $bundles = []) {
|
Chris@0
|
480 // Generates a bundle path for the newly created content type.
|
Chris@0
|
481 $bundle_path = 'admin/structure/types/manage/' . $this->type;
|
Chris@0
|
482
|
Chris@0
|
483 // Generate a random field name, must be only lowercase characters.
|
Chris@0
|
484 $field_name = strtolower($this->randomMachineName());
|
Chris@0
|
485
|
Chris@0
|
486 $storage_edit = $field_edit = [];
|
Chris@0
|
487 $storage_edit['settings[target_type]'] = $target_type;
|
Chris@0
|
488 foreach ($bundles as $bundle) {
|
Chris@0
|
489 $field_edit['settings[handler_settings][target_bundles][' . $bundle . ']'] = TRUE;
|
Chris@0
|
490 }
|
Chris@0
|
491
|
Chris@0
|
492 $this->fieldUIAddNewField($bundle_path, $field_name, NULL, 'entity_reference', $storage_edit, $field_edit);
|
Chris@0
|
493
|
Chris@0
|
494 // Returns the generated field name.
|
Chris@0
|
495 return $field_name;
|
Chris@0
|
496 }
|
Chris@0
|
497
|
Chris@0
|
498 /**
|
Chris@0
|
499 * Checks if a select element contains the specified options.
|
Chris@0
|
500 *
|
Chris@0
|
501 * @param string $name
|
Chris@0
|
502 * The field name.
|
Chris@0
|
503 * @param array $expected_options
|
Chris@0
|
504 * An array of expected options.
|
Chris@0
|
505 *
|
Chris@0
|
506 * @return bool
|
Chris@0
|
507 * TRUE if the assertion succeeded, FALSE otherwise.
|
Chris@0
|
508 */
|
Chris@0
|
509 protected function assertFieldSelectOptions($name, array $expected_options) {
|
Chris@0
|
510 $xpath = $this->buildXPathQuery('//select[@name=:name]', [':name' => $name]);
|
Chris@0
|
511 $fields = $this->xpath($xpath);
|
Chris@0
|
512 if ($fields) {
|
Chris@0
|
513 $field = $fields[0];
|
Chris@0
|
514 $options = $this->getAllOptionsList($field);
|
Chris@0
|
515
|
Chris@0
|
516 sort($options);
|
Chris@0
|
517 sort($expected_options);
|
Chris@0
|
518
|
Chris@0
|
519 return $this->assertIdentical($options, $expected_options);
|
Chris@0
|
520 }
|
Chris@0
|
521 else {
|
Chris@0
|
522 return $this->fail('Unable to find field ' . $name);
|
Chris@0
|
523 }
|
Chris@0
|
524 }
|
Chris@0
|
525
|
Chris@0
|
526 /**
|
Chris@0
|
527 * Extracts all options from a select element.
|
Chris@0
|
528 *
|
Chris@0
|
529 * @param \SimpleXMLElement $element
|
Chris@0
|
530 * The select element field information.
|
Chris@0
|
531 *
|
Chris@0
|
532 * @return array
|
Chris@0
|
533 * An array of option values as strings.
|
Chris@0
|
534 */
|
Chris@0
|
535 protected function getAllOptionsList(\SimpleXMLElement $element) {
|
Chris@0
|
536 $options = [];
|
Chris@0
|
537 // Add all options items.
|
Chris@0
|
538 foreach ($element->option as $option) {
|
Chris@0
|
539 $options[] = (string) $option['value'];
|
Chris@0
|
540 }
|
Chris@0
|
541
|
Chris@0
|
542 // Loops trough all the option groups
|
Chris@0
|
543 foreach ($element->optgroup as $optgroup) {
|
Chris@0
|
544 $options = array_merge($this->getAllOptionsList($optgroup), $options);
|
Chris@0
|
545 }
|
Chris@0
|
546
|
Chris@0
|
547 return $options;
|
Chris@0
|
548 }
|
Chris@0
|
549
|
Chris@0
|
550 }
|