Merge pull request #10171 from seamuslee001/CRM-20427
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Task / PrintDocumentTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for CRM_Contact_Form_Task_PDFLetterCommon.
30 * @group headless
31 */
32 class CRM_Contact_Form_Task_PrintDocumentTest extends CiviUnitTestCase {
33
34 protected $_docTypes = NULL;
35
36 protected $_contactIds = NULL;
37
38 protected function setUp() {
39 parent::setUp();
40 $this->_contactIds = array(
41 $this->individualCreate(array('first_name' => 'Antonia', 'last_name' => 'D`souza')),
42 $this->individualCreate(array('first_name' => 'Anthony', 'last_name' => 'Collins')),
43 );
44 $this->_docTypes = CRM_Core_SelectValues::documentApplicationType();
45 }
46
47 /**
48 * Test the documents got token replaced rightfully.
49 */
50 public function testPrintDocument() {
51 foreach (array('docx', 'odt') as $docType) {
52 $formValues = array(
53 'document_file' => array(
54 'name' => __DIR__ . "/sample_documents/Template.$docType",
55 'type' => $this->_docTypes[$docType],
56 ),
57 );
58 $this->_testDocumentContent($formValues, $docType);
59 }
60 }
61
62 /**
63 * Assert the content of document
64 *
65 * @param array $formValues
66 * @param array $type
67 */
68 public function _testDocumentContent($formValues, $type) {
69 $html = array();
70 $form = new CRM_Contact_Form_Task_PDFLetterCommon();
71 list($formValues, $categories, $html_message, $messageToken, $returnProperties) = $form->processMessageTemplate($formValues);
72 list($html_message, $zip) = CRM_Utils_PDF_Document::unzipDoc($formValues['document_file_path'], $formValues['document_type']);
73
74 foreach ($this->_contactIds as $item => $contactId) {
75 $params = array('contact_id' => $contactId);
76 list($contact) = CRM_Utils_Token::getTokenDetails($params,
77 $returnProperties,
78 FALSE,
79 FALSE,
80 NULL,
81 $messageToken,
82 'CRM_Contact_Form_Task_PDFLetterCommon'
83 );
84 $html[] = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken);
85 }
86
87 $fileName = pathinfo($formValues['document_file_path'], PATHINFO_FILENAME) . '.' . $type;
88 $returnContent = CRM_Utils_PDF_Document::printDocuments($html, $fileName, $type, $zip, TRUE);
89 $returnContent = strip_tags($returnContent);
90
91 $this->assertTrue(strpos($returnContent, 'Hello Antonia D`souza') !== 0);
92 $this->assertTrue(strpos($returnContent, 'Hello Anthony Collins') !== 0);
93 }
94
95 }