Skip to content

Commit

Permalink
Block use of legacy style contribution tokens
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
eileenmcnaughton committed Sep 18, 2021
1 parent 6e51547 commit 245b4fe
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CRM/Contact/Form/Task/EmailTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions CRM/Core/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,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) {
Expand All @@ -1807,6 +1807,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}";
Expand Down
22 changes: 10 additions & 12 deletions tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -297,25 +296,25 @@ public function testAllContributionTokens(): void {
' . " \n" . ' </head>
<body>
<div id="crm-container">
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
cancel_date : 2019-12-30 00:00:00
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
Expand All @@ -337,29 +336,28 @@ 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',
'cancel_date' => '2019-12-30',
'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',
Expand All @@ -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']),

];
}

Expand Down Expand Up @@ -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'];
}
Expand Down

0 comments on commit 245b4fe

Please sign in to comment.