Mercurial > hg > isophonics-drupal-site
comparison core/modules/field_ui/src/Tests/ManageDisplayTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\field_ui\Tests; | |
4 | |
5 use Drupal\Component\Utility\Unicode; | |
6 use Drupal\Core\Entity\Entity\EntityFormDisplay; | |
7 use Drupal\Core\Entity\Entity\EntityViewDisplay; | |
8 use Drupal\Core\Entity\EntityInterface; | |
9 use Drupal\Core\Language\LanguageInterface; | |
10 use Drupal\node\Entity\NodeType; | |
11 use Drupal\simpletest\WebTestBase; | |
12 use Drupal\taxonomy\Entity\Vocabulary; | |
13 | |
14 /** | |
15 * Tests the Field UI "Manage display" and "Manage form display" screens. | |
16 * | |
17 * @group field_ui | |
18 */ | |
19 class ManageDisplayTest extends WebTestBase { | |
20 | |
21 use FieldUiTestTrait; | |
22 | |
23 /** | |
24 * Modules to install. | |
25 * | |
26 * @var array | |
27 */ | |
28 public static $modules = ['node', 'field_ui', 'taxonomy', 'search', 'field_test', 'field_third_party_test', 'block']; | |
29 | |
30 /** | |
31 * {@inheritdoc} | |
32 */ | |
33 protected function setUp() { | |
34 parent::setUp(); | |
35 $this->drupalPlaceBlock('system_breadcrumb_block'); | |
36 $this->drupalPlaceBlock('local_tasks_block'); | |
37 | |
38 // Create a test user. | |
39 $admin_user = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer taxonomy', 'administer taxonomy_term fields', 'administer taxonomy_term display', 'administer users', 'administer account settings', 'administer user display', 'bypass node access']); | |
40 $this->drupalLogin($admin_user); | |
41 | |
42 // Create content type, with underscores. | |
43 $type_name = strtolower($this->randomMachineName(8)) . '_test'; | |
44 $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]); | |
45 $this->type = $type->id(); | |
46 | |
47 // Create a default vocabulary. | |
48 $vocabulary = Vocabulary::create([ | |
49 'name' => $this->randomMachineName(), | |
50 'description' => $this->randomMachineName(), | |
51 'vid' => Unicode::strtolower($this->randomMachineName()), | |
52 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, | |
53 'help' => '', | |
54 'nodes' => ['article' => 'article'], | |
55 'weight' => mt_rand(0, 10), | |
56 ]); | |
57 $vocabulary->save(); | |
58 $this->vocabulary = $vocabulary->id(); | |
59 } | |
60 | |
61 /** | |
62 * Tests formatter settings. | |
63 */ | |
64 public function testFormatterUI() { | |
65 $manage_fields = 'admin/structure/types/manage/' . $this->type; | |
66 $manage_display = $manage_fields . '/display'; | |
67 | |
68 // Create a field, and a node with some data for the field. | |
69 $this->fieldUIAddNewField($manage_fields, 'test', 'Test field'); | |
70 | |
71 // Get the display options (formatter and settings) that were automatically | |
72 // assigned for the 'default' display. | |
73 $display = entity_get_display('node', $this->type, 'default'); | |
74 $display_options = $display->getComponent('field_test'); | |
75 $format = $display_options['type']; | |
76 $default_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($format); | |
77 $setting_name = key($default_settings); | |
78 $setting_value = $display_options['settings'][$setting_name]; | |
79 | |
80 // Display the "Manage display" screen and check that the expected formatter | |
81 // is selected. | |
82 $this->drupalGet($manage_display); | |
83 $this->assertFieldByName('fields[field_test][type]', $format, 'The expected formatter is selected.'); | |
84 $this->assertText("$setting_name: $setting_value", 'The expected summary is displayed.'); | |
85 | |
86 // Check whether formatter weights are respected. | |
87 $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-fields-field-test-type']); | |
88 $options = array_map(function ($item) { | |
89 return (string) $item->attributes()->value[0]; | |
90 }, $result); | |
91 $expected_options = [ | |
92 'field_no_settings', | |
93 'field_empty_test', | |
94 'field_empty_setting', | |
95 'field_test_default', | |
96 'field_test_multiple', | |
97 'field_test_with_prepare_view', | |
98 'field_test_applicable', | |
99 ]; | |
100 $this->assertEqual($options, $expected_options, 'The expected formatter ordering is respected.'); | |
101 | |
102 // Ensure that fields can be hidden directly by changing the region. | |
103 $this->drupalGet($manage_display); | |
104 $this->assertFieldByName('fields[field_test][region]', 'content'); | |
105 $edit = ['fields[field_test][region]' => 'hidden']; | |
106 $this->drupalPostForm($manage_display, $edit, t('Save')); | |
107 $this->assertFieldByName('fields[field_test][region]', 'hidden'); | |
108 $display = EntityViewDisplay::load("node.{$this->type}.default"); | |
109 $this->assertNull($display->getComponent('field_test')); | |
110 | |
111 // Restore the field to the content region. | |
112 $edit = [ | |
113 'fields[field_test][type]' => 'field_test_default', | |
114 'fields[field_test][region]' => 'content', | |
115 ]; | |
116 $this->drupalPostForm($manage_display, $edit, t('Save')); | |
117 | |
118 // Change the formatter and check that the summary is updated. | |
119 $edit = [ | |
120 'fields[field_test][type]' => 'field_test_multiple', | |
121 'fields[field_test][region]' => 'content', | |
122 'refresh_rows' => 'field_test' | |
123 ]; | |
124 $this->drupalPostAjaxForm(NULL, $edit, ['op' => t('Refresh')]); | |
125 $format = 'field_test_multiple'; | |
126 $default_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($format); | |
127 $setting_name = key($default_settings); | |
128 $setting_value = $default_settings[$setting_name]; | |
129 $this->assertFieldByName('fields[field_test][type]', $format, 'The expected formatter is selected.'); | |
130 $this->assertText("$setting_name: $setting_value", 'The expected summary is displayed.'); | |
131 | |
132 // Submit the form and check that the display is updated. | |
133 $this->drupalPostForm(NULL, [], t('Save')); | |
134 $display = entity_get_display('node', $this->type, 'default'); | |
135 $display_options = $display->getComponent('field_test'); | |
136 $current_format = $display_options['type']; | |
137 $current_setting_value = $display_options['settings'][$setting_name]; | |
138 $this->assertEqual($current_format, $format, 'The formatter was updated.'); | |
139 $this->assertEqual($current_setting_value, $setting_value, 'The setting was updated.'); | |
140 | |
141 // Assert that hook_field_formatter_settings_summary_alter() is called. | |
142 $this->assertText('field_test_field_formatter_settings_summary_alter'); | |
143 | |
144 // Click on the formatter settings button to open the formatter settings | |
145 // form. | |
146 $this->drupalPostAjaxForm(NULL, [], "field_test_settings_edit"); | |
147 | |
148 // Assert that the field added in | |
149 // field_test_field_formatter_third_party_settings_form() is present. | |
150 $fieldname = 'fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_field_formatter_third_party_settings_form]'; | |
151 $this->assertField($fieldname, 'The field added in hook_field_formatter_third_party_settings_form() is present on the settings form.'); | |
152 $edit = [$fieldname => 'foo']; | |
153 $this->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update"); | |
154 | |
155 // Save the form to save the third party settings. | |
156 $this->drupalPostForm(NULL, [], t('Save')); | |
157 | |
158 \Drupal::entityManager()->clearCachedFieldDefinitions(); | |
159 $id = 'node.' . $this->type . '.default'; | |
160 $storage = $this->container->get('entity_type.manager')->getStorage('entity_view_display'); | |
161 $storage->resetCache([$id]); | |
162 $display = $storage->load($id); | |
163 $this->assertEqual($display->getRenderer('field_test')->getThirdPartySetting('field_third_party_test', 'field_test_field_formatter_third_party_settings_form'), 'foo'); | |
164 $this->assertTrue(in_array('field_third_party_test', $display->calculateDependencies()->getDependencies()['module']), 'The display has a dependency on field_third_party_test module.'); | |
165 | |
166 // Confirm that the third party settings are not updated on the settings form. | |
167 $this->drupalPostAjaxForm(NULL, [], "field_test_settings_edit"); | |
168 $this->assertFieldByName($fieldname, ''); | |
169 | |
170 // Test the empty setting formatter. | |
171 $edit = [ | |
172 'fields[field_test][type]' => 'field_empty_setting', | |
173 'fields[field_test][region]' => 'content', | |
174 ]; | |
175 $this->drupalPostForm(NULL, $edit, t('Save')); | |
176 $this->assertNoText('Default empty setting now has a value.'); | |
177 $this->assertFieldById('edit-fields-field-test-settings-edit'); | |
178 $this->drupalPostAjaxForm(NULL, [], "field_test_settings_edit"); | |
179 $fieldname = 'fields[field_test][settings_edit_form][settings][field_empty_setting]'; | |
180 $edit = [$fieldname => 'non empty setting']; | |
181 $this->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update"); | |
182 $this->assertText('Default empty setting now has a value.'); | |
183 | |
184 // Test the settings form behavior. An edit button should be present since | |
185 // there are third party settings to configure. | |
186 $edit = [ | |
187 'fields[field_test][type]' => 'field_no_settings', | |
188 'fields[field_test][region]' => 'content', | |
189 'refresh_rows' => 'field_test', | |
190 ]; | |
191 $this->drupalPostAjaxForm(NULL, $edit, ['op' => t('Refresh')]); | |
192 $this->assertFieldByName('field_test_settings_edit'); | |
193 | |
194 // Make sure we can save the third party settings when there are no settings available | |
195 $this->drupalPostAjaxForm(NULL, [], "field_test_settings_edit"); | |
196 $this->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update"); | |
197 | |
198 // When a module providing third-party settings to a formatter (or widget) | |
199 // is uninstalled, the formatter remains enabled but the provided settings, | |
200 // together with the corresponding form elements, are removed from the | |
201 // display component. | |
202 \Drupal::service('module_installer')->uninstall(['field_third_party_test']); | |
203 | |
204 // Ensure the button is still there after the module has been disabled. | |
205 $this->drupalGet($manage_display); | |
206 $this->assertResponse(200); | |
207 $this->assertFieldByName('field_test_settings_edit'); | |
208 | |
209 // Ensure that third-party form elements are not present anymore. | |
210 $this->drupalPostAjaxForm(NULL, [], 'field_test_settings_edit'); | |
211 $fieldname = 'fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_field_formatter_third_party_settings_form]'; | |
212 $this->assertNoField($fieldname); | |
213 | |
214 // Ensure that third-party settings were removed from the formatter. | |
215 $display = EntityViewDisplay::load("node.{$this->type}.default"); | |
216 $component = $display->getComponent('field_test'); | |
217 $this->assertFalse(array_key_exists('field_third_party_test', $component['third_party_settings'])); | |
218 } | |
219 | |
220 /** | |
221 * Tests widget settings. | |
222 */ | |
223 public function testWidgetUI() { | |
224 // Admin Manage Fields page. | |
225 $manage_fields = 'admin/structure/types/manage/' . $this->type; | |
226 // Admin Manage Display page. | |
227 $manage_display = $manage_fields . '/form-display'; | |
228 | |
229 // Creates a new field that can be used with multiple formatters. | |
230 // Reference: Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidgetMultiple::isApplicable(). | |
231 $this->fieldUIAddNewField($manage_fields, 'test', 'Test field'); | |
232 | |
233 // Get the display options (formatter and settings) that were automatically | |
234 // assigned for the 'default' display. | |
235 $display = entity_get_form_display('node', $this->type, 'default'); | |
236 $display_options = $display->getComponent('field_test'); | |
237 $widget_type = $display_options['type']; | |
238 $default_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($widget_type); | |
239 $setting_name = key($default_settings); | |
240 $setting_value = $display_options['settings'][$setting_name]; | |
241 | |
242 // Display the "Manage form display" screen and check if the expected | |
243 // widget is selected. | |
244 $this->drupalGet($manage_display); | |
245 $this->assertFieldByName('fields[field_test][type]', $widget_type, 'The expected widget is selected.'); | |
246 $this->assertText("$setting_name: $setting_value", 'The expected summary is displayed.'); | |
247 | |
248 // Check whether widget weights are respected. | |
249 $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-fields-field-test-type']); | |
250 $options = array_map(function ($item) { | |
251 return (string) $item->attributes()->value[0]; | |
252 }, $result); | |
253 $expected_options = [ | |
254 'test_field_widget', | |
255 'test_field_widget_multiple', | |
256 ]; | |
257 $this->assertEqual($options, $expected_options, 'The expected widget ordering is respected.'); | |
258 | |
259 // Change the widget and check that the summary is updated. | |
260 $edit = [ | |
261 'fields[field_test][type]' => 'test_field_widget_multiple', | |
262 'fields[field_test][region]' => 'content', | |
263 'refresh_rows' => 'field_test', | |
264 ]; | |
265 $this->drupalPostAjaxForm(NULL, $edit, ['op' => t('Refresh')]); | |
266 $widget_type = 'test_field_widget_multiple'; | |
267 $default_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($widget_type); | |
268 $setting_name = key($default_settings); | |
269 $setting_value = $default_settings[$setting_name]; | |
270 $this->assertFieldByName('fields[field_test][type]', $widget_type, 'The expected widget is selected.'); | |
271 $this->assertText("$setting_name: $setting_value", 'The expected summary is displayed.'); | |
272 | |
273 // Submit the form and check that the display is updated. | |
274 $this->drupalPostForm(NULL, [], t('Save')); | |
275 $display = entity_get_form_display('node', $this->type, 'default'); | |
276 $display_options = $display->getComponent('field_test'); | |
277 $current_widget = $display_options['type']; | |
278 $current_setting_value = $display_options['settings'][$setting_name]; | |
279 $this->assertEqual($current_widget, $widget_type, 'The widget was updated.'); | |
280 $this->assertEqual($current_setting_value, $setting_value, 'The setting was updated.'); | |
281 | |
282 // Assert that hook_field_widget_settings_summary_alter() is called. | |
283 $this->assertText('field_test_field_widget_settings_summary_alter'); | |
284 | |
285 // Click on the widget settings button to open the widget settings form. | |
286 $this->drupalPostAjaxForm(NULL, [], "field_test_settings_edit"); | |
287 | |
288 // Assert that the field added in | |
289 // field_test_field_widget_third_party_settings_form() is present. | |
290 $fieldname = 'fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_widget_third_party_settings_form]'; | |
291 $this->assertField($fieldname, 'The field added in hook_field_widget_third_party_settings_form() is present on the settings form.'); | |
292 $edit = [$fieldname => 'foo']; | |
293 $this->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update"); | |
294 | |
295 // Save the form to save the third party settings. | |
296 $this->drupalPostForm(NULL, [], t('Save')); | |
297 \Drupal::entityManager()->clearCachedFieldDefinitions(); | |
298 $storage = $this->container->get('entity_type.manager')->getStorage('entity_form_display'); | |
299 $storage->resetCache(['node.' . $this->type . '.default']); | |
300 $display = $storage->load('node.' . $this->type . '.default'); | |
301 $this->assertEqual($display->getRenderer('field_test')->getThirdPartySetting('field_third_party_test', 'field_test_widget_third_party_settings_form'), 'foo'); | |
302 $this->assertTrue(in_array('field_third_party_test', $display->calculateDependencies()->getDependencies()['module']), 'Form display does not have a dependency on field_third_party_test module.'); | |
303 | |
304 // Confirm that the third party settings are not updated on the settings form. | |
305 $this->drupalPostAjaxForm(NULL, [], "field_test_settings_edit"); | |
306 $this->assertFieldByName($fieldname, ''); | |
307 | |
308 // Creates a new field that can not be used with the multiple formatter. | |
309 // Reference: Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidgetMultiple::isApplicable(). | |
310 $this->fieldUIAddNewField($manage_fields, 'onewidgetfield', 'One Widget Field'); | |
311 | |
312 // Go to the Manage Form Display. | |
313 $this->drupalGet($manage_display); | |
314 | |
315 // Checks if the select elements contain the specified options. | |
316 $this->assertFieldSelectOptions('fields[field_test][type]', ['test_field_widget', 'test_field_widget_multiple']); | |
317 $this->assertFieldSelectOptions('fields[field_onewidgetfield][type]', ['test_field_widget']); | |
318 | |
319 // Ensure that fields can be hidden directly by changing the region. | |
320 $this->assertFieldByName('fields[field_test][region]', 'content'); | |
321 $edit = ['fields[field_test][region]' => 'hidden']; | |
322 $this->drupalPostForm(NULL, $edit, t('Save')); | |
323 $this->assertFieldByName('fields[field_test][region]', 'hidden'); | |
324 $display = EntityFormDisplay::load("node.{$this->type}.default"); | |
325 $this->assertNull($display->getComponent('field_test')); | |
326 } | |
327 | |
328 /** | |
329 * Tests switching view modes to use custom or 'default' settings'. | |
330 */ | |
331 public function testViewModeCustom() { | |
332 // Create a field, and a node with some data for the field. | |
333 $this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test field'); | |
334 \Drupal::entityManager()->clearCachedFieldDefinitions(); | |
335 // For this test, use a formatter setting value that is an integer unlikely | |
336 // to appear in a rendered node other than as part of the field being tested | |
337 // (for example, unlikely to be part of the "Submitted by ... on ..." line). | |
338 $value = 12345; | |
339 $settings = [ | |
340 'type' => $this->type, | |
341 'field_test' => [['value' => $value]], | |
342 ]; | |
343 $node = $this->drupalCreateNode($settings); | |
344 | |
345 // Gather expected output values with the various formatters. | |
346 $formatter_plugin_manager = \Drupal::service('plugin.manager.field.formatter'); | |
347 $field_test_default_settings = $formatter_plugin_manager->getDefaultSettings('field_test_default'); | |
348 $field_test_with_prepare_view_settings = $formatter_plugin_manager->getDefaultSettings('field_test_with_prepare_view'); | |
349 $output = [ | |
350 'field_test_default' => $field_test_default_settings['test_formatter_setting'] . '|' . $value, | |
351 'field_test_with_prepare_view' => $field_test_with_prepare_view_settings['test_formatter_setting_additional'] . '|' . $value . '|' . ($value + 1), | |
352 ]; | |
353 | |
354 // Check that the field is displayed with the default formatter in 'rss' | |
355 // mode (uses 'default'), and hidden in 'teaser' mode (uses custom settings). | |
356 $this->assertNodeViewText($node, 'rss', $output['field_test_default'], "The field is displayed as expected in view modes that use 'default' settings."); | |
357 $this->assertNodeViewNoText($node, 'teaser', $value, "The field is hidden in view modes that use custom settings."); | |
358 | |
359 // Change formatter for 'default' mode, check that the field is displayed | |
360 // accordingly in 'rss' mode. | |
361 $edit = [ | |
362 'fields[field_test][type]' => 'field_test_with_prepare_view', | |
363 'fields[field_test][region]' => 'content', | |
364 ]; | |
365 $this->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save')); | |
366 $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected in view modes that use 'default' settings."); | |
367 | |
368 // Specialize the 'rss' mode, check that the field is displayed the same. | |
369 $edit = [ | |
370 "display_modes_custom[rss]" => TRUE, | |
371 ]; | |
372 $this->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save')); | |
373 $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected in newly specialized 'rss' mode."); | |
374 | |
375 // Set the field to 'hidden' in the view mode, check that the field is | |
376 // hidden. | |
377 $edit = [ | |
378 'fields[field_test][region]' => 'hidden', | |
379 ]; | |
380 $this->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display/rss', $edit, t('Save')); | |
381 $this->assertNodeViewNoText($node, 'rss', $value, "The field is hidden in 'rss' mode."); | |
382 | |
383 // Set the view mode back to 'default', check that the field is displayed | |
384 // accordingly. | |
385 $edit = [ | |
386 "display_modes_custom[rss]" => FALSE, | |
387 ]; | |
388 $this->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save')); | |
389 $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected when 'rss' mode is set back to 'default' settings."); | |
390 | |
391 // Specialize the view mode again. | |
392 $edit = [ | |
393 "display_modes_custom[rss]" => TRUE, | |
394 ]; | |
395 $this->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save')); | |
396 // Check that the previous settings for the view mode have been kept. | |
397 $this->assertNodeViewNoText($node, 'rss', $value, "The previous settings are kept when 'rss' mode is specialized again."); | |
398 } | |
399 | |
400 /** | |
401 * Tests the local tasks are displayed correctly for view modes. | |
402 */ | |
403 public function testViewModeLocalTasks() { | |
404 $manage_display = 'admin/structure/types/manage/' . $this->type . '/display'; | |
405 $this->drupalGet($manage_display); | |
406 $this->assertNoLink('Full content'); | |
407 $this->assertLink('Teaser'); | |
408 | |
409 $this->drupalGet($manage_display . '/teaser'); | |
410 $this->assertNoLink('Full content'); | |
411 $this->assertLink('Default'); | |
412 } | |
413 | |
414 /** | |
415 * Tests that fields with no explicit display settings do not break. | |
416 */ | |
417 public function testNonInitializedFields() { | |
418 // Create a test field. | |
419 $this->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test'); | |
420 | |
421 // Check that the field appears as 'hidden' on the 'Manage display' page | |
422 // for the 'teaser' mode. | |
423 $this->drupalGet('admin/structure/types/manage/' . $this->type . '/display/teaser'); | |
424 $this->assertFieldByName('fields[field_test][region]', 'hidden', 'The field is displayed as \'hidden \'.'); | |
425 } | |
426 | |
427 /** | |
428 * Tests hiding the view modes fieldset when there's only one available. | |
429 */ | |
430 public function testSingleViewMode() { | |
431 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary . '/display'); | |
432 $this->assertNoText('Use custom display settings for the following view modes', 'Custom display settings fieldset found.'); | |
433 | |
434 // This may not trigger a notice when 'view_modes_custom' isn't available. | |
435 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary . '/overview/display', [], t('Save')); | |
436 } | |
437 | |
438 /** | |
439 * Tests that a message is shown when there are no fields. | |
440 */ | |
441 public function testNoFieldsDisplayOverview() { | |
442 // Create a fresh content type without any fields. | |
443 NodeType::create([ | |
444 'type' => 'no_fields', | |
445 'name' => 'No fields', | |
446 ])->save(); | |
447 | |
448 $this->drupalGet('admin/structure/types/manage/no_fields/display'); | |
449 $this->assertRaw(t('There are no fields yet added. You can add new fields on the <a href=":link">Manage fields</a> page.', [':link' => \Drupal::url('entity.node.field_ui_fields', ['node_type' => 'no_fields'])])); | |
450 } | |
451 | |
452 /** | |
453 * Asserts that a string is found in the rendered node in a view mode. | |
454 * | |
455 * @param EntityInterface $node | |
456 * The node. | |
457 * @param $view_mode | |
458 * The view mode in which the node should be displayed. | |
459 * @param $text | |
460 * Plain text to look for. | |
461 * @param $message | |
462 * Message to display. | |
463 * | |
464 * @return | |
465 * TRUE on pass, FALSE on fail. | |
466 */ | |
467 public function assertNodeViewText(EntityInterface $node, $view_mode, $text, $message) { | |
468 return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, FALSE); | |
469 } | |
470 | |
471 /** | |
472 * Asserts that a string is not found in the rendered node in a view mode. | |
473 * | |
474 * @param EntityInterface $node | |
475 * The node. | |
476 * @param $view_mode | |
477 * The view mode in which the node should be displayed. | |
478 * @param $text | |
479 * Plain text to look for. | |
480 * @param $message | |
481 * Message to display. | |
482 * @return | |
483 * TRUE on pass, FALSE on fail. | |
484 */ | |
485 public function assertNodeViewNoText(EntityInterface $node, $view_mode, $text, $message) { | |
486 return $this->assertNodeViewTextHelper($node, $view_mode, $text, $message, TRUE); | |
487 } | |
488 | |
489 /** | |
490 * Asserts that a string is (not) found in the rendered nodein a view mode. | |
491 * | |
492 * This helper function is used by assertNodeViewText() and | |
493 * assertNodeViewNoText(). | |
494 * | |
495 * @param EntityInterface $node | |
496 * The node. | |
497 * @param $view_mode | |
498 * The view mode in which the node should be displayed. | |
499 * @param $text | |
500 * Plain text to look for. | |
501 * @param $message | |
502 * Message to display. | |
503 * @param $not_exists | |
504 * TRUE if this text should not exist, FALSE if it should. | |
505 * | |
506 * @return | |
507 * TRUE on pass, FALSE on fail. | |
508 */ | |
509 public function assertNodeViewTextHelper(EntityInterface $node, $view_mode, $text, $message, $not_exists) { | |
510 // Make sure caches on the tester side are refreshed after changes | |
511 // submitted on the tested side. | |
512 \Drupal::entityManager()->clearCachedFieldDefinitions(); | |
513 | |
514 // Save current content so that we can restore it when we're done. | |
515 $old_content = $this->getRawContent(); | |
516 | |
517 // Render a cloned node, so that we do not alter the original. | |
518 $clone = clone $node; | |
519 $element = node_view($clone, $view_mode); | |
520 $output = \Drupal::service('renderer')->renderRoot($element); | |
521 $this->verbose(t('Rendered node - view mode: @view_mode', ['@view_mode' => $view_mode]) . '<hr />' . $output); | |
522 | |
523 // Assign content so that WebTestBase functions can be used. | |
524 $this->setRawContent($output); | |
525 $method = ($not_exists ? 'assertNoText' : 'assertText'); | |
526 $return = $this->{$method}((string) $text, $message); | |
527 | |
528 // Restore previous content. | |
529 $this->setRawContent($old_content); | |
530 | |
531 return $return; | |
532 } | |
533 | |
534 /** | |
535 * Checks if a select element contains the specified options. | |
536 * | |
537 * @param string $name | |
538 * The field name. | |
539 * @param array $expected_options | |
540 * An array of expected options. | |
541 * | |
542 * @return bool | |
543 * TRUE if the assertion succeeded, FALSE otherwise. | |
544 */ | |
545 protected function assertFieldSelectOptions($name, array $expected_options) { | |
546 $xpath = $this->buildXPathQuery('//select[@name=:name]', [':name' => $name]); | |
547 $fields = $this->xpath($xpath); | |
548 if ($fields) { | |
549 $field = $fields[0]; | |
550 $options = $this->getAllOptionsList($field); | |
551 | |
552 sort($options); | |
553 sort($expected_options); | |
554 | |
555 return $this->assertIdentical($options, $expected_options); | |
556 } | |
557 else { | |
558 return $this->fail('Unable to find field ' . $name); | |
559 } | |
560 } | |
561 | |
562 /** | |
563 * Extracts all options from a select element. | |
564 * | |
565 * @param \SimpleXMLElement $element | |
566 * The select element field information. | |
567 * | |
568 * @return array | |
569 * An array of option values as strings. | |
570 */ | |
571 protected function getAllOptionsList(\SimpleXMLElement $element) { | |
572 $options = []; | |
573 // Add all options items. | |
574 foreach ($element->option as $option) { | |
575 $options[] = (string) $option['value']; | |
576 } | |
577 | |
578 // Loops trough all the option groups | |
579 foreach ($element->optgroup as $optgroup) { | |
580 $options = array_merge($this->getAllOptionsList($optgroup), $options); | |
581 } | |
582 | |
583 return $options; | |
584 } | |
585 | |
586 } |