dev/core#2650 Add support for contribution_status_id to the processor
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 16 Jul 2021 00:59:06 +0000 (12:59 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 16 Jul 2021 11:15:34 +0000 (23:15 +1200)
Per https://lab.civicrm.org/dev/core/-/issues/2650 the goal is to get the
token processor (for each entity, in this case contributions) to be processing fields
in the same way as the legacy processor does and for this to be thoroughly tested.

This allows us to expose the token processor classes to message template processing
and use them interchangeably, with an eventual goal of migrating over.

It turns out that althought 'Contribution Status ID' is exposed in the UI
for scheduled reminders it did not work. This left us open to simply add the
token contribution.contribution_status_id and add tests.

Per 2650 the handling of pseudoconstants is inconsistent but our preferred goal
is that they would follow apiv4 style syntax and that the actual field name
would hold the actual field value.

Although the token processor exposes a value 'status' - it seems this is likely never
actually exposed to the user and is probably unused.

We can come back to how to deprecate & remove

CRM/Contribute/Tokens.php
tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php

index 93a8747e277ae5d7c44ecce6949f6b59fa235f85..a8ddf76aafcb5ddf142fe1bc5539e222f139dcce 100644 (file)
@@ -43,6 +43,7 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber {
       'receipt_date',
       'thankyou_date',
       'tax_amount',
+      'contribution_status_id',
     ];
   }
 
@@ -62,6 +63,21 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber {
     ];
   }
 
+  /**
+   * Get tokens supporting the syntax we are migrating to.
+   *
+   * In general these are tokens that were not previously supported
+   * so we can add them in the preferred way or that we have
+   * undertaken some, as yet to be written, db update.
+   *
+   * See https://lab.civicrm.org/dev/core/-/issues/2650
+   *
+   * @return string[]
+   */
+  protected function getBasicTokens(): array {
+    return ['contribution_status_id' => ts('Contribution Status ID')];
+  }
+
   /**
    * Class constructor.
    */
@@ -73,6 +89,9 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber {
     $tokens['id'] = ts('Contribution ID');
     $tokens['payment_instrument'] = ts('Payment Instrument');
     $tokens['source'] = ts('Contribution Source');
+    // Per https://lab.civicrm.org/dev/core/-/issues/2650
+    // the intent is to deprecate this field in favour of
+    // {contribution.contribution_status_id:label}
     $tokens['status'] = ts('Contribution Status');
     $tokens['type'] = ts('Financial Type');
     $tokens = array_merge($tokens, CRM_Utils_Token::getCustomFieldTokens('Contribution'));
@@ -122,12 +141,18 @@ class CRM_Contribute_Tokens extends AbstractTokenSubscriber {
       return $row->format('text/plain')->tokens($entity, $field,
         \CRM_Utils_Money::format($fieldValue, $actionSearchResult->contrib_currency));
     }
-    elseif (isset($aliasTokens[$field])) {
+    if (isset($aliasTokens[$field])) {
       $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $aliasTokens[$field], $fieldValue);
     }
     elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
       $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
     }
+    elseif (in_array($field, array_keys($this->getBasicTokens()))) {
+      // For now we just ensure that the label fields do not override the
+      // id field here.
+      // Later we will add support for contribution_status_id:label
+      $row->tokens($entity, $field, $fieldValue);
+    }
     else {
       $row->dbToken($entity, $field, 'CRM_Contribute_BAO_Contribution', $field, $fieldValue);
     }
index b622112d4ade212843093a62dc882b9f67c81e04..0fe9af14f5d38abb747ff40770016c83c7058e4f 100644 (file)
@@ -234,7 +234,29 @@ class CRM_Contribute_ActionMapping_ByTypeTest extends \Civi\ActionSchedule\Abstr
   public function useHelloFirstNameStatus() {
     $this->schedule->subject = 'Hello, {contact.first_name}. @{contribution.status}. (via subject)';
     $this->schedule->body_html = '<p>Hello, {contact.first_name}. @{contribution.status}. (via body_html)</p>';
-    $this->schedule->body_text = 'Hello, {contact.first_name}. @{contribution.status}. (via body_text)';
+    $this->schedule->body_text = 'Hello, {contact.first_name}. @{contribution.status} (via body_text)';
+  }
+
+  public function testTokenRendering() {
+    $this->targetDate = '20150201000107';
+    \CRM_Utils_Time::setTime('2015-02-01 00:00:00');
+    $this->addAliceDues();
+    $this->scheduleForAny();
+    $this->startOnTime();
+    $this->schedule->save();
+    $this->schedule->body_text = '
+      first name = {contact.first_name}
+      receive_date = {contribution.receive_date}
+      contribution status id = {contribution.contribution_status_id}
+      legacy style status = {contribution.status}';
+    $this->schedule->save();
+    $this->callAPISuccess('job', 'send_reminder', []);
+    $this->mut->checkMailLog([
+      'first name = Alice',
+      'receive_date = February 1st, 2015 12:00 AM',
+      'contribution status id = 1',
+      'legacy style status = Completed',
+    ]);
   }
 
 }