Skip to content

Commit

Permalink
Merge pull request #24291 from totten/master-ml-test
Browse files Browse the repository at this point in the history
(REF) Extract LocaleTestTrait. Tighten up multilingual testing.
  • Loading branch information
demeritcowboy authored Aug 17, 2022
2 parents 99715d5 + 6c1c896 commit a8c80a1
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 88 deletions.
87 changes: 87 additions & 0 deletions Civi/Test/LocaleTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Civi\Test;

/**
* Define helpers for testing multiple locales.
*
* Ex: Multilingual test with try/finally
* try {
* $this->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'] = [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ class CRM_Contribute_Form_ContributionPageTranslationTest extends CiviUnitTestCa
public function setUp(): void {
parent::setUp();
$this->_financialTypeID = 1;
$this->enableMultilingual();
CRM_Core_I18n_Schema::addLocale('fr_FR', 'en_US');
$this->enableMultilingual(['en_US' => 'fr_FR']);
}

public function tearDown(): void {
global $dbLocale;
if ($dbLocale) {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
$this->disableMultilingual();
}
parent::tearDown();
}
Expand Down
10 changes: 1 addition & 9 deletions tests/phpunit/CRM/Core/CopyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public function testI18nEventCopy() {

$locales = ['en_US', 'fr_CA', 'nl_NL'];

$this->enableMultilingual();
CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
CRM_Core_I18n_Schema::addLocale('nl_NL', 'en_US');

$cleanup = $this->useMultilingual(['en_US' => ['fr_CA', 'nl_NL']]);
CRM_Core_I18n::singleton()->setLocale('en_US');

$event = $this->eventCreate();
Expand Down Expand Up @@ -150,11 +147,6 @@ public function testI18nEventCopy() {
// other fields
$this->compareLocalizedCopy($eventData, $eventCopy, $locParams, $identicalParams, $locSuffix);
}

// reset to en_US only
CRM_Core_I18n::singleton()->setLocale('en_US');
CRM_Core_I18n_Schema::makeSinglelingual('en_US');

}

protected function compareLocalizedCopy($source, $dest, $locParams, $identicalParams, $locSuffix) {
Expand Down
20 changes: 3 additions & 17 deletions tests/phpunit/CRM/Core/I18n/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ class CRM_Core_I18n_LocaleTest extends CiviUnitTestCase {
*
*/
public function testI18nLocaleChange() {
$this->enableMultilingual();
CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
$cleanup = $this->useMultilingual(['en_US' => 'fr_CA']);

CRM_Core_I18n::singleton()->setLocale('fr_CA');
$locale = CRM_Core_I18n::getLocale();

$this->assertEquals($locale, 'fr_CA');

CRM_Core_I18n::singleton()->setLocale('en_US');
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
Civi::$statics['CRM_Core_I18n']['singleton'] = [];
$this->assertEquals('fr_CA', $locale);
}

public function testUiLanguages() {
Expand Down Expand Up @@ -58,11 +52,7 @@ public function testUiLanguages() {
$result = CRM_Core_I18n::uiLanguages();
$this->assertTreeEquals($languages, $result);

$this->enableMultilingual();
// Add fr_CA in db
CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
// Make fr_CA 'available'
Civi::settings()->set('languageLimit', ['en_US' => 1, 'fr_CA' => 1]);
$cleanup = $this->useMultilingual(['en_US' => 'fr_CA']);

// Multilingual, codes
$result = CRM_Core_I18n::uiLanguages(TRUE);
Expand Down Expand Up @@ -94,10 +84,6 @@ public function testUiLanguages() {
\CRM_Core_I18n::singleton()->setLocale('fr_CA');
$this->assertEquals('Planifié', \CRM_Core_PseudoConstant::getLabel("CRM_Activity_BAO_Activity", "status_id", 1));
}

CRM_Core_I18n::singleton()->setLocale('en_US');
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
Civi::$statics['CRM_Core_I18n']['singleton'] = [];
}

/**
Expand Down
9 changes: 4 additions & 5 deletions tests/phpunit/CRM/Core/I18n/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setUp(): void {
}

public function tearDown(): void {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
$this->disableMultilingual();
parent::tearDown();
}

Expand All @@ -44,7 +44,7 @@ public function tearDown(): void {
* @throws \CRM_Core_Exception
*/
public function testI18nSchemaRewrite($table, $expectedRewrite) {
CRM_Core_I18n_Schema::makeMultilingual('en_US');
$this->enableMultilingual();
$domains = $this->callAPISuccess('Domain', 'get')['values'];
$this->assertGreaterThan(1, count($domains));
foreach ($domains as $domain) {
Expand Down Expand Up @@ -106,7 +106,7 @@ public function testI18nSchemaRewrite($table, $expectedRewrite) {
}

public function testSchemaBuild() {
CRM_Core_I18n_Schema::makeMultilingual('en_US');
$this->enableMultilingual();
$inUseCollation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
$testCreateTable = CRM_Core_DAO::executeQuery("show create table civicrm_price_set", [], TRUE, NULL, FALSE, FALSE);
while ($testCreateTable->fetch()) {
Expand All @@ -116,8 +116,7 @@ public function testSchemaBuild() {
}

public function testMultilingualCustomFieldCreation() {
$this->enableMultilingual();
CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');
$this->enableMultilingual(['en_US' => 'fr_CA']);
$id = $this->customGroupCreate()['id'];
$this->customFieldCreate(['custom_group_id' => $id]);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/CRM/Financial/BAO/FinancialTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setUp(): void {
public function tearDown(): void {
global $dbLocale;
if ($dbLocale) {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
$this->disableMultilingual();
}
$this->financialAccountDelete('Donations');
parent::tearDown();
Expand Down Expand Up @@ -112,11 +112,11 @@ public function multiLingual() {
* Check method del()
*
* @dataProvider multiLingual
* @group locale
*/
public function testDel($isMultiLingual) {
if ($isMultiLingual) {
$this->enableMultilingual();
CRM_Core_I18n_Schema::addLocale('fr_FR', 'en_US');
$this->enableMultilingual(['en_US' => 'fr_FR']);
}
$params = [
'name' => 'Donations',
Expand Down
25 changes: 6 additions & 19 deletions tests/phpunit/CRM/Logging/LoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@
/**
* Class CRM_Core_DAOTest
* @group headless
* @group locale
*/
class CRM_Logging_LoggingTest extends CiviUnitTestCase {

use CRMTraits_Custom_CustomDataTrait;

/**
* Has the db been set to multilingual.
*
* @var bool
*/
protected $isDBMultilingual = FALSE;

public function tearDown(): void {
Civi::settings()->set('logging', FALSE);
if ($this->isDBMultilingual) {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
global $dbLocale;
if ($dbLocale) {
$this->disableMultilingual();
}
$logging = new CRM_Logging_Schema();
$logging->dropAllLogTables();
Expand Down Expand Up @@ -64,7 +59,7 @@ public function ignoreSillyName(array &$logTableSpec): void {
* Test creating logging schema when database is in multilingual mode.
*/
public function testMultilingualLogging(): void {
$this->makeMultilingual();
$this->enableMultilingual();
Civi::settings()->set('logging', TRUE);
$value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
$this->assertNotNull($value, 'Logging not enabled successfully');
Expand All @@ -75,7 +70,7 @@ public function testMultilingualLogging(): void {
* Also test altering a multilingual table.
*/
public function testMultilingualAlterSchemaLogging(): void {
$this->makeMultilingual();
$this->enableMultilingual();
Civi::settings()->set('logging', TRUE);
$logging = new CRM_Logging_Schema();
$value = CRM_Core_DAO::singleValueQuery('SELECT id FROM log_civicrm_contact LIMIT 1', [], FALSE, FALSE);
Expand Down Expand Up @@ -113,12 +108,4 @@ public function testMultilingualAlterSchemaLogging(): void {
);
}

/**
* Convert the database to multilingual mode.
*/
protected function makeMultilingual(): void {
CRM_Core_I18n_Schema::makeMultilingual('en_US');
$this->isDBMultilingual = TRUE;
}

}
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Mailing/BAO/MailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CRM_Mailing_BAO_MailingTest extends CiviUnitTestCase {
public function tearDown(): void {
global $dbLocale;
if ($dbLocale) {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
$this->disableMultilingual();
}
parent::tearDown();
}
Expand Down
5 changes: 2 additions & 3 deletions tests/phpunit/CRM/Mailing/MailingSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function hook_alterMailParams(&$params, $context = NULL): void {
public function tearDown(): void {
global $dbLocale;
if ($dbLocale) {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
$this->disableMultilingual();
}
parent::tearDown();
$this->assertNotEmpty($this->counts['hook_alterMailParams']);
Expand Down Expand Up @@ -195,8 +195,7 @@ public function testGitLabIssue1108($isMultiLingual) {
// transaction still increments the AUTO_INCREMENT counter for the table.
// (If this behaviour ever changes we throw an exception.)
if ($isMultiLingual) {
$this->enableMultilingual();
CRM_Core_I18n_Schema::addLocale('fr_FR', 'en_US');
$cleanup = $this->useMultilingual(['en_US' => 'fr_FR']);
}
$max_group_id = CRM_Core_DAO::singleValueQuery("SELECT MAX(id) FROM civicrm_group");
$max_mailing_id = 0;
Expand Down
18 changes: 1 addition & 17 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -3425,23 +3426,6 @@ protected function getContributionObject($contributionID) {
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
*
Expand Down
13 changes: 2 additions & 11 deletions tests/phpunit/api/v3/MultilingualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,16 @@ protected function setUp(): void {
}

public function tearDown(): void {
CRM_Core_I18n_Schema::makeSinglelingual('en_US');
$this->disableMultilingual();
parent::tearDown();
}

/**
* @dataProvider versionThreeAndFour
*/
public function testOptionLanguage($version) {
$this->enableMultilingual();
$this->_apiversion = $version;

CRM_Core_I18n_Schema::addLocale('fr_CA', 'en_US');

$this->callAPISuccess('Setting', 'create', [
'languageLimit' => [
'en_US' => 1,
'fr_CA' => 1,
],
]);
$this->enableMultilingual(['en_US' => 'fr_CA']);

// Take a semi-random OptionGroup and test manually changing its label
// in one language, while making sure it stays the same in English.
Expand Down

0 comments on commit a8c80a1

Please sign in to comment.