TableSortExtenderTest.php 4.7 KB

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