1 field_ui.test FieldUIManageDisplayTestCase::testFormatterUI()

Tests formatter settings.

File

core/modules/field_ui/tests/field_ui.test, line 559
Tests for field_ui.module.

Class

FieldUIManageDisplayTestCase
Tests the functionality of the 'Manage displays' screens.

Code

function testFormatterUI() {
  $manage_fields = 'admin/structure/types/manage/' . $this->hyphen_type;
  $manage_display = $manage_fields . '/display/default';

  // 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.
  field_info_cache_clear();
  $instance = field_info_instance('node', 'field_test', $this->type);
  $format = $instance['display']['default']['type'];
  $default_settings = field_info_formatter_settings($format);
  $setting_name = key($default_settings);
  $setting_value = $instance['display']['default']['settings'][$setting_name];

  // Display the "Manage displays" screen and check that the expected
  // formatter is selected.
  $this->backdropGet($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.');

  // Change the formatter and check that the summary is updated.
  $edit = array('fields[field_test][type]' => 'field_test_multiple', 'refresh_rows' => 'field_test');
  $this->backdropPostAJAX(NULL, $edit, array('op' => t('Refresh')));
  $format = 'field_test_multiple';
  $default_settings = field_info_formatter_settings($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 instance is updated.
  $this->backdropPost(NULL, array(), t('Save'));
  field_info_cache_clear();
  $instance = field_info_instance('node', 'field_test', $this->type);
  $current_format = $instance['display']['default']['type'];
  $current_setting_value = $instance['display']['default']['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->backdropPostAJAX(NULL, array(), "field_test_formatter_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->backdropPostAJAX(NULL, $edit, "field_test_formatter_settings_update");

  // Confirm that the settings are updated on the settings form.
  $this->assertText(t('Display text: @text', array('@text' => 'foo')));
}