You are here

public function FieldFormatterFromViewDisplayTest::testRender in (Entity Reference) Field Formatters 8

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

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

File

tests/src/Kernel/Plugin/Field/FieldFormatter/FieldFormatterFromViewDisplayTest.php, line 67

Class

FieldFormatterFromViewDisplayTest
@coversDefaultClass \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldFormatterFromViewDisplay @group field_formatter

Namespace

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

Code

public function testRender() {
  $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_from_view_display',
    'settings' => [
      'view_display_id' => 'child',
      'field_name' => 'name',
    ],
  ]);
  $parent_entity_view_display
    ->save();
  $child_view_mode = EntityViewMode::create([
    'targetEntityType' => 'entity_test',
    'id' => 'entity_test.child',
  ]);
  $child_view_mode
    ->save();
  $child_entity_view_display = EntityViewDisplay::create([
    'id' => 'entity_test.entity_test.child',
    'targetEntityType' => 'entity_test',
    'bundle' => 'entity_test',
    'mode' => 'child',
  ]);
  $child_entity_view_display
    ->setComponent('name', [
    'type' => 'string',
  ]);
  $child_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);
  $expected_output = <<<EXPECTED

  <div>
    <div>test_er_field</div>
              <div>
            <div>child name</div>
      </div>
          </div>

EXPECTED;
  $this
    ->assertEquals($expected_output, $build['test_er_field']['#markup']);
}