MDL-49294 logging: Improve cleanup tests
[moodle.git] / lib / tests / tablelib_test.php
blobdba5b3ae9bfd4bde235b69830e9d70dabfc0d5ee
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Test tablelib.
20 * @package core
21 * @category phpunit
22 * @copyright 2013 Damyon Wiese <damyon@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/tablelib.php');
31 /**
32 * Test some of tablelib.
34 * @package core
35 * @category phpunit
36 * @copyright 2013 Damyon Wiese <damyon@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_tablelib_testcase extends basic_testcase {
40 protected function generate_columns($cols) {
41 $columns = array();
42 foreach (range(0, $cols - 1) as $j) {
43 array_push($columns, 'column' . $j);
45 return $columns;
48 protected function generate_headers($cols) {
49 $columns = array();
50 foreach (range(0, $cols - 1) as $j) {
51 array_push($columns, 'Column ' . $j);
53 return $columns;
56 protected function generate_data($rows, $cols) {
57 $data = array();
59 foreach (range(0, $rows - 1) as $i) {
60 $row = array();
61 foreach (range(0, $cols - 1) as $j) {
62 $val = 'row ' . $i . ' col ' . $j;
63 $row['column' . $j] = $val;
65 array_push($data, $row);
67 return $data;
70 /**
71 * Create a table with properties as passed in params, add data and output html.
73 * @param string[] $columns
74 * @param string[] $headers
75 * @param bool $sortable
76 * @param bool $collapsible
77 * @param string[] $suppress
78 * @param string[] $nosorting
79 * @param (array|object)[] $data
80 * @param int $pagesize
82 protected function run_table_test($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, $data, $pagesize) {
83 $table = $this->create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting);
84 $table->pagesize($pagesize, count($data));
85 foreach ($data as $row) {
86 $table->add_data_keyed($row);
88 $table->finish_output();
91 /**
92 * Create a table with properties as passed in params.
94 * @param string[] $columns
95 * @param string[] $headers
96 * @param bool $sortable
97 * @param bool $collapsible
98 * @param string[] $suppress
99 * @param string[] $nosorting
100 * @return flexible_table
102 protected function create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting) {
103 $table = new flexible_table('tablelib_test');
105 $table->define_columns($columns);
106 $table->define_headers($headers);
107 $table->define_baseurl('/invalid.php');
109 $table->sortable($sortable);
110 $table->collapsible($collapsible);
111 foreach ($suppress as $column) {
112 $table->column_suppress($column);
115 foreach ($nosorting as $column) {
116 $table->no_sorting($column);
119 $table->setup();
120 return $table;
123 public function test_empty_table() {
124 $this->expectOutputRegex('/' . get_string('nothingtodisplay') . '/');
125 $this->run_table_test(
126 array('column1', 'column2'), // Columns.
127 array('Column 1', 'Column 2'), // Headers.
128 true, // Sortable.
129 false, // Collapsible.
130 array(), // Suppress columns.
131 array(), // No sorting.
132 array(), // Data.
133 10 // Page size.
137 public function test_has_next_pagination() {
139 $data = $this->generate_data(11, 2);
140 $columns = $this->generate_columns(2);
141 $headers = $this->generate_headers(2);
143 // Search for pagination controls containing '1.*2</a>.*Next</a>'.
144 $this->expectOutputRegex('/1.*2<\/a>.*' . get_string('next') . '<\/a>/');
146 $this->run_table_test(
147 $columns,
148 $headers,
149 true,
150 false,
151 array(),
152 array(),
153 $data,
158 public function test_has_hide() {
160 $data = $this->generate_data(11, 2);
161 $columns = $this->generate_columns(2);
162 $headers = $this->generate_headers(2);
164 // Search for 'hide' links in the column headers.
165 $this->expectOutputRegex('/' . get_string('hide') . '/');
167 $this->run_table_test(
168 $columns,
169 $headers,
170 true,
171 true,
172 array(),
173 array(),
174 $data,
179 public function test_has_not_hide() {
181 $data = $this->generate_data(11, 2);
182 $columns = $this->generate_columns(2);
183 $headers = $this->generate_headers(2);
185 // Make sure there are no 'hide' links in the headers.
187 ob_start();
188 $this->run_table_test(
189 $columns,
190 $headers,
191 true,
192 false,
193 array(),
194 array(),
195 $data,
198 $output = ob_get_contents();
199 ob_end_clean();
200 $this->assertNotContains(get_string('hide'), $output);
203 public function test_has_sort() {
205 $data = $this->generate_data(11, 2);
206 $columns = $this->generate_columns(2);
207 $headers = $this->generate_headers(2);
209 // Search for pagination controls containing '1.*2</a>.*Next</a>'.
210 $this->expectOutputRegex('/' . get_string('sortby') . '/');
212 $this->run_table_test(
213 $columns,
214 $headers,
215 true,
216 false,
217 array(),
218 array(),
219 $data,
224 public function test_has_not_sort() {
226 $data = $this->generate_data(11, 2);
227 $columns = $this->generate_columns(2);
228 $headers = $this->generate_headers(2);
230 // Make sure there are no 'Sort by' links in the headers.
232 ob_start();
233 $this->run_table_test(
234 $columns,
235 $headers,
236 false,
237 false,
238 array(),
239 array(),
240 $data,
243 $output = ob_get_contents();
244 ob_end_clean();
245 $this->assertNotContains(get_string('sortby'), $output);
248 public function test_has_not_next_pagination() {
250 $data = $this->generate_data(10, 2);
251 $columns = $this->generate_columns(2);
252 $headers = $this->generate_headers(2);
254 // Make sure there are no 'Next' links in the pagination.
256 ob_start();
257 $this->run_table_test(
258 $columns,
259 $headers,
260 true,
261 false,
262 array(),
263 array(),
264 $data,
268 $output = ob_get_contents();
269 ob_end_clean();
270 $this->assertNotContains(get_string('next'), $output);
273 public function test_1_col() {
275 $data = $this->generate_data(100, 1);
276 $columns = $this->generate_columns(1);
277 $headers = $this->generate_headers(1);
279 $this->expectOutputRegex('/row 0 col 0/');
281 $this->run_table_test(
282 $columns,
283 $headers,
284 true,
285 false,
286 array(),
287 array(),
288 $data,
293 public function test_empty_rows() {
295 $data = $this->generate_data(1, 5);
296 $columns = $this->generate_columns(5);
297 $headers = $this->generate_headers(5);
299 // Test that we have at least 5 columns generated for each empty row.
300 $this->expectOutputRegex('/emptyrow.*r9_c4/');
302 $this->run_table_test(
303 $columns,
304 $headers,
305 true,
306 false,
307 array(),
308 array(),
309 $data,
314 public function test_5_cols() {
316 $data = $this->generate_data(100, 5);
317 $columns = $this->generate_columns(5);
318 $headers = $this->generate_headers(5);
320 $this->expectOutputRegex('/row 0 col 0/');
322 $this->run_table_test(
323 $columns,
324 $headers,
325 true,
326 false,
327 array(),
328 array(),
329 $data,
334 public function test_50_cols() {
336 $data = $this->generate_data(100, 50);
337 $columns = $this->generate_columns(50);
338 $headers = $this->generate_headers(50);
340 $this->expectOutputRegex('/row 0 col 0/');
342 $this->run_table_test(
343 $columns,
344 $headers,
345 true,
346 false,
347 array(),
348 array(),
349 $data,
354 public function test_get_row_html() {
355 $data = $this->generate_data(1, 5);
356 $columns = $this->generate_columns(5);
357 $headers = $this->generate_headers(5);
358 $data = array_keys(array_flip($data[0]));
360 $table = new flexible_table('tablelib_test');
361 $table->define_columns($columns);
362 $table->define_headers($headers);
363 $table->define_baseurl('/invalid.php');
364 $row = $table->get_row_html($data);
365 $this->assertRegExp('/row 0 col 0/', $row);