X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FLinkerTest.php;h=dcac47146ce52c0f819eecd022bf5c93113544b2;hb=9c7f6734c397a954b8eaa5ec73876f2b4bf92afb;hp=34e5593cfc06ef8c38c9c31e97a9b8cf821a538a;hpb=8c9de54eef541b4e5997f0d40f1619ae84b0b3a4;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/LinkerTest.php b/tests/phpunit/includes/LinkerTest.php index 34e5593cfc..dcac47146c 100644 --- a/tests/phpunit/includes/LinkerTest.php +++ b/tests/phpunit/includes/LinkerTest.php @@ -1,11 +1,9 @@ '/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() { @@ -30,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 ######################################## [ @@ -88,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 @@ -159,6 +280,11 @@ class LinkerTest extends MediaWikiLangTestCase { '→‎[[linkie?]]', "/* [[linkie?]] */", ], + [ + ': // Edit via via', + // Regression test for T222857 + "/* */ // Edit via via", + ], [ '→‎autocomment: post', "/* autocomment */ post", @@ -197,6 +323,36 @@ class LinkerTest extends MediaWikiLangTestCase { "/* autocomment */", null ], + [ + '', + "/* */", + false, true + ], + [ + '', + "/* */", + null + ], + [ + '[[', + "/* [[ */", + false, true + ], + [ + '[[', + "/* [[ */", + null + ], + [ + "foo →‎[[#_\t_]]", + "foo /* [[#_\t_]] */", + false, true + ], + [ + "foo #_\t_", + "foo /* [[#_\t_]] */", + null + ], [ '→‎autocomment', "/* autocomment */", @@ -286,6 +442,66 @@ class LinkerTest extends MediaWikiLangTestCase { ); } + /** + * @covers Linker::generateRollback + * @dataProvider provideCasesForRollbackGeneration + */ + public function testGenerateRollback( $rollbackEnabled, $expectedModules, $title ) { + $this->markTestSkippedIfDbType( 'postgres' ); + + $context = RequestContext::getMain(); + $user = $context->getUser(); + $user->setOption( 'showrollbackconfirmation', $rollbackEnabled ); + + $this->assertSame( 0, Title::newFromText( $title )->getArticleID() ); + $pageData = $this->insertPage( $title ); + $page = WikiPage::factory( $pageData['title'] ); + + $updater = $page->newPageUpdater( $user ); + $updater->setContent( \MediaWiki\Revision\SlotRecord::MAIN, + new TextContent( 'Technical Wishes 123!' ) + ); + $summary = CommentStoreComment::newUnsavedComment( 'Some comment!' ); + $updater->saveRevision( $summary ); + + $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 ); + } + + public static function provideCasesForRollbackGeneration() { + return [ + [ + true, + [ 'mediawiki.page.rollback.confirmation' ], + 'Rollback_Test_Page' + ], + [ + false, + [], + 'Rollback_Test_Page2' + ] + ]; + } + public static function provideCasesForFormatLinksInComment() { // phpcs:disable Generic.Files.LineLength return [