Commit | Line | Data |
---|---|---|
e3a78cba | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
e3a78cba | 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 | | |
e3a78cba | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * Test PaymentEdit form submission | |
14 | */ | |
15 | class CRM_Financial_Form_PaymentEditTest extends CiviUnitTestCase { | |
16 | ||
17 | protected $_individualID; | |
18 | ||
19 | /** | |
20 | * Setup function. | |
21 | */ | |
5d345864 | 22 | public function setUp(): void { |
e3a78cba | 23 | parent::setUp(); |
24 | $this->createLoggedInUser(); | |
25 | ||
26 | $this->_individualID = $this->individualCreate(); | |
27 | } | |
28 | ||
29 | /** | |
30 | * Clean up after each test. | |
d8a1b674 | 31 | * |
e3a78cba | 32 | */ |
dd09ee0c | 33 | public function tearDown(): void { |
e3a78cba | 34 | $this->quickCleanUpFinancialEntities(); |
9099cab3 | 35 | $this->quickCleanup(['civicrm_note', 'civicrm_uf_match', 'civicrm_address']); |
f8c56ac7 | 36 | parent::tearDown(); |
e3a78cba | 37 | } |
38 | ||
39 | /** | |
40 | * Test the submit function of payment edit form. | |
d8a1b674 | 41 | * |
42 | * @throws \CRM_Core_Exception | |
43 | * @throws \CiviCRM_API3_Exception | |
44 | * @throws \Civi\Payment\Exception\PaymentProcessorException | |
e3a78cba | 45 | */ |
d8a1b674 | 46 | public function testSubmitOnPaymentInstrumentChange(): void { |
e3a78cba | 47 | // First create a contribution using 'Check' as payment instrument |
f8c56ac7 | 48 | $form = $this->getFormObject('CRM_Contribute_Form_Contribution', [ |
e3a78cba | 49 | 'total_amount' => 50, |
6298b166 | 50 | 'receive_date' => '2015-04-21 23:27:00', |
e3a78cba | 51 | 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), |
e3a78cba | 52 | 'contact_id' => $this->_individualID, |
650b79b4 | 53 | 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'), |
e3a78cba | 54 | 'check_number' => '123XA', |
55 | 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), | |
f8c56ac7 EM |
56 | ]); |
57 | $form->postProcess(); | |
e3a78cba | 58 | // fetch the financial trxn record later used in setting default values of payment edit form |
9099cab3 | 59 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualID]); |
e3a78cba | 60 | $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE); |
61 | $financialTrxnInfo = $payments['transaction'][0]; | |
62 | ||
63 | // build parameters which changed payment instrument and tran date values | |
9099cab3 | 64 | $params = [ |
e3a78cba | 65 | 'id' => $financialTrxnInfo['id'], |
66 | 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit Card'), | |
67 | 'card_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', 'Visa'), | |
68 | 'pan_truncation' => 1111, | |
f8c56ac7 | 69 | 'trxn_id' => 'txn_12', |
e3a78cba | 70 | 'trxn_date' => date('Y-m-d H:i:s'), |
71 | 'contribution_id' => $contribution['id'], | |
9099cab3 | 72 | ]; |
e3a78cba | 73 | $form = new CRM_Financial_Form_PaymentEdit(); |
74 | $form->testSubmit($params); | |
75 | $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE); | |
9099cab3 CW |
76 | $expectedPaymentParams = [ |
77 | [ | |
e3a78cba | 78 | 'total_amount' => 50.00, |
f70a8e69 | 79 | 'financial_type' => 'Donation', |
e3a78cba | 80 | 'payment_instrument' => 'Check', |
81 | 'status' => 'Completed', | |
82 | 'receive_date' => '2015-04-21 23:27:00', | |
83 | 'check_number' => '123XA', | |
9099cab3 CW |
84 | ], |
85 | [ | |
e3a78cba | 86 | 'total_amount' => -50.00, |
f70a8e69 | 87 | 'financial_type' => 'Donation', |
e3a78cba | 88 | 'payment_instrument' => 'Check', |
d8a1b674 | 89 | 'status' => 'Refunded Label**', |
e3a78cba | 90 | 'receive_date' => $params['trxn_date'], |
91 | 'check_number' => '123XA', | |
9099cab3 CW |
92 | ], |
93 | [ | |
e3a78cba | 94 | 'total_amount' => 50.00, |
f70a8e69 | 95 | 'financial_type' => 'Donation', |
e3a78cba | 96 | 'payment_instrument' => sprintf('Credit Card (Visa: %s)', $params['pan_truncation']), |
97 | 'status' => 'Completed', | |
98 | 'receive_date' => $params['trxn_date'], | |
9099cab3 CW |
99 | ], |
100 | ]; | |
e3a78cba | 101 | $this->assertEquals(3, count($payments['transaction'])); |
102 | foreach ($expectedPaymentParams as $key => $paymentParams) { | |
103 | foreach ($paymentParams as $fieldName => $expectedValue) { | |
104 | $this->assertEquals($expectedPaymentParams[$key][$fieldName], $payments['transaction'][$key][$fieldName]); | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
650b79b4 | 109 | /** |
110 | * Test to ensure that multiple check_numbers are concatenated | |
111 | * and stored in related contribution's check_number | |
112 | */ | |
f8c56ac7 | 113 | public function testSubmitOnCheckNumberChange(): void { |
650b79b4 | 114 | // CASE 1: Submit contribution using Check as payment instrument and check_number as '123XA' |
115 | $checkNumber1 = '123XA'; | |
116 | $checkPaymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'); | |
117 | // First create a contribution using 'Check' as payment instrument | |
f8c56ac7 | 118 | $form = $this->getFormObject('CRM_Contribute_Form_Contribution', [ |
650b79b4 | 119 | 'total_amount' => 50, |
120 | 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'), | |
650b79b4 | 121 | 'contact_id' => $this->_individualID, |
122 | 'payment_instrument_id' => $checkPaymentInstrumentID, | |
123 | 'check_number' => $checkNumber1, | |
124 | 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'), | |
f8c56ac7 EM |
125 | ]); |
126 | $form->postProcess(); | |
9099cab3 | 127 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualID]); |
650b79b4 | 128 | $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE); |
129 | $financialTrxnInfo = $payments['transaction'][0]; | |
130 | ||
131 | // CASE 2: Submit payment details via edit form and changed check_number to '456XA', | |
132 | // ensure that contribution's check_number has concatenated check-numbers | |
133 | $checkNumber2 = '456XA'; | |
134 | // build parameters which changed payment instrument and tran date values | |
9099cab3 | 135 | $params = [ |
650b79b4 | 136 | 'id' => $financialTrxnInfo['id'], |
137 | 'payment_instrument_id' => $checkPaymentInstrumentID, | |
138 | 'check_number' => $checkNumber2, | |
139 | 'trxn_date' => date('Y-m-d H:i:s'), | |
140 | 'contribution_id' => $contribution['id'], | |
9099cab3 | 141 | ]; |
650b79b4 | 142 | $form = new CRM_Financial_Form_PaymentEdit(); |
143 | $form->testSubmit($params); | |
9099cab3 CW |
144 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]); |
145 | $expectedConcatanatedCheckNumbers = implode(',', [$checkNumber1, $checkNumber2]); | |
650b79b4 | 146 | $this->assertEquals($expectedConcatanatedCheckNumbers, $contribution['check_number']); |
147 | ||
148 | // CASE 3: Submit payment details via edit form without any change, | |
149 | // ensure that contribution's check_number concatenated value isn't changed | |
150 | $form->testSubmit($params); | |
9099cab3 | 151 | $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]); |
650b79b4 | 152 | $this->assertEquals($expectedConcatanatedCheckNumbers, $contribution['check_number']); |
153 | } | |
154 | ||
e3a78cba | 155 | } |