(NFC) (dev/core#878) Simplify copyright header (tests/*)
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Task / PrintDocumentTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test class for CRM_Contact_Form_Task_PDFLetterCommon.
14 * @group headless
15 */
16 class CRM_Contact_Form_Task_PrintDocumentTest extends CiviUnitTestCase {
17
18 protected $_docTypes = NULL;
19
20 protected $_contactIds = NULL;
21
22 protected function setUp() {
23 parent::setUp();
24 $this->_contactIds = [
25 $this->individualCreate(['first_name' => 'Antonia', 'last_name' => 'D`souza']),
26 $this->individualCreate(['first_name' => 'Anthony', 'last_name' => 'Collins']),
27 ];
28 $this->_docTypes = CRM_Core_SelectValues::documentApplicationType();
29 }
30
31 /**
32 * Test the documents got token replaced rightfully.
33 */
34 public function testPrintDocument() {
35 foreach (['docx', 'odt'] as $docType) {
36 $formValues = [
37 'document_file' => [
38 'name' => __DIR__ . "/sample_documents/Template.$docType",
39 'type' => $this->_docTypes[$docType],
40 ],
41 ];
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) {
53 $html = [];
54 $form = new CRM_Contact_Form_Task_PDFLetterCommon();
55 list($formValues, $categories, $html_message, $messageToken, $returnProperties) = $form->processMessageTemplate($formValues);
56 list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
57
58 foreach ($this->_contactIds as $item => $contactId) {
59 $params = ['contact_id' => $contactId];
60 list($contact) = CRM_Utils_Token::getTokenDetails($params,
61 $returnProperties,
62 FALSE,
63 FALSE,
64 NULL,
65 $messageToken,
66 'CRM_Contact_Form_Task_PDFLetterCommon'
67 );
68 $html[] = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
69 }
70
71 $fileName = pathinfo($formValues['document_file_path'], PATHINFO_FILENAME) . '.' . $type;
72 $returnContent = CRM_Utils_PDF_Document::printDocuments($html, $fileName, $type, $zip, TRUE);
73 $returnContent = strip_tags($returnContent);
74
75 $this->assertTrue(strpos($returnContent, 'Hello Antonia D`souza') !== 0);
76 $this->assertTrue(strpos($returnContent, 'Hello Anthony Collins') !== 0);
77 }
78
79 }