(NFC) (dev/core#878) Simplify copyright header (tests/*)
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / Task / PDFLetterCommonTest.php
CommitLineData
7ab8b45b 1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
7ab8b45b 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 |
7ab8b45b 9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * @package CiviCRM_APIv3
14 * @subpackage API_Contribution
15 * @group headless
16 */
17class CRM_Member_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {
18
19 /**
20 * Clean up after each test.
21 */
22 public function tearDown() {
23 $this->quickCleanUpFinancialEntities();
24 CRM_Utils_Hook::singleton()->reset();
25 }
26
27 /**
28 * Test token replacement for Print/Merge Task
29 */
30 public function testMembershipTokenReplacementInPDF() {
9099cab3 31 $membershipIds = $returnProperties = $categories = $expected = [];
7ab8b45b 32 list($tokens, $htmlMessage) = self::getSampleHTML();
33
9099cab3 34 $membershipDates = [
7ab8b45b 35 date('Y-m-d'),
36 date('Y-m-d', strtotime('-1 month')),
9099cab3 37 ];
7ab8b45b 38 // Create sample memberships with different dates.
39 foreach ($membershipDates as $date) {
40 $contactId = $this->individualCreate();
9099cab3 41 $membershipTypeID = $this->membershipTypeCreate([
7ab8b45b 42 'minimum_fee' => '100.00',
43 'member_of_contact_id' => $contactId,
9099cab3
CW
44 ]);
45 $params = [
7ab8b45b 46 'contact_id' => $contactId,
47 'membership_type_id' => $membershipTypeID,
48 'join_date' => $date,
49 'start_date' => $date,
50 'end_date' => date('Y-m-d', strtotime("{$date} +1 year")),
9099cab3 51 ];
7ab8b45b 52 $result = $this->callAPISuccess('membership', 'create', $params);
53 $membershipIds[] = $result['id'];
54 $params = array_merge($params,
9099cab3 55 [
7ab8b45b 56 'fee' => '100.00',
57 'type' => 'General',
58 'status' => 'New',
9099cab3 59 ]
7ab8b45b 60 );
61
62 // Form an expected array replacing tokens for each contact.
63 foreach ($tokens as $key => $val) {
64 if (CRM_Utils_String::endsWith($val, '_date')) {
65 $formattedDate = CRM_Utils_Date::customFormat($params[$val]);
66 $expected[$contactId][$val] = "{$key} - {$formattedDate}";
67 }
68 else {
69 $expected[$contactId][$val] = $params[$val];
70 }
71 }
72 }
73 $messageToken = CRM_Utils_Token::getTokens($htmlMessage);
74 $testHTML = CRM_Member_Form_Task_PDFLetterCommon::generateHTML($membershipIds,
75 $returnProperties,
76 NULL,
77 NULL,
78 $messageToken,
79 $htmlMessage,
80 $categories
81 );
82
83 // Assert all membership tokens are replaced correctly.
84 $expected = array_values($expected);
85 foreach ($expected as $key => $dateVal) {
86 foreach ($tokens as $text => $token) {
87 $this->assertContains($dateVal[$token], $testHTML[$key]);
88 }
89 }
90 }
91
92 /**
93 * Generate sample HTML for testing.
94 */
95 public static function getSampleHTML() {
9099cab3 96 $tokens = [
7ab8b45b 97 'Test Fee' => 'fee',
98 'Test Type' => 'type',
99 'Test Status' => 'status',
100 'Test Join Date' => 'join_date',
101 'Test Start Date' => 'start_date',
102 'Test End Date' => 'end_date',
9099cab3 103 ];
7ab8b45b 104
105 $html = '';
106 foreach ($tokens as $key => $val) {
107 $html .= "<p>{$key} - {membership.{$val}}</p>";
108 }
9099cab3 109 return [$tokens, $html];
7ab8b45b 110 }
111
112}