Merge "Title: Title::getSubpage should not lose the interwiki prefix"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Sat, 13 Jul 2019 18:02:27 +0000 (18:02 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Sat, 13 Jul 2019 18:02:27 +0000 (18:02 +0000)
includes/Title.php
tests/phpunit/includes/TitleTest.php

index b9b1bb7..28bec0b 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' ) ],