From b024d6a1f69e6d99f17363b6d9b9f05f776b4275 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 15 Sep 2021 12:35:57 +1200 Subject: [PATCH] [REF] simplify member_tokens Now we have good test cover we can switch to using the parent. The one thorn in our side is membership.fee. Dealing with the field, and hence opening up 'listening' is out of scope for this pr We can (and should) switch fee to membership_type_id.minimum_fee and add generic support. However, we also have a formatting issue. The field is formatted 'number only' 'just cos'. If we map that field without figuring out formatting it will be locked in as an anomaly. --- CRM/Member/Tokens.php | 21 ++++--------------- .../CRM/Utils/TokenConsistencyTest.php | 12 +++++++---- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/CRM/Member/Tokens.php b/CRM/Member/Tokens.php index cf883bfeca..9db8a15420 100644 --- a/CRM/Member/Tokens.php +++ b/CRM/Member/Tokens.php @@ -71,11 +71,9 @@ class CRM_Member_Tokens extends CRM_Core_EntityTokens { if ($e->mapping->getEntity() !== 'civicrm_membership') { return; } - - // FIXME: `select('e.*')` seems too broad. + parent::alterActionScheduleQuery($e); $e->query - ->select('e.*') - ->select('mt.minimum_fee as fee, e.id as id , e.join_date, e.start_date, e.end_date, membership_type_id as Membership__membership_type_id, status_id as Membership__status_id') + ->select('mt.minimum_fee as ' . $this->getEntityAlias() . 'fee') ->join('mt', '!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id'); } @@ -83,19 +81,8 @@ class CRM_Member_Tokens extends CRM_Core_EntityTokens { * @inheritDoc */ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { - $actionSearchResult = $row->context['actionSearchResult']; - - if (in_array($field, ['start_date', 'end_date', 'join_date'])) { - $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field)); - } - elseif ($field == 'fee') { - $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($actionSearchResult->$field)); - } - elseif (isset($actionSearchResult->$field)) { - $row->tokens($entity, $field, $actionSearchResult->$field); - } - elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { - $row->customToken($entity, $cfID, $actionSearchResult->entity_id); + if ($field === 'fee') { + $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($this->getFieldValue($row, $field))); } else { parent::evaluateToken($row, $entity, $field, $prefetch); diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index 142a35e493..4950ee0bab 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -370,11 +370,14 @@ Check'; $this->assertEquals($this->getMembershipTokens(), $tokens); $newStyleTokens = "\n{membership.status_id:label}\n{membership.membership_type_id:label}\n"; $tokenString = $newStyleTokens . implode("\n", array_keys($this->getMembershipTokens())); + $memberships = CRM_Utils_Token::getMembershipTokenDetails([$this->getMembershipID()]); $messageToken = CRM_Utils_Token::getTokens($tokenString); $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $memberships[$this->getMembershipID()], $tokenString, $messageToken); $this->assertEquals($this->getExpectedMembershipTokenOutput(), $tokenHtml); + // Custom fields work in the processor so test it.... + $tokenString .= "\n{membership." . $this->getCustomFieldName('text') . '}'; // Now compare with scheduled reminder $mut = new CiviMailUtils($this); CRM_Utils_Time::setTime('2007-01-22 15:00:00'); @@ -390,7 +393,7 @@ Check'; 'body_html' => $tokenString, ]); $this->callAPISuccess('job', 'send_reminder', []); - $mut->checkMailLog([$this->getExpectedMembershipTokenOutput()]); + $mut->checkMailLog([$this->getExpectedMembershipTokenOutput() . "\nmy field"]); } /** @@ -417,9 +420,10 @@ Check'; */ protected function getMembershipID(): int { if (!isset($this->ids['Membership'][0])) { - $this->ids['Membership'][0] = $this->contactMembershipCreate( - ['contact_id' => $this->getContactID()] - ); + $this->ids['Membership'][0] = $this->contactMembershipCreate([ + 'contact_id' => $this->getContactID(), + $this->getCustomFieldName('text') => 'my field', + ]); } return $this->ids['Membership'][0]; } -- 2.25.1