public function ManageDisplayTest::testWidgetUI in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/field_ui/src/Tests/ManageDisplayTest.php \Drupal\field_ui\Tests\ManageDisplayTest::testWidgetUI()
Tests widget settings.
File
- core/
modules/ field_ui/ src/ Tests/ ManageDisplayTest.php, line 197 - Contains \Drupal\field_ui\Tests\ManageDisplayTest.
Class
- ManageDisplayTest
- Tests the Field UI "Manage display" and "Manage form display" screens.
Namespace
Drupal\field_ui\TestsCode
public function testWidgetUI() {
// Admin Manage Fields page.
$manage_fields = 'admin/structure/types/manage/' . $this->type;
// Admin Manage Display page.
$manage_display = $manage_fields . '/form-display';
// Creates a new field that can be used with multiple formatters.
// Reference: Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidgetMultiple::isApplicable().
$this
->fieldUIAddNewField($manage_fields, 'test', 'Test field');
// Get the display options (formatter and settings) that were automatically
// assigned for the 'default' display.
$display = entity_get_form_display('node', $this->type, 'default');
$display_options = $display
->getComponent('field_test');
$widget_type = $display_options['type'];
$default_settings = \Drupal::service('plugin.manager.field.widget')
->getDefaultSettings($widget_type);
$setting_name = key($default_settings);
$setting_value = $display_options['settings'][$setting_name];
// Display the "Manage form display" screen and check if the expected
// widget is selected.
$this
->drupalGet($manage_display);
$this
->assertFieldByName('fields[field_test][type]', $widget_type, 'The expected widget is selected.');
$this
->assertText("{$setting_name}: {$setting_value}", 'The expected summary is displayed.');
// Check whether widget weights are respected.
$result = $this
->xpath('//select[@id=:id]/option', array(
':id' => 'edit-fields-field-test-type',
));
$options = array_map(function ($item) {
return (string) $item
->attributes()->value[0];
}, $result);
$expected_options = array(
'test_field_widget',
'test_field_widget_multiple',
'hidden',
);
$this
->assertEqual($options, $expected_options, 'The expected widget ordering is respected.');
// Change the widget and check that the summary is updated.
$edit = array(
'fields[field_test][type]' => 'test_field_widget_multiple',
'refresh_rows' => 'field_test',
);
$this
->drupalPostAjaxForm(NULL, $edit, array(
'op' => t('Refresh'),
));
$widget_type = 'test_field_widget_multiple';
$default_settings = \Drupal::service('plugin.manager.field.widget')
->getDefaultSettings($widget_type);
$setting_name = key($default_settings);
$setting_value = $default_settings[$setting_name];
$this
->assertFieldByName('fields[field_test][type]', $widget_type, 'The expected widget is selected.');
$this
->assertText("{$setting_name}: {$setting_value}", 'The expected summary is displayed.');
// Submit the form and check that the display is updated.
$this
->drupalPostForm(NULL, array(), t('Save'));
$display = entity_get_form_display('node', $this->type, 'default');
$display_options = $display
->getComponent('field_test');
$current_widget = $display_options['type'];
$current_setting_value = $display_options['settings'][$setting_name];
$this
->assertEqual($current_widget, $widget_type, 'The widget was updated.');
$this
->assertEqual($current_setting_value, $setting_value, 'The setting was updated.');
// Assert that hook_field_widget_settings_summary_alter() is called.
$this
->assertText('field_test_field_widget_settings_summary_alter');
// Click on the widget settings button to open the widget settings form.
$this
->drupalPostAjaxForm(NULL, array(), "field_test_settings_edit");
// Assert that the field added in
// field_test_field_widget_third_party_settings_form() is present.
$fieldname = 'fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_widget_third_party_settings_form]';
$this
->assertField($fieldname, 'The field added in hook_field_widget_third_party_settings_form() is present on the settings form.');
$edit = array(
$fieldname => 'foo',
);
$this
->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update");
// Save the form to save the third party settings.
$this
->drupalPostForm(NULL, array(), t('Save'));
\Drupal::entityManager()
->clearCachedFieldDefinitions();
$display = entity_load('entity_form_display', 'node.' . $this->type . '.default', TRUE);
$this
->assertEqual($display
->getRenderer('field_test')
->getThirdPartySetting('field_third_party_test', 'field_test_widget_third_party_settings_form'), 'foo');
$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.');
// Confirm that the third party settings are not updated on the settings form.
$this
->drupalPostAjaxForm(NULL, array(), "field_test_settings_edit");
$this
->assertFieldByName($fieldname, '');
// Creates a new field that can not be used with the multiple formatter.
// Reference: Drupal\field_test\Plugin\Field\FieldWidget\TestFieldWidgetMultiple::isApplicable().
$this
->fieldUIAddNewField($manage_fields, 'onewidgetfield', 'One Widget Field');
// Go to the Manage Form Display.
$this
->drupalGet($manage_display);
// Checks if the select elements contain the specified options.
$this
->assertFieldSelectOptions('fields[field_test][type]', array(
'test_field_widget',
'test_field_widget_multiple',
'hidden',
));
$this
->assertFieldSelectOptions('fields[field_onewidgetfield][type]', array(
'test_field_widget',
'hidden',
));
}