3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * Test class for Pledge API - civicrm_pledge_*
15 * @package CiviCRM_APIv3
18 class api_v3_PledgePaymentTest
extends CiviUnitTestCase
{
20 protected $_individualId;
22 protected $_contributionID;
23 protected $_financialTypeId = 1;
24 protected $_entity = 'PledgePayment';
29 * @throws \CRM_Core_Exception
31 public function setUp() {
33 $this->_individualId
= $this->individualCreate();
34 $this->_pledgeID
= $this->pledgeCreate(['contact_id' => $this->_individualId
]);
35 $this->_contributionID
= $this->contributionCreate(['contact_id' => $this->_individualId
]);
39 * Clean up after function.
40 * @throws \CRM_Core_Exception
42 public function tearDown() {
44 'civicrm_contribution',
47 'civicrm_pledge_payment',
51 $this->quickCleanup($tablesToTruncate);
55 public function testGetPledgePayment() {
57 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__
, __FILE__
);
58 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
62 * Test that passing in a single variable works.
64 public function testGetSinglePledgePayment() {
66 'contact_id' => $this->_individualId
,
67 'pledge_id' => $this->_pledgeID
,
68 'contribution_id' => $this->_contributionID
,
71 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
73 'contribution_id' => $this->_contributionID
,
75 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
76 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
80 * Test process_pledge job log.
82 public function testProcessPledgeJob() {
83 $pledgeStatuses = CRM_Core_OptionGroup
::values('pledge_status',
84 FALSE, FALSE, FALSE, NULL, 'name'
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),
94 $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
95 //Status should be 'Pending' after first incomplete payment.
96 $checkStatus = $this->callAPISuccess('pledge', 'getsingle', [
97 'id' => $this->_pledgeID
,
98 'return' => 'pledge_status',
100 $this->assertEquals('Pending Label**', $checkStatus['pledge_status']);
102 //Execute process_pledge job log.
103 $result = $this->callAPISuccess('Job', 'process_pledge', []);
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']);
106 //Status should be 'Overdue' after processing.
107 $statusAfterProcessing = $this->callAPISuccess('pledge', 'getsingle', [
108 'id' => $this->_pledgeID
,
109 'return' => 'pledge_status',
111 $this->assertEquals('Overdue', $statusAfterProcessing['pledge_status']);
115 * Test status of pledge on payments and cancellation.
117 public function testPledgeStatus() {
118 //Status should initially be Pending.
119 $checkStatus = $this->callAPISuccess('pledge', 'getsingle', [
120 'id' => $this->_pledgeID
,
121 'return' => 'pledge_status',
123 $this->assertEquals('Pending Label**', $checkStatus['pledge_status']);
125 //Make first payment.
127 'contact_id' => $this->_individualId
,
128 'pledge_id' => $this->_pledgeID
,
129 'contribution_id' => $this->_contributionID
,
132 $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
134 //Status should be 'In Progress' after first payment.
135 $checkStatus = $this->callAPISuccess('pledge', 'getsingle', [
136 'id' => $this->_pledgeID
,
137 'return' => 'pledge_status',
139 $this->assertEquals('In Progress', $checkStatus['pledge_status']);
142 $paymentStatusTypes = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
144 'id' => $this->_pledgeID
,
145 'status_id' => array_search('Cancelled', $paymentStatusTypes),
147 $this->callAPISuccess('pledge', 'create', $updateParams);
149 //Status should be calculated as Cancelled.
150 $pledgeStatus = CRM_Pledge_BAO_PledgePayment
::calculatePledgeStatus($this->_pledgeID
);
151 $this->assertEquals('Cancelled', $paymentStatusTypes[$pledgeStatus]);
153 //Already completed payments should not be cancelled.
154 $checkPaymentStatus = $this->callAPISuccess('pledge_payment', 'getsingle', [
155 'id' => $firstPayment['id'],
156 'return' => 'status_id',
158 $this->assertEquals(array_search('Completed', $paymentStatusTypes), $checkPaymentStatus['status_id']);
162 * Test that passing in a single variable works:: status_id
164 public function testGetSinglePledgePaymentByStatusID() {
166 'contact_id' => $this->_individualId
,
167 'pledge_id' => $this->_pledgeID
,
168 'contribution_id' => $this->_contributionID
,
171 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
176 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
177 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
181 * Test that creating a payment will add the contribution ID.
183 public function testCreatePledgePayment() {
184 //check that 5 pledge payments exist at the start
185 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', []);
186 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
188 //test the pledge_payment_create function
190 'contact_id' => $this->_individualId
,
191 'pledge_id' => $this->_pledgeID
,
192 'contribution_id' => $this->_contributionID
,
194 'actual_amount' => 20,
196 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__
, __FILE__
);
198 //check existing updated not new one created - 'create' means add contribution_id in this context
199 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', []);
200 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
202 //get the created payment & check it out
203 $getParams['id'] = $result['id'];
204 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
205 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
206 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
208 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
209 $contributionParams = [
210 'total_amount' => 20,
211 'contact_id' => $this->_individualId
,
212 'financial_type_id' => $this->_financialTypeId
,
214 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
215 $params['contribution_id'] = $contribution['id'];
217 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
218 //make sure original is untouched & has not been updated
219 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
220 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
221 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
222 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
226 * Test checks behaviour when more payments are created than should be possible.
228 public function testCreatePledgePaymentAllCreated() {
230 'pledge_id' => $this->_pledgeID
,
233 // create one more pledge than there are spaces for
236 $contributionParams = [
237 'total_amount' => 20,
238 'contact_id' => $this->_individualId
,
239 'financial_type_id' => $this->_financialTypeId
,
241 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
243 $params['contribution_id'] = $contribution['id'];
245 $resultCont2 = civicrm_api('pledge_payment', 'create', $params +
['version' => $this->_apiversion
]);
248 // check that only 5 exist & we got an error setting the 6th
249 $result = $this->callAPISuccess('PledgePayment', 'Get', [
250 'pledge_id' => $this->_pledgeID
,
252 // the last one above should result in an error
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']);
254 $this->assertEquals(5, $result['count']);
256 $params['option.create_new'] = 1;
257 $params['scheduled_amount'] = 20;
258 $params['scheduled_date'] = '20131212';
259 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
260 $result = $this->callAPISuccess('PledgePayment', 'Get', [
261 'pledge_id' => $this->_pledgeID
,
264 $this->assertEquals(6, $result['count']);
268 * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule.
270 public function testCreatePledgePaymentWhereOnlyOnePayment() {
272 'contact_id' => $this->_individualId
,
273 'pledge_create_date' => date('Ymd'),
274 'start_date' => date('Ymd'),
275 'scheduled_date' => 'first day 2015',
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,
287 $contributionID = $this->contributionCreate([
288 'contact_id' => $this->_individualId
,
289 'financial_type_id' => $this->_financialTypeId
,
293 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
295 //test the pledge_payment_create function
297 'contact_id' => $this->_individualId
,
298 'pledge_id' => $pledge['id'],
299 'contribution_id' => $contributionID,
301 'actual_amount' => 20,
303 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
305 //check existing updated not new one created - 'create' means add contribution_id in this context
306 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', [
307 'contribution_id' => $contributionID,
309 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
312 public function testUpdatePledgePayment() {
314 'pledge_id' => $this->_pledgeID
,
315 'contribution_id' => $this->_contributionID
,
317 'actual_amount' => 20,
319 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
321 'id' => $result['id'],
325 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__
, __FILE__
);
326 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity
);
329 public function testDeletePledgePayment() {
331 'contact_id' => $this->_individualId
,
332 'pledge_id' => $this->_pledgeID
,
333 'contribution_id' => $this->_contributionID
,
336 'actual_amount' => 20,
338 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
341 'id' => $pledgePayment['id'],
343 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
346 public function testGetFields() {
347 $result = $this->callAPISuccess('PledgePayment', 'GetFields', []);
348 $this->assertType('array', $result);