3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for Pledge API - civicrm_pledge_*
31 * @package CiviCRM_APIv3
34 class api_v3_PledgePaymentTest
extends CiviUnitTestCase
{
36 protected $_individualId;
38 protected $_apiversion = 3;
39 protected $_contributionID;
40 protected $_financialTypeId = 1;
41 protected $_entity = 'PledgePayment';
42 public $DBResetRequired = TRUE;
44 public function setUp() {
46 $this->_individualId
= $this->individualCreate();
47 $this->_pledgeID
= $this->pledgeCreate(array('contact_id' => $this->_individualId
));
48 $this->_contributionID
= $this->contributionCreate(array('contact_id' => $this->_individualId
));
51 public function tearDown() {
52 $tablesToTruncate = array(
53 'civicrm_contribution',
56 'civicrm_pledge_payment',
60 $this->quickCleanup($tablesToTruncate);
63 public function testGetPledgePayment() {
65 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__
, __FILE__
);
66 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
70 * Test that passing in a single variable works.
72 public function testGetSinglePledgePayment() {
73 $createparams = array(
74 'contact_id' => $this->_individualId
,
75 'pledge_id' => $this->_pledgeID
,
76 'contribution_id' => $this->_contributionID
,
79 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
81 'contribution_id' => $this->_contributionID
,
83 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
84 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
88 * Test process_pledge job log.
90 public function testProcessPledgeJob() {
91 $pledgeStatuses = CRM_Core_OptionGroup
::values('pledge_status',
92 FALSE, FALSE, FALSE, NULL, 'name'
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),
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',
108 $this->assertEquals('Pending', $checkStatus['pledge_status']);
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']);
114 //Status should be 'Overdue' after processing.
115 $statusAfterProcessing = $this->callAPISuccess('pledge', 'getsingle', array(
116 'id' => $this->_pledgeID
,
117 'return' => 'pledge_status',
119 $this->assertEquals('Overdue', $statusAfterProcessing['pledge_status']);
123 * Test status of pledge on payments and cancellation.
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',
131 $this->assertEquals('Pending', $checkStatus['pledge_status']);
133 //Make first payment.
134 $paymentParams = array(
135 'contact_id' => $this->_individualId
,
136 'pledge_id' => $this->_pledgeID
,
137 'contribution_id' => $this->_contributionID
,
140 $firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
142 //Status should be 'In Progress' after first payment.
143 $checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
144 'id' => $this->_pledgeID
,
145 'return' => 'pledge_status',
147 $this->assertEquals('In Progress', $checkStatus['pledge_status']);
150 $paymentStatusTypes = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
151 $updateParams = array(
152 'id' => $this->_pledgeID
,
153 'status_id' => array_search('Cancelled', $paymentStatusTypes),
155 $this->callAPISuccess('pledge', 'create', $updateParams);
157 //Status should be calculated as Cancelled.
158 $pledgeStatus = CRM_Pledge_BAO_PledgePayment
::calculatePledgeStatus($this->_pledgeID
);
159 $this->assertEquals('Cancelled', $paymentStatusTypes[$pledgeStatus]);
161 //Already completed payments should not be cancelled.
162 $checkPaymentStatus = $this->callAPISuccess('pledge_payment', 'getsingle', array(
163 'id' => $firstPayment['id'],
164 'return' => 'status_id',
166 $this->assertEquals(array_search('Completed', $paymentStatusTypes), $checkPaymentStatus['status_id']);
170 * Test that passing in a single variable works:: status_id
172 public function testGetSinglePledgePaymentByStatusID() {
173 $createparams = array(
174 'contact_id' => $this->_individualId
,
175 'pledge_id' => $this->_pledgeID
,
176 'contribution_id' => $this->_contributionID
,
179 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
184 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
185 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
189 * Test that creating a payment will add the contribution ID.
191 public function testCreatePledgePayment() {
192 //check that 5 pledge payments exist at the start
193 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
194 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
196 //test the pledge_payment_create function
198 'contact_id' => $this->_individualId
,
199 'pledge_id' => $this->_pledgeID
,
200 'contribution_id' => $this->_contributionID
,
202 'actual_amount' => 20,
204 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__
, __FILE__
);
206 //check existing updated not new one created - 'create' means add contribution_id in this context
207 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
208 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
210 //get the created payment & check it out
211 $getParams['id'] = $result['id'];
212 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
213 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
214 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
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(
218 'total_amount' => 20,
219 'contact_id' => $this->_individualId
,
220 'financial_type_id' => $this->_financialTypeId
,
222 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
223 $params['contribution_id'] = $contribution['id'];
225 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
226 //make sure original is untouched & has not been updated
227 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
228 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
229 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
230 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
234 * Test checks behaviour when more payments are created than should be possible.
236 public function testCreatePledgePaymentAllCreated() {
238 'pledge_id' => $this->_pledgeID
,
241 // create one more pledge than there are spaces for
244 $contributionParams = array(
245 'total_amount' => 20,
246 'contact_id' => $this->_individualId
,
247 'financial_type_id' => $this->_financialTypeId
,
249 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
251 $params['contribution_id'] = $contribution['id'];
253 $resultCont2 = civicrm_api('pledge_payment', 'create', $params +
array('version' => $this->_apiversion
));
256 // check that only 5 exist & we got an error setting the 6th
257 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
258 'pledge_id' => $this->_pledgeID
,
260 // the last one above should result in an error
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']);
262 $this->assertEquals(5, $result['count']);
264 $params['option.create_new'] = 1;
265 $params['scheduled_amount'] = 20;
266 $params['scheduled_date'] = '20131212';
267 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
268 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
269 'pledge_id' => $this->_pledgeID
,
272 $this->assertEquals(6, $result['count']);
276 * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule.
278 public function testCreatePledgePaymentWhereOnlyOnePayment() {
279 $pledgeParams = array(
280 'contact_id' => $this->_individualId
,
281 'pledge_create_date' => date('Ymd'),
282 'start_date' => date('Ymd'),
283 'scheduled_date' => 'first day 2015',
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,
295 $contributionID = $this->contributionCreate(array(
296 'contact_id' => $this->_individualId
,
297 'financial_type_id' => $this->_financialTypeId
,
301 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
303 //test the pledge_payment_create function
305 'contact_id' => $this->_individualId
,
306 'pledge_id' => $pledge['id'],
307 'contribution_id' => $contributionID,
309 'actual_amount' => 20,
311 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
313 //check existing updated not new one created - 'create' means add contribution_id in this context
314 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
315 'contribution_id' => $contributionID,
317 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
320 public function testUpdatePledgePayment() {
322 'pledge_id' => $this->_pledgeID
,
323 'contribution_id' => $this->_contributionID
,
325 'actual_amount' => 20,
327 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
328 $updateparams = array(
329 'id' => $result['id'],
333 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__
, __FILE__
);
334 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity
);
337 public function testDeletePledgePayment() {
339 'contact_id' => $this->_individualId
,
340 'pledge_id' => $this->_pledgeID
,
341 'contribution_id' => $this->_contributionID
,
344 'actual_amount' => 20,
346 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
348 $deleteParams = array(
349 'id' => $pledgePayment['id'],
351 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
354 public function testGetFields() {
355 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
356 $this->assertType('array', $result);