Merge "When title contains only slashes, Title::getRootText() shouldn't return false"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Sun, 14 Jul 2019 12:40:30 +0000 (12:40 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Sun, 14 Jul 2019 12:40:30 +0000 (12:40 +0000)
1  2 
includes/Title.php
tests/phpunit/includes/TitleTest.php

diff --combined includes/Title.php
@@@ -1770,6 -1770,7 +1770,7 @@@ class Title implements LinkTarget, IDBA
                if (
                        !MediaWikiServices::getInstance()->getNamespaceInfo()->
                                hasSubpages( $this->mNamespace )
+                       || strtok( $this->getText(), '/' ) === false
                ) {
                        return $this->getText();
                }
         * @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
 +              );
        }
  
        /**
         * Get the timestamp when this page was updated since the user last saw it.
         *
         * @param User|null $user
 -       * @return string|null
 +       * @return string|bool|null String timestamp, false if not watched, null if nothing is unseen
         */
        public function getNotificationTimestamp( $user = null ) {
                global $wgUser;
@@@ -1,6 -1,5 +1,6 @@@
  <?php
  
 +use MediaWiki\Interwiki\InterwikiLookup;
  use MediaWiki\Linker\LinkTarget;
  use MediaWiki\MediaWikiServices;
  use Wikimedia\TestingAccessWrapper;
@@@ -554,6 -553,10 +554,10 @@@ class TitleTest extends MediaWikiTestCa
                        # Title, expected base, optional message
                        [ 'User:John_Doe/subOne/subTwo', 'John Doe' ],
                        [ 'User:Foo / Bar / Baz', 'Foo ' ],
+                       [ 'Talk:////', '////' ],
+                       [ 'Template:////', '////' ],
+                       [ 'Template:Foo////', 'Foo' ],
+                       [ 'Template:Foo////Bar', 'Foo' ],
                ];
        }
  
                ];
        }
  
 +      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' ) ],