MediaWiki
REL1_23
|
00001 <?php 00004 class HtmlTest extends MediaWikiTestCase { 00005 00006 protected function setUp() { 00007 parent::setUp(); 00008 00009 $langCode = 'en'; 00010 $langObj = Language::factory( $langCode ); 00011 00012 // Hardcode namespaces during test runs, 00013 // so that html output based on existing namespaces 00014 // can be properly evaluated. 00015 $langObj->setNamespaces( array( 00016 -2 => 'Media', 00017 -1 => 'Special', 00018 0 => '', 00019 1 => 'Talk', 00020 2 => 'User', 00021 3 => 'User_talk', 00022 4 => 'MyWiki', 00023 5 => 'MyWiki_Talk', 00024 6 => 'File', 00025 7 => 'File_talk', 00026 8 => 'MediaWiki', 00027 9 => 'MediaWiki_talk', 00028 10 => 'Template', 00029 11 => 'Template_talk', 00030 14 => 'Category', 00031 15 => 'Category_talk', 00032 100 => 'Custom', 00033 101 => 'Custom_talk', 00034 ) ); 00035 00036 $this->setMwGlobals( array( 00037 'wgLanguageCode' => $langCode, 00038 'wgContLang' => $langObj, 00039 'wgLang' => $langObj, 00040 'wgWellFormedXml' => false, 00041 ) ); 00042 } 00043 00047 public function testElementBasics() { 00048 $this->assertEquals( 00049 '<img>', 00050 Html::element( 'img', null, '' ), 00051 'No close tag for short-tag elements' 00052 ); 00053 00054 $this->assertEquals( 00055 '<element></element>', 00056 Html::element( 'element', null, null ), 00057 'Close tag for empty element (null, null)' 00058 ); 00059 00060 $this->assertEquals( 00061 '<element></element>', 00062 Html::element( 'element', array(), '' ), 00063 'Close tag for empty element (array, string)' 00064 ); 00065 00066 $this->setMwGlobals( 'wgWellFormedXml', true ); 00067 00068 $this->assertEquals( 00069 '<img />', 00070 Html::element( 'img', null, '' ), 00071 'Self-closing tag for short-tag elements (wgWellFormedXml = true)' 00072 ); 00073 } 00074 00075 public function dataXmlMimeType() { 00076 return array( 00077 // ( $mimetype, $isXmlMimeType ) 00078 # HTML is not an XML MimeType 00079 array( 'text/html', false ), 00080 # XML is an XML MimeType 00081 array( 'text/xml', true ), 00082 array( 'application/xml', true ), 00083 # XHTML is an XML MimeType 00084 array( 'application/xhtml+xml', true ), 00085 # Make sure other +xml MimeTypes are supported 00086 # SVG is another random MimeType even though we don't use it 00087 array( 'image/svg+xml', true ), 00088 # Complete random other MimeTypes are not XML 00089 array( 'text/plain', false ), 00090 ); 00091 } 00092 00097 public function testXmlMimeType( $mimetype, $isXmlMimeType ) { 00098 $this->assertEquals( $isXmlMimeType, Html::isXmlMimeType( $mimetype ) ); 00099 } 00100 00104 public function testExpandAttributesSkipsNullAndFalse() { 00105 00106 ### EMPTY ######## 00107 $this->assertEmpty( 00108 Html::expandAttributes( array( 'foo' => null ) ), 00109 'skip keys with null value' 00110 ); 00111 $this->assertEmpty( 00112 Html::expandAttributes( array( 'foo' => false ) ), 00113 'skip keys with false value' 00114 ); 00115 $this->assertNotEmpty( 00116 Html::expandAttributes( array( 'foo' => '' ) ), 00117 'keep keys with an empty string' 00118 ); 00119 } 00120 00124 public function testExpandAttributesForBooleans() { 00125 $this->assertEquals( 00126 '', 00127 Html::expandAttributes( array( 'selected' => false ) ), 00128 'Boolean attributes do not generates output when value is false' 00129 ); 00130 $this->assertEquals( 00131 '', 00132 Html::expandAttributes( array( 'selected' => null ) ), 00133 'Boolean attributes do not generates output when value is null' 00134 ); 00135 00136 $this->assertEquals( 00137 ' selected', 00138 Html::expandAttributes( array( 'selected' => true ) ), 00139 'Boolean attributes have no value when value is true' 00140 ); 00141 $this->assertEquals( 00142 ' selected', 00143 Html::expandAttributes( array( 'selected' ) ), 00144 'Boolean attributes have no value when value is true (passed as numerical array)' 00145 ); 00146 00147 $this->setMwGlobals( 'wgWellFormedXml', true ); 00148 00149 $this->assertEquals( 00150 ' selected=""', 00151 Html::expandAttributes( array( 'selected' => true ) ), 00152 'Boolean attributes have empty string value when value is true (wgWellFormedXml)' 00153 ); 00154 } 00155 00161 public function testExpandAttributesVariousExpansions() { 00162 ### NOT EMPTY #### 00163 $this->assertEquals( 00164 ' empty_string=""', 00165 Html::expandAttributes( array( 'empty_string' => '' ) ), 00166 'Empty string is always quoted' 00167 ); 00168 $this->assertEquals( 00169 ' key=value', 00170 Html::expandAttributes( array( 'key' => 'value' ) ), 00171 'Simple string value needs no quotes' 00172 ); 00173 $this->assertEquals( 00174 ' one=1', 00175 Html::expandAttributes( array( 'one' => 1 ) ), 00176 'Number 1 value needs no quotes' 00177 ); 00178 $this->assertEquals( 00179 ' zero=0', 00180 Html::expandAttributes( array( 'zero' => 0 ) ), 00181 'Number 0 value needs no quotes' 00182 ); 00183 00184 $this->setMwGlobals( 'wgWellFormedXml', true ); 00185 00186 $this->assertEquals( 00187 ' empty_string=""', 00188 Html::expandAttributes( array( 'empty_string' => '' ) ), 00189 'Attribute values are always quoted (wgWellFormedXml): Empty string' 00190 ); 00191 $this->assertEquals( 00192 ' key="value"', 00193 Html::expandAttributes( array( 'key' => 'value' ) ), 00194 'Attribute values are always quoted (wgWellFormedXml): Simple string' 00195 ); 00196 $this->assertEquals( 00197 ' one="1"', 00198 Html::expandAttributes( array( 'one' => 1 ) ), 00199 'Attribute values are always quoted (wgWellFormedXml): Number 1' 00200 ); 00201 $this->assertEquals( 00202 ' zero="0"', 00203 Html::expandAttributes( array( 'zero' => 0 ) ), 00204 'Attribute values are always quoted (wgWellFormedXml): Number 0' 00205 ); 00206 } 00207 00214 public function testExpandAttributesListValueAttributes() { 00215 ### STRING VALUES 00216 $this->assertEquals( 00217 ' class="redundant spaces here"', 00218 Html::expandAttributes( array( 'class' => ' redundant spaces here ' ) ), 00219 'Normalization should strip redundant spaces' 00220 ); 00221 $this->assertEquals( 00222 ' class="foo bar"', 00223 Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ), 00224 'Normalization should remove duplicates in string-lists' 00225 ); 00226 ### "EMPTY" ARRAY VALUES 00227 $this->assertEquals( 00228 ' class=""', 00229 Html::expandAttributes( array( 'class' => array() ) ), 00230 'Value with an empty array' 00231 ); 00232 $this->assertEquals( 00233 ' class=""', 00234 Html::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ), 00235 'Array with null, empty string and spaces' 00236 ); 00237 ### NON-EMPTY ARRAY VALUES 00238 $this->assertEquals( 00239 ' class="foo bar"', 00240 Html::expandAttributes( array( 'class' => array( 00241 'foo', 00242 'bar', 00243 'foo', 00244 'bar', 00245 'bar', 00246 ) ) ), 00247 'Normalization should remove duplicates in the array' 00248 ); 00249 $this->assertEquals( 00250 ' class="foo bar"', 00251 Html::expandAttributes( array( 'class' => array( 00252 'foo bar', 00253 'bar foo', 00254 'foo', 00255 'bar bar', 00256 ) ) ), 00257 'Normalization should remove duplicates in string-lists in the array' 00258 ); 00259 } 00260 00266 public function testExpandAttributesSpaceSeparatedAttributesWithBoolean() { 00267 $this->assertEquals( 00268 ' class="booltrue one"', 00269 Html::expandAttributes( array( 'class' => array( 00270 'booltrue' => true, 00271 'one' => 1, 00272 00273 # Method use isset() internally, make sure we do discard 00274 # attributes values which have been assigned well known values 00275 'emptystring' => '', 00276 'boolfalse' => false, 00277 'zero' => 0, 00278 'null' => null, 00279 ) ) ) 00280 ); 00281 } 00282 00291 public function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() { 00292 $this->assertEquals( 00293 ' class=""', 00294 Html::expandAttributes( array( 'class' => array( 00295 'GREEN', 00296 'GREEN' => false, 00297 'GREEN', 00298 ) ) ) 00299 ); 00300 } 00301 00305 public function testNamespaceSelector() { 00306 $this->assertEquals( 00307 '<select id=namespace name=namespace>' . "\n" . 00308 '<option value=0>(Main)</option>' . "\n" . 00309 '<option value=1>Talk</option>' . "\n" . 00310 '<option value=2>User</option>' . "\n" . 00311 '<option value=3>User talk</option>' . "\n" . 00312 '<option value=4>MyWiki</option>' . "\n" . 00313 '<option value=5>MyWiki Talk</option>' . "\n" . 00314 '<option value=6>File</option>' . "\n" . 00315 '<option value=7>File talk</option>' . "\n" . 00316 '<option value=8>MediaWiki</option>' . "\n" . 00317 '<option value=9>MediaWiki talk</option>' . "\n" . 00318 '<option value=10>Template</option>' . "\n" . 00319 '<option value=11>Template talk</option>' . "\n" . 00320 '<option value=14>Category</option>' . "\n" . 00321 '<option value=15>Category talk</option>' . "\n" . 00322 '<option value=100>Custom</option>' . "\n" . 00323 '<option value=101>Custom talk</option>' . "\n" . 00324 '</select>', 00325 Html::namespaceSelector(), 00326 'Basic namespace selector without custom options' 00327 ); 00328 00329 $this->assertEquals( 00330 '<label for=mw-test-namespace>Select a namespace:</label> ' . 00331 '<select id=mw-test-namespace name=wpNamespace>' . "\n" . 00332 '<option value=all>all</option>' . "\n" . 00333 '<option value=0>(Main)</option>' . "\n" . 00334 '<option value=1>Talk</option>' . "\n" . 00335 '<option value=2 selected>User</option>' . "\n" . 00336 '<option value=3>User talk</option>' . "\n" . 00337 '<option value=4>MyWiki</option>' . "\n" . 00338 '<option value=5>MyWiki Talk</option>' . "\n" . 00339 '<option value=6>File</option>' . "\n" . 00340 '<option value=7>File talk</option>' . "\n" . 00341 '<option value=8>MediaWiki</option>' . "\n" . 00342 '<option value=9>MediaWiki talk</option>' . "\n" . 00343 '<option value=10>Template</option>' . "\n" . 00344 '<option value=11>Template talk</option>' . "\n" . 00345 '<option value=14>Category</option>' . "\n" . 00346 '<option value=15>Category talk</option>' . "\n" . 00347 '<option value=100>Custom</option>' . "\n" . 00348 '<option value=101>Custom talk</option>' . "\n" . 00349 '</select>', 00350 Html::namespaceSelector( 00351 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ), 00352 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' ) 00353 ), 00354 'Basic namespace selector with custom values' 00355 ); 00356 00357 $this->assertEquals( 00358 '<label for=namespace>Select a namespace:</label> ' . 00359 '<select id=namespace name=namespace>' . "\n" . 00360 '<option value=0>(Main)</option>' . "\n" . 00361 '<option value=1>Talk</option>' . "\n" . 00362 '<option value=2>User</option>' . "\n" . 00363 '<option value=3>User talk</option>' . "\n" . 00364 '<option value=4>MyWiki</option>' . "\n" . 00365 '<option value=5>MyWiki Talk</option>' . "\n" . 00366 '<option value=6>File</option>' . "\n" . 00367 '<option value=7>File talk</option>' . "\n" . 00368 '<option value=8>MediaWiki</option>' . "\n" . 00369 '<option value=9>MediaWiki talk</option>' . "\n" . 00370 '<option value=10>Template</option>' . "\n" . 00371 '<option value=11>Template talk</option>' . "\n" . 00372 '<option value=14>Category</option>' . "\n" . 00373 '<option value=15>Category talk</option>' . "\n" . 00374 '<option value=100>Custom</option>' . "\n" . 00375 '<option value=101>Custom talk</option>' . "\n" . 00376 '</select>', 00377 Html::namespaceSelector( 00378 array( 'label' => 'Select a namespace:' ) 00379 ), 00380 'Basic namespace selector with a custom label but no id attribtue for the <select>' 00381 ); 00382 } 00383 00384 public function testCanFilterOutNamespaces() { 00385 $this->assertEquals( 00386 '<select id=namespace name=namespace>' . "\n" . 00387 '<option value=2>User</option>' . "\n" . 00388 '<option value=4>MyWiki</option>' . "\n" . 00389 '<option value=5>MyWiki Talk</option>' . "\n" . 00390 '<option value=6>File</option>' . "\n" . 00391 '<option value=7>File talk</option>' . "\n" . 00392 '<option value=8>MediaWiki</option>' . "\n" . 00393 '<option value=9>MediaWiki talk</option>' . "\n" . 00394 '<option value=10>Template</option>' . "\n" . 00395 '<option value=11>Template talk</option>' . "\n" . 00396 '<option value=14>Category</option>' . "\n" . 00397 '<option value=15>Category talk</option>' . "\n" . 00398 '</select>', 00399 Html::namespaceSelector( 00400 array( 'exclude' => array( 0, 1, 3, 100, 101 ) ) 00401 ), 00402 'Namespace selector namespace filtering.' 00403 ); 00404 } 00405 00406 public function testCanDisableANamespaces() { 00407 $this->assertEquals( 00408 '<select id=namespace name=namespace>' . "\n" . 00409 '<option disabled value=0>(Main)</option>' . "\n" . 00410 '<option disabled value=1>Talk</option>' . "\n" . 00411 '<option disabled value=2>User</option>' . "\n" . 00412 '<option disabled value=3>User talk</option>' . "\n" . 00413 '<option disabled value=4>MyWiki</option>' . "\n" . 00414 '<option value=5>MyWiki Talk</option>' . "\n" . 00415 '<option value=6>File</option>' . "\n" . 00416 '<option value=7>File talk</option>' . "\n" . 00417 '<option value=8>MediaWiki</option>' . "\n" . 00418 '<option value=9>MediaWiki talk</option>' . "\n" . 00419 '<option value=10>Template</option>' . "\n" . 00420 '<option value=11>Template talk</option>' . "\n" . 00421 '<option value=14>Category</option>' . "\n" . 00422 '<option value=15>Category talk</option>' . "\n" . 00423 '<option value=100>Custom</option>' . "\n" . 00424 '<option value=101>Custom talk</option>' . "\n" . 00425 '</select>', 00426 Html::namespaceSelector( array( 00427 'disable' => array( 0, 1, 2, 3, 4 ) 00428 ) ), 00429 'Namespace selector namespace disabling' 00430 ); 00431 } 00432 00437 public function testHtmlElementAcceptsNewHtml5TypesInHtml5Mode( $HTML5InputType ) { 00438 $this->assertEquals( 00439 '<input type=' . $HTML5InputType . '>', 00440 Html::element( 'input', array( 'type' => $HTML5InputType ) ), 00441 'In HTML5, HTML::element() should accept type="' . $HTML5InputType . '"' 00442 ); 00443 } 00444 00449 public static function provideHtml5InputTypes() { 00450 $types = array( 00451 'datetime', 00452 'datetime-local', 00453 'date', 00454 'month', 00455 'time', 00456 'week', 00457 'number', 00458 'range', 00459 'email', 00460 'url', 00461 'search', 00462 'tel', 00463 'color', 00464 ); 00465 $cases = array(); 00466 foreach ( $types as $type ) { 00467 $cases[] = array( $type ); 00468 } 00469 00470 return $cases; 00471 } 00472 00478 public function testDropDefaults( $expected, $element, $attribs, $message = '' ) { 00479 $this->assertEquals( $expected, Html::element( $element, $attribs ), $message ); 00480 } 00481 00482 public static function provideElementsWithAttributesHavingDefaultValues() { 00483 # Use cases in a concise format: 00484 # <expected>, <element name>, <array of attributes> [, <message>] 00485 # Will be mapped to Html::element() 00486 $cases = array(); 00487 00488 ### Generic cases, match $attribDefault static array 00489 $cases[] = array( '<area>', 00490 'area', array( 'shape' => 'rect' ) 00491 ); 00492 00493 $cases[] = array( '<button type=submit></button>', 00494 'button', array( 'formaction' => 'GET' ) 00495 ); 00496 $cases[] = array( '<button type=submit></button>', 00497 'button', array( 'formenctype' => 'application/x-www-form-urlencoded' ) 00498 ); 00499 00500 $cases[] = array( '<canvas></canvas>', 00501 'canvas', array( 'height' => '150' ) 00502 ); 00503 $cases[] = array( '<canvas></canvas>', 00504 'canvas', array( 'width' => '300' ) 00505 ); 00506 # Also check with numeric values 00507 $cases[] = array( '<canvas></canvas>', 00508 'canvas', array( 'height' => 150 ) 00509 ); 00510 $cases[] = array( '<canvas></canvas>', 00511 'canvas', array( 'width' => 300 ) 00512 ); 00513 00514 $cases[] = array( '<command>', 00515 'command', array( 'type' => 'command' ) 00516 ); 00517 00518 $cases[] = array( '<form></form>', 00519 'form', array( 'action' => 'GET' ) 00520 ); 00521 $cases[] = array( '<form></form>', 00522 'form', array( 'autocomplete' => 'on' ) 00523 ); 00524 $cases[] = array( '<form></form>', 00525 'form', array( 'enctype' => 'application/x-www-form-urlencoded' ) 00526 ); 00527 00528 $cases[] = array( '<input>', 00529 'input', array( 'formaction' => 'GET' ) 00530 ); 00531 $cases[] = array( '<input>', 00532 'input', array( 'type' => 'text' ) 00533 ); 00534 00535 $cases[] = array( '<keygen>', 00536 'keygen', array( 'keytype' => 'rsa' ) 00537 ); 00538 00539 $cases[] = array( '<link>', 00540 'link', array( 'media' => 'all' ) 00541 ); 00542 00543 $cases[] = array( '<menu></menu>', 00544 'menu', array( 'type' => 'list' ) 00545 ); 00546 00547 $cases[] = array( '<script></script>', 00548 'script', array( 'type' => 'text/javascript' ) 00549 ); 00550 00551 $cases[] = array( '<style></style>', 00552 'style', array( 'media' => 'all' ) 00553 ); 00554 $cases[] = array( '<style></style>', 00555 'style', array( 'type' => 'text/css' ) 00556 ); 00557 00558 $cases[] = array( '<textarea></textarea>', 00559 'textarea', array( 'wrap' => 'soft' ) 00560 ); 00561 00562 ### SPECIFIC CASES 00563 00564 # <link type="text/css"> 00565 $cases[] = array( '<link>', 00566 'link', array( 'type' => 'text/css' ) 00567 ); 00568 00569 # <input> specific handling 00570 $cases[] = array( '<input type=checkbox>', 00571 'input', array( 'type' => 'checkbox', 'value' => 'on' ), 00572 'Default value "on" is stripped of checkboxes', 00573 ); 00574 $cases[] = array( '<input type=radio>', 00575 'input', array( 'type' => 'radio', 'value' => 'on' ), 00576 'Default value "on" is stripped of radio buttons', 00577 ); 00578 $cases[] = array( '<input type=submit value=Submit>', 00579 'input', array( 'type' => 'submit', 'value' => 'Submit' ), 00580 'Default value "Submit" is kept on submit buttons (for possible l10n issues)', 00581 ); 00582 $cases[] = array( '<input type=color>', 00583 'input', array( 'type' => 'color', 'value' => '' ), 00584 ); 00585 $cases[] = array( '<input type=range>', 00586 'input', array( 'type' => 'range', 'value' => '' ), 00587 ); 00588 00589 # <button> specific handling 00590 # see remarks on http://msdn.microsoft.com/en-us/library/ie/ms535211%28v=vs.85%29.aspx 00591 $cases[] = array( '<button type=submit></button>', 00592 'button', array( 'type' => 'submit' ), 00593 'According to standard the default type is "submit". Depending on compatibility mode IE might use "button", instead.', 00594 ); 00595 00596 # <select> specifc handling 00597 $cases[] = array( '<select multiple></select>', 00598 'select', array( 'size' => '4', 'multiple' => true ), 00599 ); 00600 # .. with numeric value 00601 $cases[] = array( '<select multiple></select>', 00602 'select', array( 'size' => 4, 'multiple' => true ), 00603 ); 00604 $cases[] = array( '<select></select>', 00605 'select', array( 'size' => '1', 'multiple' => false ), 00606 ); 00607 # .. with numeric value 00608 $cases[] = array( '<select></select>', 00609 'select', array( 'size' => 1, 'multiple' => false ), 00610 ); 00611 00612 # Passing an array as value 00613 $cases[] = array( '<a class="css-class-one css-class-two"></a>', 00614 'a', array( 'class' => array( 'css-class-one', 'css-class-two' ) ), 00615 "dropDefaults accepts values given as an array" 00616 ); 00617 00618 # FIXME: doDropDefault should remove defaults given in an array 00619 # Expected should be '<a></a>' 00620 $cases[] = array( '<a class=""></a>', 00621 'a', array( 'class' => array( '', '' ) ), 00622 "dropDefaults accepts values given as an array" 00623 ); 00624 00625 # Craft the Html elements 00626 $ret = array(); 00627 foreach ( $cases as $case ) { 00628 $ret[] = array( 00629 $case[0], 00630 $case[1], $case[2], 00631 isset( $case[3] ) ? $case[3] : '' 00632 ); 00633 } 00634 00635 return $ret; 00636 } 00637 00641 public function testFormValidationBlacklist() { 00642 $this->assertEmpty( 00643 Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 2 ) ), 00644 'Blacklist form validation attributes.' 00645 ); 00646 $this->assertEquals( 00647 ' step=any', 00648 Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 'any' ) ), 00649 'Allow special case "step=any".' 00650 ); 00651 } 00652 }