You are here

public function ManageDisplayTest::testWidgetUI in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field_ui/tests/src/FunctionalJavascript/ManageDisplayTest.php \Drupal\Tests\field_ui\FunctionalJavascript\ManageDisplayTest::testWidgetUI()

Tests widget settings.

File

core/modules/field_ui/tests/src/FunctionalJavascript/ManageDisplayTest.php, line 245

Class

ManageDisplayTest
Tests the Field UI "Manage display" and "Manage form display" screens.

Namespace

Drupal\Tests\field_ui\FunctionalJavascript

Code

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';
  $form_storage = $this->entity_type_manager
    ->getStorage('entity_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.

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
  $display = $form_storage
    ->loadUnchanged("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);
  $session = $this
    ->getSession();
  $assert_session = $this
    ->assertSession();
  $page = $session
    ->getPage();
  $field_test_settings = $page
    ->find('css', 'input[name="field_test_settings_edit"]');
  $field_test_type = $page
    ->findField('fields[field_test][type]');
  $button_save = $page
    ->findButton('Save');
  $this
    ->assertEquals($widget_type, $field_test_type
    ->getValue(), 'The expected widget is selected.');
  $assert_session
    ->responseContains("{$setting_name}: {$setting_value}");

  // Check whether widget weights are respected.
  $this
    ->assertFieldSelectOptions($field_test_type, [
    'test_field_widget',
    'test_field_widget_multilingual',
    'test_field_widget_multiple',
  ]);
  $field_test_type
    ->setValue('test_field_widget_multiple');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $button_save
    ->click();
  $this
    ->drupalGet($manage_display);
  $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
    ->assertEquals($widget_type, $field_test_type
    ->getValue(), 'The expected widget is selected.');
  $assert_session
    ->responseContains("{$setting_name}: {$setting_value}");
  $button_save
    ->click();

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
  $display = $form_storage
    ->loadUnchanged("node.{$this->type}.default");
  $display_options = $display
    ->getComponent('field_test');
  $current_widget = $display_options['type'];
  $current_setting_value = $display_options['settings'][$setting_name];
  $this
    ->assertEquals($current_widget, $widget_type, 'The widget was updated.');
  $this
    ->assertEquals($current_setting_value, $setting_value, 'The setting was updated.');

  // Assert that hook_field_widget_settings_summary_alter() is called.
  $assert_session
    ->responseContains('field_test_field_widget_settings_summary_alter');
  $field_test_settings
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Assert that the field added in
  // field_test_field_widget_third_party_settings_form() is present.
  $field_third_party_test = $page
    ->findField('fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_widget_third_party_settings_form]');
  $this
    ->assertNotEmpty($field_third_party_test, 'The field added in hook_field_widget_third_party_settings_form() is present on the settings form.');
  $field_third_party_test
    ->setValue('foo');
  $page
    ->findButton('Update')
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $button_save
    ->click();
  $this
    ->drupalGet($manage_display);

  // Assert the third party settings.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
  $display = $form_storage
    ->loadUnchanged('node.' . $this->type . '.default');
  $this
    ->assertEquals('foo', $display
    ->getRenderer('field_test')
    ->getThirdPartySetting('field_third_party_test', 'field_test_widget_third_party_settings_form'));
  $this
    ->assertContains('field_third_party_test', $display
    ->calculateDependencies()
    ->getDependencies()['module'], 'Form display does not have a dependency on field_third_party_test module.');

  // 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);
  $field_onewidgetfield_type = $page
    ->findField('fields[field_onewidgetfield][type]');
  $field_test_drag_handle = $page
    ->find('css', '#field-test .tabledrag-handle');
  $field_region = $page
    ->findField('fields[field_test][region]');
  $weight_toggle = $page
    ->find('css', '.tabledrag-toggle-weight');
  $target = $page
    ->find('css', '.region-hidden-message');

  // Checks if the select elements contain the specified options.
  $this
    ->assertFieldSelectOptions($field_test_type, [
    'test_field_widget',
    'test_field_widget_multilingual',
    'test_field_widget_multiple',
  ]);
  $this
    ->assertFieldSelectOptions($field_onewidgetfield_type, [
    'test_field_widget',
    'test_field_widget_multilingual',
  ]);
  $field_test_drag_handle
    ->dragTo($target);
  $assert_session
    ->assertWaitOnAjaxRequest();
  $button_save
    ->click();

  // Validate the changed display settings on the server.

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
  $display = $form_storage
    ->loadUnchanged("node.{$this->type}.default");
  $this
    ->assertNull($display
    ->getComponent('field_test'));

  // Switch to manual mode.
  $weight_toggle
    ->click();

  // Change the region to content using the region field.
  $this
    ->assertEquals('hidden', $field_region
    ->getValue());
  $field_region
    ->setValue('content');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $button_save
    ->click();

  // Validate the change on the server.
  $this
    ->drupalGet($manage_display);
  $display = EntityFormDisplay::load("node.{$this->type}.default");
  $this
    ->assertNotNull($display
    ->getComponent('field_test'));
}