Title: Title::getSubpage should not lose the interwiki prefix
authordaniel <dkinzler@wikimedia.org>
Fri, 12 Jul 2019 10:21:44 +0000 (12:21 +0200)
committerKrinkle <krinklemail@gmail.com>
Sat, 13 Jul 2019 17:49:11 +0000 (17:49 +0000)
This issue was discovered while investigating T227700, and added some
confusion. This patch is necessary for Special:MyLanguage to behave
correctly in all cases, but it's not necessary for fixing the primary
critical problem.

Bug: T227700
Change-Id: Ib4cbeec47a877c473cbd501cc964cc66d169b99e

includes/Title.php
tests/phpunit/includes/TitleTest.php

index 6e75102..079f2c8 100644 (file)
@@ -1887,7 +1887,12 @@ class Title implements LinkTarget, IDBAccessObject {
         * @since 1.20
         */
        public function getSubpage( $text ) {
-               return self::makeTitleSafe( $this->mNamespace, $this->getText() . '/' . $text );
+               return self::makeTitleSafe(
+                       $this->mNamespace,
+                       $this->getText() . '/' . $text,
+                       '',
+                       $this->mInterwiki
+               );
        }
 
        /**
index 4ffef02..d0a95c2 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use MediaWiki\Interwiki\InterwikiLookup;
 use MediaWiki\Linker\LinkTarget;
 use MediaWiki\MediaWikiServices;
 use Wikimedia\TestingAccessWrapper;
@@ -577,6 +578,41 @@ class TitleTest extends MediaWikiTestCase {
                ];
        }
 
+       public function provideSubpage() {
+               // NOTE: avoid constructing Title objects in the provider, since it may access the database.
+               return [
+                       [ 'Foo', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
+                       [ 'Foo#bar', 'x', new TitleValue( NS_MAIN, 'Foo/x' ) ],
+                       [ 'User:Foo', 'x', new TitleValue( NS_USER, 'Foo/x' ) ],
+                       [ 'wiki:User:Foo', 'x', new TitleValue( NS_MAIN, 'User:Foo/x', '', 'wiki' ) ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideSubpage
+        * @covers Title::getSubpage
+        */
+       public function testSubpage( $title, $sub, LinkTarget $expected ) {
+               $interwikiLookup = $this->getMock( InterwikiLookup::class );
+               $interwikiLookup->expects( $this->any() )
+                       ->method( 'isValidInterwiki' )
+                       ->willReturnCallback(
+                               function ( $prefix ) {
+                                       return $prefix == 'wiki';
+                               }
+                       );
+
+               $this->setService( 'InterwikiLookup', $interwikiLookup );
+
+               $title = Title::newFromText( $title );
+               $expected = Title::newFromLinkTarget( $expected );
+               $actual = $title->getSubpage( $sub );
+
+               // NOTE: convert to string for comparison
+               $this->assertSame( $expected->getPrefixedText(), $actual->getPrefixedText(), 'text form' );
+               $this->assertTrue( $expected->equals( $actual ), 'Title equality' );
+       }
+
        public static function provideNewFromTitleValue() {
                return [
                        [ new TitleValue( NS_MAIN, 'Foo' ) ],