tablesort.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * Various tablesort tests.
  5. */
  6. /**
  7. * Test unicode handling features implemented in unicode.inc.
  8. */
  9. class TableSortTest extends DrupalUnitTestCase {
  10. /**
  11. * Storage for initial value of $_GET.
  12. *
  13. * @var array
  14. */
  15. protected $GET = array();
  16. public static function getInfo() {
  17. return array(
  18. 'name' => 'Tablesort',
  19. 'description' => 'Tests table sorting.',
  20. 'group' => 'System',
  21. );
  22. }
  23. function setUp() {
  24. // Save the original $_GET to be restored later.
  25. $this->GET = $_GET;
  26. parent::setUp();
  27. }
  28. function tearDown() {
  29. // Revert $_GET.
  30. $_GET = $this->GET;
  31. parent::tearDown();
  32. }
  33. /**
  34. * Test tablesort_init().
  35. */
  36. function testTableSortInit() {
  37. // Test simple table headers.
  38. $headers = array('foo', 'bar', 'baz');
  39. // Reset $_GET to prevent parameters from Simpletest and Batch API ending
  40. // up in $ts['query'].
  41. $_GET = array('q' => 'jahwohl');
  42. $expected_ts = array(
  43. 'name' => 'foo',
  44. 'sql' => '',
  45. 'sort' => 'asc',
  46. 'query' => array(),
  47. );
  48. $ts = tablesort_init($headers);
  49. $this->verbose(strtr('$ts: <pre>!ts</pre>', array('!ts' => check_plain(var_export($ts, TRUE)))));
  50. $this->assertEqual($ts, $expected_ts, t('Simple table headers sorted correctly.'));
  51. // Test with simple table headers plus $_GET parameters that should _not_
  52. // override the default.
  53. $_GET = array(
  54. 'q' => 'jahwohl',
  55. // This should not override the table order because only complex
  56. // headers are overridable.
  57. 'order' => 'bar',
  58. );
  59. $ts = tablesort_init($headers);
  60. $this->verbose(strtr('$ts: <pre>!ts</pre>', array('!ts' => check_plain(var_export($ts, TRUE)))));
  61. $this->assertEqual($ts, $expected_ts, t('Simple table headers plus non-overriding $_GET parameters sorted correctly.'));
  62. // Test with simple table headers plus $_GET parameters that _should_
  63. // override the default.
  64. $_GET = array(
  65. 'q' => 'jahwohl',
  66. 'sort' => 'DESC',
  67. // Add an unrelated parameter to ensure that tablesort will include
  68. // it in the links that it creates.
  69. 'alpha' => 'beta',
  70. );
  71. $expected_ts['sort'] = 'desc';
  72. $expected_ts['query'] = array('alpha' => 'beta');
  73. $ts = tablesort_init($headers);
  74. $this->verbose(strtr('$ts: <pre>!ts</pre>', array('!ts' => check_plain(var_export($ts, TRUE)))));
  75. $this->assertEqual($ts, $expected_ts, t('Simple table headers plus $_GET parameters sorted correctly.'));
  76. // Test complex table headers.
  77. $headers = array(
  78. 'foo',
  79. array(
  80. 'data' => '1',
  81. 'field' => 'one',
  82. 'sort' => 'asc',
  83. 'colspan' => 1,
  84. ),
  85. array(
  86. 'data' => '2',
  87. 'field' => 'two',
  88. 'sort' => 'desc',
  89. ),
  90. );
  91. // Reset $_GET from previous assertion.
  92. $_GET = array(
  93. 'q' => 'jahwohl',
  94. 'order' => '2',
  95. );
  96. $ts = tablesort_init($headers);
  97. $expected_ts = array(
  98. 'name' => '2',
  99. 'sql' => 'two',
  100. 'sort' => 'desc',
  101. 'query' => array(),
  102. );
  103. $this->verbose(strtr('$ts: <pre>!ts</pre>', array('!ts' => check_plain(var_export($ts, TRUE)))));
  104. $this->assertEqual($ts, $expected_ts, t('Complex table headers sorted correctly.'));
  105. // Test complex table headers plus $_GET parameters that should _not_
  106. // override the default.
  107. $_GET = array(
  108. 'q' => 'jahwohl',
  109. // This should not override the table order because this header does not
  110. // exist.
  111. 'order' => 'bar',
  112. );
  113. $ts = tablesort_init($headers);
  114. $expected_ts = array(
  115. 'name' => '1',
  116. 'sql' => 'one',
  117. 'sort' => 'asc',
  118. 'query' => array(),
  119. );
  120. $this->verbose(strtr('$ts: <pre>!ts</pre>', array('!ts' => check_plain(var_export($ts, TRUE)))));
  121. $this->assertEqual($ts, $expected_ts, t('Complex table headers plus non-overriding $_GET parameters sorted correctly.'));
  122. unset($_GET['sort'], $_GET['order'], $_GET['alpha']);
  123. // Test complex table headers plus $_GET parameters that _should_
  124. // override the default.
  125. $_GET = array(
  126. 'q' => 'jahwohl',
  127. 'order' => '1',
  128. 'sort' => 'ASC',
  129. // Add an unrelated parameter to ensure that tablesort will include
  130. // it in the links that it creates.
  131. 'alpha' => 'beta',
  132. );
  133. $expected_ts = array(
  134. 'name' => '1',
  135. 'sql' => 'one',
  136. 'sort' => 'asc',
  137. 'query' => array('alpha' => 'beta'),
  138. );
  139. $ts = tablesort_init($headers);
  140. $this->verbose(strtr('$ts: <pre>!ts</pre>', array('!ts' => check_plain(var_export($ts, TRUE)))));
  141. $this->assertEqual($ts, $expected_ts, t('Complex table headers plus $_GET parameters sorted correctly.'));
  142. unset($_GET['sort'], $_GET['order'], $_GET['alpha']);
  143. }
  144. }