diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index bb0c2dcef54c..806e22423751 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -149,19 +149,16 @@ public static function postProcess(&$form) { continue; } - $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken); - + $tokenHtml = $html_message; if ($caseId) { $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenHtml, $messageToken); } - $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $contact[$contactId], $categories, TRUE); - - if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { - $smarty = CRM_Core_Smarty::singleton(); - // also add the contact tokens to the template - $smarty->assign_by_ref('contact', $contact); - $tokenHtml = $smarty->fetch("string:$tokenHtml"); - } + $tokenHtml = CRM_Core_BAO_MessageTemplate::renderTemplate([ + 'contactId' => $contactId, + 'messageTemplate' => ['msg_html' => $tokenHtml], + 'tplParams' => ['contact' => $contact], + 'disableSmarty' => (!defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY), + ])['html']; $html[] = $tokenHtml; } @@ -181,7 +178,7 @@ public static function postProcess(&$form) { $fileName = self::getFileName($form); $fileName = "$fileName.$type"; - if ($type == 'pdf') { + if ($type === 'pdf') { CRM_Utils_PDF_Utils::html2pdf($html, $fileName, FALSE, $formValues); } elseif (!empty($formValues['document_file_path'])) { diff --git a/tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php index 4b0ec36074e9..f8a0d44a9476 100644 --- a/tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php @@ -47,18 +47,10 @@ protected function setUp(): void { * @dataProvider getFilenameCases */ public function testFilenameIsAssigned(?string $pdfFileName, ?string $activitySubject, ?bool $isLiveMode, string $expectedFilename): void { - // @todo - remove this cid - it helps direct the form controller but is - // pretty cludgey. - $_REQUEST['cid'] = $this->contactId; - $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', [ + $form = $this->getPDFForm([ 'pdf_file_name' => $pdfFileName, 'subject' => $activitySubject, - 'document_type' => 'pdf', - 'buttons' => [ - '_qf_PDF_upload' => $isLiveMode, - ], - ]); - $form->_contactIds = [$this->contactId]; + ], [$this->contactId], $isLiveMode); $fileNameAssigned = $this->submitForm($form)['fileName']; $this->assertEquals($expectedFilename, $fileNameAssigned); } @@ -127,4 +119,38 @@ protected function submitForm(CRM_Core_Form $form) { $this->fail('line should be unreachable'); } + /** + * @param array $formValues + * @param array $contactIDs + * @param bool|null $isLiveMode + * + * @return \CRM_Contact_Form_Task_PDF + */ + protected function getPDFForm(array $formValues, array $contactIDs, ?bool $isLiveMode = TRUE): CRM_Contact_Form_Task_PDF { + // pretty cludgey. + $_REQUEST['cid'] = $contactIDs[0]; + /* @var CRM_Contact_Form_Task_PDF $form */ + $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', array_merge([ + 'pdf_file_name' => 'pdf_file_name', + 'subject' => 'subject', + 'document_type' => 'pdf', + 'buttons' => [ + '_qf_PDF_upload' => $isLiveMode, + ], + ], $formValues)); + $form->_contactIds = $contactIDs; + return $form; + } + + /** + * Test contact tokens are resolved. + */ + public function testContactTokensAreResolved(): void { + $form = $this->getPDFForm([ + 'html_message' => '{contact.first_name}, {contact.email_greeting}', + ], [$this->contactId]); + $processedMessage = $this->submitForm($form)['html']; + $this->assertStringContainsString('Logged In, Dear Logged In', $processedMessage); + } + }