[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/tests/ -> tablelib_test.php (source)

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Test tablelib.
  19   *
  20   * @package    core
  21   * @category   phpunit
  22   * @copyright  2013 Damyon Wiese <[email protected]>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->libdir . '/tablelib.php');
  30  
  31  /**
  32   * Test some of tablelib.
  33   *
  34   * @package    core
  35   * @category   phpunit
  36   * @copyright  2013 Damyon Wiese <[email protected]>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  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);
  44          }
  45          return $columns;
  46      }
  47  
  48      protected function generate_headers($cols) {
  49          $columns = array();
  50          foreach (range(0, $cols - 1) as $j) {
  51              array_push($columns, 'Column ' . $j);
  52          }
  53          return $columns;
  54      }
  55  
  56      protected function generate_data($rows, $cols) {
  57          $data = array();
  58  
  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;
  64              }
  65              array_push($data, $row);
  66          }
  67          return $data;
  68      }
  69  
  70      /**
  71       * Create a table with properties as passed in params, add data and output html.
  72       *
  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
  81       */
  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);
  87          }
  88          $table->finish_output();
  89      }
  90  
  91      /**
  92       * Create a table with properties as passed in params.
  93       *
  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
 101       */
 102      protected function create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting) {
 103          $table = new flexible_table('tablelib_test');
 104  
 105          $table->define_columns($columns);
 106          $table->define_headers($headers);
 107          $table->define_baseurl('/invalid.php');
 108  
 109          $table->sortable($sortable);
 110          $table->collapsible($collapsible);
 111          foreach ($suppress as $column) {
 112              $table->column_suppress($column);
 113          }
 114  
 115          foreach ($nosorting as $column) {
 116              $table->no_sorting($column);
 117          }
 118  
 119          $table->setup();
 120          return $table;
 121      }
 122  
 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.
 134          );
 135      }
 136  
 137      public function test_has_next_pagination() {
 138  
 139          $data = $this->generate_data(11, 2);
 140          $columns = $this->generate_columns(2);
 141          $headers = $this->generate_headers(2);
 142  
 143          // Search for pagination controls containing '1.*2</a>.*Next</a>'.
 144          $this->expectOutputRegex('/1.*2<\/a>.*' . get_string('next') . '<\/a>/');
 145  
 146          $this->run_table_test(
 147              $columns,
 148              $headers,
 149              true,
 150              false,
 151              array(),
 152              array(),
 153              $data,
 154              10
 155          );
 156      }
 157  
 158      public function test_has_hide() {
 159  
 160          $data = $this->generate_data(11, 2);
 161          $columns = $this->generate_columns(2);
 162          $headers = $this->generate_headers(2);
 163  
 164          // Search for 'hide' links in the column headers.
 165          $this->expectOutputRegex('/' . get_string('hide') . '/');
 166  
 167          $this->run_table_test(
 168              $columns,
 169              $headers,
 170              true,
 171              true,
 172              array(),
 173              array(),
 174              $data,
 175              10
 176          );
 177      }
 178  
 179      public function test_has_not_hide() {
 180  
 181          $data = $this->generate_data(11, 2);
 182          $columns = $this->generate_columns(2);
 183          $headers = $this->generate_headers(2);
 184  
 185          // Make sure there are no 'hide' links in the headers.
 186  
 187          ob_start();
 188          $this->run_table_test(
 189              $columns,
 190              $headers,
 191              true,
 192              false,
 193              array(),
 194              array(),
 195              $data,
 196              10
 197          );
 198          $output = ob_get_contents();
 199          ob_end_clean();
 200          $this->assertNotContains(get_string('hide'), $output);
 201      }
 202  
 203      public function test_has_sort() {
 204  
 205          $data = $this->generate_data(11, 2);
 206          $columns = $this->generate_columns(2);
 207          $headers = $this->generate_headers(2);
 208  
 209          // Search for pagination controls containing '1.*2</a>.*Next</a>'.
 210          $this->expectOutputRegex('/' . get_string('sortby') . '/');
 211  
 212          $this->run_table_test(
 213              $columns,
 214              $headers,
 215              true,
 216              false,
 217              array(),
 218              array(),
 219              $data,
 220              10
 221          );
 222      }
 223  
 224      public function test_has_not_sort() {
 225  
 226          $data = $this->generate_data(11, 2);
 227          $columns = $this->generate_columns(2);
 228          $headers = $this->generate_headers(2);
 229  
 230          // Make sure there are no 'Sort by' links in the headers.
 231  
 232          ob_start();
 233          $this->run_table_test(
 234              $columns,
 235              $headers,
 236              false,
 237              false,
 238              array(),
 239              array(),
 240              $data,
 241              10
 242          );
 243          $output = ob_get_contents();
 244          ob_end_clean();
 245          $this->assertNotContains(get_string('sortby'), $output);
 246      }
 247  
 248      public function test_has_not_next_pagination() {
 249  
 250          $data = $this->generate_data(10, 2);
 251          $columns = $this->generate_columns(2);
 252          $headers = $this->generate_headers(2);
 253  
 254          // Make sure there are no 'Next' links in the pagination.
 255  
 256          ob_start();
 257          $this->run_table_test(
 258              $columns,
 259              $headers,
 260              true,
 261              false,
 262              array(),
 263              array(),
 264              $data,
 265              10
 266          );
 267  
 268          $output = ob_get_contents();
 269          ob_end_clean();
 270          $this->assertNotContains(get_string('next'), $output);
 271      }
 272  
 273      public function test_1_col() {
 274  
 275          $data = $this->generate_data(100, 1);
 276          $columns = $this->generate_columns(1);
 277          $headers = $this->generate_headers(1);
 278  
 279          $this->expectOutputRegex('/row 0 col 0/');
 280  
 281          $this->run_table_test(
 282              $columns,
 283              $headers,
 284              true,
 285              false,
 286              array(),
 287              array(),
 288              $data,
 289              10
 290          );
 291      }
 292  
 293      public function test_empty_rows() {
 294  
 295          $data = $this->generate_data(1, 5);
 296          $columns = $this->generate_columns(5);
 297          $headers = $this->generate_headers(5);
 298  
 299          // Test that we have at least 5 columns generated for each empty row.
 300          $this->expectOutputRegex('/emptyrow.*r9_c4/');
 301  
 302          $this->run_table_test(
 303              $columns,
 304              $headers,
 305              true,
 306              false,
 307              array(),
 308              array(),
 309              $data,
 310              10
 311          );
 312      }
 313  
 314      public function test_5_cols() {
 315  
 316          $data = $this->generate_data(100, 5);
 317          $columns = $this->generate_columns(5);
 318          $headers = $this->generate_headers(5);
 319  
 320          $this->expectOutputRegex('/row 0 col 0/');
 321  
 322          $this->run_table_test(
 323              $columns,
 324              $headers,
 325              true,
 326              false,
 327              array(),
 328              array(),
 329              $data,
 330              10
 331          );
 332      }
 333  
 334      public function test_50_cols() {
 335  
 336          $data = $this->generate_data(100, 50);
 337          $columns = $this->generate_columns(50);
 338          $headers = $this->generate_headers(50);
 339  
 340          $this->expectOutputRegex('/row 0 col 0/');
 341  
 342          $this->run_table_test(
 343              $columns,
 344              $headers,
 345              true,
 346              false,
 347              array(),
 348              array(),
 349              $data,
 350              10
 351          );
 352      }
 353  
 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]));
 359  
 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);
 366      }
 367  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1