From cc493f5e8ac15a82d720de99b5f6b57573740812 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 21 Jun 2019 17:30:33 -0400 Subject: [PATCH] Multilingual test fix and cleanup --- CRM/Core/I18n.php | 6 ++--- Civi/API/Subscriber/I18nSubscriber.php | 30 ++++++----------------- tests/phpunit/api/v3/MultilingualTest.php | 12 ++++----- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index a128119ed4..f6ef3f4de1 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -230,11 +230,11 @@ class CRM_Core_I18n { } if ($enabled === NULL) { - $config = CRM_Core_Config::singleton(); + $languageLimit = Civi::settings()->get('languageLimit'); $enabled = []; - if (isset($config->languageLimit) and $config->languageLimit) { + if ($languageLimit) { foreach ($all as $code => $name) { - if (in_array($code, array_keys($config->languageLimit))) { + if (array_key_exists($code, $languageLimit)) { $enabled[$code] = $name; } } diff --git a/Civi/API/Subscriber/I18nSubscriber.php b/Civi/API/Subscriber/I18nSubscriber.php index 02c4d7e797..335bc823af 100644 --- a/Civi/API/Subscriber/I18nSubscriber.php +++ b/Civi/API/Subscriber/I18nSubscriber.php @@ -98,38 +98,22 @@ class I18nSubscriber implements EventSubscriberInterface { * Sets the tsLocale and dbLocale for multi-lingual sites. * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve() * to avoid regressions from refactoring. - * @param $lcMessagesRequest + * @param string $lcMessages * @param int $requestId * @throws \API_Exception */ - public function setLocale($lcMessagesRequest, $requestId) { - // We must validate whether the locale is valid, otherwise setting a bad - // dbLocale could probably lead to sql-injection. + public function setLocale($lcMessages, $requestId) { $domain = new \CRM_Core_DAO_Domain(); $domain->id = \CRM_Core_Config::domainID(); $domain->find(TRUE); - // are we in a multi-language setup? - $multiLang = $domain->locales ? TRUE : FALSE; - $lcMessages = NULL; - - // on multi-lang sites based on request and civicrm_uf_match - if ($multiLang) { - $config = \CRM_Core_Config::singleton(); - $languageLimit = []; - if (isset($config->languageLimit) and $config->languageLimit) { - $languageLimit = $config->languageLimit; - } - - if (in_array($lcMessagesRequest, array_keys($languageLimit))) { - $lcMessages = $lcMessagesRequest; + // Check if the site is multi-lingual + if ($domain->locales && $lcMessages) { + // Validate language, otherwise a bad dbLocale could probably lead to sql-injection. + if (!array_key_exists($lcMessages, \Civi::settings()->get('languageLimit'))) { + throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessages])); } - else { - throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessagesRequest])); - } - } - if ($lcMessages) { global $dbLocale; global $tsLocale; diff --git a/tests/phpunit/api/v3/MultilingualTest.php b/tests/phpunit/api/v3/MultilingualTest.php index 586ee7a65b..6e1fd156a0 100644 --- a/tests/phpunit/api/v3/MultilingualTest.php +++ b/tests/phpunit/api/v3/MultilingualTest.php @@ -59,12 +59,12 @@ class api_v3_MultilingualTest extends CiviUnitTestCase { CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US'); - $this->callAPISuccess('Setting', 'create', array( - 'languageLimit' => array( - 'en_US', - 'fr_CA', - ), - )); + $this->callAPISuccess('Setting', 'create', [ + 'languageLimit' => [ + 'en_US' => 1, + 'fr_CA' => 1, + ], + ]); // Take a semi-random OptionGroup and test manually changing its label // in one language, while making sure it stays the same in English. -- 2.25.1