From 3749db7eba9288367995ab0118c0326a61e9c09f Mon Sep 17 00:00:00 2001 From: Reedy Date: Sun, 14 Mar 2021 03:43:32 +0000 Subject: [PATCH] Namespace extension Change-Id: Ic9e0b8d6b3cc6b9be4d6b026cb0a3b4a12aef38f --- extension.json | 43 ++++++++----------- includes/ApiCoreThank.php | 10 +++++ includes/ApiFlowThank.php | 7 +++ includes/ApiThank.php | 8 ++++ includes/EchoCoreThanksPresentationModel.php | 14 +++++- includes/EchoFlowThanksPresentationModel.php | 8 +++- includes/{ThanksHooks.php => Hooks.php} | 28 ++++++++++-- includes/SpecialThanks.php | 11 +++++ includes/ThanksLogFormatter.php | 7 +++ tests/phpunit/ApiCoreThankIntegrationTest.php | 2 +- tests/phpunit/ApiCoreThankUnitTest.php | 33 ++++++-------- tests/phpunit/ApiFlowThankIntegrationTest.php | 16 +++---- 12 files changed, 127 insertions(+), 60 deletions(-) rename includes/{ThanksHooks.php => Hooks.php} (96%) diff --git a/extension.json b/extension.json index 8a3ef5c8..4e1a5db7 100644 --- a/extension.json +++ b/extension.json @@ -29,7 +29,7 @@ } }, "SpecialPages": { - "Thanks": "SpecialThanks" + "Thanks": "MediaWiki\\Extension\\Thanks\\SpecialThanks" }, "LogTypes": [ "thanks" @@ -38,10 +38,10 @@ "thanks": true }, "LogActionsHandlers": { - "thanks/*": "ThanksLogFormatter" + "thanks/*": "MediaWiki\\Extension\\Thanks\\ThanksLogFormatter" }, "APIModules": { - "thank": "ApiCoreThank" + "thank": "MediaWiki\\Extension\\Thanks\\ApiCoreThank" }, "MessagesDirs": { "Thanks": [ @@ -52,15 +52,8 @@ "ExtensionMessagesFiles": { "ThanksAlias": "Thanks.alias.php" }, - "AutoloadClasses": { - "ThanksHooks": "includes/ThanksHooks.php", - "EchoCoreThanksPresentationModel": "includes/EchoCoreThanksPresentationModel.php", - "EchoFlowThanksPresentationModel": "includes/EchoFlowThanksPresentationModel.php", - "ApiThank": "includes/ApiThank.php", - "ApiCoreThank": "includes/ApiCoreThank.php", - "ApiFlowThank": "includes/ApiFlowThank.php", - "ThanksLogFormatter": "includes/ThanksLogFormatter.php", - "SpecialThanks": "includes/SpecialThanks.php" + "AutoloadNamespaces": { + "MediaWiki\\Extension\\Thanks\\": "includes/" }, "ResourceModules": { "ext.thanks.images": { @@ -172,19 +165,19 @@ ] }, "Hooks": { - "ApiMain::moduleManager": "ThanksHooks::onApiMainModuleManager", - "BeforeCreateEchoEvent": "ThanksHooks::onBeforeCreateEchoEvent", - "BeforePageDisplay": "ThanksHooks::onBeforePageDisplay", - "BeforeSpecialMobileDiffDisplay": "ThanksHooks::onBeforeSpecialMobileDiffDisplay", - "DiffTools": "ThanksHooks::insertThankLink", - "DifferenceEngineViewHeader": "ThanksHooks::onDifferenceEngineViewHeader", - "EchoGetBundleRules": "ThanksHooks::onEchoGetBundleRules", - "EchoGetDefaultNotifiedUsers": "ThanksHooks::onEchoGetDefaultNotifiedUsers", - "GetLogTypesOnUser": "ThanksHooks::onGetLogTypesOnUser", - "HistoryTools": "ThanksHooks::insertThankLink", - "LocalUserCreated": "ThanksHooks::onAccountCreated", - "LogEventsListLineEnding": "ThanksHooks::onLogEventsListLineEnding", - "PageHistoryBeforeList": "ThanksHooks::onPageHistoryBeforeList" + "ApiMain::moduleManager": "MediaWiki\\Extension\\Thanks\\Hooks::onApiMainModuleManager", + "BeforeCreateEchoEvent": "MediaWiki\\Extension\\Thanks\\Hooks::onBeforeCreateEchoEvent", + "BeforePageDisplay": "MediaWiki\\Extension\\Thanks\\Hooks::onBeforePageDisplay", + "BeforeSpecialMobileDiffDisplay": "MediaWiki\\Extension\\Thanks\\Hooks::onBeforeSpecialMobileDiffDisplay", + "DiffTools": "MediaWiki\\Extension\\Thanks\\Hooks::insertThankLink", + "DifferenceEngineViewHeader": "MediaWiki\\Extension\\Thanks\\Hooks::onDifferenceEngineViewHeader", + "EchoGetBundleRules": "MediaWiki\\Extension\\Thanks\\Hooks::onEchoGetBundleRules", + "EchoGetDefaultNotifiedUsers": "MediaWiki\\Extension\\Thanks\\Hooks::onEchoGetDefaultNotifiedUsers", + "GetLogTypesOnUser": "MediaWiki\\Extension\\Thanks\\Hooks::onGetLogTypesOnUser", + "HistoryTools": "MediaWiki\\Extension\\Thanks\\Hooks::insertThankLink", + "LocalUserCreated": "MediaWiki\\Extension\\Thanks\\Hooks::onAccountCreated", + "LogEventsListLineEnding": "MediaWiki\\Extension\\Thanks\\Hooks::onLogEventsListLineEnding", + "PageHistoryBeforeList": "MediaWiki\\Extension\\Thanks\\Hooks::onPageHistoryBeforeList" }, "config": { "ThanksSendToBots": { diff --git a/includes/ApiCoreThank.php b/includes/ApiCoreThank.php index e8fd7da6..2a3c30cc 100644 --- a/includes/ApiCoreThank.php +++ b/includes/ApiCoreThank.php @@ -1,8 +1,18 @@ getPlainActionText(); // Turn wikitext into plaintext $excerpt = Linker::formatComment( $excerpt ); - $excerpt = Sanitizer::stripAllTags( $excerpt ); - return $excerpt; + return Sanitizer::stripAllTags( $excerpt ); } else { // Try to get edit summary. $summary = $this->getRevisionEditSummary(); diff --git a/includes/EchoFlowThanksPresentationModel.php b/includes/EchoFlowThanksPresentationModel.php index eb5050b5..54195ae8 100644 --- a/includes/EchoFlowThanksPresentationModel.php +++ b/includes/EchoFlowThanksPresentationModel.php @@ -1,6 +1,12 @@ event->getTitle(); } diff --git a/includes/ThanksHooks.php b/includes/Hooks.php similarity index 96% rename from includes/ThanksHooks.php rename to includes/Hooks.php index a667d320..8ee59b0c 100644 --- a/includes/ThanksHooks.php +++ b/includes/Hooks.php @@ -1,9 +1,31 @@ addModule( 'flowthank', 'action', - 'ApiFlowThank' + ApiFlowThank::class ); } } @@ -373,7 +395,7 @@ class ThanksHooks { * * @link https://www.mediawiki.org/wiki/Manual:Hooks/LogEventsListLineEnding * @param LogEventsList $page The log events list. - * @param string &$ret The lineending HTML, to modify. + * @param string &$ret The line ending HTML, to modify. * @param DatabaseLogEntry $entry The log entry. * @param string[] &$classes CSS classes to add to the line. * @param string[] &$attribs HTML attributes to add to the line. diff --git a/includes/SpecialThanks.php b/includes/SpecialThanks.php index 4ee8337f..43668db7 100644 --- a/includes/SpecialThanks.php +++ b/includes/SpecialThanks.php @@ -1,5 +1,16 @@ getModule(); @@ -53,7 +54,7 @@ class ApiCoreThankUnitTest extends MediaWikiTestCase { $mockUser = $this->createMock( 'User' ); $mockUser->expects( $this->once() ) ->method( 'isAnon' ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $testCases[ 'anon' ] = [ $mockUser, @@ -64,10 +65,10 @@ class ApiCoreThankUnitTest extends MediaWikiTestCase { $mockUser = $this->createMock( 'User' ); $mockUser->expects( $this->once() ) ->method( 'isAnon' ) - ->will( $this->returnValue( false ) ); + ->willReturn( false ); $mockUser->expects( $this->once() ) ->method( 'pingLimiter' ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $testCases[ 'ping' ] = [ $mockUser, @@ -78,18 +79,16 @@ class ApiCoreThankUnitTest extends MediaWikiTestCase { $mockUser = $this->createMock( 'User' ); $mockUser->expects( $this->once() ) ->method( 'isAnon' ) - ->will( $this->returnValue( false ) ); + ->willReturn( false ); $mockUser->expects( $this->once() ) ->method( 'pingLimiter' ) - ->will( $this->returnValue( false ) ); + ->willReturn( false ); $mockUser->expects( $this->once() ) ->method( 'isBlockedGlobally' ) - ->will( $this->returnValue( true ) ); + ->willReturn( true ); $mockUser->expects( $this->once() ) ->method( 'getGlobalBlock' ) - ->will( $this->returnValue( - $this->createBlock( [] ) - ) ); + ->willReturn( $this->createBlock( [] ) ); $testCases[ 'globally blocked' ] = [ $mockUser, @@ -100,9 +99,7 @@ class ApiCoreThankUnitTest extends MediaWikiTestCase { $mockUser = $this->createMock( 'User' ); $mockUser->expects( $this->once() ) ->method( 'getBlock' ) - ->will( $this->returnValue( - $this->createBlock( [] ) - ) ); + ->willReturn( $this->createBlock( [] ) ); $testCases[ 'sitewide blocked' ] = [ $mockUser, @@ -113,11 +110,9 @@ class ApiCoreThankUnitTest extends MediaWikiTestCase { $mockUser = $this->createMock( 'User' ); $mockUser->expects( $this->once() ) ->method( 'getBlock' ) - ->will( $this->returnValue( - $this->createBlock( [ - 'sitewide' => false - ] ) - ) ); + ->willReturn( + $this->createBlock( [ 'sitewide' => false ] ) + ); $testCases[ 'partial blocked' ] = [ $mockUser, diff --git a/tests/phpunit/ApiFlowThankIntegrationTest.php b/tests/phpunit/ApiFlowThankIntegrationTest.php index 3a0775ba..90c9dfba 100644 --- a/tests/phpunit/ApiFlowThankIntegrationTest.php +++ b/tests/phpunit/ApiFlowThankIntegrationTest.php @@ -8,7 +8,7 @@ use Flow\Model\Workflow; /** * Integration tests for the Thanks Flow api module * - * @covers ApiFlowThank + * @covers \MediaWiki\Extension\Thanks\ApiFlowThank * * @group Thanks * @group Database @@ -67,9 +67,7 @@ class ApiFlowThankIntegrationTest extends ApiTestCase { $that = $this; $mockLoader->expects( $this->any() ) ->method( 'getWithRoot' ) - ->will( $this->returnCallback( - // Hard to work with class variables or callbacks, - // using anonymous function instead. + ->willReturnCallback( function ( $postId ) use ( $that ) { switch ( $postId ) { case $that->postByOtherUser->getPostId(): @@ -88,7 +86,7 @@ class ApiFlowThankIntegrationTest extends ApiTestCase { return [ 'post' => null ]; } } - ) ); + ); $mockWorkflow = $this->createMock( Workflow::class ); $mockWorkflow->expects( $this->any() ) @@ -104,7 +102,7 @@ class ApiFlowThankIntegrationTest extends ApiTestCase { $mockStorage->expects( $this->any() ) ->method( 'get' ) - ->will( $this->returnValue( $mockWorkflow ) ); + ->willReturn( $mockWorkflow ); $mockTemplating = $this->getMockBuilder( 'Flow\Templating' ) ->disableOriginalConstructor() @@ -112,7 +110,7 @@ class ApiFlowThankIntegrationTest extends ApiTestCase { $mockTemplating->expects( $this->any() ) ->method( 'getContent' ) - ->will( $this->returnValue( 'test content' ) ); + ->willReturn( 'test content' ); Flow\Container::reset(); $container = Flow\Container::getContainer(); @@ -149,7 +147,7 @@ class ApiFlowThankIntegrationTest extends ApiTestCase { public function testRequestWithInvalidId() { $this->expectException( ApiUsageException::class ); $this->expectExceptionMessage( 'Post ID is not valid' ); - list( $result,, ) = $this->doApiRequestWithToken( [ + $this->doApiRequestWithToken( [ 'action' => 'flowthank', 'postid' => UUID::create( '42' )->getAlphadecimal(), ] ); @@ -158,7 +156,7 @@ class ApiFlowThankIntegrationTest extends ApiTestCase { public function testRequestWithOwnId() { $this->expectException( ApiUsageException::class ); $this->expectExceptionMessage( 'You cannot thank yourself' ); - list( $result,, ) = $this->doApiRequestWithToken( [ + $this->doApiRequestWithToken( [ 'action' => 'flowthank', 'postid' => $this->postByMe->getPostId()->getAlphadecimal(), ], null, $this->meUser );