Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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   * Unit tests for the html_writer class.
  19   *
  20   * @package   core
  21   * @category  phpunit
  22   * @copyright 2010 Tim Hunt
  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 . '/outputcomponents.php');
  30  
  31  /**
  32   * Unit tests for the html_writer class.
  33   *
  34   * @copyright 2010 Tim Hunt
  35   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @covers \html_writer
  37   * @coversDefaultClass \html_writer
  38   */
  39  class html_writer_test extends basic_testcase {
  40  
  41      /**
  42       * @covers ::start_tag
  43       */
  44      public function test_start_tag() {
  45          $this->assertSame('<div>', html_writer::start_tag('div'));
  46      }
  47  
  48      /**
  49       * @covers ::start_tag
  50       */
  51      public function test_start_tag_with_attr() {
  52          $this->assertSame('<div class="frog">',
  53              html_writer::start_tag('div', array('class' => 'frog')));
  54      }
  55  
  56      /**
  57       * @covers ::start_tag
  58       */
  59      public function test_start_tag_with_attrs() {
  60          $this->assertSame('<div class="frog" id="mydiv">',
  61              html_writer::start_tag('div', array('class' => 'frog', 'id' => 'mydiv')));
  62      }
  63  
  64      /**
  65       * @covers ::end_tag
  66       */
  67      public function test_end_tag() {
  68          $this->assertSame('</div>', html_writer::end_tag('div'));
  69      }
  70  
  71      /**
  72       * @covers ::empty_Tag
  73       */
  74      public function test_empty_tag() {
  75          $this->assertSame('<br />', html_writer::empty_tag('br'));
  76      }
  77  
  78      /**
  79       * @covers ::empty_Tag
  80       */
  81      public function test_empty_tag_with_attrs() {
  82          $this->assertSame('<input type="submit" value="frog" />',
  83              html_writer::empty_tag('input', array('type' => 'submit', 'value' => 'frog')));
  84      }
  85  
  86      /**
  87       * @covers ::nonempty_tag
  88       */
  89      public function test_nonempty_tag_with_content() {
  90          $this->assertSame('<div>Hello world!</div>',
  91              html_writer::nonempty_tag('div', 'Hello world!'));
  92      }
  93  
  94      /**
  95       * @covers ::nonempty_tag
  96       */
  97      public function test_nonempty_tag_empty() {
  98          $this->assertSame('',
  99              html_writer::nonempty_tag('div', ''));
 100      }
 101  
 102      /**
 103       * @covers ::nonempty_tag
 104       */
 105      public function test_nonempty_tag_null() {
 106          $this->assertSame('',
 107              html_writer::nonempty_tag('div', null));
 108      }
 109  
 110      /**
 111       * @covers ::nonempty_tag
 112       */
 113      public function test_nonempty_tag_zero() {
 114          $this->assertSame('<div class="score">0</div>',
 115              html_writer::nonempty_tag('div', 0, array('class' => 'score')));
 116      }
 117  
 118      /**
 119       * @covers ::nonempty_tag
 120       */
 121      public function test_nonempty_tag_zero_string() {
 122          $this->assertSame('<div class="score">0</div>',
 123              html_writer::nonempty_tag('div', '0', array('class' => 'score')));
 124      }
 125  
 126      /**
 127       * @covers ::div
 128       */
 129      public function test_div() {
 130          // All options.
 131          $this->assertSame('<div class="frog" id="kermit">ribbit</div>',
 132                  html_writer::div('ribbit', 'frog', array('id' => 'kermit')));
 133          // Combine class from attributes and $class.
 134          $this->assertSame('<div class="amphibian frog">ribbit</div>',
 135                  html_writer::div('ribbit', 'frog', array('class' => 'amphibian')));
 136          // Class only.
 137          $this->assertSame('<div class="frog">ribbit</div>',
 138                  html_writer::div('ribbit', 'frog'));
 139          // Attributes only.
 140          $this->assertSame('<div id="kermit">ribbit</div>',
 141                  html_writer::div('ribbit', '', array('id' => 'kermit')));
 142          // No options.
 143          $this->assertSame('<div>ribbit</div>',
 144                  html_writer::div('ribbit'));
 145      }
 146  
 147      /**
 148       * @covers ::start_div
 149       */
 150      public function test_start_div() {
 151          // All options.
 152          $this->assertSame('<div class="frog" id="kermit">',
 153                  html_writer::start_div('frog', array('id' => 'kermit')));
 154          // Combine class from attributes and $class.
 155          $this->assertSame('<div class="amphibian frog">',
 156                  html_writer::start_div('frog', array('class' => 'amphibian')));
 157          // Class only.
 158          $this->assertSame('<div class="frog">',
 159                  html_writer::start_div('frog'));
 160          // Attributes only.
 161          $this->assertSame('<div id="kermit">',
 162                  html_writer::start_div('', array('id' => 'kermit')));
 163          // No options.
 164          $this->assertSame('<div>',
 165                  html_writer::start_div());
 166      }
 167  
 168      /**
 169       * @covers ::end_div
 170       */
 171      public function test_end_div() {
 172          $this->assertSame('</div>', html_writer::end_div());
 173      }
 174  
 175      /**
 176       * @covers ::span
 177       */
 178      public function test_span() {
 179          // All options.
 180          $this->assertSame('<span class="frog" id="kermit">ribbit</span>',
 181                  html_writer::span('ribbit', 'frog', array('id' => 'kermit')));
 182          // Combine class from attributes and $class.
 183          $this->assertSame('<span class="amphibian frog">ribbit</span>',
 184                  html_writer::span('ribbit', 'frog', array('class' => 'amphibian')));
 185          // Class only.
 186          $this->assertSame('<span class="frog">ribbit</span>',
 187                  html_writer::span('ribbit', 'frog'));
 188          // Attributes only.
 189          $this->assertSame('<span id="kermit">ribbit</span>',
 190                  html_writer::span('ribbit', '', array('id' => 'kermit')));
 191          // No options.
 192          $this->assertSame('<span>ribbit</span>',
 193                  html_writer::span('ribbit'));
 194      }
 195  
 196      /**
 197       * @covers ::start_span
 198       */
 199      public function test_start_span() {
 200          // All options.
 201          $this->assertSame('<span class="frog" id="kermit">',
 202                  html_writer::start_span('frog', array('id' => 'kermit')));
 203          // Combine class from attributes and $class.
 204          $this->assertSame('<span class="amphibian frog">',
 205                  html_writer::start_span('frog', array('class' => 'amphibian')));
 206          // Class only.
 207          $this->assertSame('<span class="frog">',
 208                  html_writer::start_span('frog'));
 209          // Attributes only.
 210          $this->assertSame('<span id="kermit">',
 211                  html_writer::start_span('', array('id' => 'kermit')));
 212          // No options.
 213          $this->assertSame('<span>',
 214                  html_writer::start_span());
 215      }
 216  
 217      /**
 218       * @covers ::end_span
 219       */
 220      public function test_end_span() {
 221          $this->assertSame('</span>', html_writer::end_span());
 222      }
 223  
 224      /**
 225       * @covers ::table
 226       * @covers \html_table_row
 227       * @covers \html_table_cell
 228       * @covers \html_table
 229       */
 230      public function test_table() {
 231          $row = new html_table_row();
 232  
 233          // The attribute will get overwritten by the ID.
 234          $row->id = 'Bob';
 235          $row->attributes['id'] = 'will get overwritten';
 236  
 237          // The data-name will be present in the output.
 238          $row->attributes['data-name'] = 'Fred';
 239          $row->class = 'this is a table row';
 240  
 241          $cell = new html_table_cell();
 242  
 243          // The attribute will get overwritten by the ID.
 244          $cell->id = 'Jeremy';
 245          $cell->attributes['id'] = 'will get overwritten';
 246  
 247          // The data-name will be present in the output.
 248          $cell->attributes['data-name'] = 'John';
 249          $cell->class = 'this is a table cell';
 250  
 251          $row->cells[] = $cell;
 252  
 253          $table = new html_table();
 254          $table->responsive = false;
 255          // The attribute will get overwritten by the ID.
 256          $table->id = 'Jeffrey';
 257          $table->attributes['id'] = 'will get overwritten';
 258  
 259          // The data-name will be present in the output.
 260          $table->attributes['data-name'] = 'Colin';
 261          // The attribute will get overwritten by the ID above.
 262          $table->data[] = $row;
 263  
 264          // Specify a caption to be output.
 265          $table->caption = "A table of meaningless data.";
 266  
 267          $output = html_writer::table($table);
 268  
 269          $expected = <<<EOF
 270  <table class="generaltable" id="Jeffrey" data-name="Colin">
 271  <caption>A table of meaningless data.</caption><tbody><tr class="lastrow" id="Bob" data-name="Fred">
 272  <td class="cell c0 lastcol" id="Jeremy" data-name="John" style=""></td>
 273  </tr>
 274  </tbody>
 275  </table>
 276  
 277  EOF;
 278          $this->assertSame($expected, $output);
 279      }
 280  
 281      /**
 282       * @covers ::table
 283       */
 284      public function test_table_hidden_caption() {
 285  
 286          $table = new html_table();
 287          $table->id = "whodat";
 288          $table->data = array(
 289              array('fred', 'MDK'),
 290              array('bob',  'Burgers'),
 291              array('dave', 'Competitiveness')
 292          );
 293          $table->caption = "Who even knows?";
 294          $table->captionhide = true;
 295          $table->responsive = false;
 296  
 297          $output = html_writer::table($table);
 298          $expected = <<<EOF
 299  <table class="generaltable" id="whodat">
 300  <caption class="accesshide">Who even knows?</caption><tbody><tr class="">
 301  <td class="cell c0" style="">fred</td>
 302  <td class="cell c1 lastcol" style="">MDK</td>
 303  </tr>
 304  <tr class="">
 305  <td class="cell c0" style="">bob</td>
 306  <td class="cell c1 lastcol" style="">Burgers</td>
 307  </tr>
 308  <tr class="lastrow">
 309  <td class="cell c0" style="">dave</td>
 310  <td class="cell c1 lastcol" style="">Competitiveness</td>
 311  </tr>
 312  </tbody>
 313  </table>
 314  
 315  EOF;
 316          $this->assertSame($expected, $output);
 317      }
 318  }