Merge "Fix escaping for MSSQL LIKE queries."
[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 'wgUseMediaWikiUIEverywhere' => false,
12 ] );
13
14 $langObj = Language::factory( 'en' );
15
16 // Hardcode namespaces during test runs,
17 // so that html output based on existing namespaces
18 // can be properly evaluated.
19 $langObj->setNamespaces( [
20 -2 => 'Media',
21 -1 => 'Special',
22 0 => '',
23 1 => 'Talk',
24 2 => 'User',
25 3 => 'User_talk',
26 4 => 'MyWiki',
27 5 => 'MyWiki_Talk',
28 6 => 'File',
29 7 => 'File_talk',
30 8 => 'MediaWiki',
31 9 => 'MediaWiki_talk',
32 10 => 'Template',
33 11 => 'Template_talk',
34 14 => 'Category',
35 15 => 'Category_talk',
36 100 => 'Custom',
37 101 => 'Custom_talk',
38 ] );
39 $this->setUserLang( $langObj );
40 $this->setContentLang( $langObj );
41 }
42
43 /**
44 * @covers Html::element
45 */
46 public function testElementBasics() {
47 $this->assertEquals(
48 '<img>',
49 Html::element( 'img', null, '' ),
50 'No close tag for short-tag elements'
51 );
52
53 $this->assertEquals(
54 '<element></element>',
55 Html::element( 'element', null, null ),
56 'Close tag for empty element (null, null)'
57 );
58
59 $this->assertEquals(
60 '<element></element>',
61 Html::element( 'element', [], '' ),
62 'Close tag for empty element (array, string)'
63 );
64
65 $this->setMwGlobals( 'wgWellFormedXml', true );
66
67 $this->assertEquals(
68 '<img/>',
69 Html::element( 'img', null, '' ),
70 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
71 );
72 }
73
74 public function dataXmlMimeType() {
75 return [
76 // ( $mimetype, $isXmlMimeType )
77 # HTML is not an XML MimeType
78 [ 'text/html', false ],
79 # XML is an XML MimeType
80 [ 'text/xml', true ],
81 [ 'application/xml', true ],
82 # XHTML is an XML MimeType
83 [ 'application/xhtml+xml', true ],
84 # Make sure other +xml MimeTypes are supported
85 # SVG is another random MimeType even though we don't use it
86 [ 'image/svg+xml', true ],
87 # Complete random other MimeTypes are not XML
88 [ 'text/plain', false ],
89 ];
90 }
91
92 /**
93 * @dataProvider dataXmlMimeType
94 * @covers Html::isXmlMimeType
95 */
96 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
97 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) );
98 }
99
100 /**
101 * @covers Html::expandAttributes
102 */
103 public function testExpandAttributesSkipsNullAndFalse() {
104
105 # ## EMPTY ########
106 $this->assertEmpty(
107 Html::expandAttributes( [ 'foo' => null ] ),
108 'skip keys with null value'
109 );
110 $this->assertEmpty(
111 Html::expandAttributes( [ 'foo' => false ] ),
112 'skip keys with false value'
113 );
114 $this->assertEquals(
115 ' foo=""',
116 Html::expandAttributes( [ 'foo' => '' ] ),
117 'keep keys with an empty string'
118 );
119 }
120
121 /**
122 * @covers Html::expandAttributes
123 */
124 public function testExpandAttributesForBooleans() {
125 $this->assertEquals(
126 '',
127 Html::expandAttributes( [ 'selected' => false ] ),
128 'Boolean attributes do not generates output when value is false'
129 );
130 $this->assertEquals(
131 '',
132 Html::expandAttributes( [ 'selected' => null ] ),
133 'Boolean attributes do not generates output when value is null'
134 );
135
136 $this->assertEquals(
137 ' selected',
138 Html::expandAttributes( [ 'selected' => true ] ),
139 'Boolean attributes have no value when value is true'
140 );
141 $this->assertEquals(
142 ' selected',
143 Html::expandAttributes( [ 'selected' ] ),
144 'Boolean attributes have no value when value is true (passed as numerical array)'
145 );
146
147 $this->setMwGlobals( 'wgWellFormedXml', true );
148
149 $this->assertEquals(
150 ' selected=""',
151 Html::expandAttributes( [ 'selected' => true ] ),
152 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
153 );
154 }
155
156 /**
157 * @covers Html::expandAttributes
158 */
159 public function testExpandAttributesForNumbers() {
160 $this->assertEquals(
161 ' value=1',
162 Html::expandAttributes( [ 'value' => 1 ] ),
163 'Integer value is cast to a string'
164 );
165 $this->assertEquals(
166 ' value=1.1',
167 Html::expandAttributes( [ 'value' => 1.1 ] ),
168 'Float value is cast to a string'
169 );
170 }
171
172 /**
173 * @covers Html::expandAttributes
174 */
175 public function testExpandAttributesForObjects() {
176 $this->assertEquals(
177 ' value=stringValue',
178 Html::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
179 'Object value is converted to a string'
180 );
181 }
182
183 /**
184 * Test for Html::expandAttributes()
185 * Please note it output a string prefixed with a space!
186 * @covers Html::expandAttributes
187 */
188 public function testExpandAttributesVariousExpansions() {
189 # ## NOT EMPTY ####
190 $this->assertEquals(
191 ' empty_string=""',
192 Html::expandAttributes( [ 'empty_string' => '' ] ),
193 'Empty string is always quoted'
194 );
195 $this->assertEquals(
196 ' key=value',
197 Html::expandAttributes( [ 'key' => 'value' ] ),
198 'Simple string value needs no quotes'
199 );
200 $this->assertEquals(
201 ' one=1',
202 Html::expandAttributes( [ 'one' => 1 ] ),
203 'Number 1 value needs no quotes'
204 );
205 $this->assertEquals(
206 ' zero=0',
207 Html::expandAttributes( [ 'zero' => 0 ] ),
208 'Number 0 value needs no quotes'
209 );
210
211 $this->setMwGlobals( 'wgWellFormedXml', true );
212
213 $this->assertEquals(
214 ' empty_string=""',
215 Html::expandAttributes( [ 'empty_string' => '' ] ),
216 'Attribute values are always quoted (wgWellFormedXml): Empty string'
217 );
218 $this->assertEquals(
219 ' key="value"',
220 Html::expandAttributes( [ 'key' => 'value' ] ),
221 'Attribute values are always quoted (wgWellFormedXml): Simple string'
222 );
223 $this->assertEquals(
224 ' one="1"',
225 Html::expandAttributes( [ 'one' => 1 ] ),
226 'Attribute values are always quoted (wgWellFormedXml): Number 1'
227 );
228 $this->assertEquals(
229 ' zero="0"',
230 Html::expandAttributes( [ 'zero' => 0 ] ),
231 'Attribute values are always quoted (wgWellFormedXml): Number 0'
232 );
233 }
234
235 /**
236 * Html::expandAttributes has special features for HTML
237 * attributes that use space separated lists and also
238 * allows arrays to be used as values.
239 * @covers Html::expandAttributes
240 */
241 public function testExpandAttributesListValueAttributes() {
242 # ## STRING VALUES
243 $this->assertEquals(
244 ' class="redundant spaces here"',
245 Html::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
246 'Normalization should strip redundant spaces'
247 );
248 $this->assertEquals(
249 ' class="foo bar"',
250 Html::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
251 'Normalization should remove duplicates in string-lists'
252 );
253 # ## "EMPTY" ARRAY VALUES
254 $this->assertEquals(
255 ' class=""',
256 Html::expandAttributes( [ 'class' => [] ] ),
257 'Value with an empty array'
258 );
259 $this->assertEquals(
260 ' class=""',
261 Html::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
262 'Array with null, empty string and spaces'
263 );
264 # ## NON-EMPTY ARRAY VALUES
265 $this->assertEquals(
266 ' class="foo bar"',
267 Html::expandAttributes( [ 'class' => [
268 'foo',
269 'bar',
270 'foo',
271 'bar',
272 'bar',
273 ] ] ),
274 'Normalization should remove duplicates in the array'
275 );
276 $this->assertEquals(
277 ' class="foo bar"',
278 Html::expandAttributes( [ 'class' => [
279 'foo bar',
280 'bar foo',
281 'foo',
282 'bar bar',
283 ] ] ),
284 'Normalization should remove duplicates in string-lists in the array'
285 );
286 }
287
288 /**
289 * Test feature added by r96188, let pass attributes values as
290 * a PHP array. Restricted to class,rel, accesskey.
291 * @covers Html::expandAttributes
292 */
293 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
294 $this->assertEquals(
295 ' class="booltrue one"',
296 Html::expandAttributes( [ 'class' => [
297 'booltrue' => true,
298 'one' => 1,
299
300 # Method use isset() internally, make sure we do discard
301 # attributes values which have been assigned well known values
302 'emptystring' => '',
303 'boolfalse' => false,
304 'zero' => 0,
305 'null' => null,
306 ] ] )
307 );
308 }
309
310 /**
311 * How do we handle duplicate keys in HTML attributes expansion?
312 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
313 * The later will take precedence.
314 *
315 * Feature added by r96188
316 * @covers Html::expandAttributes
317 */
318 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
319 $this->assertEquals(
320 ' class=""',
321 Html::expandAttributes( [ 'class' => [
322 'GREEN',
323 'GREEN' => false,
324 'GREEN',
325 ] ] )
326 );
327 }
328
329 /**
330 * @covers Html::expandAttributes
331 * @expectedException MWException
332 */
333 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
334 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
335 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
336 Html::expandAttributes( [
337 'src' => [
338 'ltr' => 'ltr.svg',
339 'rtl' => 'rtl.svg'
340 ]
341 ] );
342 }
343
344 /**
345 * @covers Html::namespaceSelector
346 */
347 public function testNamespaceSelector() {
348 $this->assertEquals(
349 '<select id=namespace name=namespace>' . "\n" .
350 '<option value=0>(Main)</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>&#160;' .
373 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
374 '<option value=all>all</option>' . "\n" .
375 '<option value=0>(Main)</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>&#160;' .
401 '<select id=namespace name=namespace>' . "\n" .
402 '<option value=0>(Main)</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 public function testCanFilterOutNamespaces() {
427 $this->assertEquals(
428 '<select id=namespace name=namespace>' . "\n" .
429 '<option value=2>User</option>' . "\n" .
430 '<option value=4>MyWiki</option>' . "\n" .
431 '<option value=5>MyWiki Talk</option>' . "\n" .
432 '<option value=6>File</option>' . "\n" .
433 '<option value=7>File talk</option>' . "\n" .
434 '<option value=8>MediaWiki</option>' . "\n" .
435 '<option value=9>MediaWiki talk</option>' . "\n" .
436 '<option value=10>Template</option>' . "\n" .
437 '<option value=11>Template talk</option>' . "\n" .
438 '<option value=14>Category</option>' . "\n" .
439 '<option value=15>Category talk</option>' . "\n" .
440 '</select>',
441 Html::namespaceSelector(
442 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
443 ),
444 'Namespace selector namespace filtering.'
445 );
446 }
447
448 public function testCanDisableANamespaces() {
449 $this->assertEquals(
450 '<select id=namespace name=namespace>' . "\n" .
451 '<option disabled value=0>(Main)</option>' . "\n" .
452 '<option disabled value=1>Talk</option>' . "\n" .
453 '<option disabled value=2>User</option>' . "\n" .
454 '<option disabled value=3>User talk</option>' . "\n" .
455 '<option disabled value=4>MyWiki</option>' . "\n" .
456 '<option value=5>MyWiki Talk</option>' . "\n" .
457 '<option value=6>File</option>' . "\n" .
458 '<option value=7>File talk</option>' . "\n" .
459 '<option value=8>MediaWiki</option>' . "\n" .
460 '<option value=9>MediaWiki talk</option>' . "\n" .
461 '<option value=10>Template</option>' . "\n" .
462 '<option value=11>Template talk</option>' . "\n" .
463 '<option value=14>Category</option>' . "\n" .
464 '<option value=15>Category talk</option>' . "\n" .
465 '<option value=100>Custom</option>' . "\n" .
466 '<option value=101>Custom talk</option>' . "\n" .
467 '</select>',
468 Html::namespaceSelector( [
469 'disable' => [ 0, 1, 2, 3, 4 ]
470 ] ),
471 'Namespace selector namespace disabling'
472 );
473 }
474
475 /**
476 * @dataProvider provideHtml5InputTypes
477 * @covers Html::element
478 */
479 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
480 $this->assertEquals(
481 '<input type=' . $HTML5InputType . '>',
482 Html::element( 'input', [ 'type' => $HTML5InputType ] ),
483 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
484 );
485 }
486
487 /**
488 * List of input element types values introduced by HTML5
489 * Full list at http://www.w3.org/TR/html-markup/input.html
490 */
491 public static function provideHtml5InputTypes() {
492 $types = [
493 'datetime',
494 'datetime-local',
495 'date',
496 'month',
497 'time',
498 'week',
499 'number',
500 'range',
501 'email',
502 'url',
503 'search',
504 'tel',
505 'color',
506 ];
507 $cases = [];
508 foreach ( $types as $type ) {
509 $cases[] = [ $type ];
510 }
511
512 return $cases;
513 }
514
515 /**
516 * Test out Html::element drops or enforces default value
517 * @covers Html::dropDefaults
518 * @dataProvider provideElementsWithAttributesHavingDefaultValues
519 */
520 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
521 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
522 }
523
524 public static function provideElementsWithAttributesHavingDefaultValues() {
525 # Use cases in a concise format:
526 # <expected>, <element name>, <array of attributes> [, <message>]
527 # Will be mapped to Html::element()
528 $cases = [];
529
530 # ## Generic cases, match $attribDefault static array
531 $cases[] = [ '<area>',
532 'area', [ 'shape' => 'rect' ]
533 ];
534
535 $cases[] = [ '<button type=submit></button>',
536 'button', [ 'formaction' => 'GET' ]
537 ];
538 $cases[] = [ '<button type=submit></button>',
539 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
540 ];
541
542 $cases[] = [ '<canvas></canvas>',
543 'canvas', [ 'height' => '150' ]
544 ];
545 $cases[] = [ '<canvas></canvas>',
546 'canvas', [ 'width' => '300' ]
547 ];
548 # Also check with numeric values
549 $cases[] = [ '<canvas></canvas>',
550 'canvas', [ 'height' => 150 ]
551 ];
552 $cases[] = [ '<canvas></canvas>',
553 'canvas', [ 'width' => 300 ]
554 ];
555
556 $cases[] = [ '<command>',
557 'command', [ 'type' => 'command' ]
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 http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.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 /**
682 * @covers Html::expandAttributes
683 */
684 public function testFormValidationBlacklist() {
685 $this->assertEmpty(
686 Html::expandAttributes( [
687 'min' => 1,
688 'max' => 100,
689 'pattern' => 'abc',
690 'required' => true,
691 'step' => 2
692 ] ),
693 'Blacklist form validation attributes.'
694 );
695 $this->assertEquals(
696 ' step=any',
697 Html::expandAttributes(
698 [
699 'min' => 1,
700 'max' => 100,
701 'pattern' => 'abc',
702 'required' => true,
703 'step' => 'any'
704 ],
705 'Allow special case "step=any".'
706 )
707 );
708 }
709
710 public function testWrapperInput() {
711 $this->assertEquals(
712 '<input type=radio value=testval name=testname>',
713 Html::input( 'testname', 'testval', 'radio' ),
714 'Input wrapper with type and value.'
715 );
716 $this->assertEquals(
717 '<input name=testname>',
718 Html::input( 'testname' ),
719 'Input wrapper with all default values.'
720 );
721 }
722
723 public function testWrapperCheck() {
724 $this->assertEquals(
725 '<input type=checkbox value=1 name=testname>',
726 Html::check( 'testname' ),
727 'Checkbox wrapper unchecked.'
728 );
729 $this->assertEquals(
730 '<input checked type=checkbox value=1 name=testname>',
731 Html::check( 'testname', true ),
732 'Checkbox wrapper checked.'
733 );
734 $this->assertEquals(
735 '<input type=checkbox value=testval name=testname>',
736 Html::check( 'testname', false, [ 'value' => 'testval' ] ),
737 'Checkbox wrapper with a value override.'
738 );
739 }
740
741 public function testWrapperRadio() {
742 $this->assertEquals(
743 '<input type=radio value=1 name=testname>',
744 Html::radio( 'testname' ),
745 'Radio wrapper unchecked.'
746 );
747 $this->assertEquals(
748 '<input checked type=radio value=1 name=testname>',
749 Html::radio( 'testname', true ),
750 'Radio wrapper checked.'
751 );
752 $this->assertEquals(
753 '<input type=radio value=testval name=testname>',
754 Html::radio( 'testname', false, [ 'value' => 'testval' ] ),
755 'Radio wrapper with a value override.'
756 );
757 }
758
759 public function testWrapperLabel() {
760 $this->assertEquals(
761 '<label for=testid>testlabel</label>',
762 Html::label( 'testlabel', 'testid' ),
763 'Label wrapper'
764 );
765 }
766
767 public static function provideSrcSetImages() {
768 return [
769 [ [], '', 'when there are no images, return empty string' ],
770 [
771 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
772 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
773 'pixel depth keys may include a trailing "x"'
774 ],
775 [
776 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
777 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
778 'pixel depth keys may omit a trailing "x"'
779 ],
780 ];
781 }
782
783 /**
784 * @dataProvider provideSrcSetImages
785 * @covers Html::srcSet
786 */
787 public function testSrcSet( $images, $expected, $message ) {
788 $this->assertEquals( Html::srcSet( $images ), $expected, $message );
789 }
790 }
791
792 class HtmlTestValue {
793 function __toString() {
794 return 'stringValue';
795 }
796 }