Start adding tests on event tokens
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 19 Sep 2021 04:09:11 +0000 (16:09 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 19 Sep 2021 04:10:00 +0000 (16:10 +1200)
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
CRM/Event/Tokens.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

index f8aba561018f4475e688e777cf1fd86e2345de7a..43045bd30fa3b85e8a09974d86b80b3ba08afaaf 100644 (file)
@@ -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'),
index 15784cd87289b3ff72e1ee10b49559e24495c0b6..54189d30eb285086ed6a5388b3935b70f75a98be 100644 (file)
@@ -10,6 +10,8 @@
  +--------------------------------------------------------------------+
  */
 
+use Civi\ActionSchedule\Event\MailingQueryEvent;
+
 /**
  * Class CRM_Event_Tokens
  *
  * 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;
     }
index edc3bc43f1bf77ad0ee7882f9b30d43d6ad86b51..6bb288bd74d7d5760da506c834d843a5f4ca1eb7 100644 (file)
@@ -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',
+    ];
+  }
+
 }