Convert token processing to use token processor for contacts
authoreileen <emcnaughton@wikimedia.org>
Sat, 6 Feb 2021 03:36:45 +0000 (16:36 +1300)
committereileen <emcnaughton@wikimedia.org>
Sat, 27 Mar 2021 00:49:02 +0000 (13:49 +1300)
Note this will test fail on 3 things that worked with the other. The way in
which we load tokens means that some values have been exposed that
possibly would be better not exposed - but we can either remove them
from advertised tokens, fix the tokenCompat class to support them
or possibly deprecate by removing from the UI display but
continuing to handle. One notable difference is the contact_reference
entity id.

CRM/Core/BAO/MessageTemplate.php
tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php

index e64e2639a9254fd4725b447aad42161d8b97cb54..44267824cceeb64366882fd9c04e8d6fd9a03534 100644 (file)
@@ -700,18 +700,12 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate {
    * @return array
    */
   public static function renderMessageTemplate(array $mailContent, bool $disableSmarty, $contactID, array $smartyAssigns): array {
-    if ($contactID) {
-      // @todo resolve contact ID below - see https://github.com/civicrm/civicrm-core/pull/19550
-      // for things to resolve first.
-      $tokens = self::getTokensToResolve($mailContent);
-      $mailContent = self::resolveContactTokens($contactID, $tokens, $mailContent, !$disableSmarty);
-    }
     CRM_Core_Smarty::singleton()->pushScope($smartyAssigns);
     $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), ['smarty' => !$disableSmarty]);
     $tokenProcessor->addMessage('html', $mailContent['html'], 'text/html');
     $tokenProcessor->addMessage('text', $mailContent['text'], 'text/plain');
     $tokenProcessor->addMessage('subject', $mailContent['subject'], 'text/plain');
-    $tokenProcessor->addRow([]);
+    $tokenProcessor->addRow($contactID ? ['contactId' => $contactID] : []);
     $tokenProcessor->evaluate();
     foreach ($tokenProcessor->getRows() as $row) {
       $mailContent['html'] = $row->render('html');
@@ -719,7 +713,6 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate {
       $mailContent['subject'] = $row->render('subject');
     }
     CRM_Core_Smarty::singleton()->popScope();
-
     $mailContent['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $mailContent['subject']));
     return $mailContent;
   }
index 34311a1485761ef02ee6dc834391a1543a988694..83d5029aa054de23d884194e8453e7b6858b5660 100644 (file)
@@ -117,7 +117,6 @@ London, 90210
   /**
    * Test rendering of smarty tokens.
    *
-   * @throws \CRM_Core_Exception
    */
   public function testRenderMessageTemplateIgnoreSmarty(): void {
     $messageContent = CRM_Core_BAO_MessageTemplate::renderMessageTemplate([
@@ -187,7 +186,7 @@ Default Domain Name
     foreach (array_keys($tokenData) as $key) {
       $tokenString .= "{$key}:{contact.{$key}}\n";
     }
-    $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), []);
+    $tokenProcessor = new TokenProcessor(Civi::dispatcher(), []);
     $tokenProcessor->addMessage('html', $tokenString, 'text/html');
     $tokenProcessor->addRow(['contactId' => $tokenData['contact_id']]);
     $tokenProcessor->evaluate();
@@ -398,7 +397,7 @@ sort_name:Smith, Robert
 display_name:Mr. Robert Smith II
 nick_name:Bob
 image_URL:https://example.com
-preferred_communication_method:
+preferred_communication_method:Phone
 preferred_language:fr_CA
 preferred_mail_format:Both
 hash:xyz