MediaWiki  master
TitleTest.php
Go to the documentation of this file.
00001 <?php
00002 
00008 class TitleTest extends MediaWikiTestCase {
00009 
00010         protected function setUp() {
00011                 parent::setUp();
00012 
00013                 $this->setMwGlobals( array(
00014                         'wgLanguageCode' => 'en',
00015                         'wgContLang' => Language::factory( 'en' ),
00016                         // User language
00017                         'wgLang' => Language::factory( 'en' ),
00018                         'wgAllowUserJs' => false,
00019                         'wgDefaultLanguageVariant' => false,
00020                 ) );
00021         }
00022 
00023         function testLegalChars() {
00024                 $titlechars = Title::legalChars();
00025 
00026                 foreach ( range( 1, 255 ) as $num ) {
00027                         $chr = chr( $num );
00028                         if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
00029                                 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
00030                         } else {
00031                                 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
00032                         }
00033                 }
00034         }
00035 
00039         function testBug31100FixSpecialName( $text, $expectedParam ) {
00040                 $title = Title::newFromText( $text );
00041                 $fixed = $title->fixSpecialName();
00042                 $stuff = explode( '/', $fixed->getDbKey(), 2 );
00043                 if ( count( $stuff ) == 2 ) {
00044                         $par = $stuff[1];
00045                 } else {
00046                         $par = null;
00047                 }
00048                 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
00049         }
00050 
00051         public static function provideBug31100() {
00052                 return array(
00053                         array( 'Special:Version', null ),
00054                         array( 'Special:Version/', '' ),
00055                         array( 'Special:Version/param', 'param' ),
00056                 );
00057         }
00058         
00068         function testIsValidMoveOperation( $source, $target, $expected ) {
00069                 $title = Title::newFromText( $source );
00070                 $nt = Title::newFromText( $target );
00071                 $errors = $title->isValidMoveOperation( $nt, false );
00072                 if ( $expected === true ) {
00073                         $this->assertTrue( $errors );
00074                 } else {
00075                         $errors = $this->flattenErrorsArray( $errors );
00076                         foreach ( (array)$expected as $error ) {
00077                                 $this->assertContains( $error, $errors );
00078                         }
00079                 }
00080         }
00081         
00082         function flattenErrorsArray( $errors ) {
00083                 $result = array();
00084                 foreach ( $errors as $error ) {
00085                         $result[] = $error[0];
00086                 }
00087                 return $result;
00088         }
00089         
00090         public static function provideTestIsValidMoveOperation() {
00091                 return array( 
00092                         array( 'Test', 'Test', 'selfmove' ),
00093                         array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
00094                 );
00095         }
00096 
00100         function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
00101                 global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
00102 
00103                 // Setup environnement for this test
00104                 $wgLanguageCode = $contLang;
00105                 $wgContLang = Language::factory( $contLang );
00106                 $wgLang = Language::factory( $lang );
00107                 $wgDefaultLanguageVariant = $variant;
00108                 $wgAllowUserJs = true;
00109 
00110                 $title = Title::newFromText( $titleText );
00111                 $this->assertInstanceOf( 'Title', $title,
00112                         "Test must be passed a valid title text, you gave '$titleText'"
00113                 );
00114                 $this->assertEquals( $expected,
00115                         $title->getPageViewLanguage()->getCode(),
00116                         $msg
00117                 );
00118         }
00119 
00120         function provideCasesForGetpageviewlanguage() {
00121                 # Format:
00122                 # - expected
00123                 # - Title name
00124                 # - wgContLang (expected in most case)
00125                 # - wgLang (on some specific pages)
00126                 # - wgDefaultLanguageVariant
00127                 # - Optional message
00128                 return array(
00129                         array( 'fr', 'Help:I_need_somebody', 'fr', 'fr', false ),
00130                         array( 'es', 'Help:I_need_somebody', 'es', 'zh-tw', false ),
00131                         array( 'zh', 'Help:I_need_somebody', 'zh', 'zh-tw', false ),
00132 
00133                         array( 'es',    'Help:I_need_somebody',      'es', 'zh-tw', 'zh-cn' ),
00134                         array( 'es',    'MediaWiki:About',           'es', 'zh-tw', 'zh-cn' ),
00135                         array( 'es',    'MediaWiki:About/',          'es', 'zh-tw', 'zh-cn' ),
00136                         array( 'de',    'MediaWiki:About/de',        'es', 'zh-tw', 'zh-cn' ),
00137                         array( 'en',    'MediaWiki:Common.js',       'es', 'zh-tw', 'zh-cn' ),
00138                         array( 'en',    'MediaWiki:Common.css',      'es', 'zh-tw', 'zh-cn' ),
00139                         array( 'en',    'User:JohnDoe/Common.js',    'es', 'zh-tw', 'zh-cn' ),
00140                         array( 'en',    'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
00141 
00142                         array( 'zh-cn', 'Help:I_need_somebody',      'zh', 'zh-tw', 'zh-cn' ),
00143                         array( 'zh',    'MediaWiki:About',           'zh', 'zh-tw', 'zh-cn' ),
00144                         array( 'zh',    'MediaWiki:About/',          'zh', 'zh-tw', 'zh-cn' ),
00145                         array( 'de',    'MediaWiki:About/de',        'zh', 'zh-tw', 'zh-cn' ),
00146                         array( 'zh-cn', 'MediaWiki:About/zh-cn',     'zh', 'zh-tw', 'zh-cn' ),
00147                         array( 'zh-tw', 'MediaWiki:About/zh-tw',     'zh', 'zh-tw', 'zh-cn' ),
00148                         array( 'en',    'MediaWiki:Common.js',       'zh', 'zh-tw', 'zh-cn' ),
00149                         array( 'en',    'MediaWiki:Common.css',      'zh', 'zh-tw', 'zh-cn' ),
00150                         array( 'en',    'User:JohnDoe/Common.js',    'zh', 'zh-tw', 'zh-cn' ),
00151                         array( 'en',    'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
00152 
00153                         array( 'zh-tw', 'Special:NewPages',       'es', 'zh-tw', 'zh-cn' ),
00154                         array( 'zh-tw', 'Special:NewPages',       'zh', 'zh-tw', 'zh-cn' ),
00155 
00156                 );
00157         }
00158 
00162         function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
00163                 $title = Title::newFromText( $title );
00164                 $this->assertEquals( $expected,
00165                         $title->getBaseText(),
00166                         $msg
00167                 );
00168         }
00169 
00170         function provideBaseTitleCases() {
00171                 return array(
00172                         # Title, expected base, optional message
00173                         array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
00174                         array('User:Foo/Bar/Baz', 'Foo/Bar' ),
00175                 );
00176         }
00177 
00181         function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
00182                 $title = Title::newFromText( $title );
00183                 $this->assertEquals( $expected,
00184                         $title->getRootText(),
00185                         $msg
00186                 );
00187         }
00188 
00189         public static function provideRootTitleCases() {
00190                 return array(
00191                         # Title, expected base, optional message
00192                         array('User:John_Doe/subOne/subTwo', 'John Doe' ),
00193                         array('User:Foo/Bar/Baz', 'Foo' ),
00194                 );
00195         }
00196 
00201         function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
00202                 $title = Title::newFromText( $title );
00203                 $this->assertEquals( $expected,
00204                         $title->getSubpageText(),
00205                         $msg
00206                 );
00207         }
00208 
00209         function provideSubpageTitleCases() {
00210                 return array(
00211                         # Title, expected base, optional message
00212                         array('User:John_Doe/subOne/subTwo', 'subTwo' ),
00213                         array('User:John_Doe/subOne', 'subOne' ),
00214                 );
00215         }
00216 
00217 }