2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
6 protected function setUp() {
10 'wgUseMediaWikiUIEverywhere' => false,
13 $langObj = Language
::factory( 'en' );
15 // Hardcode namespaces during test runs,
16 // so that html output based on existing namespaces
17 // can be properly evaluated.
18 $langObj->setNamespaces( [
30 9 => 'MediaWiki_talk',
32 11 => 'Template_talk',
34 15 => 'Category_talk',
38 $this->setUserLang( $langObj );
39 $this->setContentLang( $langObj );
43 * @covers Html::element
45 public function testElementBasics() {
48 Html
::element( 'img', null, '' ),
49 'Self-closing tag for short-tag elements'
53 '<element></element>',
54 Html
::element( 'element', null, null ),
55 'Close tag for empty element (null, null)'
59 '<element></element>',
60 Html
::element( 'element', [], '' ),
61 'Close tag for empty element (array, string)'
65 public function dataXmlMimeType() {
67 // ( $mimetype, $isXmlMimeType )
68 # HTML is not an XML MimeType
69 [ 'text/html', false ],
70 # XML is an XML MimeType
72 [ 'application/xml', true ],
73 # XHTML is an XML MimeType
74 [ 'application/xhtml+xml', true ],
75 # Make sure other +xml MimeTypes are supported
76 # SVG is another random MimeType even though we don't use it
77 [ 'image/svg+xml', true ],
78 # Complete random other MimeTypes are not XML
79 [ 'text/plain', false ],
84 * @dataProvider dataXmlMimeType
85 * @covers Html::isXmlMimeType
87 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
88 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
92 * @covers Html::expandAttributes
94 public function testExpandAttributesSkipsNullAndFalse() {
97 Html
::expandAttributes( [ 'foo' => null ] ),
98 'skip keys with null value'
101 Html
::expandAttributes( [ 'foo' => false ] ),
102 'skip keys with false value'
106 Html
::expandAttributes( [ 'foo' => '' ] ),
107 'keep keys with an empty string'
112 * @covers Html::expandAttributes
114 public function testExpandAttributesForBooleans() {
117 Html
::expandAttributes( [ 'selected' => false ] ),
118 'Boolean attributes do not generates output when value is false'
122 Html
::expandAttributes( [ 'selected' => null ] ),
123 'Boolean attributes do not generates output when value is null'
128 Html
::expandAttributes( [ 'selected' => true ] ),
129 'Boolean attributes have no value when value is true'
133 Html
::expandAttributes( [ 'selected' ] ),
134 'Boolean attributes have no value when value is true (passed as numerical array)'
139 * @covers Html::expandAttributes
141 public function testExpandAttributesForNumbers() {
144 Html
::expandAttributes( [ 'value' => 1 ] ),
145 'Integer value is cast to a string'
149 Html
::expandAttributes( [ 'value' => 1.1 ] ),
150 'Float value is cast to a string'
155 * @covers Html::expandAttributes
157 public function testExpandAttributesForObjects() {
159 ' value="stringValue"',
160 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
161 'Object value is converted to a string'
166 * Test for Html::expandAttributes()
167 * Please note it output a string prefixed with a space!
168 * @covers Html::expandAttributes
170 public function testExpandAttributesVariousExpansions() {
174 Html
::expandAttributes( [ 'empty_string' => '' ] ),
175 'Empty string is always quoted'
179 Html
::expandAttributes( [ 'key' => 'value' ] ),
180 'Simple string value needs no quotes'
184 Html
::expandAttributes( [ 'one' => 1 ] ),
185 'Number 1 value needs no quotes'
189 Html
::expandAttributes( [ 'zero' => 0 ] ),
190 'Number 0 value needs no quotes'
195 * Html::expandAttributes has special features for HTML
196 * attributes that use space separated lists and also
197 * allows arrays to be used as values.
198 * @covers Html::expandAttributes
200 public function testExpandAttributesListValueAttributes() {
203 ' class="redundant spaces here"',
204 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
205 'Normalization should strip redundant spaces'
209 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
210 'Normalization should remove duplicates in string-lists'
212 # ## "EMPTY" ARRAY VALUES
215 Html
::expandAttributes( [ 'class' => [] ] ),
216 'Value with an empty array'
220 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
221 'Array with null, empty string and spaces'
223 # ## NON-EMPTY ARRAY VALUES
226 Html
::expandAttributes( [ 'class' => [
233 'Normalization should remove duplicates in the array'
237 Html
::expandAttributes( [ 'class' => [
243 'Normalization should remove duplicates in string-lists in the array'
248 * Test feature added by r96188, let pass attributes values as
249 * a PHP array. Restricted to class,rel, accesskey.
250 * @covers Html::expandAttributes
252 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
254 ' class="booltrue one"',
255 Html
::expandAttributes( [ 'class' => [
259 # Method use isset() internally, make sure we do discard
260 # attributes values which have been assigned well known values
262 'boolfalse' => false,
270 * How do we handle duplicate keys in HTML attributes expansion?
271 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
272 * The latter will take precedence.
274 * Feature added by r96188
275 * @covers Html::expandAttributes
277 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
280 Html
::expandAttributes( [ 'class' => [
289 * @covers Html::expandAttributes
290 * @expectedException MWException
292 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
293 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
294 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
295 Html
::expandAttributes( [
304 * @covers Html::namespaceSelector
306 public function testNamespaceSelector() {
308 '<select id="namespace" name="namespace">' . "\n" .
309 '<option value="0">(Main)</option>' . "\n" .
310 '<option value="1">Talk</option>' . "\n" .
311 '<option value="2">User</option>' . "\n" .
312 '<option value="3">User talk</option>' . "\n" .
313 '<option value="4">MyWiki</option>' . "\n" .
314 '<option value="5">MyWiki Talk</option>' . "\n" .
315 '<option value="6">File</option>' . "\n" .
316 '<option value="7">File talk</option>' . "\n" .
317 '<option value="8">MediaWiki</option>' . "\n" .
318 '<option value="9">MediaWiki talk</option>' . "\n" .
319 '<option value="10">Template</option>' . "\n" .
320 '<option value="11">Template talk</option>' . "\n" .
321 '<option value="14">Category</option>' . "\n" .
322 '<option value="15">Category talk</option>' . "\n" .
323 '<option value="100">Custom</option>' . "\n" .
324 '<option value="101">Custom talk</option>' . "\n" .
326 Html
::namespaceSelector(),
327 'Basic namespace selector without custom options'
331 '<label for="mw-test-namespace">Select a namespace:</label> ' .
332 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
333 '<option value="all">all</option>' . "\n" .
334 '<option value="0">(Main)</option>' . "\n" .
335 '<option value="1">Talk</option>' . "\n" .
336 '<option value="2" selected="">User</option>' . "\n" .
337 '<option value="3">User talk</option>' . "\n" .
338 '<option value="4">MyWiki</option>' . "\n" .
339 '<option value="5">MyWiki Talk</option>' . "\n" .
340 '<option value="6">File</option>' . "\n" .
341 '<option value="7">File talk</option>' . "\n" .
342 '<option value="8">MediaWiki</option>' . "\n" .
343 '<option value="9">MediaWiki talk</option>' . "\n" .
344 '<option value="10">Template</option>' . "\n" .
345 '<option value="11">Template talk</option>' . "\n" .
346 '<option value="14">Category</option>' . "\n" .
347 '<option value="15">Category talk</option>' . "\n" .
348 '<option value="100">Custom</option>' . "\n" .
349 '<option value="101">Custom talk</option>' . "\n" .
351 Html
::namespaceSelector(
352 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
353 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
355 'Basic namespace selector with custom values'
359 '<label for="namespace">Select a namespace:</label> ' .
360 '<select id="namespace" name="namespace">' . "\n" .
361 '<option value="0">(Main)</option>' . "\n" .
362 '<option value="1">Talk</option>' . "\n" .
363 '<option value="2">User</option>' . "\n" .
364 '<option value="3">User talk</option>' . "\n" .
365 '<option value="4">MyWiki</option>' . "\n" .
366 '<option value="5">MyWiki Talk</option>' . "\n" .
367 '<option value="6">File</option>' . "\n" .
368 '<option value="7">File talk</option>' . "\n" .
369 '<option value="8">MediaWiki</option>' . "\n" .
370 '<option value="9">MediaWiki talk</option>' . "\n" .
371 '<option value="10">Template</option>' . "\n" .
372 '<option value="11">Template talk</option>' . "\n" .
373 '<option value="14">Category</option>' . "\n" .
374 '<option value="15">Category talk</option>' . "\n" .
375 '<option value="100">Custom</option>' . "\n" .
376 '<option value="101">Custom talk</option>' . "\n" .
378 Html
::namespaceSelector(
379 [ 'label' => 'Select a namespace:' ]
381 'Basic namespace selector with a custom label but no id attribtue for the <select>'
385 public function testCanFilterOutNamespaces() {
387 '<select id="namespace" name="namespace">' . "\n" .
388 '<option value="2">User</option>' . "\n" .
389 '<option value="4">MyWiki</option>' . "\n" .
390 '<option value="5">MyWiki Talk</option>' . "\n" .
391 '<option value="6">File</option>' . "\n" .
392 '<option value="7">File talk</option>' . "\n" .
393 '<option value="8">MediaWiki</option>' . "\n" .
394 '<option value="9">MediaWiki talk</option>' . "\n" .
395 '<option value="10">Template</option>' . "\n" .
396 '<option value="11">Template talk</option>' . "\n" .
397 '<option value="14">Category</option>' . "\n" .
398 '<option value="15">Category talk</option>' . "\n" .
400 Html
::namespaceSelector(
401 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
403 'Namespace selector namespace filtering.'
407 public function testCanDisableANamespaces() {
409 '<select id="namespace" name="namespace">' . "\n" .
410 '<option disabled="" value="0">(Main)</option>' . "\n" .
411 '<option disabled="" value="1">Talk</option>' . "\n" .
412 '<option disabled="" value="2">User</option>' . "\n" .
413 '<option disabled="" value="3">User talk</option>' . "\n" .
414 '<option disabled="" value="4">MyWiki</option>' . "\n" .
415 '<option value="5">MyWiki Talk</option>' . "\n" .
416 '<option value="6">File</option>' . "\n" .
417 '<option value="7">File talk</option>' . "\n" .
418 '<option value="8">MediaWiki</option>' . "\n" .
419 '<option value="9">MediaWiki talk</option>' . "\n" .
420 '<option value="10">Template</option>' . "\n" .
421 '<option value="11">Template talk</option>' . "\n" .
422 '<option value="14">Category</option>' . "\n" .
423 '<option value="15">Category talk</option>' . "\n" .
424 '<option value="100">Custom</option>' . "\n" .
425 '<option value="101">Custom talk</option>' . "\n" .
427 Html
::namespaceSelector( [
428 'disable' => [ 0, 1, 2, 3, 4 ]
430 'Namespace selector namespace disabling'
435 * @dataProvider provideHtml5InputTypes
436 * @covers Html::element
438 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
440 '<input type="' . $HTML5InputType . '"/>',
441 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
442 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
447 * List of input element types values introduced by HTML5
448 * Full list at https://www.w3.org/TR/html-markup/input.html
450 public static function provideHtml5InputTypes() {
467 foreach ( $types as $type ) {
468 $cases[] = [ $type ];
475 * Test out Html::element drops or enforces default value
476 * @covers Html::dropDefaults
477 * @dataProvider provideElementsWithAttributesHavingDefaultValues
479 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
480 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
483 public static function provideElementsWithAttributesHavingDefaultValues() {
484 # Use cases in a concise format:
485 # <expected>, <element name>, <array of attributes> [, <message>]
486 # Will be mapped to Html::element()
489 # ## Generic cases, match $attribDefault static array
490 $cases[] = [ '<area/>',
491 'area', [ 'shape' => 'rect' ]
494 $cases[] = [ '<button type="submit"></button>',
495 'button', [ 'formaction' => 'GET' ]
497 $cases[] = [ '<button type="submit"></button>',
498 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
501 $cases[] = [ '<canvas></canvas>',
502 'canvas', [ 'height' => '150' ]
504 $cases[] = [ '<canvas></canvas>',
505 'canvas', [ 'width' => '300' ]
507 # Also check with numeric values
508 $cases[] = [ '<canvas></canvas>',
509 'canvas', [ 'height' => 150 ]
511 $cases[] = [ '<canvas></canvas>',
512 'canvas', [ 'width' => 300 ]
515 $cases[] = [ '<form></form>',
516 'form', [ 'action' => 'GET' ]
518 $cases[] = [ '<form></form>',
519 'form', [ 'autocomplete' => 'on' ]
521 $cases[] = [ '<form></form>',
522 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
525 $cases[] = [ '<input/>',
526 'input', [ 'formaction' => 'GET' ]
528 $cases[] = [ '<input/>',
529 'input', [ 'type' => 'text' ]
532 $cases[] = [ '<keygen/>',
533 'keygen', [ 'keytype' => 'rsa' ]
536 $cases[] = [ '<link/>',
537 'link', [ 'media' => 'all' ]
540 $cases[] = [ '<menu></menu>',
541 'menu', [ 'type' => 'list' ]
544 $cases[] = [ '<script></script>',
545 'script', [ 'type' => 'text/javascript' ]
548 $cases[] = [ '<style></style>',
549 'style', [ 'media' => 'all' ]
551 $cases[] = [ '<style></style>',
552 'style', [ 'type' => 'text/css' ]
555 $cases[] = [ '<textarea></textarea>',
556 'textarea', [ 'wrap' => 'soft' ]
561 # <link type="text/css">
562 $cases[] = [ '<link/>',
563 'link', [ 'type' => 'text/css' ]
566 # <input> specific handling
567 $cases[] = [ '<input type="checkbox"/>',
568 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
569 'Default value "on" is stripped of checkboxes',
571 $cases[] = [ '<input type="radio"/>',
572 'input', [ 'type' => 'radio', 'value' => 'on' ],
573 'Default value "on" is stripped of radio buttons',
575 $cases[] = [ '<input type="submit" value="Submit"/>',
576 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
577 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
579 $cases[] = [ '<input type="color"/>',
580 'input', [ 'type' => 'color', 'value' => '' ],
582 $cases[] = [ '<input type="range"/>',
583 'input', [ 'type' => 'range', 'value' => '' ],
586 # <button> specific handling
587 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
588 $cases[] = [ '<button type="submit"></button>',
589 'button', [ 'type' => 'submit' ],
590 'According to standard the default type is "submit". '
591 . 'Depending on compatibility mode IE might use "button", instead.',
594 # <select> specific handling
595 $cases[] = [ '<select multiple=""></select>',
596 'select', [ 'size' => '4', 'multiple' => true ],
598 # .. with numeric value
599 $cases[] = [ '<select multiple=""></select>',
600 'select', [ 'size' => 4, 'multiple' => true ],
602 $cases[] = [ '<select></select>',
603 'select', [ 'size' => '1', 'multiple' => false ],
605 # .. with numeric value
606 $cases[] = [ '<select></select>',
607 'select', [ 'size' => 1, 'multiple' => false ],
610 # Passing an array as value
611 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
612 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
613 "dropDefaults accepts values given as an array"
616 # FIXME: doDropDefault should remove defaults given in an array
617 # Expected should be '<a></a>'
618 $cases[] = [ '<a class=""></a>',
619 'a', [ 'class' => [ '', '' ] ],
620 "dropDefaults accepts values given as an array"
623 # Craft the Html elements
625 foreach ( $cases as $case ) {
629 isset( $case[3] ) ?
$case[3] : ''
636 public function testWrapperInput() {
638 '<input type="radio" value="testval" name="testname"/>',
639 Html
::input( 'testname', 'testval', 'radio' ),
640 'Input wrapper with type and value.'
643 '<input name="testname"/>',
644 Html
::input( 'testname' ),
645 'Input wrapper with all default values.'
649 public function testWrapperCheck() {
651 '<input type="checkbox" value="1" name="testname"/>',
652 Html
::check( 'testname' ),
653 'Checkbox wrapper unchecked.'
656 '<input checked="" type="checkbox" value="1" name="testname"/>',
657 Html
::check( 'testname', true ),
658 'Checkbox wrapper checked.'
661 '<input type="checkbox" value="testval" name="testname"/>',
662 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
663 'Checkbox wrapper with a value override.'
667 public function testWrapperRadio() {
669 '<input type="radio" value="1" name="testname"/>',
670 Html
::radio( 'testname' ),
671 'Radio wrapper unchecked.'
674 '<input checked="" type="radio" value="1" name="testname"/>',
675 Html
::radio( 'testname', true ),
676 'Radio wrapper checked.'
679 '<input type="radio" value="testval" name="testname"/>',
680 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
681 'Radio wrapper with a value override.'
685 public function testWrapperLabel() {
687 '<label for="testid">testlabel</label>',
688 Html
::label( 'testlabel', 'testid' ),
693 public static function provideSrcSetImages() {
695 [ [], '', 'when there are no images, return empty string' ],
697 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
698 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
699 'pixel depth keys may include a trailing "x"'
702 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
703 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
704 'pixel depth keys may omit a trailing "x"'
707 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
708 'small.png 1x, large.png 1.5x',
709 'omit larger duplicates'
712 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
713 'small.png 1x, large.png 1.5x',
714 'omit larger duplicates in irregular order'
720 * @dataProvider provideSrcSetImages
721 * @covers Html::srcSet
723 public function testSrcSet( $images, $expected, $message ) {
724 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
728 class HtmlTestValue
{
729 function __toString() {
730 return 'stringValue';