Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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