From bd6b5299296f34e7908af33bdf2e25de61bbb060 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 13 Aug 2021 10:53:44 +1200 Subject: [PATCH] dev/core#2745 Expose all field to token processor for contribution This just excludes contact id - see https://lab.civicrm.org/dev/core/-/issues/2745 for extra list --- CRM/Contribute/Tokens.php | 65 ------------------- CRM/Core/EntityTokens.php | 65 +++++++++++++++++++ .../Contribute/ActionMapping/ByTypeTest.php | 5 +- .../CRM/Contribute/BAO/ContributionTest.php | 3 +- 4 files changed, 71 insertions(+), 67 deletions(-) diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index ca1b952cc7..6a8c7754f9 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -20,13 +20,6 @@ */ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { - /** - * @return string - */ - protected function getEntityName(): string { - return 'contribution'; - } - /** * @return string */ @@ -46,62 +39,4 @@ class CRM_Contribute_Tokens extends CRM_Core_EntityTokens { return 'Contribution'; } - /** - * Get a list of tokens for the entity for which access is permitted to. - * - * This list is historical and we need to question whether we - * should filter out any fields (other than those fields, like api_key - * on the contact entity) with permissions defined. - * - * @return array - */ - protected function getExposedFields(): array { - $fields = [ - 'contribution_page_id', - 'source', - 'id', - 'receive_date', - 'total_amount', - 'fee_amount', - 'net_amount', - 'non_deductible_amount', - 'trxn_id', - 'invoice_id', - 'currency', - 'cancel_date', - 'receipt_date', - 'thankyou_date', - 'tax_amount', - '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; - } - - /** - * Get tokens supporting the syntax we are migrating to. - * - * In general these are tokens that were not previously supported - * so we can add them in the preferred way or that we have - * undertaken some, as yet to be written, db update. - * - * See https://lab.civicrm.org/dev/core/-/issues/2650 - * - * @return string[] - */ - public function getBasicTokens(): array { - $return = []; - foreach ($this->getExposedFields() as $fieldName) { - $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title']; - } - return $return; - } - } diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index 542ff711ac..c967815ac5 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -214,6 +214,15 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { // from the metadata as yet. return FALSE; } + if ($this->getFieldMetadata()[$fieldName]['type'] === 'Custom') { + // If we remove this early return then we get that extra nuanced goodness + // and support for the more portable v4 style field names + // on custom fields - where labels or names can be returned. + // At present the gap is that the metadata for the label is not accessed + // and tests failed on the enotice and we don't have a clear plan about + // v4 style custom tokens - but medium term this IF will probably go. + return FALSE; + } return (bool) $this->getFieldMetadata()[$fieldName]['options']; } @@ -284,4 +293,60 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { } } + /** + * Get tokens supporting the syntax we are migrating to. + * + * In general these are tokens that were not previously supported + * so we can add them in the preferred way or that we have + * undertaken some, as yet to be written, db update. + * + * See https://lab.civicrm.org/dev/core/-/issues/2650 + * + * @return string[] + * @throws \API_Exception + */ + public function getBasicTokens(): array { + $return = []; + foreach ($this->getExposedFields() as $fieldName) { + $return[$fieldName] = $this->getFieldMetadata()[$fieldName]['title']; + } + return $return; + } + + /** + * Get entity fields that should be exposed as tokens. + * + * @return string[] + * + */ + public function getExposedFields(): array { + $return = []; + foreach ($this->getFieldMetadata() as $field) { + if (!in_array($field['name'], $this->getSkippedFields(), TRUE)) { + $return[] = $field['name']; + } + } + return $return; + } + + /** + * Get entity fields that should not be exposed as tokens. + * + * @return string[] + */ + public function getSkippedFields(): array { + $fields = ['contact_id']; + if (!CRM_Campaign_BAO_Campaign::isCampaignEnable()) { + $fields[] = 'campaign_id'; + } + return $fields; + } + + /** + * @return string + */ + protected function getEntityName(): string { + return CRM_Core_DAO_AllCoreTables::convertEntityNameToLower($this->getApiEntityName()); + } + } diff --git a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php index d10f687bd7..2b2c9cc289 100644 --- a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php +++ b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php @@ -255,6 +255,7 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr * legacy processor function. Once this is true we can expose the listener on the * token processor for contribution and call it internally from the legacy code. * + * @throws \API_Exception * @throws \CiviCRM_API3_Exception */ public function testTokenRendering(): void { @@ -381,7 +382,9 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr foreach ($fields as $field) { $allFields[$field['name']] = $field['title']; } - // $this->assertEquals($realLegacyTokens, $allFields); + // contact ID is skipped. + unset($allFields['contact_id']); + $this->assertEquals($allFields, $realLegacyTokens); $this->assertEquals($legacyTokens, $processor->tokenNames); foreach ($tokens as $token) { $this->assertEquals(CRM_Core_SelectValues::contributionTokens()['{contribution.' . $token . '}'], $processor->tokenNames[$token]); diff --git a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php index 1eada58482..f74d3624c4 100644 --- a/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/BAO/ContributionTest.php @@ -1304,7 +1304,8 @@ WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; /** * Test for replaceContributionTokens. - * This function tests whether the contribution tokens are replaced with + * + * This function tests whether the contribution tokens are replaced with * values from contribution. * * @throws \CiviCRM_API3_Exception -- 2.25.1