Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / HtmlTest.php
blobe9349656bee268412d0e1a1eb34c26d573dec481
1 <?php
2 /** tests for includes/Html.php */
4 class HtmlTest extends MediaWikiTestCase {
6 protected function setUp() {
7 parent::setUp();
9 $langCode = 'en';
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(
16 -2 => 'Media',
17 -1 => 'Special',
18 0 => '',
19 1 => 'Talk',
20 2 => 'User',
21 3 => 'User_talk',
22 4 => 'MyWiki',
23 5 => 'MyWiki_Talk',
24 6 => 'File',
25 7 => 'File_talk',
26 8 => 'MediaWiki',
27 9 => 'MediaWiki_talk',
28 10 => 'Template',
29 11 => 'Template_talk',
30 14 => 'Category',
31 15 => 'Category_talk',
32 100 => 'Custom',
33 101 => 'Custom_talk',
34 ) );
36 $this->setMwGlobals( array(
37 'wgLanguageCode' => $langCode,
38 'wgContLang' => $langObj,
39 'wgLang' => $langObj,
40 'wgWellFormedXml' => false,
41 ) );
44 /**
45 * @covers Html::element
47 public function testElementBasics() {
48 $this->assertEquals(
49 '<img>',
50 Html::element( 'img', null, '' ),
51 'No close tag for short-tag elements'
54 $this->assertEquals(
55 '<element></element>',
56 Html::element( 'element', null, null ),
57 'Close tag for empty element (null, null)'
60 $this->assertEquals(
61 '<element></element>',
62 Html::element( 'element', array(), '' ),
63 'Close tag for empty element (array, string)'
66 $this->setMwGlobals( 'wgWellFormedXml', true );
68 $this->assertEquals(
69 '<img />',
70 Html::element( 'img', null, '' ),
71 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
75 public function dataXmlMimeType() {
76 return array(
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 ),
93 /**
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() {
106 ### EMPTY ########
107 $this->assertEmpty(
108 Html::expandAttributes( array( 'foo' => null ) ),
109 'skip keys with null value'
111 $this->assertEmpty(
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() {
125 $this->assertEquals(
127 Html::expandAttributes( array( 'selected' => false ) ),
128 'Boolean attributes do not generates output when value is false'
130 $this->assertEquals(
132 Html::expandAttributes( array( 'selected' => null ) ),
133 'Boolean attributes do not generates output when value is null'
136 $this->assertEquals(
137 ' selected',
138 Html::expandAttributes( array( 'selected' => true ) ),
139 'Boolean attributes have no value when value is true'
141 $this->assertEquals(
142 ' selected',
143 Html::expandAttributes( array( 'selected' ) ),
144 'Boolean attributes have no value when value is true (passed as numerical array)'
147 $this->setMwGlobals( 'wgWellFormedXml', true );
149 $this->assertEquals(
150 ' selected=""',
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() {
162 ### NOT EMPTY ####
163 $this->assertEquals(
164 ' empty_string=""',
165 Html::expandAttributes( array( 'empty_string' => '' ) ),
166 'Empty string is always quoted'
168 $this->assertEquals(
169 ' key=value',
170 Html::expandAttributes( array( 'key' => 'value' ) ),
171 'Simple string value needs no quotes'
173 $this->assertEquals(
174 ' one=1',
175 Html::expandAttributes( array( 'one' => 1 ) ),
176 'Number 1 value needs no quotes'
178 $this->assertEquals(
179 ' zero=0',
180 Html::expandAttributes( array( 'zero' => 0 ) ),
181 'Number 0 value needs no quotes'
184 $this->setMwGlobals( 'wgWellFormedXml', true );
186 $this->assertEquals(
187 ' empty_string=""',
188 Html::expandAttributes( array( 'empty_string' => '' ) ),
189 'Attribute values are always quoted (wgWellFormedXml): Empty string'
191 $this->assertEquals(
192 ' key="value"',
193 Html::expandAttributes( array( 'key' => 'value' ) ),
194 'Attribute values are always quoted (wgWellFormedXml): Simple string'
196 $this->assertEquals(
197 ' one="1"',
198 Html::expandAttributes( array( 'one' => 1 ) ),
199 'Attribute values are always quoted (wgWellFormedXml): Number 1'
201 $this->assertEquals(
202 ' zero="0"',
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() {
215 ### STRING VALUES
216 $this->assertEquals(
217 ' class="redundant spaces here"',
218 Html::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
219 'Normalization should strip redundant spaces'
221 $this->assertEquals(
222 ' class="foo bar"',
223 Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
224 'Normalization should remove duplicates in string-lists'
226 ### "EMPTY" ARRAY VALUES
227 $this->assertEquals(
228 ' class=""',
229 Html::expandAttributes( array( 'class' => array() ) ),
230 'Value with an empty array'
232 $this->assertEquals(
233 ' class=""',
234 Html::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
235 'Array with null, empty string and spaces'
237 ### NON-EMPTY ARRAY VALUES
238 $this->assertEquals(
239 ' class="foo bar"',
240 Html::expandAttributes( array( 'class' => array(
241 'foo',
242 'bar',
243 'foo',
244 'bar',
245 'bar',
246 ) ) ),
247 'Normalization should remove duplicates in the array'
249 $this->assertEquals(
250 ' class="foo bar"',
251 Html::expandAttributes( array( 'class' => array(
252 'foo bar',
253 'bar foo',
254 'foo',
255 'bar bar',
256 ) ) ),
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() {
267 $this->assertEquals(
268 ' class="booltrue one"',
269 Html::expandAttributes( array( 'class' => array(
270 'booltrue' => true,
271 'one' => 1,
273 # Method use isset() internally, make sure we do discard
274 # attributes values which have been assigned well known values
275 'emptystring' => '',
276 'boolfalse' => false,
277 'zero' => 0,
278 'null' => null,
279 ) ) )
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() {
292 $this->assertEquals(
293 ' class=""',
294 Html::expandAttributes( array( 'class' => array(
295 'GREEN',
296 'GREEN' => false,
297 'GREEN',
298 ) ) )
303 * @covers Html::namespaceSelector
305 public function testNamespaceSelector() {
306 $this->assertEquals(
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" .
324 '</select>',
325 Html::namespaceSelector(),
326 'Basic namespace selector without custom options'
329 $this->assertEquals(
330 '<label for=mw-test-namespace>Select a namespace:</label>&#160;' .
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" .
349 '</select>',
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'
357 $this->assertEquals(
358 '<label for=namespace>Select a namespace:</label>&#160;' .
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" .
376 '</select>',
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() {
385 $this->assertEquals(
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" .
398 '</select>',
399 Html::namespaceSelector(
400 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
402 'Namespace selector namespace filtering.'
406 public function testCanDisableANamespaces() {
407 $this->assertEquals(
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" .
425 '</select>',
426 Html::namespaceSelector( array(
427 'disable' => array( 0, 1, 2, 3, 4 )
428 ) ),
429 'Namespace selector namespace disabling'
434 * @dataProvider provideHtml5InputTypes
435 * @covers Html::element
437 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
438 $this->assertEquals(
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() {
450 $types = array(
451 'datetime',
452 'datetime-local',
453 'date',
454 'month',
455 'time',
456 'week',
457 'number',
458 'range',
459 'email',
460 'url',
461 'search',
462 'tel',
463 'color',
465 $cases = array();
466 foreach ( $types as $type ) {
467 $cases[] = array( $type );
470 return $cases;
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()
486 $cases = array();
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' )
562 ### SPECIFIC CASES
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
627 $ret = array();
628 foreach ( $cases as $case ) {
629 $ret[] = array(
630 $case[0],
631 $case[1], $case[2],
632 isset( $case[3] ) ? $case[3] : ''
636 return $ret;
640 * @covers Html::expandAttributes
642 public function testFormValidationBlacklist() {
643 $this->assertEmpty(
644 Html::expandAttributes( array(
645 'min' => 1,
646 'max' => 100,
647 'pattern' => 'abc',
648 'required' => true,
649 'step' => 2
650 ) ),
651 'Blacklist form validation attributes.'
653 $this->assertEquals(
654 ' step=any',
655 Html::expandAttributes(
656 array(
657 'min' => 1,
658 'max' => 100,
659 'pattern' => 'abc',
660 'required' => true,
661 'step' => 'any'
663 'Allow special case "step=any".'