Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / tests / phpunit / includes / LinkerTest.php
index 438d3e7..fe32507 100644 (file)
@@ -4,7 +4,6 @@
  * @group Database
  */
 class LinkerTest extends MediaWikiLangTestCase {
-
        /**
         * @dataProvider provideCasesForUserLink
         * @covers Linker::userLink
@@ -14,11 +13,16 @@ class LinkerTest extends MediaWikiLangTestCase {
                        'wgArticlePath' => '/wiki/$1',
                ] );
 
-               $this->assertEquals(
-                       $expected,
-                       Linker::userLink( $userId, $userName, $altUserName ),
-                       $msg
-               );
+               // We'd also test the warning, but injecting a mock logger into a static method is tricky.
+               if ( !$userName ) {
+                       Wikimedia\suppressWarnings();
+               }
+               $actual = Linker::userLink( $userId, $userName, $altUserName );
+               if ( !$userName ) {
+                       Wikimedia\restoreWarnings();
+               }
+
+               $this->assertEquals( $expected, $actual, $msg );
        }
 
        public static function provideCasesForUserLink() {
@@ -29,6 +33,12 @@ class LinkerTest extends MediaWikiLangTestCase {
                # - optional altUserName
                # - optional message
                return [
+                       # Empty name (T222529)
+                       'Empty username, userid 0' => [ '(no username available)', 0, '' ],
+                       'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
+
+                       'false instead of username' => [ '(no username available)', 73, false ],
+                       'null instead of username' => [ '(no username available)', 0, null ],
 
                        # ## ANONYMOUS USER ########################################
                        [
@@ -87,6 +97,118 @@ class LinkerTest extends MediaWikiLangTestCase {
                ];
        }
 
+       /**
+        * @dataProvider provideUserToolLinks
+        * @covers Linker::userToolLinks
+        * @param string $expected
+        * @param int $userId
+        * @param string $userText
+        */
+       public function testUserToolLinks( $expected, $userId, $userText ) {
+               // We'd also test the warning, but injecting a mock logger into a static method is tricky.
+               if ( $userText === '' ) {
+                       Wikimedia\suppressWarnings();
+               }
+               $actual = Linker::userToolLinks( $userId, $userText );
+               if ( $userText === '' ) {
+                       Wikimedia\restoreWarnings();
+               }
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       public static function provideUserToolLinks() {
+               return [
+                       // Empty name (T222529)
+                       'Empty username, userid 0' => [ ' (no username available)', 0, '' ],
+                       'Empty username, userid > 0' => [ ' (no username available)', 73, '' ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideUserTalkLink
+        * @covers Linker::userTalkLink
+        * @param string $expected
+        * @param int $userId
+        * @param string $userText
+        */
+       public function testUserTalkLink( $expected, $userId, $userText ) {
+               // We'd also test the warning, but injecting a mock logger into a static method is tricky.
+               if ( $userText === '' ) {
+                       Wikimedia\suppressWarnings();
+               }
+               $actual = Linker::userTalkLink( $userId, $userText );
+               if ( $userText === '' ) {
+                       Wikimedia\restoreWarnings();
+               }
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       public static function provideUserTalkLink() {
+               return [
+                       // Empty name (T222529)
+                       'Empty username, userid 0' => [ '(no username available)', 0, '' ],
+                       'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideBlockLink
+        * @covers Linker::blockLink
+        * @param string $expected
+        * @param int $userId
+        * @param string $userText
+        */
+       public function testBlockLink( $expected, $userId, $userText ) {
+               // We'd also test the warning, but injecting a mock logger into a static method is tricky.
+               if ( $userText === '' ) {
+                       Wikimedia\suppressWarnings();
+               }
+               $actual = Linker::blockLink( $userId, $userText );
+               if ( $userText === '' ) {
+                       Wikimedia\restoreWarnings();
+               }
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       public static function provideBlockLink() {
+               return [
+                       // Empty name (T222529)
+                       'Empty username, userid 0' => [ '(no username available)', 0, '' ],
+                       'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideEmailLink
+        * @covers Linker::emailLink
+        * @param string $expected
+        * @param int $userId
+        * @param string $userText
+        */
+       public function testEmailLink( $expected, $userId, $userText ) {
+               // We'd also test the warning, but injecting a mock logger into a static method is tricky.
+               if ( $userText === '' ) {
+                       Wikimedia\suppressWarnings();
+               }
+               $actual = Linker::emailLink( $userId, $userText );
+               if ( $userText === '' ) {
+                       Wikimedia\restoreWarnings();
+               }
+
+               $this->assertSame( $expected, $actual );
+       }
+
+       public static function provideEmailLink() {
+               return [
+                       // Empty name (T222529)
+                       'Empty username, userid 0' => [ '(no username available)', 0, '' ],
+                       'Empty username, userid > 0' => [ '(no username available)', 73, '' ],
+               ];
+       }
+
        /**
         * @dataProvider provideCasesForFormatComment
         * @covers Linker::formatComment
@@ -158,6 +280,11 @@ class LinkerTest extends MediaWikiLangTestCase {
                                '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#linkie.3F" title="Special:BlankPage">→‎&#91;[linkie?]]</a></span></span>',
                                "/* [[linkie?]] */",
                        ],
+                       [
+                               '<span dir="auto"><span class="autocomment">: </span> // Edit via via</span>',
+                               // Regression test for T222857
+                               "/*  */ // Edit via via",
+                       ],
                        [
                                '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a>: </span> post</span>',
                                "/* autocomment */ post",
@@ -196,6 +323,36 @@ class LinkerTest extends MediaWikiLangTestCase {
                                "/* autocomment */",
                                null
                        ],
+                       [
+                               '',
+                               "/* */",
+                               false, true
+                       ],
+                       [
+                               '',
+                               "/* */",
+                               null
+                       ],
+                       [
+                               '<span dir="auto"><span class="autocomment">[[</span></span>',
+                               "/* [[ */",
+                               false, true
+                       ],
+                       [
+                               '<span dir="auto"><span class="autocomment">[[</span></span>',
+                               "/* [[ */",
+                               null
+                       ],
+                       [
+                               "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#.23\">→‎&#91;[#_\t_]]</a></span></span>",
+                               "foo /* [[#_\t_]] */",
+                               false, true
+                       ],
+                       [
+                               "foo <span dir=\"auto\"><span class=\"autocomment\"><a href=\"#_.09\">#_\t_</a></span></span>",
+                               "foo /* [[#_\t_]] */",
+                               null
+                       ],
                        [
                                '<span dir="auto"><span class="autocomment"><a href="/wiki/Special:BlankPage#autocomment" title="Special:BlankPage">→‎autocomment</a></span></span>',
                                "/* autocomment */",
@@ -289,14 +446,15 @@ class LinkerTest extends MediaWikiLangTestCase {
         * @covers Linker::generateRollback
         * @dataProvider provideCasesForRollbackGeneration
         */
-       public function testGenerateRollback( $rollbackEnabled, $expectedModules ) {
+       public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) {
                $this->markTestSkippedIfDbType( 'postgres' );
 
                $context = RequestContext::getMain();
                $user = $context->getUser();
                $user->setOption( 'showrollbackconfirmation', $rollbackEnabled );
 
-               $pageData = $this->insertPage( 'Rollback_Test_Page' );
+               $this->assertEquals( 0, Title::newFromText( $title )->getArticleID() );
+               $pageData = $this->insertPage( $title );
                $page = WikiPage::factory( $pageData['title'] );
 
                $updater = $page->newPageUpdater( $user );
@@ -308,8 +466,24 @@ class LinkerTest extends MediaWikiLangTestCase {
 
                $rollbackOutput = Linker::generateRollback( $page->getRevision(), $context );
                $modules = $context->getOutput()->getModules();
+               $currentRev = $page->getRevision();
+               $oldestRev = $page->getOldestRevision();
 
                $this->assertEquals( $expectedModules, $modules );
+               $this->assertEquals( $user->getName(), $currentRev->getUserText() );
+               $this->assertEquals(
+                       static::getTestSysop()->getUser(),
+                       $oldestRev->getUserText()
+               );
+
+               $ids = [];
+               $r = $oldestRev;
+               while ( $r ) {
+                       $ids[] = $r->getId();
+                       $r = $r->getNext();
+               }
+               $this->assertEquals( [ $oldestRev->getId(), $currentRev->getId() ], $ids );
+
                $this->assertContains( 'rollback 1 edit', $rollbackOutput );
        }
 
@@ -317,12 +491,13 @@ class LinkerTest extends MediaWikiLangTestCase {
                return [
                        [
                                true,
-                               [ 'mediawiki.page.rollback.confirmation' ]
-
+                               [ 'mediawiki.page.rollback.confirmation' ],
+                               'Rollback_Test_Page'
                        ],
                        [
                                false,
-                               []
+                               [],
+                               'Rollback_Test_Page2'
                        ]
                ];
        }