merge latest master into Wikidata branch
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
index e322293..ff535c3 100644 (file)
@@ -1,5 +1,10 @@
 <?php
 
+/**
+ *
+ * @group Database
+ *        ^--- needed for language cache stuff
+ */
 class TitleTest extends MediaWikiTestCase {
 
        function testLegalChars() {
@@ -16,107 +21,198 @@ class TitleTest extends MediaWikiTestCase {
        }
 
        /**
-        * Helper to test getLocalURL
-        *
-        * @cover Title:getLocalURL
+        * @dataProvider dataBug31100
         */
-       function assertGetLocalURL( $expected, $dbkey, $query, $articlepath, $actionpaths = array(), $variant = false )
-       {
-               global $wgArticlePath;
-               $wgArticlePath = $articlepath;
-               global $wgActionPaths;
-               $wgActionPaths = $actionpaths;
-
-               $t = Title::newFromDBKey( $dbkey );
+       function testBug31100FixSpecialName( $text, $expectedParam ) {
+               $title = Title::newFromText( $text );
+               $fixed = $title->fixSpecialName();
+               $stuff = explode( '/', $fixed->getDbKey(), 2 );
+               if ( count( $stuff ) == 2 ) {
+                       $par = $stuff[1];
+               } else {
+                       $par = null;
+               }
+               $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
+       }
 
-               $this->assertEquals( $expected, $t->getLocalURL( $query, $variant ) );
+       function dataBug31100() {
+               return array(
+                       array( 'Special:Version', null ),
+                       array( 'Special:Version/', '' ),
+                       array( 'Special:Version/param', 'param' ),
+               );
        }
        
        /**
-        * @dataProvider provider1
-        * @cover Title:getLocalURL
+        * Auth-less test of Title::isValidMoveOperation
+        * 
+        * @group Database
+        * @param string $source
+        * @param string $target
+        * @param array|string|true $expected Required error
+        * @dataProvider dataTestIsValidMoveOperation
         */
-       function testGetLocalUrl( $expected, $dbkey, $query ) 
-       {
-               $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/$1' );
+       function testIsValidMoveOperation( $source, $target, $expected ) {
+               $title = Title::newFromText( $source );
+               $nt = Title::newFromText( $target );
+               $errors = $title->isValidMoveOperation( $nt, false );
+               if ( $expected === true ) {
+                       $this->assertTrue( $errors );
+               } else {
+                       $errors = $this->flattenErrorsArray( $errors );
+                       foreach ( (array)$expected as $error ) {
+                               $this->assertContains( $error, $errors );
+                       }
+               }
+       }
+       
+       function flattenErrorsArray( $errors ) {
+               $result = array();
+               foreach ( $errors as $error ) {
+                       $result[] = $error[0];
+               }
+               return $result;
+       }
+       
+       function dataTestIsValidMoveOperation() {
+               return array( 
+                       array( 'Test', 'Test', 'selfmove' ),
+                       array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
+               );
        }
 
-       function provider1()
-       {
-               return array(
-                       array( '/wiki/Recentchanges', 'Recentchanges', '' ),
-                       array( '/wiki/Recentchanges?foo=2', 'Recentchanges', 'foo=2' ),
-                       array( '/wiki/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1' ),
+       /**
+        * @dataProvider provideCasesForGetpageviewlanguage
+        */
+       function testGetpageviewlanguage( $expected, $titleText, $contLang, $lang, $variant, $msg='' ) {
+               // Save globals
+               global $wgContLang, $wgLang, $wgAllowUserJs, $wgLanguageCode, $wgDefaultLanguageVariant;
+               $save['wgContLang']               = $wgContLang;
+               $save['wgLang']                   = $wgLang;
+               $save['wgAllowUserJs']            = $wgAllowUserJs;
+               $save['wgLanguageCode']           = $wgLanguageCode;
+               $save['wgDefaultLanguageVariant'] = $wgDefaultLanguageVariant;
+
+               // Setup test environnement:
+               $wgContLang = Language::factory( $contLang );
+               $wgLang     = Language::factory( $lang );
+               # To test out .js titles:
+               $wgAllowUserJs = true;
+               $wgLanguageCode = $contLang;
+               $wgDefaultLanguageVariant = $variant;
+
+               $title = Title::newFromText( $titleText );
+               $this->assertInstanceOf( 'Title', $title,
+                       "Test must be passed a valid title text, you gave '$titleText'"
                );
+               $this->assertEquals( $expected,
+                       $title->getPageViewLanguage()->getCode(),
+                       $msg
+               );
+
+               // Restore globals
+               $wgContLang               = $save['wgContLang'];
+               $wgLang                   = $save['wgLang'];
+               $wgAllowUserJs            = $save['wgAllowUserJs'];
+               $wgLanguageCode           = $save['wgLanguageCode'];
+               $wgDefaultLanguageVariant = $save['wgDefaultLanguageVariant'];
        }
 
+       function provideCasesForGetpageviewlanguage() {
+               # Format:
+               # - expected
+               # - Title name
+               # - wgContLang (expected in most case)
+               # - wgLang (on some specific pages)
+               # - wgDefaultLanguageVariant
+               # - Optional message
+               return array(
+                       array( 'fr', 'Main_page', 'fr', 'fr', false ),
+                       array( 'es', 'Main_page', 'es', 'zh-tw', false ),
+                       array( 'zh', 'Main_page', 'zh', 'zh-tw', false ),
+
+                       array( 'es',    'Main_page',                 'es', 'zh-tw', 'zh-cn' ),
+                       array( 'es',    'MediaWiki:About',           'es', 'zh-tw', 'zh-cn' ),
+                       array( 'es',    'MediaWiki:About/',          'es', 'zh-tw', 'zh-cn' ),
+                       array( 'de',    'MediaWiki:About/de',        'es', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'MediaWiki:Common.js',       'es', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'MediaWiki:Common.css',      'es', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'User:JohnDoe/Common.js',    'es', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'User:JohnDoe/Monobook.css', 'es', 'zh-tw', 'zh-cn' ),
+
+                       array( 'zh-cn', 'Main_page',                 'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'zh',    'MediaWiki:About',           'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'zh',    'MediaWiki:About/',          'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'de',    'MediaWiki:About/de',        'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'zh-cn', 'MediaWiki:About/zh-cn',     'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'zh-tw', 'MediaWiki:About/zh-tw',     'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'MediaWiki:Common.js',       'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'MediaWiki:Common.css',      'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'User:JohnDoe/Common.js',    'zh', 'zh-tw', 'zh-cn' ),
+                       array( 'en',    'User:JohnDoe/Monobook.css', 'zh', 'zh-tw', 'zh-cn' ),
+
+                       array( 'zh-tw', 'Special:NewPages',       'es', 'zh-tw', 'zh-cn' ),
+                       array( 'zh-tw', 'Special:NewPages',       'zh', 'zh-tw', 'zh-cn' ),
+
+               );
+       }
 
        /**
-        * @dataProvider provider2
-        * @cover Title:getLocalURL
+        * @dataProvider provideBaseTitleCases
         */
-       function testGetLocalUrlWithArticlePath( $expected, $dbkey, $query, $actions ) 
-       {
-               $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/view/$1', $actions );
+       function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
+               $title = Title::newFromText( $title );
+               $this->assertEquals( $expected,
+                       $title->getBaseText(),
+                       $msg
+               );
        }
 
-       function provider2()
-       {
+       function provideBaseTitleCases() {
                return array(
-                       array( '/wiki/view/Recentchanges', 'Recentchanges', '', array( ) ),
-                       array( '/wiki/view/Recentchanges', 'Recentchanges', '', array( 'view' => 'OH MEN' ) ),
-                       array( '/wiki/view/Recentchanges', 'Recentchanges', '', array( 'view' => '/wiki/view/$1' ) ),
-                       array( '/wiki/view/Recentchanges?foo=2', 'Recentchanges', 'foo=2', array( 'view' => '/wiki/view/$1' ) ),
-                       array( '/wiki/view/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1', array() ),
+                       # Title, expected base, optional message
+                       array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
+                       array('User:Foo/Bar/Baz', 'Foo/Bar' ),
                );
        }
 
-
        /**
-        * @dataProvider provider3
-        * @cover Title:getLocalURL
+        * @dataProvider provideRootTitleCases
         */
-       function testGetLocalUrlWithActionPaths( $expected, $dbkey, $query ) 
-       {
-               $actions = array( 'edit' => '/wiki/edit/$1' );
-               $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/view/$1', $actions );
+       function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
+               $title = Title::newFromText( $title );
+               $this->assertEquals( $expected,
+                       $title->getRootText(),
+                       $msg
+               );
        }
 
-       function provider3() {
+       function provideRootTitleCases() {
                return array(
-                       array( '/wiki/view/Recentchanges', 'Recentchanges', ''),
-                       array( '/wiki/edit/Recentchanges', 'Recentchanges', 'action=edit' ),
-                       array( '/wiki/edit/Recentchanges?foo=2', 'Recentchanges', 'action=edit&foo=2' ),
-                       array( '/wiki/edit/Recentchanges?foo=2', 'Recentchanges', 'foo=2&action=edit' ),
-                       array( '/wiki/view/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1' ),
-                       array( '/wiki/edit/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&bar=1&action=edit' ),
-                       array( '/wiki/edit/Recentchanges?foo=A&bar=1', 'Recentchanges', 'foo=A&action=edit&bar=1' ),
-                       array( '/wiki/edit/Recentchanges?foo=A&bar=1', 'Recentchanges', 'action=edit&foo=A&bar=1' ),
-
-                       # @todo FIXME: The next two are equals but need investigation:
-                       array( '/wiki/edit/Recentchanges', 'Recentchanges', 'action=view&action=edit' ),
-                       array( '/wiki/view/Recentchanges?action=edit&action=view', 'Recentchanges', 'action=edit&action=view' ),
+                       # Title, expected base, optional message
+                       array('User:John_Doe/subOne/subTwo', 'John Doe' ),
+                       array('User:Foo/Bar/Baz', 'Foo' ),
                );
        }
 
        /**
-        * @dataProvider provider4
-        * @cover Title:getLocalURL
+        * @todo Handle $wgNamespacesWithSubpages cases
+        * @dataProvider provideSubpageTitleCases
         */
-       function testGetLocalUrlWithVariantArticlePaths( $expected, $dbkey, $query )
-       {
-               # @todo FIXME: Find a language with variants!
-               $this->markTestIncomplete();
-
-               $actions = array( 'edit' => '/wiki/edit/$1' );
-               $this->assertGetLocalURL( $expected, $dbkey, $query, '/wiki/view/$1', array(), '/$2/$1' );
+       function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
+               $title = Title::newFromText( $title );
+               $this->assertEquals( $expected,
+                       $title->getSubpageText(),
+                       $msg
+               );
        }
 
-       function provider4() {
+       function provideSubpageTitleCases() {
                return array(
-                       array( '/wiki/view/Recentchanges', 'Recentchanges', '', ),
+                       # Title, expected base, optional message
+                       array('User:John_Doe/subOne/subTwo', 'subTwo' ),
+                       array('User:John_Doe/subOne', 'subOne' ),
                );
        }
 
-
 }