Merge "Allow local interwiki links with an empty title part"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
index a332368..6871582 100644 (file)
@@ -29,9 +29,15 @@ class TitleTest extends MediaWikiTestCase {
                foreach ( range( 1, 255 ) as $num ) {
                        $chr = chr( $num );
                        if ( strpos( "#[]{}<>|", $chr ) !== false || preg_match( "/[\\x00-\\x1f\\x7f]/", $chr ) ) {
-                               $this->assertFalse( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is not a valid titlechar" );
+                               $this->assertFalse(
+                                       (bool)preg_match( "/[$titlechars]/", $chr ),
+                                       "chr($num) = $chr is not a valid titlechar"
+                               );
                        } else {
-                               $this->assertTrue( (bool)preg_match( "/[$titlechars]/", $chr ), "chr($num) = $chr is a valid titlechar" );
+                               $this->assertTrue(
+                                       (bool)preg_match( "/[$titlechars]/", $chr ),
+                                       "chr($num) = $chr is a valid titlechar"
+                               );
                        }
                }
        }
@@ -75,11 +81,13 @@ class TitleTest extends MediaWikiTestCase {
                        'Foo/.../Sandbox',
                        'Sandbox/...',
                        'A~~',
+                       ':A',
                        // Length is 256 total, but only title part matters
                        'Category:' . str_repeat( 'x', 248 ),
                        str_repeat( 'x', 252 ),
                        // interwiki prefix
                        'localtestiw: #anchor',
+                       'localtestiw:',
                        'localtestiw:foo',
                        'localtestiw: foo # anchor',
                        'localtestiw: Talk: Sandbox # anchor',
@@ -87,7 +95,9 @@ class TitleTest extends MediaWikiTestCase {
                        'remotetestiw: Talk: # anchor',
                        'remotetestiw: #bar',
                        'remotetestiw: Talk:',
-                       'remotetestiw: Talk: Foo'
+                       'remotetestiw: Talk: Foo',
+                       'localtestiw:remotetestiw:',
+                       'localtestiw:remotetestiw:foo'
                ) as $text ) {
                        $this->assertInstanceOf( 'Title', Title::newFromText( $text ), "Valid: $text" );
                }
@@ -138,7 +148,6 @@ class TitleTest extends MediaWikiTestCase {
                        'Category: ',
                        'Category: #bar',
                        // interwiki prefix
-                       'localtestiw:',
                        'localtestiw: Talk: # anchor',
                        'localtestiw: Talk:'
                ) as $text ) {
@@ -229,7 +238,11 @@ class TitleTest extends MediaWikiTestCase {
                } else {
                        $par = null;
                }
-               $this->assertEquals( $expectedParam, $par, "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter" );
+               $this->assertEquals(
+                       $expectedParam,
+                       $par,
+                       "Bug 31100 regression check: Title->fixSpecialName() should preserve parameter"
+               );
        }
 
        public static function provideBug31100() {
@@ -330,7 +343,11 @@ class TitleTest extends MediaWikiTestCase {
                        $allowableness = $expected
                                ? " should be allowed"
                                : " should NOT be allowed";
-                       $this->assertEquals( $expected, $errors, "User action '$action' on [[$source]] $allowableness." );
+                       $this->assertEquals(
+                               $expected,
+                               $errors,
+                               "User action '$action' on [[$source]] $allowableness."
+                       );
                } else {
                        $errors = $this->flattenErrorsArray( $errors );
                        foreach ( (array)$expected as $error ) {
@@ -398,7 +415,9 @@ class TitleTest extends MediaWikiTestCase {
         * @dataProvider provideGetPageViewLanguage
         * @covers Title::getPageViewLanguage
         */
-       public function testGetPageViewLanguage( $expected, $titleText, $contLang, $lang, $variant, $msg = '' ) {
+       public function testGetPageViewLanguage( $expected, $titleText, $contLang,
+               $lang, $variant, $msg = ''
+       ) {
                global $wgLanguageCode, $wgContLang, $wgLang, $wgDefaultLanguageVariant, $wgAllowUserJs;
 
                // Setup environnement for this test
@@ -517,4 +536,74 @@ class TitleTest extends MediaWikiTestCase {
                        array( 'User:John_Doe/subOne', 'subOne' ),
                );
        }
+
+       public function provideNewFromTitleValue() {
+               return array(
+                       array( new TitleValue( NS_MAIN, 'Foo' ) ),
+                       array( new TitleValue( NS_MAIN, 'Foo', 'bar' ) ),
+                       array( new TitleValue( NS_USER, 'Hansi_Maier' ) ),
+               );
+       }
+
+       /**
+        * @dataProvider provideNewFromTitleValue
+        */
+       public function testNewFromTitleValue( TitleValue $value ) {
+               $title = Title::newFromTitleValue( $value );
+
+               $dbkey = str_replace( ' ', '_', $value->getText() );
+               $this->assertEquals( $dbkey, $title->getDBkey() );
+               $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
+               $this->assertEquals( $value->getFragment(), $title->getFragment() );
+       }
+
+       public function provideGetTitleValue() {
+               return array(
+                       array( 'Foo' ),
+                       array( 'Foo#bar' ),
+                       array( 'User:Hansi_Maier' ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetTitleValue
+        */
+       public function testGetTitleValue( $text ) {
+               $title = Title::newFromText( $text );
+               $value = $title->getTitleValue();
+
+               $dbkey = str_replace( ' ', '_', $value->getText() );
+               $this->assertEquals( $title->getDBkey(), $dbkey );
+               $this->assertEquals( $title->getNamespace(), $value->getNamespace() );
+               $this->assertEquals( $title->getFragment(), $value->getFragment() );
+       }
+
+       public function provideGetFragment() {
+               return array(
+                       array( 'Foo', '' ),
+                       array( 'Foo#bar', 'bar' ),
+                       array( 'Foo#bär', 'bär' ),
+
+                       // Inner whitespace is normalized
+                       array( 'Foo#bar_bar', 'bar bar' ),
+                       array( 'Foo#bar bar', 'bar bar' ),
+                       array( 'Foo#bar   bar', 'bar bar' ),
+
+                       // Leading whitespace is kept, trailing whitespace is trimmed.
+                       // XXX: Is this really want we want?
+                       array( 'Foo#_bar_bar_', ' bar bar' ),
+                       array( 'Foo# bar bar ', ' bar bar' ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetFragment
+        *
+        * @param string $full
+        * @param string $fragment
+        */
+       public function testGetFragment( $full, $fragment ) {
+               $title = Title::newFromText( $full );
+               $this->assertEquals( $fragment, $title->getFragment() );
+       }
 }