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