MDL-80203 courseformat: Fix some typos and PHPDoc
[moodle.git] / lib / tests / html_writer_test.php
blob623078eaa5796f0a35d1672a559cc36b27072dfc
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.
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/>.
17 /**
18 * Unit tests for the html_writer class.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/outputcomponents.php');
31 /**
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 {
41 /**
42 * @covers ::start_tag
44 public function test_start_tag() {
45 $this->assertSame('<div>', html_writer::start_tag('div'));
48 /**
49 * @covers ::start_tag
51 public function test_start_tag_with_attr() {
52 $this->assertSame('<div class="frog">',
53 html_writer::start_tag('div', array('class' => 'frog')));
56 /**
57 * @covers ::start_tag
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')));
64 /**
65 * @covers ::end_tag
67 public function test_end_tag() {
68 $this->assertSame('</div>', html_writer::end_tag('div'));
71 /**
72 * @covers ::empty_Tag
74 public function test_empty_tag() {
75 $this->assertSame('<br />', html_writer::empty_tag('br'));
78 /**
79 * @covers ::empty_Tag
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')));
86 /**
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!'));
94 /**
95 * @covers ::nonempty_tag
97 public function test_nonempty_tag_empty() {
98 $this->assertSame('',
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')));
127 * @covers ::div
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'));
148 * @covers ::start_div
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());
169 * @covers ::end_div
171 public function test_end_div() {
172 $this->assertSame('</div>', html_writer::end_div());
176 * @covers ::span
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'));
197 * @covers ::start_span
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());
218 * @covers ::end_span
220 public function test_end_span() {
221 $this->assertSame('</span>', html_writer::end_span());
225 * @covers ::table
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.
234 $row->id = 'Bob';
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);
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>
275 EOF;
276 $this->assertSame($expected, $output);
280 * @covers ::table
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);
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>
313 EOF;
314 $this->assertSame($expected, $output);