UserrightsPage::makeGroupNameListForLog() was removed
[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 'wgWellFormedXml' => 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 */
45 public function testElementBasics() {
46 $this->assertEquals(
47 '<img>',
48 Html::element( 'img', null, '' ),
49 'No close tag for short-tag elements'
50 );
51
52 $this->assertEquals(
53 '<element></element>',
54 Html::element( 'element', null, null ),
55 'Close tag for empty element (null, null)'
56 );
57
58 $this->assertEquals(
59 '<element></element>',
60 Html::element( 'element', [], '' ),
61 'Close tag for empty element (array, string)'
62 );
63
64 $this->setMwGlobals( 'wgWellFormedXml', true );
65
66 $this->assertEquals(
67 '<img />',
68 Html::element( 'img', null, '' ),
69 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
70 );
71 }
72
73 public function dataXmlMimeType() {
74 return [
75 // ( $mimetype, $isXmlMimeType )
76 # HTML is not an XML MimeType
77 [ 'text/html', false ],
78 # XML is an XML MimeType
79 [ 'text/xml', true ],
80 [ 'application/xml', true ],
81 # XHTML is an XML MimeType
82 [ 'application/xhtml+xml', true ],
83 # Make sure other +xml MimeTypes are supported
84 # SVG is another random MimeType even though we don't use it
85 [ 'image/svg+xml', true ],
86 # Complete random other MimeTypes are not XML
87 [ 'text/plain', false ],
88 ];
89 }
90
91 /**
92 * @dataProvider dataXmlMimeType
93 * @covers Html::isXmlMimeType
94 */
95 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
96 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
97 }
98
99 /**
100 * @covers Html::expandAttributes
101 */
102 public function testExpandAttributesSkipsNullAndFalse() {
103
104 # ## EMPTY ########
105 $this->assertEmpty(
106 Html::expandAttributes( [ 'foo' => null ] ),
107 'skip keys with null value'
108 );
109 $this->assertEmpty(
110 Html::expandAttributes( [ 'foo' => false ] ),
111 'skip keys with false value'
112 );
113 $this->assertEquals(
114 ' foo=""',
115 Html::expandAttributes( [ 'foo' => '' ] ),
116 'keep keys with an empty string'
117 );
118 }
119
120 /**
121 * @covers Html::expandAttributes
122 */
123 public function testExpandAttributesForBooleans() {
124 $this->assertEquals(
125 '',
126 Html::expandAttributes( [ 'selected' => false ] ),
127 'Boolean attributes do not generates output when value is false'
128 );
129 $this->assertEquals(
130 '',
131 Html::expandAttributes( [ 'selected' => null ] ),
132 'Boolean attributes do not generates output when value is null'
133 );
134
135 $this->assertEquals(
136 ' selected',
137 Html::expandAttributes( [ 'selected' => true ] ),
138 'Boolean attributes have no value when value is true'
139 );
140 $this->assertEquals(
141 ' selected',
142 Html::expandAttributes( [ 'selected' ] ),
143 'Boolean attributes have no value when value is true (passed as numerical array)'
144 );
145
146 $this->setMwGlobals( 'wgWellFormedXml', true );
147
148 $this->assertEquals(
149 ' selected=""',
150 Html::expandAttributes( [ 'selected' => true ] ),
151 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
152 );
153 }
154
155 /**
156 * @covers Html::expandAttributes
157 */
158 public function testExpandAttributesForNumbers() {
159 $this->assertEquals(
160 ' value=1',
161 Html::expandAttributes( [ 'value' => 1 ] ),
162 'Integer value is cast to a string'
163 );
164 $this->assertEquals(
165 ' value=1.1',
166 Html::expandAttributes( [ 'value' => 1.1 ] ),
167 'Float value is cast to a string'
168 );
169 }
170
171 /**
172 * @covers Html::expandAttributes
173 */
174 public function testExpandAttributesForObjects() {
175 $this->assertEquals(
176 ' value=stringValue',
177 Html::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
178 'Object value is converted to a string'
179 );
180 }
181
182 /**
183 * Test for Html::expandAttributes()
184 * Please note it output a string prefixed with a space!
185 * @covers Html::expandAttributes
186 */
187 public function testExpandAttributesVariousExpansions() {
188 # ## NOT EMPTY ####
189 $this->assertEquals(
190 ' empty_string=""',
191 Html::expandAttributes( [ 'empty_string' => '' ] ),
192 'Empty string is always quoted'
193 );
194 $this->assertEquals(
195 ' key=value',
196 Html::expandAttributes( [ 'key' => 'value' ] ),
197 'Simple string value needs no quotes'
198 );
199 $this->assertEquals(
200 ' one=1',
201 Html::expandAttributes( [ 'one' => 1 ] ),
202 'Number 1 value needs no quotes'
203 );
204 $this->assertEquals(
205 ' zero=0',
206 Html::expandAttributes( [ 'zero' => 0 ] ),
207 'Number 0 value needs no quotes'
208 );
209
210 $this->setMwGlobals( 'wgWellFormedXml', true );
211
212 $this->assertEquals(
213 ' empty_string=""',
214 Html::expandAttributes( [ 'empty_string' => '' ] ),
215 'Attribute values are always quoted (wgWellFormedXml): Empty string'
216 );
217 $this->assertEquals(
218 ' key="value"',
219 Html::expandAttributes( [ 'key' => 'value' ] ),
220 'Attribute values are always quoted (wgWellFormedXml): Simple string'
221 );
222 $this->assertEquals(
223 ' one="1"',
224 Html::expandAttributes( [ 'one' => 1 ] ),
225 'Attribute values are always quoted (wgWellFormedXml): Number 1'
226 );
227 $this->assertEquals(
228 ' zero="0"',
229 Html::expandAttributes( [ 'zero' => 0 ] ),
230 'Attribute values are always quoted (wgWellFormedXml): Number 0'
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 later 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 */
346 public function testNamespaceSelector() {
347 $this->assertEquals(
348 '<select id=namespace name=namespace>' . "\n" .
349 '<option value=0>(Main)</option>' . "\n" .
350 '<option value=1>Talk</option>' . "\n" .
351 '<option value=2>User</option>' . "\n" .
352 '<option value=3>User talk</option>' . "\n" .
353 '<option value=4>MyWiki</option>' . "\n" .
354 '<option value=5>MyWiki Talk</option>' . "\n" .
355 '<option value=6>File</option>' . "\n" .
356 '<option value=7>File talk</option>' . "\n" .
357 '<option value=8>MediaWiki</option>' . "\n" .
358 '<option value=9>MediaWiki talk</option>' . "\n" .
359 '<option value=10>Template</option>' . "\n" .
360 '<option value=11>Template talk</option>' . "\n" .
361 '<option value=14>Category</option>' . "\n" .
362 '<option value=15>Category talk</option>' . "\n" .
363 '<option value=100>Custom</option>' . "\n" .
364 '<option value=101>Custom talk</option>' . "\n" .
365 '</select>',
366 Html::namespaceSelector(),
367 'Basic namespace selector without custom options'
368 );
369
370 $this->assertEquals(
371 '<label for=mw-test-namespace>Select a namespace:</label>&#160;' .
372 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
373 '<option value=all>all</option>' . "\n" .
374 '<option value=0>(Main)</option>' . "\n" .
375 '<option value=1>Talk</option>' . "\n" .
376 '<option value=2 selected>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 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
393 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
394 ),
395 'Basic namespace selector with custom values'
396 );
397
398 $this->assertEquals(
399 '<label for=namespace>Select a namespace:</label>&#160;' .
400 '<select id=namespace name=namespace>' . "\n" .
401 '<option value=0>(Main)</option>' . "\n" .
402 '<option value=1>Talk</option>' . "\n" .
403 '<option value=2>User</option>' . "\n" .
404 '<option value=3>User talk</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 '<option value=100>Custom</option>' . "\n" .
416 '<option value=101>Custom talk</option>' . "\n" .
417 '</select>',
418 Html::namespaceSelector(
419 [ 'label' => 'Select a namespace:' ]
420 ),
421 'Basic namespace selector with a custom label but no id attribtue for the <select>'
422 );
423 }
424
425 public function testCanFilterOutNamespaces() {
426 $this->assertEquals(
427 '<select id=namespace name=namespace>' . "\n" .
428 '<option value=2>User</option>' . "\n" .
429 '<option value=4>MyWiki</option>' . "\n" .
430 '<option value=5>MyWiki Talk</option>' . "\n" .
431 '<option value=6>File</option>' . "\n" .
432 '<option value=7>File talk</option>' . "\n" .
433 '<option value=8>MediaWiki</option>' . "\n" .
434 '<option value=9>MediaWiki talk</option>' . "\n" .
435 '<option value=10>Template</option>' . "\n" .
436 '<option value=11>Template talk</option>' . "\n" .
437 '<option value=14>Category</option>' . "\n" .
438 '<option value=15>Category talk</option>' . "\n" .
439 '</select>',
440 Html::namespaceSelector(
441 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
442 ),
443 'Namespace selector namespace filtering.'
444 );
445 }
446
447 public function testCanDisableANamespaces() {
448 $this->assertEquals(
449 '<select id=namespace name=namespace>' . "\n" .
450 '<option disabled value=0>(Main)</option>' . "\n" .
451 '<option disabled value=1>Talk</option>' . "\n" .
452 '<option disabled value=2>User</option>' . "\n" .
453 '<option disabled value=3>User talk</option>' . "\n" .
454 '<option disabled value=4>MyWiki</option>' . "\n" .
455 '<option value=5>MyWiki Talk</option>' . "\n" .
456 '<option value=6>File</option>' . "\n" .
457 '<option value=7>File talk</option>' . "\n" .
458 '<option value=8>MediaWiki</option>' . "\n" .
459 '<option value=9>MediaWiki talk</option>' . "\n" .
460 '<option value=10>Template</option>' . "\n" .
461 '<option value=11>Template talk</option>' . "\n" .
462 '<option value=14>Category</option>' . "\n" .
463 '<option value=15>Category talk</option>' . "\n" .
464 '<option value=100>Custom</option>' . "\n" .
465 '<option value=101>Custom talk</option>' . "\n" .
466 '</select>',
467 Html::namespaceSelector( [
468 'disable' => [ 0, 1, 2, 3, 4 ]
469 ] ),
470 'Namespace selector namespace disabling'
471 );
472 }
473
474 /**
475 * @dataProvider provideHtml5InputTypes
476 * @covers Html::element
477 */
478 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
479 $this->assertEquals(
480 '<input type=' . $HTML5InputType . '>',
481 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
482 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
483 );
484 }
485
486 /**
487 * List of input element types values introduced by HTML5
488 * Full list at http://www.w3.org/TR/html-markup/input.html
489 */
490 public static function provideHtml5InputTypes() {
491 $types = [
492 'datetime',
493 'datetime-local',
494 'date',
495 'month',
496 'time',
497 'week',
498 'number',
499 'range',
500 'email',
501 'url',
502 'search',
503 'tel',
504 'color',
505 ];
506 $cases = [];
507 foreach ( $types as $type ) {
508 $cases[] = [ $type ];
509 }
510
511 return $cases;
512 }
513
514 /**
515 * Test out Html::element drops or enforces default value
516 * @covers Html::dropDefaults
517 * @dataProvider provideElementsWithAttributesHavingDefaultValues
518 */
519 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
520 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
521 }
522
523 public static function provideElementsWithAttributesHavingDefaultValues() {
524 # Use cases in a concise format:
525 # <expected>, <element name>, <array of attributes> [, <message>]
526 # Will be mapped to Html::element()
527 $cases = [];
528
529 # ## Generic cases, match $attribDefault static array
530 $cases[] = [ '<area>',
531 'area', [ 'shape' => 'rect' ]
532 ];
533
534 $cases[] = [ '<button type=submit></button>',
535 'button', [ 'formaction' => 'GET' ]
536 ];
537 $cases[] = [ '<button type=submit></button>',
538 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
539 ];
540
541 $cases[] = [ '<canvas></canvas>',
542 'canvas', [ 'height' => '150' ]
543 ];
544 $cases[] = [ '<canvas></canvas>',
545 'canvas', [ 'width' => '300' ]
546 ];
547 # Also check with numeric values
548 $cases[] = [ '<canvas></canvas>',
549 'canvas', [ 'height' => 150 ]
550 ];
551 $cases[] = [ '<canvas></canvas>',
552 'canvas', [ 'width' => 300 ]
553 ];
554
555 $cases[] = [ '<command>',
556 'command', [ 'type' => 'command' ]
557 ];
558
559 $cases[] = [ '<form></form>',
560 'form', [ 'action' => 'GET' ]
561 ];
562 $cases[] = [ '<form></form>',
563 'form', [ 'autocomplete' => 'on' ]
564 ];
565 $cases[] = [ '<form></form>',
566 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
567 ];
568
569 $cases[] = [ '<input>',
570 'input', [ 'formaction' => 'GET' ]
571 ];
572 $cases[] = [ '<input>',
573 'input', [ 'type' => 'text' ]
574 ];
575
576 $cases[] = [ '<keygen>',
577 'keygen', [ 'keytype' => 'rsa' ]
578 ];
579
580 $cases[] = [ '<link>',
581 'link', [ 'media' => 'all' ]
582 ];
583
584 $cases[] = [ '<menu></menu>',
585 'menu', [ 'type' => 'list' ]
586 ];
587
588 $cases[] = [ '<script></script>',
589 'script', [ 'type' => 'text/javascript' ]
590 ];
591
592 $cases[] = [ '<style></style>',
593 'style', [ 'media' => 'all' ]
594 ];
595 $cases[] = [ '<style></style>',
596 'style', [ 'type' => 'text/css' ]
597 ];
598
599 $cases[] = [ '<textarea></textarea>',
600 'textarea', [ 'wrap' => 'soft' ]
601 ];
602
603 # ## SPECIFIC CASES
604
605 # <link type="text/css">
606 $cases[] = [ '<link>',
607 'link', [ 'type' => 'text/css' ]
608 ];
609
610 # <input> specific handling
611 $cases[] = [ '<input type=checkbox>',
612 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
613 'Default value "on" is stripped of checkboxes',
614 ];
615 $cases[] = [ '<input type=radio>',
616 'input', [ 'type' => 'radio', 'value' => 'on' ],
617 'Default value "on" is stripped of radio buttons',
618 ];
619 $cases[] = [ '<input type=submit value=Submit>',
620 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
621 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
622 ];
623 $cases[] = [ '<input type=color>',
624 'input', [ 'type' => 'color', 'value' => '' ],
625 ];
626 $cases[] = [ '<input type=range>',
627 'input', [ 'type' => 'range', 'value' => '' ],
628 ];
629
630 # <button> specific handling
631 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
632 $cases[] = [ '<button type=submit></button>',
633 'button', [ 'type' => 'submit' ],
634 'According to standard the default type is "submit". '
635 . 'Depending on compatibility mode IE might use "button", instead.',
636 ];
637
638 # <select> specific handling
639 $cases[] = [ '<select multiple></select>',
640 'select', [ 'size' => '4', 'multiple' => true ],
641 ];
642 # .. with numeric value
643 $cases[] = [ '<select multiple></select>',
644 'select', [ 'size' => 4, 'multiple' => true ],
645 ];
646 $cases[] = [ '<select></select>',
647 'select', [ 'size' => '1', 'multiple' => false ],
648 ];
649 # .. with numeric value
650 $cases[] = [ '<select></select>',
651 'select', [ 'size' => 1, 'multiple' => false ],
652 ];
653
654 # Passing an array as value
655 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
656 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
657 "dropDefaults accepts values given as an array"
658 ];
659
660 # FIXME: doDropDefault should remove defaults given in an array
661 # Expected should be '<a></a>'
662 $cases[] = [ '<a class=""></a>',
663 'a', [ 'class' => [ '', '' ] ],
664 "dropDefaults accepts values given as an array"
665 ];
666
667 # Craft the Html elements
668 $ret = [];
669 foreach ( $cases as $case ) {
670 $ret[] = [
671 $case[0],
672 $case[1], $case[2],
673 isset( $case[3] ) ? $case[3] : ''
674 ];
675 }
676
677 return $ret;
678 }
679
680 /**
681 * @covers Html::expandAttributes
682 */
683 public function testFormValidationBlacklist() {
684 $this->assertEmpty(
685 Html::expandAttributes( [
686 'min' => 1,
687 'max' => 100,
688 'pattern' => 'abc',
689 'required' => true,
690 'step' => 2
691 ] ),
692 'Blacklist form validation attributes.'
693 );
694 $this->assertEquals(
695 ' step=any',
696 Html::expandAttributes(
697 [
698 'min' => 1,
699 'max' => 100,
700 'pattern' => 'abc',
701 'required' => true,
702 'step' => 'any'
703 ],
704 'Allow special case "step=any".'
705 )
706 );
707 }
708
709 public function testWrapperInput() {
710 $this->assertEquals(
711 '<input type=radio value=testval name=testname>',
712 Html::input( 'testname', 'testval', 'radio' ),
713 'Input wrapper with type and value.'
714 );
715 $this->assertEquals(
716 '<input name=testname>',
717 Html::input( 'testname' ),
718 'Input wrapper with all default values.'
719 );
720 }
721
722 public function testWrapperCheck() {
723 $this->assertEquals(
724 '<input type=checkbox value=1 name=testname>',
725 Html::check( 'testname' ),
726 'Checkbox wrapper unchecked.'
727 );
728 $this->assertEquals(
729 '<input checked type=checkbox value=1 name=testname>',
730 Html::check( 'testname', true ),
731 'Checkbox wrapper checked.'
732 );
733 $this->assertEquals(
734 '<input type=checkbox value=testval name=testname>',
735 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
736 'Checkbox wrapper with a value override.'
737 );
738 }
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 public function testWrapperLabel() {
759 $this->assertEquals(
760 '<label for=testid>testlabel</label>',
761 Html::label( 'testlabel', 'testid' ),
762 'Label wrapper'
763 );
764 }
765
766 public static function provideSrcSetImages() {
767 return [
768 [ [], '', 'when there are no images, return empty string' ],
769 [
770 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
771 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
772 'pixel depth keys may include a trailing "x"'
773 ],
774 [
775 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
776 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
777 'pixel depth keys may omit a trailing "x"'
778 ],
779 ];
780 }
781
782 /**
783 * @dataProvider provideSrcSetImages
784 * @covers Html::srcSet
785 */
786 public function testSrcSet( $images, $expected, $message ) {
787 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
788 }
789 }
790
791 class HtmlTestValue {
792 function __toString() {
793 return 'stringValue';
794 }
795 }