From 97ca72e4cd8d714a9d2c4125c72f0fc0eb3d3ad9 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 10 Sep 2021 15:59:42 +1200 Subject: [PATCH] Fix booleans to use :label format --- CRM/Core/EntityTokens.php | 28 ++++++++++++++++++- .../Contribute/ActionMapping/ByTypeTest.php | 4 ++- .../CRM/Utils/TokenConsistencyTest.php | 16 +++++------ 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index c3882b4bbf..8841dcdb6e 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -124,7 +124,30 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { * @return array|string[] */ public function getAllTokens(): array { - return array_merge($this->getBasicTokens(), $this->getPseudoTokens(), CRM_Utils_Token::getCustomFieldTokens($this->getApiEntityName())); + $basicTokens = $this->getBasicTokens(); + foreach (array_keys($basicTokens) as $fieldName) { + // The goal is to be able to render more complete tokens + // (eg. actual booleans, field names, raw ids) for a more + // advanced audiences - ie those using conditionals + // and to specify that audience in the api that retrieves. + // But, for now, let's not advertise, given that most of these fields + // aren't really needed even once... + if ($this->isBooleanField($fieldName)) { + unset($basicTokens[$fieldName]); + } + } + return array_merge($basicTokens, $this->getPseudoTokens(), CRM_Utils_Token::getCustomFieldTokens($this->getApiEntityName())); + } + + /** + * Is the given field a boolean field. + * + * @param string $fieldName + * + * @return bool + */ + public function isBooleanField(string $fieldName): bool { + return $this->getFieldMetadata()[$fieldName]['data_type'] === 'Boolean'; } /** @@ -204,6 +227,9 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { $return[$fieldName . ':label'] = $fieldLabel; $return[$fieldName . ':name'] = ts('Machine name') . ': ' . $fieldLabel; } + if ($this->isBooleanField($fieldName)) { + $return[$fieldName . ':label'] = $this->getFieldMetadata()[$fieldName]['title']; + } } return $return; } diff --git a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php index 14da2ef425..2140dd8ed4 100644 --- a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php +++ b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php @@ -397,7 +397,9 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr $fields = (array) Contribution::getFields()->addSelect('name', 'title')->execute()->indexBy('name'); $allFields = []; foreach ($fields as $field) { - $allFields[$field['name']] = $field['title']; + if (!in_array($field['name'], ['is_test', 'is_pay_later', 'is_template'], TRUE)) { + $allFields[$field['name']] = $field['title']; + } } // contact ID is skipped. unset($allFields['contact_id']); diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index f0826af266..662bbe9323 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -221,16 +221,16 @@ No 'trxn_id' => 'Transaction ID', 'invoice_id' => 'Invoice ID', 'contribution_status_id' => 'Status', - 'is_test' => 'Test', + 'is_test:label' => 'Test', 'cycle_day' => 'Cycle Day', 'next_sched_contribution_date' => 'Next Scheduled Contribution Date', 'failure_count' => 'Number of Failures', 'failure_retry_date' => 'Retry Failed Attempt Date', - 'auto_renew' => 'Auto Renew', + 'auto_renew:label' => 'Auto Renew', 'payment_processor_id' => 'Payment Processor ID', 'financial_type_id' => 'Financial Type ID', 'payment_instrument_id' => 'Payment Method', - 'is_email_receipt' => 'Send email Receipt?', + 'is_email_receipt:label' => 'Send email Receipt?', 'frequency_unit:label' => 'Frequency Unit', 'frequency_unit:name' => 'Machine name: Frequency Unit', 'contribution_status_id:label' => 'Status', @@ -309,22 +309,22 @@ abc 123 inv123 2 -1 +Yes 15 0 January 3rd, 2020 12:00 AM -1 +Yes 1 2 4 -1 +Yes year year Pending Label** Pending -Dummy -Dummy +Dummy (test) +Dummy (test) Member Dues Member Dues Check -- 2.25.1