Skip to content

Commit

Permalink
IBX-7862: Fixed user setting value
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Apr 29, 2024
1 parent b9c431f commit 82c9f92
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/lib/UserSetting/Setting/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/lib/UserSetting/Setting/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
}
}

0 comments on commit 82c9f92

Please sign in to comment.