From 4faf42838522a5320a41ff7b9cb96fd1c39802ab Mon Sep 17 00:00:00 2001 From: mikolaj Date: Mon, 8 Apr 2024 14:43:34 +0200 Subject: [PATCH 1/3] 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']; + } +} From 6b0a9e5f7d29735264251485ed04d749b5fbf6d2 Mon Sep 17 00:00:00 2001 From: mikolaj Date: Wed, 17 Apr 2024 15:13:54 +0200 Subject: [PATCH 2/3] IBX-7862: Fixed user setting value --- phpstan-baseline.neon | 5 ----- 1 file changed, 5 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 1638df3..6652d0f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -760,11 +760,6 @@ parameters: count: 1 path: src/lib/UserSetting/Setting/FullDateTimeFormat.php - - - message: "#^Method Ibexa\\\\User\\\\UserSetting\\\\Setting\\\\Language\\:\\:getDefaultValue\\(\\) should return string but returns string\\|false\\.$#" - count: 1 - path: src/lib/UserSetting/Setting/Language.php - - message: "#^Method Ibexa\\\\User\\\\UserSetting\\\\Setting\\\\ShortDateTimeFormat\\:\\:getDefaultValue\\(\\) should return string but returns string\\|null\\.$#" count: 1 From e3ca2a9f5ac8e176a8d7758d7eb80d33b73f3074 Mon Sep 17 00:00:00 2001 From: mikolaj Date: Mon, 29 Apr 2024 14:42:33 +0200 Subject: [PATCH 3/3] IBX-7862: Fixed user setting value --- src/lib/UserSetting/Setting/Language.php | 9 ++++++--- tests/lib/UserSetting/Setting/LanguageTest.php | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) 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']; } }