From ec20b7801c212113c8cae6be801554a17d962a77 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 18 Sep 2021 18:09:30 +1200 Subject: [PATCH] Block use of legacy style contribution tokens ReplaceContributionTokens is only used from the pdf letter and email tasks. This puts them in the validation rule to prevent form submission with them and switches the test over to the preferred / advertised tokens Docs https://lab.civicrm.org/-/ide/project/documentation/docs/sysadmin/tree/case/-/docs/upgrade/version-specific.md/ --- CRM/Contact/Form/Task/EmailTrait.php | 4 ++++ CRM/Core/Form/Task/PDFLetterCommon.php | 4 ++++ CRM/Utils/Token.php | 6 ++++- .../Form/Task/PDFLetterCommonTest.php | 22 +++++++++---------- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/CRM/Contact/Form/Task/EmailTrait.php b/CRM/Contact/Form/Task/EmailTrait.php index 4bfa01b407fe..91041e3f8189 100644 --- a/CRM/Contact/Form/Task/EmailTrait.php +++ b/CRM/Contact/Form/Task/EmailTrait.php @@ -692,6 +692,10 @@ public static function deprecatedTokensFormRule(array $fields) { $deprecatedTokens = [ '{case.status_id}' => '{case.status_id:label}', '{case.case_type_id}' => '{case.case_type_id:label}', + '{contribution.campaign}' => '{contribution.campaign_id:label}', + '{contribution.payment_instrument}' => '{contribution.payment_instrument_id:label}', + '{contribution.contribution_id}' => '{contribution.id}', + '{contribution.contribution_source}' => '{contribution.source}', ]; $tokenErrors = []; foreach ($deprecatedTokens as $token => $replacement) { diff --git a/CRM/Core/Form/Task/PDFLetterCommon.php b/CRM/Core/Form/Task/PDFLetterCommon.php index c98886324b72..f267a10d8569 100644 --- a/CRM/Core/Form/Task/PDFLetterCommon.php +++ b/CRM/Core/Form/Task/PDFLetterCommon.php @@ -209,6 +209,10 @@ public static function formRule($fields, $files, $self) { '{case.case_type_id}' => '{case.case_type_id:label}', '{membership.status}' => '{membership.status_id:label}', '{membership.type}' => '{membership.membership_type_id:label}', + '{contribution.campaign}' => '{contribution.campaign_id:label}', + '{contribution.payment_instrument}' => '{contribution.payment_instrument_id:label}', + '{contribution.contribution_id}' => '{contribution.id}', + '{contribution.contribution_source}' => '{contribution.source}', ]; $tokenErrors = []; foreach ($deprecatedTokens as $token => $replacement) { diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index c2ec7615ab71..7ff8587b36b0 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1777,7 +1777,7 @@ public static function getMembershipTokenReplacement($entity, $token, $membershi * @return mixed|string * @throws \CRM_Core_Exception */ - public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) { + public static function getContributionTokenReplacement($token, $contribution, $html = FALSE, $escapeSmarty = FALSE) { self::_buildContributionTokens(); switch ($token) { @@ -1798,6 +1798,10 @@ public static function getContributionTokenReplacement($token, &$contribution, $ $value = CRM_Utils_Date::customFormat($value, $config->dateformatDatetime); break; + case 'source': + $value = CRM_Utils_Array::retrieveValueRecursive($contribution, 'contribution_source'); + break; + default: if (!in_array($token, self::$_tokens['contribution'])) { $value = "{contribution.$token}"; diff --git a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php index 2c90ca30dcec..859462228e1d 100644 --- a/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php @@ -265,7 +265,6 @@ public function testNoContributionTokens(): void { * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception - * @throws \Civi\API\Exception\UnauthorizedException */ public function testAllContributionTokens(): void { $this->createLoggedInUser(); @@ -281,7 +280,7 @@ public function testAllContributionTokens(): void { } /* @var $form CRM_Contribute_Form_Task_PDFLetter */ $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); - $form->setContributionIds([$this->createContribution(array_merge(['campaign_id' => $tokens['campaign']], $tokens))]); + $form->setContributionIds([$this->createContribution(array_merge(['campaign_id' => $tokens['campaign_id:label']], $tokens))]); try { $form->postProcess(); } @@ -297,13 +296,13 @@ public function testAllContributionTokens(): void { ' . " \n" . '
-contribution_id : 1 +id : 1 total_amount : € 9,999.99 fee_amount : € 1,111.11 net_amount : € 7,777.78 non_deductible_amount : € 2,222.22 receive_date : July 20th, 2018 12:00 AM -payment_instrument : Check +payment_instrument_id:label : Check trxn_id : 1234 invoice_id : 568 currency : EUR @@ -311,11 +310,11 @@ public function testAllContributionTokens(): void { cancel_reason : Contribution Cancel Reason receipt_date : October 30th, 2019 12:00 AM thankyou_date : 2019-11-30 00:00:00 -contribution_source : Contribution Source +source : Contribution Source amount_level : Amount Level contribution_status_id : 2 check_number : 6789 -campaign : Big one +campaign_id:label : Big one ' . $this->getCustomFieldName('text') . ' : Bobsled ' . $this->getCustomFieldName('select_string') . ' : Red ' . $this->getCustomFieldName('select_date') . ' : 01/20/2021 12:00AM @@ -337,17 +336,16 @@ public function testAllContributionTokens(): void { * Get all the tokens available to contributions. * * @return array - * @throws \CiviCRM_API3_Exception */ public function getAllContributionTokens(): array { return [ - 'contribution_id' => '', + 'id' => '', 'total_amount' => '9999.99', 'fee_amount' => '1111.11', 'net_amount' => '7777.78', 'non_deductible_amount' => '2222.22', 'receive_date' => '2018-07-20', - 'payment_instrument' => 'Check', + 'payment_instrument_id:label' => 'Check', 'trxn_id' => '1234', 'invoice_id' => '568', 'currency' => 'EUR', @@ -355,11 +353,11 @@ public function getAllContributionTokens(): array { 'cancel_reason' => 'Contribution Cancel Reason', 'receipt_date' => '2019-10-30', 'thankyou_date' => '2019-11-30', - 'contribution_source' => 'Contribution Source', + 'source' => 'Contribution Source', 'amount_level' => 'Amount Level', 'contribution_status_id' => 'Pending', 'check_number' => '6789', - 'campaign' => 'Big one', + 'campaign_id:label' => 'Big one', $this->getCustomFieldName('text') => 'Bobsled', $this->getCustomFieldName('select_string') => 'R', $this->getCustomFieldName('select_date') => '2021-01-20', @@ -373,7 +371,6 @@ public function getAllContributionTokens(): array { $this->getCustomFieldName('boolean') => TRUE, $this->getCustomFieldName('checkbox') => 'P', $this->getCustomFieldName('contact_reference') => $this->individualCreate(['first_name' => 'Spider', 'last_name' => 'Man']), - ]; } @@ -675,6 +672,7 @@ protected function createContribution(array $contributionParams = []) { 'contact_id' => $this->individualCreate(), 'total_amount' => 100, 'financial_type_id' => 'Donation', + 'source' => 'Contribution Source', ], $contributionParams); return $this->callAPISuccess('Contribution', 'create', $contributionParams)['id']; }