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() {
98 Html
::expandAttributes( [ 'foo' => null ] ),
99 'skip keys with null value'
102 Html
::expandAttributes( [ 'foo' => false ] ),
103 'skip keys with false value'
107 Html
::expandAttributes( [ 'foo' => '' ] ),
108 'keep keys with an empty string'
113 * @covers Html::expandAttributes
115 public function testExpandAttributesForBooleans() {
118 Html
::expandAttributes( [ 'selected' => false ] ),
119 'Boolean attributes do not generates output when value is false'
123 Html
::expandAttributes( [ 'selected' => null ] ),
124 'Boolean attributes do not generates output when value is null'
129 Html
::expandAttributes( [ 'selected' => true ] ),
130 'Boolean attributes have no value when value is true'
134 Html
::expandAttributes( [ 'selected' ] ),
135 'Boolean attributes have no value when value is true (passed as numerical array)'
140 * @covers Html::expandAttributes
142 public function testExpandAttributesForNumbers() {
145 Html
::expandAttributes( [ 'value' => 1 ] ),
146 'Integer value is cast to a string'
150 Html
::expandAttributes( [ 'value' => 1.1 ] ),
151 'Float value is cast to a string'
156 * @covers Html::expandAttributes
158 public function testExpandAttributesForObjects() {
160 ' value="stringValue"',
161 Html
::expandAttributes( [ 'value' => new HtmlTestValue() ] ),
162 'Object value is converted to a string'
167 * Test for Html::expandAttributes()
168 * Please note it output a string prefixed with a space!
169 * @covers Html::expandAttributes
171 public function testExpandAttributesVariousExpansions() {
175 Html
::expandAttributes( [ 'empty_string' => '' ] ),
176 'Empty string is always quoted'
180 Html
::expandAttributes( [ 'key' => 'value' ] ),
181 'Simple string value needs no quotes'
185 Html
::expandAttributes( [ 'one' => 1 ] ),
186 'Number 1 value needs no quotes'
190 Html
::expandAttributes( [ 'zero' => 0 ] ),
191 'Number 0 value needs no quotes'
197 * Html::expandAttributes has special features for HTML
198 * attributes that use space separated lists and also
199 * allows arrays to be used as values.
200 * @covers Html::expandAttributes
202 public function testExpandAttributesListValueAttributes() {
205 ' class="redundant spaces here"',
206 Html
::expandAttributes( [ 'class' => ' redundant spaces here ' ] ),
207 'Normalization should strip redundant spaces'
211 Html
::expandAttributes( [ 'class' => 'foo bar foo bar bar' ] ),
212 'Normalization should remove duplicates in string-lists'
214 # ## "EMPTY" ARRAY VALUES
217 Html
::expandAttributes( [ 'class' => [] ] ),
218 'Value with an empty array'
222 Html
::expandAttributes( [ 'class' => [ null, '', ' ', ' ' ] ] ),
223 'Array with null, empty string and spaces'
225 # ## NON-EMPTY ARRAY VALUES
228 Html
::expandAttributes( [ 'class' => [
235 'Normalization should remove duplicates in the array'
239 Html
::expandAttributes( [ 'class' => [
245 'Normalization should remove duplicates in string-lists in the array'
250 * Test feature added by r96188, let pass attributes values as
251 * a PHP array. Restricted to class,rel, accesskey.
252 * @covers Html::expandAttributes
254 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
256 ' class="booltrue one"',
257 Html
::expandAttributes( [ 'class' => [
261 # Method use isset() internally, make sure we do discard
262 # attributes values which have been assigned well known values
264 'boolfalse' => false,
272 * How do we handle duplicate keys in HTML attributes expansion?
273 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
274 * The latter will take precedence.
276 * Feature added by r96188
277 * @covers Html::expandAttributes
279 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
282 Html
::expandAttributes( [ 'class' => [
291 * @covers Html::expandAttributes
292 * @expectedException MWException
294 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
295 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
296 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
297 Html
::expandAttributes( [
306 * @covers Html::namespaceSelector
308 public function testNamespaceSelector() {
310 '<select id="namespace" name="namespace">' . "\n" .
311 '<option value="0">(Main)</option>' . "\n" .
312 '<option value="1">Talk</option>' . "\n" .
313 '<option value="2">User</option>' . "\n" .
314 '<option value="3">User talk</option>' . "\n" .
315 '<option value="4">MyWiki</option>' . "\n" .
316 '<option value="5">MyWiki Talk</option>' . "\n" .
317 '<option value="6">File</option>' . "\n" .
318 '<option value="7">File talk</option>' . "\n" .
319 '<option value="8">MediaWiki</option>' . "\n" .
320 '<option value="9">MediaWiki talk</option>' . "\n" .
321 '<option value="10">Template</option>' . "\n" .
322 '<option value="11">Template talk</option>' . "\n" .
323 '<option value="14">Category</option>' . "\n" .
324 '<option value="15">Category talk</option>' . "\n" .
325 '<option value="100">Custom</option>' . "\n" .
326 '<option value="101">Custom talk</option>' . "\n" .
328 Html
::namespaceSelector(),
329 'Basic namespace selector without custom options'
333 '<label for="mw-test-namespace">Select a namespace:</label> ' .
334 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
335 '<option value="all">all</option>' . "\n" .
336 '<option value="0">(Main)</option>' . "\n" .
337 '<option value="1">Talk</option>' . "\n" .
338 '<option value="2" selected="">User</option>' . "\n" .
339 '<option value="3">User talk</option>' . "\n" .
340 '<option value="4">MyWiki</option>' . "\n" .
341 '<option value="5">MyWiki Talk</option>' . "\n" .
342 '<option value="6">File</option>' . "\n" .
343 '<option value="7">File talk</option>' . "\n" .
344 '<option value="8">MediaWiki</option>' . "\n" .
345 '<option value="9">MediaWiki talk</option>' . "\n" .
346 '<option value="10">Template</option>' . "\n" .
347 '<option value="11">Template talk</option>' . "\n" .
348 '<option value="14">Category</option>' . "\n" .
349 '<option value="15">Category talk</option>' . "\n" .
350 '<option value="100">Custom</option>' . "\n" .
351 '<option value="101">Custom talk</option>' . "\n" .
353 Html
::namespaceSelector(
354 [ 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ],
355 [ 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ]
357 'Basic namespace selector with custom values'
361 '<label for="namespace">Select a namespace:</label> ' .
362 '<select id="namespace" name="namespace">' . "\n" .
363 '<option value="0">(Main)</option>' . "\n" .
364 '<option value="1">Talk</option>' . "\n" .
365 '<option value="2">User</option>' . "\n" .
366 '<option value="3">User talk</option>' . "\n" .
367 '<option value="4">MyWiki</option>' . "\n" .
368 '<option value="5">MyWiki Talk</option>' . "\n" .
369 '<option value="6">File</option>' . "\n" .
370 '<option value="7">File talk</option>' . "\n" .
371 '<option value="8">MediaWiki</option>' . "\n" .
372 '<option value="9">MediaWiki talk</option>' . "\n" .
373 '<option value="10">Template</option>' . "\n" .
374 '<option value="11">Template talk</option>' . "\n" .
375 '<option value="14">Category</option>' . "\n" .
376 '<option value="15">Category talk</option>' . "\n" .
377 '<option value="100">Custom</option>' . "\n" .
378 '<option value="101">Custom talk</option>' . "\n" .
380 Html
::namespaceSelector(
381 [ 'label' => 'Select a namespace:' ]
383 'Basic namespace selector with a custom label but no id attribtue for the <select>'
387 public function testCanFilterOutNamespaces() {
389 '<select id="namespace" name="namespace">' . "\n" .
390 '<option value="2">User</option>' . "\n" .
391 '<option value="4">MyWiki</option>' . "\n" .
392 '<option value="5">MyWiki Talk</option>' . "\n" .
393 '<option value="6">File</option>' . "\n" .
394 '<option value="7">File talk</option>' . "\n" .
395 '<option value="8">MediaWiki</option>' . "\n" .
396 '<option value="9">MediaWiki talk</option>' . "\n" .
397 '<option value="10">Template</option>' . "\n" .
398 '<option value="11">Template talk</option>' . "\n" .
399 '<option value="14">Category</option>' . "\n" .
400 '<option value="15">Category talk</option>' . "\n" .
402 Html
::namespaceSelector(
403 [ 'exclude' => [ 0, 1, 3, 100, 101 ] ]
405 'Namespace selector namespace filtering.'
409 public function testCanDisableANamespaces() {
411 '<select id="namespace" name="namespace">' . "\n" .
412 '<option disabled="" value="0">(Main)</option>' . "\n" .
413 '<option disabled="" value="1">Talk</option>' . "\n" .
414 '<option disabled="" value="2">User</option>' . "\n" .
415 '<option disabled="" value="3">User talk</option>' . "\n" .
416 '<option disabled="" value="4">MyWiki</option>' . "\n" .
417 '<option value="5">MyWiki Talk</option>' . "\n" .
418 '<option value="6">File</option>' . "\n" .
419 '<option value="7">File talk</option>' . "\n" .
420 '<option value="8">MediaWiki</option>' . "\n" .
421 '<option value="9">MediaWiki talk</option>' . "\n" .
422 '<option value="10">Template</option>' . "\n" .
423 '<option value="11">Template talk</option>' . "\n" .
424 '<option value="14">Category</option>' . "\n" .
425 '<option value="15">Category talk</option>' . "\n" .
426 '<option value="100">Custom</option>' . "\n" .
427 '<option value="101">Custom talk</option>' . "\n" .
429 Html
::namespaceSelector( [
430 'disable' => [ 0, 1, 2, 3, 4 ]
432 'Namespace selector namespace disabling'
437 * @dataProvider provideHtml5InputTypes
438 * @covers Html::element
440 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
442 '<input type="' . $HTML5InputType . '"/>',
443 Html
::element( 'input', [ 'type' => $HTML5InputType ] ),
444 'In HTML5, Html::element() should accept type="' . $HTML5InputType . '"'
449 * List of input element types values introduced by HTML5
450 * Full list at http://www.w3.org/TR/html-markup/input.html
452 public static function provideHtml5InputTypes() {
469 foreach ( $types as $type ) {
470 $cases[] = [ $type ];
477 * Test out Html::element drops or enforces default value
478 * @covers Html::dropDefaults
479 * @dataProvider provideElementsWithAttributesHavingDefaultValues
481 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
482 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
485 public static function provideElementsWithAttributesHavingDefaultValues() {
486 # Use cases in a concise format:
487 # <expected>, <element name>, <array of attributes> [, <message>]
488 # Will be mapped to Html::element()
491 # ## Generic cases, match $attribDefault static array
492 $cases[] = [ '<area/>',
493 'area', [ 'shape' => 'rect' ]
496 $cases[] = [ '<button type="submit"></button>',
497 'button', [ 'formaction' => 'GET' ]
499 $cases[] = [ '<button type="submit"></button>',
500 'button', [ 'formenctype' => 'application/x-www-form-urlencoded' ]
503 $cases[] = [ '<canvas></canvas>',
504 'canvas', [ 'height' => '150' ]
506 $cases[] = [ '<canvas></canvas>',
507 'canvas', [ 'width' => '300' ]
509 # Also check with numeric values
510 $cases[] = [ '<canvas></canvas>',
511 'canvas', [ 'height' => 150 ]
513 $cases[] = [ '<canvas></canvas>',
514 'canvas', [ 'width' => 300 ]
517 $cases[] = [ '<command/>',
518 'command', [ 'type' => 'command' ]
521 $cases[] = [ '<form></form>',
522 'form', [ 'action' => 'GET' ]
524 $cases[] = [ '<form></form>',
525 'form', [ 'autocomplete' => 'on' ]
527 $cases[] = [ '<form></form>',
528 'form', [ 'enctype' => 'application/x-www-form-urlencoded' ]
531 $cases[] = [ '<input/>',
532 'input', [ 'formaction' => 'GET' ]
534 $cases[] = [ '<input/>',
535 'input', [ 'type' => 'text' ]
538 $cases[] = [ '<keygen/>',
539 'keygen', [ 'keytype' => 'rsa' ]
542 $cases[] = [ '<link/>',
543 'link', [ 'media' => 'all' ]
546 $cases[] = [ '<menu></menu>',
547 'menu', [ 'type' => 'list' ]
550 $cases[] = [ '<script></script>',
551 'script', [ 'type' => 'text/javascript' ]
554 $cases[] = [ '<style></style>',
555 'style', [ 'media' => 'all' ]
557 $cases[] = [ '<style></style>',
558 'style', [ 'type' => 'text/css' ]
561 $cases[] = [ '<textarea></textarea>',
562 'textarea', [ 'wrap' => 'soft' ]
567 # <link type="text/css">
568 $cases[] = [ '<link/>',
569 'link', [ 'type' => 'text/css' ]
572 # <input> specific handling
573 $cases[] = [ '<input type="checkbox"/>',
574 'input', [ 'type' => 'checkbox', 'value' => 'on' ],
575 'Default value "on" is stripped of checkboxes',
577 $cases[] = [ '<input type="radio"/>',
578 'input', [ 'type' => 'radio', 'value' => 'on' ],
579 'Default value "on" is stripped of radio buttons',
581 $cases[] = [ '<input type="submit" value="Submit"/>',
582 'input', [ 'type' => 'submit', 'value' => 'Submit' ],
583 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
585 $cases[] = [ '<input type="color"/>',
586 'input', [ 'type' => 'color', 'value' => '' ],
588 $cases[] = [ '<input type="range"/>',
589 'input', [ 'type' => 'range', 'value' => '' ],
592 # <button> specific handling
593 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
594 $cases[] = [ '<button type="submit"></button>',
595 'button', [ 'type' => 'submit' ],
596 'According to standard the default type is "submit". '
597 . 'Depending on compatibility mode IE might use "button", instead.',
600 # <select> specific handling
601 $cases[] = [ '<select multiple=""></select>',
602 'select', [ 'size' => '4', 'multiple' => true ],
604 # .. with numeric value
605 $cases[] = [ '<select multiple=""></select>',
606 'select', [ 'size' => 4, 'multiple' => true ],
608 $cases[] = [ '<select></select>',
609 'select', [ 'size' => '1', 'multiple' => false ],
611 # .. with numeric value
612 $cases[] = [ '<select></select>',
613 'select', [ 'size' => 1, 'multiple' => false ],
616 # Passing an array as value
617 $cases[] = [ '<a class="css-class-one css-class-two"></a>',
618 'a', [ 'class' => [ 'css-class-one', 'css-class-two' ] ],
619 "dropDefaults accepts values given as an array"
622 # FIXME: doDropDefault should remove defaults given in an array
623 # Expected should be '<a></a>'
624 $cases[] = [ '<a class=""></a>',
625 'a', [ 'class' => [ '', '' ] ],
626 "dropDefaults accepts values given as an array"
629 # Craft the Html elements
631 foreach ( $cases as $case ) {
635 isset( $case[3] ) ?
$case[3] : ''
643 * @covers Html::expandAttributes
645 public function testFormValidationBlacklist() {
647 Html
::expandAttributes( [
654 'Blacklist form validation attributes.'
658 Html
::expandAttributes(
666 'Allow special case "step=any".'
671 public function testWrapperInput() {
673 '<input type="radio" value="testval" name="testname"/>',
674 Html
::input( 'testname', 'testval', 'radio' ),
675 'Input wrapper with type and value.'
678 '<input name="testname"/>',
679 Html
::input( 'testname' ),
680 'Input wrapper with all default values.'
684 public function testWrapperCheck() {
686 '<input type="checkbox" value="1" name="testname"/>',
687 Html
::check( 'testname' ),
688 'Checkbox wrapper unchecked.'
691 '<input checked="" type="checkbox" value="1" name="testname"/>',
692 Html
::check( 'testname', true ),
693 'Checkbox wrapper checked.'
696 '<input type="checkbox" value="testval" name="testname"/>',
697 Html
::check( 'testname', false, [ 'value' => 'testval' ] ),
698 'Checkbox wrapper with a value override.'
702 public function testWrapperRadio() {
704 '<input type="radio" value="1" name="testname"/>',
705 Html
::radio( 'testname' ),
706 'Radio wrapper unchecked.'
709 '<input checked="" type="radio" value="1" name="testname"/>',
710 Html
::radio( 'testname', true ),
711 'Radio wrapper checked.'
714 '<input type="radio" value="testval" name="testname"/>',
715 Html
::radio( 'testname', false, [ 'value' => 'testval' ] ),
716 'Radio wrapper with a value override.'
720 public function testWrapperLabel() {
722 '<label for="testid">testlabel</label>',
723 Html
::label( 'testlabel', 'testid' ),
728 public static function provideSrcSetImages() {
730 [ [], '', 'when there are no images, return empty string' ],
732 [ '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ],
733 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
734 'pixel depth keys may include a trailing "x"'
737 [ '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ],
738 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
739 'pixel depth keys may omit a trailing "x"'
742 [ '1' => 'small.png', '1.5' => 'large.png', '2' => 'large.png' ],
743 'small.png 1x, large.png 1.5x',
744 'omit larger duplicates'
747 [ '1' => 'small.png', '2' => 'large.png', '1.5' => 'large.png' ],
748 'small.png 1x, large.png 1.5x',
749 'omit larger duplicates in irregular order'
755 * @dataProvider provideSrcSetImages
756 * @covers Html::srcSet
758 public function testSrcSet( $images, $expected, $message ) {
759 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
763 class HtmlTestValue
{
764 function __toString() {
765 return 'stringValue';