Merge "Title::getTalkPage(): Restore behavior of interwiki-prefixed & fragment-only...
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
index e8f0873..608d590 100644 (file)
@@ -3,7 +3,6 @@
 use MediaWiki\Interwiki\InterwikiLookup;
 use MediaWiki\Linker\LinkTarget;
 use MediaWiki\MediaWikiServices;
-use Wikimedia\TestingAccessWrapper;
 
 /**
  * @group Database
@@ -22,12 +21,6 @@ class TitleTest extends MediaWikiTestCase {
                $this->setContentLang( 'en' );
        }
 
-       protected function tearDown() {
-               // For testNewMainPage
-               MessageCache::destroyInstance();
-               parent::tearDown();
-       }
-
        /**
         * @covers Title::legalChars
         */
@@ -157,9 +150,6 @@ class TitleTest extends MediaWikiTestCase {
                                ]
                        ]
                ] );
-
-               // Reset services since we modified $wgLocalInterwikis
-               $this->overrideMwServices();
        }
 
        /**
@@ -286,59 +276,6 @@ class TitleTest extends MediaWikiTestCase {
                ];
        }
 
-       /**
-        * Auth-less test of Title::isValidMoveOperation
-        *
-        * @param string $source
-        * @param string $target
-        * @param array|string|bool $expected Required error
-        * @dataProvider provideTestIsValidMoveOperation
-        * @covers Title::isValidMoveOperation
-        */
-       public function testIsValidMoveOperation( $source, $target, $expected ) {
-               global $wgMultiContentRevisionSchemaMigrationStage;
-
-               $this->hideDeprecated( 'Title::isValidMoveOperation' );
-
-               if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD ) {
-                       // We can only set this to false with the old schema
-                       $this->setMwGlobals( 'wgContentHandlerUseDB', false );
-               }
-
-               $title = Title::newFromText( $source );
-               $nt = Title::newFromText( $target );
-               $errors = $title->isValidMoveOperation( $nt, false );
-               if ( $expected === true ) {
-                       $this->assertTrue( $errors );
-               } else {
-                       $errors = $this->flattenErrorsArray( $errors );
-                       foreach ( (array)$expected as $error ) {
-                               $this->assertContains( $error, $errors );
-                       }
-               }
-       }
-
-       public static function provideTestIsValidMoveOperation() {
-               global $wgMultiContentRevisionSchemaMigrationStage;
-               $ret = [
-                       // for Title::isValidMoveOperation
-                       [ 'Some page', '', 'badtitletext' ],
-                       [ 'Test', 'Test', 'selfmove' ],
-                       [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
-                       [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
-                       [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
-                       [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
-               ];
-               if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD ) {
-                       // The error can only occur if $wgContentHandlerUseDB is false, which doesn't work with
-                       // the new schema, so omit the test in that case
-                       array_push( $ret,
-                               [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ]
-                       );
-               }
-               return $ret;
-       }
-
        /**
         * Auth-less test of Title::userCan
         *
@@ -831,12 +768,12 @@ class TitleTest extends MediaWikiTestCase {
                $this->assertEquals(
                        false,
                        $title->exists(),
-                       'exists() should rely on link cache unless GAID_FOR_UPDATE is used'
+                       'exists() should rely on link cache unless READ_LATEST is used'
                );
                $this->assertEquals(
                        true,
-                       $title->exists( Title::GAID_FOR_UPDATE ),
-                       'exists() should re-query database when GAID_FOR_UPDATE is used'
+                       $title->exists( Title::READ_LATEST ),
+                       'exists() should re-query database when READ_LATEST is used'
                );
        }
 
@@ -897,8 +834,23 @@ class TitleTest extends MediaWikiTestCase {
                return [
                        [ Title::makeTitle( NS_SPECIAL, 'Test' ) ],
                        [ Title::makeTitle( NS_MEDIA, 'Test' ) ],
-                       [ Title::makeTitle( NS_MAIN, '', 'Kittens' ) ],
-                       [ Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ) ],
+               ];
+       }
+
+       public static function provideGetTalkPage_broken() {
+               // These cases *should* be bad, but are not treated as bad, for backwards compatibility.
+               // See discussion on T227817.
+               return [
+                       [
+                               Title::makeTitle( NS_MAIN, '', 'Kittens' ),
+                               Title::makeTitle( NS_TALK, '' ), // Section is lost!
+                               false,
+                       ],
+                       [
+                               Title::makeTitle( NS_MAIN, 'Kittens', '', 'acme' ),
+                               Title::makeTitle( NS_TALK, 'Kittens', '' ), // Interwiki prefix is lost!
+                               true,
+                       ],
                ];
        }
 
@@ -958,6 +910,23 @@ class TitleTest extends MediaWikiTestCase {
                $title->getTalkPage();
        }
 
+       /**
+        * @dataProvider provideGetTalkPage_broken
+        * @covers Title::getTalkPageIfDefined
+        */
+       public function testGetTalkPage_broken( Title $title, Title $expected, $valid ) {
+               $errorLevel = error_reporting( E_ERROR );
+
+               // NOTE: Eventually we want to throw in this case. But while there is still code that
+               // calls this method without checking, we want to avoid fatal errors.
+               // See discussion on T227817.
+               $result = $title->getTalkPage();
+               $this->assertTrue( $expected->equals( $result ) );
+               $this->assertSame( $valid, $result->isValid() );
+
+               error_reporting( $errorLevel );
+       }
+
        /**
         * @dataProvider provideGetTalkPage_good
         * @covers Title::getTalkPageIfDefined
@@ -1007,6 +976,41 @@ class TitleTest extends MediaWikiTestCase {
                $title->getOtherPage();
        }
 
+       /**
+        * @dataProvider provideIsMovable
+        * @covers Title::isMovable
+        *
+        * @param string|Title $title
+        * @param bool $expected
+        * @param callable|null $hookCallback For TitleIsMovable
+        */
+       public function testIsMovable( $title, $expected, $hookCallback = null ) {
+               if ( $hookCallback ) {
+                       $this->setTemporaryHook( 'TitleIsMovable', $hookCallback );
+               }
+               if ( is_string( $title ) ) {
+                       $title = Title::newFromText( $title );
+               }
+
+               $this->assertSame( $expected, $title->isMovable() );
+       }
+
+       public static function provideIsMovable() {
+               return [
+                       'Simple title' => [ 'Foo', true ],
+                       // @todo Should these next two really be true?
+                       'Empty name' => [ Title::makeTitle( NS_MAIN, '' ), true ],
+                       'Invalid name' => [ Title::makeTitle( NS_MAIN, '<' ), true ],
+                       'Interwiki' => [ Title::makeTitle( NS_MAIN, 'Test', '', 'otherwiki' ), false ],
+                       'Special page' => [ 'Special:FooBar', false ],
+                       'Aborted by hook' => [ 'Hooked in place', false,
+                               function ( Title $title, &$result ) {
+                                       $result = false;
+                               }
+                       ],
+               ];
+       }
+
        public function provideCreateFragmentTitle() {
                return [
                        [ Title::makeTitle( NS_MAIN, 'Test' ), 'foo' ],
@@ -1302,10 +1306,11 @@ class TitleTest extends MediaWikiTestCase {
         * @covers Title::newMainPage
         */
        public function testNewMainPage() {
-               $msgCache = TestingAccessWrapper::newFromClass( MessageCache::class );
-               $msgCache->instance = $this->createMock( MessageCache::class );
-               $msgCache->instance->method( 'get' )->willReturn( 'Foresheet' );
-               $msgCache->instance->method( 'transform' )->willReturn( 'Foresheet' );
+               $mock = $this->createMock( MessageCache::class );
+               $mock->method( 'get' )->willReturn( 'Foresheet' );
+               $mock->method( 'transform' )->willReturn( 'Foresheet' );
+
+               $this->setService( 'MessageCache', $mock );
 
                $this->assertSame(
                        'Foresheet',