From b8fe55cd99125ab6a9ece855b2fedb1119c9e707 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 30 Dec 2021 10:47:15 +1300 Subject: [PATCH] Always assign taxTerm to templates 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 | 6 ++++-- Civi/WorkflowMessage/GenericWorkflowMessage.php | 11 +++++++++++ tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php | 9 ++++----- tests/phpunit/CRM/Utils/TokenConsistencyTest.php | 1 + tests/phpunit/api/v3/ContributionTest.php | 4 +++- .../contribution_online_receipt_html.tpl | 4 ---- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/CRM/Core/DomainTokens.php b/CRM/Core/DomainTokens.php index fe7384270b..ca8372146f 100644 --- a/CRM/Core/DomainTokens.php +++ b/CRM/Core/DomainTokens.php @@ -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); diff --git a/Civi/WorkflowMessage/GenericWorkflowMessage.php b/Civi/WorkflowMessage/GenericWorkflowMessage.php index 05ba1cc7ee..2d828f7a16 100644 --- a/Civi/WorkflowMessage/GenericWorkflowMessage.php +++ b/Civi/WorkflowMessage/GenericWorkflowMessage.php @@ -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'; + } + } diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 207fde7e67..a6ffa82a0f 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -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('

Hello testSendTemplate_RenderMode_OpenTemplate Abba Baab!

', $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(); diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index a83fbc9477..c72c6b8733 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -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)', ]; } diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 113083f594..d9635294b2 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -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(); diff --git a/tests/templates/message_templates/contribution_online_receipt_html.tpl b/tests/templates/message_templates/contribution_online_receipt_html.tpl index eba77796a5..4ced13c285 100644 --- a/tests/templates/message_templates/contribution_online_receipt_html.tpl +++ b/tests/templates/message_templates/contribution_online_receipt_html.tpl @@ -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} @@ -82,10 +80,8 @@ {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} -- 2.25.1