Pull merge.
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Render / Element / TableSortExtenderTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Render\Element;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
8
9 /**
10  * Tests table sorting.
11  *
12  * @group Common
13  */
14 class TableSortExtenderTest extends KernelTestBase {
15
16   /**
17    * Tests tablesort_init().
18    */
19   public function testTableSortInit() {
20
21     // Test simple table headers.
22
23     $headers = ['foo', 'bar', 'baz'];
24     // Reset $request->query to prevent parameters from Simpletest and Batch API
25     // ending up in $ts['query'].
26     $expected_ts = [
27       'name' => 'foo',
28       'sql' => '',
29       'sort' => 'asc',
30       'query' => [],
31     ];
32     $request = Request::createFromGlobals();
33     $request->query->replace([]);
34     \Drupal::getContainer()->get('request_stack')->push($request);
35     $ts = tablesort_init($headers);
36     $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
37     $this->assertEqual($ts, $expected_ts, 'Simple table headers sorted correctly.');
38
39     // Test with simple table headers plus $_GET parameters that should _not_
40     // override the default.
41     $request = Request::createFromGlobals();
42     $request->query->replace([
43       // This should not override the table order because only complex
44       // headers are overridable.
45       'order' => 'bar',
46     ]);
47     \Drupal::getContainer()->get('request_stack')->push($request);
48     $ts = tablesort_init($headers);
49     $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
50     $this->assertEqual($ts, $expected_ts, 'Simple table headers plus non-overriding $_GET parameters sorted correctly.');
51
52     // Test with simple table headers plus $_GET parameters that _should_
53     // override the default.
54     $request = Request::createFromGlobals();
55     $request->query->replace([
56       'sort' => 'DESC',
57       // Add an unrelated parameter to ensure that tablesort will include
58       // it in the links that it creates.
59       'alpha' => 'beta',
60     ]);
61     \Drupal::getContainer()->get('request_stack')->push($request);
62     $expected_ts['sort'] = 'desc';
63     $expected_ts['query'] = ['alpha' => 'beta'];
64     $ts = tablesort_init($headers);
65     $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
66     $this->assertEqual($ts, $expected_ts, 'Simple table headers plus $_GET parameters sorted correctly.');
67
68     // Test complex table headers.
69
70     $headers = [
71       'foo',
72       [
73         'data' => '1',
74         'field' => 'one',
75         'sort' => 'asc',
76         'colspan' => 1,
77       ],
78       [
79         'data' => '2',
80         'field' => 'two',
81         'sort' => 'desc',
82       ],
83     ];
84     // Reset $_GET from previous assertion.
85     $request = Request::createFromGlobals();
86     $request->query->replace([
87       'order' => '2',
88     ]);
89     \Drupal::getContainer()->get('request_stack')->push($request);
90     $ts = tablesort_init($headers);
91     $expected_ts = [
92       'name' => '2',
93       'sql' => 'two',
94       'sort' => 'desc',
95       'query' => [],
96     ];
97     $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
98     $this->assertEqual($ts, $expected_ts, 'Complex table headers sorted correctly.');
99
100     // Test complex table headers plus $_GET parameters that should _not_
101     // override the default.
102     $request = Request::createFromGlobals();
103     $request->query->replace([
104       // This should not override the table order because this header does not
105       // exist.
106       'order' => 'bar',
107     ]);
108     \Drupal::getContainer()->get('request_stack')->push($request);
109     $ts = tablesort_init($headers);
110     $expected_ts = [
111       'name' => '1',
112       'sql' => 'one',
113       'sort' => 'asc',
114       'query' => [],
115     ];
116     $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
117     $this->assertEqual($ts, $expected_ts, 'Complex table headers plus non-overriding $_GET parameters sorted correctly.');
118
119     // Test complex table headers plus $_GET parameters that _should_
120     // override the default.
121     $request = Request::createFromGlobals();
122     $request->query->replace([
123       'order' => '1',
124       'sort' => 'ASC',
125       // Add an unrelated parameter to ensure that tablesort will include
126       // it in the links that it creates.
127       'alpha' => 'beta',
128     ]);
129     \Drupal::getContainer()->get('request_stack')->push($request);
130     $expected_ts = [
131       'name' => '1',
132       'sql' => 'one',
133       'sort' => 'asc',
134       'query' => ['alpha' => 'beta'],
135     ];
136     $ts = tablesort_init($headers);
137     $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
138     $this->assertEqual($ts, $expected_ts, 'Complex table headers plus $_GET parameters sorted correctly.');
139   }
140
141 }