Merge "Define index types as strings"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
index 4af12a8..c0de1bf 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 
+use MediaWiki\Linker\LinkTarget;
 use MediaWiki\MediaWikiServices;
+use Wikimedia\TestingAccessWrapper;
 
 /**
  * @group Database
@@ -19,6 +21,12 @@ class TitleTest extends MediaWikiTestCase {
                $this->setContentLang( 'en' );
        }
 
+       protected function tearDown() {
+               // For testNewMainPage
+               MessageCache::destroyInstance();
+               parent::tearDown();
+       }
+
        /**
         * @covers Title::legalChars
         */
@@ -149,12 +157,7 @@ class TitleTest extends MediaWikiTestCase {
                        ]
                ] );
 
-               // Reset TitleParser since we modified $wgLocalInterwikis
-               $this->setService( 'TitleParser', new MediaWikiTitleCodec(
-                               Language::factory( 'en' ),
-                               new GenderCache(),
-                               [ 'localtestiw' ]
-               ) );
+               $this->overrideMwServices();
        }
 
        /**
@@ -326,7 +329,7 @@ class TitleTest extends MediaWikiTestCase {
         * @param string $action
         * @param array|string|bool $expected Required error
         *
-        * @covers Title::checkReadPermissions
+        * @covers \Mediawiki\Permissions\PermissionManager::checkReadPermissions
         * @dataProvider dataWgWhitelistReadRegexp
         */
        public function testWgWhitelistReadRegexp( $whitelistRegexp, $source, $action, $expected ) {
@@ -564,6 +567,53 @@ class TitleTest extends MediaWikiTestCase {
                $this->assertEquals( $value->getFragment(), $title->getFragment() );
        }
 
+       /**
+        * @covers Title::newFromLinkTarget
+        * @dataProvider provideNewFromTitleValue
+        */
+       public function testNewFromLinkTarget( LinkTarget $value ) {
+               $title = Title::newFromLinkTarget( $value );
+
+               $dbkey = str_replace( ' ', '_', $value->getText() );
+               $this->assertEquals( $dbkey, $title->getDBkey() );
+               $this->assertEquals( $value->getNamespace(), $title->getNamespace() );
+               $this->assertEquals( $value->getFragment(), $title->getFragment() );
+       }
+
+       /**
+        * @covers Title::newFromLinkTarget
+        */
+       public function testNewFromLinkTarget_clone() {
+               $title = Title::newFromText( __METHOD__ );
+               $this->assertSame( $title, Title::newFromLinkTarget( $title ) );
+
+               // The Title::NEW_CLONE flag should ensure that a fresh instance is returned.
+               $clone = Title::newFromLinkTarget( $title, Title::NEW_CLONE );
+               $this->assertNotSame( $title, $clone );
+               $this->assertTrue( $clone->equals( $title ) );
+       }
+
+       public function provideCastFromLinkTarget() {
+               return array_merge( [ [ null ] ], self::provideNewFromTitleValue() );
+       }
+
+       /**
+        * @covers Title::castFromLinkTarget
+        * @dataProvider provideCastFromLinkTarget
+        */
+       public function testCastFromLinkTarget( $value ) {
+               $title = Title::castFromLinkTarget( $value );
+
+               if ( $value === null ) {
+                       $this->assertNull( $title );
+               } else {
+                       $dbkey = str_replace( ' ', '_', $value->getText() );
+                       $this->assertSame( $dbkey, $title->getDBkey() );
+                       $this->assertSame( $value->getNamespace(), $title->getNamespace() );
+                       $this->assertSame( $value->getFragment(), $title->getFragment() );
+               }
+       }
+
        public static function provideGetTitleValue() {
                return [
                        [ 'Foo' ],
@@ -981,4 +1031,121 @@ class TitleTest extends MediaWikiTestCase {
                        [ 'Main Page', false ],
                ];
        }
+
+       public function provideEquals() {
+               yield [
+                       Title::newFromText( 'Main Page' ),
+                       Title::newFromText( 'Main Page' ),
+                       true
+               ];
+               yield [
+                       Title::newFromText( 'Main Page' ),
+                       Title::newFromText( 'Not The Main Page' ),
+                       false
+               ];
+               yield [
+                       Title::newFromText( 'Main Page' ),
+                       Title::newFromText( 'Project:Main Page' ),
+                       false
+               ];
+               yield [
+                       Title::newFromText( 'File:Example.png' ),
+                       Title::newFromText( 'Image:Example.png' ),
+                       true
+               ];
+               yield [
+                       Title::newFromText( 'Special:Version' ),
+                       Title::newFromText( 'Special:Version' ),
+                       true
+               ];
+               yield [
+                       Title::newFromText( 'Special:Version' ),
+                       Title::newFromText( 'Special:Recentchanges' ),
+                       false
+               ];
+               yield [
+                       Title::newFromText( 'Special:Version' ),
+                       Title::newFromText( 'Main Page' ),
+                       false
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
+                       Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
+                       true
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
+                       Title::makeTitle( NS_MAIN, 'Bar', '', '' ),
+                       false
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
+                       Title::makeTitle( NS_TALK, 'Foo', '', '' ),
+                       false
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
+                       Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
+                       true
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
+                       Title::makeTitle( NS_MAIN, 'Foo', 'Baz', '' ),
+                       true
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', 'Bar', '' ),
+                       Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
+                       true
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
+                       Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
+                       true
+               ];
+               yield [
+                       Title::makeTitle( NS_MAIN, 'Foo', '', '' ),
+                       Title::makeTitle( NS_MAIN, 'Foo', '', 'baz' ),
+                       false
+               ];
+       }
+
+       /**
+        * @covers Title::equals
+        * @dataProvider provideEquals
+        */
+       public function testEquals( Title $firstValue, /* LinkTarget */ $secondValue, $expectedSame ) {
+               $this->assertSame(
+                       $expectedSame,
+                       $firstValue->equals( $secondValue )
+               );
+       }
+
+       /**
+        * @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' );
+
+               $this->assertSame(
+                       'Foresheet',
+                       Title::newMainPage()->getText()
+               );
+       }
+
+       /**
+        * @covers Title::newMainPage
+        */
+       public function testNewMainPageWithLocal() {
+               $local = $this->createMock( MessageLocalizer::class );
+               $local->method( 'msg' )->willReturn( new RawMessage( 'Prime Article' ) );
+
+               $this->assertSame(
+                       'Prime Article',
+                       Title::newMainPage( $local )->getText()
+               );
+       }
 }