dev/core#4079 Add Pledge Tokens
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 16 Mar 2023 22:41:36 +0000 (11:41 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 16 Mar 2023 23:21:13 +0000 (12:21 +1300)
CRM/Event/Tokens.php
CRM/Pledge/Tokens.php [new file with mode: 0644]
Civi/Core/Container.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

index 3ac471ad17da9ac61e3fd6b2dae94368e0165e80..5e00e521dd25d998a6eb3595805212fedeb3a8a6 100644 (file)
@@ -17,13 +17,6 @@ use Civi\Token\TokenRow;
  * Class CRM_Event_Tokens
  *
  * Generate "event.*" tokens.
- *
- * This TokenSubscriber was produced by refactoring the code from the
- * scheduled-reminder system with the goal of making that system
- * more flexible. The current implementation is still coupled to
- * scheduled-reminders. It would be good to figure out a more generic
- * implementation which is not tied to scheduled reminders, although
- * that is outside the current scope.
  */
 class CRM_Event_Tokens extends CRM_Core_EntityTokens {
 
diff --git a/CRM/Pledge/Tokens.php b/CRM/Pledge/Tokens.php
new file mode 100644 (file)
index 0000000..c296148
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved.                        |
+ |                                                                    |
+ | This work is published under the GNU AGPLv3 license with some      |
+ | permitted exceptions and without any warranty. For full license    |
+ | and copyright information, see https://civicrm.org/licensing       |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ * Class CRM_Pledge_Tokens
+ *
+ * Generate "pledge.*" tokens.
+ *
+ * @noinspection PhpUnused
+ */
+class CRM_Pledge_Tokens extends CRM_Core_EntityTokens {
+
+  /**
+   * Get the entity name for api v4 calls.
+   *
+   * @return string
+   */
+  protected function getApiEntityName(): string {
+    return 'Pledge';
+  }
+
+  /**
+   * Get entity fields that should be exposed as tokens.
+   *
+   * These are the fields that seem most likely to be useful as tokens.
+   *
+   * @todo - add more pseudo-fields like 'paid_amount', 'balance_amount'
+   * to v4 api - see the ContributionGetSpecProvider for how.
+   *
+   * @return string[]
+   *
+   */
+  protected function getExposedFields(): array {
+    return [
+      'amount',
+      'currency',
+      'frequency_unit',
+      'frequency_interval',
+      'frequency_day',
+      'installments',
+      'start_date',
+      'create_date',
+      'cancel_date',
+      'end_date',
+    ];
+  }
+
+}
index 2da5b7abd92e54fe53d311ad93fe0e6cae1f9be0..5b983fb637652e658ab683a463f6d964a2db39e7 100644 (file)
@@ -345,9 +345,9 @@ class Container {
       []
     ))->addTag('kernel.event_subscriber')->setPublic(TRUE);
 
-    foreach (['Activity', 'Contact', 'Contribute', 'Event', 'Mailing', 'Member', 'Case'] as $comp) {
-      $container->setDefinition('crm_' . strtolower($comp) . '_tokens', new Definition(
-        "CRM_{$comp}_Tokens",
+    foreach (['Activity', 'Contact', 'Contribute', 'Event', 'Mailing', 'Member', 'Case', 'Pledge'] as $component) {
+      $container->setDefinition('crm_' . strtolower($component) . '_tokens', new Definition(
+        "CRM_{$component}_Tokens",
         []
       ))->addTag('kernel.event_subscriber')->setPublic(TRUE);
     }
index 3f2154215a3d24920210e3b52be7ecad3a9606f4..29bd312fedd1f805575fc6bbe4948e4a9d129048 100644 (file)
@@ -528,6 +528,37 @@ contribution_recur.payment_instrument_id:name :Check
 
   }
 
+  public function testPledgeTokens(): void {
+    $pledgeID = $this->pledgeCreate(['contact_id' => $this->individualCreate(), 'create_date' => '2022-04-07', 'start_date' => '2022-04-07']);
+    $tokenProcessor = $this->getTokenProcessor(['schema' => ['pledgeId']]);
+    $list = $tokenProcessor->listTokens();
+    $this->assertEquals(array_merge($this->getPledgeTokens(), $this->getDomainTokens()), $list);
+    $tokenString = implode(', ', array_keys($this->getPledgeTokens()));
+    $tokenProcessor->addMessage('text', $tokenString, 'text/plain');
+    $tokenProcessor->addRow(['pledgeId' => $pledgeID]);
+    $tokenProcessor->evaluate();
+    $this->assertEquals($this->getExpectedPledgeTokenOutput(), $tokenProcessor->getRow(0)->render('text'));
+  }
+
+  public function getExpectedPledgeTokenOutput(): string {
+    return '$100.00, US Dollar, year, 5, 15, 5, April 7th, 2022, April 7th, 2022, , ';
+  }
+
+  public function getPledgeTokens(): array {
+    return [
+      '{pledge.amount}' => 'Total Pledged',
+      '{pledge.currency:label}' => 'Pledge Currency',
+      '{pledge.frequency_unit:label}' => 'Pledge Frequency Unit',
+      '{pledge.frequency_interval}' => 'Pledge Frequency Interval',
+      '{pledge.frequency_day}' => 'Pledge day',
+      '{pledge.installments}' => 'Pledge Number of Installments',
+      '{pledge.start_date}' => 'Pledge Start Date',
+      '{pledge.create_date}' => 'Pledge Made',
+      '{pledge.cancel_date}' => 'Pledge Cancelled Date',
+      '{pledge.end_date}' => 'Pledge End Date',
+    ];
+  }
+
   /**
    * Get the advertised tokens the legacy function doesn't know about.
    *