MediaWiki
REL1_24
|
00001 <?php 00002 00006 class XmlTest extends MediaWikiTestCase { 00007 00008 protected function setUp() { 00009 parent::setUp(); 00010 00011 $langObj = Language::factory( 'en' ); 00012 $langObj->setNamespaces( array( 00013 -2 => 'Media', 00014 -1 => 'Special', 00015 0 => '', 00016 1 => 'Talk', 00017 2 => 'User', 00018 3 => 'User_talk', 00019 4 => 'MyWiki', 00020 5 => 'MyWiki_Talk', 00021 6 => 'File', 00022 7 => 'File_talk', 00023 8 => 'MediaWiki', 00024 9 => 'MediaWiki_talk', 00025 10 => 'Template', 00026 11 => 'Template_talk', 00027 100 => 'Custom', 00028 101 => 'Custom_talk', 00029 ) ); 00030 00031 $this->setMwGlobals( array( 00032 'wgLang' => $langObj, 00033 'wgWellFormedXml' => true, 00034 ) ); 00035 } 00036 00040 public function testExpandAttributes() { 00041 $this->assertNull( Xml::expandAttributes( null ), 00042 'Converting a null list of attributes' 00043 ); 00044 $this->assertEquals( '', Xml::expandAttributes( array() ), 00045 'Converting an empty list of attributes' 00046 ); 00047 } 00048 00052 public function testExpandAttributesException() { 00053 $this->setExpectedException( 'MWException' ); 00054 Xml::expandAttributes( 'string' ); 00055 } 00056 00060 public function testElementOpen() { 00061 $this->assertEquals( 00062 '<element>', 00063 Xml::element( 'element', null, null ), 00064 'Opening element with no attributes' 00065 ); 00066 } 00067 00071 public function testElementEmpty() { 00072 $this->assertEquals( 00073 '<element />', 00074 Xml::element( 'element', null, '' ), 00075 'Terminated empty element' 00076 ); 00077 } 00078 00082 public function testElementInputCanHaveAValueOfZero() { 00083 $this->assertEquals( 00084 '<input name="name" value="0" class="mw-ui-input" />', 00085 Xml::input( 'name', false, 0 ), 00086 'Input with a value of 0 (bug 23797)' 00087 ); 00088 } 00089 00093 public function testElementEscaping() { 00094 $this->assertEquals( 00095 '<element>hello <there> you & you</element>', 00096 Xml::element( 'element', null, 'hello <there> you & you' ), 00097 'Element with no attributes and content that needs escaping' 00098 ); 00099 } 00100 00104 public function testEscapeTagsOnly() { 00105 $this->assertEquals( '"><', Xml::escapeTagsOnly( '"><' ), 00106 'replace " > and < with their HTML entitites' 00107 ); 00108 } 00109 00113 public function testElementAttributes() { 00114 $this->assertEquals( 00115 '<element key="value" <>="<>">', 00116 Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ), 00117 'Element attributes, keys are not escaped' 00118 ); 00119 } 00120 00124 public function testOpenElement() { 00125 $this->assertEquals( 00126 '<element k="v">', 00127 Xml::openElement( 'element', array( 'k' => 'v' ) ), 00128 'openElement() shortcut' 00129 ); 00130 } 00131 00135 public function testCloseElement() { 00136 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' ); 00137 } 00138 00142 public function testDateMenu() { 00143 $curYear = intval( gmdate( 'Y' ) ); 00144 $prevYear = $curYear - 1; 00145 00146 $curMonth = intval( gmdate( 'n' ) ); 00147 00148 $nextMonth = $curMonth + 1; 00149 if ( $nextMonth == 13 ) { 00150 $nextMonth = 1; 00151 } 00152 00153 $this->assertEquals( 00154 '<label for="year">From year (and earlier):</label> ' . 00155 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year" class="mw-ui-input" /> ' . 00156 '<label for="month">From month (and earlier):</label> ' . 00157 '<select id="month" name="month" class="mw-month-selector">' . 00158 '<option value="-1">all</option>' . "\n" . 00159 '<option value="1">January</option>' . "\n" . 00160 '<option value="2" selected="">February</option>' . "\n" . 00161 '<option value="3">March</option>' . "\n" . 00162 '<option value="4">April</option>' . "\n" . 00163 '<option value="5">May</option>' . "\n" . 00164 '<option value="6">June</option>' . "\n" . 00165 '<option value="7">July</option>' . "\n" . 00166 '<option value="8">August</option>' . "\n" . 00167 '<option value="9">September</option>' . "\n" . 00168 '<option value="10">October</option>' . "\n" . 00169 '<option value="11">November</option>' . "\n" . 00170 '<option value="12">December</option></select>', 00171 Xml::dateMenu( 2011, 02 ), 00172 "Date menu for february 2011" 00173 ); 00174 $this->assertEquals( 00175 '<label for="year">From year (and earlier):</label> ' . 00176 '<input id="year" maxlength="4" size="7" type="number" value="2011" name="year" class="mw-ui-input" /> ' . 00177 '<label for="month">From month (and earlier):</label> ' . 00178 '<select id="month" name="month" class="mw-month-selector">' . 00179 '<option value="-1">all</option>' . "\n" . 00180 '<option value="1">January</option>' . "\n" . 00181 '<option value="2">February</option>' . "\n" . 00182 '<option value="3">March</option>' . "\n" . 00183 '<option value="4">April</option>' . "\n" . 00184 '<option value="5">May</option>' . "\n" . 00185 '<option value="6">June</option>' . "\n" . 00186 '<option value="7">July</option>' . "\n" . 00187 '<option value="8">August</option>' . "\n" . 00188 '<option value="9">September</option>' . "\n" . 00189 '<option value="10">October</option>' . "\n" . 00190 '<option value="11">November</option>' . "\n" . 00191 '<option value="12">December</option></select>', 00192 Xml::dateMenu( 2011, -1 ), 00193 "Date menu with negative month for 'All'" 00194 ); 00195 $this->assertEquals( 00196 Xml::dateMenu( $curYear, $curMonth ), 00197 Xml::dateMenu( '', $curMonth ), 00198 "Date menu year is the current one when not specified" 00199 ); 00200 00201 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear; 00202 $this->assertEquals( 00203 Xml::dateMenu( $wantedYear, $nextMonth ), 00204 Xml::dateMenu( '', $nextMonth ), 00205 "Date menu next month is 11 months ago" 00206 ); 00207 00208 $this->assertEquals( 00209 '<label for="year">From year (and earlier):</label> ' . 00210 '<input id="year" maxlength="4" size="7" type="number" name="year" class="mw-ui-input" /> ' . 00211 '<label for="month">From month (and earlier):</label> ' . 00212 '<select id="month" name="month" class="mw-month-selector">' . 00213 '<option value="-1">all</option>' . "\n" . 00214 '<option value="1">January</option>' . "\n" . 00215 '<option value="2">February</option>' . "\n" . 00216 '<option value="3">March</option>' . "\n" . 00217 '<option value="4">April</option>' . "\n" . 00218 '<option value="5">May</option>' . "\n" . 00219 '<option value="6">June</option>' . "\n" . 00220 '<option value="7">July</option>' . "\n" . 00221 '<option value="8">August</option>' . "\n" . 00222 '<option value="9">September</option>' . "\n" . 00223 '<option value="10">October</option>' . "\n" . 00224 '<option value="11">November</option>' . "\n" . 00225 '<option value="12">December</option></select>', 00226 Xml::dateMenu( '', '' ), 00227 "Date menu with neither year or month" 00228 ); 00229 } 00230 00234 public function testTextareaNoContent() { 00235 $this->assertEquals( 00236 '<textarea name="name" id="name" cols="40" rows="5" class="mw-ui-input"></textarea>', 00237 Xml::textarea( 'name', '' ), 00238 'textarea() with not content' 00239 ); 00240 } 00241 00245 public function testTextareaAttribs() { 00246 $this->assertEquals( 00247 '<textarea name="name" id="name" cols="20" rows="10" class="mw-ui-input"><txt></textarea>', 00248 Xml::textarea( 'name', '<txt>', 20, 10 ), 00249 'textarea() with custom attribs' 00250 ); 00251 } 00252 00256 public function testLabelCreation() { 00257 $this->assertEquals( 00258 '<label for="id">name</label>', 00259 Xml::label( 'name', 'id' ), 00260 'label() with no attribs' 00261 ); 00262 } 00263 00267 public function testLabelAttributeCanOnlyBeClassOrTitle() { 00268 $this->assertEquals( 00269 '<label for="id">name</label>', 00270 Xml::label( 'name', 'id', array( 'generated' => true ) ), 00271 'label() can not be given a generated attribute' 00272 ); 00273 $this->assertEquals( 00274 '<label for="id" class="nice">name</label>', 00275 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ), 00276 'label() can get a class attribute' 00277 ); 00278 $this->assertEquals( 00279 '<label for="id" title="nice tooltip">name</label>', 00280 Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ), 00281 'label() can get a title attribute' 00282 ); 00283 $this->assertEquals( 00284 '<label for="id" class="nice" title="nice tooltip">name</label>', 00285 Xml::label( 'name', 'id', array( 00286 'generated' => true, 00287 'class' => 'nice', 00288 'title' => 'nice tooltip', 00289 'anotherattr' => 'value', 00290 ) 00291 ), 00292 'label() skip all attributes but "class" and "title"' 00293 ); 00294 } 00295 00299 public function testLanguageSelector() { 00300 $select = Xml::languageSelector( 'en', true, null, 00301 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) ); 00302 $this->assertEquals( 00303 '<label for="testlang">Language:</label>', 00304 $select[0] 00305 ); 00306 } 00307 00311 public function testEscapeJsStringSpecialChars() { 00312 $this->assertEquals( 00313 '\\\\\r\n', 00314 Xml::escapeJsString( "\\\r\n" ), 00315 'escapeJsString() with special characters' 00316 ); 00317 } 00318 00322 public function testEncodeJsVarBoolean() { 00323 $this->assertEquals( 00324 'true', 00325 Xml::encodeJsVar( true ), 00326 'encodeJsVar() with boolean' 00327 ); 00328 } 00329 00333 public function testEncodeJsVarNull() { 00334 $this->assertEquals( 00335 'null', 00336 Xml::encodeJsVar( null ), 00337 'encodeJsVar() with null' 00338 ); 00339 } 00340 00344 public function testEncodeJsVarArray() { 00345 $this->assertEquals( 00346 '["a",1]', 00347 Xml::encodeJsVar( array( 'a', 1 ) ), 00348 'encodeJsVar() with array' 00349 ); 00350 $this->assertEquals( 00351 '{"a":"a","b":1}', 00352 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ), 00353 'encodeJsVar() with associative array' 00354 ); 00355 } 00356 00360 public function testEncodeJsVarObject() { 00361 $this->assertEquals( 00362 '{"a":"a","b":1}', 00363 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ), 00364 'encodeJsVar() with object' 00365 ); 00366 } 00367 00371 public function testEncodeJsVarInt() { 00372 $this->assertEquals( 00373 '123456', 00374 Xml::encodeJsVar( 123456 ), 00375 'encodeJsVar() with int' 00376 ); 00377 } 00378 00382 public function testEncodeJsVarFloat() { 00383 $this->assertEquals( 00384 '1.23456', 00385 Xml::encodeJsVar( 1.23456 ), 00386 'encodeJsVar() with float' 00387 ); 00388 } 00389 00393 public function testEncodeJsVarIntString() { 00394 $this->assertEquals( 00395 '"123456"', 00396 Xml::encodeJsVar( '123456' ), 00397 'encodeJsVar() with int-like string' 00398 ); 00399 } 00400 00404 public function testEncodeJsVarFloatString() { 00405 $this->assertEquals( 00406 '"1.23456"', 00407 Xml::encodeJsVar( '1.23456' ), 00408 'encodeJsVar() with float-like string' 00409 ); 00410 } 00411 }