diff --git a/includes/resourceloader/ResourceLoaderImage.php b/includes/resourceloader/ResourceLoaderImage.php index bea192d4f7c..73d5fc48067 100644 --- a/includes/resourceloader/ResourceLoaderImage.php +++ b/includes/resourceloader/ResourceLoaderImage.php @@ -18,6 +18,8 @@ * @file */ +use MediaWiki\MediaWikiServices; +use MediaWiki\Languages\LanguageFallback; use MediaWiki\Shell\Shell; /** @@ -141,6 +143,17 @@ class ResourceLoaderImage { return array_keys( $this->variants ); } + /** + * @internal For unit testing overrride + * @param string $lang + * @return string[] + */ + protected function getLangFallbacks( string $lang ) : array { + return MediaWikiServices::getInstance() + ->getLanguageFallback() + ->getAll( $lang, LanguageFallback::STRICT ); + } + /** * Get the path to image file for given context. * @@ -158,7 +171,7 @@ class ResourceLoaderImage { if ( isset( $desc['lang'][$contextLang] ) ) { return $this->getLocalPath( $desc['lang'][$contextLang] ); } - $fallbacks = Language::getFallbacksFor( $contextLang, Language::STRICT_FALLBACKS ); + $fallbacks = $this->getLangFallbacks( $contextLang ); foreach ( $fallbacks as $lang ) { if ( isset( $desc['lang'][$lang] ) ) { return $this->getLocalPath( $desc['lang'][$lang] ); diff --git a/tests/phpunit/unit/includes/resourceloader/ResourceLoaderImageTest.php b/tests/phpunit/unit/includes/resourceloader/ResourceLoaderImageTest.php index 5d53c302e7e..34f24daed25 100644 --- a/tests/phpunit/unit/includes/resourceloader/ResourceLoaderImageTest.php +++ b/tests/phpunit/unit/includes/resourceloader/ResourceLoaderImageTest.php @@ -53,7 +53,6 @@ class ResourceLoaderImageTest extends MediaWikiUnitTestCase { * @dataProvider provideGetPath */ public function testGetPath( $imageName, $languageCode, $path ) { - $this->markTestSkipped( 'Depends on overriding LanguageFallback/LocalisationCache' ); static $dirMap = [ 'en' => 'ltr', 'en-gb' => 'ltr', @@ -70,13 +69,6 @@ class ResourceLoaderImageTest extends MediaWikiUnitTestCase { ); $context->setLanguage( $languageCode ); $context->setDirection( $dirMap[$languageCode] ); - Language::$dataCache = $this->createMock( LocalisationCache::class ); - Language::$dataCache->method( 'getItem' )->will( $this->returnCallback( function ( $code ) { - return ( [ - 'en-gb' => [ 'en' ], - 'de-formal' => [ 'de' ], - ] )[ $code ] ?? []; - } ) ); $this->assertEquals( $image->getPath( $context ), $this->imagesPath . '/' . $path ); } @@ -121,6 +113,11 @@ class ResourceLoaderImageTest extends MediaWikiUnitTestCase { } class ResourceLoaderImageTestable extends ResourceLoaderImage { + private $mockFallbacks = [ + 'en-gb' => [ 'en' ], + 'de-formal' => [ 'de' ], + ]; + // Make some protected methods public public function massageSvgPathdata( $svg ) { return parent::massageSvgPathdata( $svg ); @@ -130,4 +127,8 @@ class ResourceLoaderImageTestable extends ResourceLoaderImage { public function rasterize( $svg ) { return 'RASTERIZESTUB'; } + + protected function getLangFallbacks( string $code ) : array { + return $this->mockFallbacks[$code] ?? []; + } }