From 49ab56aef758276e41f1950fe76439613f408eb6 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 6 Feb 2021 16:36:45 +1300 Subject: [PATCH] Convert token processing to use token processor for contacts 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 | 9 +-------- tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php | 5 ++--- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index e64e2639a9..44267824cc 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -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; } diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 34311a1485..83d5029aa0 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -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 -- 2.25.1