MediaWiki  REL1_19
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         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 &lt;there&gt; you &amp; 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( '&quot;&gt;&lt;', Xml::escapeTagsOnly( '"><' ),
00091                         'replace " > and < with their HTML entitites'
00092                 );
00093         }
00094 
00095         function testElementAttributes() {
00096                 $this->assertEquals(
00097                         '<element key="value" <>="&lt;&gt;">',
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         function testNamespaceSelector() {
00197                 $this->assertEquals(
00198                         '<select class="namespaceselector" id="namespace" name="namespace">' . "\n" .
00199 '<option value="0">(Main)</option>' . "\n" .
00200 '<option value="1">Talk</option>' . "\n" .
00201 '<option value="2">User</option>' . "\n" .
00202 '<option value="3">User talk</option>' . "\n" .
00203 '<option value="4">MyWiki</option>' . "\n" .
00204 '<option value="5">MyWiki Talk</option>' . "\n" .
00205 '<option value="6">File</option>' . "\n" .
00206 '<option value="7">File talk</option>' . "\n" .
00207 '<option value="8">MediaWiki</option>' . "\n" .
00208 '<option value="9">MediaWiki talk</option>' . "\n" .
00209 '<option value="10">Template</option>' . "\n" .
00210 '<option value="11">Template talk</option>' . "\n" .
00211 '<option value="100">Custom</option>' . "\n" .
00212 '<option value="101">Custom talk</option>' . "\n" .
00213 '</select>',
00214                         Xml::namespaceSelector(),
00215                         'Basic namespace selector without custom options'
00216                 );
00217                 $this->assertEquals(
00218                         '<label for="namespace">Select a namespace:</label>' .
00219 '&#160;<select class="namespaceselector" id="namespace" name="myname">' . "\n" .
00220 '<option value="all">all</option>' . "\n" .
00221 '<option value="0">(Main)</option>' . "\n" .
00222 '<option value="1">Talk</option>' . "\n" .
00223 '<option value="2" selected="">User</option>' . "\n" .
00224 '<option value="3">User talk</option>' . "\n" .
00225 '<option value="4">MyWiki</option>' . "\n" .
00226 '<option value="5">MyWiki Talk</option>' . "\n" .
00227 '<option value="6">File</option>' . "\n" .
00228 '<option value="7">File talk</option>' . "\n" .
00229 '<option value="8">MediaWiki</option>' . "\n" .
00230 '<option value="9">MediaWiki talk</option>' . "\n" .
00231 '<option value="10">Template</option>' . "\n" .
00232 '<option value="11">Template talk</option>' . "\n" .
00233 '<option value="100">Custom</option>' . "\n" .
00234 '<option value="101">Custom talk</option>' . "\n" .
00235 '</select>',
00236                         Xml::namespaceSelector( $selected = '2', $all = 'all', $element_name = 'myname', $label = 'Select a namespace:' ),
00237                         'Basic namespace selector with custom values'
00238                 );
00239         }
00240 
00241 
00242         #
00243         # textarea
00244         #
00245         function testTextareaNoContent() {
00246                 $this->assertEquals(
00247                         '<textarea name="name" id="name" cols="40" rows="5"></textarea>',
00248                         Xml::textarea( 'name', '' ),
00249                         'textarea() with not content'
00250                 );
00251         }
00252 
00253         function testTextareaAttribs() {
00254                 $this->assertEquals(
00255                         '<textarea name="name" id="name" cols="20" rows="10">&lt;txt&gt;</textarea>',
00256                         Xml::textarea( 'name', '<txt>', 20, 10 ),
00257                         'textarea() with custom attribs'
00258                 );
00259         }
00260 
00261         #
00262         # input and label
00263         #
00264         function testLabelCreation() {
00265                 $this->assertEquals(
00266                         '<label for="id">name</label>',
00267                         Xml::label( 'name', 'id' ),
00268                         'label() with no attribs'
00269                 );
00270         }
00271         function testLabelAttributeCanOnlyBeClassOrTitle() {
00272                 $this->assertEquals(
00273                         '<label for="id">name</label>',
00274                         Xml::label( 'name', 'id', array( 'generated' => true ) ),
00275                         'label() can not be given a generated attribute'
00276                 );
00277                 $this->assertEquals(
00278                         '<label for="id" class="nice">name</label>',
00279                         Xml::label( 'name', 'id', array( 'class' => 'nice' ) ),
00280                         'label() can get a class attribute'
00281                 );
00282                 $this->assertEquals(
00283                         '<label for="id" title="nice tooltip">name</label>',
00284                         Xml::label( 'name', 'id', array( 'title' => 'nice tooltip' ) ),
00285                         'label() can get a title attribute'
00286                 );
00287                 $this->assertEquals(
00288                         '<label for="id" class="nice" title="nice tooltip">name</label>',
00289                         Xml::label( 'name', 'id', array(
00290                                 'generated' => true,
00291                                 'class' => 'nice',
00292                                 'title' => 'nice tooltip',
00293                                 'anotherattr' => 'value',
00294                                 )
00295                         ),
00296                         'label() skip all attributes but "class" and "title"'
00297                 );
00298         }
00299 
00300         #
00301         # JS
00302         #
00303         function testEscapeJsStringSpecialChars() {
00304                 $this->assertEquals(
00305                         '\\\\\r\n',
00306                         Xml::escapeJsString( "\\\r\n" ),
00307                         'escapeJsString() with special characters'
00308                 );
00309         }
00310 
00311         function testEncodeJsVarBoolean() {
00312                 $this->assertEquals(
00313                         'true',
00314                         Xml::encodeJsVar( true ),
00315                         'encodeJsVar() with boolean'
00316                 );
00317         }
00318 
00319         function testEncodeJsVarNull() {
00320                 $this->assertEquals(
00321                         'null',
00322                         Xml::encodeJsVar( null ),
00323                         'encodeJsVar() with null'
00324                 );
00325         }
00326 
00327         function testEncodeJsVarArray() {
00328                 $this->assertEquals(
00329                         '["a",1]',
00330                         Xml::encodeJsVar( array( 'a', 1 ) ),
00331                         'encodeJsVar() with array'
00332                 );
00333                 $this->assertEquals(
00334                         '{"a":"a","b":1}',
00335                         Xml::encodeJsVar( array( 'a' => 'a', 'b' => 1 ) ),
00336                         'encodeJsVar() with associative array'
00337                 );
00338         }
00339 
00340         function testEncodeJsVarObject() {
00341                 $this->assertEquals(
00342                         '{"a":"a","b":1}',
00343                         Xml::encodeJsVar( (object)array( 'a' => 'a', 'b' => 1 ) ),
00344                         'encodeJsVar() with object'
00345                 );
00346         }
00347 
00348         function testEncodeJsVarInt() {
00349                 $this->assertEquals(
00350                         '123456',
00351                         Xml::encodeJsVar( 123456 ),
00352                         'encodeJsVar() with int'
00353                 );
00354         }
00355 
00356         function testEncodeJsVarFloat() {
00357                 $this->assertEquals(
00358                         '1.23456',
00359                         Xml::encodeJsVar( 1.23456 ),
00360                         'encodeJsVar() with float'
00361                 );
00362         }
00363 
00364         function testEncodeJsVarIntString() {
00365                 $this->assertEquals(
00366                         '"123456"',
00367                         Xml::encodeJsVar( '123456' ),
00368                         'encodeJsVar() with int-like string'
00369                 );
00370         }
00371 
00372         function testEncodeJsVarFloatString() {
00373                 $this->assertEquals(
00374                         '"1.23456"',
00375                         Xml::encodeJsVar( '1.23456' ),
00376                         'encodeJsVar() with float-like string'
00377                 );
00378         }
00379 }