Merge pull request #20807 from davidjosephhayes/wordpress-profile-signon-2
[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 /**
21 * Contact ID.
22 *
23 * @var int
24 */
25 protected $contactId;
26
27 /**
28 * {@inheritdoc}
29 */
30 protected function setUp(): void {
31 parent::setUp();
32 $this->contactId = $this->createLoggedInUser();
33 }
34
35 /**
36 * Test the pdf filename is assigned as expected.
37 *
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.
46 *
47 * @dataProvider getFilenameCases
48 */
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);
56 }
57
58 /**
59 * DataProvider for testFilenameIsAssigned.
60 *
61 * @return array
62 * Array with the test information.
63 */
64 public function getFilenameCases(): array {
65 return [
66 [
67 'FilenameInParam',
68 'FilenameInActivitySubject',
69 NULL,
70 'FilenameInParam_preview',
71 ],
72 [
73 'FilenameInParam',
74 'FilenameInActivitySubject',
75 TRUE,
76 'FilenameInParam',
77 ],
78 [
79 NULL,
80 'FilenameInActivitySubject',
81 NULL,
82 'FilenameInActivitySubject_preview',
83 ],
84 [
85 NULL,
86 'FilenameInActivitySubject',
87 TRUE,
88 'FilenameInActivitySubject',
89 ],
90 [
91 NULL,
92 NULL,
93 NULL,
94 'CiviLetter_preview',
95 ],
96 [
97 NULL,
98 NULL,
99 TRUE,
100 'CiviLetter',
101 ],
102 ];
103 }
104
105 /**
106 * @param \CRM_Core_Form $form
107 *
108 * @return int|mixed
109 */
110 protected function submitForm(CRM_Core_Form $form) {
111 $form->preProcess();
112 try {
113 $form->postProcess();
114 }
115 catch (CRM_Core_Exception_PrematureExitException $e) {
116 return $e->errorData;
117
118 }
119 $this->fail('line should be unreachable');
120 }
121
122 /**
123 * @param array $formValues
124 * @param array $contactIDs
125 * @param bool|null $isLiveMode
126 *
127 * @return \CRM_Contact_Form_Task_PDF
128 */
129 protected function getPDFForm(array $formValues, array $contactIDs, ?bool $isLiveMode = TRUE): CRM_Contact_Form_Task_PDF {
130 // pretty cludgey.
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',
137 'buttons' => [
138 '_qf_PDF_upload' => $isLiveMode,
139 ],
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 }