You are here

public function EntityReferenceTest::testGenericEntityFormatting 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::testGenericEntityFormatting()

Tests with generic entity referencing.

File

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

Class

EntityReferenceTest
Tests the entity reference table formatter.

Namespace

Drupal\Tests\reference_table_formatter\Kernel

Code

public function testGenericEntityFormatting() {
  $this
    ->installEntitySchema('entity_test_with_bundle');
  EntityTestBundle::create([
    'id' => 'test_bundle',
  ])
    ->save();
  $this
    ->setUpChildFields('entity_test_with_bundle', 'test_bundle');
  $child_0 = EntityTestWithBundle::create([
    'type' => 'test_bundle',
    'name' => $this
      ->randomMachineName(),
    'field_string' => 'Foo',
  ]);
  $child_0
    ->save();
  $child_1 = EntityTestWithBundle::create([
    'type' => 'test_bundle',
    'name' => $this
      ->randomMachineName(),
  ]);
  $child_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',
    'cardinality' => -1,
    'settings' => [
      'target_type' => 'entity_test_with_bundle',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => 'References',
    'settings' => [
      'handler' => 'default:entity_test_with_bundle',
      'handler_settings' => [
        'target_bundles' => [
          'test_bundle' => 'test_bundle',
        ],
      ],
    ],
  ]);
  $field
    ->save();
  $parent = EntityTest::create([
    'field_reference' => [
      [
        'target_id' => $child_0
          ->id(),
      ],
      [
        'target_id' => $child_1
          ->id(),
      ],
    ],
  ]);
  $build = $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($parent) {
    return $parent->field_reference
      ->view([
      'type' => 'entity_reference_table',
      'settings' => [
        'show_entity_label' => TRUE,
      ],
    ]);
  });
  $this
    ->setRawContent($this->renderer
    ->renderPlain($build));
  $this
    ->assertText($child_0
    ->label());
  $this
    ->assertText('Foo');
  $this
    ->assertText($child_1
    ->label());

  // Test no error raised with deleted referenced entity.
  $child_1
    ->delete();
  $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($parent) {
    return $parent->field_reference
      ->view([
      'type' => 'entity_reference_table',
      'settings' => [
        'show_entity_label' => TRUE,
      ],
    ]);
  });
  $this
    ->expectException(\Exception::class);
  $this
    ->expectExceptionMessage('Using non-default reference handler with reference_table_formatter has not yet been implemented.');

  // Install views so we can test using an entity reference selection plugin
  // other than default.
  $this
    ->installModule('views');
  $field
    ->setSettings([
    'handler' => 'views',
    'handler_settings' => [],
  ])
    ->save();

  // Re-create parent entity to reload latest FieldConfig instance.
  $parent = EntityTest::create([
    'field_reference' => [
      [
        'target_id' => $child_0
          ->id(),
      ],
    ],
  ]);
  $build = $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($parent) {
    return $parent->field_reference
      ->view([
      'type' => 'entity_reference_table',
    ]);
  });
}