Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
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 240 $cell = new html_table_cell(); 241 242 // The attribute will get overwritten by the ID. 243 $cell->id = 'Jeremy'; 244 $cell->attributes['id'] = 'will get overwritten'; 245 246 // The data-name will be present in the output. 247 $cell->attributes['data-name'] = 'John'; 248 249 $row->cells[] = $cell; 250 251 $table = new html_table(); 252 $table->responsive = false; 253 // The attribute will get overwritten by the ID. 254 $table->id = 'Jeffrey'; 255 $table->attributes['id'] = 'will get overwritten'; 256 257 // The data-name will be present in the output. 258 $table->attributes['data-name'] = 'Colin'; 259 // The attribute will get overwritten by the ID above. 260 $table->data[] = $row; 261 262 // Specify a caption to be output. 263 $table->caption = "A table of meaningless data."; 264 265 $output = html_writer::table($table); 266 267 $expected = <<<EOF 268 <table class="generaltable" id="Jeffrey" data-name="Colin"> 269 <caption>A table of meaningless data.</caption><tbody><tr class="lastrow" id="Bob" data-name="Fred"> 270 <td class="cell c0 lastcol" id="Jeremy" data-name="John" style=""></td> 271 </tr> 272 </tbody> 273 </table> 274 275 EOF; 276 $this->assertSame($expected, $output); 277 } 278 279 /** 280 * @covers ::table 281 */ 282 public function test_table_hidden_caption() { 283 284 $table = new html_table(); 285 $table->id = "whodat"; 286 $table->data = array( 287 array('fred', 'MDK'), 288 array('bob', 'Burgers'), 289 array('dave', 'Competitiveness') 290 ); 291 $table->caption = "Who even knows?"; 292 $table->captionhide = true; 293 $table->responsive = false; 294 295 $output = html_writer::table($table); 296 $expected = <<<EOF 297 <table class="generaltable" id="whodat"> 298 <caption class="accesshide">Who even knows?</caption><tbody><tr class=""> 299 <td class="cell c0" style="">fred</td> 300 <td class="cell c1 lastcol" style="">MDK</td> 301 </tr> 302 <tr class=""> 303 <td class="cell c0" style="">bob</td> 304 <td class="cell c1 lastcol" style="">Burgers</td> 305 </tr> 306 <tr class="lastrow"> 307 <td class="cell c0" style="">dave</td> 308 <td class="cell c1 lastcol" style="">Competitiveness</td> 309 </tr> 310 </tbody> 311 </table> 312 313 EOF; 314 $this->assertSame($expected, $output); 315 } 316 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body