MediaWiki  REL1_20
TitleTest.php
Go to the documentation of this file.
00001 <?php
00002 
00003 class TitleTest extends MediaWikiTestCase {
00004 
00005         function testLegalChars() {
00006                 $titlechars = Title::legalChars();
00007 
00008                 foreach ( range( 1, 255 ) as $num ) {
00009                         $chr = chr( $num );
00010                         if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
00011                                 $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
00012                         } else {
00013                                 $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
00014                         }
00015                 }
00016         }
00017 
00021         function testBug31100FixSpecialName( $text, $expectedParam ) {
00022                 $title = Title::newFromText( $text );
00023                 $fixed = $title->fixSpecialName();
00024                 $stuff = explode( '/', $fixed->getDbKey(), 2 );
00025                 if ( count( $stuff ) == 2 ) {
00026                         $par = $stuff[1];
00027                 } else {
00028                         $par = null;
00029                 }
00030                 $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
00031         }
00032 
00033         function dataBug31100() {
00034                 return array(
00035                         array( 'Special:Version', null ),
00036                         array( 'Special:Version/', '' ),
00037                         array( 'Special:Version/param', 'param' ),
00038                 );
00039         }
00040         
00050         function testIsValidMoveOperation( $source, $target, $expected ) {
00051                 $title = Title::newFromText( $source );
00052                 $nt = Title::newFromText( $target );
00053                 $errors = $title->isValidMoveOperation( $nt, false );
00054                 if ( $expected === true ) {
00055                         $this->assertTrue( $errors );
00056                 } else {
00057                         $errors = $this->flattenErrorsArray( $errors );
00058                         foreach ( (array)$expected as $error ) {
00059                                 $this->assertContains( $error, $errors );
00060                         }
00061                 }
00062         }
00063         
00064         function flattenErrorsArray( $errors ) {
00065                 $result = array();
00066                 foreach ( $errors as $error ) {
00067                         $result[] = $error[0];
00068                 }
00069                 return $result;
00070         }
00071         
00072         function dataTestIsValidMoveOperation() {
00073                 return array( 
00074                         array( 'Test', 'Test', 'selfmove' ),
00075                         array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
00076                 );
00077         }
00078         
00079         
00083         function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='' ) {
00084                 // Save globals
00085                 global $wgContLang, $wgLang, $wgAllowUserJs, $wgLanguageCode, $wgDefaultLanguageVariant;
00086                 $save['wgContLang']               = $wgContLang;
00087                 $save['wgLang']                   = $wgLang;
00088                 $save['wgAllowUserJs']            = $wgAllowUserJs;
00089                 $save['wgLanguageCode']           = $wgLanguageCode;
00090                 $save['wgDefaultLanguageVariant'] = $wgDefaultLanguageVariant;
00091 
00092                 // Setup test environnement:
00093                 $wgContLang = Language::factory( $contLang );
00094                 $wgLang     = Language::factory( $lang );
00095                 # To test out .js titles:
00096                 $wgAllowUserJs = true;
00097                 $wgLanguageCode = $contLang;
00098                 $wgDefaultLanguageVariant = $variant;
00099 
00100                 $title = Title::newFromText( $titleText );
00101                 $this->assertInstanceOf( 'Title', $title,
00102                         "Test must be passed a valid title text, you gave '$titleText'"
00103                 );
00104                 $this->assertEquals( $expected,
00105                         $title->getPageViewLanguage()->getCode(),
00106                         $msg
00107                 );
00108 
00109                 // Restore globals
00110                 $wgContLang               = $save['wgContLang'];
00111                 $wgLang                   = $save['wgLang'];
00112                 $wgAllowUserJs            = $save['wgAllowUserJs'];
00113                 $wgLanguageCode           = $save['wgLanguageCode'];
00114                 $wgDefaultLanguageVariant = $save['wgDefaultLanguageVariant'];
00115         }
00116 
00117         function provideCasesForGetpageviewlanguage() {
00118                 # Format:
00119                 # - expected
00120                 # - Title name
00121                 # - wgContLang (expected in most case)
00122                 # - wgLang (on some specific pages)
00123                 # - wgDefaultLanguageVariant
00124                 # - Optional message
00125                 return array(
00126                         array( 'fr', 'Main_page', 'fr', 'fr', false ),
00127                         array( 'es', 'Main_page', 'es', 'zh-tw', false ),
00128                         array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
00129 
00130                         array( 'es',    'Main_page',                 'es', 'zh-tw', 'zh-cn' ),
00131                         array( 'es',    'MediaWiki:About',           'es', 'zh-tw', 'zh-cn' ),
00132                         array( 'es',    'MediaWiki:About/',          'es', 'zh-tw', 'zh-cn' ),
00133                         array( 'de',    'MediaWiki:About/de',        'es', 'zh-tw', 'zh-cn' ),
00134                         array( 'en',    'MediaWiki:Common.js',       'es', 'zh-tw', 'zh-cn' ),
00135                         array( 'en',    'MediaWiki:Common.css',      'es', 'zh-tw', 'zh-cn' ),
00136                         array( 'en',    'User:JohnDoe/Common.js',    'es', 'zh-tw', 'zh-cn' ),
00137                         array( 'en',    'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
00138 
00139                         array( 'zh-cn', 'Main_page',                 'zh', 'zh-tw', 'zh-cn' ),
00140                         array( 'zh',    'MediaWiki:About',           'zh', 'zh-tw', 'zh-cn' ),
00141                         array( 'zh',    'MediaWiki:About/',          'zh', 'zh-tw', 'zh-cn' ),
00142                         array( 'de',    'MediaWiki:About/de',        'zh', 'zh-tw', 'zh-cn' ),
00143                         array( 'zh-cn', 'MediaWiki:About/zh-cn',     'zh', 'zh-tw', 'zh-cn' ),
00144                         array( 'zh-tw', 'MediaWiki:About/zh-tw',     'zh', 'zh-tw', 'zh-cn' ),
00145                         array( 'en',    'MediaWiki:Common.js',       'zh', 'zh-tw', 'zh-cn' ),
00146                         array( 'en',    'MediaWiki:Common.css',      'zh', 'zh-tw', 'zh-cn' ),
00147                         array( 'en',    'User:JohnDoe/Common.js',    'zh', 'zh-tw', 'zh-cn' ),
00148                         array( 'en',    'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
00149 
00150                         array( 'zh-tw', 'Special:NewPages',       'es', 'zh-tw', 'zh-cn' ),
00151                         array( 'zh-tw', 'Special:NewPages',       'zh', 'zh-tw', 'zh-cn' ),
00152 
00153                 );
00154         }
00155 }