3 namespace Drupal\KernelTests\Core\Render\Element;
5 use Drupal\Component\Utility\Html;
6 use Drupal\KernelTests\KernelTestBase;
7 use Symfony\Component\HttpFoundation\Request;
10 * Tests table sorting.
14 class TableSortExtenderTest extends KernelTestBase {
17 * Tests tablesort_init().
19 public function testTableSortInit() {
21 // Test simple table headers.
23 $headers = ['foo', 'bar', 'baz'];
24 // Reset $request->query to prevent parameters from Simpletest and Batch API
25 // ending up in $ts['query'].
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.');
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.
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.');
52 // Test with simple table headers plus $_GET parameters that _should_
53 // override the default.
54 $request = Request::createFromGlobals();
55 $request->query->replace([
57 // Add an unrelated parameter to ensure that tablesort will include
58 // it in the links that it creates.
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.');
68 // Test complex table headers.
84 // Reset $_GET from previous assertion.
85 $request = Request::createFromGlobals();
86 $request->query->replace([
89 \Drupal::getContainer()->get('request_stack')->push($request);
90 $ts = tablesort_init($headers);
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.');
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
108 \Drupal::getContainer()->get('request_stack')->push($request);
109 $ts = tablesort_init($headers);
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.');
119 // Test complex table headers plus $_GET parameters that _should_
120 // override the default.
121 $request = Request::createFromGlobals();
122 $request->query->replace([
125 // Add an unrelated parameter to ensure that tablesort will include
126 // it in the links that it creates.
129 \Drupal::getContainer()->get('request_stack')->push($request);
134 'query' => ['alpha' => 'beta'],
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.');