You are here

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

Tests formatting for referenced entities that have no label.

File

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

Class

EntityReferenceTest
Tests the entity reference table formatter.

Namespace

Drupal\Tests\reference_table_formatter\Kernel

Code

public function testNoLabel() {
  $this
    ->installEntitySchema('entity_test_no_label');
  $field = $this
    ->setUpChildFields('entity_test_no_label', 'entity_test_no_label');
  $child_0 = EntityTestNoLabel::create([
    'name' => $this
      ->randomMachineName(),
    'field_string' => 'Foo',
  ]);
  $child_0
    ->save();
  $child_1 = EntityTestNoLabel::create([
    'name' => $this
      ->randomMachineName(),
    'field_string' => 'Bar',
  ]);
  $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_no_label',
    ],
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => 'References',
    'settings' => [
      'handler' => 'default:entity_test_no_label',
      'handler_settings' => [
        'target_bundles' => [
          'entity_test_no_label' => 'entity_test_no_label',
        ],
      ],
    ],
  ]);
  $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('Foo');
  $this
    ->assertText('Bar');
  $elements = $this
    ->cssSelect('th');
  $this
    ->assertCount(1, $elements, 'Only fields shown event with show_entity_label set to TRUE when entities have no label key.');
}