MediaWiki  REL1_23
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 
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> <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" .
00155                 '<option value="1">January</option>' . "\n" .
00156                 '<option value="2" selected="">February</option>' . "\n" .
00157                 '<option value="3">March</option>' . "\n" .
00158                 '<option value="4">April</option>' . "\n" .
00159                 '<option value="5">May</option>' . "\n" .
00160                 '<option value="6">June</option>' . "\n" .
00161                 '<option value="7">July</option>' . "\n" .
00162                 '<option value="8">August</option>' . "\n" .
00163                 '<option value="9">September</option>' . "\n" .
00164                 '<option value="10">October</option>' . "\n" .
00165                 '<option value="11">November</option>' . "\n" .
00166                 '<option value="12">December</option></select>',
00167             Xml::dateMenu( 2011, 02 ),
00168             "Date menu for february 2011"
00169         );
00170         $this->assertEquals(
00171             '<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" .
00172                 '<option value="1">January</option>' . "\n" .
00173                 '<option value="2">February</option>' . "\n" .
00174                 '<option value="3">March</option>' . "\n" .
00175                 '<option value="4">April</option>' . "\n" .
00176                 '<option value="5">May</option>' . "\n" .
00177                 '<option value="6">June</option>' . "\n" .
00178                 '<option value="7">July</option>' . "\n" .
00179                 '<option value="8">August</option>' . "\n" .
00180                 '<option value="9">September</option>' . "\n" .
00181                 '<option value="10">October</option>' . "\n" .
00182                 '<option value="11">November</option>' . "\n" .
00183                 '<option value="12">December</option></select>',
00184             Xml::dateMenu( 2011, -1 ),
00185             "Date menu with negative month for 'All'"
00186         );
00187         $this->assertEquals(
00188             Xml::dateMenu( $curYear, $curMonth ),
00189             Xml::dateMenu( '', $curMonth ),
00190             "Date menu year is the current one when not specified"
00191         );
00192 
00193         $wantedYear = $nextMonth == 1 ? $curYear : $prevYear;
00194         $this->assertEquals(
00195             Xml::dateMenu( $wantedYear, $nextMonth ),
00196             Xml::dateMenu( '', $nextMonth ),
00197             "Date menu next month is 11 months ago"
00198         );
00199 
00200         $this->assertEquals(
00201             '<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" .
00202                 '<option value="1">January</option>' . "\n" .
00203                 '<option value="2">February</option>' . "\n" .
00204                 '<option value="3">March</option>' . "\n" .
00205                 '<option value="4">April</option>' . "\n" .
00206                 '<option value="5">May</option>' . "\n" .
00207                 '<option value="6">June</option>' . "\n" .
00208                 '<option value="7">July</option>' . "\n" .
00209                 '<option value="8">August</option>' . "\n" .
00210                 '<option value="9">September</option>' . "\n" .
00211                 '<option value="10">October</option>' . "\n" .
00212                 '<option value="11">November</option>' . "\n" .
00213                 '<option value="12">December</option></select>',
00214             Xml::dateMenu( '', '' ),
00215             "Date menu with neither year or month"
00216         );
00217     }
00218 
00222     public function testTextareaNoContent() {
00223         $this->assertEquals(
00224             '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
00225             Xml::textarea( 'name', '' ),
00226             'textarea() with not content'
00227         );
00228     }
00229 
00233     public function testTextareaAttribs() {
00234         $this->assertEquals(
00235             '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
00236             Xml::textarea( 'name', '<txt>', 20, 10 ),
00237             'textarea() with custom attribs'
00238         );
00239     }
00240 
00244     public function testLabelCreation() {
00245         $this->assertEquals(
00246             '<label for="id">name</label>',
00247             Xml::label( 'name', 'id' ),
00248             'label() with no attribs'
00249         );
00250     }
00251 
00255     public function testLabelAttributeCanOnlyBeClassOrTitle() {
00256         $this->assertEquals(
00257             '<label for="id">name</label>',
00258             Xml::label( 'name', 'id', array( 'generated' => true ) ),
00259             'label() can not be given a generated attribute'
00260         );
00261         $this->assertEquals(
00262             '<label for="id" class="nice">name</label>',
00263             Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
00264             'label() can get a class attribute'
00265         );
00266         $this->assertEquals(
00267             '<label for="id" title="nice tooltip">name</label>',
00268             Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
00269             'label() can get a title attribute'
00270         );
00271         $this->assertEquals(
00272             '<label for="id" class="nice" title="nice tooltip">name</label>',
00273             Xml::label( 'name', 'id', array(
00274                     'generated' => true,
00275                     'class' => 'nice',
00276                     'title' => 'nice tooltip',
00277                     'anotherattr' => 'value',
00278                 )
00279             ),
00280             'label() skip all attributes but "class" and "title"'
00281         );
00282     }
00283 
00287     public function testLanguageSelector() {
00288         $select = Xml::languageSelector( 'en', true, null,
00289             array( 'id' => 'testlang' ), wfMessage( 'yourlanguage' ) );
00290         $this->assertEquals(
00291             '<label for="testlang">Language:</label>',
00292             $select[0]
00293         );
00294     }
00295 
00299     public function testEscapeJsStringSpecialChars() {
00300         $this->assertEquals(
00301             '\\\\\r\n',
00302             Xml::escapeJsString( "\\\r\n" ),
00303             'escapeJsString() with special characters'
00304         );
00305     }
00306 
00310     public function testEncodeJsVarBoolean() {
00311         $this->assertEquals(
00312             'true',
00313             Xml::encodeJsVar( true ),
00314             'encodeJsVar() with boolean'
00315         );
00316     }
00317 
00321     public function testEncodeJsVarNull() {
00322         $this->assertEquals(
00323             'null',
00324             Xml::encodeJsVar( null ),
00325             'encodeJsVar() with null'
00326         );
00327     }
00328 
00332     public function testEncodeJsVarArray() {
00333         $this->assertEquals(
00334             '["a",1]',
00335             Xml::encodeJsVar( array( 'a', 1 ) ),
00336             'encodeJsVar() with array'
00337         );
00338         $this->assertEquals(
00339             '{"a":"a","b":1}',
00340             Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
00341             'encodeJsVar() with associative array'
00342         );
00343     }
00344 
00348     public function testEncodeJsVarObject() {
00349         $this->assertEquals(
00350             '{"a":"a","b":1}',
00351             Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
00352             'encodeJsVar() with object'
00353         );
00354     }
00355 
00359     public function testEncodeJsVarInt() {
00360         $this->assertEquals(
00361             '123456',
00362             Xml::encodeJsVar( 123456 ),
00363             'encodeJsVar() with int'
00364         );
00365     }
00366 
00370     public function testEncodeJsVarFloat() {
00371         $this->assertEquals(
00372             '1.23456',
00373             Xml::encodeJsVar( 1.23456 ),
00374             'encodeJsVar() with float'
00375         );
00376     }
00377 
00381     public function testEncodeJsVarIntString() {
00382         $this->assertEquals(
00383             '"123456"',
00384             Xml::encodeJsVar( '123456' ),
00385             'encodeJsVar() with int-like string'
00386         );
00387     }
00388 
00392     public function testEncodeJsVarFloatString() {
00393         $this->assertEquals(
00394             '"1.23456"',
00395             Xml::encodeJsVar( '1.23456' ),
00396             'encodeJsVar() with float-like string'
00397         );
00398     }
00399 }