From: Eileen McNaughton Date: Wed, 22 Sep 2021 06:18:31 +0000 (+1200) Subject: dev/core#2814 fix tokenCompat to be consistent with unresolved tokens X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=72379673cc446de68d7b68e2173112f6693ca8aa;p=civicrm-core.git 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*' --- diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index 92e20b7806..bef111009a 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -411,21 +411,18 @@ class TokenCompatSubscriber implements EventSubscriberInterface { * 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/Activity/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php index 4e920f45f5..97c0760797 100644 --- a/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php @@ -140,15 +140,16 @@ class CRM_Activity_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { } /** + * Unknown tokens are removed at the very end. + * * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ public function testCreateDocumentUnknownTokens(): void { $activity = $this->activityCreate(); - $html_message = 'Unknown token: {activity.something_unknown}'; + $html_message = 'Unknown token: '; $form = $this->getFormObject('CRM_Activity_Form_Task_PDF'); $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]); - // Unknown tokens should be left alone $this->assertEquals($html_message, $output[0]); } diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index ed303f2734..3b5454cb66 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -397,6 +397,20 @@ emo } } + /** + * Test that unresolved tokens are not causing a fatal error in smarty. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function testUnresolvedTokens(): void { + CRM_Core_BAO_MessageTemplate::renderTemplate([ + 'messageTemplate' => [ + 'msg_text' => '{contact.blah}', + ], + ])['text']; + } + /** * Hook to advertise tokens. *