Merge pull request #21474 from MegaphoneJon/core-2318
[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 'buttons' => [
140 '_qf_PDF_upload' => $isLiveMode,
141 ],
142 ], $formValues));
143 $form->_contactIds = $contactIDs;
144 return $form;
145 }
146
147 /**
148 * Test contact tokens are resolved.
149 */
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);
156 }
157
158 /**
159 * Test case tokens are resolved in pdf letter.
160 */
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);
169 }
170
171 /**
172 * Get case ID.
173 *
174 * @return int
175 */
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(),
184 'status_id' => 1,
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',
191 'medium_id' => 2,
192 'details' => 'case details',
193 'activity_details' => 'blah blah',
194 'sequential' => 1,
195 $this->getCustomFieldName('text') => 'bb',
196 ])['id'];
197 }
198 return $this->ids['Case'][0];
199 }
200
201 /**
202 * @return int
203 */
204 protected function getContactID(): int {
205 if (!isset($this->ids['Contact'][0])) {
206 $this->ids['Contact'][0] = $this->individualCreate();
207 }
208 return $this->ids['Contact'][0];
209 }
210
211 }