CRM-20392 add unit test and fix for incorrect total used in line item creation code.
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / AdditionalPaymentTest.php
CommitLineData
61bfc595
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase {
36
37 /**
38 * Contact ID.
39 *
40 * @var int
41 */
42 protected $_individualId;
43
44 /**
45 * Parameters to create contribution.
46 *
47 * @var array
48 */
49 protected $_params;
50
51 /**
52 * Contribution ID.
53 *
54 * @var int
55 */
56 protected $_contributionId;
57
58 /**
59 * Parameters to create payment processor.
60 *
61 * @var array
62 */
63 protected $_processorParams = array();
64
65 /**
66 * Payment instrument mapping.
67 *
68 * @var array
69 */
70 protected $paymentInstruments = array();
71
72 /**
73 * Dummy payment processor.
74 *
75 * @var CRM_Core_Payment_Dummy
76 */
77 protected $paymentProcessor;
78
79 /**
80 * Payment processor ID.
81 *
82 * @var int
83 */
84 protected $paymentProcessorID;
85
86 /**
87 * Setup function.
88 */
89 public function setUp() {
90 parent::setUp();
91 $this->createLoggedInUser();
92
93 $this->_individualId = $this->individualCreate();
94 $this->_params = array(
95 'total_amount' => 100,
96 'currency' => 'USD',
97 'contact_id' => $this->_individualId,
98 'financial_type_id' => 1,
99 );
100 $this->_processorParams = array(
101 'domain_id' => 1,
102 'name' => 'Dummy',
103 'payment_processor_type_id' => 10,
104 'financial_account_id' => 12,
105 'is_active' => 1,
106 'user_name' => '',
107 'url_site' => 'http://dummy.com',
108 'url_recur' => 'http://dummy.com',
109 'billing_mode' => 1,
110 );
111
112 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
113 $this->paymentInstruments = $instruments['values'];
114
115 $this->paymentProcessor = $this->dummyProcessorCreate();
116 $processor = $this->paymentProcessor->getPaymentProcessor();
117 $this->paymentProcessorID = $processor['id'];
118 }
119
120 /**
121 * Clean up after each test.
122 */
123 public function tearDown() {
124 $this->quickCleanUpFinancialEntities();
125 parent::tearDown();
126 }
127
128 /**
129 * Test the submit function that completes the partially paid Contribution using Credit Card.
130
131 public function testAddPaymentUsingCreditCardForPartialyPaidContribution() {
132 $this->createContribution('Partially paid');
133
134 // pay additional amount by using Credit Card
135 $this->submitPayment(70, 'live');
136 $this->checkResults(array(30, 70), 2);
137 }
138 */
139
140 /**
141 * Test the submit function that completes the partially paid Contribution.
142 */
143 public function testAddPaymentForPartialyPaidContribution() {
144 $this->createContribution('Partially paid');
145
146 // pay additional amount
147 $this->submitPayment(70);
148 $this->checkResults(array(30, 70), 2);
149 }
150
151 /**
152 * Test the submit function that completes the partially paid Contribution with multiple payments.
153 */
154 public function testMultiplePaymentForPartialyPaidContribution() {
155 $this->createContribution('Partially paid');
156
157 // pay additional amount
158 $this->submitPayment(50);
159 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
160 $this->assertEquals('Partially paid', $contribution['contribution_status']);
161
162 // pay additional amount
163 $this->submitPayment(20);
164 $this->checkResults(array(30, 50, 20), 3);
165 }
166 /**
167 * Test the submit function that completes the partially paid Contribution with multiple payments.
168 *
169 public function testMultiplePaymentForPartialyPaidContributionWithOneCreditCardPayment() {
170 $this->createContribution('Partially paid');
171
172 // pay additional amount
173 $this->submitPayment(50);
174 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
175 $this->assertEquals('Partially paid', $contribution['contribution_status']);
176
177 // pay additional amount by using credit card
178 $this->submitPayment(20, 'live');
179 $this->checkResults(array(30, 50, 20), 3);
180 }
181
182
183 /**
184 * Test the submit function that completes the pending pay later Contribution using Credit Card.
185 *
186 public function testAddPaymentUsingCreditCardForPendingPayLaterContribution() {
187 $this->createContribution('Pending');
188
189 // pay additional amount by using Credit Card
190 $this->submitPayment(100, 'live');
191 $this->checkResults(array(100), 1);
192 }
193 */
194
195 /**
196 * Test the submit function that completes the pending pay later Contribution.
197 */
198 public function testAddPaymentForPendingPayLaterContribution() {
199 $this->createContribution('Pending');
200
201 // pay additional amount
202 $this->submitPayment(70);
203 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
204 $this->assertEquals('Partially paid', $contribution['contribution_status']);
205
206 // pay additional amount
207 $this->submitPayment(30);
208 $this->checkResults(array(30, 70), 2);
209 }
210
211 /**
212 * Test the submit function that completes the pending pay later Contribution with multiple payments.
213 */
214 public function testMultiplePaymentForPendingPayLaterContribution() {
215 $this->createContribution('Pending');
216
217 // pay additional amount
218 $this->submitPayment(40);
219 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
220 $this->assertEquals('Partially paid', $contribution['contribution_status']);
221
222 $this->submitPayment(20);
223 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
224 $this->assertEquals('Partially paid', $contribution['contribution_status']);
225
226 $this->submitPayment(30);
227 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
228 $this->assertEquals('Partially paid', $contribution['contribution_status']);
229
230 $this->submitPayment(10);
231 $this->checkResults(array(40, 20, 30, 10), 4);
232 }
233
234 /**
235 * Test the submit function that completes the pending pay later Contribution with multiple payments.
236 *
237 public function testMultiplePaymentForPendingPayLaterContributionWithOneCreditCardPayment() {
238 $this->createContribution('Pending');
239
240 // pay additional amount
241 $this->submitPayment(50);
242 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
243 $this->assertEquals('Partially paid', $contribution['contribution_status']);
244
245 $this->submitPayment(20, 'live');
246 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
247 $this->assertEquals('Partially paid', $contribution['contribution_status']);
248
249 $this->submitPayment(20);
250 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
251 $this->assertEquals('Partially paid', $contribution['contribution_status']);
252
253 $this->submitPayment(10, 'live');
254 $this->checkResults(array(50, 20, 20, 10), 4);
255 }
256 */
257
258 /**
259 * Function to create pending pay later or partially paid conntribution.
260 *
261 * @param string $typeofContribution
262 *
263 */
264 public function createContribution($typeofContribution = 'Pending') {
265 if ($typeofContribution == 'Partially paid') {
266 $contributionParams = array_merge($this->_params, array(
267 'partial_payment_total' => 100.00,
268 'partial_amount_pay' => 30,
269 'contribution_status_id' => 1,
270 ));
271 }
272 elseif ($typeofContribution == 'Pending') {
273 $contributionParams = array_merge($this->_params, array(
274 'contribution_status_id' => 2,
275 'is_pay_later' => 1,
276 ));
277 }
278 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
279 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contribution['id']));
280 $this->assertNotEmpty($contribution);
281 $this->assertEquals($typeofContribution, $contribution['contribution_status']);
282 $this->_contributionId = $contribution['id'];
283 }
284
285 /**
286 * Function to submit payments for contribution.
287 *
288 * @param float $amount
289 * Payment Amount
290 * @param string $mode
291 * Mode of Payment
292 *
293 */
294 public function submitPayment($amount, $mode = NULL) {
295 $form = new CRM_Contribute_Form_AdditionalPayment();
296
297 $submitParams = array(
298 'contribution_id' => $this->_contributionId,
299 'contact_id' => $this->_individualId,
300 'total_amount' => $amount,
301 'currency' => 'USD',
302 'financial_type_id' => 1,
303 'receive_date' => '04/21/2015',
304 'receive_date_time' => '11:27PM',
305 'trxn_date' => '2017-04-11 13:05:11',
306 );
307 if ($mode) {
308 $submitParams += array(
309 'payment_instrument_id' => array_search('Credit card', $this->paymentInstruments),
310 'payment_processor_id' => $this->paymentProcessorID,
311 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
312 'credit_card_number' => '411111111111111',
313 'cvv2' => 234,
314 'credit_card_type' => 'Visa',
315 'billing_city-5' => 'Vancouver',
316 'billing_state_province_id-5' => 1059,
317 'billing_postal_code-5' => 1321312,
318 'billing_country_id-5' => 1228,
319 );
320 }
321 else {
322 $submitParams += array(
323 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
324 'check_number' => 'check-12345',
325 );
326 }
327 $form->testSubmit($submitParams, $mode);
328 }
329
330 /**
331 * Function to check result.
332 *
333 * @param array $amounts
334 * Array of payment amount for contribution
335 * @param int $count
336 * Number payment for contribution
337 *
338 */
339 public function checkResults($amounts, $count) {
340 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
341 $this->assertNotEmpty($contribution);
342 $this->assertEquals('Completed', $contribution['contribution_status']);
343
344 $this->callAPISuccessGetCount('EntityFinancialTrxn', array(
345 'entity_table' => "civicrm_contribution",
346 'entity_id' => $this->_contributionId,
347 'financial_trxn_id.is_payment' => 1,
348 'financial_trxn_id.total_amount' => array('IN' => $amounts),
349 ), $count);
350 }
351
352}