merge latest master into Wikidata branch
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
index 51b3616..ff535c3 100644 (file)
@@ -1,5 +1,10 @@
 <?php
 
+/**
+ *
+ * @group Database
+ *        ^--- needed for language cache stuff
+ */
 class TitleTest extends MediaWikiTestCase {
 
        function testLegalChars() {
@@ -41,9 +46,10 @@ class TitleTest extends MediaWikiTestCase {
        /**
         * Auth-less test of Title::isValidMoveOperation
         * 
+        * @group Database
         * @param string $source
         * @param string $target
-        * @param array|string|true $requiredErrors
+        * @param array|string|true $expected Required error
         * @dataProvider dataTestIsValidMoveOperation
         */
        function testIsValidMoveOperation( $source, $target, $expected ) {
@@ -74,6 +80,139 @@ class TitleTest extends MediaWikiTestCase {
                        array( 'File:Test.jpg', 'Page', 'imagenocrossnamespace' )
                );
        }
-       
-       
+
+       /**
+        * @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 provideBaseTitleCases
+        */
+       function testExtractingBaseTextFromTitle( $title, $expected, $msg='' ) {
+               $title = Title::newFromText( $title );
+               $this->assertEquals( $expected,
+                       $title->getBaseText(),
+                       $msg
+               );
+       }
+
+       function provideBaseTitleCases() {
+               return array(
+                       # Title, expected base, optional message
+                       array('User:John_Doe/subOne/subTwo', 'John Doe/subOne' ),
+                       array('User:Foo/Bar/Baz', 'Foo/Bar' ),
+               );
+       }
+
+       /**
+        * @dataProvider provideRootTitleCases
+        */
+       function testExtractingRootTextFromTitle( $title, $expected, $msg='' ) {
+               $title = Title::newFromText( $title );
+               $this->assertEquals( $expected,
+                       $title->getRootText(),
+                       $msg
+               );
+       }
+
+       function provideRootTitleCases() {
+               return array(
+                       # Title, expected base, optional message
+                       array('User:John_Doe/subOne/subTwo', 'John Doe' ),
+                       array('User:Foo/Bar/Baz', 'Foo' ),
+               );
+       }
+
+       /**
+        * @todo Handle $wgNamespacesWithSubpages cases
+        * @dataProvider provideSubpageTitleCases
+        */
+       function testExtractingSubpageTextFromTitle( $title, $expected, $msg='' ) {
+               $title = Title::newFromText( $title );
+               $this->assertEquals( $expected,
+                       $title->getSubpageText(),
+                       $msg
+               );
+       }
+
+       function provideSubpageTitleCases() {
+               return array(
+                       # Title, expected base, optional message
+                       array('User:John_Doe/subOne/subTwo', 'subTwo' ),
+                       array('User:John_Doe/subOne', 'subOne' ),
+               );
+       }
+
 }