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