Merge pull request #24115 from kcristiano/5.52-token
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Task / PDFLetterCommonTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 * Test class for CRM_Contact_Form_Task_PDFLetterCommon.
15 *
16 * @group headless
17 */
18 class CRM_Contact_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
19
20 use CRMTraits_Custom_CustomDataTrait;
21
22 /**
23 * Contact ID.
24 *
25 * @var int
26 */
27 protected $contactId;
28
29 /**
30 * {@inheritdoc}
31 */
32 protected function setUp(): void {
33 parent::setUp();
34 $this->contactId = $this->createLoggedInUser();
35 }
36
37 /**
38 * Test the pdf filename is assigned as expected.
39 *
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.
48 *
49 * @dataProvider getFilenameCases
50 */
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);
58 }
59
60 /**
61 * DataProvider for testFilenameIsAssigned.
62 *
63 * @return array
64 * Array with the test information.
65 */
66 public function getFilenameCases(): array {
67 return [
68 [
69 'FilenameInParam',
70 'FilenameInActivitySubject',
71 NULL,
72 'FilenameInParam_preview',
73 ],
74 [
75 'FilenameInParam',
76 'FilenameInActivitySubject',
77 TRUE,
78 'FilenameInParam',
79 ],
80 [
81 NULL,
82 'FilenameInActivitySubject',
83 NULL,
84 'FilenameInActivitySubject_preview',
85 ],
86 [
87 NULL,
88 'FilenameInActivitySubject',
89 TRUE,
90 'FilenameInActivitySubject',
91 ],
92 [
93 NULL,
94 NULL,
95 NULL,
96 'CiviLetter_preview',
97 ],
98 [
99 NULL,
100 NULL,
101 TRUE,
102 'CiviLetter',
103 ],
104 ];
105 }
106
107 /**
108 * @param \CRM_Core_Form $form
109 *
110 * @return int|mixed
111 */
112 protected function submitForm(CRM_Core_Form $form) {
113 $form->preProcess();
114 try {
115 $form->postProcess();
116 }
117 catch (CRM_Core_Exception_PrematureExitException $e) {
118 return $e->errorData;
119
120 }
121 $this->fail('line should be unreachable');
122 }
123
124 /**
125 * @param array $formValues
126 * @param array $contactIDs
127 * @param bool|null $isLiveMode
128 *
129 * @return \CRM_Contact_Form_Task_PDF
130 */
131 protected function getPDFForm(array $formValues, array $contactIDs, ?bool $isLiveMode = TRUE): CRM_Contact_Form_Task_PDF {
132 // pretty cludgey.
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' : '')),
140 ], $formValues));
141 $form->_contactIds = $contactIDs;
142 return $form;
143 }
144
145 /**
146 * Test contact tokens are resolved.
147 */
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);
154 }
155
156 /**
157 * Test case tokens are resolved in pdf letter.
158 */
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);
167 }
168
169 /**
170 * Get case ID.
171 *
172 * @return int
173 */
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(),
182 'status_id' => 1,
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',
189 'medium_id' => 2,
190 'details' => 'case details',
191 'activity_details' => 'blah blah',
192 'sequential' => 1,
193 $this->getCustomFieldName('text') => 'bb',
194 ])['id'];
195 }
196 return $this->ids['Case'][0];
197 }
198
199 /**
200 * @return int
201 */
202 protected function getContactID(): int {
203 if (!isset($this->ids['Contact'][0])) {
204 $this->ids['Contact'][0] = $this->individualCreate();
205 }
206 return $this->ids['Contact'][0];
207 }
208
209 }