3 namespace Drupal\Tests\views\Functional\Handler;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Render\RenderContext;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\Tests\views\Functional\ViewTestBase;
9 use Drupal\views\Views;
10 use Drupal\field\Entity\FieldStorageConfig;
13 * Tests the "Display all values in the same row" setting.
15 * @see \Drupal\views\Plugin\views\field\EntityField
19 class FieldGroupRowsTest extends ViewTestBase {
22 * Views used by this test.
26 public static $testViews = ['test_group_rows', 'test_ungroup_rows'];
33 public static $modules = ['node', 'field_test'];
36 * Field that will be created to test the group/ungroup rows functionality
40 private $fieldName = 'field_group_rows';
42 protected function setUp($import_test_views = TRUE) {
43 parent::setUp($import_test_views);
45 // Create content type with unlimited text field.
46 $node_type = $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
48 // Create the unlimited text field.
49 $field_storage = FieldStorageConfig::create([
50 'field_name' => $this->fieldName,
51 'entity_type' => 'node',
53 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
55 $field_storage->save();
57 // Create an instance of the text field on the content type.
59 'field_storage' => $field_storage,
60 'bundle' => $node_type->id(),
62 FieldConfig::create($field)->save();
66 * Testing the "Grouped rows" functionality.
68 public function testGroupRows() {
69 /** @var \Drupal\Core\Render\RendererInterface $renderer */
70 $renderer = \Drupal::service('renderer');
73 'title' => $this->randomMachineName(),
74 $this->fieldName => ['a', 'b', 'c'],
76 $this->drupalCreateNode($edit);
78 $view = Views::getView('test_group_rows');
81 $this->executeView($view);
82 $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
83 return $view->field[$this->fieldName]->advancedRender($view->result[0]);
85 $this->assertEqual($output, 'a, b, c');
87 // Change the group_rows checkbox to false.
88 $view = Views::getView('test_group_rows');
89 $view->setHandlerOption('default', 'field', $this->fieldName, 'group_rows', FALSE);
91 // Test ungrouped rows.
92 $this->executeView($view);
96 $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
97 return $view->field[$this->fieldName]->advancedRender($view->result[0]);
99 $this->assertEqual($output, 'a');
100 $view->row_index = 1;
101 $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
102 return $view->field[$this->fieldName]->advancedRender($view->result[1]);
104 $this->assertEqual($output, 'b');
105 $view->row_index = 2;
106 $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
107 return $view->field[$this->fieldName]->advancedRender($view->result[2]);
109 $this->assertEqual($output, 'c');