Merge pull request #13959 from mlutfy/setMessageError
[civicrm-core.git] / tests / phpunit / api / v3 / PledgePaymentTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 class for Pledge API - civicrm_pledge_*
30 *
31 * @package CiviCRM_APIv3
32 * @group headless
33 */
34 class api_v3_PledgePaymentTest extends CiviUnitTestCase {
35
36 protected $_individualId;
37 protected $_pledgeID;
38 protected $_apiversion = 3;
39 protected $_contributionID;
40 protected $_financialTypeId = 1;
41 protected $_entity = 'PledgePayment';
42 public $DBResetRequired = TRUE;
43
44 public function setUp() {
45 parent::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));
49 }
50
51 public function tearDown() {
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);
61 }
62
63 public function testGetPledgePayment() {
64 $params = array();
65 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__, __FILE__);
66 $this->assertEquals(5, $result['count'], " in line " . __LINE__);
67 }
68
69 /**
70 * Test that passing in a single variable works.
71 */
72 public function testGetSinglePledgePayment() {
73 $createparams = array(
74 'contact_id' => $this->_individualId,
75 'pledge_id' => $this->_pledgeID,
76 'contribution_id' => $this->_contributionID,
77 'status_id' => 1,
78 );
79 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
80 $params = array(
81 'contribution_id' => $this->_contributionID,
82 );
83 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
84 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
85 }
86
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
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
169 /**
170 * Test that passing in a single variable works:: status_id
171 */
172 public function testGetSinglePledgePaymentByStatusID() {
173 $createparams = array(
174 'contact_id' => $this->_individualId,
175 'pledge_id' => $this->_pledgeID,
176 'contribution_id' => $this->_contributionID,
177 'status_id' => 1,
178 );
179 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
180 $params = array(
181 'status_id' => 1,
182 );
183
184 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
185 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
186 }
187
188 /**
189 * Test that creating a payment will add the contribution ID.
190 */
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__);
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,
201 'status_id' => 1,
202 'actual_amount' => 20,
203 );
204 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__, __FILE__);
205
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__);
209
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__);
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(
218 'total_amount' => 20,
219 'contact_id' => $this->_individualId,
220 'financial_type_id' => $this->_financialTypeId,
221 );
222 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
223 $params['contribution_id'] = $contribution['id'];
224
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__);
231 }
232
233 /**
234 * Test checks behaviour when more payments are created than should be possible.
235 */
236 public function testCreatePledgePaymentAllCreated() {
237 $params = array(
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(
245 'total_amount' => 20,
246 'contact_id' => $this->_individualId,
247 'financial_type_id' => $this->_financialTypeId,
248 );
249 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
250
251 $params['contribution_id'] = $contribution['id'];
252
253 $resultCont2 = civicrm_api('pledge_payment', 'create', $params + array('version' => $this->_apiversion));
254 $i++;
255 }
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,
259 ));
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']);
263
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,
270 ));
271
272 $this->assertEquals(6, $result['count']);
273 }
274
275 /**
276 * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule.
277 */
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,
291 'installments' => 1,
292 'sequential' => 1,
293 );
294
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 ));
301 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
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,
308 'status_id' => 1,
309 'actual_amount' => 20,
310 );
311 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
312
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,
316 ));
317 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__);
318 }
319
320 public function testUpdatePledgePayment() {
321 $params = array(
322 'pledge_id' => $this->_pledgeID,
323 'contribution_id' => $this->_contributionID,
324 'status_id' => 2,
325 'actual_amount' => 20,
326 );
327 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
328 $updateparams = array(
329 'id' => $result['id'],
330 'status_id' => 1,
331 );
332
333 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__, __FILE__);
334 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity);
335 }
336
337 public function testDeletePledgePayment() {
338 $params = array(
339 'contact_id' => $this->_individualId,
340 'pledge_id' => $this->_pledgeID,
341 'contribution_id' => $this->_contributionID,
342 'status_id' => 1,
343 'sequential' => 1,
344 'actual_amount' => 20,
345 );
346 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
347
348 $deleteParams = array(
349 'id' => $pledgePayment['id'],
350 );
351 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
352 }
353
354 public function testGetFields() {
355 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
356 $this->assertType('array', $result);
357 }
358
359 }