You are here

public function EntityReferenceTest::testEntityReferenceRevisions in Reference Table Formatter 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Kernel/EntityReferenceTest.php \Drupal\Tests\reference_table_formatter\Kernel\EntityReferenceTest::testEntityReferenceRevisions()

Tests compatibility with entity reference revisions field type.

@requires module entity_reference_revisions @requires module paragraphs

File

tests/src/Kernel/EntityReferenceTest.php, line 289

Class

EntityReferenceTest
Tests the entity reference table formatter.

Namespace

Drupal\Tests\reference_table_formatter\Kernel

Code

public function testEntityReferenceRevisions() {
  $this
    ->installModule('entity_reference_revisions');
  $this
    ->installModule('file');
  $this
    ->installModule('paragraphs');
  $this
    ->installEntitySchema('paragraph');
  ParagraphsType::create([
    'id' => 'test_bundle',
  ])
    ->save();
  $this
    ->setUpChildFields('paragraph', 'test_bundle');
  $paragraph_0 = Paragraph::create([
    'type' => 'test_bundle',
    'field_string' => 'Foo',
  ]);
  $paragraph_0
    ->save();
  $paragraph_1 = Paragraph::create([
    'type' => 'test_bundle',
    'field_string' => 'Bar',
  ]);
  $paragraph_1
    ->save();

  // Setup reference field to be used on the parent entity.
  $field_storage = FieldStorageConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'field_reference',
    'type' => 'entity_reference_revisions',
    'cardinality' => -1,
    'settings' => [
      'target_type' => 'paragraph',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => 'References',
    'settings' => [
      'handler' => 'default:paragraph',
      'handler_settings' => [
        'target_bundles' => [
          'test_bundle' => 'test_bundle',
        ],
      ],
    ],
  ]);
  $field
    ->save();
  $parent = EntityTest::create([
    'field_reference' => [
      [
        'target_id' => $paragraph_0
          ->id(),
        'target_revision_id' => $paragraph_0
          ->getRevisionId(),
      ],
      [
        'target_id' => $paragraph_1
          ->id(),
        'target_revision_id' => $paragraph_1
          ->getRevisionId(),
      ],
    ],
  ]);
  $build = $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($parent) {
    return $parent->field_reference
      ->view([
      'type' => 'entity_reference_table',
    ]);
  });
  $this
    ->setRawContent($this->renderer
    ->renderPlain($build));
  $this
    ->assertText('Foo');
  $this
    ->assertText('Bar');
}