function ManageDisplayTest::testFormatterUI in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/field_ui/src/Tests/ManageDisplayTest.php \Drupal\field_ui\Tests\ManageDisplayTest::testFormatterUI()
Tests formatter settings.
File
- core/
modules/ field_ui/ src/ Tests/ ManageDisplayTest.php, line 67 - Contains \Drupal\field_ui\Tests\ManageDisplayTest.
Class
- ManageDisplayTest
- Tests the Field UI "Manage display" and "Manage form display" screens.
Namespace
Drupal\field_ui\TestsCode
function testFormatterUI() {
$manage_fields = 'admin/structure/types/manage/' . $this->type;
$manage_display = $manage_fields . '/display';
// Create a field, and a node with some data for the field.
$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_display('node', $this->type, 'default');
$display_options = $display
->getComponent('field_test');
$format = $display_options['type'];
$default_settings = \Drupal::service('plugin.manager.field.formatter')
->getDefaultSettings($format);
$setting_name = key($default_settings);
$setting_value = $display_options['settings'][$setting_name];
// Display the "Manage display" screen and check that the expected formatter
// is selected.
$this
->drupalGet($manage_display);
$this
->assertFieldByName('fields[field_test][type]', $format, 'The expected formatter is selected.');
$this
->assertText("{$setting_name}: {$setting_value}", 'The expected summary is displayed.');
// Check whether formatter 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(
'field_no_settings',
'field_empty_test',
'field_empty_setting',
'field_test_default',
'field_test_multiple',
'field_test_with_prepare_view',
'field_test_applicable',
'hidden',
);
$this
->assertEqual($options, $expected_options, 'The expected formatter ordering is respected.');
// Change the formatter and check that the summary is updated.
$edit = array(
'fields[field_test][type]' => 'field_test_multiple',
'refresh_rows' => 'field_test',
);
$this
->drupalPostAjaxForm(NULL, $edit, array(
'op' => t('Refresh'),
));
$format = 'field_test_multiple';
$default_settings = \Drupal::service('plugin.manager.field.formatter')
->getDefaultSettings($format);
$setting_name = key($default_settings);
$setting_value = $default_settings[$setting_name];
$this
->assertFieldByName('fields[field_test][type]', $format, 'The expected formatter 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_display('node', $this->type, 'default');
$display_options = $display
->getComponent('field_test');
$current_format = $display_options['type'];
$current_setting_value = $display_options['settings'][$setting_name];
$this
->assertEqual($current_format, $format, 'The formatter was updated.');
$this
->assertEqual($current_setting_value, $setting_value, 'The setting was updated.');
// Assert that hook_field_formatter_settings_summary_alter() is called.
$this
->assertText('field_test_field_formatter_settings_summary_alter');
// Click on the formatter settings button to open the formatter settings
// form.
$this
->drupalPostAjaxForm(NULL, array(), "field_test_settings_edit");
// Assert that the field added in
// field_test_field_formatter_third_party_settings_form() is present.
$fieldname = 'fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_field_formatter_third_party_settings_form]';
$this
->assertField($fieldname, 'The field added in hook_field_formatter_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_view_display', 'node.' . $this->type . '.default', TRUE);
$this
->assertEqual($display
->getRenderer('field_test')
->getThirdPartySetting('field_third_party_test', 'field_test_field_formatter_third_party_settings_form'), 'foo');
$this
->assertTrue(in_array('field_third_party_test', $display
->calculateDependencies()
->getDependencies()['module']), 'The display has 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, '');
// Test the empty setting formatter.
$edit = array(
'fields[field_test][type]' => 'field_empty_setting',
);
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertNoText('Default empty setting now has a value.');
$this
->assertFieldById('edit-fields-field-test-settings-edit');
$this
->drupalPostAjaxForm(NULL, array(), "field_test_settings_edit");
$fieldname = 'fields[field_test][settings_edit_form][settings][field_empty_setting]';
$edit = array(
$fieldname => 'non empty setting',
);
$this
->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update");
$this
->assertText('Default empty setting now has a value.');
// Test the settings form behavior. An edit button should be present since
// there are third party settings to configure.
$edit = array(
'fields[field_test][type]' => 'field_no_settings',
'refresh_rows' => 'field_test',
);
$this
->drupalPostAjaxForm(NULL, $edit, array(
'op' => t('Refresh'),
));
$this
->assertFieldByName('field_test_settings_edit');
// Make sure we can save the third party settings when there are no settings available
$this
->drupalPostAjaxForm(NULL, array(), "field_test_settings_edit");
$this
->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update");
// When a module providing third-party settings to a formatter (or widget)
// is uninstalled, the formatter remains enabled but the provided settings,
// together with the corresponding form elements, are removed from the
// display component.
\Drupal::service('module_installer')
->uninstall(array(
'field_third_party_test',
));
// Ensure the button is still there after the module has been disabled.
$this
->drupalGet($manage_display);
$this
->assertResponse(200);
$this
->assertFieldByName('field_test_settings_edit');
// Ensure that third-party form elements are not present anymore.
$this
->drupalPostAjaxForm(NULL, array(), 'field_test_settings_edit');
$fieldname = 'fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_field_formatter_third_party_settings_form]';
$this
->assertNoField($fieldname);
// Ensure that third-party settings were removed from the formatter.
$display = EntityViewDisplay::load("node.{$this->type}.default");
$component = $display
->getComponent('field_test');
$this
->assertFalse(array_key_exists('field_third_party_test', $component['third_party_settings']));
}