CRM-20432: Bug is no more reproducible. Removed patch which was updating the Membersh...
[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.
80c9b98c 130 */
61bfc595
PN
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 }
61bfc595
PN
138
139 /**
140 * Test the submit function that completes the partially paid Contribution.
141 */
142 public function testAddPaymentForPartialyPaidContribution() {
143 $this->createContribution('Partially paid');
144
145 // pay additional amount
146 $this->submitPayment(70);
147 $this->checkResults(array(30, 70), 2);
148 }
149
150 /**
151 * Test the submit function that completes the partially paid Contribution with multiple payments.
152 */
153 public function testMultiplePaymentForPartialyPaidContribution() {
154 $this->createContribution('Partially paid');
155
156 // pay additional amount
157 $this->submitPayment(50);
158 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
159 $this->assertEquals('Partially paid', $contribution['contribution_status']);
160
161 // pay additional amount
162 $this->submitPayment(20);
163 $this->checkResults(array(30, 50, 20), 3);
164 }
80c9b98c 165
61bfc595
PN
166 /**
167 * Test the submit function that completes the partially paid Contribution with multiple payments.
80c9b98c
PN
168 */
169 public function testMultiplePaymentForPartiallyPaidContributionWithOneCreditCardPayment() {
61bfc595
PN
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
61bfc595
PN
182 /**
183 * Test the submit function that completes the pending pay later Contribution using Credit Card.
80c9b98c 184 */
61bfc595
PN
185 public function testAddPaymentUsingCreditCardForPendingPayLaterContribution() {
186 $this->createContribution('Pending');
187
188 // pay additional amount by using Credit Card
189 $this->submitPayment(100, 'live');
190 $this->checkResults(array(100), 1);
191 }
61bfc595
PN
192
193 /**
194 * Test the submit function that completes the pending pay later Contribution.
195 */
196 public function testAddPaymentForPendingPayLaterContribution() {
197 $this->createContribution('Pending');
198
199 // pay additional amount
200 $this->submitPayment(70);
201 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
202 $this->assertEquals('Partially paid', $contribution['contribution_status']);
203
204 // pay additional amount
205 $this->submitPayment(30);
206 $this->checkResults(array(30, 70), 2);
207 }
208
425f113a
AP
209 /**
210 * Test the Membership status after completing the pending pay later Contribution.
211 */
212 public function testMembershipStatusAfterCompletingPayLaterContribution() {
213 $this->createContribution('Pending');
214 $membership = $this->createPendingMembershipAndRecordContribution($this->_contributionId);
215 // pay additional amount
216 $this->submitPayment(100);
217 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
218 $contributionMembership = $this->callAPISuccessGetSingle('Membership', array('id' => $membership["id"]));
219 $membershipStatus = $this->callAPISuccessGetSingle('MembershipStatus', array('id' => $contributionMembership["status_id"]));
220 $this->assertEquals('Current', $membershipStatus['name']);
221 }
222
223 private function createPendingMembershipAndRecordContribution($contributionId) {
224 $this->_individualId = $this->individualCreate();
225 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array(
226 'domain_id' => 1,
227 'name' => "AnnualFixed",
228 'member_of_contact_id' => 1,
229 'duration_unit' => "year",
230 'duration_interval' => 1,
231 'period_type' => "fixed",
232 'fixed_period_start_day' => "101",
233 'fixed_period_rollover_day' => "1231",
234 'relationship_type_id' => 20,
235 'financial_type_id' => 2,
236 ));
237 $membershipStatuses = CRM_Member_PseudoConstant::membershipStatus();
238 $pendingStatusId = array_search('Pending', $membershipStatuses);
239 $membership = $this->callAPISuccess('Membership', 'create', array(
240 'contact_id' => $this->_individualId,
241 'membership_type_id' => $membershipTypeAnnualFixed['id'],
242 ));
243 // Updating Membership status to Pending
244 $membership = $this->callAPISuccess('Membership', 'create', array(
245 'id' => $membership["id"],
246 'status_id' => $pendingStatusId,
247 ));
248 $membershipPayment = $this->callAPISuccess('MembershipPayment', 'create', array(
249 'membership_id' => $membership["id"],
250 'contribution_id' => $contributionId,
251 ));
252 return $membership;
253 }
254
61bfc595
PN
255 /**
256 * Test the submit function that completes the pending pay later Contribution with multiple payments.
257 */
258 public function testMultiplePaymentForPendingPayLaterContribution() {
259 $this->createContribution('Pending');
260
261 // pay additional amount
262 $this->submitPayment(40);
263 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
264 $this->assertEquals('Partially paid', $contribution['contribution_status']);
265
266 $this->submitPayment(20);
267 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
268 $this->assertEquals('Partially paid', $contribution['contribution_status']);
269
270 $this->submitPayment(30);
271 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
272 $this->assertEquals('Partially paid', $contribution['contribution_status']);
273
274 $this->submitPayment(10);
275 $this->checkResults(array(40, 20, 30, 10), 4);
276 }
277
278 /**
279 * Test the submit function that completes the pending pay later Contribution with multiple payments.
80c9b98c 280 */
61bfc595
PN
281 public function testMultiplePaymentForPendingPayLaterContributionWithOneCreditCardPayment() {
282 $this->createContribution('Pending');
283
284 // pay additional amount
285 $this->submitPayment(50);
286 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
287 $this->assertEquals('Partially paid', $contribution['contribution_status']);
288
289 $this->submitPayment(20, 'live');
290 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
291 $this->assertEquals('Partially paid', $contribution['contribution_status']);
292
293 $this->submitPayment(20);
294 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
295 $this->assertEquals('Partially paid', $contribution['contribution_status']);
296
297 $this->submitPayment(10, 'live');
298 $this->checkResults(array(50, 20, 20, 10), 4);
299 }
61bfc595
PN
300
301 /**
302 * Function to create pending pay later or partially paid conntribution.
303 *
304 * @param string $typeofContribution
305 *
306 */
307 public function createContribution($typeofContribution = 'Pending') {
308 if ($typeofContribution == 'Partially paid') {
309 $contributionParams = array_merge($this->_params, array(
310 'partial_payment_total' => 100.00,
f49cdeab 311 'partial_amount_to_pay' => 30,
61bfc595
PN
312 'contribution_status_id' => 1,
313 ));
314 }
315 elseif ($typeofContribution == 'Pending') {
316 $contributionParams = array_merge($this->_params, array(
317 'contribution_status_id' => 2,
318 'is_pay_later' => 1,
319 ));
320 }
321 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
322 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contribution['id']));
323 $this->assertNotEmpty($contribution);
324 $this->assertEquals($typeofContribution, $contribution['contribution_status']);
325 $this->_contributionId = $contribution['id'];
326 }
327
328 /**
329 * Function to submit payments for contribution.
330 *
331 * @param float $amount
332 * Payment Amount
333 * @param string $mode
334 * Mode of Payment
335 *
336 */
337 public function submitPayment($amount, $mode = NULL) {
338 $form = new CRM_Contribute_Form_AdditionalPayment();
339
340 $submitParams = array(
341 'contribution_id' => $this->_contributionId,
342 'contact_id' => $this->_individualId,
343 'total_amount' => $amount,
344 'currency' => 'USD',
345 'financial_type_id' => 1,
346 'receive_date' => '04/21/2015',
347 'receive_date_time' => '11:27PM',
348 'trxn_date' => '2017-04-11 13:05:11',
80c9b98c 349 'payment_processor_id' => 0,
61bfc595
PN
350 );
351 if ($mode) {
352 $submitParams += array(
353 'payment_instrument_id' => array_search('Credit card', $this->paymentInstruments),
354 'payment_processor_id' => $this->paymentProcessorID,
355 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
356 'credit_card_number' => '411111111111111',
357 'cvv2' => 234,
358 'credit_card_type' => 'Visa',
359 'billing_city-5' => 'Vancouver',
360 'billing_state_province_id-5' => 1059,
361 'billing_postal_code-5' => 1321312,
362 'billing_country_id-5' => 1228,
363 );
364 }
365 else {
366 $submitParams += array(
367 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
368 'check_number' => 'check-12345',
369 );
370 }
23cb875c 371 $form->cid = $this->_individualId;
61bfc595
PN
372 $form->testSubmit($submitParams, $mode);
373 }
374
375 /**
376 * Function to check result.
377 *
378 * @param array $amounts
379 * Array of payment amount for contribution
380 * @param int $count
381 * Number payment for contribution
382 *
383 */
384 public function checkResults($amounts, $count) {
385 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $this->_contributionId));
386 $this->assertNotEmpty($contribution);
387 $this->assertEquals('Completed', $contribution['contribution_status']);
388
389 $this->callAPISuccessGetCount('EntityFinancialTrxn', array(
390 'entity_table' => "civicrm_contribution",
391 'entity_id' => $this->_contributionId,
392 'financial_trxn_id.is_payment' => 1,
393 'financial_trxn_id.total_amount' => array('IN' => $amounts),
394 ), $count);
395 }
396
397}