From e2d052a224a4568c339fcf969977cc039ba002b7 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 22 Sep 2021 18:18:31 +1200 Subject: [PATCH] dev/core#2814 fix tokenCompat to be consistent with unresolved tokens This fixes the tokenCompat subscriber to replace unresolved tokens with a blank string in a consistent way. Prior to this it would crash if smarty was enabled but not all tokens were resolved & print unresolved tokens if smarty was not enabled. The inconsistencies appear to be due to 'separate evolution' rather than '*reasons*' --- Civi/Token/TokenCompatSubscriber.php | 21 ++++++++----------- .../CRM/Core/BAO/MessageTemplateTest.php | 11 ++++++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index 92e20b780680..bef111009a22 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -411,21 +411,18 @@ protected function getFieldMetadata(): array { * Apply the various CRM_Utils_Token helpers. * * @param \Civi\Token\Event\TokenRenderEvent $e - * - * @throws \CRM_Core_Exception */ public function onRender(TokenRenderEvent $e): void { - $isHtml = ($e->message['format'] === 'text/html'); $useSmarty = !empty($e->context['smarty']); - - if (!empty($e->context['contact'])) { - // @todo - remove this - it simply removes the last unresolved tokens before - // they break smarty. - // historically it was only called when context['contact'] so that is - // retained but it only works because it's almost always true. - $remainingTokens = array_keys(\CRM_Utils_Token::getTokens($e->string)); - if (!empty($remainingTokens)) { - $e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $remainingTokens); + $remainingTokens = \CRM_Utils_Token::getTokens($e->string); + + if ($remainingTokens) { + foreach ($remainingTokens as $part1 => $part2) { + $e->string = preg_replace( + '/(?string + ); } } diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index ed303f27341f..71252480e731 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -397,6 +397,17 @@ public function hookTokenValues(array &$details): void { } } + /** + * Test that unresolved tokens are removed in the end. + */ + public function testUnresolvedTokens(): void { + CRM_Core_BAO_MessageTemplate::renderTemplate([ + 'messageTemplate' => [ + 'msg_text' => '{contact.blah}', + ], + ])['text']; + } + /** * Hook to advertise tokens. *