You are here

public function ManageDisplayTest::testViewModeCustom in Drupal 9

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

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

File

core/modules/field_ui/tests/src/Functional/ManageDisplayTest.php, line 90

Class

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

Namespace

Drupal\Tests\field_ui\Functional

Code

public 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::service('entity_field.manager')
    ->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 = [
    'type' => $this->type,
    'field_test' => [
      [
        '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 = [
    '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 = [
    'fields[field_test][type]' => 'field_test_with_prepare_view',
    'fields[field_test][region]' => 'content',
  ];
  $this
    ->drupalGet('admin/structure/types/manage/' . $this->type . '/display');
  $this
    ->submitForm($edit, '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 = [
    "display_modes_custom[rss]" => TRUE,
  ];
  $this
    ->drupalGet('admin/structure/types/manage/' . $this->type . '/display');
  $this
    ->submitForm($edit, '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 = [
    'fields[field_test][region]' => 'hidden',
  ];
  $this
    ->drupalGet('admin/structure/types/manage/' . $this->type . '/display/rss');
  $this
    ->submitForm($edit, '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 = [
    "display_modes_custom[rss]" => FALSE,
  ];
  $this
    ->drupalGet('admin/structure/types/manage/' . $this->type . '/display');
  $this
    ->submitForm($edit, '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 = [
    "display_modes_custom[rss]" => TRUE,
  ];
  $this
    ->drupalGet('admin/structure/types/manage/' . $this->type . '/display');
  $this
    ->submitForm($edit, '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.");
}