From d0b75036384da18144ccdcef5db109e8ee5a6b8a Mon Sep 17 00:00:00 2001 From: mikolaj Date: Mon, 8 Apr 2024 14:43:34 +0200 Subject: [PATCH] IBX-7862: Fixed user setting value --- src/lib/UserSetting/Setting/Language.php | 6 +- .../lib/UserSetting/Setting/LanguageTest.php | 66 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/lib/UserSetting/Setting/LanguageTest.php diff --git a/src/lib/UserSetting/Setting/Language.php b/src/lib/UserSetting/Setting/Language.php index 713563c..0bfa245 100644 --- a/src/lib/UserSetting/Setting/Language.php +++ b/src/lib/UserSetting/Setting/Language.php @@ -74,7 +74,11 @@ public function getDefaultValue(): string { $preferredLocales = $this->userLanguagePreferenceProvider->getPreferredLocales(); - return reset($preferredLocales); + $list = $this->availableLocaleChoiceLoader->getChoiceList(); + $commonLocales = array_intersect($list, $preferredLocales); + $locales = empty($commonLocales) ? $preferredLocales : $commonLocales; + + return reset($locales); } /** diff --git a/tests/lib/UserSetting/Setting/LanguageTest.php b/tests/lib/UserSetting/Setting/LanguageTest.php new file mode 100644 index 0000000..de313fb --- /dev/null +++ b/tests/lib/UserSetting/Setting/LanguageTest.php @@ -0,0 +1,66 @@ +userLanguagePreferenceProvider = $this->createMock( + UserLanguagePreferenceProviderInterface::class + ); + $this->availableLocaleChoiceLoader = $this->createMock( + AvailableLocaleChoiceLoader::class + ); + } + + /** + * @dataProvider providerForDefaultValue + * + * @param string[] $availableLocales + * @param string[] $preferredLocales + */ + public function testGetDefaultValue( + array $preferredLocales, + array $availableLocales, + string $expectedDefaultValue + ): void { + $this->userLanguagePreferenceProvider->method('getPreferredLocales')->willReturn($preferredLocales); + $this->availableLocaleChoiceLoader->method('getChoiceList')->willReturn($availableLocales); + + $language = new Language( + $this->createMock(TranslatorInterface::class), + $this->userLanguagePreferenceProvider, + $this->availableLocaleChoiceLoader, + ); + + self::assertSame($expectedDefaultValue, $language->getDefaultValue()); + } + + /** + * @return iterable> + */ + public function providerForDefaultValue(): iterable + { + yield 'intersection' => [['en_GB', 'en'], ['en', 'de', 'el', 'en_US'], 'en']; + yield 'preferred_locales' => [['en_GB', 'en'], ['de', 'el', 'en_US'], 'en_GB']; + } +}