You are here

public function FieldGroupRowsTest::testGroupRows in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Handler/FieldGroupRowsTest.php \Drupal\Tests\views\Kernel\Handler\FieldGroupRowsTest::testGroupRows()
  2. 9 core/modules/views/tests/src/Kernel/Handler/FieldGroupRowsTest.php \Drupal\Tests\views\Kernel\Handler\FieldGroupRowsTest::testGroupRows()

Testing the "Grouped rows" functionality.

File

core/modules/views/tests/src/Kernel/Handler/FieldGroupRowsTest.php, line 42

Class

FieldGroupRowsTest
Tests the "Display all values in the same row" setting.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testGroupRows() {
  $this
    ->installConfig([
    'filter',
  ]);
  $this
    ->installEntitySchema('node');
  $this
    ->installEntitySchema('user');
  NodeType::create([
    'type' => 'page',
  ])
    ->save();

  // Create a text with unlimited cardinality.
  FieldStorageConfig::create([
    'type' => 'text',
    'entity_type' => 'node',
    'field_name' => 'field_group_rows',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ])
    ->save();
  FieldConfig::create([
    'entity_type' => 'node',
    'bundle' => 'page',
    'field_name' => 'field_group_rows',
  ])
    ->save();
  Node::create([
    'type' => 'page',
    'title' => $this
      ->randomMachineName(),
    'field_group_rows' => [
      'a',
      'b',
      'c',
    ],
  ])
    ->save();
  $renderer = $this->container
    ->get('renderer');
  $view = Views::getView('test_group_rows');

  // Test grouped rows.
  $this
    ->executeView($view);
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['field_group_rows']
      ->advancedRender($view->result[0]);
  });
  $this
    ->assertEquals('a, b, c', $output);

  // Change the group_rows checkbox to false.
  $view = Views::getView('test_group_rows');
  $view
    ->setHandlerOption('default', 'field', 'field_group_rows', 'group_rows', FALSE);

  // Test ungrouped rows.
  $this
    ->executeView($view);
  $view
    ->render();
  $view->row_index = 0;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['field_group_rows']
      ->advancedRender($view->result[0]);
  });
  $this
    ->assertEquals('a', $output);
  $view->row_index = 1;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['field_group_rows']
      ->advancedRender($view->result[1]);
  });
  $this
    ->assertEquals('b', $output);
  $view->row_index = 2;
  $output = $renderer
    ->executeInRenderContext(new RenderContext(), function () use ($view) {
    return $view->field['field_group_rows']
      ->advancedRender($view->result[2]);
  });
  $this
    ->assertEquals('c', $output);
}