Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/4.6' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mikadamczyk committed Jul 19, 2024
2 parents 8d5b5cd + f4f48cf commit 4398c55
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
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'];
}
}

0 comments on commit 4398c55

Please sign in to comment.