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