2 /** tests for includes/Html.php */
4 class HtmlTest
extends MediaWikiTestCase
{
6 protected function setUp() {
10 $langObj = Language
::factory( $langCode );
12 // Hardcode namespaces during test runs,
13 // so that html output based on existing namespaces
14 // can be properly evaluated.
15 $langObj->setNamespaces( array(
27 9 => 'MediaWiki_talk',
29 11 => 'Template_talk',
31 15 => 'Category_talk',
36 $this->setMwGlobals( array(
37 'wgLanguageCode' => $langCode,
38 'wgContLang' => $langObj,
40 'wgWellFormedXml' => false,
45 * @covers Html::element
47 public function testElementBasics() {
50 Html
::element( 'img', null, '' ),
51 'No close tag for short-tag elements'
55 '<element></element>',
56 Html
::element( 'element', null, null ),
57 'Close tag for empty element (null, null)'
61 '<element></element>',
62 Html
::element( 'element', array(), '' ),
63 'Close tag for empty element (array, string)'
66 $this->setMwGlobals( 'wgWellFormedXml', true );
70 Html
::element( 'img', null, '' ),
71 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
75 public function dataXmlMimeType() {
77 // ( $mimetype, $isXmlMimeType )
78 # HTML is not an XML MimeType
79 array( 'text/html', false ),
80 # XML is an XML MimeType
81 array( 'text/xml', true ),
82 array( 'application/xml', true ),
83 # XHTML is an XML MimeType
84 array( 'application/xhtml+xml', true ),
85 # Make sure other +xml MimeTypes are supported
86 # SVG is another random MimeType even though we don't use it
87 array( 'image/svg+xml', true ),
88 # Complete random other MimeTypes are not XML
89 array( 'text/plain', false ),
94 * @dataProvider dataXmlMimeType
95 * @covers Html::isXmlMimeType
97 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
98 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
102 * @covers HTML::expandAttributes
104 public function testExpandAttributesSkipsNullAndFalse() {
108 Html
::expandAttributes( array( 'foo' => null ) ),
109 'skip keys with null value'
112 Html
::expandAttributes( array( 'foo' => false ) ),
113 'skip keys with false value'
117 Html
::expandAttributes( array( 'foo' => '' ) ),
118 'keep keys with an empty string'
123 * @covers HTML::expandAttributes
125 public function testExpandAttributesForBooleans() {
128 Html
::expandAttributes( array( 'selected' => false ) ),
129 'Boolean attributes do not generates output when value is false'
133 Html
::expandAttributes( array( 'selected' => null ) ),
134 'Boolean attributes do not generates output when value is null'
139 Html
::expandAttributes( array( 'selected' => true ) ),
140 'Boolean attributes have no value when value is true'
144 Html
::expandAttributes( array( 'selected' ) ),
145 'Boolean attributes have no value when value is true (passed as numerical array)'
148 $this->setMwGlobals( 'wgWellFormedXml', true );
152 Html
::expandAttributes( array( 'selected' => true ) ),
153 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
158 * @covers HTML::expandAttributes
160 public function testExpandAttributesForNumbers() {
163 Html
::expandAttributes( array( 'value' => 1 ) ),
164 'Integer value is cast to a string'
168 Html
::expandAttributes( array( 'value' => 1.1 ) ),
169 'Float value is cast to a string'
174 * @covers HTML::expandAttributes
176 public function testExpandAttributesForObjects() {
178 ' value=stringValue',
179 Html
::expandAttributes( array( 'value' => new HtmlTestValue() ) ),
180 'Object value is converted to a string'
185 * Test for Html::expandAttributes()
186 * Please note it output a string prefixed with a space!
187 * @covers Html::expandAttributes
189 public function testExpandAttributesVariousExpansions() {
193 Html
::expandAttributes( array( 'empty_string' => '' ) ),
194 'Empty string is always quoted'
198 Html
::expandAttributes( array( 'key' => 'value' ) ),
199 'Simple string value needs no quotes'
203 Html
::expandAttributes( array( 'one' => 1 ) ),
204 'Number 1 value needs no quotes'
208 Html
::expandAttributes( array( 'zero' => 0 ) ),
209 'Number 0 value needs no quotes'
212 $this->setMwGlobals( 'wgWellFormedXml', true );
216 Html
::expandAttributes( array( 'empty_string' => '' ) ),
217 'Attribute values are always quoted (wgWellFormedXml): Empty string'
221 Html
::expandAttributes( array( 'key' => 'value' ) ),
222 'Attribute values are always quoted (wgWellFormedXml): Simple string'
226 Html
::expandAttributes( array( 'one' => 1 ) ),
227 'Attribute values are always quoted (wgWellFormedXml): Number 1'
231 Html
::expandAttributes( array( 'zero' => 0 ) ),
232 'Attribute values are always quoted (wgWellFormedXml): Number 0'
237 * Html::expandAttributes has special features for HTML
238 * attributes that use space separated lists and also
239 * allows arrays to be used as values.
240 * @covers Html::expandAttributes
242 public function testExpandAttributesListValueAttributes() {
245 ' class="redundant spaces here"',
246 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
247 'Normalization should strip redundant spaces'
251 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
252 'Normalization should remove duplicates in string-lists'
254 # ## "EMPTY" ARRAY VALUES
257 Html
::expandAttributes( array( 'class' => array() ) ),
258 'Value with an empty array'
262 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
263 'Array with null, empty string and spaces'
265 # ## NON-EMPTY ARRAY VALUES
268 Html
::expandAttributes( array( 'class' => array(
275 'Normalization should remove duplicates in the array'
279 Html
::expandAttributes( array( 'class' => array(
285 'Normalization should remove duplicates in string-lists in the array'
290 * Test feature added by r96188, let pass attributes values as
291 * a PHP array. Restricted to class,rel, accesskey.
292 * @covers Html::expandAttributes
294 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
296 ' class="booltrue one"',
297 Html
::expandAttributes( array( 'class' => array(
301 # Method use isset() internally, make sure we do discard
302 # attributes values which have been assigned well known values
304 'boolfalse' => false,
312 * How do we handle duplicate keys in HTML attributes expansion?
313 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
314 * The later will take precedence.
316 * Feature added by r96188
317 * @covers Html::expandAttributes
319 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
322 Html
::expandAttributes( array( 'class' => array(
331 * @covers Html::expandAttributes
332 * @expectedException MWException
334 public function testExpandAttributes_ArrayOnNonListValueAttribute_ThrowsException() {
335 // Real-life test case found in the Popups extension (see Gerrit cf0fd64),
336 // when used with an outdated BetaFeatures extension (see Gerrit deda1e7)
337 Html
::expandAttributes( array(
346 * @covers Html::namespaceSelector
348 public function testNamespaceSelector() {
350 '<select id=namespace name=namespace>' . "\n" .
351 '<option value=0>(Main)</option>' . "\n" .
352 '<option value=1>Talk</option>' . "\n" .
353 '<option value=2>User</option>' . "\n" .
354 '<option value=3>User talk</option>' . "\n" .
355 '<option value=4>MyWiki</option>' . "\n" .
356 '<option value=5>MyWiki Talk</option>' . "\n" .
357 '<option value=6>File</option>' . "\n" .
358 '<option value=7>File talk</option>' . "\n" .
359 '<option value=8>MediaWiki</option>' . "\n" .
360 '<option value=9>MediaWiki talk</option>' . "\n" .
361 '<option value=10>Template</option>' . "\n" .
362 '<option value=11>Template talk</option>' . "\n" .
363 '<option value=14>Category</option>' . "\n" .
364 '<option value=15>Category talk</option>' . "\n" .
365 '<option value=100>Custom</option>' . "\n" .
366 '<option value=101>Custom talk</option>' . "\n" .
368 Html
::namespaceSelector(),
369 'Basic namespace selector without custom options'
373 '<label for=mw-test-namespace>Select a namespace:</label> ' .
374 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
375 '<option value=all>all</option>' . "\n" .
376 '<option value=0>(Main)</option>' . "\n" .
377 '<option value=1>Talk</option>' . "\n" .
378 '<option value=2 selected>User</option>' . "\n" .
379 '<option value=3>User talk</option>' . "\n" .
380 '<option value=4>MyWiki</option>' . "\n" .
381 '<option value=5>MyWiki Talk</option>' . "\n" .
382 '<option value=6>File</option>' . "\n" .
383 '<option value=7>File talk</option>' . "\n" .
384 '<option value=8>MediaWiki</option>' . "\n" .
385 '<option value=9>MediaWiki talk</option>' . "\n" .
386 '<option value=10>Template</option>' . "\n" .
387 '<option value=11>Template talk</option>' . "\n" .
388 '<option value=14>Category</option>' . "\n" .
389 '<option value=15>Category talk</option>' . "\n" .
390 '<option value=100>Custom</option>' . "\n" .
391 '<option value=101>Custom talk</option>' . "\n" .
393 Html
::namespaceSelector(
394 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
395 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
397 'Basic namespace selector with custom values'
401 '<label for=namespace>Select a namespace:</label> ' .
402 '<select id=namespace name=namespace>' . "\n" .
403 '<option value=0>(Main)</option>' . "\n" .
404 '<option value=1>Talk</option>' . "\n" .
405 '<option value=2>User</option>' . "\n" .
406 '<option value=3>User talk</option>' . "\n" .
407 '<option value=4>MyWiki</option>' . "\n" .
408 '<option value=5>MyWiki Talk</option>' . "\n" .
409 '<option value=6>File</option>' . "\n" .
410 '<option value=7>File talk</option>' . "\n" .
411 '<option value=8>MediaWiki</option>' . "\n" .
412 '<option value=9>MediaWiki talk</option>' . "\n" .
413 '<option value=10>Template</option>' . "\n" .
414 '<option value=11>Template talk</option>' . "\n" .
415 '<option value=14>Category</option>' . "\n" .
416 '<option value=15>Category talk</option>' . "\n" .
417 '<option value=100>Custom</option>' . "\n" .
418 '<option value=101>Custom talk</option>' . "\n" .
420 Html
::namespaceSelector(
421 array( 'label' => 'Select a namespace:' )
423 'Basic namespace selector with a custom label but no id attribtue for the <select>'
427 public function testCanFilterOutNamespaces() {
429 '<select id=namespace name=namespace>' . "\n" .
430 '<option value=2>User</option>' . "\n" .
431 '<option value=4>MyWiki</option>' . "\n" .
432 '<option value=5>MyWiki Talk</option>' . "\n" .
433 '<option value=6>File</option>' . "\n" .
434 '<option value=7>File talk</option>' . "\n" .
435 '<option value=8>MediaWiki</option>' . "\n" .
436 '<option value=9>MediaWiki talk</option>' . "\n" .
437 '<option value=10>Template</option>' . "\n" .
438 '<option value=11>Template talk</option>' . "\n" .
439 '<option value=14>Category</option>' . "\n" .
440 '<option value=15>Category talk</option>' . "\n" .
442 Html
::namespaceSelector(
443 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
445 'Namespace selector namespace filtering.'
449 public function testCanDisableANamespaces() {
451 '<select id=namespace name=namespace>' . "\n" .
452 '<option disabled value=0>(Main)</option>' . "\n" .
453 '<option disabled value=1>Talk</option>' . "\n" .
454 '<option disabled value=2>User</option>' . "\n" .
455 '<option disabled value=3>User talk</option>' . "\n" .
456 '<option disabled value=4>MyWiki</option>' . "\n" .
457 '<option value=5>MyWiki Talk</option>' . "\n" .
458 '<option value=6>File</option>' . "\n" .
459 '<option value=7>File talk</option>' . "\n" .
460 '<option value=8>MediaWiki</option>' . "\n" .
461 '<option value=9>MediaWiki talk</option>' . "\n" .
462 '<option value=10>Template</option>' . "\n" .
463 '<option value=11>Template talk</option>' . "\n" .
464 '<option value=14>Category</option>' . "\n" .
465 '<option value=15>Category talk</option>' . "\n" .
466 '<option value=100>Custom</option>' . "\n" .
467 '<option value=101>Custom talk</option>' . "\n" .
469 Html
::namespaceSelector( array(
470 'disable' => array( 0, 1, 2, 3, 4 )
472 'Namespace selector namespace disabling'
477 * @dataProvider provideHtml5InputTypes
478 * @covers Html::element
480 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
482 '<input type=' . $HTML5InputType . '>',
483 Html
::element( 'input', array( 'type' => $HTML5InputType ) ),
484 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"'
489 * List of input element types values introduced by HTML5
490 * Full list at http://www.w3.org/TR/html-markup/input.html
492 public static function provideHtml5InputTypes() {
509 foreach ( $types as $type ) {
510 $cases[] = array( $type );
517 * Test out Html::element drops or enforces default value
518 * @covers Html::dropDefaults
519 * @dataProvider provideElementsWithAttributesHavingDefaultValues
521 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
522 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
525 public static function provideElementsWithAttributesHavingDefaultValues() {
526 # Use cases in a concise format:
527 # <expected>, <element name>, <array of attributes> [, <message>]
528 # Will be mapped to Html::element()
531 # ## Generic cases, match $attribDefault static array
532 $cases[] = array( '<area>',
533 'area', array( 'shape' => 'rect' )
536 $cases[] = array( '<button type=submit></button>',
537 'button', array( 'formaction' => 'GET' )
539 $cases[] = array( '<button type=submit></button>',
540 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' )
543 $cases[] = array( '<canvas></canvas>',
544 'canvas', array( 'height' => '150' )
546 $cases[] = array( '<canvas></canvas>',
547 'canvas', array( 'width' => '300' )
549 # Also check with numeric values
550 $cases[] = array( '<canvas></canvas>',
551 'canvas', array( 'height' => 150 )
553 $cases[] = array( '<canvas></canvas>',
554 'canvas', array( 'width' => 300 )
557 $cases[] = array( '<command>',
558 'command', array( 'type' => 'command' )
561 $cases[] = array( '<form></form>',
562 'form', array( 'action' => 'GET' )
564 $cases[] = array( '<form></form>',
565 'form', array( 'autocomplete' => 'on' )
567 $cases[] = array( '<form></form>',
568 'form', array( 'enctype' => 'application/x-www-form-urlencoded' )
571 $cases[] = array( '<input>',
572 'input', array( 'formaction' => 'GET' )
574 $cases[] = array( '<input>',
575 'input', array( 'type' => 'text' )
578 $cases[] = array( '<keygen>',
579 'keygen', array( 'keytype' => 'rsa' )
582 $cases[] = array( '<link>',
583 'link', array( 'media' => 'all' )
586 $cases[] = array( '<menu></menu>',
587 'menu', array( 'type' => 'list' )
590 $cases[] = array( '<script></script>',
591 'script', array( 'type' => 'text/javascript' )
594 $cases[] = array( '<style></style>',
595 'style', array( 'media' => 'all' )
597 $cases[] = array( '<style></style>',
598 'style', array( 'type' => 'text/css' )
601 $cases[] = array( '<textarea></textarea>',
602 'textarea', array( 'wrap' => 'soft' )
607 # <link type="text/css">
608 $cases[] = array( '<link>',
609 'link', array( 'type' => 'text/css' )
612 # <input> specific handling
613 $cases[] = array( '<input type=checkbox>',
614 'input', array( 'type' => 'checkbox', 'value' => 'on' ),
615 'Default value "on" is stripped of checkboxes',
617 $cases[] = array( '<input type=radio>',
618 'input', array( 'type' => 'radio', 'value' => 'on' ),
619 'Default value "on" is stripped of radio buttons',
621 $cases[] = array( '<input type=submit value=Submit>',
622 'input', array( 'type' => 'submit', 'value' => 'Submit' ),
623 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
625 $cases[] = array( '<input type=color>',
626 'input', array( 'type' => 'color', 'value' => '' ),
628 $cases[] = array( '<input type=range>',
629 'input', array( 'type' => 'range', 'value' => '' ),
632 # <button> specific handling
633 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
634 $cases[] = array( '<button type=submit></button>',
635 'button', array( 'type' => 'submit' ),
636 'According to standard the default type is "submit". '
637 . 'Depending on compatibility mode IE might use "button", instead.',
640 # <select> specific handling
641 $cases[] = array( '<select multiple></select>',
642 'select', array( 'size' => '4', 'multiple' => true ),
644 # .. with numeric value
645 $cases[] = array( '<select multiple></select>',
646 'select', array( 'size' => 4, 'multiple' => true ),
648 $cases[] = array( '<select></select>',
649 'select', array( 'size' => '1', 'multiple' => false ),
651 # .. with numeric value
652 $cases[] = array( '<select></select>',
653 'select', array( 'size' => 1, 'multiple' => false ),
656 # Passing an array as value
657 $cases[] = array( '<a class="css-class-one css-class-two"></a>',
658 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ),
659 "dropDefaults accepts values given as an array"
662 # FIXME: doDropDefault should remove defaults given in an array
663 # Expected should be '<a></a>'
664 $cases[] = array( '<a class=""></a>',
665 'a', array( 'class' => array( '', '' ) ),
666 "dropDefaults accepts values given as an array"
669 # Craft the Html elements
671 foreach ( $cases as $case ) {
675 isset( $case[3] ) ?
$case[3] : ''
683 * @covers Html::expandAttributes
685 public function testFormValidationBlacklist() {
687 Html
::expandAttributes( array(
694 'Blacklist form validation attributes.'
698 Html
::expandAttributes(
706 'Allow special case "step=any".'
711 public function testWrapperInput() {
713 '<input type=radio value=testval name=testname>',
714 Html
::input( 'testname', 'testval', 'radio' ),
715 'Input wrapper with type and value.'
718 '<input name=testname>',
719 Html
::input( 'testname' ),
720 'Input wrapper with all default values.'
724 public function testWrapperCheck() {
726 '<input type=checkbox value=1 name=testname>',
727 Html
::check( 'testname' ),
728 'Checkbox wrapper unchecked.'
731 '<input checked type=checkbox value=1 name=testname>',
732 Html
::check( 'testname', true ),
733 'Checkbox wrapper checked.'
736 '<input type=checkbox value=testval name=testname>',
737 Html
::check( 'testname', false, array( 'value' => 'testval' ) ),
738 'Checkbox wrapper with a value override.'
742 public function testWrapperRadio() {
744 '<input type=radio value=1 name=testname>',
745 Html
::radio( 'testname' ),
746 'Radio wrapper unchecked.'
749 '<input checked type=radio value=1 name=testname>',
750 Html
::radio( 'testname', true ),
751 'Radio wrapper checked.'
754 '<input type=radio value=testval name=testname>',
755 Html
::radio( 'testname', false, array( 'value' => 'testval' ) ),
756 'Radio wrapper with a value override.'
760 public function testWrapperLabel() {
762 '<label for=testid>testlabel</label>',
763 Html
::label( 'testlabel', 'testid' ),
768 public static function provideSrcSetImages() {
770 array( array(), '', 'when there are no images, return empty string' ),
772 array( '1x' => '1x.png', '1.5x' => '1_5x.png', '2x' => '2x.png' ),
773 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
774 'pixel depth keys may include a trailing "x"'
777 array( '1' => '1x.png', '1.5' => '1_5x.png', '2' => '2x.png' ),
778 '1x.png 1x, 1_5x.png 1.5x, 2x.png 2x',
779 'pixel depth keys may omit a trailing "x"'
785 * @dataProvider provideSrcSetImages
786 * @covers Html::srcSet
788 public function testSrcSet( $images, $expected, $message ) {
789 $this->assertEquals( Html
::srcSet( $images ), $expected, $message );
793 class HtmlTestValue
{
794 function __toString() {
795 return 'stringValue';