Merge pull request #15362 from yashodha/edit
[civicrm-core.git] / tests / phpunit / CRM / Financial / Form / PaymentEditTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 PaymentEdit form submission
30 */
31 class CRM_Financial_Form_PaymentEditTest extends CiviUnitTestCase {
32
33 protected $_individualID;
34
35 /**
36 * Setup function.
37 */
38 public function setUp() {
39 parent::setUp();
40 $this->createLoggedInUser();
41
42 $this->_individualID = $this->individualCreate();
43 }
44
45 /**
46 * Clean up after each test.
47 */
48 public function tearDown() {
49 $this->quickCleanUpFinancialEntities();
50 $this->quickCleanup(['civicrm_note', 'civicrm_uf_match', 'civicrm_address']);
51 }
52
53 /**
54 * Test the submit function of payment edit form.
55 */
56 public function testSubmitOnPaymentInstrumentChange() {
57 // First create a contribution using 'Check' as payment instrument
58 $form = new CRM_Contribute_Form_Contribution();
59 $form->testSubmit([
60 'total_amount' => 50,
61 'receive_date' => '2015-04-21 23:27:00',
62 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
63 'contact_id' => $this->_individualID,
64 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check'),
65 'check_number' => '123XA',
66 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
67 ],
68 CRM_Core_Action::ADD);
69 // fetch the financial trxn record later used in setting default values of payment edit form
70 $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualID]);
71 $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE);
72 $financialTrxnInfo = $payments['transaction'][0];
73
74 // build parameters which changed payment instrument and tran date values
75 $params = [
76 'id' => $financialTrxnInfo['id'],
77 'payment_instrument_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Credit Card'),
78 'card_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', 'Visa'),
79 'pan_truncation' => 1111,
80 'trnx_id' => 'txn_12AAAA',
81 'trxn_date' => date('Y-m-d H:i:s'),
82 'contribution_id' => $contribution['id'],
83 ];
84 $form = new CRM_Financial_Form_PaymentEdit();
85 $form->testSubmit($params);
86 $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE);
87 $expectedPaymentParams = [
88 [
89 'total_amount' => 50.00,
90 'financial_type' => 'Donation',
91 'payment_instrument' => 'Check',
92 'status' => 'Completed',
93 'receive_date' => '2015-04-21 23:27:00',
94 'check_number' => '123XA',
95 ],
96 [
97 'total_amount' => -50.00,
98 'financial_type' => 'Donation',
99 'payment_instrument' => 'Check',
100 'status' => 'Completed',
101 'receive_date' => $params['trxn_date'],
102 'check_number' => '123XA',
103 ],
104 [
105 'total_amount' => 50.00,
106 'financial_type' => 'Donation',
107 'payment_instrument' => sprintf('Credit Card (Visa: %s)', $params['pan_truncation']),
108 'status' => 'Completed',
109 'receive_date' => $params['trxn_date'],
110 ],
111 ];
112 $this->assertEquals(3, count($payments['transaction']));
113 foreach ($expectedPaymentParams as $key => $paymentParams) {
114 foreach ($paymentParams as $fieldName => $expectedValue) {
115 $this->assertEquals($expectedPaymentParams[$key][$fieldName], $payments['transaction'][$key][$fieldName]);
116 }
117 }
118 }
119
120 /**
121 * Test to ensure that multiple check_numbers are concatenated
122 * and stored in related contribution's check_number
123 */
124 public function testSubmitOnCheckNumberChange() {
125 // CASE 1: Submit contribution using Check as payment instrument and check_number as '123XA'
126 $checkNumber1 = '123XA';
127 $checkPaymentInstrumentID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', 'Check');
128 // First create a contribution using 'Check' as payment instrument
129 $form = new CRM_Contribute_Form_Contribution();
130 $form->testSubmit([
131 'total_amount' => 50,
132 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
133 'contact_id' => $this->_individualID,
134 'payment_instrument_id' => $checkPaymentInstrumentID,
135 'check_number' => $checkNumber1,
136 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
137 ],
138 CRM_Core_Action::ADD);
139 $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualID]);
140 $payments = CRM_Contribute_BAO_Contribution::getPaymentInfo($contribution['id'], 'contribute', TRUE);
141 $financialTrxnInfo = $payments['transaction'][0];
142
143 // CASE 2: Submit payment details via edit form and changed check_number to '456XA',
144 // ensure that contribution's check_number has concatenated check-numbers
145 $checkNumber2 = '456XA';
146 // build parameters which changed payment instrument and tran date values
147 $params = [
148 'id' => $financialTrxnInfo['id'],
149 'payment_instrument_id' => $checkPaymentInstrumentID,
150 'check_number' => $checkNumber2,
151 'trxn_date' => date('Y-m-d H:i:s'),
152 'contribution_id' => $contribution['id'],
153 ];
154 $form = new CRM_Financial_Form_PaymentEdit();
155 $form->testSubmit($params);
156 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]);
157 $expectedConcatanatedCheckNumbers = implode(',', [$checkNumber1, $checkNumber2]);
158 $this->assertEquals($expectedConcatanatedCheckNumbers, $contribution['check_number']);
159
160 // CASE 3: Submit payment details via edit form without any change,
161 // ensure that contribution's check_number concatenated value isn't changed
162 $form->testSubmit($params);
163 $contribution = $this->callAPISuccessGetSingle('Contribution', ['id' => $contribution['id']]);
164 $this->assertEquals($expectedConcatanatedCheckNumbers, $contribution['check_number']);
165 }
166
167 }