Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-7862: Fixed user setting value #77

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/lib/UserSetting/Setting/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,16 @@ public function getDisplayValue(string $storageValue): string
*/
public function getDefaultValue(): string
{
$defaultLocale = '';
$preferredLocales = $this->userLanguagePreferenceProvider->getPreferredLocales();

return reset($preferredLocales);
$list = $this->availableLocaleChoiceLoader->getChoiceList();
$commonLocales = array_intersect($preferredLocales, $list);
if (!empty($commonLocales)) {
$defaultLocale = reset($commonLocales);
}

return $defaultLocale;
}

/**
Expand Down
67 changes: 67 additions & 0 deletions tests/lib/UserSetting/Setting/LanguageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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 'no available locale' => [['en_GB', 'en'], ['de', 'el', 'en_US'], ''];
yield 'user preferred language priority' => [['en_GB', 'en', 'de'], ['de', 'en', 'el'], 'en'];
}
}
Loading