From e0dc1c0c32f594cf02cab1933f3f15a89f7a4edc Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 29 Jun 2024 21:36:41 +0000 Subject: [PATCH] Use service 'GadgetsRepo' instead of deprecated GadgetRepo::singleton() The service 'GadgetsRepo' gets injected as optional service. This change requires a phan dependency on extension Gadgets in project integration/config in file zuul/parameter_functions.py: I5052e0c666b7dc7af6061e57001f9feac666e029 Change-Id: Ib405ad79b3c348bed51a8938a6a8f73bd35267d2 --- .phan/config.php | 18 +++++++++++++++++- extension.json | 3 +++ includes/Hooks.php | 14 ++++++++------ tests/phpunit/HookTest.php | 10 +++++----- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/.phan/config.php b/.phan/config.php index 76efd06d..9178fb24 100644 --- a/.phan/config.php +++ b/.phan/config.php @@ -1,3 +1,19 @@ userOptionsLookup = $userOptionsLookup; $this->useV6 = $config->get( 'CodeMirrorV6' ); $this->conflictingGadgets = $config->get( 'CodeMirrorConflictingGadgets' ); $this->isSupportedRtlWiki = $config->get( 'CodeMirrorRTL' ); + $this->gadgetRepo = $gadgetRepo; } /** @@ -88,15 +92,13 @@ class Hooks implements * @return bool */ private function conflictingGadgetsEnabled( ExtensionRegistry $extensionRegistry, User $user ): bool { - if ( !$extensionRegistry->isLoaded( 'Gadgets' ) ) { + if ( !$extensionRegistry->isLoaded( 'Gadgets' ) || !$this->gadgetRepo ) { return false; } - // @phan-suppress-next-line PhanUndeclaredClassMethod Code path won't be followed if class doesn't exist. - $gadgetRepo = GadgetRepo::singleton(); - $conflictingGadgets = array_intersect( $this->conflictingGadgets, $gadgetRepo->getGadgetIds() ); + $conflictingGadgets = array_intersect( $this->conflictingGadgets, $this->gadgetRepo->getGadgetIds() ); foreach ( $conflictingGadgets as $conflictingGadget ) { try { - if ( $gadgetRepo->getGadget( $conflictingGadget )->isEnabled( $user ) ) { + if ( $this->gadgetRepo->getGadget( $conflictingGadget )->isEnabled( $user ) ) { return true; } } catch ( InvalidArgumentException $e ) { diff --git a/tests/phpunit/HookTest.php b/tests/phpunit/HookTest.php index da411f63..40461abe 100644 --- a/tests/phpunit/HookTest.php +++ b/tests/phpunit/HookTest.php @@ -59,7 +59,7 @@ class HookTest extends MediaWikiIntegrationTestCase { $isFirstCall = false; } ); - $hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig() ); + $hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig(), null ); $method = $readOnly ? 'onEditPage__showReadOnlyForm_initial' : 'onEditPage__showEditForm_initial'; $hooks->{$method}( $this->createMock( EditPage::class ), $out ); } @@ -97,7 +97,7 @@ class HookTest extends MediaWikiIntegrationTestCase { // CodeMirror 5 $this->overrideConfigValues( [ 'CodeMirrorV6' => false ] ); - $hook = new Hooks( $userOptionsLookup, $config ); + $hook = new Hooks( $userOptionsLookup, $config, null ); $preferences = []; $hook->onGetPreferences( $user, $preferences ); self::assertArrayHasKey( 'usecodemirror', $preferences ); @@ -107,7 +107,7 @@ class HookTest extends MediaWikiIntegrationTestCase { // CodeMirror 6 $this->overrideConfigValues( [ 'CodeMirrorV6' => true ] ); - $hook = new Hooks( $userOptionsLookup, $config ); + $hook = new Hooks( $userOptionsLookup, $config, null ); $preferences = []; $hook->onGetPreferences( $user, $preferences ); self::assertArrayHasKey( 'usecodemirror', $preferences ); @@ -153,6 +153,7 @@ class HookTest extends MediaWikiIntegrationTestCase { ->with( 'CodeMirrorContentModels' ) ->willReturn( [ CONTENT_MODEL_WIKITEXT ] ); + $gadgetRepoMock = null; if ( $conds['gadget'] ) { $gadgetMock = $this->createMock( Gadget::class ); $gadgetMock->expects( $this->once() ) @@ -165,10 +166,9 @@ class HookTest extends MediaWikiIntegrationTestCase { $gadgetRepoMock->expects( $this->once() ) ->method( 'getGadgetIds' ) ->willReturn( [ $conds['gadget'] ] ); - GadgetRepo::setSingleton( $gadgetRepoMock ); } - $hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig() ); + $hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig(), $gadgetRepoMock ); self::assertSame( $expectation, $hooks->shouldLoadCodeMirror( $out, $extensionRegistry ) ); }