2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
29 require_once($CFG->libdir
. '/tablelib.php');
32 * Test some of tablelib.
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) {
42 foreach (range(0, $cols - 1) as $j) {
43 array_push($columns, 'column' . $j);
48 protected function generate_headers($cols) {
50 foreach (range(0, $cols - 1) as $j) {
51 array_push($columns, 'Column ' . $j);
56 protected function generate_data($rows, $cols) {
59 foreach (range(0, $rows - 1) as $i) {
61 foreach (range(0, $cols - 1) as $j) {
62 $val = 'row ' . $i . ' col ' . $j;
63 $row['column' . $j] = $val;
65 array_push($data, $row);
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();
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);
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.
129 false, // Collapsible.
130 array(), // Suppress columns.
131 array(), // No sorting.
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(
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(
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.
188 $this->run_table_test(
198 $output = ob_get_contents();
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(
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.
233 $this->run_table_test(
243 $output = ob_get_contents();
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.
257 $this->run_table_test(
268 $output = ob_get_contents();
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(
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(
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(
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(
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);