From ca8aeb145b92d636832ba588d3e69a857f6f5293 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 17 Aug 2022 00:05:28 -0700 Subject: [PATCH] Extract LocaleTestTrait from CiviUnitTestCase. Add more helpers. --- Civi/Test/LocaleTestTrait.php | 87 +++++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 18 +---- 2 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 Civi/Test/LocaleTestTrait.php diff --git a/Civi/Test/LocaleTestTrait.php b/Civi/Test/LocaleTestTrait.php new file mode 100644 index 0000000000..7689a63d61 --- /dev/null +++ b/Civi/Test/LocaleTestTrait.php @@ -0,0 +1,87 @@ +enableMultilingual(['en_US' => 'fr_CA']); + * $this->assert(...); + * } finally { + * $this->disbleMultilingual(); + * } + * + * Ex: Multilingual with auto-clean + * $cleanup = $this->useMultilingual(['en_US' => 'fr_CA']); + */ +trait LocaleTestTrait { + + /** + * @var string + */ + private static $defaultSystemLocale = 'en_US'; + + /** + * Temporarily use multilingual. + * + * @param array $addLocales + * A list of new locales to setup. + * A locale is initialized by copying from an existing locale. + * + * Ex: Copy from en_US to fr_CA + * ['en_US' => 'fr_CA'] + * Ex: Copy from en_US to fr_CA and de_DE + * ['en_US' => ['fr_CA', 'de_DE]] + * @return \CRM_Utils_AutoClean + * A reference to the temporary configuration. Once removed, the system will revert to single language. + */ + public function useMultilingual(array $addLocales): \CRM_Utils_AutoClean { + $this->enableMultilingual($addLocales); + return \CRM_Utils_AutoClean::with([$this, 'disableMultilingual']); + } + + /** + * Enable multilingual. + * + * @param array|null $addLocales + * A list of new locales to setup. + * A locale is initialized by copying from an existing locale. + * + * Ex: Copy from en_US to fr_CA + * ['en_US' => 'fr_CA'] + * Ex: Copy from en_US to fr_CA and de_DE + * ['en_US' => ['fr_CA', 'de_DE]] + */ + public function enableMultilingual(?array $addLocales = NULL): void { + $this->callAPISuccess('Setting', 'create', [ + 'lcMessages' => static::$defaultSystemLocale, + 'languageLimit' => [ + static::$defaultSystemLocale => 1, + ], + ]); + + \CRM_Core_I18n_Schema::makeMultilingual(static::$defaultSystemLocale); + + global $dbLocale; + $dbLocale = '_' . static::$defaultSystemLocale; + + if ($addLocales !== NULL) { + foreach ($addLocales as $fromLocale => $toLocales) { + foreach ((array) $toLocales as $toLocale) { + \CRM_Core_I18n_Schema::addLocale($toLocale, $fromLocale); + } + \Civi::settings()->set('languageLimit', \Civi::settings()->get('languageLimit') + [$toLocale => '1']); + } + } + } + + public function disableMultilingual(): void { + \CRM_Core_I18n::singleton()->setLocale(static::$defaultSystemLocale); + \CRM_Core_I18n_Schema::makeSinglelingual(static::$defaultSystemLocale); + \Civi::settings()->revert('languageLimit'); + \Civi::$statics['CRM_Core_I18n']['singleton'] = []; + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index b833c72531..4025ec69bb 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -74,6 +74,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { use \Civi\Test\DbTestTrait; use \Civi\Test\ContactTestTrait; use \Civi\Test\MailingTestTrait; + use \Civi\Test\LocaleTestTrait; /** * Database has been initialized. @@ -3425,23 +3426,6 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { return $contributionObj; } - /** - * Enable multilingual. - */ - public function enableMultilingual() { - $this->callAPISuccess('Setting', 'create', [ - 'lcMessages' => 'en_US', - 'languageLimit' => [ - 'en_US' => 1, - ], - ]); - - CRM_Core_I18n_Schema::makeMultilingual('en_US'); - - global $dbLocale; - $dbLocale = '_en_US'; - } - /** * Setup or clean up SMS tests * -- 2.25.1