From 8e4c83bfc6911a43273db5a0bffa6f1a1a6a2f80 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Fri, 6 Sep 2024 16:25:26 +0200 Subject: [PATCH] [refactor] Rewrite context as service Puts the ReferencePreviewsContext behind a service to improve testability. Bug: T363162 Change-Id: I63aa4144081a2625da1b89f4d56adb612741c8fc --- extension.json | 4 ++++ src/Hooks/CiteHooks.php | 13 ++++++------ src/ServiceWiring.php | 16 ++++++++++++++ tests/phpunit/CiteHooksTest.php | 21 +++++++++++++------ .../ReferencePreviewsContextTest.php | 16 ++++++++------ tests/phpunit/unit/CiteHooksUnitTest.php | 8 +++++-- 6 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 src/ServiceWiring.php diff --git a/extension.json b/extension.json index 829424c3b..5acc10517 100644 --- a/extension.json +++ b/extension.json @@ -39,6 +39,7 @@ "main": { "class": "Cite\\Hooks\\CiteHooks", "services": [ + "Cite.ReferencePreviewsContext", "UserOptionsLookup" ] }, @@ -239,6 +240,9 @@ "localBasePath": "modules", "remoteExtPath": "Cite/modules" }, + "ServiceWiringFiles": [ + "src/ServiceWiring.php" + ], "QUnitTestModule": { "localBasePath": "", "remoteExtPath": "Cite", diff --git a/src/Hooks/CiteHooks.php b/src/Hooks/CiteHooks.php index 9cc705d34..cfa49e34f 100644 --- a/src/Hooks/CiteHooks.php +++ b/src/Hooks/CiteHooks.php @@ -35,9 +35,14 @@ class CiteHooks implements EditPage__showEditForm_initialHook { + private ReferencePreviewsContext $referencePreviewsContext; private UserOptionsLookup $userOptionsLookup; - public function __construct( UserOptionsLookup $userOptionsLookup ) { + public function __construct( + ReferencePreviewsContext $referencePreviewsContext, + UserOptionsLookup $userOptionsLookup + ) { + $this->referencePreviewsContext = $referencePreviewsContext; $this->userOptionsLookup = $userOptionsLookup; } @@ -66,11 +71,7 @@ class CiteHooks implements * @param OutputPage $out */ public function onMakeGlobalVariablesScript( &$vars, $out ): void { - $referencePreviewsContext = new ReferencePreviewsContext( - $out->getConfig(), - $this->userOptionsLookup - ); - $vars['wgCiteReferencePreviewsActive'] = $referencePreviewsContext->isReferencePreviewsEnabled( + $vars['wgCiteReferencePreviewsActive'] = $this->referencePreviewsContext->isReferencePreviewsEnabled( $out->getUser(), $out->getSkin() ); diff --git a/src/ServiceWiring.php b/src/ServiceWiring.php new file mode 100644 index 000000000..99c243afe --- /dev/null +++ b/src/ServiceWiring.php @@ -0,0 +1,16 @@ + static function ( MediaWikiServices $services ): ReferencePreviewsContext { + return new ReferencePreviewsContext( + $services->getMainConfig(), + $services->getUserOptionsLookup() + ); + }, +]; diff --git a/tests/phpunit/CiteHooksTest.php b/tests/phpunit/CiteHooksTest.php index 4918238fb..0ced36b06 100644 --- a/tests/phpunit/CiteHooksTest.php +++ b/tests/phpunit/CiteHooksTest.php @@ -26,8 +26,11 @@ class CiteHooksTest extends \MediaWikiIntegrationTestCase { 'CiteBookReferencing' => $enabled, ] ); - $citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) ); - $citeHooks->onResourceLoaderGetConfigVars( $vars, 'vector', $config ); + ( new CiteHooks( + $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ), + new StaticUserOptionsLookup( [] ) + ) ) + ->onResourceLoaderGetConfigVars( $vars, 'vector', $config ); $this->assertSame( [ 'wgCiteVisualEditorOtherGroup' => $enabled, @@ -48,8 +51,11 @@ class CiteHooksTest extends \MediaWikiIntegrationTestCase { $resourceLoader->expects( $this->exactly( (int)$enabled ) ) ->method( 'register' ); - $citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) ); - $citeHooks->onResourceLoaderRegisterModules( $resourceLoader ); + ( new CiteHooks( + $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ), + new StaticUserOptionsLookup( [] ) + ) ) + ->onResourceLoaderRegisterModules( $resourceLoader ); } /** @@ -63,8 +69,11 @@ class CiteHooksTest extends \MediaWikiIntegrationTestCase { $data = []; - $citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) ); - $citeHooks->onAPIQuerySiteInfoGeneralInfo( $api, $data ); + ( new CiteHooks( + $this->getServiceContainer()->getService( 'Cite.ReferencePreviewsContext' ), + new StaticUserOptionsLookup( [] ) + ) ) + ->onAPIQuerySiteInfoGeneralInfo( $api, $data ); $this->assertSame( [ 'citeresponsivereferences' => $enabled ], $data ); } diff --git a/tests/phpunit/integration/ReferencePreviews/ReferencePreviewsContextTest.php b/tests/phpunit/integration/ReferencePreviews/ReferencePreviewsContextTest.php index f2ae9d621..69b196d84 100644 --- a/tests/phpunit/integration/ReferencePreviews/ReferencePreviewsContextTest.php +++ b/tests/phpunit/integration/ReferencePreviews/ReferencePreviewsContextTest.php @@ -31,8 +31,6 @@ class ReferencePreviewsContextTest extends MediaWikiIntegrationTestCase { ] ); $userOptLookup = $this->createNoOpMock( UserOptionsLookup::class ); - $context = new ReferencePreviewsContext( $config, $userOptLookup ); - $user = $this->createMock( User::class ); $user->method( 'isNamed' )->willReturn( false ); @@ -40,7 +38,11 @@ class ReferencePreviewsContextTest extends MediaWikiIntegrationTestCase { $skin->method( 'getSkinName' )->willReturn( $skinName ); $this->assertSame( $expected, - $context->isReferencePreviewsEnabled( $user, $skin ), + ( new ReferencePreviewsContext( + $config, + $userOptLookup + ) ) + ->isReferencePreviewsEnabled( $user, $skin ), ( $expected ? 'A' : 'No' ) . ' module is sent to the user.' ); } @@ -73,12 +75,14 @@ class ReferencePreviewsContextTest extends MediaWikiIntegrationTestCase { 'CiteReferencePreviewsConflictingRefTooltipsGadgetName' => '', ] ); - $context = new ReferencePreviewsContext( $config, $userOptLookup ); - $skin = $this->createMock( Skin::class ); $this->assertSame( $expected, - $context->isReferencePreviewsEnabled( $user, $skin ), + ( new ReferencePreviewsContext( + $config, + $userOptLookup + ) ) + ->isReferencePreviewsEnabled( $user, $skin ), ( $expected ? 'A' : 'No' ) . ' module is sent to the user.' ); } diff --git a/tests/phpunit/unit/CiteHooksUnitTest.php b/tests/phpunit/unit/CiteHooksUnitTest.php index a487b8e41..a4817e8ae 100644 --- a/tests/phpunit/unit/CiteHooksUnitTest.php +++ b/tests/phpunit/unit/CiteHooksUnitTest.php @@ -3,6 +3,7 @@ namespace Cite\Tests\Unit; use Cite\Hooks\CiteHooks; +use Cite\ReferencePreviews\ReferencePreviewsContext; use MediaWiki\Title\Title; use MediaWiki\User\Options\StaticUserOptionsLookup; @@ -19,8 +20,11 @@ class CiteHooksUnitTest extends \MediaWikiUnitTestCase { $title->method( 'getText' ) ->willReturn( 'Cite-tool-definition.json' ); - $citeHooks = new CiteHooks( new StaticUserOptionsLookup( [] ) ); - $citeHooks->onContentHandlerDefaultModelFor( $title, $model ); + ( new CiteHooks( + $this->createMock( ReferencePreviewsContext::class ), + new StaticUserOptionsLookup( [] ) + ) ) + ->onContentHandlerDefaultModelFor( $title, $model ); $this->assertSame( CONTENT_MODEL_JSON, $model ); }