Always assign taxTerm to templates
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 29 Dec 2021 21:47:15 +0000 (10:47 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 2 Jan 2022 22:14:59 +0000 (11:14 +1300)
I do wonder about making this a domain token - I go back & forth a bit but as
a domain token it would be available in non-smarty contexts too & could be exported

CRM/Core/DomainTokens.php
Civi/WorkflowMessage/GenericWorkflowMessage.php
tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php
tests/phpunit/api/v3/ContributionTest.php
tests/templates/message_templates/contribution_online_receipt_html.tpl

index fe7384270bef950898ba84e09d58867a5b837cc9..ca8372146f299639bcc60dd54df428a30c085a85 100644 (file)
@@ -48,6 +48,7 @@ class CRM_Core_DomainTokens extends AbstractTokenSubscriber {
       'id' => ts('Domain ID'),
       'description' => ts('Domain Description'),
       'now' => ts('Current time/date'),
+      'tax_term' => ts('Sales tax term (e.g VAT)'),
     ];
   }
 
@@ -92,9 +93,9 @@ class CRM_Core_DomainTokens extends AbstractTokenSubscriber {
         $domain->find(TRUE);
       }
       $tokens = [
-        'name' => $domain->name,
+        'name' => $domain->name ?? '',
         'id' => $domain->id,
-        'description' => $domain->description,
+        'description' => $domain->description ?? '',
       ];
       $loc = $domain->getLocationValues();
       if ($html) {
@@ -107,6 +108,7 @@ class CRM_Core_DomainTokens extends AbstractTokenSubscriber {
       $email = reset($loc['email']);
       $tokens['phone'] = $phone['phone'] ?? '';
       $tokens['email'] = $email['email'] ?? '';
+      $tokens['tax_term'] = (string) Civi::settings()->get('tax_term');
       Civi::cache('metadata')->set($cacheKey, $tokens);
     }
     return Civi::cache('metadata')->get($cacheKey);
index 05ba1cc7eeabcc03cac3443add7785dba55e4532..2d828f7a1634117e9aa18d68f46461f89829892f 100644 (file)
@@ -100,4 +100,15 @@ class GenericWorkflowMessage implements WorkflowMessageInterface {
     }
   }
 
+  /**
+   * Define tokens to be exported as smarty values.
+   *
+   * @param array $export
+   */
+  protected function exportExtraTokenContext(array &$export): void {
+    // Tax term is exposed at the generic level as so many templates use it
+    // (e.g. Membership, participant, pledge as well as contributions).
+    $export['smartyTokenAlias']['taxTerm'] = 'domain.tax_term';
+  }
+
 }
index 207fde7e679f0eff614f99d2826892fc6daa47e9..a6ffa82a0fbd7ded1ae77a77099ef496c5ca9038 100644 (file)
@@ -15,15 +15,14 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase {
 
   /**
    * Post test cleanup.
-   *
-   * @throws \CRM_Core_Exception
    */
   public function tearDown():void {
     $this->quickCleanup(['civicrm_address', 'civicrm_phone', 'civicrm_im', 'civicrm_website', 'civicrm_openid', 'civicrm_email'], TRUE);
     parent::tearDown();
+    Civi::cache('metadata')->clear();
   }
 
-  public function testRenderTemplate() {
+  public function testRenderTemplate(): void {
     $contactId = $this->individualCreate([
       'first_name' => 'Abba',
       'last_name' => 'Baab',
@@ -107,7 +106,7 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase {
     $this->assertStringContainsString('<p>Hello testSendTemplate_RenderMode_OpenTemplate Abba Baab!</p>', $messageHtml);
   }
 
-  public function testSendTemplate_RenderMode_DefaultTpl() {
+  public function testSendTemplate_RenderMode_DefaultTpl(): void {
     CRM_Core_Transaction::create(TRUE)->run(function(CRM_Core_Transaction $tx) {
       $tx->rollback();
 
@@ -144,7 +143,7 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase {
     });
   }
 
-  public function testSendTemplate_RenderMode_TokenContext() {
+  public function testSendTemplateRenderModeTokenContext(): void {
     CRM_Core_Transaction::create(TRUE)->run(function(CRM_Core_Transaction $tx) {
       $tx->rollback();
 
index a83fbc9477dc1198d8f91188790a440ff52318e4..c72c6b8733b803e76637471565568ed4088e66c4 100644 (file)
@@ -769,6 +769,7 @@ December 21st, 2007
       '{domain.id}' => ts('Domain ID'),
       '{domain.description}' => ts('Domain Description'),
       '{domain.now}' => 'Current time/date',
+      '{domain.tax_term}' => 'Sales tax term (e.g VAT)',
     ];
   }
 
index 113083f59454167706bd90ac971881879a7df279..d9635294b2d8c2bcba64aed9349b6e7dfe54616d 100644 (file)
@@ -2052,8 +2052,9 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * Note that we are creating a logged in user because email goes out from
    * that person
    */
-  public function testCompleteTransaction() {
+  public function testCompleteTransaction(): void {
     $mut = new CiviMailUtils($this, TRUE);
+    Civi::settings()->set('tax_term', 'GST');
     $this->swapMessageTemplateForTestTemplate();
     $this->createLoggedInUser();
     $params = array_merge($this->_params, ['contribution_status_id' => 2]);
@@ -2074,6 +2075,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       "receipt_date:::\n",
       'title:::Contribution',
       'contributionStatus:::Completed',
+      'taxTerm:::GST',
     ]);
     $mut->stop();
     $this->revertTemplateToReservedTemplate();
index eba77796a5d3efe22be235ef0701d0593832bb58..4ced13c285b70aeec63c427add31a516938d79ea 100644 (file)
@@ -62,9 +62,7 @@
   {if !empty($priceset)}
   priceset:::{$priceset}
   {/if}
-  {if !empty($taxTerm)}
   taxTerm:::{$taxTerm}
-  {/if}
   {if !empty($pcpBlock)}
   pcpBlock:::{$pcpBlock}
   pcp_display_in_roll:::{$pcp_display_in_roll}
   {if !empty($isBillingAddressRequiredForPayLater)}
   isBillingAddressRequiredForPayLater:::{$isBillingAddressRequiredForPayLater}
   {/if}
-  {if !empty($billingName)}
   billingName:::{$billingName}
   address:::{$address}
-  {/if}
   {if !empty($credit_card_type)}
   credit_card_type:::{$credit_card_type}
   credit_card_number:::{$credit_card_number}