[REF] simplify member_tokens
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 15 Sep 2021 00:35:57 +0000 (12:35 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 15 Sep 2021 00:41:44 +0000 (12:41 +1200)
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
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

index cf883bfeca1a693806ae62d721c87a6edc7e64e4..9db8a15420ae08310ede86ab797f2f8268e1dfd6 100644 (file)
@@ -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);
index 142a35e4939e75982914d06a62d852c58ed59954..4950ee0babfb92a8d382ff29f9221ca6446c8fa0 100644 (file)
@@ -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];
   }