You are here

public function ManageDisplayTest::testFormatterUI in Drupal 10

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

Tests formatter settings.

File

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

Class

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

Namespace

Drupal\Tests\field_ui\FunctionalJavascript

Code

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');
  $display_id = 'node.' . $this->type . '.default';
  $display_storage = $this->entity_type_manager
    ->getStorage('entity_view_display');

  // Get the display options (formatter and settings) that were automatically
  // assigned for the 'default' display.

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  $display = $display_storage
    ->loadUnchanged($display_id);
  $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);
  $session = $this
    ->getSession();
  $assert_session = $this
    ->assertSession();
  $page = $session
    ->getPage();

  // Find commonly used elements in this test.
  $button_save = $page
    ->findButton('Save');
  $field_test_format_type = $page
    ->findField('fields[field_test][type]');
  $field_test_drag_handle = $page
    ->find('css', '#field-test .tabledrag-handle');
  $field_test_settings = $page
    ->find('css', 'input[name="field_test_settings_edit"]');
  $weight_toggle = $page
    ->find('css', '.tabledrag-toggle-weight');

  // Assert the format type field is visible and contains the expected
  // formatter.
  $this
    ->assertTrue($field_test_format_type
    ->isVisible());
  $this
    ->assertEquals($format, $field_test_format_type
    ->getValue());
  $assert_session
    ->responseContains("{$setting_name}: {$setting_value}");

  // Validate the selectbox.
  $this
    ->assertFieldSelectOptions($field_test_format_type, [
    'field_no_settings',
    'field_empty_test',
    'field_empty_setting',
    'field_test_default',
    'field_test_multiple',
    'field_test_with_prepare_view',
    'field_test_applicable',
  ]);

  // Ensure that fields can be hidden directly by dragging the element.
  $target = $page
    ->find('css', '.region-hidden-message');
  $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\EntityViewDisplayInterface $display */
  $display = $display_storage
    ->loadUnchanged($display_id);
  $this
    ->assertNull($display
    ->getComponent('field_test'));

  // Switch to manual mode.
  $weight_toggle
    ->click();
  $field_region = $page
    ->findField('fields[field_test][region]');

  // 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();

  // Change the format for the test field.
  $field_test_format_type
    ->setValue('field_test_multiple');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $plugin_summary = $page
    ->find('css', '#field-test .field-plugin-summary');
  $this
    ->assertStringContainsString("test_formatter_setting_multiple: dummy test string", $plugin_summary
    ->getText(), 'The expected summary is displayed.');

  // Submit the form and assert that
  // hook_field_formatter_settings_summary_alter() is called.
  $button_save
    ->click();
  $assert_session
    ->responseContains('field_test_field_formatter_settings_summary_alter');

  // Open the settings form for the test field.
  $field_test_settings
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Assert that the field added in
  // field_test_field_formatter_third_party_settings_form() is present.
  $field_third_party = $page
    ->findField('fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_field_formatter_third_party_settings_form]');
  $this
    ->assertNotEmpty($field_third_party, 'The field added in hook_field_formatter_third_party_settings_form() is present on the settings form.');

  // Change the value and submit the form to save the third party settings.
  $field_third_party
    ->setValue('foo');
  $page
    ->findButton('Update')
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $button_save
    ->click();

  // Assert the third party settings.
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $this
    ->drupalGet($manage_display);
  $id = 'node.' . $this->type . '.default';

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  $display = $display_storage
    ->loadUnchanged($id);
  $this
    ->assertEquals('foo', $display
    ->getRenderer('field_test')
    ->getThirdPartySetting('field_third_party_test', 'field_test_field_formatter_third_party_settings_form'));
  $this
    ->assertContains('field_third_party_test', $display
    ->calculateDependencies()
    ->getDependencies()['module'], 'The display has a dependency on field_third_party_test module.');

  // Change the formatter to an empty setting and validate it's initialized
  // correctly.
  $field_test_format_type = $page
    ->findField('fields[field_test][type]');
  $field_test_format_type
    ->setValue('field_empty_setting');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->responseNotContains('Default empty setting now has a value.');
  $this
    ->assertTrue($field_test_settings
    ->isVisible(), TRUE);

  // Set the empty_setting option to a non-empty value again and validate
  // the formatting summary now display's this correctly.
  $field_test_settings
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $field_empty_setting = $page
    ->findField('fields[field_test][settings_edit_form][settings][field_empty_setting]');
  $field_empty_setting
    ->setValue('non empty setting');
  $page
    ->findButton('Update')
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $assert_session
    ->responseContains('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.
  $field_test_format_type
    ->setValue('field_no_settings');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $this
    ->assertTrue($field_test_settings
    ->isVisible());

  // Make sure we can save the third party settings when there are no settings
  // available.
  $field_test_settings
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->findButton('Update')
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();

  // 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
    ->assertTrue($field_test_settings
    ->isVisible(), TRUE);

  // Ensure that third-party form elements are not present anymore.
  $field_test_settings
    ->click();
  $assert_session
    ->assertWaitOnAjaxRequest();
  $field_third_party = $page
    ->findField('fields[field_test][settings_edit_form][third_party_settings][field_third_party_test][field_test_field_formatter_third_party_settings_form]');
  $this
    ->assertEmpty($field_third_party);

  // Ensure that third-party settings were removed from the formatter.

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  $display = $display_storage
    ->loadUnchanged($display_id);
  $component = $display
    ->getComponent('field_test');
  $this
    ->assertArrayNotHasKey('field_third_party_test', $component['third_party_settings']);
}