MediaWiki
master
|
00001 <?php 00002 00003 class XmlTest extends MediaWikiTestCase { 00004 private static $oldLang; 00005 private static $oldNamespaces; 00006 00007 protected function setUp() { 00008 parent::setUp(); 00009 00010 $langObj = Language::factory( 'en' ); 00011 $langObj->setNamespaces( array( 00012 -2 => 'Media', 00013 -1 => 'Special', 00014 0 => '', 00015 1 => 'Talk', 00016 2 => 'User', 00017 3 => 'User_talk', 00018 4 => 'MyWiki', 00019 5 => 'MyWiki_Talk', 00020 6 => 'File', 00021 7 => 'File_talk', 00022 8 => 'MediaWiki', 00023 9 => 'MediaWiki_talk', 00024 10 => 'Template', 00025 11 => 'Template_talk', 00026 100 => 'Custom', 00027 101 => 'Custom_talk', 00028 ) ); 00029 00030 $this->setMwGlobals( array( 00031 'wgLang' => $langObj, 00032 ) ); 00033 } 00034 00035 public function testExpandAttributes() { 00036 $this->assertNull( Xml::expandAttributes(null), 00037 'Converting a null list of attributes' 00038 ); 00039 $this->assertEquals( '', Xml::expandAttributes( array() ), 00040 'Converting an empty list of attributes' 00041 ); 00042 } 00043 00044 public function testExpandAttributesException() { 00045 $this->setExpectedException('MWException'); 00046 Xml::expandAttributes('string'); 00047 } 00048 00049 function testElementOpen() { 00050 $this->assertEquals( 00051 '<element>', 00052 Xml::element( 'element', null, null ), 00053 'Opening element with no attributes' 00054 ); 00055 } 00056 00057 function testElementEmpty() { 00058 $this->assertEquals( 00059 '<element />', 00060 Xml::element( 'element', null, '' ), 00061 'Terminated empty element' 00062 ); 00063 } 00064 00065 function testElementInputCanHaveAValueOfZero() { 00066 $this->assertEquals( 00067 '<input name="name" value="0" />', 00068 Xml::input( 'name', false, 0 ), 00069 'Input with a value of 0 (bug 23797)' 00070 ); 00071 } 00072 function testElementEscaping() { 00073 $this->assertEquals( 00074 '<element>hello <there> you & you</element>', 00075 Xml::element( 'element', null, 'hello <there> you & you' ), 00076 'Element with no attributes and content that needs escaping' 00077 ); 00078 } 00079 00080 public function testEscapeTagsOnly() { 00081 $this->assertEquals( '"><', Xml::escapeTagsOnly( '"><' ), 00082 'replace " > and < with their HTML entitites' 00083 ); 00084 } 00085 00086 function testElementAttributes() { 00087 $this->assertEquals( 00088 '<element key="value" <>="<>">', 00089 Xml::element( 'element', array( 'key' => 'value', '<>' => '<>' ), null ), 00090 'Element attributes, keys are not escaped' 00091 ); 00092 } 00093 00094 function testOpenElement() { 00095 $this->assertEquals( 00096 '<element k="v">', 00097 Xml::openElement( 'element', array( 'k' => 'v' ) ), 00098 'openElement() shortcut' 00099 ); 00100 } 00101 00102 function testCloseElement() { 00103 $this->assertEquals( '</element>', Xml::closeElement( 'element' ), 'closeElement() shortcut' ); 00104 } 00105 00106 public function testDateMenu( ) { 00107 $curYear = intval(gmdate('Y')); 00108 $prevYear = $curYear - 1; 00109 00110 $curMonth = intval(gmdate('n')); 00111 $prevMonth = $curMonth - 1; 00112 if( $prevMonth == 0 ) { $prevMonth = 12; } 00113 $nextMonth = $curMonth + 1; 00114 if( $nextMonth == 13 ) { $nextMonth = 1; } 00115 00116 $this->assertEquals( 00117 '<label for="year">From year (and earlier):</label> <input id="year" maxlength="4" size="7" type="number" value="2011" name="year" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" . 00118 '<option value="1">January</option>' . "\n" . 00119 '<option value="2" selected="">February</option>' . "\n" . 00120 '<option value="3">March</option>' . "\n" . 00121 '<option value="4">April</option>' . "\n" . 00122 '<option value="5">May</option>' . "\n" . 00123 '<option value="6">June</option>' . "\n" . 00124 '<option value="7">July</option>' . "\n" . 00125 '<option value="8">August</option>' . "\n" . 00126 '<option value="9">September</option>' . "\n" . 00127 '<option value="10">October</option>' . "\n" . 00128 '<option value="11">November</option>' . "\n" . 00129 '<option value="12">December</option></select>', 00130 Xml::dateMenu( 2011, 02 ), 00131 "Date menu for february 2011" 00132 ); 00133 $this->assertEquals( 00134 '<label for="year">From year (and earlier):</label> <input id="year" maxlength="4" size="7" type="number" value="2011" name="year" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" . 00135 '<option value="1">January</option>' . "\n" . 00136 '<option value="2">February</option>' . "\n" . 00137 '<option value="3">March</option>' . "\n" . 00138 '<option value="4">April</option>' . "\n" . 00139 '<option value="5">May</option>' . "\n" . 00140 '<option value="6">June</option>' . "\n" . 00141 '<option value="7">July</option>' . "\n" . 00142 '<option value="8">August</option>' . "\n" . 00143 '<option value="9">September</option>' . "\n" . 00144 '<option value="10">October</option>' . "\n" . 00145 '<option value="11">November</option>' . "\n" . 00146 '<option value="12">December</option></select>', 00147 Xml::dateMenu( 2011, -1), 00148 "Date menu with negative month for 'All'" 00149 ); 00150 $this->assertEquals( 00151 Xml::dateMenu( $curYear, $curMonth ), 00152 Xml::dateMenu( '' , $curMonth ), 00153 "Date menu year is the current one when not specified" 00154 ); 00155 00156 $wantedYear = $nextMonth == 1 ? $curYear : $prevYear; 00157 $this->assertEquals( 00158 Xml::dateMenu( $wantedYear, $nextMonth ), 00159 Xml::dateMenu( '', $nextMonth ), 00160 "Date menu next month is 11 months ago" 00161 ); 00162 00163 $this->assertEquals( 00164 '<label for="year">From year (and earlier):</label> <input id="year" maxlength="4" size="7" type="number" name="year" /> <label for="month">From month (and earlier):</label> <select id="month" name="month" class="mw-month-selector"><option value="-1">all</option>' . "\n" . 00165 '<option value="1">January</option>' . "\n" . 00166 '<option value="2">February</option>' . "\n" . 00167 '<option value="3">March</option>' . "\n" . 00168 '<option value="4">April</option>' . "\n" . 00169 '<option value="5">May</option>' . "\n" . 00170 '<option value="6">June</option>' . "\n" . 00171 '<option value="7">July</option>' . "\n" . 00172 '<option value="8">August</option>' . "\n" . 00173 '<option value="9">September</option>' . "\n" . 00174 '<option value="10">October</option>' . "\n" . 00175 '<option value="11">November</option>' . "\n" . 00176 '<option value="12">December</option></select>', 00177 Xml::dateMenu( '', '' ), 00178 "Date menu with neither year or month" 00179 ); 00180 } 00181 00182 # 00183 # textarea 00184 # 00185 function testTextareaNoContent() { 00186 $this->assertEquals( 00187 '<textarea name="name" id="name" cols="40" rows="5"></textarea>', 00188 Xml::textarea( 'name', '' ), 00189 'textarea() with not content' 00190 ); 00191 } 00192 00193 function testTextareaAttribs() { 00194 $this->assertEquals( 00195 '<textarea name="name" id="name" cols="20" rows="10"><txt></textarea>', 00196 Xml::textarea( 'name', '<txt>', 20, 10 ), 00197 'textarea() with custom attribs' 00198 ); 00199 } 00200 00201 # 00202 # input and label 00203 # 00204 function testLabelCreation() { 00205 $this->assertEquals( 00206 '<label for="id">name</label>', 00207 Xml::label( 'name', 'id' ), 00208 'label() with no attribs' 00209 ); 00210 } 00211 function testLabelAttributeCanOnlyBeClassOrTitle() { 00212 $this->assertEquals( 00213 '<label for="id">name</label>', 00214 Xml::label( 'name', 'id', array( 'generated' => true ) ), 00215 'label() can not be given a generated attribute' 00216 ); 00217 $this->assertEquals( 00218 '<label for="id" class="nice">name</label>', 00219 Xml::label( 'name', 'id', array( 'class' => 'nice' ) ), 00220 'label() can get a class attribute' 00221 ); 00222 $this->assertEquals( 00223 '<label for="id" title="nice tooltip">name</label>', 00224 Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ), 00225 'label() can get a title attribute' 00226 ); 00227 $this->assertEquals( 00228 '<label for="id" class="nice" title="nice tooltip">name</label>', 00229 Xml::label( 'name', 'id', array( 00230 'generated' => true, 00231 'class' => 'nice', 00232 'title' => 'nice tooltip', 00233 'anotherattr' => 'value', 00234 ) 00235 ), 00236 'label() skip all attributes but "class" and "title"' 00237 ); 00238 } 00239 00240 function testLanguageSelector() { 00241 $select = Xml::languageSelector( 'en', true, null, 00242 array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) ); 00243 $this->assertEquals( 00244 '<label for="testlang">Language:</label>', 00245 $select[0] 00246 ); 00247 } 00248 00249 # 00250 # JS 00251 # 00252 function testEscapeJsStringSpecialChars() { 00253 $this->assertEquals( 00254 '\\\\\r\n', 00255 Xml::escapeJsString( "\\\r\n" ), 00256 'escapeJsString() with special characters' 00257 ); 00258 } 00259 00260 function testEncodeJsVarBoolean() { 00261 $this->assertEquals( 00262 'true', 00263 Xml::encodeJsVar( true ), 00264 'encodeJsVar() with boolean' 00265 ); 00266 } 00267 00268 function testEncodeJsVarNull() { 00269 $this->assertEquals( 00270 'null', 00271 Xml::encodeJsVar( null ), 00272 'encodeJsVar() with null' 00273 ); 00274 } 00275 00276 function testEncodeJsVarArray() { 00277 $this->assertEquals( 00278 '["a",1]', 00279 Xml::encodeJsVar( array( 'a', 1 ) ), 00280 'encodeJsVar() with array' 00281 ); 00282 $this->assertEquals( 00283 '{"a":"a","b":1}', 00284 Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ), 00285 'encodeJsVar() with associative array' 00286 ); 00287 } 00288 00289 function testEncodeJsVarObject() { 00290 $this->assertEquals( 00291 '{"a":"a","b":1}', 00292 Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ), 00293 'encodeJsVar() with object' 00294 ); 00295 } 00296 00297 function testEncodeJsVarInt() { 00298 $this->assertEquals( 00299 '123456', 00300 Xml::encodeJsVar( 123456 ), 00301 'encodeJsVar() with int' 00302 ); 00303 } 00304 00305 function testEncodeJsVarFloat() { 00306 $this->assertEquals( 00307 '1.23456', 00308 Xml::encodeJsVar( 1.23456 ), 00309 'encodeJsVar() with float' 00310 ); 00311 } 00312 00313 function testEncodeJsVarIntString() { 00314 $this->assertEquals( 00315 '"123456"', 00316 Xml::encodeJsVar( '123456' ), 00317 'encodeJsVar() with int-like string' 00318 ); 00319 } 00320 00321 function testEncodeJsVarFloatString() { 00322 $this->assertEquals( 00323 '"1.23456"', 00324 Xml::encodeJsVar( '1.23456' ), 00325 'encodeJsVar() with float-like string' 00326 ); 00327 } 00328 }