From 4a9f8623b50ab0ba6a7fd411c0750a6bd5f63c14 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 7 Jul 2017 11:17:54 +0200 Subject: [PATCH] Fix review & tests Signed-off-by: Thomas Citharel --- .../lib/Controller/UsersController.php | 4 +- .../tests/Controller/UsersControllerTest.php | 13 ++-- lib/private/L10N/Factory.php | 60 +++++++++++++++++++ .../{Settings/Personal => L10N}/locales.json | 0 .../Settings/Personal/PersonalInfo.php | 13 +--- lib/public/L10N/IFactory.php | 22 +++++++ .../settings/personal/personal.info.php | 2 + 7 files changed, 97 insertions(+), 17 deletions(-) rename lib/private/{Settings/Personal => L10N}/locales.json (100%) diff --git a/apps/provisioning_api/lib/Controller/UsersController.php b/apps/provisioning_api/lib/Controller/UsersController.php index 0430226e12..32967742bd 100644 --- a/apps/provisioning_api/lib/Controller/UsersController.php +++ b/apps/provisioning_api/lib/Controller/UsersController.php @@ -522,7 +522,9 @@ class UsersController extends AUserData { $this->config->setUserValue($targetUser->getUID(), 'core', 'lang', $value); break; case 'locale': - // do some stuff + if (!$this->l10nFactory->localeExists($value)) { + throw new OCSException('Invalid locale', 102); + } $this->config->setUserValue($targetUser->getUID(), 'core', 'locale', $value); break; case AccountManager::PROPERTY_EMAIL: diff --git a/apps/provisioning_api/tests/Controller/UsersControllerTest.php b/apps/provisioning_api/tests/Controller/UsersControllerTest.php index 114742de4f..0de62bc0d1 100644 --- a/apps/provisioning_api/tests/Controller/UsersControllerTest.php +++ b/apps/provisioning_api/tests/Controller/UsersControllerTest.php @@ -747,6 +747,10 @@ class UsersControllerTest extends TestCase { ->expects($this->once()) ->method('getDisplayName') ->will($this->returnValue('Demo User')); + $targetUser + ->expects($this->exactly(5)) + ->method('getUID') + ->will($this->returnValue('UID')); $targetUser ->expects($this->once()) ->method('getHome') @@ -759,10 +763,6 @@ class UsersControllerTest extends TestCase { ->expects($this->once()) ->method('getBackendClassName') ->will($this->returnValue('Database')); - $targetUser - ->expects($this->exactly(5)) - ->method('getUID') - ->will($this->returnValue('UID')); $expected = [ 'id' => 'UID', @@ -780,6 +780,7 @@ class UsersControllerTest extends TestCase { 'twitter' => 'twitter', 'groups' => ['group0', 'group1', 'group2'], 'language' => 'de', + 'locale' => null, ]; $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID'])); } @@ -865,7 +866,7 @@ class UsersControllerTest extends TestCase { ->method('getBackendClassName') ->will($this->returnValue('Database')); $targetUser - ->expects($this->exactly(5)) + ->expects($this->exactly(6)) ->method('getUID') ->will($this->returnValue('UID')); $this->accountManager->expects($this->any())->method('getUser') @@ -895,6 +896,7 @@ class UsersControllerTest extends TestCase { 'twitter' => 'twitter', 'groups' => [], 'language' => 'da', + 'locale' => null, ]; $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID'])); } @@ -1050,6 +1052,7 @@ class UsersControllerTest extends TestCase { 'twitter' => 'twitter', 'groups' => [], 'language' => 'ru', + 'locale' => null, ]; $this->assertEquals($expected, $this->invokePrivate($this->api, 'getUserData', ['UID'])); } diff --git a/lib/private/L10N/Factory.php b/lib/private/L10N/Factory.php index c84bc53f9e..a6b91d53e4 100644 --- a/lib/private/L10N/Factory.php +++ b/lib/private/L10N/Factory.php @@ -115,6 +115,10 @@ class Factory implements IFactory { $lang = $this->findLanguage($app); } + if ($locale === null || !$this->localeExists($locale)) { + $locale = $this->findLocale($app, $lang); + } + if (!isset($this->instances[$lang][$app])) { $this->instances[$lang][$app] = new L10N( $this, $app, $lang, $locale, @@ -186,6 +190,38 @@ class Factory implements IFactory { return 'en'; } + public function findLocale($app = null, $lang = null) + { + if ($this->config->getSystemValue('installed', false)) { + $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; + $userLocale = null; + if (null !== $userId) { + $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null); + } + } else { + $userId = null; + $userLocale = null; + } + + if ($userLocale && $this->localeExists($userLocale)) { + return $userLocale; + } + + // If no user locale set, use lang as locale + if (null !== $lang && $this->localeExists($lang)) { + return $lang; + } + + // Default : use system default locale + $defaultLocale = $this->config->getSystemValue('default_locale', false); + if ($defaultLocale !== false && $this->localeExists($defaultLocale)) { + return $defaultLocale; + } + + // At last, return USA + return 'en_US'; + } + /** * Find all available languages for an app * @@ -237,6 +273,12 @@ class Factory implements IFactory { return $available; } + public function findAvailableLocales() + { + $localeData = file_get_contents(__DIR__ . '/locales.json'); + return json_decode($localeData, true); + } + /** * @param string|null $app App id or null for core * @param string $lang @@ -251,6 +293,24 @@ class Factory implements IFactory { return array_search($lang, $languages) !== false; } + /** + * @param string $locale + * @return bool + */ + public function localeExists($locale) { + if ($locale === 'en') { //english is always available + return true; + } + + $locales = $this->findAvailableLocales(); + + $userLocale = array_filter($locales, function($value) use ($locale) { + return $locale === $value['code']; + }); + + return !empty($userLocale); + } + /** * @param string|null $app * @return string diff --git a/lib/private/Settings/Personal/locales.json b/lib/private/L10N/locales.json similarity index 100% rename from lib/private/Settings/Personal/locales.json rename to lib/private/L10N/locales.json diff --git a/lib/private/Settings/Personal/PersonalInfo.php b/lib/private/Settings/Personal/PersonalInfo.php index f6fb9d48d9..267be2878d 100644 --- a/lib/private/Settings/Personal/PersonalInfo.php +++ b/lib/private/Settings/Personal/PersonalInfo.php @@ -232,14 +232,13 @@ class PersonalInfo implements ISettings { $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage()); - $localeData = file_get_contents(__DIR__ . '/locales.json'); - $localeCodes = json_decode($localeData, true); + $localeCodes = $this->l10nFactory->findAvailableLocales(); $userLocale = array_filter($localeCodes, function($value) use ($userLocaleString) { return $userLocaleString === $value['code']; }); - if (count($userLocale) > 0) + if (!empty($userLocale)) { $userLocale = reset($userLocale); } @@ -248,14 +247,6 @@ class PersonalInfo implements ISettings { return 0 === strpos($localeCode['code'], $userLang); }); - /*$localesForLanguage = []; - - foreach (array_keys($localeCodes) as $localeCode) { - if (0 === strpos($localeCode, $userLang)) { - $localesForLanguage[] = $localeCode; - } - }*/ - return [ 'activelocaleLang' => $userLocaleString, 'activelocale' => $userLocale, diff --git a/lib/public/L10N/IFactory.php b/lib/public/L10N/IFactory.php index 9820082c72..08e4f83416 100644 --- a/lib/public/L10N/IFactory.php +++ b/lib/public/L10N/IFactory.php @@ -53,6 +53,12 @@ interface IFactory { */ public function findAvailableLanguages($app = null); + /** + * @return array an array of available + * @since 13.0.0 + */ + public function findAvailableLocales(); + /** * @param string|null $app App id or null for core * @param string $lang @@ -60,4 +66,20 @@ interface IFactory { * @since 9.0.0 */ public function languageExists($app, $lang); + + /** + * @param string $locale + * @return bool + * @since 13.0.0 + */ + public function localeExists($locale); + + /** + * Creates a function from the plural string + * + * @param string $string + * @return string Unique function name + * @since 9.0.0 + */ + public function createPluralFunction($string); } diff --git a/settings/templates/settings/personal/personal.info.php b/settings/templates/settings/personal/personal.info.php index 959650cf2f..20e346c867 100644 --- a/settings/templates/settings/personal/personal.info.php +++ b/settings/templates/settings/personal/personal.info.php @@ -381,6 +381,7 @@ vendor_style('jcrop/css/jquery.Jcrop'); +

@@ -406,3 +407,4 @@ vendor_style('jcrop/css/jquery.Jcrop'); +