From 52583a336dd328138b1f93bfd2e58b0024df5d7a Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sat, 2 Mar 2024 19:34:53 +0000 Subject: [PATCH] Inject service LanguageConverterFactory in LanguagesHelper Change-Id: I2fd1b4c3b25a94cca59d08216390652759859a62 --- includes/LanguagesHelper.php | 14 ++++++--- includes/ServiceWiring.php | 7 +++-- tests/phpunit/LanguagesHelperTest.php | 45 +++++++++++++++++++++------ 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/includes/LanguagesHelper.php b/includes/LanguagesHelper.php index 2e2fe15e9..afa8d8d99 100644 --- a/includes/LanguagesHelper.php +++ b/includes/LanguagesHelper.php @@ -19,7 +19,7 @@ */ namespace MediaWiki\Minerva; -use MediaWiki\MediaWikiServices; +use MediaWiki\Languages\LanguageConverterFactory; use MediaWiki\Output\OutputPage; use MediaWiki\Title\Title; @@ -29,15 +29,22 @@ use MediaWiki\Title\Title; */ class LanguagesHelper { + private LanguageConverterFactory $languageConverterFactory; + /** * @var bool */ private $hasLanguages; /** + * @param LanguageConverterFactory $languageConverterFactory * @param OutputPage $out Output page to fetch language links */ - public function __construct( OutputPage $out ) { + public function __construct( + LanguageConverterFactory $languageConverterFactory, + OutputPage $out + ) { + $this->languageConverterFactory = $languageConverterFactory; $this->hasLanguages = $out->getLanguageLinks() !== []; } @@ -50,8 +57,7 @@ class LanguagesHelper { if ( $this->hasLanguages ) { return true; } - $langConv = MediaWikiServices::getInstance()->getLanguageConverterFactory() - ->getLanguageConverter( $title->getPageLanguage() ); + $langConv = $this->languageConverterFactory->getLanguageConverter( $title->getPageLanguage() ); return $langConv->hasVariants(); } } diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 1ace5589e..aae30d3d8 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -151,8 +151,11 @@ return [ RequestContext::getMain() ); }, - 'Minerva.LanguagesHelper' => static function (): LanguagesHelper { - return new LanguagesHelper( RequestContext::getMain()->getOutput() ); + 'Minerva.LanguagesHelper' => static function ( MediaWikiServices $services ): LanguagesHelper { + return new LanguagesHelper( + $services->getLanguageConverterFactory(), + RequestContext::getMain()->getOutput() + ); }, 'Minerva.SkinOptions' => static function (): SkinOptions { return new SkinOptions(); diff --git a/tests/phpunit/LanguagesHelperTest.php b/tests/phpunit/LanguagesHelperTest.php index 6728eabe8..11a11ad50 100644 --- a/tests/phpunit/LanguagesHelperTest.php +++ b/tests/phpunit/LanguagesHelperTest.php @@ -31,17 +31,24 @@ class LanguagesHelperTest extends MediaWikiIntegrationTestCase { } /** - * Build test Title object + * Build test LanguageConverterFactory object * @param bool $hasVariants - * @return Title + * @return LanguageConverterFactory */ - private function getTitle( $hasVariants ) { + private function getLanguageConverterFactory( $hasVariants ) { $langConv = $this->createMock( ILanguageConverter::class ); $langConv->method( 'hasVariants' )->willReturn( $hasVariants ); $langConvFactory = $this->createMock( LanguageConverterFactory::class ); $langConvFactory->method( 'getLanguageConverter' )->willReturn( $langConv ); - $this->setService( 'LanguageConverterFactory', $langConvFactory ); + return $langConvFactory; + } + + /** + * Build test Title object + * @return Title + */ + private function getTitle() { $languageMock = $this->createMock( Language::class ); $title = $this->createMock( Title::class ); $title->method( 'getPageLanguage' ) @@ -55,10 +62,19 @@ class LanguagesHelperTest extends MediaWikiIntegrationTestCase { * @covers ::doesTitleHasLanguagesOrVariants */ public function testReturnsWhenOutputPageHasLangLinks() { - $helper = new LanguagesHelper( $this->getOutput( [ 'pl:StronaTestowa', 'en:TestPage' ] ) ); + $helper = new LanguagesHelper( + $this->getLanguageConverterFactory( false ), + $this->getOutput( [ 'pl:StronaTestowa', 'en:TestPage' ] ) + ); - $this->assertTrue( $helper->doesTitleHasLanguagesOrVariants( $this->getTitle( false ) ) ); - $this->assertTrue( $helper->doesTitleHasLanguagesOrVariants( $this->getTitle( true ) ) ); + $this->assertTrue( $helper->doesTitleHasLanguagesOrVariants( $this->getTitle() ) ); + + $helper = new LanguagesHelper( + $this->getLanguageConverterFactory( true ), + $this->getOutput( [ 'pl:StronaTestowa', 'en:TestPage' ] ) + ); + + $this->assertTrue( $helper->doesTitleHasLanguagesOrVariants( $this->getTitle() ) ); } /** @@ -66,11 +82,20 @@ class LanguagesHelperTest extends MediaWikiIntegrationTestCase { * @covers ::doesTitleHasLanguagesOrVariants */ public function testReturnsWhenOutputDoesNotHaveLangLinks() { - $helper = new LanguagesHelper( $this->getOutput( [] ) ); + $helper = new LanguagesHelper( + $this->getLanguageConverterFactory( false ), + $this->getOutput( [] ) + ); $this->assertFalse( $helper->doesTitleHasLanguagesOrVariants( - $this->getTitle( false ) ) ); + $this->getTitle() ) ); + + $helper = new LanguagesHelper( + $this->getLanguageConverterFactory( true ), + $this->getOutput( [] ) + ); + $this->assertTrue( $helper->doesTitleHasLanguagesOrVariants( - $this->getTitle( true ) ) ); + $this->getTitle() ) ); } }