Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] dev/core#2790 dev/core#2814 Start migration to MessageTemplate::render #21335

Merged
merged 1 commit into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}