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