From cb31dd27a6cf97aa80643e3bd3f5345998419a7f Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 16 Jul 2021 12:59:06 +1200 Subject: [PATCH] dev/core#2650 Add support for contribution_status_id to the processor 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 | 27 ++++++++++++++++++- .../Contribute/ActionMapping/ByTypeTest.php | 24 ++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CRM/Contribute/Tokens.php b/CRM/Contribute/Tokens.php index 93a8747e27..a8ddf76aaf 100644 --- a/CRM/Contribute/Tokens.php +++ b/CRM/Contribute/Tokens.php @@ -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); } diff --git a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php index b622112d4a..0fe9af14f5 100644 --- a/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php +++ b/tests/phpunit/CRM/Contribute/ActionMapping/ByTypeTest.php @@ -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 = '

Hello, {contact.first_name}. @{contribution.status}. (via body_html)

'; - $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', + ]); } } -- 2.25.1