diff --git a/src/lib/UserSetting/Setting/Language.php b/src/lib/UserSetting/Setting/Language.php index 0bfa245..6290f93 100644 --- a/src/lib/UserSetting/Setting/Language.php +++ b/src/lib/UserSetting/Setting/Language.php @@ -72,13 +72,16 @@ public function getDisplayValue(string $storageValue): string */ public function getDefaultValue(): string { + $defaultLocale = ''; $preferredLocales = $this->userLanguagePreferenceProvider->getPreferredLocales(); $list = $this->availableLocaleChoiceLoader->getChoiceList(); - $commonLocales = array_intersect($list, $preferredLocales); - $locales = empty($commonLocales) ? $preferredLocales : $commonLocales; + $commonLocales = array_intersect($preferredLocales, $list); + if (!empty($commonLocales)) { + $defaultLocale = reset($commonLocales); + } - return reset($locales); + return $defaultLocale; } /** diff --git a/tests/lib/UserSetting/Setting/LanguageTest.php b/tests/lib/UserSetting/Setting/LanguageTest.php index de313fb..0a39aa1 100644 --- a/tests/lib/UserSetting/Setting/LanguageTest.php +++ b/tests/lib/UserSetting/Setting/LanguageTest.php @@ -16,10 +16,10 @@ final class LanguageTest extends TestCase { - /** @var \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface|\PHPUnit\Framework\MockObject\MockObject */ + /** @var \Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface&\PHPUnit\Framework\MockObject\MockObject */ private UserLanguagePreferenceProviderInterface $userLanguagePreferenceProvider; - /** @var \Ibexa\User\Form\ChoiceList\Loader\AvailableLocaleChoiceLoader|\PHPUnit\Framework\MockObject\MockObject */ + /** @var \Ibexa\User\Form\ChoiceList\Loader\AvailableLocaleChoiceLoader&\PHPUnit\Framework\MockObject\MockObject */ private AvailableLocaleChoiceLoader $availableLocaleChoiceLoader; protected function setUp(): void @@ -61,6 +61,7 @@ public function testGetDefaultValue( 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']; + yield 'no available locale' => [['en_GB', 'en'], ['de', 'el', 'en_US'], '']; + yield 'user preferred language priority' => [['en_GB', 'en', 'de'], ['de', 'en', 'el'], 'en']; } }