Skip to content
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
22 changes: 1 addition & 21 deletions lib/private/L10N/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,7 @@ public function findGenericLanguage(?string $appId = null): string {
return $defaultLanguage;
}

// Step 3.1: Check if Nextcloud is already installed before we try to access user info
if (!$this->config->getSystemValueBool('installed', false)) {
return 'en';
}
// Step 3.2: Check the current user (if any) for their preferred language
$user = $this->userSession->getUser();
if ($user !== null) {
$userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null);
if ($userLang !== null) {
return $userLang;
}
}

// Step 4: Check the request headers
try {
return $this->getLanguageFromRequest($appId);
} catch (LanguageNotFoundException $e) {
// Ignore and continue
}

// Step 5: fall back to English
// Step 3: fall back to English
return 'en';
}

Expand Down
99 changes: 38 additions & 61 deletions tests/lib/L10N/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,24 @@ public function testFindLanguage($loggedIn, $availableLang, $expected): void {
self::assertSame($expected, $lang);
}

public function testFindGenericLanguageByRequestParam(): void {
$factory = $this->getFactory();
$this->request->expects(self::once())
->method('getParam')
->with('forceLanguage')
->willReturn('cz');

$lang = $factory->findGenericLanguage();

self::assertSame('cz', $lang);
}

public function testFindGenericLanguageByEnforcedLanguage(): void {
$factory = $this->getFactory();
$this->request->expects(self::once())
->method('getParam')
->with('forceLanguage')
->willReturn(null);
$this->config->expects(self::once())
->method('getSystemValue')
->with('force_language', false)
Expand All @@ -577,6 +593,10 @@ public function testFindGenericLanguageByEnforcedLanguage(): void {

public function testFindGenericLanguageByDefaultLanguage(): void {
$factory = $this->getFactory(['languageExists']);
$this->request->expects(self::once())
->method('getParam')
->with('forceLanguage')
->willReturn(null);
$this->config->expects(self::exactly(2))
->method('getSystemValue')
->willReturnMap([
Expand All @@ -593,83 +613,40 @@ public function testFindGenericLanguageByDefaultLanguage(): void {
self::assertSame('cz', $lang);
}

public function testFindGenericLanguageByUserLanguage(): void {
$factory = $this->getFactory();
public function testFindGenericLanguageByDefaultLanguageNotExists(): void {
$factory = $this->getFactory(['languageExists']);
$this->request->expects(self::once())
->method('getParam')
->with('forceLanguage')
->willReturn(null);
$this->config->expects(self::exactly(2))
->method('getSystemValue')
->willReturnMap([
['force_language', false, false,],
['default_language', false, false,],
]);
$user = $this->createMock(IUser::class);
$this->userSession->expects(self::once())
->method('getUser')
->willReturn($user);
$user->method('getUID')->willReturn('user123');
$this->config->expects(self::once())
->method('getUserValue')
->with('user123', 'core', 'lang', null)
->willReturn('cz');

$lang = $factory->findGenericLanguage();

self::assertSame('cz', $lang);
}

public function testFindGenericLanguageByRequestLanguage(): void {
$factory = $this->getFactory(['findAvailableLanguages', 'languageExists']);
$this->config->method('getSystemValue')
->willReturnMap([
['force_language', false, false,],
['default_language', false, false,],
['default_language', false, 'cz',],
]);
$user = $this->createMock(IUser::class);
$this->userSession->expects(self::once())
->method('getUser')
->willReturn($user);
$user->method('getUID')->willReturn('user123');
$this->config->expects(self::once())
->method('getUserValue')
->with('user123', 'core', 'lang', null)
->willReturn(null);
$this->request->expects(self::once())
->method('getHeader')
->with('ACCEPT_LANGUAGE')
->willReturn('cz');
$factory->expects(self::once())
->method('findAvailableLanguages')
->with(null)
->willReturn(['cz']);
->method('languageExists')
->with(null, 'cz')
->willReturn(false);

$lang = $factory->findGenericLanguage();

self::assertSame('cz', $lang);
self::assertSame('en', $lang);
}

public function testFindGenericLanguageFallback(): void {
$factory = $this->getFactory(['findAvailableLanguages', 'languageExists']);
$this->config->method('getSystemValue')
$factory = $this->getFactory();
$this->request->expects(self::once())
->method('getParam')
->with('forceLanguage')
->willReturn(null);
$this->config->expects(self::exactly(2))
->method('getSystemValue')
->willReturnMap([
['force_language', false, false,],
['default_language', false, false,],
]);
$user = $this->createMock(IUser::class);
$this->userSession->expects(self::once())
->method('getUser')
->willReturn($user);
$user->method('getUID')->willReturn('user123');
$this->config->expects(self::once())
->method('getUserValue')
->with('user123', 'core', 'lang', null)
->willReturn(null);
$this->request->expects(self::once())
->method('getHeader')
->with('ACCEPT_LANGUAGE')
->willReturn('');
$factory->expects(self::never())
->method('findAvailableLanguages');
$factory->expects(self::never())
->method('languageExists');

$lang = $factory->findGenericLanguage();

Expand Down
Loading