| 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 | * @package CiviCRM_APIv3 |
| 14 | * @subpackage API_Contribution |
| 15 | * @group headless |
| 16 | */ |
| 17 | class 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() { |
| 31 | $membershipIds = $returnProperties = $categories = $expected = []; |
| 32 | list($tokens, $htmlMessage) = self::getSampleHTML(); |
| 33 | |
| 34 | $membershipDates = [ |
| 35 | date('Y-m-d'), |
| 36 | date('Y-m-d', strtotime('-1 month')), |
| 37 | ]; |
| 38 | // Create sample memberships with different dates. |
| 39 | foreach ($membershipDates as $date) { |
| 40 | $contactId = $this->individualCreate(); |
| 41 | $membershipTypeID = $this->membershipTypeCreate([ |
| 42 | 'minimum_fee' => '100.00', |
| 43 | 'member_of_contact_id' => $contactId, |
| 44 | ]); |
| 45 | $params = [ |
| 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")), |
| 51 | ]; |
| 52 | $result = $this->callAPISuccess('membership', 'create', $params); |
| 53 | $membershipIds[] = $result['id']; |
| 54 | $params = array_merge($params, |
| 55 | [ |
| 56 | 'fee' => '100.00', |
| 57 | 'type' => 'General', |
| 58 | 'status' => 'New', |
| 59 | ] |
| 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() { |
| 96 | $tokens = [ |
| 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', |
| 103 | ]; |
| 104 | |
| 105 | $html = ''; |
| 106 | foreach ($tokens as $key => $val) { |
| 107 | $html .= "<p>{$key} - {membership.{$val}}</p>"; |
| 108 | } |
| 109 | return [$tokens, $html]; |
| 110 | } |
| 111 | |
| 112 | } |