PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/tests/Drupal/KernelTests/Core/Render/Element/TableSortExtenderTest.php

http://github.com/drupal/drupal
PHP | 256 lines | 200 code | 18 blank | 38 comment | 0 complexity | 280332a8ec3cb94a9cf0c27573520c15 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\KernelTests\Core\Render\Element;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Core\Utility\TableSort;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Symfony\Component\HttpFoundation\Request;
  7. /**
  8. * Tests table sorting.
  9. *
  10. * @group Common
  11. */
  12. class TableSortExtenderTest extends KernelTestBase {
  13. /**
  14. * Tests \Drupal\Core\Utility\TableSort::getContextFromRequest().
  15. */
  16. public function testTableSortInit() {
  17. // Test simple table headers.
  18. $headers = ['foo', 'bar', 'baz'];
  19. // Reset $request->query to prevent parameters from Simpletest and Batch API
  20. // ending up in $ts['query'].
  21. $expected_ts = [
  22. 'name' => 'foo',
  23. 'sql' => '',
  24. 'sort' => 'asc',
  25. 'query' => [],
  26. ];
  27. $request = Request::createFromGlobals();
  28. $request->query->replace([]);
  29. \Drupal::getContainer()->get('request_stack')->push($request);
  30. $ts = TableSort::getContextFromRequest($headers, $request);
  31. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  32. $this->assertEqual($ts, $expected_ts, 'Simple table headers sorted correctly.');
  33. // Test with simple table headers plus $_GET parameters that should _not_
  34. // override the default.
  35. $request = Request::createFromGlobals();
  36. $request->query->replace([
  37. // This should not override the table order because only complex
  38. // headers are overridable.
  39. 'order' => 'bar',
  40. ]);
  41. \Drupal::getContainer()->get('request_stack')->push($request);
  42. $ts = TableSort::getContextFromRequest($headers, $request);
  43. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  44. $this->assertEqual($ts, $expected_ts, 'Simple table headers plus non-overriding $_GET parameters sorted correctly.');
  45. // Test with simple table headers plus $_GET parameters that _should_
  46. // override the default.
  47. $request = Request::createFromGlobals();
  48. $request->query->replace([
  49. 'sort' => 'DESC',
  50. // Add an unrelated parameter to ensure that tablesort will include
  51. // it in the links that it creates.
  52. 'alpha' => 'beta',
  53. ]);
  54. \Drupal::getContainer()->get('request_stack')->push($request);
  55. $expected_ts['sort'] = 'desc';
  56. $expected_ts['query'] = ['alpha' => 'beta'];
  57. $ts = TableSort::getContextFromRequest($headers, $request);
  58. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  59. $this->assertEqual($ts, $expected_ts, 'Simple table headers plus $_GET parameters sorted correctly.');
  60. // Test complex table headers.
  61. $headers = [
  62. 'foo',
  63. [
  64. 'data' => '1',
  65. 'field' => 'one',
  66. 'sort' => 'asc',
  67. 'colspan' => 1,
  68. ],
  69. [
  70. 'data' => '2',
  71. 'field' => 'two',
  72. 'sort' => 'desc',
  73. ],
  74. ];
  75. // Reset $_GET from previous assertion.
  76. $request = Request::createFromGlobals();
  77. $request->query->replace([
  78. 'order' => '2',
  79. ]);
  80. \Drupal::getContainer()->get('request_stack')->push($request);
  81. $ts = TableSort::getContextFromRequest($headers, $request);
  82. $expected_ts = [
  83. 'name' => '2',
  84. 'sql' => 'two',
  85. 'sort' => 'desc',
  86. 'query' => [],
  87. ];
  88. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  89. $this->assertEqual($ts, $expected_ts, 'Complex table headers sorted correctly.');
  90. // Test complex table headers plus $_GET parameters that should _not_
  91. // override the default.
  92. $request = Request::createFromGlobals();
  93. $request->query->replace([
  94. // This should not override the table order because this header does not
  95. // exist.
  96. 'order' => 'bar',
  97. ]);
  98. \Drupal::getContainer()->get('request_stack')->push($request);
  99. $ts = TableSort::getContextFromRequest($headers, $request);
  100. $expected_ts = [
  101. 'name' => '1',
  102. 'sql' => 'one',
  103. 'sort' => 'asc',
  104. 'query' => [],
  105. ];
  106. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  107. $this->assertEqual($ts, $expected_ts, 'Complex table headers plus non-overriding $_GET parameters sorted correctly.');
  108. // Test complex table headers plus $_GET parameters that _should_
  109. // override the default.
  110. $request = Request::createFromGlobals();
  111. $request->query->replace([
  112. 'order' => '1',
  113. 'sort' => 'ASC',
  114. // Add an unrelated parameter to ensure that tablesort will include
  115. // it in the links that it creates.
  116. 'alpha' => 'beta',
  117. ]);
  118. \Drupal::getContainer()->get('request_stack')->push($request);
  119. $expected_ts = [
  120. 'name' => '1',
  121. 'sql' => 'one',
  122. 'sort' => 'asc',
  123. 'query' => ['alpha' => 'beta'],
  124. ];
  125. $ts = TableSort::getContextFromRequest($headers, $request);
  126. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  127. $this->assertEquals($expected_ts, $ts, 'Complex table headers plus $_GET parameters sorted correctly.');
  128. // Test the initial_click_sort parameter.
  129. $headers = [
  130. 'foo',
  131. [
  132. 'data' => '1',
  133. 'field' => 'one',
  134. 'initial_click_sort' => 'desc',
  135. 'colspan' => 1,
  136. ],
  137. [
  138. 'data' => '2',
  139. 'field' => 'two',
  140. ],
  141. [
  142. 'data' => '3',
  143. 'field' => 'three',
  144. 'initial_click_sort' => 'desc',
  145. 'sort' => 'asc',
  146. ],
  147. [
  148. 'data' => '4',
  149. 'field' => 'four',
  150. 'initial_click_sort' => 'asc',
  151. ],
  152. [
  153. 'data' => '5',
  154. 'field' => 'five',
  155. 'initial_click_sort' => 'foo',
  156. ],
  157. ];
  158. $request = Request::createFromGlobals();
  159. $request->query->replace([
  160. 'order' => '1',
  161. ]);
  162. \Drupal::getContainer()->get('request_stack')->push($request);
  163. $ts = TableSort::getContextFromRequest($headers, $request);
  164. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  165. $expected_ts = [
  166. 'name' => '1',
  167. 'sql' => 'one',
  168. 'sort' => 'desc',
  169. 'query' => [],
  170. ];
  171. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  172. $this->assertEquals($expected_ts, $ts, 'Complex table headers using the initial_click_sort parameter are sorted correctly.');
  173. // Test that if the initial_click_sort parameter is not defined, the default
  174. // must be used instead (which is "asc").
  175. $request = Request::createFromGlobals();
  176. $request->query->replace([
  177. 'order' => '2',
  178. ]);
  179. \Drupal::getContainer()->get('request_stack')->push($request);
  180. $ts = TableSort::getContextFromRequest($headers, $request);
  181. $expected_ts = [
  182. 'name' => '2',
  183. 'sql' => 'two',
  184. 'sort' => 'asc',
  185. 'query' => [],
  186. ];
  187. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  188. $this->assertEquals($expected_ts, $ts, 'Complex table headers without using the initial_click_sort parameter are sorted correctly.');
  189. // Test that if the initial_click_sort parameter is defined, and the sort
  190. // parameter is defined as well, the sort parameter has precedence.
  191. $request = Request::createFromGlobals();
  192. $request->query->replace([
  193. 'order' => '3',
  194. ]);
  195. \Drupal::getContainer()->get('request_stack')->push($request);
  196. $ts = TableSort::getContextFromRequest($headers, $request);
  197. $expected_ts = [
  198. 'name' => '3',
  199. 'sql' => 'three',
  200. 'sort' => 'asc',
  201. 'query' => [],
  202. ];
  203. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  204. $this->assertEquals($expected_ts, $ts, 'Complex table headers using the initial_click_sort and sort parameters are sorted correctly.');
  205. // Test that if the initial_click_sort parameter is defined and the value
  206. // is "asc" it should be sorted correctly.
  207. $request = Request::createFromGlobals();
  208. $request->query->replace([
  209. 'order' => '4',
  210. ]);
  211. \Drupal::getContainer()->get('request_stack')->push($request);
  212. $ts = TableSort::getContextFromRequest($headers, $request);
  213. $expected_ts = [
  214. 'name' => '4',
  215. 'sql' => 'four',
  216. 'sort' => 'asc',
  217. 'query' => [],
  218. ];
  219. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  220. $this->assertEquals($expected_ts, $ts, 'Complex table headers with the initial_click_sort set as ASC are sorted correctly.');
  221. // Tests that if the initial_click_sort is defined with a non expected value
  222. // that value will be passed as the "sort" value.
  223. $request = Request::createFromGlobals();
  224. $request->query->replace([
  225. 'order' => '5',
  226. ]);
  227. \Drupal::getContainer()->get('request_stack')->push($request);
  228. $ts = TableSort::getContextFromRequest($headers, $request);
  229. $expected_ts = [
  230. 'name' => '5',
  231. 'sql' => 'five',
  232. 'sort' => 'foo',
  233. 'query' => [],
  234. ];
  235. $this->verbose(strtr('$ts: <pre>!ts</pre>', ['!ts' => Html::escape(var_export($ts, TRUE))]));
  236. $this->assertEquals($expected_ts, $ts, 'Complex table headers with the initial_click_sort set as foo are sorted correctly.');
  237. }
  238. }