3 namespace Drupal\Tests\views\Kernel\Plugin;
5 use Drupal\views\Views;
6 use Drupal\views\ViewExecutable;
7 use Symfony\Component\HttpFoundation\Request;
10 * Tests the table style plugin.
13 * @see \Drupal\views\Plugin\views\style\Table
15 class StyleTableUnitTest extends PluginKernelTestBase {
18 * Views used by this test.
22 public static $testViews = ['test_table'];
25 * Tests the table style.
27 public function testTable() {
28 $view = Views::getView('test_table');
30 $view->setDisplay('default');
32 $view->initHandlers();
34 $style_plugin = $view->style_plugin;
36 // Test the buildSort() method.
37 $request = new Request();
38 $view->setRequest($request);
40 $style_plugin->options['override'] = FALSE;
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.');
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.');
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.');
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.');
54 $style_plugin->options['override'] = TRUE;
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.');
58 // Test the buildSortPost() method.
59 $request = new Request();
60 $view->setRequest($request);
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.');
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.');
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.');
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.');
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.');
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.");
125 // Render a non empty result, and ensure that the empty area handler is not
127 $this->executeView($view);
128 $output = $view->preview();
129 $output = \Drupal::service('renderer')->renderRoot($output);
131 $this->assertFalse(strpos($output, 'custom text') !== FALSE, 'Empty handler was not rendered on a non empty table.');
133 // Render an empty result, and ensure that the area handler is rendered.
134 $view->setDisplay('default');
135 $view->executed = TRUE;
137 $output = $view->preview();
138 $output = \Drupal::service('renderer')->renderRoot($output);
140 $this->assertTrue(strpos($output, 'custom text') !== FALSE, 'Empty handler got rendered on an empty table.');
144 * Prepares a view executable by initializing everything which is needed.
146 * @param \Drupal\views\ViewExecutable $view
147 * The executable to prepare.
149 protected function prepareView(ViewExecutable $view) {
152 $view->initHandlers();