function ManageDisplayTest::testFormatterUI

You are here

8 ManageDisplayTest.php ManageDisplayTest::testFormatterUI()

Tests formatter settings.

File

core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageDisplayTest.php, line 35
Contains \Drupal\field_ui\Tests\ManageDisplayTest.

Namespace

Drupal\field_ui\Tests

Class

ManageDisplayTest
Tests the functionality of the 'Manage display' screens.

Code

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.
  $edit = array(
    'fields[_add_new_field][label]' => 'Test field',
    'fields[_add_new_field][field_name]' => 'test',
  );
  $this->fieldUIAddNewField($manage_fields, $edit);

  // Clear the test-side cache and get the saved field instance.
  $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',
    '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_settings_form_alter() is present.
  $fieldname = 'fields[field_test][settings_edit_form][settings][field_test_formatter_settings_form_alter]';
  $this->assertField($fieldname, 'The field added in hook_field_formatter_settings_form_alter() is present on the settings form.');
  $edit = array($fieldname => 'foo');
  $this->drupalPostAjaxForm(NULL, $edit, "field_test_plugin_settings_update");

  // Confirm that the extra 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 no settings form behavior.
  $edit = array(
    'fields[field_test][type]' => 'field_no_settings',
    'refresh_rows' => 'field_test',
  );
  $this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Refresh')));
  $this->assertNoFieldByName('field_test_settings_edit');
}