From b5e0905cb40f7d91842653767ea7e2ae02e20bd8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 6 Aug 2021 11:08:19 +1200 Subject: [PATCH] Reconcile remaining fields between scheduled reminders and legacy tokens This adds the last fields that were in legacy tokens but not scheduled reminders and now the same code is deriving the list for each. I wound up UI testing rather than unit testing that custom fields are still advertised without this line as the test uses rollback & adding custom fields would break that. --- CRM/Contribute/Tokens.php | 9 +++++- CRM/Core/SelectValues.php | 9 +----- .../Contribute/ActionMapping/ByTypeTest.php | 31 ++++++++++++++++++- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index e0aa7b2054..ca1b952cc7 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -56,7 +56,7 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { * @return array */ protected function getExposedFields(): array { - return [ + $fields = [ 'contribution_page_id', 'source', 'id', @@ -75,7 +75,14 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { 'contribution_status_id', 'financial_type_id', 'payment_instrument_id', + 'cancel_reason', + 'amount_level', + 'check_number', ]; + if (CRM_Campaign_BAO_Campaign::isCampaignEnable()) { + $fields[] = 'campaign_id'; + } + return $fields; } /** diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index 94e6da9cdc..ca20c9d271 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -568,14 +568,7 @@ class CRM_Core_SelectValues { foreach ($processor->getAllTokens() as $token => $title) { $tokens['{contribution.' . $token . '}'] = $title; } - return array_merge($tokens, [ - '{contribution.cancel_reason}' => ts('Contribution Cancel Reason'), - '{contribution.amount_level}' => ts('Amount Level'), - '{contribution.check_number}' => ts('Check Number'), - '{contribution.campaign}' => ts('Contribution Campaign'), - // @todo - we shouldn't need to include custom fields here - - // remove, with test. - ], CRM_Utils_Token::getCustomFieldTokens('Contribution', TRUE)); + return $tokens; } /** diff --git a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php index aa0dca7030..d10f687bd7 100644 --- a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php +++ b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\Contribution; + /** * Class CRM_Contribute_ActionMapping_ByTypeTest * @group ActionSchedule @@ -151,6 +153,8 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr * Create a contribution record for Alice with type "Member Dues". */ public function addAliceDues(): void { + $this->enableCiviCampaign(); + $campaignID = $this->campaignCreate(); $this->ids['Contribution']['alice'] = $this->callAPISuccess('Contribution', 'create', [ 'contact_id' => $this->contacts['alice']['id'], 'receive_date' => date('Ymd', strtotime($this->targetDate)), @@ -164,6 +168,7 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr // Having a cancel date is a bit artificial here but we can test it.... 'cancel_date' => '2021-08-09', 'contribution_status_id' => 1, + 'campaign_id' => $campaignID, 'soft_credit' => [ '1' => [ 'contact_id' => $this->contacts['carol']['id'], @@ -281,7 +286,10 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr non_deductible_amount = {contribution.non_deductible_amount} total_amount = {contribution.total_amount} net_amount = {contribution.net_amount} - fee_amount = {contribution.fee_amount}'; + fee_amount = {contribution.fee_amount} + campaign_id = {contribution.campaign_id} + campaign name = {contribution.campaign_id:name} + campaign label = {contribution.campaign_id:label}'; $this->schedule->save(); $this->callAPISuccess('job', 'send_reminder', []); @@ -305,6 +313,9 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr 'total_amount = € 100.00', 'net_amount = € 95.00', 'fee_amount = € 5.00', + 'campaign_id = 1', + 'campaign name = big_campaign', + 'campaign label = Campaign', ]; $this->mut->checkMailLog($expected); @@ -337,6 +348,9 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr 'total_amount = € 100.00', 'net_amount = € 95.00', 'fee_amount = € 5.00', + 'campaign_id = 1', + 'campaign name = big_campaign', + 'campaign label = Campaign', ]; foreach ($expected as $string) { $this->assertStringContainsString($string, $contributionDetails[$this->contacts['alice']['id']]['html']); @@ -354,6 +368,21 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr 'contribution_status_id:label', ]; $processor = new CRM_Contribute_Tokens(); + $legacyTokens = []; + $realLegacyTokens = []; + foreach (CRM_Core_SelectValues::contributionTokens() as $token => $label) { + $legacyTokens[substr($token, 14, -1)] = $label; + if (strpos($token, ':') === FALSE) { + $realLegacyTokens[substr($token, 14, -1)] = $label; + } + } + $fields = (array) Contribution::getFields()->addSelect('name', 'title')->execute()->indexBy('name'); + $allFields = []; + foreach ($fields as $field) { + $allFields[$field['name']] = $field['title']; + } + // $this->assertEquals($realLegacyTokens, $allFields); + $this->assertEquals($legacyTokens, $processor->tokenNames); foreach ($tokens as $token) { $this->assertEquals(CRM_Core_SelectValues::contributionTokens()['{contribution.' . $token . '}'], $processor->tokenNames[$token]); } -- 2.25.1