You are here

function ManageDisplayTest::testViewModeCustom in Zircon Profile 8.0

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

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

File

core/modules/field_ui/src/Tests/ManageDisplayTest.php, line 292
Contains \Drupal\field_ui\Tests\ManageDisplayTest.

Class

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

Namespace

Drupal\field_ui\Tests

Code

function testViewModeCustom() {

  // Create a field, and a node with some data for the field.
  $this
    ->fieldUIAddNewField('admin/structure/types/manage/' . $this->type, 'test', 'Test field');
  \Drupal::entityManager()
    ->clearCachedFieldDefinitions();

  // 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 "Submitted by ... on ..." line).
  $value = 12345;
  $settings = array(
    'type' => $this->type,
    'field_test' => array(
      array(
        'value' => $value,
      ),
    ),
  );
  $node = $this
    ->drupalCreateNode($settings);

  // Gather expected output values with the various formatters.
  $formatter_plugin_manager = \Drupal::service('plugin.manager.field.formatter');
  $field_test_default_settings = $formatter_plugin_manager
    ->getDefaultSettings('field_test_default');
  $field_test_with_prepare_view_settings = $formatter_plugin_manager
    ->getDefaultSettings('field_test_with_prepare_view');
  $output = array(
    'field_test_default' => $field_test_default_settings['test_formatter_setting'] . '|' . $value,
    'field_test_with_prepare_view' => $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 view modes that use 'default' settings.");
  $this
    ->assertNodeViewNoText($node, 'teaser', $value, "The field is hidden in view modes that use custom settings.");

  // Change formatter 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
    ->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
  $this
    ->assertNodeViewText($node, 'rss', $output['field_test_with_prepare_view'], "The field is displayed as expected in view modes that use 'default' settings.");

  // Specialize the 'rss' mode, check that the field is displayed the same.
  $edit = array(
    "display_modes_custom[rss]" => TRUE,
  );
  $this
    ->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
  $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 view mode, check that the field is
  // hidden.
  $edit = array(
    'fields[field_test][type]' => 'hidden',
  );
  $this
    ->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display/rss', $edit, t('Save'));
  $this
    ->assertNodeViewNoText($node, 'rss', $value, "The field is hidden in 'rss' mode.");

  // Set the view mode back to 'default', check that the field is displayed
  // accordingly.
  $edit = array(
    "display_modes_custom[rss]" => FALSE,
  );
  $this
    ->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));
  $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 view mode again.
  $edit = array(
    "display_modes_custom[rss]" => TRUE,
  );
  $this
    ->drupalPostForm('admin/structure/types/manage/' . $this->type . '/display', $edit, t('Save'));

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