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