MediaWiki
REL1_21
|
00001 <?php 00002 00008 class TitleTest extends MediaWikiTestCase { 00009 protected function setUp() { 00010 parent::setUp(); 00011 00012 $this->setMwGlobals( array( 00013 'wgLanguageCode' => 'en', 00014 'wgContLang' => Language::factory( 'en' ), 00015 // User language 00016 'wgLang' => Language::factory( 'en' ), 00017 'wgAllowUserJs' => false, 00018 'wgDefaultLanguageVariant' => false, 00019 ) ); 00020 } 00021 00022 function testLegalChars() { 00023 $titlechars = Title::legalChars(); 00024 00025 foreach ( range( 1, 255 ) as $num ) { 00026 $chr = chr( $num ); 00027 if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) { 00028 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" ); 00029 } else { 00030 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" ); 00031 } 00032 } 00033 } 00034 00038 function testBug31100FixSpecialName( $text, $expectedParam ) { 00039 $title = Title::newFromText( $text ); 00040 $fixed = $title->fixSpecialName(); 00041 $stuff = explode( '/', $fixed->getDbKey(), 2 ); 00042 if ( count( $stuff ) == 2 ) { 00043 $par = $stuff[1]; 00044 } else { 00045 $par = null; 00046 } 00047 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" ); 00048 } 00049 00050 public static function provideBug31100() { 00051 return array( 00052 array( 'Special:Version', null ), 00053 array( 'Special:Version/', '' ), 00054 array( 'Special:Version/param', 'param' ), 00055 ); 00056 } 00057 00067 function testIsValidMoveOperation( $source, $target, $expected ) { 00068 $title = Title::newFromText( $source ); 00069 $nt = Title::newFromText( $target ); 00070 $errors = $title->isValidMoveOperation( $nt, false ); 00071 if ( $expected === true ) { 00072 $this->assertTrue( $errors ); 00073 } else { 00074 $errors = $this->flattenErrorsArray( $errors ); 00075 foreach ( (array)$expected as $error ) { 00076 $this->assertContains( $error, $errors ); 00077 } 00078 } 00079 } 00080 00084 function dataTestIsValidMoveOperation() { 00085 return array( 00086 array( 'Test', 'Test', 'selfmove' ), 00087 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ) 00088 ); 00089 } 00090 00102 function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) { 00103 // $wgWhitelistReadRegexp must be an array. Since the provided test cases 00104 // usually have only one regex, it is more concise to write the lonely regex 00105 // as a string. Thus we cast to an array() to honor $wgWhitelistReadRegexp 00106 // type requisite. 00107 if ( is_string( $whitelistRegexp ) ) { 00108 $whitelistRegexp = array( $whitelistRegexp ); 00109 } 00110 00111 $title = Title::newFromDBkey( $source ); 00112 00113 global $wgGroupPermissions; 00114 $oldPermissions = $wgGroupPermissions; 00115 // Disallow all so we can ensure our regex works 00116 $wgGroupPermissions = array(); 00117 $wgGroupPermissions['*']['read'] = false; 00118 00119 global $wgWhitelistRead; 00120 $oldWhitelist = $wgWhitelistRead; 00121 // Undo any LocalSettings explicite whitelists so they won't cause a 00122 // failing test to succeed. Set it to some random non sense just 00123 // to make sure we properly test Title::checkReadPermissions() 00124 $wgWhitelistRead = array( 'some random non sense title' ); 00125 00126 global $wgWhitelistReadRegexp; 00127 $oldWhitelistRegexp = $wgWhitelistReadRegexp; 00128 $wgWhitelistReadRegexp = $whitelistRegexp; 00129 00130 // Just use $wgUser which in test is a user object for '127.0.0.1' 00131 global $wgUser; 00132 // Invalidate user rights cache to take in account $wgGroupPermissions 00133 // change above. 00134 $wgUser->clearInstanceCache(); 00135 $errors = $title->userCan( $action, $wgUser ); 00136 00137 // Restore globals 00138 $wgGroupPermissions = $oldPermissions; 00139 $wgWhitelistRead = $oldWhitelist; 00140 $wgWhitelistReadRegexp = $oldWhitelistRegexp; 00141 00142 if ( is_bool( $expected ) ) { 00143 # Forge the assertion message depending on the assertion expectation 00144 $allowableness = $expected 00145 ? " should be allowed" 00146 : " should NOT be allowed"; 00147 $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." ); 00148 } else { 00149 $errors = $this->flattenErrorsArray( $errors ); 00150 foreach ( (array)$expected as $error ) { 00151 $this->assertContains( $error, $errors ); 00152 } 00153 } 00154 } 00155 00159 function dataWgWhitelistReadRegexp() { 00160 $ALLOWED = true; 00161 $DISALLOWED = false; 00162 00163 return array( 00164 // Everything, if this doesn't work, we're really in trouble 00165 array( '/.*/', 'Main_Page', 'read', $ALLOWED ), 00166 array( '/.*/', 'Main_Page', 'edit', $DISALLOWED ), 00167 00168 // We validate against the title name, not the db key 00169 array( '/^Main_Page$/', 'Main_Page', 'read', $DISALLOWED ), 00170 // Main page 00171 array( '/^Main/', 'Main_Page', 'read', $ALLOWED ), 00172 array( '/^Main.*/', 'Main_Page', 'read', $ALLOWED ), 00173 // With spaces 00174 array( '/Mic\sCheck/', 'Mic Check', 'read', $ALLOWED ), 00175 // Unicode multibyte 00176 // ...without unicode modifier 00177 array( '/Unicode Test . Yes/', 'Unicode Test Ñ Yes', 'read', $DISALLOWED ), 00178 // ...with unicode modifier 00179 array( '/Unicode Test . Yes/u', 'Unicode Test Ñ Yes', 'read', $ALLOWED ), 00180 // Case insensitive 00181 array( '/MiC ChEcK/', 'mic check', 'read', $DISALLOWED ), 00182 array( '/MiC ChEcK/i', 'mic check', 'read', $ALLOWED ), 00183 00184 // From DefaultSettings.php: 00185 array( "@^UsEr.*@i", 'User is banned', 'read', $ALLOWED ), 00186 array( "@^UsEr.*@i", 'User:John Doe', 'read', $ALLOWED ), 00187 00188 // With namespaces: 00189 array( '/^Special:NewPages$/', 'Special:NewPages', 'read', $ALLOWED ), 00190 array( null, 'Special:Newpages', 'read', $DISALLOWED ), 00191 00192 ); 00193 } 00194 00195 function flattenErrorsArray( $errors ) { 00196 $result = array(); 00197 foreach ( $errors as $error ) { 00198 $result[] = $error[0]; 00199 } 00200 return $result; 00201 } 00202 00203 public static function provideTestIsValidMoveOperation() { 00204 return array( 00205 array( 'Test', 'Test', 'selfmove' ), 00206 array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ) 00207 ); 00208 } 00209 00213 function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) { 00214 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs; 00215 00216 // Setup environnement for this test 00217 $wgLanguageCode = $contLang; 00218 $wgContLang = Language::factory( $contLang ); 00219 $wgLang = Language::factory( $lang ); 00220 $wgDefaultLanguageVariant = $variant; 00221 $wgAllowUserJs = true; 00222 00223 $title = Title::newFromText( $titleText ); 00224 $this->assertInstanceOf( 'Title', $title, 00225 "Test must be passed a valid title text, you gave '$titleText'" 00226 ); 00227 $this->assertEquals( $expected, 00228 $title->getPageViewLanguage()->getCode(), 00229 $msg 00230 ); 00231 } 00232 00233 function provideCasesForGetpageviewlanguage() { 00234 # Format: 00235 # - expected 00236 # - Title name 00237 # - wgContLang (expected in most case) 00238 # - wgLang (on some specific pages) 00239 # - wgDefaultLanguageVariant 00240 # - Optional message 00241 return array( 00242 array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ), 00243 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ), 00244 array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ), 00245 00246 array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', 'zh-cn' ), 00247 array( 'es', 'MediaWiki:About', 'es', 'zh-tw', 'zh-cn' ), 00248 array( 'es', 'MediaWiki:About/', 'es', 'zh-tw', 'zh-cn' ), 00249 array( 'de', 'MediaWiki:About/de', 'es', 'zh-tw', 'zh-cn' ), 00250 array( 'en', 'MediaWiki:Common.js', 'es', 'zh-tw', 'zh-cn' ), 00251 array( 'en', 'MediaWiki:Common.css', 'es', 'zh-tw', 'zh-cn' ), 00252 array( 'en', 'User:JohnDoe/Common.js', 'es', 'zh-tw', 'zh-cn' ), 00253 array( 'en', 'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ), 00254 00255 array( 'zh-cn', 'Help:I_need_somebody', 'zh', 'zh-tw', 'zh-cn' ), 00256 array( 'zh', 'MediaWiki:About', 'zh', 'zh-tw', 'zh-cn' ), 00257 array( 'zh', 'MediaWiki:About/', 'zh', 'zh-tw', 'zh-cn' ), 00258 array( 'de', 'MediaWiki:About/de', 'zh', 'zh-tw', 'zh-cn' ), 00259 array( 'zh-cn', 'MediaWiki:About/zh-cn', 'zh', 'zh-tw', 'zh-cn' ), 00260 array( 'zh-tw', 'MediaWiki:About/zh-tw', 'zh', 'zh-tw', 'zh-cn' ), 00261 array( 'en', 'MediaWiki:Common.js', 'zh', 'zh-tw', 'zh-cn' ), 00262 array( 'en', 'MediaWiki:Common.css', 'zh', 'zh-tw', 'zh-cn' ), 00263 array( 'en', 'User:JohnDoe/Common.js', 'zh', 'zh-tw', 'zh-cn' ), 00264 array( 'en', 'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ), 00265 00266 array( 'zh-tw', 'Special:NewPages', 'es', 'zh-tw', 'zh-cn' ), 00267 array( 'zh-tw', 'Special:NewPages', 'zh', 'zh-tw', 'zh-cn' ), 00268 00269 ); 00270 } 00271 00275 function testExtractingBaseTextFromTitle( $title, $expected, $msg = '' ) { 00276 $title = Title::newFromText( $title ); 00277 $this->assertEquals( $expected, 00278 $title->getBaseText(), 00279 $msg 00280 ); 00281 } 00282 00283 function provideBaseTitleCases() { 00284 return array( 00285 # Title, expected base, optional message 00286 array( 'User:John_Doe/subOne/subTwo', 'John Doe/subOne' ), 00287 array( 'User:Foo/Bar/Baz', 'Foo/Bar' ), 00288 ); 00289 } 00290 00294 function testExtractingRootTextFromTitle( $title, $expected, $msg = '' ) { 00295 $title = Title::newFromText( $title ); 00296 $this->assertEquals( $expected, 00297 $title->getRootText(), 00298 $msg 00299 ); 00300 } 00301 00302 public static function provideRootTitleCases() { 00303 return array( 00304 # Title, expected base, optional message 00305 array( 'User:John_Doe/subOne/subTwo', 'John Doe' ), 00306 array( 'User:Foo/Bar/Baz', 'Foo' ), 00307 ); 00308 } 00309 00314 function testExtractingSubpageTextFromTitle( $title, $expected, $msg = '' ) { 00315 $title = Title::newFromText( $title ); 00316 $this->assertEquals( $expected, 00317 $title->getSubpageText(), 00318 $msg 00319 ); 00320 } 00321 00322 function provideSubpageTitleCases() { 00323 return array( 00324 # Title, expected base, optional message 00325 array( 'User:John_Doe/subOne/subTwo', 'subTwo' ), 00326 array( 'User:John_Doe/subOne', 'subOne' ), 00327 ); 00328 } 00329 }