Skip to content

Commit

Permalink
[REF] dev/core#2790 Start migration to MessageTemplate::render
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Sep 1, 2021
1 parent a7a5556 commit 5ce0a61
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
19 changes: 8 additions & 11 deletions CRM/Contact/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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'])) {
Expand Down
46 changes: 36 additions & 10 deletions tests/phpunit/CRM/Contact/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 5ce0a61

Please sign in to comment.