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'
115 $this->assertNotEmpty(
116 Html
::expandAttributes( array( 'foo' => '' ) ),
117 'keep keys with an empty string'
122 * @covers HTML::expandAttributes
124 public function testExpandAttributesForBooleans() {
127 Html
::expandAttributes( array( 'selected' => false ) ),
128 'Boolean attributes do not generates output when value is false'
132 Html
::expandAttributes( array( 'selected' => null ) ),
133 'Boolean attributes do not generates output when value is null'
138 Html
::expandAttributes( array( 'selected' => true ) ),
139 'Boolean attributes have no value when value is true'
143 Html
::expandAttributes( array( 'selected' ) ),
144 'Boolean attributes have no value when value is true (passed as numerical array)'
147 $this->setMwGlobals( 'wgWellFormedXml', true );
151 Html
::expandAttributes( array( 'selected' => true ) ),
152 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
157 * Test for Html::expandAttributes()
158 * Please note it output a string prefixed with a space!
159 * @covers Html::expandAttributes
161 public function testExpandAttributesVariousExpansions() {
165 Html
::expandAttributes( array( 'empty_string' => '' ) ),
166 'Empty string is always quoted'
170 Html
::expandAttributes( array( 'key' => 'value' ) ),
171 'Simple string value needs no quotes'
175 Html
::expandAttributes( array( 'one' => 1 ) ),
176 'Number 1 value needs no quotes'
180 Html
::expandAttributes( array( 'zero' => 0 ) ),
181 'Number 0 value needs no quotes'
184 $this->setMwGlobals( 'wgWellFormedXml', true );
188 Html
::expandAttributes( array( 'empty_string' => '' ) ),
189 'Attribute values are always quoted (wgWellFormedXml): Empty string'
193 Html
::expandAttributes( array( 'key' => 'value' ) ),
194 'Attribute values are always quoted (wgWellFormedXml): Simple string'
198 Html
::expandAttributes( array( 'one' => 1 ) ),
199 'Attribute values are always quoted (wgWellFormedXml): Number 1'
203 Html
::expandAttributes( array( 'zero' => 0 ) ),
204 'Attribute values are always quoted (wgWellFormedXml): Number 0'
209 * Html::expandAttributes has special features for HTML
210 * attributes that use space separated lists and also
211 * allows arrays to be used as values.
212 * @covers Html::expandAttributes
214 public function testExpandAttributesListValueAttributes() {
217 ' class="redundant spaces here"',
218 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
219 'Normalization should strip redundant spaces'
223 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
224 'Normalization should remove duplicates in string-lists'
226 ### "EMPTY" ARRAY VALUES
229 Html
::expandAttributes( array( 'class' => array() ) ),
230 'Value with an empty array'
234 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
235 'Array with null, empty string and spaces'
237 ### NON-EMPTY ARRAY VALUES
240 Html
::expandAttributes( array( 'class' => array(
247 'Normalization should remove duplicates in the array'
251 Html
::expandAttributes( array( 'class' => array(
257 'Normalization should remove duplicates in string-lists in the array'
262 * Test feature added by r96188, let pass attributes values as
263 * a PHP array. Restricted to class,rel, accesskey.
264 * @covers Html::expandAttributes
266 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
268 ' class="booltrue one"',
269 Html
::expandAttributes( array( 'class' => array(
273 # Method use isset() internally, make sure we do discard
274 # attributes values which have been assigned well known values
276 'boolfalse' => false,
284 * How do we handle duplicate keys in HTML attributes expansion?
285 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
286 * The later will take precedence.
288 * Feature added by r96188
289 * @covers Html::expandAttributes
291 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
294 Html
::expandAttributes( array( 'class' => array(
303 * @covers Html::namespaceSelector
305 public function testNamespaceSelector() {
307 '<select id=namespace name=namespace>' . "\n" .
308 '<option value=0>(Main)</option>' . "\n" .
309 '<option value=1>Talk</option>' . "\n" .
310 '<option value=2>User</option>' . "\n" .
311 '<option value=3>User talk</option>' . "\n" .
312 '<option value=4>MyWiki</option>' . "\n" .
313 '<option value=5>MyWiki Talk</option>' . "\n" .
314 '<option value=6>File</option>' . "\n" .
315 '<option value=7>File talk</option>' . "\n" .
316 '<option value=8>MediaWiki</option>' . "\n" .
317 '<option value=9>MediaWiki talk</option>' . "\n" .
318 '<option value=10>Template</option>' . "\n" .
319 '<option value=11>Template talk</option>' . "\n" .
320 '<option value=14>Category</option>' . "\n" .
321 '<option value=15>Category talk</option>' . "\n" .
322 '<option value=100>Custom</option>' . "\n" .
323 '<option value=101>Custom talk</option>' . "\n" .
325 Html
::namespaceSelector(),
326 'Basic namespace selector without custom options'
330 '<label for=mw-test-namespace>Select a namespace:</label> ' .
331 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
332 '<option value=all>all</option>' . "\n" .
333 '<option value=0>(Main)</option>' . "\n" .
334 '<option value=1>Talk</option>' . "\n" .
335 '<option value=2 selected>User</option>' . "\n" .
336 '<option value=3>User talk</option>' . "\n" .
337 '<option value=4>MyWiki</option>' . "\n" .
338 '<option value=5>MyWiki Talk</option>' . "\n" .
339 '<option value=6>File</option>' . "\n" .
340 '<option value=7>File talk</option>' . "\n" .
341 '<option value=8>MediaWiki</option>' . "\n" .
342 '<option value=9>MediaWiki talk</option>' . "\n" .
343 '<option value=10>Template</option>' . "\n" .
344 '<option value=11>Template talk</option>' . "\n" .
345 '<option value=14>Category</option>' . "\n" .
346 '<option value=15>Category talk</option>' . "\n" .
347 '<option value=100>Custom</option>' . "\n" .
348 '<option value=101>Custom talk</option>' . "\n" .
350 Html
::namespaceSelector(
351 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
352 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
354 'Basic namespace selector with custom values'
358 '<label for=namespace>Select a namespace:</label> ' .
359 '<select id=namespace name=namespace>' . "\n" .
360 '<option value=0>(Main)</option>' . "\n" .
361 '<option value=1>Talk</option>' . "\n" .
362 '<option value=2>User</option>' . "\n" .
363 '<option value=3>User talk</option>' . "\n" .
364 '<option value=4>MyWiki</option>' . "\n" .
365 '<option value=5>MyWiki Talk</option>' . "\n" .
366 '<option value=6>File</option>' . "\n" .
367 '<option value=7>File talk</option>' . "\n" .
368 '<option value=8>MediaWiki</option>' . "\n" .
369 '<option value=9>MediaWiki talk</option>' . "\n" .
370 '<option value=10>Template</option>' . "\n" .
371 '<option value=11>Template talk</option>' . "\n" .
372 '<option value=14>Category</option>' . "\n" .
373 '<option value=15>Category talk</option>' . "\n" .
374 '<option value=100>Custom</option>' . "\n" .
375 '<option value=101>Custom talk</option>' . "\n" .
377 Html
::namespaceSelector(
378 array( 'label' => 'Select a namespace:' )
380 'Basic namespace selector with a custom label but no id attribtue for the <select>'
384 public function testCanFilterOutNamespaces() {
386 '<select id=namespace name=namespace>' . "\n" .
387 '<option value=2>User</option>' . "\n" .
388 '<option value=4>MyWiki</option>' . "\n" .
389 '<option value=5>MyWiki Talk</option>' . "\n" .
390 '<option value=6>File</option>' . "\n" .
391 '<option value=7>File talk</option>' . "\n" .
392 '<option value=8>MediaWiki</option>' . "\n" .
393 '<option value=9>MediaWiki talk</option>' . "\n" .
394 '<option value=10>Template</option>' . "\n" .
395 '<option value=11>Template talk</option>' . "\n" .
396 '<option value=14>Category</option>' . "\n" .
397 '<option value=15>Category talk</option>' . "\n" .
399 Html
::namespaceSelector(
400 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
402 'Namespace selector namespace filtering.'
406 public function testCanDisableANamespaces() {
408 '<select id=namespace name=namespace>' . "\n" .
409 '<option disabled value=0>(Main)</option>' . "\n" .
410 '<option disabled value=1>Talk</option>' . "\n" .
411 '<option disabled value=2>User</option>' . "\n" .
412 '<option disabled value=3>User talk</option>' . "\n" .
413 '<option disabled value=4>MyWiki</option>' . "\n" .
414 '<option value=5>MyWiki Talk</option>' . "\n" .
415 '<option value=6>File</option>' . "\n" .
416 '<option value=7>File talk</option>' . "\n" .
417 '<option value=8>MediaWiki</option>' . "\n" .
418 '<option value=9>MediaWiki talk</option>' . "\n" .
419 '<option value=10>Template</option>' . "\n" .
420 '<option value=11>Template talk</option>' . "\n" .
421 '<option value=14>Category</option>' . "\n" .
422 '<option value=15>Category talk</option>' . "\n" .
423 '<option value=100>Custom</option>' . "\n" .
424 '<option value=101>Custom talk</option>' . "\n" .
426 Html
::namespaceSelector( array(
427 'disable' => array( 0, 1, 2, 3, 4 )
429 'Namespace selector namespace disabling'
434 * @dataProvider provideHtml5InputTypes
435 * @covers Html::element
437 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
439 '<input type=' . $HTML5InputType . '>',
440 Html
::element( 'input', array( 'type' => $HTML5InputType ) ),
441 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"'
446 * List of input element types values introduced by HTML5
447 * Full list at http://www.w3.org/TR/html-markup/input.html
449 public static function provideHtml5InputTypes() {
466 foreach ( $types as $type ) {
467 $cases[] = array( $type );
474 * Test out Html::element drops or enforces default value
475 * @covers Html::dropDefaults
476 * @dataProvider provideElementsWithAttributesHavingDefaultValues
478 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
479 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
482 public static function provideElementsWithAttributesHavingDefaultValues() {
483 # Use cases in a concise format:
484 # <expected>, <element name>, <array of attributes> [, <message>]
485 # Will be mapped to Html::element()
488 ### Generic cases, match $attribDefault static array
489 $cases[] = array( '<area>',
490 'area', array( 'shape' => 'rect' )
493 $cases[] = array( '<button type=submit></button>',
494 'button', array( 'formaction' => 'GET' )
496 $cases[] = array( '<button type=submit></button>',
497 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' )
500 $cases[] = array( '<canvas></canvas>',
501 'canvas', array( 'height' => '150' )
503 $cases[] = array( '<canvas></canvas>',
504 'canvas', array( 'width' => '300' )
506 # Also check with numeric values
507 $cases[] = array( '<canvas></canvas>',
508 'canvas', array( 'height' => 150 )
510 $cases[] = array( '<canvas></canvas>',
511 'canvas', array( 'width' => 300 )
514 $cases[] = array( '<command>',
515 'command', array( 'type' => 'command' )
518 $cases[] = array( '<form></form>',
519 'form', array( 'action' => 'GET' )
521 $cases[] = array( '<form></form>',
522 'form', array( 'autocomplete' => 'on' )
524 $cases[] = array( '<form></form>',
525 'form', array( 'enctype' => 'application/x-www-form-urlencoded' )
528 $cases[] = array( '<input>',
529 'input', array( 'formaction' => 'GET' )
531 $cases[] = array( '<input>',
532 'input', array( 'type' => 'text' )
535 $cases[] = array( '<keygen>',
536 'keygen', array( 'keytype' => 'rsa' )
539 $cases[] = array( '<link>',
540 'link', array( 'media' => 'all' )
543 $cases[] = array( '<menu></menu>',
544 'menu', array( 'type' => 'list' )
547 $cases[] = array( '<script></script>',
548 'script', array( 'type' => 'text/javascript' )
551 $cases[] = array( '<style></style>',
552 'style', array( 'media' => 'all' )
554 $cases[] = array( '<style></style>',
555 'style', array( 'type' => 'text/css' )
558 $cases[] = array( '<textarea></textarea>',
559 'textarea', array( 'wrap' => 'soft' )
564 # <link type="text/css">
565 $cases[] = array( '<link>',
566 'link', array( 'type' => 'text/css' )
569 # <input> specific handling
570 $cases[] = array( '<input type=checkbox>',
571 'input', array( 'type' => 'checkbox', 'value' => 'on' ),
572 'Default value "on" is stripped of checkboxes',
574 $cases[] = array( '<input type=radio>',
575 'input', array( 'type' => 'radio', 'value' => 'on' ),
576 'Default value "on" is stripped of radio buttons',
578 $cases[] = array( '<input type=submit value=Submit>',
579 'input', array( 'type' => 'submit', 'value' => 'Submit' ),
580 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
582 $cases[] = array( '<input type=color>',
583 'input', array( 'type' => 'color', 'value' => '' ),
585 $cases[] = array( '<input type=range>',
586 'input', array( 'type' => 'range', 'value' => '' ),
589 # <button> specific handling
590 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
591 $cases[] = array( '<button type=submit></button>',
592 'button', array( 'type' => 'submit' ),
593 'According to standard the default type is "submit". '
594 . 'Depending on compatibility mode IE might use "button", instead.',
597 # <select> specifc handling
598 $cases[] = array( '<select multiple></select>',
599 'select', array( 'size' => '4', 'multiple' => true ),
601 # .. with numeric value
602 $cases[] = array( '<select multiple></select>',
603 'select', array( 'size' => 4, 'multiple' => true ),
605 $cases[] = array( '<select></select>',
606 'select', array( 'size' => '1', 'multiple' => false ),
608 # .. with numeric value
609 $cases[] = array( '<select></select>',
610 'select', array( 'size' => 1, 'multiple' => false ),
613 # Passing an array as value
614 $cases[] = array( '<a class="css-class-one css-class-two"></a>',
615 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ),
616 "dropDefaults accepts values given as an array"
619 # FIXME: doDropDefault should remove defaults given in an array
620 # Expected should be '<a></a>'
621 $cases[] = array( '<a class=""></a>',
622 'a', array( 'class' => array( '', '' ) ),
623 "dropDefaults accepts values given as an array"
626 # Craft the Html elements
628 foreach ( $cases as $case ) {
632 isset( $case[3] ) ?
$case[3] : ''
640 * @covers Html::expandAttributes
642 public function testFormValidationBlacklist() {
644 Html
::expandAttributes( array(
651 'Blacklist form validation attributes.'
655 Html
::expandAttributes(
663 'Allow special case "step=any".'