dev/core#2650 add support & testing for preferred token format
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 13 Sep 2021 06:24:31 +0000 (18:24 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 14 Sep 2021 03:13:18 +0000 (15:13 +1200)
This fixes the membership token processor to support the preferred
token format style in scheduled reminders
and adds test cover

Next step is to upgrade the old tokens out of the scheduled reminders
and remove support for them. For the legacy token handler
it is just used from one place from core so we can add a rule
to prevent them from being 'requested' & switch them
off the legacy method.

The test cover in this is more thorough than pre-existing tests
but here is a list of them

api_v3_JobTest.testCallSendReminderSuccessMoreThanDefaultLimit
api_v3_JobTest.testCallSendReminderLimitToSMS
api_v3_JobTest.testCallSendReminderLimitToSMSWithDeletedProvider
CRM_Core_BAO_ActionScheduleTest.testMembershipDateMatch
CRM_Core_BAO_ActionScheduleTest.testMembershipJoinDateNonMatch
CRM_Core_BAO_ActionScheduleTest.testMembershipEndDateRepeat
CRM_Core_BAO_ActionScheduleTest.testMembershipEndDateRepeatChangedEndDate_CRM_15376
CRM_Core_BAO_ActionScheduleTest.testMembershipEndDateMatch
CRM_Core_BAO_ActionScheduleTest.testMultipleMembershipEndDateMatch
CRM_Core_BAO_ActionScheduleTest.testMembershipEndDateNoMatch
CRM_Core_BAO_ActionScheduleTest.testMembershipLimitToNone
CRM_Core_BAO_ActionScheduleTest.testMembershipWithReferenceDate
CRM_Core_BAO_ActionScheduleTest.testMembershipOnMultipleReminder
CRM_Core_BAO_ActionScheduleTest.testRepetitionFrequencyUnit
CRM_Core_BAO_ActionScheduleTest.testInheritedMembershipPermissions
CRM_Core_BAO_ActionScheduleTest.testMembershipScheduleWithAbsoluteDate

CRM/Member/Tokens.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

index 686cc71f4652102c7c29e474dfd94c4102fb0592..8aff36f7ae13be055d8d3296e3b3fca9bbd2042b 100644 (file)
  * implementation which is not tied to scheduled reminders, although
  * that is outside the current scope.
  */
-class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
+class CRM_Member_Tokens extends CRM_Core_EntityTokens {
 
   /**
-   * Class constructor.
+   * Get the entity name for api v4 calls.
+   *
+   * @return string
+   */
+  protected function getApiEntityName(): string {
+    return 'Membership';
+  }
+
+  /**
+   * Get all tokens.
+   *
+   * This function will be removed once the parent class can determine it.
    */
-  public function __construct() {
-    parent::__construct('membership', array_merge(
+  public function getAllTokens(): array {
+    return array_merge(
       [
         'fee' => ts('Membership Fee'),
         'id' => ts('Membership ID'),
@@ -37,9 +48,11 @@ class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
         'end_date' => ts('Membership End Date'),
         'status' => ts('Membership Status'),
         'type' => ts('Membership Type'),
+        'status_id:label' => ts('Membership Status'),
+        'membership_type_id:label' => ts('Membership Type'),
       ],
       CRM_Utils_Token::getCustomFieldTokens('Membership')
-    ));
+    );
   }
 
   /**
@@ -56,7 +69,7 @@ class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
    *
    * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
    */
-  public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
+  public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void {
     if ($e->mapping->getEntity() !== 'civicrm_membership') {
       return;
     }
@@ -64,9 +77,9 @@ class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
     // FIXME: `select('e.*')` seems too broad.
     $e->query
       ->select('e.*')
-      ->select('mt.minimum_fee as fee, e.id as id , e.join_date, e.start_date, e.end_date, ms.name as status, mt.name as type')
-      ->join('mt', "!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id")
-      ->join('ms', "!casMailingJoinType civicrm_membership_status ms ON e.status_id = ms.id");
+      ->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, ms.name as status, mt.name as type')
+      ->join('mt', '!casMailingJoinType civicrm_membership_type mt ON e.membership_type_id = mt.id')
+      ->join('ms', '!casMailingJoinType civicrm_membership_status ms ON e.status_id = ms.id');
   }
 
   /**
@@ -88,7 +101,7 @@ class CRM_Member_Tokens extends \Civi\Token\AbstractTokenSubscriber {
       $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
     }
     else {
-      $row->tokens($entity, $field, '');
+      parent::evaluateToken($row, $entity, $field, $prefetch);
     }
   }
 
index ee3bf2d2998f5d6b33d7da253f9b89a43d02a341..b3a08b28737621811e0448de18432005707bc689 100644 (file)
@@ -374,6 +374,23 @@ Check';
     $messageToken = CRM_Utils_Token::getTokens($tokenString);
     $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $memberships[$this->getMembershipID()], $tokenString, $messageToken);
     $this->assertEquals($this->getExpectedMembershipTokenOutput(), $tokenHtml);
+
+    // Now compare with scheduled reminder
+    $mut = new CiviMailUtils($this);
+    CRM_Utils_Time::setTime('2007-01-22 15:00:00');
+    $this->callAPISuccess('action_schedule', 'create', [
+      'title' => 'job',
+      'subject' => 'job',
+      'entity_value' => 1,
+      'mapping_id' => 4,
+      'start_action_date' => 'membership_join_date',
+      'start_action_offset' => 1,
+      'start_action_condition' => 'after',
+      'start_action_unit' => 'day',
+      'body_html' => $tokenString,
+    ]);
+    $this->callAPISuccess('job', 'send_reminder', []);
+    $mut->checkMailLog([$this->getExpectedMembershipTokenOutput()]);
   }
 
   /**