jenkins-bot has submitted this change and it was merged. Change subject: Add CognatePageHookHandler & Tests ......................................................................
Add CognatePageHookHandler & Tests Change-Id: I88c3444e10d0e1f47f54fb83f2ea08b09e053e40 --- M extension.json M src/CognateHooks.php M src/ServiceWiring.php A src/hooks/CognatePageHookHandler.php M tests/phpunit/ServiceWiringTest.php A tests/phpunit/hooks/CognatePageHookHandlerTest.php 6 files changed, 265 insertions(+), 70 deletions(-) Approvals: Legoktm: Looks good to me, approved jenkins-bot: Verified diff --git a/extension.json b/extension.json index 51e8bf1..364d02f 100644 --- a/extension.json +++ b/extension.json @@ -10,7 +10,8 @@ "type": "other", "AutoloadClasses": { "CognateHooks": "src/CognateHooks.php", - "CognateStore": "src/CognateStore.php" + "CognateStore": "src/CognateStore.php", + "CognatePageHookHandler": "src/hooks/CognatePageHookHandler.php" }, "config": { "CognateDb": false, diff --git a/src/CognateHooks.php b/src/CognateHooks.php index f9766d2..56dfb9e 100644 --- a/src/CognateHooks.php +++ b/src/CognateHooks.php @@ -1,80 +1,28 @@ <?php -// TODO Make this class testable by creating non-static public methods for each hook and using an instance in the static methods, see https://git.wikimedia.org/blob/mediawiki%2Fextensions%2FWikibase/master/client%2FWikibaseClient.hooks.php -// TODO Split into two hook handler classes, one handling page-related stuff, the other handling database update and tests use MediaWiki\MediaWikiServices; class CognateHooks { - /** - * Occurs after the save page request has been processed. - * @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentSaveComplete - * - * @param WikiPage $article - * @param User $user - * @param Content $content - * @param string $summary - * @param boolean $isMinor - * @param boolean $isWatch - * @param $section Deprecated - * @param integer $flags - * @param {Revision|null} $revision - * @param Status $status - * @param integer $baseRevId - * - * @return boolean - */ - public static function onPageContentSaveComplete( - WikiPage $article, - User $user, - Content $content, - $summary, - $isMinor, - $isWatch, - $section, - $flags, - $revision, - Status $status, - $baseRevId - ) { - global $wgCognateNamespaces, $wgLanguageCode; - - $title = $article->getTitle(); - if ( !$title->inNamespaces( $wgCognateNamespaces ) ) { - return true; - } - $store = MediaWikiServices::getInstance()->getService( 'CognateStore' ); - $store->savePage( $wgLanguageCode, $title->getDBkey() ); - + public static function onPageContentSaveComplete() { + call_user_func_array( + [ + MediaWikiServices::getInstance()->getService( 'CognatePageHookHandler' ), + 'onPageContentSaveComplete' + ], + func_get_args() + ); return true; } - /** - * @param WikiPage $page - * @param Content|null $content - * @param DataUpdate[] $updates - * - * @return bool - */ - public static function onWikiPageDeletionUpdates( - WikiPage $page, - Content $content = null, - array &$updates - ) { - global $wgCognateNamespaces; - - $title = $page->getTitle(); - if ( $title->inNamespaces( $wgCognateNamespaces ) ) { - $updates[] = new MWCallableUpdate( - function () use ( $title ){ - global $wgLanguageCode; - $store = MediaWikiServices::getInstance()->getService( 'CognateStore' ); - $store->deletePage( $wgLanguageCode, $title->getDBkey() ); - }, - __METHOD__ - ); - } - + public static function onWikiPageDeletionUpdates() { + call_user_func_array( + [ + MediaWikiServices::getInstance()->getService( 'CognatePageHookHandler' ), + 'onWikiPageDeletionUpdates' + ], + func_get_args() + ); return true; } diff --git a/src/ServiceWiring.php b/src/ServiceWiring.php index af44ab9..d4c2ba2 100644 --- a/src/ServiceWiring.php +++ b/src/ServiceWiring.php @@ -17,4 +17,11 @@ } return new CognateStore( $lb ); }, + + 'CognatePageHookHandler' => function( MediaWikiServices $services ) { + return new CognatePageHookHandler( + $services->getMainConfig()->get( 'CognateNamespaces' ), + $services->getMainConfig()->get( 'LanguageCode' ) + ); + }, ]; diff --git a/src/hooks/CognatePageHookHandler.php b/src/hooks/CognatePageHookHandler.php new file mode 100644 index 0000000..1058ce3 --- /dev/null +++ b/src/hooks/CognatePageHookHandler.php @@ -0,0 +1,94 @@ +<?php + +use MediaWiki\MediaWikiServices; +use Wikimedia\Assert\Assert; + +/** + * @license GNU GPL v2+ + * @author Addshore + */ +class CognatePageHookHandler { + + /** + * @var string + */ + private $languageCode; + + /** + * @var int[] + */ + private $namespaces; + + /** + * CognatePageHookHandler constructor. + * + * @param int[] $namespaces array of namespace ids the hooks should operate on + * @param string $languageCode the language code of the current site + */ + public function __construct( array $namespaces, $languageCode ) { + $this->namespaces = $namespaces; + $this->languageCode = $languageCode; + } + + /** + * Occurs after the save page request has been processed. + * @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentSaveComplete + * + * @param WikiPage $article + * @param User $user + * @param Content $content + * @param string $summary + * @param boolean $isMinor + * @param boolean $isWatch + * @param mixed $section Deprecated + * @param integer $flags + * @param Revision|null $revision + * @param Status $status + * @param integer $baseRevId + */ + public function onPageContentSaveComplete( + WikiPage $article, + User $user, + Content $content, + $summary, + $isMinor, + $isWatch, + $section, + $flags, + $revision, + Status $status, + $baseRevId + ) { + if ( $article->getTitle()->inNamespaces( $this->namespaces ) ) { + $store = MediaWikiServices::getInstance()->getService( 'CognateStore' ); + $store->savePage( $this->languageCode, $article->getTitle()->getDBkey() ); + } + } + + /** + * Manipulate the list of DataUpdates to be applied when a page is deleted + * @see https://www.mediawiki.org/wiki/Manual:Hooks/WikiPageDeletionUpdates + * + * @param WikiPage $page + * @param Content|null $content + * @param DeferrableUpdate[] $updates + */ + public function onWikiPageDeletionUpdates( + WikiPage $page, + Content $content = null, + array &$updates + ) { + $title = $page->getTitle(); + $language = $this->languageCode; + if ( $title->inNamespaces( $this->namespaces ) ) { + $updates[] = new MWCallableUpdate( + function () use ( $title, $language ){ + $store = MediaWikiServices::getInstance()->getService( 'CognateStore' ); + $store->deletePage( $language, $title->getDBkey() ); + }, + __METHOD__ + ); + } + } + +} \ No newline at end of file diff --git a/tests/phpunit/ServiceWiringTest.php b/tests/phpunit/ServiceWiringTest.php index afbe911..2a137d7 100644 --- a/tests/phpunit/ServiceWiringTest.php +++ b/tests/phpunit/ServiceWiringTest.php @@ -9,7 +9,8 @@ public function provideServices() { return [ - [ 'CognateStore', CognateStore::class ] + [ 'CognateStore', CognateStore::class ], + [ 'CognatePageHookHandler', CognatePageHookHandler::class ], ]; } diff --git a/tests/phpunit/hooks/CognatePageHookHandlerTest.php b/tests/phpunit/hooks/CognatePageHookHandlerTest.php new file mode 100644 index 0000000..e6f898d --- /dev/null +++ b/tests/phpunit/hooks/CognatePageHookHandlerTest.php @@ -0,0 +1,144 @@ +<?php + +use MediaWiki\Linker\LinkTarget; + +/** + * @license GNU GPL v2+ + * @author Addshore + */ +class CognatePageHookHandlerTest extends MediaWikiTestCase { + + /** + * @var PHPUnit_Framework_MockObject_MockObject|CognateStore + */ + private $store; + + public function setUp() { + parent::setUp(); + $store = $this->getMockBuilder( CognateStore::class ) + ->disableOriginalConstructor() + ->getMock(); + $this->store = $store; + $this->overrideMwServices( + null, + [ + 'CognateStore' => function () use ( $store ) { + return $store; + }, + ] + ); + } + + public function test_onPageContentSaveComplete_namespaceMatch() { + $this->store->expects( $this->never() ) + ->method( 'deletePage' ); + $this->store->expects( $this->once() ) + ->method( 'savePage' ) + ->with( 'abc2', 'ArticleDbKey' ); + + $this->call_onPageContentSaveComplete( [ 0 ], 'abc2', new TitleValue( 0, 'ArticleDbKey' ) ); + } + + public function test_onPageContentSaveComplete_noNamespaceMatch() { + $this->store->expects( $this->never() ) + ->method( 'deletePage' ); + $this->store->expects( $this->never() ) + ->method( 'savePage' ); + + $this->call_onPageContentSaveComplete( [ 120 ], 'abc2', new TitleValue( 0, 'ArticleDbKey' ) ); + } + + /** + * @param int[] $namespaces + * @param string $language + * @param LinkTarget $linkTarget + */ + private function call_onPageContentSaveComplete( + array $namespaces, + $language, + LinkTarget $linkTarget + ) { + /** @var WikiPage|PHPUnit_Framework_MockObject_MockObject $mockWikiPage */ + $mockWikiPage = $this->getMockBuilder( 'WikiPage' ) + ->disableOriginalConstructor() + ->getMock(); + $mockWikiPage->expects( $this->any() ) + ->method( 'getTitle' ) + ->willReturn( Title::newFromLinkTarget( $linkTarget ) ); + + $handler = new CognatePageHookHandler( $namespaces, $language ); + $handler->onPageContentSaveComplete( + $mockWikiPage, + User::newFromId( 0 ), + $this->getMock( 'Content' ), + null, null, null, null, null, null, + $this->getMock( 'Status' ), + null + ); + } + + public function test_onWikiPageDeletionUpdates_namespaceMatch() { + $this->store->expects( $this->once() ) + ->method( 'deletePage' ) + ->with( 'abc2', 'ArticleDbKey' ); + $this->store->expects( $this->never() ) + ->method( 'savePage' ); + + $updates = $this->call_onWikiPageDeletionUpdates( + [ 0 ], + 'abc2', + new TitleValue( 0, 'ArticleDbKey' ) + ); + + $this->assertCount( 1, $updates ); + $updates[0]->doUpdate(); + } + + public function test_onWikiPageDeletionUpdates_noNamespaceMatch() { + $this->store->expects( $this->never() ) + ->method( 'deletePage' ); + $this->store->expects( $this->never() ) + ->method( 'savePage' ); + + $updates = $this->call_onWikiPageDeletionUpdates( + [ 120 ], + 'abc2', + new TitleValue( 0, 'ArticleDbKey' ) + ); + + $this->assertCount( 0, $updates ); + } + + /** + * @param int[] $namespaces + * @param string $language + * @param LinkTarget $linkTarget + * + * @return DeferrableUpdate[] + */ + private function call_onWikiPageDeletionUpdates( + array $namespaces, + $language, + LinkTarget $linkTarget + ) { + $updates = []; + + /** @var WikiPage|PHPUnit_Framework_MockObject_MockObject $mockWikiPage */ + $mockWikiPage = $this->getMockBuilder( 'WikiPage' ) + ->disableOriginalConstructor() + ->getMock(); + $mockWikiPage->expects( $this->any() ) + ->method( 'getTitle' ) + ->willReturn( Title::newFromLinkTarget( $linkTarget ) ); + + $handler = new CognatePageHookHandler( $namespaces, $language ); + $handler->onWikiPageDeletionUpdates( + $mockWikiPage, + null, + $updates + ); + + return $updates; + } + +} -- To view, visit https://gerrit.wikimedia.org/r/311452 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I88c3444e10d0e1f47f54fb83f2ea08b09e053e40 Gerrit-PatchSet: 9 Gerrit-Project: mediawiki/extensions/Cognate Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Gabriel Birke <gabriel.bi...@wikimedia.de> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits