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