You are here

public function FieldFormatterWithInlineSettingsTest::testRender in (Entity Reference) Field Formatters 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettingsTest.php \Drupal\Tests\field_formatter\Kernel\Plugin\Field\FieldFormatter\FieldFormatterWithInlineSettingsTest::testRender()
  2. 3.x tests/src/Kernel/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettingsTest.php \Drupal\Tests\field_formatter\Kernel\Plugin\Field\FieldFormatter\FieldFormatterWithInlineSettingsTest::testRender()

Tests field_formatter_with_inline_settings formatter output.

@covers ::viewElements @covers ::getAvailableFieldNames @covers ::getViewDisplay

@dataProvider providerTestRender

Parameters

string|null $label_option: The value for the "label" formatter option.

string $expected_output: The expected output.

File

tests/src/Kernel/Plugin/Field/FieldFormatter/FieldFormatterWithInlineSettingsTest.php, line 75

Class

FieldFormatterWithInlineSettingsTest
@coversDefaultClass \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterWithInlineSettings @group field_formatter

Namespace

Drupal\Tests\field_formatter\Kernel\Plugin\Field\FieldFormatter

Code

public function testRender($label_option, $expected_output) {
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'test_er_field',
    'entity_type' => 'entity_test',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'entity_test',
    ],
  ]);
  $field_storage
    ->save();
  $field_config = FieldConfig::create([
    'field_name' => 'test_er_field',
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
  ]);
  $field_config
    ->save();
  $parent_entity_view_display = EntityViewDisplay::create([
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'default',
    'content' => [],
  ]);
  $parent_entity_view_display
    ->setComponent('test_er_field', [
    'type' => 'field_formatter_with_inline_settings',
    'settings' => [
      'field_name' => 'name',
      'type' => 'string',
      'settings' => [],
      'label' => $label_option,
    ],
  ]);
  $parent_entity_view_display
    ->save();
  $child_entity = EntityTest::create([
    'name' => [
      'child name',
    ],
  ]);
  $child_entity
    ->save();
  $entity = EntityTest::create([
    'test_er_field' => [
      [
        'target_id' => $child_entity
          ->id(),
      ],
    ],
  ]);
  $entity
    ->save();
  $build = $parent_entity_view_display
    ->build($entity);
  \Drupal::service('renderer')
    ->renderRoot($build);

  // Prepare XML structure to compare:
  $expected_output = str_replace([
    "\r",
    "\n",
  ], '', trim($expected_output));
  $actual_output = str_replace([
    "\r",
    "\n",
  ], '', trim($build['test_er_field']['#markup']));
  $this
    ->assertXmlStringEqualsXmlString($expected_output, $actual_output, 'Expected HTML is "' . htmlentities($expected_output) . '" but was: "' . htmlentities($actual_output) . '"');
}