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 17, 2024
1 parent ca8da09 commit 27dbb82
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/lib/UserSetting/Setting/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
66 changes: 66 additions & 0 deletions tests/lib/UserSetting/Setting/LanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\User\UserSetting;

use Ibexa\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use Ibexa\User\Form\ChoiceList\Loader\AvailableLocaleChoiceLoader;
use Ibexa\User\UserSetting\Setting\Language;
use PHPUnit\Framework\TestCase;
use Symfony\Contracts\Translation\TranslatorInterface;

final class LanguageTest extends TestCase
{
/** @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 */
private AvailableLocaleChoiceLoader $availableLocaleChoiceLoader;

protected function setUp(): void
{
$this->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<string, array<mixed>>
*/
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'];
}
}

0 comments on commit 27dbb82

Please sign in to comment.