Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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