Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / tests / src / Kernel / Plugin / StyleTableUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Kernel\Plugin;
4
5 use Drupal\views\Views;
6 use Drupal\views\ViewExecutable;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10  * Tests the table style plugin.
11  *
12  * @group views
13  * @see \Drupal\views\Plugin\views\style\Table
14  */
15 class StyleTableUnitTest extends PluginKernelTestBase {
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['test_table'];
23
24   /**
25    * Tests the table style.
26    */
27   public function testTable() {
28     $view = Views::getView('test_table');
29
30     $view->setDisplay('default');
31     $view->initStyle();
32     $view->initHandlers();
33     $view->initQuery();
34     $style_plugin = $view->style_plugin;
35
36     // Test the buildSort() method.
37     $request = new Request();
38     $view->setRequest($request);
39
40     $style_plugin->options['override'] = FALSE;
41
42     $style_plugin->options['default'] = '';
43     $this->assertTrue($style_plugin->buildSort(), 'If no order and no default order is specified, the normal sort should be used.');
44
45     $style_plugin->options['default'] = 'id';
46     $this->assertTrue($style_plugin->buildSort(), 'If no order but a default order is specified, the normal sort should be used.');
47
48     $request->attributes->set('order', $this->randomMachineName());
49     $this->assertTrue($style_plugin->buildSort(), 'If no valid field is chosen for order, the normal sort should be used.');
50
51     $request->attributes->set('order', 'id');
52     $this->assertTrue($style_plugin->buildSort(), 'If a valid order is specified but the table is configured to not override, the normal sort should be used.');
53
54     $style_plugin->options['override'] = TRUE;
55
56     $this->assertFalse($style_plugin->buildSort(), 'If a valid order is specified and the table is configured to override, the normal sort should not be used.');
57
58     // Test the buildSortPost() method.
59     $request = new Request();
60     $view->setRequest($request);
61
62     // Setup no valid default.
63     $this->prepareView($view);
64     $style_plugin = $view->style_plugin;
65     $style_plugin->options['default'] = '';
66     $style_plugin->buildSortPost();
67     $this->assertIdentical($style_plugin->order, NULL, 'No sort order was set, when no order was specified and no default column was selected.');
68     $this->assertIdentical($style_plugin->active, NULL, 'No sort field was set, when no order was specified and no default column was selected.');
69     $view->destroy();
70
71     // Setup a valid default + column specific default sort order.
72     $this->prepareView($view);
73     $style_plugin = $view->style_plugin;
74     $style_plugin->options['default'] = 'id';
75     $style_plugin->options['info']['id']['default_sort_order'] = 'desc';
76     $style_plugin->buildSortPost();
77     $this->assertIdentical($style_plugin->order, 'desc', 'Fallback to the right default sort order.');
78     $this->assertIdentical($style_plugin->active, 'id', 'Fallback to the right default sort field.');
79     $view->destroy();
80
81     // Setup a valid default + table default sort order.
82     $this->prepareView($view);
83     $style_plugin = $view->style_plugin;
84     $style_plugin->options['default'] = 'id';
85     $style_plugin->options['info']['id']['default_sort_order'] = NULL;
86     $style_plugin->options['order'] = 'asc';
87     $style_plugin->buildSortPost();
88     $this->assertIdentical($style_plugin->order, 'asc', 'Fallback to the right default sort order.');
89     $this->assertIdentical($style_plugin->active, 'id', 'Fallback to the right default sort field.');
90     $view->destroy();
91
92     // Use an invalid field.
93     $this->prepareView($view);
94     $style_plugin = $view->style_plugin;
95     $request->query->set('sort', 'asc');
96     $random_name = $this->randomMachineName();
97     $request->query->set('order', $random_name);
98     $style_plugin->buildSortPost();
99     $this->assertIdentical($style_plugin->order, 'asc', 'No sort order was set, when invalid sort order was specified.');
100     $this->assertIdentical($style_plugin->active, NULL, 'No sort field was set, when invalid sort order was specified.');
101     $view->destroy();
102
103     // Use a existing field, and sort both ascending and descending.
104     foreach (['asc', 'desc'] as $order) {
105       $this->prepareView($view);
106       $style_plugin = $view->style_plugin;
107       $request->query->set('sort', $order);
108       $request->query->set('order', 'id');
109       $style_plugin->buildSortPost();
110       $this->assertIdentical($style_plugin->order, $order, 'Ensure the right sort order was set.');
111       $this->assertIdentical($style_plugin->active, 'id', 'Ensure the right order was set.');
112       $view->destroy();
113     }
114
115     $view->destroy();
116
117     // Excluded field to make sure its wrapping td doesn't show.
118     $this->prepareView($view);
119     $view->field['name']->options['exclude'] = TRUE;
120     $output = $view->preview();
121     $output = \Drupal::service('renderer')->renderRoot($output);
122     $this->assertFalse(strpos($output, 'views-field-name') !== FALSE, "Excluded field's wrapper was not rendered.");
123     $view->destroy();
124
125     // Render a non empty result, and ensure that the empty area handler is not
126     // rendered.
127     $this->executeView($view);
128     $output = $view->preview();
129     $output = \Drupal::service('renderer')->renderRoot($output);
130
131     $this->assertFalse(strpos($output, 'custom text') !== FALSE, 'Empty handler was not rendered on a non empty table.');
132
133     // Render an empty result, and ensure that the area handler is rendered.
134     $view->setDisplay('default');
135     $view->executed = TRUE;
136     $view->result = [];
137     $output = $view->preview();
138     $output = \Drupal::service('renderer')->renderRoot($output);
139
140     $this->assertTrue(strpos($output, 'custom text') !== FALSE, 'Empty handler got rendered on an empty table.');
141   }
142
143   /**
144    * Prepares a view executable by initializing everything which is needed.
145    *
146    * @param \Drupal\views\ViewExecutable $view
147    *   The executable to prepare.
148    */
149   protected function prepareView(ViewExecutable $view) {
150     $view->setDisplay();
151     $view->initStyle();
152     $view->initHandlers();
153     $view->initQuery();
154   }
155
156 }