(bug 17602) fix Monobook action tabs not quite touching the page body
[lhc/web/wiklou.git] / tests / phpunit / includes / HtmlTest.php
1 <?php
2 /** tests for includes/Html.php */
3
4 class HtmlTest extends MediaWikiTestCase {
5
6 protected function setUp() {
7 parent::setUp();
8
9 $langCode = 'en';
10 $langObj = Language::factory( $langCode );
11
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 ) );
35
36 $this->setMwGlobals( array(
37 'wgLanguageCode' => $langCode,
38 'wgContLang' => $langObj,
39 'wgLang' => $langObj,
40 'wgHtml5' => true,
41 'wgWellFormedXml' => false,
42 ) );
43 }
44
45 public function testElementBasics() {
46 $this->assertEquals(
47 '<img>',
48 Html::element( 'img', null, '' ),
49 'No close tag for short-tag elements'
50 );
51
52 $this->assertEquals(
53 '<element></element>',
54 Html::element( 'element', null, null ),
55 'Close tag for empty element (null, null)'
56 );
57
58 $this->assertEquals(
59 '<element></element>',
60 Html::element( 'element', array(), '' ),
61 'Close tag for empty element (array, string)'
62 );
63
64 $this->setMwGlobals( 'wgWellFormedXml', true );
65
66 $this->assertEquals(
67 '<img />',
68 Html::element( 'img', null, '' ),
69 'Self-closing tag for short-tag elements (wgWellFormedXml = true)'
70 );
71 }
72
73 public function testExpandAttributesSkipsNullAndFalse() {
74
75 ### EMPTY ########
76 $this->assertEmpty(
77 Html::expandAttributes( array( 'foo' => null ) ),
78 'skip keys with null value'
79 );
80 $this->assertEmpty(
81 Html::expandAttributes( array( 'foo' => false ) ),
82 'skip keys with false value'
83 );
84 $this->assertNotEmpty(
85 Html::expandAttributes( array( 'foo' => '' ) ),
86 'keep keys with an empty string'
87 );
88 }
89
90 public function testExpandAttributesForBooleans() {
91 $this->assertEquals(
92 '',
93 Html::expandAttributes( array( 'selected' => false ) ),
94 'Boolean attributes do not generates output when value is false'
95 );
96 $this->assertEquals(
97 '',
98 Html::expandAttributes( array( 'selected' => null ) ),
99 'Boolean attributes do not generates output when value is null'
100 );
101
102 $this->assertEquals(
103 ' selected',
104 Html::expandAttributes( array( 'selected' => true ) ),
105 'Boolean attributes have no value when value is true'
106 );
107 $this->assertEquals(
108 ' selected',
109 Html::expandAttributes( array( 'selected' ) ),
110 'Boolean attributes have no value when value is true (passed as numerical array)'
111 );
112
113 $this->setMwGlobals( 'wgWellFormedXml', true );
114
115 $this->assertEquals(
116 ' selected=""',
117 Html::expandAttributes( array( 'selected' => true ) ),
118 'Boolean attributes have empty string value when value is true (wgWellFormedXml)'
119 );
120
121 $this->setMwGlobals( 'wgHtml5', false );
122
123 $this->assertEquals(
124 ' selected="selected"',
125 Html::expandAttributes( array( 'selected' => true ) ),
126 'Boolean attributes have their key as value when value is true (wgWellFormedXml, wgHTML5 = false)'
127 );
128 }
129
130 /**
131 * Test for Html::expandAttributes()
132 * Please note it output a string prefixed with a space!
133 */
134 public function testExpandAttributesVariousExpansions() {
135 ### NOT EMPTY ####
136 $this->assertEquals(
137 ' empty_string=""',
138 Html::expandAttributes( array( 'empty_string' => '' ) ),
139 'Empty string is always quoted'
140 );
141 $this->assertEquals(
142 ' key=value',
143 Html::expandAttributes( array( 'key' => 'value' ) ),
144 'Simple string value needs no quotes'
145 );
146 $this->assertEquals(
147 ' one=1',
148 Html::expandAttributes( array( 'one' => 1 ) ),
149 'Number 1 value needs no quotes'
150 );
151 $this->assertEquals(
152 ' zero=0',
153 Html::expandAttributes( array( 'zero' => 0 ) ),
154 'Number 0 value needs no quotes'
155 );
156
157 $this->setMwGlobals( 'wgWellFormedXml', true );
158
159 $this->assertEquals(
160 ' empty_string=""',
161 Html::expandAttributes( array( 'empty_string' => '' ) ),
162 'Attribute values are always quoted (wgWellFormedXml): Empty string'
163 );
164 $this->assertEquals(
165 ' key="value"',
166 Html::expandAttributes( array( 'key' => 'value' ) ),
167 'Attribute values are always quoted (wgWellFormedXml): Simple string'
168 );
169 $this->assertEquals(
170 ' one="1"',
171 Html::expandAttributes( array( 'one' => 1 ) ),
172 'Attribute values are always quoted (wgWellFormedXml): Number 1'
173 );
174 $this->assertEquals(
175 ' zero="0"',
176 Html::expandAttributes( array( 'zero' => 0 ) ),
177 'Attribute values are always quoted (wgWellFormedXml): Number 0'
178 );
179 }
180
181 /**
182 * Html::expandAttributes has special features for HTML
183 * attributes that use space separated lists and also
184 * allows arrays to be used as values.
185 */
186 public function testExpandAttributesListValueAttributes() {
187 ### STRING VALUES
188 $this->assertEquals(
189 ' class="redundant spaces here"',
190 Html::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
191 'Normalization should strip redundant spaces'
192 );
193 $this->assertEquals(
194 ' class="foo bar"',
195 Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
196 'Normalization should remove duplicates in string-lists'
197 );
198 ### "EMPTY" ARRAY VALUES
199 $this->assertEquals(
200 ' class=""',
201 Html::expandAttributes( array( 'class' => array() ) ),
202 'Value with an empty array'
203 );
204 $this->assertEquals(
205 ' class=""',
206 Html::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
207 'Array with null, empty string and spaces'
208 );
209 ### NON-EMPTY ARRAY VALUES
210 $this->assertEquals(
211 ' class="foo bar"',
212 Html::expandAttributes( array( 'class' => array(
213 'foo',
214 'bar',
215 'foo',
216 'bar',
217 'bar',
218 ) ) ),
219 'Normalization should remove duplicates in the array'
220 );
221 $this->assertEquals(
222 ' class="foo bar"',
223 Html::expandAttributes( array( 'class' => array(
224 'foo bar',
225 'bar foo',
226 'foo',
227 'bar bar',
228 ) ) ),
229 'Normalization should remove duplicates in string-lists in the array'
230 );
231 }
232
233 /**
234 * Test feature added by r96188, let pass attributes values as
235 * a PHP array. Restricted to class,rel, accesskey.
236 */
237 function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
238 $this->assertEquals(
239 ' class="booltrue one"',
240 Html::expandAttributes( array( 'class' => array(
241 'booltrue' => true,
242 'one' => 1,
243
244 # Method use isset() internally, make sure we do discard
245 # attributes values which have been assigned well known values
246 'emptystring' => '',
247 'boolfalse' => false,
248 'zero' => 0,
249 'null' => null,
250 ) ) )
251 );
252 }
253
254 /**
255 * How do we handle duplicate keys in HTML attributes expansion?
256 * We could pass a "class" the values: 'GREEN' and array( 'GREEN' => false )
257 * The later will take precedence.
258 *
259 * Feature added by r96188
260 */
261 function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
262 $this->assertEquals(
263 ' class=""',
264 Html::expandAttributes( array( 'class' => array(
265 'GREEN',
266 'GREEN' => false,
267 'GREEN',
268 ) ) )
269 );
270 }
271
272 function testNamespaceSelector() {
273 $this->assertEquals(
274 '<select id=namespace name=namespace>' . "\n" .
275 '<option value=0>(Main)</option>' . "\n" .
276 '<option value=1>Talk</option>' . "\n" .
277 '<option value=2>User</option>' . "\n" .
278 '<option value=3>User talk</option>' . "\n" .
279 '<option value=4>MyWiki</option>' . "\n" .
280 '<option value=5>MyWiki Talk</option>' . "\n" .
281 '<option value=6>File</option>' . "\n" .
282 '<option value=7>File talk</option>' . "\n" .
283 '<option value=8>MediaWiki</option>' . "\n" .
284 '<option value=9>MediaWiki talk</option>' . "\n" .
285 '<option value=10>Template</option>' . "\n" .
286 '<option value=11>Template talk</option>' . "\n" .
287 '<option value=14>Category</option>' . "\n" .
288 '<option value=15>Category talk</option>' . "\n" .
289 '<option value=100>Custom</option>' . "\n" .
290 '<option value=101>Custom talk</option>' . "\n" .
291 '</select>',
292 Html::namespaceSelector(),
293 'Basic namespace selector without custom options'
294 );
295
296 $this->assertEquals(
297 '<label for=mw-test-namespace>Select a namespace:</label>&#160;' .
298 '<select id=mw-test-namespace name=wpNamespace>' . "\n" .
299 '<option value=all>all</option>' . "\n" .
300 '<option value=0>(Main)</option>' . "\n" .
301 '<option value=1>Talk</option>' . "\n" .
302 '<option value=2 selected>User</option>' . "\n" .
303 '<option value=3>User talk</option>' . "\n" .
304 '<option value=4>MyWiki</option>' . "\n" .
305 '<option value=5>MyWiki Talk</option>' . "\n" .
306 '<option value=6>File</option>' . "\n" .
307 '<option value=7>File talk</option>' . "\n" .
308 '<option value=8>MediaWiki</option>' . "\n" .
309 '<option value=9>MediaWiki talk</option>' . "\n" .
310 '<option value=10>Template</option>' . "\n" .
311 '<option value=11>Template talk</option>' . "\n" .
312 '<option value=14>Category</option>' . "\n" .
313 '<option value=15>Category talk</option>' . "\n" .
314 '<option value=100>Custom</option>' . "\n" .
315 '<option value=101>Custom talk</option>' . "\n" .
316 '</select>',
317 Html::namespaceSelector(
318 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
319 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
320 ),
321 'Basic namespace selector with custom values'
322 );
323
324 $this->assertEquals(
325 '<label for=namespace>Select a namespace:</label>&#160;' .
326 '<select id=namespace name=namespace>' . "\n" .
327 '<option value=0>(Main)</option>' . "\n" .
328 '<option value=1>Talk</option>' . "\n" .
329 '<option value=2>User</option>' . "\n" .
330 '<option value=3>User talk</option>' . "\n" .
331 '<option value=4>MyWiki</option>' . "\n" .
332 '<option value=5>MyWiki Talk</option>' . "\n" .
333 '<option value=6>File</option>' . "\n" .
334 '<option value=7>File talk</option>' . "\n" .
335 '<option value=8>MediaWiki</option>' . "\n" .
336 '<option value=9>MediaWiki talk</option>' . "\n" .
337 '<option value=10>Template</option>' . "\n" .
338 '<option value=11>Template talk</option>' . "\n" .
339 '<option value=14>Category</option>' . "\n" .
340 '<option value=15>Category talk</option>' . "\n" .
341 '<option value=100>Custom</option>' . "\n" .
342 '<option value=101>Custom talk</option>' . "\n" .
343 '</select>',
344 Html::namespaceSelector(
345 array( 'label' => 'Select a namespace:' )
346 ),
347 'Basic namespace selector with a custom label but no id attribtue for the <select>'
348 );
349 }
350
351 function testCanFilterOutNamespaces() {
352 $this->assertEquals(
353 '<select id=namespace name=namespace>' . "\n" .
354 '<option value=2>User</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 '</select>',
366 Html::namespaceSelector(
367 array( 'exclude' => array( 0, 1, 3, 100, 101 ) )
368 ),
369 'Namespace selector namespace filtering.'
370 );
371 }
372
373 function testCanDisableANamespaces() {
374 $this->assertEquals(
375 '<select id=namespace name=namespace>' . "\n" .
376 '<option disabled value=0>(Main)</option>' . "\n" .
377 '<option disabled value=1>Talk</option>' . "\n" .
378 '<option disabled value=2>User</option>' . "\n" .
379 '<option disabled value=3>User talk</option>' . "\n" .
380 '<option disabled 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" .
392 '</select>',
393 Html::namespaceSelector( array(
394 'disable' => array( 0, 1, 2, 3, 4 )
395 ) ),
396 'Namespace selector namespace disabling'
397 );
398 }
399
400 /**
401 * @dataProvider provideHtml5InputTypes
402 */
403 function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) {
404 $this->assertEquals(
405 '<input type=' . $HTML5InputType . '>',
406 Html::element( 'input', array( 'type' => $HTML5InputType ) ),
407 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"'
408 );
409 }
410
411 /**
412 * List of input element types values introduced by HTML5
413 * Full list at http://www.w3.org/TR/html-markup/input.html
414 */
415 public static function provideHtml5InputTypes() {
416 $types = array(
417 'datetime',
418 'datetime-local',
419 'date',
420 'month',
421 'time',
422 'week',
423 'number',
424 'range',
425 'email',
426 'url',
427 'search',
428 'tel',
429 'color',
430 );
431 $cases = array();
432 foreach ( $types as $type ) {
433 $cases[] = array( $type );
434 }
435 return $cases;
436 }
437
438 /**
439 * Test out Html::element drops or enforces default value
440 * @covers Html::dropDefaults
441 * @dataProvider provideElementsWithAttributesHavingDefaultValues
442 */
443 function testDropDefaults( $expected, $element, $attribs, $message = '' ) {
444 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message );
445 }
446
447 public static function provideElementsWithAttributesHavingDefaultValues() {
448 # Use cases in a concise format:
449 # <expected>, <element name>, <array of attributes> [, <message>]
450 # Will be mapped to Html::element()
451 $cases = array();
452
453 ### Generic cases, match $attribDefault static array
454 $cases[] = array( '<area>',
455 'area', array( 'shape' => 'rect' )
456 );
457
458 $cases[] = array( '<button type=submit></button>',
459 'button', array( 'formaction' => 'GET' )
460 );
461 $cases[] = array( '<button type=submit></button>',
462 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' )
463 );
464
465 $cases[] = array( '<canvas></canvas>',
466 'canvas', array( 'height' => '150' )
467 );
468 $cases[] = array( '<canvas></canvas>',
469 'canvas', array( 'width' => '300' )
470 );
471 # Also check with numeric values
472 $cases[] = array( '<canvas></canvas>',
473 'canvas', array( 'height' => 150 )
474 );
475 $cases[] = array( '<canvas></canvas>',
476 'canvas', array( 'width' => 300 )
477 );
478
479 $cases[] = array( '<command>',
480 'command', array( 'type' => 'command' )
481 );
482
483 $cases[] = array( '<form></form>',
484 'form', array( 'action' => 'GET' )
485 );
486 $cases[] = array( '<form></form>',
487 'form', array( 'autocomplete' => 'on' )
488 );
489 $cases[] = array( '<form></form>',
490 'form', array( 'enctype' => 'application/x-www-form-urlencoded' )
491 );
492
493 $cases[] = array( '<input>',
494 'input', array( 'formaction' => 'GET' )
495 );
496 $cases[] = array( '<input>',
497 'input', array( 'type' => 'text' )
498 );
499
500 $cases[] = array( '<keygen>',
501 'keygen', array( 'keytype' => 'rsa' )
502 );
503
504 $cases[] = array( '<link>',
505 'link', array( 'media' => 'all' )
506 );
507
508 $cases[] = array( '<menu></menu>',
509 'menu', array( 'type' => 'list' )
510 );
511
512 $cases[] = array( '<script></script>',
513 'script', array( 'type' => 'text/javascript' )
514 );
515
516 $cases[] = array( '<style></style>',
517 'style', array( 'media' => 'all' )
518 );
519 $cases[] = array( '<style></style>',
520 'style', array( 'type' => 'text/css' )
521 );
522
523 $cases[] = array( '<textarea></textarea>',
524 'textarea', array( 'wrap' => 'soft' )
525 );
526
527 ### SPECIFIC CASES
528
529 # <link type="text/css">
530 $cases[] = array( '<link>',
531 'link', array( 'type' => 'text/css' )
532 );
533
534 # <input> specific handling
535 $cases[] = array( '<input type=checkbox>',
536 'input', array( 'type' => 'checkbox', 'value' => 'on' ),
537 'Default value "on" is stripped of checkboxes',
538 );
539 $cases[] = array( '<input type=radio>',
540 'input', array( 'type' => 'radio', 'value' => 'on' ),
541 'Default value "on" is stripped of radio buttons',
542 );
543 $cases[] = array( '<input type=submit value=Submit>',
544 'input', array( 'type' => 'submit', 'value' => 'Submit' ),
545 'Default value "Submit" is kept on submit buttons (for possible l10n issues)',
546 );
547 $cases[] = array( '<input type=color>',
548 'input', array( 'type' => 'color', 'value' => '' ),
549 );
550 $cases[] = array( '<input type=range>',
551 'input', array( 'type' => 'range', 'value' => '' ),
552 );
553
554 # <button> specific handling
555 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx
556 $cases[] = array( '<button type=submit></button>',
557 'button', array( 'type' => 'submit' ),
558 'According to standard the default type is "submit". Depending on compatibility mode IE might use "button", instead.',
559 );
560
561 # <select> specifc handling
562 $cases[] = array( '<select multiple></select>',
563 'select', array( 'size' => '4', 'multiple' => true ),
564 );
565 # .. with numeric value
566 $cases[] = array( '<select multiple></select>',
567 'select', array( 'size' => 4, 'multiple' => true ),
568 );
569 $cases[] = array( '<select></select>',
570 'select', array( 'size' => '1', 'multiple' => false ),
571 );
572 # .. with numeric value
573 $cases[] = array( '<select></select>',
574 'select', array( 'size' => 1, 'multiple' => false ),
575 );
576
577 # Passing an array as value
578 $cases[] = array( '<a class="css-class-one css-class-two"></a>',
579 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ),
580 "dropDefaults accepts values given as an array"
581 );
582
583 # FIXME: doDropDefault should remove defaults given in an array
584 # Expected should be '<a></a>'
585 $cases[] = array( '<a class=""></a>',
586 'a', array( 'class' => array( '', '' ) ),
587 "dropDefaults accepts values given as an array"
588 );
589
590 # Craft the Html elements
591 $ret = array();
592 foreach ( $cases as $case ) {
593 $ret[] = array(
594 $case[0],
595 $case[1], $case[2],
596 isset( $case[3] ) ? $case[3] : ''
597 );
598 }
599 return $ret;
600 }
601
602 public function testFormValidationBlacklist() {
603 $this->assertEmpty(
604 Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 2 ) ),
605 'Blacklist form validation attributes.'
606 );
607 $this->assertEquals(
608 ' step=any',
609 Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 'any' ) ),
610 'Allow special case "step=any".'
611 );
612 }
613
614 }