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,
44 public function testElementBasics() {
47 Html
::element( 'img', null, '' ),
48 'No close tag for short-tag elements'
52 '<element></element>',
53 Html
::element( 'element', null, null ),
54 'Close tag for empty element (null, null)'
58 '<element></element>',
59 Html
::element( 'element', array(), '' ),
60 'Close tag for empty element (array, string)'
63 $this->setMwGlobals( 'wgWellFormedXml', true );
67 Html
::element( 'img', null, '' ),
68 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
72 public function dataXmlMimeType() {
74 // ( $mimetype, $isXmlMimeType )
75 # HTML is not an XML MimeType
76 array( 'text/html', false ),
77 # XML is an XML MimeType
78 array( 'text/xml', true ),
79 array( 'application/xml', true ),
80 # XHTML is an XML MimeType
81 array( 'application/xhtml+xml', true ),
82 # Make sure other +xml MimeTypes are supported
83 # SVG is another random MimeType even though we don't use it
84 array( 'image/svg+xml', true ),
85 # Complete random other MimeTypes are not XML
86 array( 'text/plain', false ),
91 * @dataProvider dataXmlMimeType
93 public function testXmlMimeType( $mimetype, $isXmlMimeType ) {
94 $this->assertEquals( $isXmlMimeType, Html
::isXmlMimeType( $mimetype ) );
97 public function testExpandAttributesSkipsNullAndFalse() {
101 Html
::expandAttributes( array( 'foo' => null ) ),
102 'skip keys with null value'
105 Html
::expandAttributes( array( 'foo' => false ) ),
106 'skip keys with false value'
108 $this->assertNotEmpty(
109 Html
::expandAttributes( array( 'foo' => '' ) ),
110 'keep keys with an empty string'
114 public function testExpandAttributesForBooleans() {
117 Html
::expandAttributes( array( 'selected' => false ) ),
118 'Boolean attributes do not generates output when value is false'
122 Html
::expandAttributes( array( 'selected' => null ) ),
123 'Boolean attributes do not generates output when value is null'
128 Html
::expandAttributes( array( 'selected' => true ) ),
129 'Boolean attributes have no value when value is true'
133 Html
::expandAttributes( array( 'selected' ) ),
134 'Boolean attributes have no value when value is true (passed as numerical array)'
137 $this->setMwGlobals( 'wgWellFormedXml', true );
141 Html
::expandAttributes( array( 'selected' => true ) ),
142 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
147 * Test for Html::expandAttributes()
148 * Please note it output a string prefixed with a space!
150 public function testExpandAttributesVariousExpansions() {
154 Html
::expandAttributes( array( 'empty_string' => '' ) ),
155 'Empty string is always quoted'
159 Html
::expandAttributes( array( 'key' => 'value' ) ),
160 'Simple string value needs no quotes'
164 Html
::expandAttributes( array( 'one' => 1 ) ),
165 'Number 1 value needs no quotes'
169 Html
::expandAttributes( array( 'zero' => 0 ) ),
170 'Number 0 value needs no quotes'
173 $this->setMwGlobals( 'wgWellFormedXml', true );
177 Html
::expandAttributes( array( 'empty_string' => '' ) ),
178 'Attribute values are always quoted (wgWellFormedXml): Empty string'
182 Html
::expandAttributes( array( 'key' => 'value' ) ),
183 'Attribute values are always quoted (wgWellFormedXml): Simple string'
187 Html
::expandAttributes( array( 'one' => 1 ) ),
188 'Attribute values are always quoted (wgWellFormedXml): Number 1'
192 Html
::expandAttributes( array( 'zero' => 0 ) ),
193 'Attribute values are always quoted (wgWellFormedXml): Number 0'
198 * Html::expandAttributes has special features for HTML
199 * attributes that use space separated lists and also
200 * allows arrays to be used as values.
202 public function testExpandAttributesListValueAttributes() {
205 ' class="redundant spaces here"',
206 Html
::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
207 'Normalization should strip redundant spaces'
211 Html
::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
212 'Normalization should remove duplicates in string-lists'
214 ### "EMPTY" ARRAY VALUES
217 Html
::expandAttributes( array( 'class' => array() ) ),
218 'Value with an empty array'
222 Html
::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
223 'Array with null, empty string and spaces'
225 ### NON-EMPTY ARRAY VALUES
228 Html
::expandAttributes( array( 'class' => array(
235 'Normalization should remove duplicates in the array'
239 Html
::expandAttributes( array( 'class' => array(
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.
253 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
255 ' class="booltrue one"',
256 Html
::expandAttributes( array( 'class' => array(
260 # Method use isset() internally, make sure we do discard
261 # attributes values which have been assigned well known values
263 'boolfalse' => false,
271 * How do we handle duplicate keys in HTML attributes expansion?
272 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
273 * The later will take precedence.
275 * Feature added by r96188
277 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
280 Html
::expandAttributes( array( 'class' => array(
288 function testNamespaceSelector() {
290 '<select id=namespace name=namespace>' . "\n" .
291 '<option value=0>(Main)</option>' . "\n" .
292 '<option value=1>Talk</option>' . "\n" .
293 '<option value=2>User</option>' . "\n" .
294 '<option value=3>User talk</option>' . "\n" .
295 '<option value=4>MyWiki</option>' . "\n" .
296 '<option value=5>MyWiki Talk</option>' . "\n" .
297 '<option value=6>File</option>' . "\n" .
298 '<option value=7>File talk</option>' . "\n" .
299 '<option value=8>MediaWiki</option>' . "\n" .
300 '<option value=9>MediaWiki talk</option>' . "\n" .
301 '<option value=10>Template</option>' . "\n" .
302 '<option value=11>Template talk</option>' . "\n" .
303 '<option value=14>Category</option>' . "\n" .
304 '<option value=15>Category talk</option>' . "\n" .
305 '<option value=100>Custom</option>' . "\n" .
306 '<option value=101>Custom talk</option>' . "\n" .
308 Html
::namespaceSelector(),
309 'Basic namespace selector without custom options'
313 '<label for=mw-test-namespace>Select a namespace:</label> ' .
314 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
315 '<option value=all>all</option>' . "\n" .
316 '<option value=0>(Main)</option>' . "\n" .
317 '<option value=1>Talk</option>' . "\n" .
318 '<option value=2 selected>User</option>' . "\n" .
319 '<option value=3>User talk</option>' . "\n" .
320 '<option value=4>MyWiki</option>' . "\n" .
321 '<option value=5>MyWiki Talk</option>' . "\n" .
322 '<option value=6>File</option>' . "\n" .
323 '<option value=7>File talk</option>' . "\n" .
324 '<option value=8>MediaWiki</option>' . "\n" .
325 '<option value=9>MediaWiki talk</option>' . "\n" .
326 '<option value=10>Template</option>' . "\n" .
327 '<option value=11>Template talk</option>' . "\n" .
328 '<option value=14>Category</option>' . "\n" .
329 '<option value=15>Category talk</option>' . "\n" .
330 '<option value=100>Custom</option>' . "\n" .
331 '<option value=101>Custom talk</option>' . "\n" .
333 Html
::namespaceSelector(
334 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
335 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
337 'Basic namespace selector with custom values'
341 '<label for=namespace>Select a namespace:</label> ' .
342 '<select id=namespace name=namespace>' . "\n" .
343 '<option value=0>(Main)</option>' . "\n" .
344 '<option value=1>Talk</option>' . "\n" .
345 '<option value=2>User</option>' . "\n" .
346 '<option value=3>User talk</option>' . "\n" .
347 '<option value=4>MyWiki</option>' . "\n" .
348 '<option value=5>MyWiki Talk</option>' . "\n" .
349 '<option value=6>File</option>' . "\n" .
350 '<option value=7>File talk</option>' . "\n" .
351 '<option value=8>MediaWiki</option>' . "\n" .
352 '<option value=9>MediaWiki talk</option>' . "\n" .
353 '<option value=10>Template</option>' . "\n" .
354 '<option value=11>Template talk</option>' . "\n" .
355 '<option value=14>Category</option>' . "\n" .
356 '<option value=15>Category talk</option>' . "\n" .
357 '<option value=100>Custom</option>' . "\n" .
358 '<option value=101>Custom talk</option>' . "\n" .
360 Html
::namespaceSelector(
361 array( 'label' => 'Select a namespace:' )
363 'Basic namespace selector with a custom label but no id attribtue for the <select>'
367 function testCanFilterOutNamespaces() {
369 '<select id=namespace name=namespace>' . "\n" .
370 '<option value=2>User</option>' . "\n" .
371 '<option value=4>MyWiki</option>' . "\n" .
372 '<option value=5>MyWiki Talk</option>' . "\n" .
373 '<option value=6>File</option>' . "\n" .
374 '<option value=7>File talk</option>' . "\n" .
375 '<option value=8>MediaWiki</option>' . "\n" .
376 '<option value=9>MediaWiki talk</option>' . "\n" .
377 '<option value=10>Template</option>' . "\n" .
378 '<option value=11>Template talk</option>' . "\n" .
379 '<option value=14>Category</option>' . "\n" .
380 '<option value=15>Category talk</option>' . "\n" .
382 Html
::namespaceSelector(
383 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
385 'Namespace selector namespace filtering.'
389 function testCanDisableANamespaces() {
391 '<select id=namespace name=namespace>' . "\n" .
392 '<option disabled value=0>(Main)</option>' . "\n" .
393 '<option disabled value=1>Talk</option>' . "\n" .
394 '<option disabled value=2>User</option>' . "\n" .
395 '<option disabled value=3>User talk</option>' . "\n" .
396 '<option disabled value=4>MyWiki</option>' . "\n" .
397 '<option value=5>MyWiki Talk</option>' . "\n" .
398 '<option value=6>File</option>' . "\n" .
399 '<option value=7>File talk</option>' . "\n" .
400 '<option value=8>MediaWiki</option>' . "\n" .
401 '<option value=9>MediaWiki talk</option>' . "\n" .
402 '<option value=10>Template</option>' . "\n" .
403 '<option value=11>Template talk</option>' . "\n" .
404 '<option value=14>Category</option>' . "\n" .
405 '<option value=15>Category talk</option>' . "\n" .
406 '<option value=100>Custom</option>' . "\n" .
407 '<option value=101>Custom talk</option>' . "\n" .
409 Html
::namespaceSelector( array(
410 'disable' => array( 0, 1, 2, 3, 4 )
412 'Namespace selector namespace disabling'
417 * @dataProvider provideHtml5InputTypes
419 function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
421 '<input type=' . $HTML5InputType . '>',
422 Html
::element( 'input', array( 'type' => $HTML5InputType ) ),
423 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"'
428 * List of input element types values introduced by HTML5
429 * Full list at http://www.w3.org/TR/html-markup/input.html
431 public static function provideHtml5InputTypes() {
448 foreach ( $types as $type ) {
449 $cases[] = array( $type );
456 * Test out Html::element drops or enforces default value
457 * @covers Html::dropDefaults
458 * @dataProvider provideElementsWithAttributesHavingDefaultValues
460 function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
461 $this->assertEquals( $expected, Html
::element( $element, $attribs ), $message );
464 public static function provideElementsWithAttributesHavingDefaultValues() {
465 # Use cases in a concise format:
466 # <expected>, <element name>, <array of attributes> [, <message>]
467 # Will be mapped to Html::element()
470 ### Generic cases, match $attribDefault static array
471 $cases[] = array( '<area>',
472 'area', array( 'shape' => 'rect' )
475 $cases[] = array( '<button type=submit></button>',
476 'button', array( 'formaction' => 'GET' )
478 $cases[] = array( '<button type=submit></button>',
479 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' )
482 $cases[] = array( '<canvas></canvas>',
483 'canvas', array( 'height' => '150' )
485 $cases[] = array( '<canvas></canvas>',
486 'canvas', array( 'width' => '300' )
488 # Also check with numeric values
489 $cases[] = array( '<canvas></canvas>',
490 'canvas', array( 'height' => 150 )
492 $cases[] = array( '<canvas></canvas>',
493 'canvas', array( 'width' => 300 )
496 $cases[] = array( '<command>',
497 'command', array( 'type' => 'command' )
500 $cases[] = array( '<form></form>',
501 'form', array( 'action' => 'GET' )
503 $cases[] = array( '<form></form>',
504 'form', array( 'autocomplete' => 'on' )
506 $cases[] = array( '<form></form>',
507 'form', array( 'enctype' => 'application/x-www-form-urlencoded' )
510 $cases[] = array( '<input>',
511 'input', array( 'formaction' => 'GET' )
513 $cases[] = array( '<input>',
514 'input', array( 'type' => 'text' )
517 $cases[] = array( '<keygen>',
518 'keygen', array( 'keytype' => 'rsa' )
521 $cases[] = array( '<link>',
522 'link', array( 'media' => 'all' )
525 $cases[] = array( '<menu></menu>',
526 'menu', array( 'type' => 'list' )
529 $cases[] = array( '<script></script>',
530 'script', array( 'type' => 'text/javascript' )
533 $cases[] = array( '<style></style>',
534 'style', array( 'media' => 'all' )
536 $cases[] = array( '<style></style>',
537 'style', array( 'type' => 'text/css' )
540 $cases[] = array( '<textarea></textarea>',
541 'textarea', array( 'wrap' => 'soft' )
546 # <link type="text/css">
547 $cases[] = array( '<link>',
548 'link', array( 'type' => 'text/css' )
551 # <input> specific handling
552 $cases[] = array( '<input type=checkbox>',
553 'input', array( 'type' => 'checkbox', 'value' => 'on' ),
554 'Default value "on" is stripped of checkboxes',
556 $cases[] = array( '<input type=radio>',
557 'input', array( 'type' => 'radio', 'value' => 'on' ),
558 'Default value "on" is stripped of radio buttons',
560 $cases[] = array( '<input type=submit value=Submit>',
561 'input', array( 'type' => 'submit', 'value' => 'Submit' ),
562 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
564 $cases[] = array( '<input type=color>',
565 'input', array( 'type' => 'color', 'value' => '' ),
567 $cases[] = array( '<input type=range>',
568 'input', array( 'type' => 'range', 'value' => '' ),
571 # <button> specific handling
572 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
573 $cases[] = array( '<button type=submit></button>',
574 'button', array( 'type' => 'submit' ),
575 'According to standard the default type is "submit". Depending on compatibility mode IE might use "button", instead.',
578 # <select> specifc handling
579 $cases[] = array( '<select multiple></select>',
580 'select', array( 'size' => '4', 'multiple' => true ),
582 # .. with numeric value
583 $cases[] = array( '<select multiple></select>',
584 'select', array( 'size' => 4, 'multiple' => true ),
586 $cases[] = array( '<select></select>',
587 'select', array( 'size' => '1', 'multiple' => false ),
589 # .. with numeric value
590 $cases[] = array( '<select></select>',
591 'select', array( 'size' => 1, 'multiple' => false ),
594 # Passing an array as value
595 $cases[] = array( '<a class="css-class-one css-class-two"></a>',
596 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ),
597 "dropDefaults accepts values given as an array"
600 # FIXME: doDropDefault should remove defaults given in an array
601 # Expected should be '<a></a>'
602 $cases[] = array( '<a class=""></a>',
603 'a', array( 'class' => array( '', '' ) ),
604 "dropDefaults accepts values given as an array"
607 # Craft the Html elements
609 foreach ( $cases as $case ) {
613 isset( $case[3] ) ?
$case[3] : ''
620 public function testFormValidationBlacklist() {
622 Html
::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 2 ) ),
623 'Blacklist form validation attributes.'
627 Html
::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 'any' ) ),
628 'Allow special case "step=any".'