(REF) Move test for `$translationMode` to its own class
authorTim Otten <totten@civicrm.org>
Mon, 8 Aug 2022 06:14:39 +0000 (23:14 -0700)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 1 Sep 2022 06:51:02 +0000 (18:51 +1200)
The functionality is bigger than `MessageTemplate`, and the test is fairly long.

tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php
tests/phpunit/api/v4/Options/TranslationModeTest.php [new file with mode: 0644]

index 431c4710132237ebbea6eb7520d70cafbcdeae9f..7b87397f885ddf63752f7f5b4dd5a17aab8b1659 100644 (file)
@@ -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 (file)
index 0000000..6cf7c19
--- /dev/null
@@ -0,0 +1,121 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+
+namespace api\v4\Options;
+
+use api\v4\Api4TestBase;
+use Civi\Api4\MessageTemplate;
+use Civi\Api4\Translation;
+
+/**
+ * Tests for the option `$apiRequest->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']);
+  }
+
+}