Commit 08555611 authored by catch's avatar catch

Issue #2809501 by Lendude, michielnugter, MerryHamster, Manuel Garcia, jibran:...

Issue #2809501 by Lendude, michielnugter, MerryHamster, Manuel Garcia, jibran: Convert AJAX part of \Drupal\field_ui\Tests\ManageDisplayTest to JavascriptTestBase
parent 876c6c16
......@@ -2,8 +2,6 @@
namespace Drupal\field_ui\Tests;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\node\Entity\NodeType;
......@@ -57,274 +55,6 @@ protected function setUp() {
$this->vocabulary = $vocabulary->id();
}
/**
* Tests formatter settings.
*/
public 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', [':id' => 'edit-fields-field-test-type']);
$options = array_map(function ($item) {
return (string) $item->attributes()->value[0];
}, $result);
$expected_options = [
'field_no_settings',
'field_empty_test',
'field_empty_setting',
'field_test_default',
'field_test_multiple',
'field_test_with_prepare_view',
'field_test_applicable',
];
$this->assertEqual($options, $expected_options, 'The expected formatter ordering is respected.');
// Ensure that fields can be hidden directly by changing the region.
$this->drupalGet($manage_display);
$this->assertFieldByName('fields[field_test][region]', 'content');
$edit = ['fields[field_test][region]' => 'hidden'];
$this->drupalPostForm($manage_display, $edit, t('Save'));
$this->assertFieldByName('fields[field_test][region]', 'hidden');
$display = EntityViewDisplay::load("node.{$this->type}.default");
$this->assertNull($display->getComponent('field_test'));
// Restore the field to the content region.
$edit = [
'fields[field_test][type]' => 'field_test_default',
'fields[field_test][region]' => 'content',
];
$this->drupalPostForm($manage_display, $edit, t('Save'));
// Change the formatter and check that the summary is updated.
$edit = [
'fields[field_test][type]' => 'field_test_multiple',
'fields[field_test][region]' => 'content',
'refresh_rows' => 'field_test',
];
$this->drupalPostAjaxForm(NULL, $edit, ['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, [], 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, [], "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 = [$fieldname => 'foo'];
$this->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update");
// Save the form to save the third party settings.
$this->drupalPostForm(NULL, [], t('Save'));
\Drupal::entityManager()->clearCachedFieldDefinitions();
$id = 'node.' . $this->type . '.default';
$storage = $this->container->get('entity_type.manager')->getStorage('entity_view_display');
$storage->resetCache([$id]);
$display = $storage->load($id);
$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, [], "field_test_settings_edit");
$this->assertFieldByName($fieldname, '');
// Test the empty setting formatter.
$edit = [
'fields[field_test][type]' => 'field_empty_setting',
'fields[field_test][region]' => 'content',
];
$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, [], "field_test_settings_edit");
$fieldname = 'fields[field_test][settings_edit_form][settings][field_empty_setting]';
$edit = [$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 = [
'fields[field_test][type]' => 'field_no_settings',
'fields[field_test][region]' => 'content',
'refresh_rows' => 'field_test',
];
$this->drupalPostAjaxForm(NULL, $edit, ['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, [], "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(['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, [], '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']));
}
/**
* Tests widget settings.
*/
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', [':id' => 'edit-fields-field-test-type']);
$options = array_map(function ($item) {
return (string) $item->attributes()->value[0];
}, $result);
$expected_options = [
'test_field_widget',
'test_field_widget_multilingual',
'test_field_widget_multiple',
];
$this->assertEqual($options, $expected_options, 'The expected widget ordering is respected.');
// Change the widget and check that the summary is updated.
$edit = [
'fields[field_test][type]' => 'test_field_widget_multiple',
'fields[field_test][region]' => 'content',
'refresh_rows' => 'field_test',
];
$this->drupalPostAjaxForm(NULL, $edit, ['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, [], 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, [], "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 = [$fieldname => 'foo'];
$this->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update");
// Save the form to save the third party settings.
$this->drupalPostForm(NULL, [], t('Save'));
\Drupal::entityManager()->clearCachedFieldDefinitions();
$storage = $this->container->get('entity_type.manager')->getStorage('entity_form_display');
$storage->resetCache(['node.' . $this->type . '.default']);
$display = $storage->load('node.' . $this->type . '.default');
$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, [], "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]', ['test_field_widget', 'test_field_widget_multilingual', 'test_field_widget_multiple']);
$this->assertFieldSelectOptions('fields[field_onewidgetfield][type]', ['test_field_widget', 'test_field_widget_multilingual']);
// Ensure that fields can be hidden directly by changing the region.
$this->assertFieldByName('fields[field_test][region]', 'content');
$edit = ['fields[field_test][region]' => 'hidden'];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertFieldByName('fields[field_test][region]', 'hidden');
$display = EntityFormDisplay::load("node.{$this->type}.default");
$this->assertNull($display->getComponent('field_test'));
}
/**
* Tests switching view modes to use custom or 'default' settings'.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment