You are here

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

Tests formatting with various default selection plugin handler settings.

File

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

Class

EntityReferenceTest
Tests the entity reference table formatter.

Namespace

Drupal\Tests\reference_table_formatter\Kernel

Code

public function testDefaultSelectionTargetBundles() {
  $this
    ->installEntitySchema('entity_test_with_bundle');
  EntityTestBundle::create([
    'id' => 'test_bundle',
  ])
    ->save();
  $this
    ->setUpChildFields('entity_test_with_bundle', 'test_bundle');
  $child = EntityTestWithBundle::create([
    'type' => 'test_bundle',
    'name' => $this
      ->randomMachineName(),
    'field_string' => 'Foo',
  ]);
  $child
    ->save();
  $field_storage = FieldStorageConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'field_missing_bundles',
    '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 Missing Bundles',
    'settings' => [
      'handler' => 'default:entity_test_with_bundle',
      'handler_settings' => [
        'target_bundles' => [],
      ],
    ],
  ]);
  $field
    ->save();
  $parent = EntityTest::create([
    'field_missing_bundles' => [
      [
        'target_id' => $child
          ->id(),
      ],
    ],
  ]);
  $this
    ->expectException(\Exception::class);
  $this
    ->expectExceptionMessage('Cannot render reference table for References Missing Bundles: target_bundles setting on the field should not be empty.');
  $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($parent) {
    return $parent->field_missing_bundles
      ->view([
      'type' => 'entity_reference_table',
    ]);
  });
  $field_storage = FieldStorageConfig::create([
    'entity_type' => 'entity_test',
    'field_name' => 'field_no_bundle',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => [
      'target_type' => 'entity_test',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => 'References No Bundles',
    'settings' => [
      'handler' => 'default:entity_test',
      'handler_settings' => [
        'target_bundles' => [],
      ],
    ],
  ]);
  $field
    ->save();
  $child = EntityTest::create([
    'field_string' => 'Foo',
  ]);
  $child
    ->save();
  $parent = EntityTest::create([
    'field_no_bundle' => [
      [
        'target_id' => $child
          ->id(),
      ],
    ],
  ]);
  $build = $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($parent) {
    return $parent->field_no_bundle
      ->view([
      'type' => 'entity_reference_table',
    ]);
  });
  $this
    ->setRawContent($this->renderer
    ->renderPlain($build));
  $this
    ->assertText('Foo', 'Formatter should work for entities with no bundles.');
}