From f61325efa16dc0f54e6fc2b82a8319e8af184eca Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sun, 7 Aug 2022 23:14:39 -0700 Subject: [PATCH] (REF) Move test for `$translationMode` to its own class The functionality is bigger than `MessageTemplate`, and the test is fairly long. --- .../CRM/Core/BAO/MessageTemplateTest.php | 85 ------------ .../api/v4/Options/TranslationModeTest.php | 121 ++++++++++++++++++ 2 files changed, 121 insertions(+), 85 deletions(-) create mode 100644 tests/phpunit/api/v4/Options/TranslationModeTest.php diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 431c471013..7b87397f88 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -3,7 +3,6 @@ use Civi\Api4\Address; use Civi\Api4\Contact; use Civi\Api4\MessageTemplate; -use Civi\Api4\Translation; use Civi\Token\TokenProcessor; /** @@ -202,56 +201,6 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { $this->assertStringContainsString('Case ID : 1234', $message); } - public function getTranslationSettings(): array { - $es = []; - $es['fr_FR-full'] = [ - ['partial_locales' => FALSE, 'uiLanguages' => ['en_US', 'fr_FR', 'fr_CA']], - ]; - $es['fr_FR-partial'] = [ - ['partial_locales' => TRUE, 'uiLanguages' => ['en_US']], - ]; - return $es; - } - - /** - * Test that translated strings are rendered for templates where they exist. - * - * @dataProvider getTranslationSettings - * @throws \API_Exception|\CRM_Core_Exception - */ - public function testGetTranslatedTemplate($translationSettings): void { - $cleanup = \CRM_Utils_AutoClean::swapSettings($translationSettings); - - $this->individualCreate(['preferred_language' => 'fr_FR']); - $this->contributionCreate(['contact_id' => $this->ids['Contact']['individual_0']]); - $this->addTranslation(); - - $messageTemplate = MessageTemplate::get() - ->addWhere('is_default', '=', 1) - ->addWhere('workflow_name', 'IN', ['contribution_online_receipt', 'contribution_offline_receipt']) - ->addSelect('id', 'msg_subject', 'msg_html', 'workflow_name') - ->setLanguage('fr_FR') - ->setTranslationMode('fuzzy') - ->execute()->indexBy('workflow_name'); - - $this->assertFrenchTranslationRetrieved($messageTemplate['contribution_online_receipt']); - - $this->assertStringContainsString('{ts}Contribution Receipt{/ts}', $messageTemplate['contribution_offline_receipt']['msg_subject']); - $this->assertStringContainsString('Below you will find a receipt', $messageTemplate['contribution_offline_receipt']['msg_html']); - $this->assertArrayNotHasKey('actual_language', $messageTemplate['contribution_offline_receipt']); - - $messageTemplate = MessageTemplate::get() - ->addWhere('is_default', '=', 1) - ->addWhere('workflow_name', 'IN', ['contribution_online_receipt', 'contribution_offline_receipt']) - ->addSelect('id', 'msg_subject', 'msg_html', 'workflow_name') - ->setLanguage('fr_CA') - ->setTranslationMode('fuzzy') - ->execute()->indexBy('workflow_name'); - - $this->assertFrenchTranslationRetrieved($messageTemplate['contribution_online_receipt']); - - } - /** * Test rendering of domain tokens. * @@ -971,38 +920,4 @@ t_stuff.favourite_emoticon | return $expected; } - /** - * @return mixed - * @throws \API_Exception - * @throws \Civi\API\Exception\UnauthorizedException - */ - private function addTranslation() { - $messageTemplateID = MessageTemplate::get() - ->addWhere('is_default', '=', 1) - ->addWhere('workflow_name', '=', 'contribution_online_receipt') - ->addSelect('id') - ->execute()->first()['id']; - - Translation::save()->setRecords([ - ['entity_field' => 'msg_subject', 'string' => 'Bonjour'], - ['entity_field' => 'msg_html', 'string' => 'Voila!'], - ['entity_field' => 'msg_text', 'string' => '{contribution.total_amount}'], - ])->setDefaults([ - 'entity_table' => 'civicrm_msg_template', - 'entity_id' => $messageTemplateID, - 'status_id:name' => 'active', - 'language' => 'fr_FR', - ])->execute(); - return $messageTemplateID; - } - - /** - * @param $contribution_online_receipt - */ - private function assertFrenchTranslationRetrieved($contribution_online_receipt): void { - $this->assertEquals('Bonjour', $contribution_online_receipt['msg_subject']); - $this->assertEquals('Voila!', $contribution_online_receipt['msg_html']); - $this->assertEquals('fr_FR', $contribution_online_receipt['actual_language']); - } - } diff --git a/tests/phpunit/api/v4/Options/TranslationModeTest.php b/tests/phpunit/api/v4/Options/TranslationModeTest.php new file mode 100644 index 0000000000..6cf7c19e97 --- /dev/null +++ b/tests/phpunit/api/v4/Options/TranslationModeTest.php @@ -0,0 +1,121 @@ +setTranslationMode(...)`. + * + * Broadly, these tests need to: + * - Make some example business records + * - Add translations for them + * - Read back the translations, with variations on the translation-mode. + * + * @group headless + */ +class TranslationModeTest extends Api4TestBase { + + public function getTranslationSettings(): array { + $es = []; + $es['fr_FR-full'] = [ + ['partial_locales' => FALSE, 'uiLanguages' => ['en_US', 'fr_FR', 'fr_CA']], + ]; + $es['fr_FR-partial'] = [ + ['partial_locales' => TRUE, 'uiLanguages' => ['en_US']], + ]; + return $es; + } + + /** + * Test that translated strings are rendered for templates where they exist. + * + * @dataProvider getTranslationSettings + * @throws \API_Exception|\CRM_Core_Exception + * @group locale + */ + public function testGetTranslatedTemplate($translationSettings): void { + $cleanup = \CRM_Utils_AutoClean::swapSettings($translationSettings); + + $cid = $this->createTestRecord('Contact', ['preferred_language' => 'fr_FR'])['id']; + $this->createTestRecord('Contribution', ['contact_id' => $cid]); + $this->addTranslation(); + + $messageTemplate = MessageTemplate::get() + ->addWhere('is_default', '=', 1) + ->addWhere('workflow_name', 'IN', ['contribution_online_receipt', 'contribution_offline_receipt']) + ->addSelect('id', 'msg_subject', 'msg_html', 'workflow_name') + ->setLanguage('fr_FR') + ->setTranslationMode('fuzzy') + ->execute()->indexBy('workflow_name'); + + $this->assertFrenchTranslationRetrieved($messageTemplate['contribution_online_receipt']); + + $this->assertStringContainsString('{ts}Contribution Receipt{/ts}', $messageTemplate['contribution_offline_receipt']['msg_subject']); + $this->assertStringContainsString('Below you will find a receipt', $messageTemplate['contribution_offline_receipt']['msg_html']); + $this->assertArrayNotHasKey('actual_language', $messageTemplate['contribution_offline_receipt']); + + $messageTemplate = MessageTemplate::get() + ->addWhere('is_default', '=', 1) + ->addWhere('workflow_name', 'IN', ['contribution_online_receipt', 'contribution_offline_receipt']) + ->addSelect('id', 'msg_subject', 'msg_html', 'workflow_name') + ->setLanguage('fr_CA') + ->setTranslationMode('fuzzy') + ->execute()->indexBy('workflow_name'); + + $this->assertFrenchTranslationRetrieved($messageTemplate['contribution_online_receipt']); + } + + /** + * @return mixed + * @throws \API_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + private function addTranslation() { + $messageTemplateID = MessageTemplate::get() + ->addWhere('is_default', '=', 1) + ->addWhere('workflow_name', '=', 'contribution_online_receipt') + ->addSelect('id') + ->execute()->first()['id']; + + Translation::save()->setRecords([ + ['entity_field' => 'msg_subject', 'string' => 'Bonjour'], + ['entity_field' => 'msg_html', 'string' => 'Voila!'], + ['entity_field' => 'msg_text', 'string' => '{contribution.total_amount}'], + ])->setDefaults([ + 'entity_table' => 'civicrm_msg_template', + 'entity_id' => $messageTemplateID, + 'status_id:name' => 'active', + 'language' => 'fr_FR', + ])->execute(); + return $messageTemplateID; + } + + /** + * @param $contribution_online_receipt + */ + private function assertFrenchTranslationRetrieved($contribution_online_receipt): void { + $this->assertEquals('Bonjour', $contribution_online_receipt['msg_subject']); + $this->assertEquals('Voila!', $contribution_online_receipt['msg_html']); + $this->assertEquals('fr_FR', $contribution_online_receipt['actual_language']); + } + +} -- 2.25.1