From 7ddd4af778ac32fdb819a127eb3e6f1f51217873 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 3 Aug 2021 09:40:32 +1200 Subject: [PATCH] Reconcile and upgrade tokens for payment instrument, financial type This switches the last 2 'alias' tokens from Contribute_Tokens to the preferred format, adds tests & upgrade and switches the advertised tokens. I tested & the token removed from the sent letter collection still works Next step is to analyse the remaining differences between the 2 sets of tokens. My suspicion is that we have enough metadata that we use in search kit that we don't need a hard-coded white list of tokens in 2 different places & can just expose 'all the fields' that are not acl or otherwise limited --- CRM/Contribute/Tokens.php | 33 +++++------------- CRM/Core/SelectValues.php | 17 ++++------ CRM/Upgrade/Incremental/php/FiveFortyOne.php | 6 ++++ .../Contribute/ActionMapping/ByTypeTest.php | 34 +++++++++++++++++-- 4 files changed, 53 insertions(+), 37 deletions(-) diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index 3fe10f3ede..77207fa5f9 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -67,18 +67,8 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber { 'thankyou_date', 'tax_amount', 'contribution_status_id', - ]; - } - - /** - * Get alias tokens. - * - * @return array - */ - protected function getAliasTokens(): array { - return [ - 'payment_instrument' => 'payment_instrument_id', - 'type' => 'financial_type_id', + 'financial_type_id', + 'payment_instrument_id', ]; } @@ -93,8 +83,12 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber { * * @return string[] */ - protected function getBasicTokens(): array { - return ['contribution_status_id' => ts('Contribution Status ID')]; + public function getBasicTokens(): array { + $return = []; + foreach (['contribution_status_id', 'payment_instrument_id', 'financial_type_id'] as $fieldName) { + $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title']; + } + return $return; } /** @@ -123,10 +117,6 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber { CRM_Utils_Array::collect('title', $this->getFieldMetadata()), $this->getPassthruTokens() ); - $tokens['id'] = ts('Contribution ID'); - $tokens['payment_instrument'] = ts('Payment Instrument'); - $tokens['source'] = ts('Contribution Source'); - $tokens['type'] = ts('Financial Type'); $tokens = array_merge($tokens, $this->getPseudoTokens(), CRM_Utils_Token::getCustomFieldTokens('Contribution')); parent::__construct('contribution', $tokens); } @@ -161,9 +151,6 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber { $split = explode(':', $token); $e->query->select("e." . $fields[$split[0]]['name'] . " AS contrib_{$split[0]}"); } - foreach ($this->getAliasTokens() as $alias => $orig) { - $e->query->select('e.' . $fields[$orig]['name'] . " AS contrib_{$alias}"); - } } /** @@ -173,14 +160,10 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber { $actionSearchResult = $row->context['actionSearchResult']; $fieldValue = $actionSearchResult->{"contrib_$field"} ?? NULL; - $aliasTokens = $this->getAliasTokens(); if (in_array($field, ['total_amount', 'fee_amount', 'net_amount'])) { return $row->format('text/plain')->tokens($entity, $field, \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency)); } - if (isset($aliasTokens[$field])) { - $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue); - } elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { $row->customToken($entity, $cfID, $actionSearchResult->entity_id); } diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 348170ee90..28f90a86e7 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -562,15 +562,19 @@ class CRM_Core_SelectValues { * * @return array */ - public static function contributionTokens() { - return array_merge([ + public static function contributionTokens(): array { + $tokens = []; + $processor = new CRM_Contribute_Tokens(); + foreach (array_merge($processor->getPseudoTokens(), $processor->getBasicTokens()) as $token => $title) { + $tokens['{contribution.' . $token . '}'] = $title; + } + return array_merge($tokens, [ '{contribution.id}' => ts('Contribution ID'), '{contribution.total_amount}' => ts('Total Amount'), '{contribution.fee_amount}' => ts('Fee Amount'), '{contribution.net_amount}' => ts('Net Amount'), '{contribution.non_deductible_amount}' => ts('Non-deductible Amount'), '{contribution.receive_date}' => ts('Contribution Date Received'), - '{contribution.payment_instrument}' => ts('Payment Method'), '{contribution.trxn_id}' => ts('Transaction ID'), '{contribution.invoice_id}' => ts('Invoice ID'), '{contribution.currency}' => ts('Currency'), @@ -580,13 +584,6 @@ class CRM_Core_SelectValues { '{contribution.thankyou_date}' => ts('Thank You Date'), '{contribution.contribution_source}' => ts('Contribution Source'), '{contribution.amount_level}' => ts('Amount Level'), - //'{contribution.contribution_recur_id}' => ts('Contribution Recurring ID'), - //'{contribution.honor_contact_id}' => ts('Honor Contact ID'), - '{contribution.contribution_status_id}' => ts('Contribution Status ID'), - '{contribution.contribution_status_id:label}' => ts('Contribution Status'), - '{contribution.contribution_status_id:name}' => ts('Machine name') . ': ' . ts('Contribution Status'), - //'{contribution.honor_type_id}' => ts('Honor Type ID'), - //'{contribution.address_id}' => ts('Address ID'), '{contribution.check_number}' => ts('Check Number'), '{contribution.campaign}' => ts('Contribution Campaign'), ], CRM_Utils_Token::getCustomFieldTokens('Contribution', TRUE)); diff --git a/CRM/Upgrade/Incremental/php/FiveFortyOne.php b/CRM/Upgrade/Incremental/php/FiveFortyOne.php index 700735a430..c1140ba635 100644 --- a/CRM/Upgrade/Incremental/php/FiveFortyOne.php +++ b/CRM/Upgrade/Incremental/php/FiveFortyOne.php @@ -80,6 +80,12 @@ class CRM_Upgrade_Incremental_php_FiveFortyOne extends CRM_Upgrade_Incremental_B $this->addTask('Replace contribution source token in action schedule', 'updateActionScheduleToken', 'contribution.contribution_source', 'contribution.source', $rev ); + $this->addTask('Replace contribution type token in action schedule', + 'updateActionScheduleToken', 'contribution.type', 'contribution.financial_type_id:label', $rev + ); + $this->addTask('Replace contribution payment instrument token in action schedule', + 'updateActionScheduleToken', 'contribution.payment_instrument', 'contribution.payment_instrument_id:label', $rev + ); } /** diff --git a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php index a512abec1b..8d4b5e6225 100644 --- a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php +++ b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php @@ -269,7 +269,14 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr id {contribution.id} contribution_id {contribution.contribution_id} - not valid for action schedule cancel date {contribution.cancel_date} - source {contribution.source}'; + source {contribution.source} + financial type id = {contribution.financial_type_id} + financial type name = {contribution.financial_type_id:name} + financial type label = {contribution.financial_type_id:label} + payment instrument id = {contribution.payment_instrument_id} + payment instrument name = {contribution.payment_instrument_id:name} + payment instrument label = {contribution.payment_instrument_id:label}'; + $this->schedule->save(); $this->callAPISuccess('job', 'send_reminder', []); $expected = [ @@ -282,6 +289,12 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr 'id - not valid for action schedule', 'cancel date August 9th, 2021 12:00 AM', 'source SSF', + 'financial type id = 1', + 'financial type name = Donation', + 'financial type label = Donation', + 'payment instrument id = 4', + 'payment instrument name = Check', + 'payment instrument label = Check', ]; $this->mut->checkMailLog($expected); @@ -302,11 +315,28 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr 'contribution status id = 1', 'id ' . $this->ids['Contribution']['alice'], 'contribution_id ' . $this->ids['Contribution']['alice'], + 'financial type id = 1', + 'financial type name = Donation', + 'financial type label = Donation', + 'payment instrument id = 4', + 'payment instrument name = Check', + 'payment instrument label = Check', ]; foreach ($expected as $string) { $this->assertStringContainsString($string, $contributionDetails[$this->contacts['alice']['id']]['html']); } - $tokens = ['id', 'contribution_status_id', 'contribution_status_id:name', 'contribution_status_id:label']; + $tokens = [ + 'id', + 'payment_instrument_id', + 'payment_instrument_id:name', + 'payment_instrument_id:label', + 'financial_type_id', + 'financial_type_id:name', + 'financial_type_id:label', + 'contribution_status_id', + 'contribution_status_id:name', + 'contribution_status_id:label', + ]; $processor = new CRM_Contribute_Tokens(); foreach ($tokens as $token) { $this->assertEquals(CRM_Core_SelectValues::contributionTokens()['{contribution.' . $token . '}'], $processor->tokenNames[$token]); -- 2.25.1