Merge branch '5.48' to master
[civicrm-core.git] / tests / phpunit / CRM / Financial / Form / PaymentEditTest.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 PaymentEdit form submission
14 */
15 class CRM_Financial_Form_PaymentEditTest extends CiviUnitTestCase {
16
17 protected $_individualID;
18
19 /**
20 * Setup function.
21 */
22 public function setUp(): void {
23 parent::setUp();
24 $this->createLoggedInUser();
25
26 $this->_individualID = $this->individualCreate();
27 }
28
29 /**
30 * Clean up after each test.
31 *
32 */
33 public function tearDown(): void {
34 $this->quickCleanUpFinancialEntities();
35 $this->quickCleanup(['civicrm_note', 'civicrm_uf_match', 'civicrm_address']);
36 parent::tearDown();
37 }
38
39 /**
40 * Test the submit function of payment edit form.
41 *
42 * @throws \CRM_Core_Exception
43 * @throws \CiviCRM_API3_Exception
44 * @throws \Civi\Payment\Exception\PaymentProcessorException
45 */
46 public function testSubmitOnPaymentInstrumentChange(): void {
47 // First create a contribution using 'Check' as payment instrument
48 $form = $this->getFormObject('CRM_Contribute_Form_Contribution', [
49 'total_amount' => 50,
50 'receive_date' => '2015-04-21 23:27:00',
51 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
52 'contact_id' => $this->_individualID,
53 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
54 'check_number' => '123XA',
55 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
56 ]);
57 $form->postProcess();
58 // fetch the financial trxn record later used in setting default values of payment edit form
59 $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualID]);
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
64 $params = [
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,
69 'trxn_id' => 'txn_12',
70 'trxn_date' => date('Y-m-d H:i:s'),
71 'contribution_id' => $contribution['id'],
72 ];
73 $form = new CRM_Financial_Form_PaymentEdit();
74 $form->testSubmit($params);
75 $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE);
76 $expectedPaymentParams = [
77 [
78 'total_amount' => 50.00,
79 'financial_type' => 'Donation',
80 'payment_instrument' => 'Check',
81 'status' => 'Completed',
82 'receive_date' => '2015-04-21 23:27:00',
83 'check_number' => '123XA',
84 ],
85 [
86 'total_amount' => -50.00,
87 'financial_type' => 'Donation',
88 'payment_instrument' => 'Check',
89 'status' => 'Refunded Label**',
90 'receive_date' => $params['trxn_date'],
91 'check_number' => '123XA',
92 ],
93 [
94 'total_amount' => 50.00,
95 'financial_type' => 'Donation',
96 'payment_instrument' => sprintf('Credit Card (Visa: %s)', $params['pan_truncation']),
97 'status' => 'Completed',
98 'receive_date' => $params['trxn_date'],
99 ],
100 ];
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
109 /**
110 * Test to ensure that multiple check_numbers are concatenated
111 * and stored in related contribution's check_number
112 */
113 public function testSubmitOnCheckNumberChange(): void {
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
118 $form = $this->getFormObject('CRM_Contribute_Form_Contribution', [
119 'total_amount' => 50,
120 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
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'),
125 ]);
126 $form->postProcess();
127 $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualID]);
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
135 $params = [
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'],
141 ];
142 $form = new CRM_Financial_Form_PaymentEdit();
143 $form->testSubmit($params);
144 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]);
145 $expectedConcatanatedCheckNumbers = implode(',', [$checkNumber1, $checkNumber2]);
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);
151 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]);
152 $this->assertEquals($expectedConcatanatedCheckNumbers, $contribution['check_number']);
153 }
154
155 }