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
{
20 use CRMTraits_Custom_CustomDataTrait
;
32 protected function setUp(): void
{
34 $this->contactId
= $this->createLoggedInUser();
38 * Test the pdf filename is assigned as expected.
40 * @param string|null $pdfFileName
41 * Value for pdf_file_name param.
42 * @param string|null $activitySubject
43 * Value of the subject of the activity.
44 * @param bool|null $isLiveMode
45 * TRUE when the form is in live mode, NULL when it is a preview.
46 * @param string $expectedFilename
47 * Expected filename assigned to the pdf.
49 * @dataProvider getFilenameCases
51 public function testFilenameIsAssigned(?
string $pdfFileName, ?
string $activitySubject, ?
bool $isLiveMode, string $expectedFilename): void
{
52 $form = $this->getPDFForm([
53 'pdf_file_name' => $pdfFileName,
54 'subject' => $activitySubject,
55 ], [$this->contactId
], $isLiveMode);
56 $fileNameAssigned = $this->submitForm($form)['fileName'];
57 $this->assertEquals($expectedFilename, $fileNameAssigned);
61 * DataProvider for testFilenameIsAssigned.
64 * Array with the test information.
66 public function getFilenameCases(): array {
70 'FilenameInActivitySubject',
72 'FilenameInParam_preview',
76 'FilenameInActivitySubject',
82 'FilenameInActivitySubject',
84 'FilenameInActivitySubject_preview',
88 'FilenameInActivitySubject',
90 'FilenameInActivitySubject',
108 * @param \CRM_Core_Form $form
112 protected function submitForm(CRM_Core_Form
$form) {
115 $form->postProcess();
117 catch (CRM_Core_Exception_PrematureExitException
$e) {
118 return $e->errorData
;
121 $this->fail('line should be unreachable');
125 * @param array $formValues
126 * @param array $contactIDs
127 * @param bool|null $isLiveMode
129 * @return \CRM_Contact_Form_Task_PDF
131 protected function getPDFForm(array $formValues, array $contactIDs, ?
bool $isLiveMode = TRUE): CRM_Contact_Form_Task_PDF
{
133 $_REQUEST['cid'] = $contactIDs[0];
134 /* @var CRM_Contact_Form_Task_PDF $form */
135 $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', array_merge([
136 'pdf_file_name' => 'pdf_file_name',
137 'subject' => 'subject',
138 'document_type' => 'pdf',
139 '_qf_button_name' => ('_qf_PDF_upload' . (!$isLiveMode ?
'_preview' : '')),
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);
157 * Test case tokens are resolved in pdf letter.
159 public function testCaseTokensAreResolved() : void
{
160 // @todo - find a better way to set case id....
161 $_REQUEST['caseid'] = $this->getCaseID();
162 $form = $this->getPDFForm([
163 'html_message' => '{contact.first_name}, {case.case_type_id:label} {case.' . $this->getCustomFieldName('text') . '}',
164 ], [$this->contactId
]);
165 $processedMessage = $this->submitForm($form)['html'];
166 $this->assertStringContainsString('Logged In, Housing Support bb', $processedMessage);
174 protected function getCaseID(): int {
175 if (!isset($this->ids
['Case'][0])) {
176 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
177 $this->createCustomGroupWithFieldOfType(['extends' => 'Case']);
178 $this->ids
['Case'][0] = $this->callAPISuccess('Case', 'create', [
179 'case_type_id' => 'housing_support',
180 'activity_subject' => 'Case Subject',
181 'client_id' => $this->getContactID(),
183 'subject' => 'Case Subject',
184 'start_date' => '2021-07-23 15:39:20',
185 // Note end_date is inconsistent with status Ongoing but for the
186 // purposes of testing tokens is ok. Creating it with status Resolved
187 // then ignores our known fixed end date.
188 'end_date' => '2021-07-26 18:07:20',
190 'details' => 'case details',
191 'activity_details' => 'blah blah',
193 $this->getCustomFieldName('text') => 'bb',
196 return $this->ids
['Case'][0];
202 protected function getContactID(): int {
203 if (!isset($this->ids
['Contact'][0])) {
204 $this->ids
['Contact'][0] = $this->individualCreate();
206 return $this->ids
['Contact'][0];