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