Merge pull request #1 from civicrm/master
[civicrm-core.git] / tests / phpunit / api / v3 / PledgePaymentTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * Test class for Pledge API - civicrm_pledge_*
30 *
6c6e6187 31 * @package CiviCRM_APIv3
acb109b7 32 * @group headless
6a488035 33 */
6a488035
TO
34class api_v3_PledgePaymentTest extends CiviUnitTestCase {
35
6a488035
TO
36 protected $_individualId;
37 protected $_pledgeID;
256f4425 38 protected $_apiversion = 3;
6a488035 39 protected $_contributionID;
256f4425 40 protected $_financialTypeId = 1;
6a488035 41 protected $_entity = 'PledgePayment';
430ae6dd
TO
42 public $DBResetRequired = TRUE;
43
00be9182 44 public function setUp() {
6a488035 45 parent::setUp();
e4d5f1e2 46 $this->_individualId = $this->individualCreate();
5210fa35 47 $this->_pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId));
c3a3074f 48 $this->_contributionID = $this->contributionCreate(array('contact_id' => $this->_individualId));
6a488035
TO
49 }
50
00be9182 51 public function tearDown() {
6a488035
TO
52 $tablesToTruncate = array(
53 'civicrm_contribution',
54 'civicrm_contact',
55 'civicrm_pledge',
56 'civicrm_pledge_payment',
57 'civicrm_line_item',
58 );
59
60 $this->quickCleanup($tablesToTruncate);
6a488035
TO
61 }
62
00be9182 63 public function testGetPledgePayment() {
256f4425 64 $params = array();
65 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
66 $this->assertEquals(5, $result['count'], " in line " . __LINE__);
67 }
68
c490a46a 69 /**
eceb18cc 70 * Test that passing in a single variable works.
c490a46a 71 */
00be9182 72 public function testGetSinglePledgePayment() {
6a488035
TO
73 $createparams = array(
74 'contact_id' => $this->_individualId,
75 'pledge_id' => $this->_pledgeID,
76 'contribution_id' => $this->_contributionID,
6a488035
TO
77 'status_id' => 1,
78 );
256f4425 79 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
6a488035 80 $params = array(
6a488035
TO
81 'contribution_id' => $this->_contributionID,
82 );
256f4425 83 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
6a488035
TO
84 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
85 }
86
e7212d86
JP
87 /**
88 * Test process_pledge job log.
89 */
90 public function testProcessPledgeJob() {
91 $pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
92 FALSE, FALSE, FALSE, NULL, 'name'
93 );
94 //Make first payment.
95 $paymentParams = array(
96 'contact_id' => $this->_individualId,
97 'pledge_id' => $this->_pledgeID,
98 'contribution_id' => $this->_contributionID,
99 'scheduled_date' => date('Ymd', strtotime("-1 days")),
100 'status_id' => array_search('Pending', $pledgeStatuses),
101 );
102 $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
103 //Status should be 'Pending' after first incomplete payment.
104 $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
105 'id' => $this->_pledgeID,
106 'return' => 'pledge_status',
107 ));
108 $this->assertEquals('Pending', $checkStatus['pledge_status']);
109
110 //Execute process_pledge job log.
111 $result = $this->callAPISuccess('Job', 'process_pledge', array());
112 $this->assertEquals("Checking if status update is needed for Pledge Id: {$this->_pledgeID} (current status is Pending)\n\r- status updated to: Overdue\n\r1 records updated.", $result['values']);
113
114 //Status should be 'Overdue' after processing.
115 $statusAfterProcessing = $this->callAPISuccess('pledge', 'getsingle', array(
116 'id' => $this->_pledgeID,
117 'return' => 'pledge_status',
118 ));
119 $this->assertEquals('Overdue', $statusAfterProcessing['pledge_status']);
120 }
121
2c0f8c67
JP
122 /**
123 * Test status of pledge on payments and cancellation.
124 */
125 public function testPledgeStatus() {
126 //Status should initially be Pending.
127 $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
128 'id' => $this->_pledgeID,
129 'return' => 'pledge_status',
130 ));
131 $this->assertEquals('Pending', $checkStatus['pledge_status']);
132
133 //Make first payment.
134 $paymentParams = array(
135 'contact_id' => $this->_individualId,
136 'pledge_id' => $this->_pledgeID,
137 'contribution_id' => $this->_contributionID,
138 'status_id' => 1,
139 );
140 $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
141
142 //Status should be 'In Progress' after first payment.
143 $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
144 'id' => $this->_pledgeID,
145 'return' => 'pledge_status',
146 ));
147 $this->assertEquals('In Progress', $checkStatus['pledge_status']);
148
149 //Cancel the Pledge.
150 $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
151 $updateParams = array(
152 'id' => $this->_pledgeID,
153 'status_id' => array_search('Cancelled', $paymentStatusTypes),
154 );
155 $this->callAPISuccess('pledge', 'create', $updateParams);
156
157 //Status should be calculated as Cancelled.
158 $pledgeStatus = CRM_Pledge_BAO_PledgePayment::calculatePledgeStatus($this->_pledgeID);
159 $this->assertEquals('Cancelled', $paymentStatusTypes[$pledgeStatus]);
160
161 //Already completed payments should not be cancelled.
162 $checkPaymentStatus = $this->callAPISuccess('pledge_payment', 'getsingle', array(
163 'id' => $firstPayment['id'],
164 'return' => 'status_id',
165 ));
166 $this->assertEquals(array_search('Completed', $paymentStatusTypes), $checkPaymentStatus['status_id']);
167 }
168
c490a46a
CW
169 /**
170 * Test that passing in a single variable works:: status_id
171 */
00be9182 172 public function testGetSinglePledgePaymentByStatusID() {
6a488035
TO
173 $createparams = array(
174 'contact_id' => $this->_individualId,
175 'pledge_id' => $this->_pledgeID,
176 'contribution_id' => $this->_contributionID,
6a488035
TO
177 'status_id' => 1,
178 );
256f4425 179 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
6a488035 180 $params = array(
6a488035
TO
181 'status_id' => 1,
182 );
183
256f4425 184 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
6a488035
TO
185 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
186 }
187
c490a46a 188 /**
eceb18cc 189 * Test that creating a payment will add the contribution ID.
c490a46a 190 */
00be9182 191 public function testCreatePledgePayment() {
6a488035 192 //check that 5 pledge payments exist at the start
256f4425 193 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
6a488035
TO
194 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__);
195
196 //test the pledge_payment_create function
197 $params = array(
198 'contact_id' => $this->_individualId,
199 'pledge_id' => $this->_pledgeID,
200 'contribution_id' => $this->_contributionID,
6a488035
TO
201 'status_id' => 1,
202 'actual_amount' => 20,
203 );
256f4425 204 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
205
206 //check existing updated not new one created - 'create' means add contribution_id in this context
256f4425 207 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
6a488035
TO
208 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__);
209
210 //get the created payment & check it out
211 $getParams['id'] = $result['id'];
256f4425 212 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
6a488035
TO
213 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__);
214 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__);
215
216 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
217 $contributionParams = array(
6a488035
TO
218 'total_amount' => 20,
219 'contact_id' => $this->_individualId,
256f4425 220 'financial_type_id' => $this->_financialTypeId,
6a488035 221 );
256f4425 222 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
6a488035
TO
223 $params['contribution_id'] = $contribution['id'];
224
256f4425 225 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
226 //make sure original is untouched & has not been updated
227 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__);
256f4425 228 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
6a488035
TO
229 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__);
230 $this->assertEquals($this->_contributionID, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__);
231 }
232
c490a46a 233 /**
eceb18cc 234 * Test checks behaviour when more payments are created than should be possible.
c490a46a 235 */
00be9182 236 public function testCreatePledgePaymentAllCreated() {
6a488035 237 $params = array(
6a488035
TO
238 'pledge_id' => $this->_pledgeID,
239 'status_id' => 1,
240 );
241 // create one more pledge than there are spaces for
242 $i = 0;
243 while ($i <= 5) {
244 $contributionParams = array(
6a488035
TO
245 'total_amount' => 20,
246 'contact_id' => $this->_individualId,
256f4425 247 'financial_type_id' => $this->_financialTypeId,
6a488035 248 );
256f4425 249 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
6a488035
TO
250
251 $params['contribution_id'] = $contribution['id'];
256f4425 252
253 $resultCont2 = civicrm_api('pledge_payment', 'create', $params + array('version' => $this->_apiversion));
6a488035
TO
254 $i++;
255 }
256 // check that only 5 exist & we got an error setting the 6th
256f4425 257 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
6a488035
TO
258 'pledge_id' => $this->_pledgeID,
259 ));
256f4425 260 // the last one above should result in an error
6a488035 261 $this->assertEquals("There are no unmatched payment on this pledge. Pass in the pledge_payment id to specify one or 'option.create_new' to create one", $resultCont2['error_message']);
256f4425 262 $this->assertEquals(5, $result['count']);
6a488035
TO
263
264 $params['option.create_new'] = 1;
265 $params['scheduled_amount'] = 20;
266 $params['scheduled_date'] = '20131212';
256f4425 267 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
268 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
92915c55
TO
269 'pledge_id' => $this->_pledgeID,
270 ));
6a488035
TO
271
272 $this->assertEquals(6, $result['count']);
273 }
6a488035 274
c490a46a 275 /**
fe482240 276 * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule.
c490a46a 277 */
00be9182 278 public function testCreatePledgePaymentWhereOnlyOnePayment() {
6a488035
TO
279 $pledgeParams = array(
280 'contact_id' => $this->_individualId,
281 'pledge_create_date' => date('Ymd'),
282 'start_date' => date('Ymd'),
cd6ca96a 283 'scheduled_date' => 'first day 2015',
6a488035
TO
284 'pledge_amount' => 100.00,
285 'pledge_status_id' => '2',
286 'pledge_financial_type_id' => '1',
287 'pledge_original_installment_amount' => 20,
288 'frequency_interval' => 5,
289 'frequency_unit' => 'year',
290 'frequency_day' => 15,
291 'installments' => 1,
292 'sequential' => 1,
6a488035
TO
293 );
294
78ab0ca4 295 $contributionID = $this->contributionCreate(array(
296 'contact_id' => $this->_individualId,
297 'financial_type_id' => $this->_financialTypeId,
298 'invoice_id' => 45,
299 'trxn_id' => 45,
300 ));
256f4425 301 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
6a488035
TO
302
303 //test the pledge_payment_create function
304 $params = array(
305 'contact_id' => $this->_individualId,
306 'pledge_id' => $pledge['id'],
307 'contribution_id' => $contributionID,
6a488035
TO
308 'status_id' => 1,
309 'actual_amount' => 20,
310 );
256f4425 311 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
312
313 //check existing updated not new one created - 'create' means add contribution_id in this context
256f4425 314 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
315 'contribution_id' => $contributionID,
92915c55 316 ));
6a488035
TO
317 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__);
318 }
319
00be9182 320 public function testUpdatePledgePayment() {
6a488035
TO
321 $params = array(
322 'pledge_id' => $this->_pledgeID,
323 'contribution_id' => $this->_contributionID,
6a488035
TO
324 'status_id' => 2,
325 'actual_amount' => 20,
326 );
256f4425 327 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
328 $updateparams = array(
329 'id' => $result['id'],
330 'status_id' => 1,
6a488035
TO
331 );
332
256f4425 333 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__, __FILE__);
6c6e6187 334 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity);
6a488035
TO
335 }
336
00be9182 337 public function testDeletePledgePayment() {
6a488035
TO
338 $params = array(
339 'contact_id' => $this->_individualId,
340 'pledge_id' => $this->_pledgeID,
341 'contribution_id' => $this->_contributionID,
6a488035
TO
342 'status_id' => 1,
343 'sequential' => 1,
344 'actual_amount' => 20,
345 );
256f4425 346 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
347
348 $deleteParams = array(
349 'id' => $pledgePayment['id'],
6a488035 350 );
256f4425 351 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
6a488035
TO
352 }
353
00be9182 354 public function testGetFields() {
256f4425 355 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
6a488035
TO
356 $this->assertType('array', $result);
357 }
96025800 358
6a488035 359}