MediaWiki  REL1_22
XmlTest.php
Go to the documentation of this file.
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" />',
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 &lt;there&gt; you &amp; 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( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
00106             'replace " > and < with their HTML entitites'
00107         );
00108     }
00109 
00113     public function testElementAttributes() {
00114         $this->assertEquals(
00115             '<element key="value" <>="&lt;&gt;">',
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         $prevMonth = $curMonth - 1;
00148         if ( $prevMonth == 0 ) {
00149             $prevMonth = 12;
00150         }
00151         $nextMonth = $curMonth + 1;
00152         if ( $nextMonth == 13 ) {
00153             $nextMonth = 1;
00154         }
00155 
00156         $this->assertEquals(
00157             '<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" .
00158                 '<option value="1">January</option>' . "\n" .
00159                 '<option value="2" selected="">February</option>' . "\n" .
00160                 '<option value="3">March</option>' . "\n" .
00161                 '<option value="4">April</option>' . "\n" .
00162                 '<option value="5">May</option>' . "\n" .
00163                 '<option value="6">June</option>' . "\n" .
00164                 '<option value="7">July</option>' . "\n" .
00165                 '<option value="8">August</option>' . "\n" .
00166                 '<option value="9">September</option>' . "\n" .
00167                 '<option value="10">October</option>' . "\n" .
00168                 '<option value="11">November</option>' . "\n" .
00169                 '<option value="12">December</option></select>',
00170             Xml::dateMenu( 2011, 02 ),
00171             "Date menu for february 2011"
00172         );
00173         $this->assertEquals(
00174             '<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" .
00175                 '<option value="1">January</option>' . "\n" .
00176                 '<option value="2">February</option>' . "\n" .
00177                 '<option value="3">March</option>' . "\n" .
00178                 '<option value="4">April</option>' . "\n" .
00179                 '<option value="5">May</option>' . "\n" .
00180                 '<option value="6">June</option>' . "\n" .
00181                 '<option value="7">July</option>' . "\n" .
00182                 '<option value="8">August</option>' . "\n" .
00183                 '<option value="9">September</option>' . "\n" .
00184                 '<option value="10">October</option>' . "\n" .
00185                 '<option value="11">November</option>' . "\n" .
00186                 '<option value="12">December</option></select>',
00187             Xml::dateMenu( 2011, -1 ),
00188             "Date menu with negative month for 'All'"
00189         );
00190         $this->assertEquals(
00191             Xml::dateMenu( $curYear, $curMonth ),
00192             Xml::dateMenu( '', $curMonth ),
00193             "Date menu year is the current one when not specified"
00194         );
00195 
00196         $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
00197         $this->assertEquals(
00198             Xml::dateMenu( $wantedYear, $nextMonth ),
00199             Xml::dateMenu( '', $nextMonth ),
00200             "Date menu next month is 11 months ago"
00201         );
00202 
00203         $this->assertEquals(
00204             '<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" .
00205                 '<option value="1">January</option>' . "\n" .
00206                 '<option value="2">February</option>' . "\n" .
00207                 '<option value="3">March</option>' . "\n" .
00208                 '<option value="4">April</option>' . "\n" .
00209                 '<option value="5">May</option>' . "\n" .
00210                 '<option value="6">June</option>' . "\n" .
00211                 '<option value="7">July</option>' . "\n" .
00212                 '<option value="8">August</option>' . "\n" .
00213                 '<option value="9">September</option>' . "\n" .
00214                 '<option value="10">October</option>' . "\n" .
00215                 '<option value="11">November</option>' . "\n" .
00216                 '<option value="12">December</option></select>',
00217             Xml::dateMenu( '', '' ),
00218             "Date menu with neither year or month"
00219         );
00220     }
00221 
00225     public function testTextareaNoContent() {
00226         $this->assertEquals(
00227             '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
00228             Xml::textarea( 'name', '' ),
00229             'textarea() with not content'
00230         );
00231     }
00232 
00236     public function testTextareaAttribs() {
00237         $this->assertEquals(
00238             '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
00239             Xml::textarea( 'name', '<txt>', 20, 10 ),
00240             'textarea() with custom attribs'
00241         );
00242     }
00243 
00247     public function testLabelCreation() {
00248         $this->assertEquals(
00249             '<label for="id">name</label>',
00250             Xml::label( 'name', 'id' ),
00251             'label() with no attribs'
00252         );
00253     }
00254 
00258     public function testLabelAttributeCanOnlyBeClassOrTitle() {
00259         $this->assertEquals(
00260             '<label for="id">name</label>',
00261             Xml::label( 'name', 'id', array( 'generated' => true ) ),
00262             'label() can not be given a generated attribute'
00263         );
00264         $this->assertEquals(
00265             '<label for="id" class="nice">name</label>',
00266             Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
00267             'label() can get a class attribute'
00268         );
00269         $this->assertEquals(
00270             '<label for="id" title="nice tooltip">name</label>',
00271             Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
00272             'label() can get a title attribute'
00273         );
00274         $this->assertEquals(
00275             '<label for="id" class="nice" title="nice tooltip">name</label>',
00276             Xml::label( 'name', 'id', array(
00277                     'generated' => true,
00278                     'class' => 'nice',
00279                     'title' => 'nice tooltip',
00280                     'anotherattr' => 'value',
00281                 )
00282             ),
00283             'label() skip all attributes but "class" and "title"'
00284         );
00285     }
00286 
00290     public function testLanguageSelector() {
00291         $select = Xml::languageSelector( 'en', true, null,
00292             array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
00293         $this->assertEquals(
00294             '<label for="testlang">Language:</label>',
00295             $select[0]
00296         );
00297     }
00298 
00302     public function testEscapeJsStringSpecialChars() {
00303         $this->assertEquals(
00304             '\\\\\r\n',
00305             Xml::escapeJsString( "\\\r\n" ),
00306             'escapeJsString() with special characters'
00307         );
00308     }
00309 
00313     public function testEncodeJsVarBoolean() {
00314         $this->assertEquals(
00315             'true',
00316             Xml::encodeJsVar( true ),
00317             'encodeJsVar() with boolean'
00318         );
00319     }
00320 
00324     public function testEncodeJsVarNull() {
00325         $this->assertEquals(
00326             'null',
00327             Xml::encodeJsVar( null ),
00328             'encodeJsVar() with null'
00329         );
00330     }
00331 
00335     public function testEncodeJsVarArray() {
00336         $this->assertEquals(
00337             '["a",1]',
00338             Xml::encodeJsVar( array( 'a', 1 ) ),
00339             'encodeJsVar() with array'
00340         );
00341         $this->assertEquals(
00342             '{"a":"a","b":1}',
00343             Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
00344             'encodeJsVar() with associative array'
00345         );
00346     }
00347 
00351     public function testEncodeJsVarObject() {
00352         $this->assertEquals(
00353             '{"a":"a","b":1}',
00354             Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
00355             'encodeJsVar() with object'
00356         );
00357     }
00358 
00362     public function testEncodeJsVarInt() {
00363         $this->assertEquals(
00364             '123456',
00365             Xml::encodeJsVar( 123456 ),
00366             'encodeJsVar() with int'
00367         );
00368     }
00369 
00373     public function testEncodeJsVarFloat() {
00374         $this->assertEquals(
00375             '1.23456',
00376             Xml::encodeJsVar( 1.23456 ),
00377             'encodeJsVar() with float'
00378         );
00379     }
00380 
00384     public function testEncodeJsVarIntString() {
00385         $this->assertEquals(
00386             '"123456"',
00387             Xml::encodeJsVar( '123456' ),
00388             'encodeJsVar() with int-like string'
00389         );
00390     }
00391 
00395     public function testEncodeJsVarFloatString() {
00396         $this->assertEquals(
00397             '"1.23456"',
00398             Xml::encodeJsVar( '1.23456' ),
00399             'encodeJsVar() with float-like string'
00400         );
00401     }
00402 }