Merge "Add OOUI for HTMLFormFieldCloner"
[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
426 /**
427 * @covers Html::namespaceSelector
428 */
429 public function testCanFilterOutNamespaces() {
430 $this->assertEquals(
431 '<select id="namespace" name="namespace">' . "\n" .
432 '<option value="2">User</option>' . "\n" .
433 '<option value="4">MyWiki</option>' . "\n" .
434 '<option value="5">MyWiki Talk</option>' . "\n" .
435 '<option value="6">File</option>' . "\n" .
436 '<option value="7">File talk</option>' . "\n" .
437 '<option value="8">MediaWiki</option>' . "\n" .
438 '<option value="9">MediaWiki talk</option>' . "\n" .
439 '<option value="10">Template</option>' . "\n" .
440 '<option value="11">Template talk</option>' . "\n" .
441 '<option value="14">Category</option>' . "\n" .
442 '<option value="15">Category talk</option>' . "\n" .
443 '</select>',
444 Html::namespaceSelector(
445 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
446 ),
447 'Namespace selector namespace filtering.'
448 );
449 }
450
451 /**
452 * @covers Html::namespaceSelector
453 */
454 public function testCanDisableANamespaces() {
455 $this->assertEquals(
456 '<select id="namespace" name="namespace">' . "\n" .
457 '<option disabled="" value="0">(Principal)</option>' . "\n" .
458 '<option disabled="" value="1">Talk</option>' . "\n" .
459 '<option disabled="" value="2">User</option>' . "\n" .
460 '<option disabled="" value="3">User talk</option>' . "\n" .
461 '<option disabled="" value="4">MyWiki</option>' . "\n" .
462 '<option value="5">MyWiki Talk</option>' . "\n" .
463 '<option value="6">File</option>' . "\n" .
464 '<option value="7">File talk</option>' . "\n" .
465 '<option value="8">MediaWiki</option>' . "\n" .
466 '<option value="9">MediaWiki talk</option>' . "\n" .
467 '<option value="10">Template</option>' . "\n" .
468 '<option value="11">Template talk</option>' . "\n" .
469 '<option value="14">Category</option>' . "\n" .
470 '<option value="15">Category talk</option>' . "\n" .
471 '<option value="100">Custom</option>' . "\n" .
472 '<option value="101">Custom talk</option>' . "\n" .
473 '</select>',
474 Html::namespaceSelector( [
475 'disable' => [ 0, 1, 2, 3, 4 ]
476 ] ),
477 'Namespace selector namespace disabling'
478 );
479 }
480
481 /**
482 * @dataProvider provideHtml5InputTypes
483 * @covers Html::element
484 */
485 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
486 $this->assertEquals(
487 '<input type="' . $HTML5InputType . '"/>',
488 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
489 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
490 );
491 }
492
493 /**
494 * @covers Html::warningBox
495 * @covers Html::messageBox
496 */
497 public function testWarningBox() {
498 $this->assertEquals(
499 Html::warningBox( 'warn' ),
500 '<div class="warningbox">warn</div>'
501 );
502 }
503
504 /**
505 * @covers Html::errorBox
506 * @covers Html::messageBox
507 */
508 public function testErrorBox() {
509 $this->assertEquals(
510 Html::errorBox( 'err' ),
511 '<div class="errorbox">err</div>'
512 );
513 $this->assertEquals(
514 Html::errorBox( 'err', 'heading' ),
515 '<div class="errorbox"><h2>heading</h2>err</div>'
516 );
517 $this->assertEquals(
518 Html::errorBox( 'err', '0' ),
519 '<div class="errorbox"><h2>0</h2>err</div>'
520 );
521 }
522
523 /**
524 * @covers Html::successBox
525 * @covers Html::messageBox
526 */
527 public function testSuccessBox() {
528 $this->assertEquals(
529 Html::successBox( 'great' ),
530 '<div class="successbox">great</div>'
531 );
532 $this->assertEquals(
533 Html::successBox( '<script>beware no escaping!</script>' ),
534 '<div class="successbox"><script>beware no escaping!</script></div>'
535 );
536 }
537
538 /**
539 * List of input element types values introduced by HTML5
540 * Full list at https://www.w3.org/TR/html-markup/input.html
541 */
542 public static function provideHtml5InputTypes() {
543 $types = [
544 'datetime',
545 'datetime-local',
546 'date',
547 'month',
548 'time',
549 'week',
550 'number',
551 'range',
552 'email',
553 'url',
554 'search',
555 'tel',
556 'color',
557 ];
558 $cases = [];
559 foreach ( $types as $type ) {
560 $cases[] = [ $type ];
561 }
562
563 return $cases;
564 }
565
566 /**
567 * Test out Html::element drops or enforces default value
568 * @covers Html::dropDefaults
569 * @dataProvider provideElementsWithAttributesHavingDefaultValues
570 */
571 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
572 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
573 }
574
575 public static function provideElementsWithAttributesHavingDefaultValues() {
576 # Use cases in a concise format:
577 # <expected>, <element name>, <array of attributes> [, <message>]
578 # Will be mapped to Html::element()
579 $cases = [];
580
581 # ## Generic cases, match $attribDefault static array
582 $cases[] = [ '<area/>',
583 'area', [ 'shape' => 'rect' ]
584 ];
585
586 $cases[] = [ '<button type="submit"></button>',
587 'button', [ 'formaction' => 'GET' ]
588 ];
589 $cases[] = [ '<button type="submit"></button>',
590 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
591 ];
592
593 $cases[] = [ '<canvas></canvas>',
594 'canvas', [ 'height' => '150' ]
595 ];
596 $cases[] = [ '<canvas></canvas>',
597 'canvas', [ 'width' => '300' ]
598 ];
599 # Also check with numeric values
600 $cases[] = [ '<canvas></canvas>',
601 'canvas', [ 'height' => 150 ]
602 ];
603 $cases[] = [ '<canvas></canvas>',
604 'canvas', [ 'width' => 300 ]
605 ];
606
607 $cases[] = [ '<form></form>',
608 'form', [ 'action' => 'GET' ]
609 ];
610 $cases[] = [ '<form></form>',
611 'form', [ 'autocomplete' => 'on' ]
612 ];
613 $cases[] = [ '<form></form>',
614 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
615 ];
616
617 $cases[] = [ '<input/>',
618 'input', [ 'formaction' => 'GET' ]
619 ];
620 $cases[] = [ '<input/>',
621 'input', [ 'type' => 'text' ]
622 ];
623
624 $cases[] = [ '<keygen/>',
625 'keygen', [ 'keytype' => 'rsa' ]
626 ];
627
628 $cases[] = [ '<link/>',
629 'link', [ 'media' => 'all' ]
630 ];
631
632 $cases[] = [ '<menu></menu>',
633 'menu', [ 'type' => 'list' ]
634 ];
635
636 $cases[] = [ '<script></script>',
637 'script', [ 'type' => 'text/javascript' ]
638 ];
639
640 $cases[] = [ '<style></style>',
641 'style', [ 'media' => 'all' ]
642 ];
643 $cases[] = [ '<style></style>',
644 'style', [ 'type' => 'text/css' ]
645 ];
646
647 $cases[] = [ '<textarea></textarea>',
648 'textarea', [ 'wrap' => 'soft' ]
649 ];
650
651 # ## SPECIFIC CASES
652
653 # <link type="text/css">
654 $cases[] = [ '<link/>',
655 'link', [ 'type' => 'text/css' ]
656 ];
657
658 # <input> specific handling
659 $cases[] = [ '<input type="checkbox"/>',
660 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
661 'Default value "on" is stripped of checkboxes',
662 ];
663 $cases[] = [ '<input type="radio"/>',
664 'input', [ 'type' => 'radio', 'value' => 'on' ],
665 'Default value "on" is stripped of radio buttons',
666 ];
667 $cases[] = [ '<input type="submit" value="Submit"/>',
668 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
669 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
670 ];
671 $cases[] = [ '<input type="color"/>',
672 'input', [ 'type' => 'color', 'value' => '' ],
673 ];
674 $cases[] = [ '<input type="range"/>',
675 'input', [ 'type' => 'range', 'value' => '' ],
676 ];
677
678 # <button> specific handling
679 # see remarks on https://msdn.microsoft.com/library/ms535211(v=vs.85).aspx
680 $cases[] = [ '<button type="submit"></button>',
681 'button', [ 'type' => 'submit' ],
682 'According to standard the default type is "submit". '
683 . 'Depending on compatibility mode IE might use "button", instead.',
684 ];
685
686 # <select> specific handling
687 $cases[] = [ '<select multiple=""></select>',
688 'select', [ 'size' => '4', 'multiple' => true ],
689 ];
690 # .. with numeric value
691 $cases[] = [ '<select multiple=""></select>',
692 'select', [ 'size' => 4, 'multiple' => true ],
693 ];
694 $cases[] = [ '<select></select>',
695 'select', [ 'size' => '1', 'multiple' => false ],
696 ];
697 # .. with numeric value
698 $cases[] = [ '<select></select>',
699 'select', [ 'size' => 1, 'multiple' => false ],
700 ];
701
702 # Passing an array as value
703 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
704 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
705 "dropDefaults accepts values given as an array"
706 ];
707
708 # FIXME: doDropDefault should remove defaults given in an array
709 # Expected should be '<a></a>'
710 $cases[] = [ '<a class=""></a>',
711 'a', [ 'class' => [ '', '' ] ],
712 "dropDefaults accepts values given as an array"
713 ];
714
715 # Craft the Html elements
716 $ret = [];
717 foreach ( $cases as $case ) {
718 $ret[] = [
719 $case[0],
720 $case[1], $case[2],
721 $case[3] ?? ''
722 ];
723 }
724
725 return $ret;
726 }
727
728 /**
729 * @covers Html::input
730 */
731 public function testWrapperInput() {
732 $this->assertEquals(
733 '<input type="radio" value="testval" name="testname"/>',
734 Html::input( 'testname', 'testval', 'radio' ),
735 'Input wrapper with type and value.'
736 );
737 $this->assertEquals(
738 '<input name="testname"/>',
739 Html::input( 'testname' ),
740 'Input wrapper with all default values.'
741 );
742 }
743
744 /**
745 * @covers Html::check
746 */
747 public function testWrapperCheck() {
748 $this->assertEquals(
749 '<input type="checkbox" value="1" name="testname"/>',
750 Html::check( 'testname' ),
751 'Checkbox wrapper unchecked.'
752 );
753 $this->assertEquals(
754 '<input checked="" type="checkbox" value="1" name="testname"/>',
755 Html::check( 'testname', true ),
756 'Checkbox wrapper checked.'
757 );
758 $this->assertEquals(
759 '<input type="checkbox" value="testval" name="testname"/>',
760 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
761 'Checkbox wrapper with a value override.'
762 );
763 }
764
765 /**
766 * @covers Html::radio
767 */
768 public function testWrapperRadio() {
769 $this->assertEquals(
770 '<input type="radio" value="1" name="testname"/>',
771 Html::radio( 'testname' ),
772 'Radio wrapper unchecked.'
773 );
774 $this->assertEquals(
775 '<input checked="" type="radio" value="1" name="testname"/>',
776 Html::radio( 'testname', true ),
777 'Radio wrapper checked.'
778 );
779 $this->assertEquals(
780 '<input type="radio" value="testval" name="testname"/>',
781 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
782 'Radio wrapper with a value override.'
783 );
784 }
785
786 /**
787 * @covers Html::label
788 */
789 public function testWrapperLabel() {
790 $this->assertEquals(
791 '<label for="testid">testlabel</label>',
792 Html::label( 'testlabel', 'testid' ),
793 'Label wrapper'
794 );
795 }
796
797 public static function provideSrcSetImages() {
798 return [
799 [ [], '', 'when there are no images, return empty string' ],
800 [
801 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
802 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
803 'pixel depth keys may include a trailing "x"'
804 ],
805 [
806 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
807 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
808 'pixel depth keys may omit a trailing "x"'
809 ],
810 [
811 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
812 'small.png 1x, large.png 1.5x',
813 'omit larger duplicates'
814 ],
815 [
816 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
817 'small.png 1x, large.png 1.5x',
818 'omit larger duplicates in irregular order'
819 ],
820 ];
821 }
822
823 /**
824 * @dataProvider provideSrcSetImages
825 * @covers Html::srcSet
826 */
827 public function testSrcSet( $images, $expected, $message ) {
828 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
829 }
830
831 public static function provideInlineScript() {
832 return [
833 'Empty' => [
834 '',
835 '<script></script>'
836 ],
837 'Simple' => [
838 'EXAMPLE.label("foo");',
839 '<script>EXAMPLE.label("foo");</script>'
840 ],
841 'Ampersand' => [
842 'EXAMPLE.is(a && b);',
843 '<script>EXAMPLE.is(a && b);</script>'
844 ],
845 'HTML' => [
846 'EXAMPLE.label("<a>");',
847 '<script>EXAMPLE.label("<a>");</script>'
848 ],
849 'Script closing string (lower)' => [
850 'EXAMPLE.label("</script>");',
851 '<script>/* ERROR: Invalid script */</script>',
852 true,
853 ],
854 'Script closing with non-standard attributes (mixed)' => [
855 'EXAMPLE.label("</SCriPT and STyLE>");',
856 '<script>/* ERROR: Invalid script */</script>',
857 true,
858 ],
859 'HTML-comment-open and script-open' => [
860 // In HTML, <script> contents aren't just plain CDATA until </script>,
861 // there are levels of escaping modes, and the below sequence puts an
862 // HTML parser in a state where </script> would *not* close the script.
863 // https://html.spec.whatwg.org/multipage/parsing.html#script-data-double-escape-end-state
864 'var a = "<!--<script>";',
865 '<script>/* ERROR: Invalid script */</script>',
866 true,
867 ],
868 ];
869 }
870
871 /**
872 * @dataProvider provideInlineScript
873 * @covers Html::inlineScript
874 */
875 public function testInlineScript( $code, $expected, $error = false ) {
876 if ( $error ) {
877 Wikimedia\suppressWarnings();
878 $this->restoreWarnings = true;
879 }
880 $this->assertSame( Html::inlineScript( $code ), $expected );
881 }
882 }
883
884 class HtmlTestValue {
885 function __toString() {
886 return 'stringValue';
887 }
888 }