1 field_ui.test FieldUIManageDisplayTestCase::testViewModeCustom()

Tests switching display modes to use custom or 'default' settings'.

File

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

Class

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

Code

function testViewModeCustom() {
  // 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('admin/structure/types/manage/' . $this->hyphen_type, $edit);
  // For this test, use a formatter setting value that is an integer unlikely
  // to appear in a rendered node other than as part of the field being tested
  // (for example, unlikely to be part of the author and date information
  // line).
  $value = 12345;
  $settings = array(
    'type' => $this->type,
    'field_test' => array(LANGUAGE_NONE => array(array('value' => $value))),
  );
  $node = $this->backdropCreateNode($settings);

  // Gather expected output values with the various formatters.
  $formatters = field_info_formatter_types();
  $output = array(
    'field_test_default' => $formatters['field_test_default']['settings']['test_formatter_setting'] . '|' . $value,
    'field_test_with_prepare_view' => $formatters['field_test_with_prepare_view']['settings']['test_formatter_setting_additional'] . '|' . $value . '|' . ($value + 1),
  );

  // Check that the field is displayed with the default formatter in 'rss'
  // mode (uses 'default'), and hidden in 'teaser' mode (uses custom settings).
  $this->assertNodeViewText($node, 'rss', $output['field_test_default'], "The field is displayed as expected in display modes that use 'default' settings.");
  $this->assertNodeViewNoText($node, 'teaser', $value, "The field is hidden in display modes that use custom settings.");

  // Change fomatter for 'default' mode, check that the field is displayed
  // accordingly in 'rss' mode.
  $edit = array(
    'fields[field_test][type]' => 'field_test_with_prepare_view',
  );
  $this->backdropPost('admin/structure/types/manage/' . $this->hyphen_type . '/display/default', $edit, t('Save'));
  $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected in display modes that use 'default' settings.");

  // Specialize the 'rss' mode, check that the field is displayed the same.
  // The enable link is protected with a CSRF token, so get the link href
  // directly using XPath.
  $this->backdropGet('admin/structure/types/manage/' . $this->hyphen_type . '/display');
  list($enable_link) = $this->xpath('//tr[contains(@class, "view-mode--rss")]//a');
  $enable_href_parts = backdrop_parse_url($enable_link['href']);
  $this->backdropGet($enable_href_parts['path'], $enable_href_parts);
  $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected in newly specialized 'rss' mode.");

  // Set the field to 'hidden' in the display mode, check that the field is
  // hidden.
  $edit = array(
    'fields[field_test][type]' => 'hidden',
  );
  $this->backdropPost('admin/structure/types/manage/' . $this->hyphen_type . '/display/rss', $edit, t('Save'));
  $this->assertNodeViewNoText($node, 'rss', $value, "The field is hidden in 'rss' mode.");

  // Set the display mode back to 'default', check that the field is displayed
  // accordingly.
  $this->backdropPost('admin/structure/types/manage/' . $this->hyphen_type . '/display/rss/reset', array(), t('Reset'));
  $this->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected when 'rss' mode is set back to 'default' settings.");

  // Specialize the display mode again.
  $this->backdropGet($enable_href_parts['path'], $enable_href_parts);
  // Check that the previous settings for the display mode have been kept.
  $this->assertNodeViewNoText($node, 'rss', $value, "The previous settings are kept when 'rss' mode is specialized again.");
}