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',
140 '_qf_PDF_upload' => $isLiveMode,
143 $form->_contactIds
= $contactIDs;
148 * Test contact tokens are resolved.
150 public function testContactTokensAreResolved(): void
{
151 $form = $this->getPDFForm([
152 'html_message' => '{contact.first_name}, {contact.email_greeting}',
153 ], [$this->contactId
]);
154 $processedMessage = $this->submitForm($form)['html'];
155 $this->assertStringContainsString('Logged In, Dear Logged In', $processedMessage);
159 * Test case tokens are resolved in pdf letter.
161 public function testCaseTokensAreResolved() : void
{
162 // @todo - find a better way to set case id....
163 $_REQUEST['caseid'] = $this->getCaseID();
164 $form = $this->getPDFForm([
165 'html_message' => '{contact.first_name}, {case.case_type_id:label} {case.' . $this->getCustomFieldName('text') . '}',
166 ], [$this->contactId
]);
167 $processedMessage = $this->submitForm($form)['html'];
168 $this->assertStringContainsString('Logged In, Housing Support bb', $processedMessage);
176 protected function getCaseID(): int {
177 if (!isset($this->ids
['Case'][0])) {
178 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
179 $this->createCustomGroupWithFieldOfType(['extends' => 'Case']);
180 $this->ids
['Case'][0] = $this->callAPISuccess('Case', 'create', [
181 'case_type_id' => 'housing_support',
182 'activity_subject' => 'Case Subject',
183 'client_id' => $this->getContactID(),
185 'subject' => 'Case Subject',
186 'start_date' => '2021-07-23 15:39:20',
187 // Note end_date is inconsistent with status Ongoing but for the
188 // purposes of testing tokens is ok. Creating it with status Resolved
189 // then ignores our known fixed end date.
190 'end_date' => '2021-07-26 18:07:20',
192 'details' => 'case details',
193 'activity_details' => 'blah blah',
195 $this->getCustomFieldName('text') => 'bb',
198 return $this->ids
['Case'][0];
204 protected function getContactID(): int {
205 if (!isset($this->ids
['Contact'][0])) {
206 $this->ids
['Contact'][0] = $this->individualCreate();
208 return $this->ids
['Contact'][0];