Merge "JSON formatversion=2 is no longer experimental"
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
1 <?php
2
3 class HtmlTest extends MediaWikiTestCase {
4 private $restoreWarnings;
5
6 protected function setUp() {
7 parent::setUp();
8
9 $this->setMwGlobals( [
10 'wgUseMediaWikiUIEverywhere' => false,
11 ] );
12
13 $contLangObj = Language::factory( 'en' );
14
15 // Hardcode namespaces during test runs,
16 // so that html output based on existing namespaces
17 // can be properly evaluated.
18 $contLangObj->setNamespaces( [
19 -2 => 'Media',
20 -1 => 'Special',
21 0 => '',
22 1 => 'Talk',
23 2 => 'User',
24 3 => 'User_talk',
25 4 => 'MyWiki',
26 5 => 'MyWiki_Talk',
27 6 => 'File',
28 7 => 'File_talk',
29 8 => 'MediaWiki',
30 9 => 'MediaWiki_talk',
31 10 => 'Template',
32 11 => 'Template_talk',
33 14 => 'Category',
34 15 => 'Category_talk',
35 100 => 'Custom',
36 101 => 'Custom_talk',
37 ] );
38 $this->setContentLang( $contLangObj );
39
40 $userLangObj = Language::factory( 'es' );
41 $userLangObj->setNamespaces( [
42 -2 => "Medio",
43 -1 => "Especial",
44 0 => "",
45 1 => "Discusión",
46 2 => "Usuario",
47 3 => "Usuario discusión",
48 4 => "Wiki",
49 5 => "Wiki discusión",
50 6 => "Archivo",
51 7 => "Archivo discusión",
52 8 => "MediaWiki",
53 9 => "MediaWiki discusión",
54 10 => "Plantilla",
55 11 => "Plantilla discusión",
56 12 => "Ayuda",
57 13 => "Ayuda discusión",
58 14 => "Categoría",
59 15 => "Categoría discusión",
60 100 => "Personalizado",
61 101 => "Personalizado discusión",
62 ] );
63 $this->setUserLang( $userLangObj );
64
65 $this->restoreWarnings = false;
66 }
67
68 protected function tearDown() {
69 Language::factory( 'en' )->resetNamespaces();
70
71 if ( $this->restoreWarnings ) {
72 $this->restoreWarnings = false;
73 Wikimedia\restoreWarnings();
74 }
75
76 parent::tearDown();
77 }
78
79 /**
80 * @covers Html::element
81 * @covers Html::rawElement
82 * @covers Html::openElement
83 * @covers Html::closeElement
84 */
85 public function testElementBasics() {
86 $this->assertEquals(
87 '<img/>',
88 Html::element( 'img', null, '' ),
89 'Self-closing tag for short-tag elements'
90 );
91
92 $this->assertEquals(
93 '<element></element>',
94 Html::element( 'element', null, null ),
95 'Close tag for empty element (null, null)'
96 );
97
98 $this->assertEquals(
99 '<element></element>',
100 Html::element( 'element', [], '' ),
101 'Close tag for empty element (array, string)'
102 );
103 }
104
105 public function dataXmlMimeType() {
106 return [
107 // ( $mimetype, $isXmlMimeType )
108 # HTML is not an XML MimeType
109 [ 'text/html', false ],
110 # XML is an XML MimeType
111 [ 'text/xml', true ],
112 [ 'application/xml', true ],
113 # XHTML is an XML MimeType
114 [ 'application/xhtml+xml', true ],
115 # Make sure other +xml MimeTypes are supported
116 # SVG is another random MimeType even though we don't use it
117 [ 'image/svg+xml', true ],
118 # Complete random other MimeTypes are not XML
119 [ 'text/plain', false ],
120 ];
121 }
122
123 /**
124 * @dataProvider dataXmlMimeType
125 * @covers Html::isXmlMimeType
126 */
127 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
128 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
129 }
130
131 /**
132 * @covers Html::expandAttributes
133 */
134 public function testExpandAttributesSkipsNullAndFalse() {
135 # ## EMPTY ########
136 $this->assertEmpty(
137 Html::expandAttributes( [ 'foo' => null ] ),
138 'skip keys with null value'
139 );
140 $this->assertEmpty(
141 Html::expandAttributes( [ 'foo' => false ] ),
142 'skip keys with false value'
143 );
144 $this->assertEquals(
145 ' foo=""',
146 Html::expandAttributes( [ 'foo' => '' ] ),
147 'keep keys with an empty string'
148 );
149 }
150
151 /**
152 * @covers Html::expandAttributes
153 */
154 public function testExpandAttributesForBooleans() {
155 $this->assertEquals(
156 '',
157 Html::expandAttributes( [ 'selected' => false ] ),
158 'Boolean attributes do not generates output when value is false'
159 );
160 $this->assertEquals(
161 '',
162 Html::expandAttributes( [ 'selected' => null ] ),
163 'Boolean attributes do not generates output when value is null'
164 );
165
166 $this->assertEquals(
167 ' selected=""',
168 Html::expandAttributes( [ 'selected' => true ] ),
169 'Boolean attributes have no value when value is true'
170 );
171 $this->assertEquals(
172 ' selected=""',
173 Html::expandAttributes( [ 'selected' ] ),
174 'Boolean attributes have no value when value is true (passed as numerical array)'
175 );
176 }
177
178 /**
179 * @covers Html::expandAttributes
180 */
181 public function testExpandAttributesForNumbers() {
182 $this->assertEquals(
183 ' value="1"',
184 Html::expandAttributes( [ 'value' => 1 ] ),
185 'Integer value is cast to a string'
186 );
187 $this->assertEquals(
188 ' value="1.1"',
189 Html::expandAttributes( [ 'value' => 1.1 ] ),
190 'Float value is cast to a string'
191 );
192 }
193
194 /**
195 * @covers Html::expandAttributes
196 */
197 public function testExpandAttributesForObjects() {
198 $this->assertEquals(
199 ' value="stringValue"',
200 Html::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
201 'Object value is converted to a string'
202 );
203 }
204
205 /**
206 * Test for Html::expandAttributes()
207 * Please note it output a string prefixed with a space!
208 * @covers Html::expandAttributes
209 */
210 public function testExpandAttributesVariousExpansions() {
211 # ## NOT EMPTY ####
212 $this->assertEquals(
213 ' empty_string=""',
214 Html::expandAttributes( [ 'empty_string' => '' ] ),
215 'Empty string is always quoted'
216 );
217 $this->assertEquals(
218 ' key="value"',
219 Html::expandAttributes( [ 'key' => 'value' ] ),
220 'Simple string value needs no quotes'
221 );
222 $this->assertEquals(
223 ' one="1"',
224 Html::expandAttributes( [ 'one' => 1 ] ),
225 'Number 1 value needs no quotes'
226 );
227 $this->assertEquals(
228 ' zero="0"',
229 Html::expandAttributes( [ 'zero' => 0 ] ),
230 'Number 0 value needs no quotes'
231 );
232 }
233
234 /**
235 * Html::expandAttributes has special features for HTML
236 * attributes that use space separated lists and also
237 * allows arrays to be used as values.
238 * @covers Html::expandAttributes
239 */
240 public function testExpandAttributesListValueAttributes() {
241 # ## STRING VALUES
242 $this->assertEquals(
243 ' class="redundant spaces here"',
244 Html::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
245 'Normalization should strip redundant spaces'
246 );
247 $this->assertEquals(
248 ' class="foo bar"',
249 Html::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
250 'Normalization should remove duplicates in string-lists'
251 );
252 # ## "EMPTY" ARRAY VALUES
253 $this->assertEquals(
254 ' class=""',
255 Html::expandAttributes( [ 'class' => [] ] ),
256 'Value with an empty array'
257 );
258 $this->assertEquals(
259 ' class=""',
260 Html::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
261 'Array with null, empty string and spaces'
262 );
263 # ## NON-EMPTY ARRAY VALUES
264 $this->assertEquals(
265 ' class="foo bar"',
266 Html::expandAttributes( [ 'class' => [
267 'foo',
268 'bar',
269 'foo',
270 'bar',
271 'bar',
272 ] ] ),
273 'Normalization should remove duplicates in the array'
274 );
275 $this->assertEquals(
276 ' class="foo bar"',
277 Html::expandAttributes( [ 'class' => [
278 'foo bar',
279 'bar foo',
280 'foo',
281 'bar bar',
282 ] ] ),
283 'Normalization should remove duplicates in string-lists in the array'
284 );
285 }
286
287 /**
288 * Test feature added by r96188, let pass attributes values as
289 * a PHP array. Restricted to class,rel, accesskey.
290 * @covers Html::expandAttributes
291 */
292 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
293 $this->assertEquals(
294 ' class="booltrue one"',
295 Html::expandAttributes( [ 'class' => [
296 'booltrue' => true,
297 'one' => 1,
298
299 # Method use isset() internally, make sure we do discard
300 # attributes values which have been assigned well known values
301 'emptystring' => '',
302 'boolfalse' => false,
303 'zero' => 0,
304 'null' => null,
305 ] ] )
306 );
307 }
308
309 /**
310 * How do we handle duplicate keys in HTML attributes expansion?
311 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
312 * The latter will take precedence.
313 *
314 * Feature added by r96188
315 * @covers Html::expandAttributes
316 */
317 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
318 $this->assertEquals(
319 ' class=""',
320 Html::expandAttributes( [ 'class' => [
321 'GREEN',
322 'GREEN' => false,
323 'GREEN',
324 ] ] )
325 );
326 }
327
328 /**
329 * @covers Html::expandAttributes
330 * @expectedException MWException
331 */
332 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
333 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
334 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
335 Html::expandAttributes( [
336 'src' => [
337 'ltr' => 'ltr.svg',
338 'rtl' => 'rtl.svg'
339 ]
340 ] );
341 }
342
343 /**
344 * @covers Html::namespaceSelector
345 * @covers Html::namespaceSelectorOptions
346 */
347 public function testNamespaceSelector() {
348 $this->assertEquals(
349 '<select id="namespace" name="namespace">' . "\n" .
350 '<option value="0">(Principal)</option>' . "\n" .
351 '<option value="1">Talk</option>' . "\n" .
352 '<option value="2">User</option>' . "\n" .
353 '<option value="3">User talk</option>' . "\n" .
354 '<option value="4">MyWiki</option>' . "\n" .
355 '<option value="5">MyWiki Talk</option>' . "\n" .
356 '<option value="6">File</option>' . "\n" .
357 '<option value="7">File talk</option>' . "\n" .
358 '<option value="8">MediaWiki</option>' . "\n" .
359 '<option value="9">MediaWiki talk</option>' . "\n" .
360 '<option value="10">Template</option>' . "\n" .
361 '<option value="11">Template talk</option>' . "\n" .
362 '<option value="14">Category</option>' . "\n" .
363 '<option value="15">Category talk</option>' . "\n" .
364 '<option value="100">Custom</option>' . "\n" .
365 '<option value="101">Custom talk</option>' . "\n" .
366 '</select>',
367 Html::namespaceSelector(),
368 'Basic namespace selector without custom options'
369 );
370
371 $this->assertEquals(
372 '<label for="mw-test-namespace">Select a namespace:</label>' . "\u{00A0}" .
373 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
374 '<option value="all">todos</option>' . "\n" .
375 '<option value="0">(Principal)</option>' . "\n" .
376 '<option value="1">Talk</option>' . "\n" .
377 '<option value="2" selected="">User</option>' . "\n" .
378 '<option value="3">User talk</option>' . "\n" .
379 '<option value="4">MyWiki</option>' . "\n" .
380 '<option value="5">MyWiki Talk</option>' . "\n" .
381 '<option value="6">File</option>' . "\n" .
382 '<option value="7">File talk</option>' . "\n" .
383 '<option value="8">MediaWiki</option>' . "\n" .
384 '<option value="9">MediaWiki talk</option>' . "\n" .
385 '<option value="10">Template</option>' . "\n" .
386 '<option value="11">Template talk</option>' . "\n" .
387 '<option value="14">Category</option>' . "\n" .
388 '<option value="15">Category talk</option>' . "\n" .
389 '<option value="100">Custom</option>' . "\n" .
390 '<option value="101">Custom talk</option>' . "\n" .
391 '</select>',
392 Html::namespaceSelector(
393 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
394 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
395 ),
396 'Basic namespace selector with custom values'
397 );
398
399 $this->assertEquals(
400 '<label for="namespace">Select a namespace:</label>' . "\u{00A0}" .
401 '<select id="namespace" name="namespace">' . "\n" .
402 '<option value="0">(Principal)</option>' . "\n" .
403 '<option value="1">Talk</option>' . "\n" .
404 '<option value="2">User</option>' . "\n" .
405 '<option value="3">User talk</option>' . "\n" .
406 '<option value="4">MyWiki</option>' . "\n" .
407 '<option value="5">MyWiki Talk</option>' . "\n" .
408 '<option value="6">File</option>' . "\n" .
409 '<option value="7">File talk</option>' . "\n" .
410 '<option value="8">MediaWiki</option>' . "\n" .
411 '<option value="9">MediaWiki talk</option>' . "\n" .
412 '<option value="10">Template</option>' . "\n" .
413 '<option value="11">Template talk</option>' . "\n" .
414 '<option value="14">Category</option>' . "\n" .
415 '<option value="15">Category talk</option>' . "\n" .
416 '<option value="100">Custom</option>' . "\n" .
417 '<option value="101">Custom talk</option>' . "\n" .
418 '</select>',
419 Html::namespaceSelector(
420 [ 'label' => 'Select a namespace:' ]
421 ),
422 'Basic namespace selector with a custom label but no id attribtue for the <select>'
423 );
424
425 $this->assertEquals(
426 '<select id="namespace" name="namespace">' . "\n" .
427 '<option value="0">(Principal)</option>' . "\n" .
428 '<option value="1">Discusión</option>' . "\n" .
429 '<option value="2">Usuario</option>' . "\n" .
430 '<option value="3">Usuario discusión</option>' . "\n" .
431 '<option value="4">Wiki</option>' . "\n" .
432 '<option value="5">Wiki discusión</option>' . "\n" .
433 '<option value="6">Archivo</option>' . "\n" .
434 '<option value="7">Archivo discusión</option>' . "\n" .
435 '<option value="8">MediaWiki</option>' . "\n" .
436 '<option value="9">MediaWiki discusión</option>' . "\n" .
437 '<option value="10">Plantilla</option>' . "\n" .
438 '<option value="11">Plantilla discusión</option>' . "\n" .
439 '<option value="12">Ayuda</option>' . "\n" .
440 '<option value="13">Ayuda discusión</option>' . "\n" .
441 '<option value="14">Categoría</option>' . "\n" .
442 '<option value="15">Categoría discusión</option>' . "\n" .
443 '<option value="100">Personalizado</option>' . "\n" .
444 '<option value="101">Personalizado discusión</option>' . "\n" .
445 '</select>',
446 Html::namespaceSelector(
447 [ 'in-user-lang' => true ]
448 ),
449 'Basic namespace selector in user language'
450 );
451 }
452
453 /**
454 * @covers Html::namespaceSelector
455 */
456 public function testCanFilterOutNamespaces() {
457 $this->assertEquals(
458 '<select id="namespace" name="namespace">' . "\n" .
459 '<option value="2">User</option>' . "\n" .
460 '<option value="4">MyWiki</option>' . "\n" .
461 '<option value="5">MyWiki Talk</option>' . "\n" .
462 '<option value="6">File</option>' . "\n" .
463 '<option value="7">File talk</option>' . "\n" .
464 '<option value="8">MediaWiki</option>' . "\n" .
465 '<option value="9">MediaWiki talk</option>' . "\n" .
466 '<option value="10">Template</option>' . "\n" .
467 '<option value="11">Template talk</option>' . "\n" .
468 '<option value="14">Category</option>' . "\n" .
469 '<option value="15">Category talk</option>' . "\n" .
470 '</select>',
471 Html::namespaceSelector(
472 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
473 ),
474 'Namespace selector namespace filtering.'
475 );
476 }
477
478 /**
479 * @covers Html::namespaceSelector
480 */
481 public function testCanDisableANamespaces() {
482 $this->assertEquals(
483 '<select id="namespace" name="namespace">' . "\n" .
484 '<option disabled="" value="0">(Principal)</option>' . "\n" .
485 '<option disabled="" value="1">Talk</option>' . "\n" .
486 '<option disabled="" value="2">User</option>' . "\n" .
487 '<option disabled="" value="3">User talk</option>' . "\n" .
488 '<option disabled="" value="4">MyWiki</option>' . "\n" .
489 '<option value="5">MyWiki Talk</option>' . "\n" .
490 '<option value="6">File</option>' . "\n" .
491 '<option value="7">File talk</option>' . "\n" .
492 '<option value="8">MediaWiki</option>' . "\n" .
493 '<option value="9">MediaWiki talk</option>' . "\n" .
494 '<option value="10">Template</option>' . "\n" .
495 '<option value="11">Template talk</option>' . "\n" .
496 '<option value="14">Category</option>' . "\n" .
497 '<option value="15">Category talk</option>' . "\n" .
498 '<option value="100">Custom</option>' . "\n" .
499 '<option value="101">Custom talk</option>' . "\n" .
500 '</select>',
501 Html::namespaceSelector( [
502 'disable' => [ 0, 1, 2, 3, 4 ]
503 ] ),
504 'Namespace selector namespace disabling'
505 );
506 }
507
508 /**
509 * @dataProvider provideHtml5InputTypes
510 * @covers Html::element
511 */
512 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
513 $this->assertEquals(
514 '<input type="' . $HTML5InputType . '"/>',
515 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
516 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
517 );
518 }
519
520 /**
521 * @covers Html::warningBox
522 * @covers Html::messageBox
523 */
524 public function testWarningBox() {
525 $this->assertEquals(
526 Html::warningBox( 'warn' ),
527 '<div class="warningbox">warn</div>'
528 );
529 }
530
531 /**
532 * @covers Html::errorBox
533 * @covers Html::messageBox
534 */
535 public function testErrorBox() {
536 $this->assertEquals(
537 Html::errorBox( 'err' ),
538 '<div class="errorbox">err</div>'
539 );
540 $this->assertEquals(
541 Html::errorBox( 'err', 'heading' ),
542 '<div class="errorbox"><h2>heading</h2>err</div>'
543 );
544 $this->assertEquals(
545 Html::errorBox( 'err', '0' ),
546 '<div class="errorbox"><h2>0</h2>err</div>'
547 );
548 }
549
550 /**
551 * @covers Html::successBox
552 * @covers Html::messageBox
553 */
554 public function testSuccessBox() {
555 $this->assertEquals(
556 Html::successBox( 'great' ),
557 '<div class="successbox">great</div>'
558 );
559 $this->assertEquals(
560 Html::successBox( '<script>beware no escaping!</script>' ),
561 '<div class="successbox"><script>beware no escaping!</script></div>'
562 );
563 }
564
565 /**
566 * List of input element types values introduced by HTML5
567 * Full list at https://www.w3.org/TR/html-markup/input.html
568 */
569 public static function provideHtml5InputTypes() {
570 $types = [
571 'datetime',
572 'datetime-local',
573 'date',
574 'month',
575 'time',
576 'week',
577 'number',
578 'range',
579 'email',
580 'url',
581 'search',
582 'tel',
583 'color',
584 ];
585 $cases = [];
586 foreach ( $types as $type ) {
587 $cases[] = [ $type ];
588 }
589
590 return $cases;
591 }
592
593 /**
594 * Test out Html::element drops or enforces default value
595 * @covers Html::dropDefaults
596 * @dataProvider provideElementsWithAttributesHavingDefaultValues
597 */
598 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
599 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
600 }
601
602 public static function provideElementsWithAttributesHavingDefaultValues() {
603 # Use cases in a concise format:
604 # <expected>, <element name>, <array of attributes> [, <message>]
605 # Will be mapped to Html::element()
606 $cases = [];
607
608 # ## Generic cases, match $attribDefault static array
609 $cases[] = [ '<area/>',
610 'area', [ 'shape' => 'rect' ]
611 ];
612
613 $cases[] = [ '<button type="submit"></button>',
614 'button', [ 'formaction' => 'GET' ]
615 ];
616 $cases[] = [ '<button type="submit"></button>',
617 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
618 ];
619
620 $cases[] = [ '<canvas></canvas>',
621 'canvas', [ 'height' => '150' ]
622 ];
623 $cases[] = [ '<canvas></canvas>',
624 'canvas', [ 'width' => '300' ]
625 ];
626 # Also check with numeric values
627 $cases[] = [ '<canvas></canvas>',
628 'canvas', [ 'height' => 150 ]
629 ];
630 $cases[] = [ '<canvas></canvas>',
631 'canvas', [ 'width' => 300 ]
632 ];
633
634 $cases[] = [ '<form></form>',
635 'form', [ 'action' => 'GET' ]
636 ];
637 $cases[] = [ '<form></form>',
638 'form', [ 'autocomplete' => 'on' ]
639 ];
640 $cases[] = [ '<form></form>',
641 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
642 ];
643
644 $cases[] = [ '<input/>',
645 'input', [ 'formaction' => 'GET' ]
646 ];
647 $cases[] = [ '<input/>',
648 'input', [ 'type' => 'text' ]
649 ];
650
651 $cases[] = [ '<keygen/>',
652 'keygen', [ 'keytype' => 'rsa' ]
653 ];
654
655 $cases[] = [ '<link/>',
656 'link', [ 'media' => 'all' ]
657 ];
658
659 $cases[] = [ '<menu></menu>',
660 'menu', [ 'type' => 'list' ]
661 ];
662
663 $cases[] = [ '<script></script>',
664 'script', [ 'type' => 'text/javascript' ]
665 ];
666
667 $cases[] = [ '<style></style>',
668 'style', [ 'media' => 'all' ]
669 ];
670 $cases[] = [ '<style></style>',
671 'style', [ 'type' => 'text/css' ]
672 ];
673
674 $cases[] = [ '<textarea></textarea>',
675 'textarea', [ 'wrap' => 'soft' ]
676 ];
677
678 # ## SPECIFIC CASES
679
680 # <link type="text/css">
681 $cases[] = [ '<link/>',
682 'link', [ 'type' => 'text/css' ]
683 ];
684
685 # <input> specific handling
686 $cases[] = [ '<input type="checkbox"/>',
687 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
688 'Default value "on" is stripped of checkboxes',
689 ];
690 $cases[] = [ '<input type="radio"/>',
691 'input', [ 'type' => 'radio', 'value' => 'on' ],
692 'Default value "on" is stripped of radio buttons',
693 ];
694 $cases[] = [ '<input type="submit" value="Submit"/>',
695 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
696 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
697 ];
698 $cases[] = [ '<input type="color"/>',
699 'input', [ 'type' => 'color', 'value' => '' ],
700 ];
701 $cases[] = [ '<input type="range"/>',
702 'input', [ 'type' => 'range', 'value' => '' ],
703 ];
704
705 # <button> specific handling
706 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
707 $cases[] = [ '<button type="submit"></button>',
708 'button', [ 'type' => 'submit' ],
709 'According to standard the default type is "submit". '
710 . 'Depending on compatibility mode IE might use "button", instead.',
711 ];
712
713 # <select> specific handling
714 $cases[] = [ '<select multiple=""></select>',
715 'select', [ 'size' => '4', 'multiple' => true ],
716 ];
717 # .. with numeric value
718 $cases[] = [ '<select multiple=""></select>',
719 'select', [ 'size' => 4, 'multiple' => true ],
720 ];
721 $cases[] = [ '<select></select>',
722 'select', [ 'size' => '1', 'multiple' => false ],
723 ];
724 # .. with numeric value
725 $cases[] = [ '<select></select>',
726 'select', [ 'size' => 1, 'multiple' => false ],
727 ];
728
729 # Passing an array as value
730 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
731 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
732 "dropDefaults accepts values given as an array"
733 ];
734
735 # FIXME: doDropDefault should remove defaults given in an array
736 # Expected should be '<a></a>'
737 $cases[] = [ '<a class=""></a>',
738 'a', [ 'class' => [ '', '' ] ],
739 "dropDefaults accepts values given as an array"
740 ];
741
742 # Craft the Html elements
743 $ret = [];
744 foreach ( $cases as $case ) {
745 $ret[] = [
746 $case[0],
747 $case[1], $case[2],
748 $case[3] ?? ''
749 ];
750 }
751
752 return $ret;
753 }
754
755 /**
756 * @covers Html::input
757 */
758 public function testWrapperInput() {
759 $this->assertEquals(
760 '<input type="radio" value="testval" name="testname"/>',
761 Html::input( 'testname', 'testval', 'radio' ),
762 'Input wrapper with type and value.'
763 );
764 $this->assertEquals(
765 '<input name="testname"/>',
766 Html::input( 'testname' ),
767 'Input wrapper with all default values.'
768 );
769 }
770
771 /**
772 * @covers Html::check
773 */
774 public function testWrapperCheck() {
775 $this->assertEquals(
776 '<input type="checkbox" value="1" name="testname"/>',
777 Html::check( 'testname' ),
778 'Checkbox wrapper unchecked.'
779 );
780 $this->assertEquals(
781 '<input checked="" type="checkbox" value="1" name="testname"/>',
782 Html::check( 'testname', true ),
783 'Checkbox wrapper checked.'
784 );
785 $this->assertEquals(
786 '<input type="checkbox" value="testval" name="testname"/>',
787 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
788 'Checkbox wrapper with a value override.'
789 );
790 }
791
792 /**
793 * @covers Html::radio
794 */
795 public function testWrapperRadio() {
796 $this->assertEquals(
797 '<input type="radio" value="1" name="testname"/>',
798 Html::radio( 'testname' ),
799 'Radio wrapper unchecked.'
800 );
801 $this->assertEquals(
802 '<input checked="" type="radio" value="1" name="testname"/>',
803 Html::radio( 'testname', true ),
804 'Radio wrapper checked.'
805 );
806 $this->assertEquals(
807 '<input type="radio" value="testval" name="testname"/>',
808 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
809 'Radio wrapper with a value override.'
810 );
811 }
812
813 /**
814 * @covers Html::label
815 */
816 public function testWrapperLabel() {
817 $this->assertEquals(
818 '<label for="testid">testlabel</label>',
819 Html::label( 'testlabel', 'testid' ),
820 'Label wrapper'
821 );
822 }
823
824 public static function provideSrcSetImages() {
825 return [
826 [ [], '', 'when there are no images, return empty string' ],
827 [
828 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
829 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
830 'pixel depth keys may include a trailing "x"'
831 ],
832 [
833 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
834 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
835 'pixel depth keys may omit a trailing "x"'
836 ],
837 [
838 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
839 'small.png 1x, large.png 1.5x',
840 'omit larger duplicates'
841 ],
842 [
843 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
844 'small.png 1x, large.png 1.5x',
845 'omit larger duplicates in irregular order'
846 ],
847 ];
848 }
849
850 /**
851 * @dataProvider provideSrcSetImages
852 * @covers Html::srcSet
853 */
854 public function testSrcSet( $images, $expected, $message ) {
855 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
856 }
857
858 public static function provideInlineScript() {
859 return [
860 'Empty' => [
861 '',
862 '<script></script>'
863 ],
864 'Simple' => [
865 'EXAMPLE.label("foo");',
866 '<script>EXAMPLE.label("foo");</script>'
867 ],
868 'Ampersand' => [
869 'EXAMPLE.is(a && b);',
870 '<script>EXAMPLE.is(a && b);</script>'
871 ],
872 'HTML' => [
873 'EXAMPLE.label("<a>");',
874 '<script>EXAMPLE.label("<a>");</script>'
875 ],
876 'Script closing string (lower)' => [
877 'EXAMPLE.label("</script>");',
878 '<script>/* ERROR: Invalid script */</script>',
879 true,
880 ],
881 'Script closing with non-standard attributes (mixed)' => [
882 'EXAMPLE.label("</SCriPT and STyLE>");',
883 '<script>/* ERROR: Invalid script */</script>',
884 true,
885 ],
886 'HTML-comment-open and script-open' => [
887 // In HTML, <script> contents aren't just plain CDATA until </script>,
888 // there are levels of escaping modes, and the below sequence puts an
889 // HTML parser in a state where </script> would *not* close the script.
890 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
891 'var a = "<!--<script>";',
892 '<script>/* ERROR: Invalid script */</script>',
893 true,
894 ],
895 ];
896 }
897
898 /**
899 * @dataProvider provideInlineScript
900 * @covers Html::inlineScript
901 */
902 public function testInlineScript( $code, $expected, $error = false ) {
903 if ( $error ) {
904 Wikimedia\suppressWarnings();
905 $this->restoreWarnings = true;
906 }
907 $this->assertSame( Html::inlineScript( $code ), $expected );
908 }
909 }
910
911 class HtmlTestValue {
912 function __toString() {
913 return 'stringValue';
914 }
915 }