You are here

public function EntityToTableRendererTest::testEmptyFields in Reference Table Formatter 8

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

Tests handling of empty fields.

File

tests/src/Kernel/EntityToTableRendererTest.php, line 267

Class

EntityToTableRendererTest
Tests reference table formatter entity to table renderer.

Namespace

Drupal\Tests\reference_table_formatter\Kernel

Code

public function testEmptyFields() {
  $this
    ->createField([
    'type' => 'string',
    'field_name' => 'field_test',
  ], [
    'bundle' => 't_shirt',
  ]);

  // Create a field which will be empty across all referenced entities to test
  // it gets filtered out.
  $this
    ->createField([
    'type' => 'string',
    'field_name' => 'field_empty',
  ], [
    'bundle' => 't_shirt',
  ]);
  $this->displayRepository
    ->getViewDisplay('node', 't_shirt', 'teaser')
    ->setComponent('field_test')
    ->setComponent('field_empty')
    ->save();
  $node_0 = Node::create([
    'title' => $this
      ->randomMachineName(),
    'type' => 't_shirt',
    // Tests entity with less field values still has field present in table.
    'field_color' => 'Red',
    // Tests overlapping field value with other entity.
    'field_test' => $this
      ->randomMachineName(),
  ]);
  $node_0
    ->save();
  $node_1 = Node::create([
    'title' => $this
      ->randomMachineName(),
    'type' => 't_shirt',
    'field_size' => 'L',
    'field_price' => 15.0,
    'field_test' => $this
      ->randomMachineName(),
  ]);
  $node_1
    ->save();
  $nodes = [
    $node_0,
    $node_1,
  ];
  $table = $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($nodes) {
    return $this->tableRenderer
      ->getTable('node', 't_shirt', $nodes, [
      'show_entity_label' => FALSE,
      'view_mode' => 'teaser',
      'empty_cell_value' => '',
      'hide_header' => FALSE,
    ]);
  });
  $expected = [
    'field_price' => 'Price',
    'field_size' => 'Size',
    'field_color' => 'Color',
    'field_test' => 'field_test',
  ];
  $this
    ->assertEquals($expected, $table['#header'], 'All non-empty fields across displayed entities are present.');
}