d38ba41792bac35df11e849c10c26bef37008599
[yaffs-website] / web / core / modules / views / tests / src / Functional / Handler / FieldGroupRowsTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Handler;
4
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;
11
12 /**
13  * Tests the "Display all values in the same row" setting.
14  *
15  * @see \Drupal\views\Plugin\views\field\EntityField
16  *
17  * @group views
18  */
19 class FieldGroupRowsTest extends ViewTestBase {
20
21   /**
22    * Views used by this test.
23    *
24    * @var array
25    */
26   public static $testViews = ['test_group_rows', 'test_ungroup_rows'];
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['node', 'field_test'];
34
35   /**
36    * Field that will be created to test the group/ungroup rows functionality
37    *
38    * @var string
39    */
40   private $fieldName = 'field_group_rows';
41
42   protected function setUp($import_test_views = TRUE) {
43     parent::setUp($import_test_views);
44
45     // Create content type with unlimited text field.
46     $node_type = $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
47
48     // Create the unlimited text field.
49     $field_storage = FieldStorageConfig::create([
50         'field_name' => $this->fieldName,
51         'entity_type' => 'node',
52         'type' => 'text',
53         'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
54       ]);
55     $field_storage->save();
56
57     // Create an instance of the text field on the content type.
58     $field = [
59       'field_storage' => $field_storage,
60       'bundle' => $node_type->id(),
61     ];
62     FieldConfig::create($field)->save();
63   }
64
65   /**
66    * Testing the "Grouped rows" functionality.
67    */
68   public function testGroupRows() {
69     /** @var \Drupal\Core\Render\RendererInterface $renderer */
70     $renderer = \Drupal::service('renderer');
71
72     $edit = [
73       'title' => $this->randomMachineName(),
74       $this->fieldName => ['a', 'b', 'c'],
75     ];
76     $this->drupalCreateNode($edit);
77
78     $view = Views::getView('test_group_rows');
79
80     // Test grouped rows.
81     $this->executeView($view);
82     $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
83       return $view->field[$this->fieldName]->advancedRender($view->result[0]);
84     });
85     $this->assertEqual($output, 'a, b, c');
86
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);
90
91     // Test ungrouped rows.
92     $this->executeView($view);
93     $view->render();
94
95     $view->row_index = 0;
96     $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
97       return $view->field[$this->fieldName]->advancedRender($view->result[0]);
98     });
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]);
103     });
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]);
108     });
109     $this->assertEqual($output, 'c');
110   }
111
112 }