MediaWiki  REL1_19
HtmlTest.php
Go to the documentation of this file.
00001 <?php
00004 class HtmlTest extends MediaWikiTestCase {
00005         private static $oldLang;
00006         private static $oldContLang;
00007         private static $oldLanguageCode;
00008         private static $oldNamespaces;
00009 
00010         public function setUp() {
00011                 global $wgLang, $wgContLang, $wgLanguageCode;
00012                 
00013                 self::$oldLang = $wgLang;
00014                 self::$oldContLang = $wgContLang;
00015                 self::$oldNamespaces = $wgContLang->getNamespaces();
00016                 self::$oldLanguageCode = $wgLanguageCode;
00017                 
00018                 $wgLanguageCode = 'en';
00019                 $wgContLang = $wgLang = Language::factory( $wgLanguageCode );
00020 
00021                 // Hardcode namespaces during test runs,
00022                 // so that html output based on existing namespaces
00023                 // can be properly evaluated.
00024                 $wgContLang->setNamespaces( array(
00025                         -2 => 'Media',
00026                         -1 => 'Special',
00027                         0  => '',
00028                         1  => 'Talk',
00029                         2  => 'User',
00030                         3  => 'User_talk',
00031                         4  => 'MyWiki',
00032                         5  => 'MyWiki_Talk',
00033                         6  => 'File',
00034                         7  => 'File_talk',
00035                         8  => 'MediaWiki',
00036                         9  => 'MediaWiki_talk',
00037                         10  => 'Template',
00038                         11  => 'Template_talk',
00039                         100  => 'Custom',
00040                         101  => 'Custom_talk',
00041                 ) );
00042         }
00043         
00044         public function tearDown() {
00045                 global $wgLang, $wgContLang, $wgLanguageCode;
00046 
00047                 $wgContLang->setNamespaces( self::$oldNamespaces );
00048                 $wgLang = self::$oldLang;
00049                 $wgContLang = self::$oldContLang;
00050                 $wgLanguageCode = self::$oldLanguageCode;
00051         }
00052 
00053         public function testExpandAttributesSkipsNullAndFalse() {
00054                 
00055                 ### EMPTY ########
00056                 $this->AssertEmpty(
00057                         Html::expandAttributes( array( 'foo' => null ) ),
00058                         'skip keys with null value'
00059                 );
00060                 $this->AssertEmpty(
00061                         Html::expandAttributes( array( 'foo' => false ) ),
00062                         'skip keys with false value'
00063                 );
00064                 $this->AssertNotEmpty(
00065                         Html::expandAttributes( array( 'foo' => '' ) ),
00066                         'keep keys with an empty string'
00067                 );
00068         }
00069 
00070         public function testExpandAttributesForBooleans() {
00071                 global $wgHtml5;
00072                 $this->AssertEquals(
00073                         '',
00074                         Html::expandAttributes( array( 'selected' => false ) ),
00075                         'Boolean attributes do not generates output when value is false'
00076                 );
00077                 $this->AssertEquals(
00078                         '',
00079                         Html::expandAttributes( array( 'selected' => null ) ),
00080                         'Boolean attributes do not generates output when value is null'
00081                 );
00082 
00083                 $this->AssertEquals(
00084                         $wgHtml5 ? ' selected=""' : ' selected="selected"',
00085                         Html::expandAttributes( array( 'selected' => true ) ),
00086                         'Boolean attributes skip value output'
00087                 );
00088                 $this->AssertEquals(
00089                         $wgHtml5 ? ' selected=""' : ' selected="selected"',
00090                         Html::expandAttributes( array( 'selected' ) ),
00091                         'Boolean attributes (ex: selected) do not need a value'
00092                 );
00093         }
00094 
00099         public function testExpandAttributesVariousExpansions() {
00100                 ### NOT EMPTY ####
00101                 $this->AssertEquals(
00102                         ' empty_string=""',
00103                         Html::expandAttributes( array( 'empty_string' => '' ) ),
00104                         'Value with an empty string'
00105                 );
00106                 $this->AssertEquals(
00107                         ' key="value"',
00108                         Html::expandAttributes( array( 'key' => 'value' ) ),
00109                         'Value is a string'
00110                 );
00111                 $this->AssertEquals(
00112                         ' one="1"',
00113                         Html::expandAttributes( array( 'one' => 1 ) ),
00114                         'Value is a numeric one'
00115                 );
00116                 $this->AssertEquals(
00117                         ' zero="0"',
00118                         Html::expandAttributes( array( 'zero' => 0 ) ),
00119                         'Value is a numeric zero'
00120                 );
00121         }
00122 
00128         public function testExpandAttributesListValueAttributes() {
00129                 ### STRING VALUES
00130                 $this->AssertEquals(
00131                         ' class="redundant spaces here"',
00132                         Html::expandAttributes( array( 'class' => ' redundant  spaces  here  ' ) ),
00133                         'Normalization should strip redundant spaces'
00134                 );
00135                 $this->AssertEquals(
00136                         ' class="foo bar"',
00137                         Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
00138                         'Normalization should remove duplicates in string-lists'
00139                 );
00140                 ### "EMPTY" ARRAY VALUES
00141                 $this->AssertEquals(
00142                         ' class=""',
00143                         Html::expandAttributes( array( 'class' => array() ) ),
00144                         'Value with an empty array'
00145                 );
00146                 $this->AssertEquals(
00147                         ' class=""',
00148                         Html::expandAttributes( array( 'class' => array( null, '', ' ', '  ' ) ) ),
00149                         'Array with null, empty string and spaces'
00150                 );
00151                 ### NON-EMPTY ARRAY VALUES
00152                 $this->AssertEquals(
00153                         ' class="foo bar"',
00154                         Html::expandAttributes( array( 'class' => array(
00155                                 'foo',
00156                                 'bar',
00157                                 'foo',
00158                                 'bar',
00159                                 'bar',
00160                         ) ) ),
00161                         'Normalization should remove duplicates in the array'
00162                 );
00163                 $this->AssertEquals(
00164                         ' class="foo bar"',
00165                         Html::expandAttributes( array( 'class' => array(
00166                                 'foo bar',
00167                                 'bar foo',
00168                                 'foo',
00169                                 'bar bar',
00170                         ) ) ),
00171                         'Normalization should remove duplicates in string-lists in the array'
00172                 );
00173         }
00174 
00179         function testExpandAttributesSpaceSeparatedAttributesWithBoolean() {
00180                 $this->assertEquals(
00181                         ' class="booltrue one"',
00182                         Html::expandAttributes( array( 'class' => array(
00183                                 'booltrue' => true,
00184                                 'one' => 1,
00185 
00186                                 # Method use isset() internally, make sure we do discard
00187                             # attributes values which have been assigned well known values
00188                                 'emptystring' => '',
00189                                 'boolfalse' => false,
00190                                 'zero' => 0,
00191                                 'null' => null,
00192                         )))
00193                 );
00194         }
00195 
00203         function testValueIsAuthoritativeInSpaceSeparatedAttributesArrays() {
00204                 $this->assertEquals(
00205                         ' class=""',
00206                         Html::expandAttributes( array( 'class' => array(
00207                                 'GREEN',
00208                                 'GREEN' => false,
00209                                 'GREEN',
00210                         )))
00211                 );
00212         }
00213 
00214         function testNamespaceSelector() {
00215                 $this->assertEquals(
00216                         '<select id="namespace" name="namespace">' . "\n" .
00217 '<option value="0">(Main)</option>' . "\n" .
00218 '<option value="1">Talk</option>' . "\n" .
00219 '<option value="2">User</option>' . "\n" .
00220 '<option value="3">User talk</option>' . "\n" .
00221 '<option value="4">MyWiki</option>' . "\n" .
00222 '<option value="5">MyWiki Talk</option>' . "\n" .
00223 '<option value="6">File</option>' . "\n" .
00224 '<option value="7">File talk</option>' . "\n" .
00225 '<option value="8">MediaWiki</option>' . "\n" .
00226 '<option value="9">MediaWiki talk</option>' . "\n" .
00227 '<option value="10">Template</option>' . "\n" .
00228 '<option value="11">Template talk</option>' . "\n" .
00229 '<option value="100">Custom</option>' . "\n" .
00230 '<option value="101">Custom talk</option>' . "\n" .
00231 '</select>',
00232                         Html::namespaceSelector(),
00233                         'Basic namespace selector without custom options'
00234                 );
00235                 $this->assertEquals(
00236                         '<label for="mw-test-namespace">Select a namespace:</label>&#160;' .
00237 '<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
00238 '<option value="all">all</option>' . "\n" .
00239 '<option value="0">(Main)</option>' . "\n" .
00240 '<option value="1">Talk</option>' . "\n" .
00241 '<option value="2" selected="">User</option>' . "\n" .
00242 '<option value="3">User talk</option>' . "\n" .
00243 '<option value="4">MyWiki</option>' . "\n" .
00244 '<option value="5">MyWiki Talk</option>' . "\n" .
00245 '<option value="6">File</option>' . "\n" .
00246 '<option value="7">File talk</option>' . "\n" .
00247 '<option value="8">MediaWiki</option>' . "\n" .
00248 '<option value="9">MediaWiki talk</option>' . "\n" .
00249 '<option value="10">Template</option>' . "\n" .
00250 '<option value="11">Template talk</option>' . "\n" .
00251 '<option value="100">Custom</option>' . "\n" .
00252 '<option value="101">Custom talk</option>' . "\n" .
00253 '</select>',
00254                         Html::namespaceSelector(
00255                                 array( 'selected' => '2', 'all' => 'all', 'label' => 'Select a namespace:' ),
00256                                 array( 'name' => 'wpNamespace', 'id' => 'mw-test-namespace' )
00257                         ),
00258                         'Basic namespace selector with custom values'
00259                 );
00260         }
00261 
00262         function testNamespaceSelectorIdAndNameDefaultsAttributes() {
00263 
00264                 $this->assertNsSelectorIdAndName(
00265                         'namespace', 'namespace',
00266                         Html::namespaceSelector( array(), array(
00267                                 # neither 'id' nor 'name' key given
00268                         )),
00269                         "Neither 'id' nor 'name' key given"
00270                 );
00271 
00272                 $this->assertNsSelectorIdAndName(
00273                         'namespace', 'select_name',
00274                         Html::namespaceSelector( array(), array(
00275                                 'name' => 'select_name',
00276                                 # no 'id' key given
00277                         )),
00278                         "No 'id' key given, 'name' given"
00279                 );
00280 
00281                 $this->assertNsSelectorIdAndName(
00282                         'select_id', 'namespace',
00283                         Html::namespaceSelector( array(), array(
00284                                 'id' => 'select_id',
00285                                 # no 'name' key given
00286                         )),
00287                         "'id' given, no 'name' key given"
00288                 );
00289 
00290                 $this->assertNsSelectorIdAndName(
00291                         'select_id', 'select_name',
00292                         Html::namespaceSelector( array(), array(
00293                                 'id'   => 'select_id',
00294                                 'name' => 'select_name',
00295                         )),
00296                         "Both 'id' and 'name' given"
00297                 );
00298         }
00299 
00310         function assertNsSelectorIdAndName( $expectedId, $expectedName, $html, $msg = '' ) {
00311                 $actualId = 'namespace';
00312                 if( 1 === preg_match( '/id="(.+?)"/', $html, $m ) ) {
00313                         $actualId = $m[1];
00314                 }
00315 
00316                 $actualName = 'namespace';
00317                 if( 1 === preg_match( '/name="(.+?)"/', $html, $m ) ) {
00318                         $actualName = $m[1];
00319                 }
00320                 $this->assertEquals(
00321                         array( #expected
00322                                 'id'   => $expectedId,
00323                                 'name' => $expectedName,
00324                         ),
00325                         array( #actual
00326                                 'id'   => $actualId,
00327                                 'name' => $actualName,
00328                         ),
00329                         'Html::namespaceSelector() got wrong id and/or name attribute(s). ' . $msg
00330                 );
00331         }
00332 
00333         public function testFormValidationBlacklist() {
00334                 $this->assertEmpty(
00335                         Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 2 ) ),
00336                         'Blacklist form validation attributes.'
00337                 );
00338                 $this->assertEquals(
00339                         ' step="any"',
00340                         Html::expandAttributes( array( 'min' => 1, 'max' => 100, 'pattern' => 'abc', 'required' => true, 'step' => 'any' ) ),
00341                         "Allow special case 'step=\"any\"'."
00342                 );
00343         }
00344 
00345 }