From dd2f879ad23e28bbd5bc38e64f62bc57dcbef1fe Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 13 Sep 2021 18:24:31 +1200 Subject: [PATCH] dev/core#2650 add support & testing for preferred token format 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 | 33 +++++++++++++------ .../CRM/Utils/TokenConsistencyTest.php | 17 ++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/CRM/Member/Tokens.php b/CRM/Member/Tokens.php index 686cc71f46..8aff36f7ae 100644 --- a/CRM/Member/Tokens.php +++ b/CRM/Member/Tokens.php @@ -22,13 +22,24 @@ * 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); } } diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index ee3bf2d299..b3a08b2873 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -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()]); } /** -- 2.25.1