Commit | Line | Data |
---|---|---|
2bd6b6cf | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
2bd6b6cf | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
2bd6b6cf | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
0ceb63d9 | 13 | * Test class for CRM_Contact_Form_Task_PDF. |
2bd6b6cf | 14 | * @group headless |
15 | */ | |
16 | class CRM_Contact_Form_Task_PrintDocumentTest extends CiviUnitTestCase { | |
17 | ||
18 | protected $_docTypes = NULL; | |
19 | ||
20 | protected $_contactIds = NULL; | |
21 | ||
faba1457 | 22 | protected function setUp(): void { |
2bd6b6cf | 23 | parent::setUp(); |
9099cab3 CW |
24 | $this->_contactIds = [ |
25 | $this->individualCreate(['first_name' => 'Antonia', 'last_name' => 'D`souza']), | |
26 | $this->individualCreate(['first_name' => 'Anthony', 'last_name' => 'Collins']), | |
27 | ]; | |
2bd6b6cf | 28 | $this->_docTypes = CRM_Core_SelectValues::documentApplicationType(); |
29 | } | |
30 | ||
31 | /** | |
32 | * Test the documents got token replaced rightfully. | |
33 | */ | |
ad1e89e3 | 34 | public function testPrintDocument(): void { |
9099cab3 CW |
35 | foreach (['docx', 'odt'] as $docType) { |
36 | $formValues = [ | |
37 | 'document_file' => [ | |
2bd6b6cf | 38 | 'name' => __DIR__ . "/sample_documents/Template.$docType", |
39 | 'type' => $this->_docTypes[$docType], | |
9099cab3 CW |
40 | ], |
41 | ]; | |
2bd6b6cf | 42 | $this->_testDocumentContent($formValues, $docType); |
43 | } | |
44 | } | |
45 | ||
46 | /** | |
47 | * Assert the content of document | |
48 | * | |
49 | * @param array $formValues | |
50 | * @param array $type | |
51 | */ | |
52 | public function _testDocumentContent($formValues, $type) { | |
9099cab3 | 53 | $html = []; |
0ceb63d9 EM |
54 | /* @var CRM_Contact_Form_Task_PDF $form */ |
55 | $form = $this->getFormObject('CRM_Contact_Form_Task_PDF', [], NULL, [ | |
56 | 'radio_ts' => 'ts_sel', | |
57 | 'task' => CRM_Member_Task::PDF_LETTER, | |
58 | ]); | |
59 | list($formValues, $html_message, $messageToken, $returnProperties) = $form->processMessageTemplate($formValues); | |
2bd6b6cf | 60 | list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']); |
61 | ||
62 | foreach ($this->_contactIds as $item => $contactId) { | |
ad1e89e3 | 63 | $html[] = CRM_Core_BAO_MessageTemplate::renderTemplate(['messageTemplate' => ['msg_html' => $html_message], 'contactId' => $contactId, 'disableSmarty' => TRUE])['html']; |
2bd6b6cf | 64 | } |
65 | ||
63c8a9ba TO |
66 | $fileName = pathinfo($formValues['document_file_path'], PATHINFO_FILENAME) . '.' . $type; |
67 | $returnContent = CRM_Utils_PDF_Document::printDocuments($html, $fileName, $type, $zip, TRUE); | |
2bd6b6cf | 68 | $returnContent = strip_tags($returnContent); |
69 | ||
70 | $this->assertTrue(strpos($returnContent, 'Hello Antonia D`souza') !== 0); | |
71 | $this->assertTrue(strpos($returnContent, 'Hello Anthony Collins') !== 0); | |
72 | } | |
73 | ||
74 | } |