// 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."
);