class FieldGroupRowsTest

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Kernel/Handler/FieldGroupRowsTest.php \Drupal\Tests\views\Kernel\Handler\FieldGroupRowsTest

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

@group views

Hierarchy

Expanded class hierarchy of FieldGroupRowsTest

See also

\Drupal\views\Plugin\views\field\EntityField

File

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

Namespace

Drupal\Tests\views\Kernel\Handler
View source
class FieldGroupRowsTest extends ViewsKernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'field',
    'filter',
    'node',
    'text',
    'user',
  ];
  
  /**
   * {@inheritdoc}
   */
  public static $testViews = [
    'test_group_rows',
    'test_ungroup_rows',
  ];
  
  /**
   * Testing the "Grouped rows" functionality.
   */
  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);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.