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/>.
18 * Unit tests for the html_writer class.
22 * @copyright 2010 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
. '/outputcomponents.php');
32 * Unit tests for the html_writer class.
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
39 class html_writer_test
extends basic_testcase
{
44 public function test_start_tag() {
45 $this->assertSame('<div>', html_writer
::start_tag('div'));
51 public function test_start_tag_with_attr() {
52 $this->assertSame('<div class="frog">',
53 html_writer
::start_tag('div', array('class' => 'frog')));
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')));
67 public function test_end_tag() {
68 $this->assertSame('</div>', html_writer
::end_tag('div'));
74 public function test_empty_tag() {
75 $this->assertSame('<br />', html_writer
::empty_tag('br'));
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')));
87 * @covers ::nonempty_tag
89 public function test_nonempty_tag_with_content() {
90 $this->assertSame('<div>Hello world!</div>',
91 html_writer
::nonempty_tag('div', 'Hello world!'));
95 * @covers ::nonempty_tag
97 public function test_nonempty_tag_empty() {
99 html_writer
::nonempty_tag('div', ''));
103 * @covers ::nonempty_tag
105 public function test_nonempty_tag_null() {
106 $this->assertSame('',
107 html_writer
::nonempty_tag('div', null));
111 * @covers ::nonempty_tag
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')));
119 * @covers ::nonempty_tag
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')));
129 public function test_div() {
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')));
137 $this->assertSame('<div class="frog">ribbit</div>',
138 html_writer
::div('ribbit', 'frog'));
140 $this->assertSame('<div id="kermit">ribbit</div>',
141 html_writer
::div('ribbit', '', array('id' => 'kermit')));
143 $this->assertSame('<div>ribbit</div>',
144 html_writer
::div('ribbit'));
148 * @covers ::start_div
150 public function test_start_div() {
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')));
158 $this->assertSame('<div class="frog">',
159 html_writer
::start_div('frog'));
161 $this->assertSame('<div id="kermit">',
162 html_writer
::start_div('', array('id' => 'kermit')));
164 $this->assertSame('<div>',
165 html_writer
::start_div());
171 public function test_end_div() {
172 $this->assertSame('</div>', html_writer
::end_div());
178 public function test_span() {
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')));
186 $this->assertSame('<span class="frog">ribbit</span>',
187 html_writer
::span('ribbit', 'frog'));
189 $this->assertSame('<span id="kermit">ribbit</span>',
190 html_writer
::span('ribbit', '', array('id' => 'kermit')));
192 $this->assertSame('<span>ribbit</span>',
193 html_writer
::span('ribbit'));
197 * @covers ::start_span
199 public function test_start_span() {
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')));
207 $this->assertSame('<span class="frog">',
208 html_writer
::start_span('frog'));
210 $this->assertSame('<span id="kermit">',
211 html_writer
::start_span('', array('id' => 'kermit')));
213 $this->assertSame('<span>',
214 html_writer
::start_span());
220 public function test_end_span() {
221 $this->assertSame('</span>', html_writer
::end_span());
226 * @covers \html_table_row
227 * @covers \html_table_cell
228 * @covers \html_table
230 public function test_table() {
231 $row = new html_table_row();
233 // The attribute will get overwritten by the ID.
235 $row->attributes
['id'] = 'will get overwritten';
237 // The data-name will be present in the output.
238 $row->attributes
['data-name'] = 'Fred';
240 $cell = new html_table_cell();
242 // The attribute will get overwritten by the ID.
243 $cell->id
= 'Jeremy';
244 $cell->attributes
['id'] = 'will get overwritten';
246 // The data-name will be present in the output.
247 $cell->attributes
['data-name'] = 'John';
249 $row->cells
[] = $cell;
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';
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;
262 // Specify a caption to be output.
263 $table->caption
= "A table of meaningless data.";
265 $output = html_writer
::table($table);
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>
276 $this->assertSame($expected, $output);
282 public function test_table_hidden_caption() {
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')
291 $table->caption
= "Who even knows?";
292 $table->captionhide
= true;
293 $table->responsive
= false;
295 $output = html_writer
::table($table);
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>
303 <td class="cell c0" style="">bob</td>
304 <td class="cell c1 lastcol" style="">Burgers</td>
307 <td class="cell c0" style="">dave</td>
308 <td class="cell c1 lastcol" style="">Competitiveness</td>
314 $this->assertSame($expected, $output);