4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
14 * Test class for CRM_Contact_Form_Task_PDFLetterCommon.
18 class CRM_Contact_Form_Task_PDFLetterCommonTest
extends CiviUnitTestCase
{
30 protected function setUp(): void
{
32 $this->contactId
= $this->createLoggedInUser();
36 * Test the pdf filename is assigned as expected.
38 * @param string|null $pdfFileName
39 * Value for pdf_file_name param.
40 * @param string|null $activitySubject
41 * Value of the subject of the activity.
42 * @param bool|null $isLiveMode
43 * TRUE when the form is in live mode, NULL when it is a preview.
44 * @param string $expectedFilename
45 * Expected filename assigned to the pdf.
47 * @dataProvider getFilenameCases
49 public function testFilenameIsAssigned(?
string $pdfFileName, ?
string $activitySubject, ?
bool $isLiveMode, string $expectedFilename): void
{
50 $form = $this->getPDFForm([
51 'pdf_file_name' => $pdfFileName,
52 'subject' => $activitySubject,
53 ], [$this->contactId
], $isLiveMode);
54 $fileNameAssigned = $this->submitForm($form)['fileName'];
55 $this->assertEquals($expectedFilename, $fileNameAssigned);
59 * DataProvider for testFilenameIsAssigned.
62 * Array with the test information.
64 public function getFilenameCases(): array {
68 'FilenameInActivitySubject',
70 'FilenameInParam_preview',
74 'FilenameInActivitySubject',
80 'FilenameInActivitySubject',
82 'FilenameInActivitySubject_preview',
86 'FilenameInActivitySubject',
88 'FilenameInActivitySubject',
106 * @param \CRM_Core_Form $form
110 protected function submitForm(CRM_Core_Form
$form) {
113 $form->postProcess();
115 catch (CRM_Core_Exception_PrematureExitException
$e) {
116 return $e->errorData
;
119 $this->fail('line should be unreachable');
123 * @param array $formValues
124 * @param array $contactIDs
125 * @param bool|null $isLiveMode
127 * @return \CRM_Contact_Form_Task_PDF
129 protected function getPDFForm(array $formValues, array $contactIDs, ?
bool $isLiveMode = TRUE): CRM_Contact_Form_Task_PDF
{
131 $_REQUEST['cid'] = $contactIDs[0];
132 /* @var CRM_Contact_Form_Task_PDF $form */
133 $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', array_merge([
134 'pdf_file_name' => 'pdf_file_name',
135 'subject' => 'subject',
136 'document_type' => 'pdf',
138 '_qf_PDF_upload' => $isLiveMode,
141 $form->_contactIds
= $contactIDs;
146 * Test contact tokens are resolved.
148 public function testContactTokensAreResolved(): void
{
149 $form = $this->getPDFForm([
150 'html_message' => '{contact.first_name}, {contact.email_greeting}',
151 ], [$this->contactId
]);
152 $processedMessage = $this->submitForm($form)['html'];
153 $this->assertStringContainsString('Logged In, Dear Logged In', $processedMessage);