From ce971869a9adeff62b2cb789221398184b1db8e2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 19 Sep 2021 16:09:11 +1200 Subject: [PATCH] Start adding tests on event tokens This reconciles the 2 ways of listing event tokens. (The way we are about to deprecate doesn't list custom fields but can just call the other --- CRM/Core/SelectValues.php | 2 +- CRM/Event/Tokens.php | 34 ++++++++++++----- .../CRM/Utils/TokenConsistencyTest.php | 38 +++++++++++++++++++ 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index f8aba56101..43045bd30f 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -550,7 +550,7 @@ class CRM_Core_SelectValues { '{event.contact_phone}' => ts('Event Contact Phone'), '{event.description}' => ts('Event Description'), '{event.location}' => ts('Event Location'), - '{event.fee_amount}' => ts('Event Fees'), + '{event.fee_amount}' => ts('Event Fee'), '{event.info_url}' => ts('Event Info URL'), '{event.registration_url}' => ts('Event Registration URL'), '{event.balance}' => ts('Event Balance'), diff --git a/CRM/Event/Tokens.php b/CRM/Event/Tokens.php index 15784cd872..54189d30eb 100644 --- a/CRM/Event/Tokens.php +++ b/CRM/Event/Tokens.php @@ -10,6 +10,8 @@ +--------------------------------------------------------------------+ */ +use Civi\ActionSchedule\Event\MailingQueryEvent; + /** * Class CRM_Event_Tokens * @@ -22,13 +24,24 @@ * implementation which is not tied to scheduled reminders, although * that is outside the current scope. */ -class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber { +class CRM_Event_Tokens extends CRM_Core_EntityTokens { /** - * Class constructor. + * Get the entity name for api v4 calls. + * + * @return string + */ + protected function getApiEntityName(): string { + return 'Event'; + } + + /** + * Get all tokens. + * + * This function will be removed once the parent class can determine it. */ - public function __construct() { - parent::__construct('event', array_merge( + public function getAllTokens(): array { + return array_merge( [ 'event_type' => ts('Event Type'), 'title' => ts('Event Title'), @@ -41,12 +54,12 @@ class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber { 'info_url' => ts('Event Info URL'), 'registration_url' => ts('Event Registration URL'), 'fee_amount' => ts('Event Fee'), - 'contact_email' => ts('Event Contact (Email)'), - 'contact_phone' => ts('Event Contact (Phone)'), + 'contact_email' => ts('Event Contact Email'), + 'contact_phone' => ts('Event Contact Phone'), 'balance' => ts('Event Balance'), ], CRM_Utils_Token::getCustomFieldTokens('Event') - )); + ); } /** @@ -54,8 +67,9 @@ class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber { */ public function checkActive(\Civi\Token\TokenProcessor $processor) { // Extracted from scheduled-reminders code. See the class description. - return !empty($processor->context['actionMapping']) - && $processor->context['actionMapping']->getEntity() === 'civicrm_participant'; + return ((!empty($processor->context['actionMapping']) + && $processor->context['actionMapping']->getEntity() === 'civicrm_participant')) + || in_array($this->getEntityIDField(), $processor->context['schema'], TRUE); } /** @@ -63,7 +77,7 @@ class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber { * * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e */ - public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) { + public function alterActionScheduleQuery(MailingQueryEvent $e): void { if ($e->mapping->getEntity() !== 'civicrm_participant') { return; } diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index edc3bc43f1..6bb288bd74 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -535,4 +535,42 @@ December 21st, 2007 ]; } + /** + * Test that domain tokens are consistently rendered. + */ + public function testEventTokenConsistency(): void { + $tokens = CRM_Core_SelectValues::eventTokens(); + $this->assertEquals($this->getEventTokens(), $tokens); + $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ + 'controller' => __CLASS__, + 'smarty' => FALSE, + 'schema' => ['eventId'], + ]); + $this->assertEquals(array_merge($tokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); + } + + /** + * Get expected event tokens. + * + * @return string[] + */ + protected function getEventTokens(): array { + return [ + '{event.event_id}' => 'Event ID', + '{event.title}' => 'Event Title', + '{event.start_date}' => 'Event Start Date', + '{event.end_date}' => 'Event End Date', + '{event.event_type}' => 'Event Type', + '{event.summary}' => 'Event Summary', + '{event.contact_email}' => 'Event Contact Email', + '{event.contact_phone}' => 'Event Contact Phone', + '{event.description}' => 'Event Description', + '{event.location}' => 'Event Location', + '{event.fee_amount}' => 'Event Fee', + '{event.info_url}' => 'Event Info URL', + '{event.registration_url}' => 'Event Registration URL', + '{event.balance}' => 'Event Balance', + ]; + } + } -- 2.25.1