Merge "Linker: Fix fatal error for "/* */" in an edit summary"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleTest.php
index 149c25b..c46f69b 100644 (file)
@@ -2,6 +2,7 @@
 
 use MediaWiki\Linker\LinkTarget;
 use MediaWiki\MediaWikiServices;
+use Wikimedia\TestingAccessWrapper;
 
 /**
  * @group Database
@@ -20,6 +21,12 @@ class TitleTest extends MediaWikiTestCase {
                $this->setContentLang( 'en' );
        }
 
+       protected function tearDown() {
+               // For testNewMainPage
+               MessageCache::destroyInstance();
+               parent::tearDown();
+       }
+
        /**
         * @covers Title::legalChars
         */
@@ -150,12 +157,8 @@ class TitleTest extends MediaWikiTestCase {
                        ]
                ] );
 
-               // Reset TitleParser since we modified $wgLocalInterwikis
-               $this->setService( 'TitleParser', new MediaWikiTitleCodec(
-                               Language::factory( 'en' ),
-                               new GenderCache(),
-                               [ 'localtestiw' ]
-               ) );
+               // Reset services since we modified $wgLocalInterwikis
+               $this->overrideMwServices();
        }
 
        /**
@@ -591,6 +594,27 @@ class TitleTest extends MediaWikiTestCase {
                $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' ],
@@ -762,19 +786,6 @@ class TitleTest extends MediaWikiTestCase {
                ];
        }
 
-       /**
-        * @dataProvider provideGetTalkPage_good
-        * @covers Title::getTalkPage
-        */
-       public function testGetTalkPage_good( Title $title, Title $expected ) {
-               $talk = $title->getTalkPage();
-               $this->assertSame(
-                       $expected->getPrefixedDBKey(),
-                       $talk->getPrefixedDBKey(),
-                       $title->getPrefixedDBKey()
-               );
-       }
-
        /**
         * @dataProvider provideGetTalkPage_good
         * @covers Title::getTalkPageIfDefined
@@ -1097,4 +1108,32 @@ class TitleTest extends MediaWikiTestCase {
                        $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()
+               );
+       }
 }