Merge branch '5.11' of https://github.com/civicrm/civicrm-core
[civicrm-core.git] / tests / phpunit / api / v3 / PledgeTest.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_PledgeTest extends CiviUnitTestCase {
35
36 /**
37 * Assume empty database with just civicrm_data.
38 */
39 protected $_individualId;
40 protected $_pledge;
41 protected $_apiversion;
42 protected $_params;
43 protected $_entity;
44 protected $scheduled_date;
45 public $DBResetRequired = TRUE;
46
47 public function setUp() {
48 $this->_apiversion = 3;
49 parent::setUp();
50 $this->quickCleanup(array('civicrm_pledge', 'civicrm_pledge_payment'));
51 //need to set scheduled payment in advance we are running test @ midnight & it becomes unexpectedly overdue
52 //due to timezone issues
53 $this->scheduled_date = date('Ymd', mktime(0, 0, 0, date("m"), date("d") + 2, date("y")));
54 $this->_entity = 'Pledge';
55 $this->_individualId = $this->individualCreate();
56 $this->_params = array(
57 'contact_id' => $this->_individualId,
58 'pledge_create_date' => date('Ymd'),
59 'start_date' => date('Ymd'),
60 'scheduled_date' => $this->scheduled_date,
61 'amount' => 100.00,
62 'pledge_status_id' => '2',
63 'pledge_financial_type_id' => '1',
64 'pledge_original_installment_amount' => 20,
65 'frequency_interval' => 5,
66 'frequency_unit' => 'year',
67 'frequency_day' => 15,
68 'installments' => 5,
69 'sequential' => 1,
70 );
71 }
72
73 public function tearDown() {
74 $this->contactDelete($this->_individualId);
75 }
76
77 /**
78 * Check with complete array + custom field.
79 *
80 * Note that the test is written on purpose without any
81 * variables specific to participant so it can be replicated into other entities
82 * and / or moved to the automated test suite
83 */
84 public function testCreateWithCustom() {
85 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
86
87 $params = $this->_params;
88 $params['custom_' . $ids['custom_field_id']] = "custom string";
89
90 $result = $this->callAPISuccess($this->_entity, 'create', $params);
91 $this->assertAPISuccess($result, " testCreateWithCustom ");
92 $this->assertAPISuccess($result);
93 $getParams = array('id' => $result['id'], 'return.custom_' . $ids['custom_field_id'] => 1);
94 $check = $this->callAPISuccess($this->_entity, 'get', $getParams);
95 $this->callAPISuccess('pledge', 'delete', array('id' => $check['id']));
96 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
97
98 $this->customFieldDelete($ids['custom_field_id']);
99 $this->customGroupDelete($ids['custom_group_id']);
100 }
101
102 /**
103 * Test getfields function for pledge.
104 */
105 public function testGetfieldsPledge() {
106 $result = $this->callAPISuccess('pledge', 'getfields', array('action' => 'get'));
107 $this->assertEquals(1, $result['values']['next_pay_date']['api.return']);
108 }
109
110 /**
111 * Test get pledge api.
112 */
113 public function testGetPledge() {
114
115 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
116 $params = array(
117 'pledge_id' => $this->_pledge['id'],
118 );
119 $result = $this->callAPIAndDocument('pledge', 'get', $params, __FUNCTION__, __FILE__);
120 $pledge = $result['values'][$this->_pledge['id']];
121 $this->assertEquals($this->_individualId, $pledge['contact_id']);
122 $this->assertEquals($this->_pledge['id'], $pledge['pledge_id']);
123 $this->assertEquals(date('Y-m-d') . ' 00:00:00', $pledge['pledge_create_date']);
124 $this->assertEquals(100.00, $pledge['pledge_amount']);
125 $this->assertEquals('Pending', $pledge['pledge_status']);
126 $this->assertEquals(5, $pledge['pledge_frequency_interval']);
127 $this->assertEquals('year', $pledge['pledge_frequency_unit']);
128 $this->assertEquals(date('Y-m-d', strtotime($this->scheduled_date)) . ' 00:00:00', $pledge['pledge_next_pay_date']);
129 $this->assertEquals($pledge['pledge_next_pay_amount'], 20.00);
130
131 $params2 = array(
132 'pledge_id' => $this->_pledge['id'],
133 );
134 $pledge = $this->callAPISuccess('pledge', 'delete', $params2);
135 }
136
137 /**
138 * Test 'return.pledge_financial_type' => 1 works.
139 */
140 public function testGetPledgeWithReturn() {
141
142 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
143 $params = array(
144 'pledge_id' => $this->_pledge['id'],
145 'return.pledge_financial_type' => 1,
146 );
147 $result = $this->callAPISuccess('pledge', 'get', $params);
148 $pledge = $result['values'][$this->_pledge['id']];
149 $this->callAPISuccess('pledge', 'delete', $pledge);
150 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
151 }
152
153 /**
154 * Test 'return.pledge_contribution_type' => 1 works.
155 *
156 * This is for legacy compatibility
157 */
158 public function testGetPledgeWithReturnLegacy() {
159
160 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
161 $params = array(
162 'pledge_id' => $this->_pledge['id'],
163 'return.pledge_financial_type' => 1,
164 );
165 $result = $this->callAPISuccess('pledge', 'get', $params);
166 $pledge = $result['values'][$this->_pledge['id']];
167 $this->callAPISuccess('pledge', 'delete', $pledge);
168 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
169 }
170
171 /**
172 * Test date legacy date filters like pledge_start_date_high.
173 */
174 public function testPledgeGetReturnFilters() {
175 $this->callAPISuccess('pledge', 'create', $this->_params);
176
177 $overdueParams = array(
178 'scheduled_date' => 'first saturday of march last year',
179 'start_date' => 'first saturday of march last year',
180 );
181 $oldPledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
182
183 $pledgeGetParams = array();
184 $allPledges = $this->callAPISuccess('pledge', 'getcount', $pledgeGetParams);
185
186 $this->assertEquals(2, $allPledges, 'Check we have 2 pledges to place with in line ' . __LINE__);
187 $pledgeGetParams['pledge_start_date_high'] = date('YmdHis', strtotime('2 days ago'));
188 $earlyPledge = $this->callAPIAndDocument('pledge', 'get', $pledgeGetParams, __FUNCTION__, __FILE__, "demonstrates high date filter", "GetFilterHighDate");
189 $this->assertEquals(1, $earlyPledge['count'], ' check only one returned with start date filter in line ' . __LINE__);
190 $this->assertEquals($oldPledge['id'], $earlyPledge['id'], ' check correct pledge returned ' . __LINE__);
191 }
192
193 /**
194 * Create 2 pledges - see if we can get by status id.
195 */
196 public function testGetOverduePledge() {
197 $overdueParams = array(
198 'scheduled_date' => 'first saturday of march last year',
199 'start_date' => 'first saturday of march last year',
200 );
201 $this->_pledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
202
203 $result = $this->callAPISuccess('pledge', 'get', array('status_id' => 'Overdue'));
204 $emptyResult = $this->callAPISuccess('pledge', 'get', array(
205 'pledge_status_id' => '1',
206 ));
207 $pledge = $result['values'][$this->_pledge['id']];
208 $this->callAPISuccess('pledge', 'delete', $pledge);
209 $this->assertEquals(1, $result['count']);
210 $this->assertEquals(0, $emptyResult['count']);
211 }
212
213 /**
214 * Test pledge_status option group
215 */
216 public function testOptionGroupForPledgeStatus() {
217 $pledgeOg = $this->callAPISuccess('OptionGroup', 'get', array(
218 'name' => "pledge_status",
219 ));
220 $this->assertEquals(1, $pledgeOg['count']);
221
222 $pledgeOv = $this->callAPISuccess('OptionValue', 'get', array(
223 'sequential' => 1,
224 'option_group_id' => "pledge_status",
225 ));
226 $this->assertEquals(5, $pledgeOv['count']);
227 $pledgeStatus = CRM_Utils_Array::collect('name', $pledgeOv['values']);
228 $expected = array('Completed', 'Pending', 'Cancelled', 'In Progress', 'Overdue');
229 $this->assertEquals($expected, $pledgeStatus);
230 }
231
232 /**
233 * Create 2 pledges - see if we can get by status id.
234 */
235 public function testSortParamPledge() {
236 $pledge1 = $this->callAPISuccess('pledge', 'create', $this->_params);
237 $overdueParams = array(
238 'scheduled_date' => 'first saturday of march last year',
239 'start_date' => 'first saturday of march last year',
240 'create_date' => 'first saturday of march last year',
241 );
242 $pledge2 = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
243 $params = array(
244 'pledge_is_test' => 0,
245 'rowCount' => 1,
246 );
247 $result = $this->callAPISuccess('pledge', 'get', $params);
248
249 $resultSortedAsc = $this->callAPISuccess('pledge', 'get', array(
250 'rowCount' => 1,
251 'sort' => 'start_date ASC',
252 ));
253 $resultSortedDesc = $this->callAPISuccess('pledge', 'get', array(
254 'rowCount' => 1,
255 'sort' => 'start_date DESC',
256 ));
257
258 $this->assertEquals($pledge1['id'], $result['id'], 'pledge get gets first created pledge in line ' . __LINE__);
259 $this->assertEquals($pledge2['id'], $resultSortedAsc['id'], 'Ascending pledge sort works');
260 $this->assertEquals($pledge1['id'], $resultSortedDesc['id'], 'Decending pledge sort works');
261 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge1['id']));
262 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge2['id']));
263 }
264
265 public function testCreatePledge() {
266
267 $result = $this->callAPIAndDocument('pledge', 'create', $this->_params, __FUNCTION__, __FILE__);
268 $this->assertEquals($result['values'][0]['amount'], 100.00);
269 $this->assertEquals($result['values'][0]['installments'], 5);
270 $this->assertEquals($result['values'][0]['frequency_unit'], 'year');
271 $this->assertEquals($result['values'][0]['frequency_interval'], 5);
272 $this->assertEquals($result['values'][0]['frequency_day'], 15);
273 $this->assertEquals($result['values'][0]['original_installment_amount'], 20);
274 $this->assertEquals($result['values'][0]['status_id'], 2);
275 $this->assertEquals($result['values'][0]['create_date'], date('Ymd') . '000000');
276 $this->assertEquals($result['values'][0]['start_date'], date('Ymd') . '000000');
277 $this->assertAPISuccess($result);
278 $payments = $this->callAPISuccess('PledgePayment', 'Get', array('pledge_id' => $result['id'], 'sequential' => 1));
279 $this->assertAPISuccess($payments);
280 $this->assertEquals($payments['count'], 5);
281 $shouldBeDate = CRM_Utils_Date::format(CRM_Utils_Date::intervalAdd('year', 5 * 4, $this->scheduled_date), "-");
282 $this->assertEquals(substr($shouldBeDate, 0, 10), substr($payments['values'][4]['scheduled_date'], 0, 10));
283
284 $pledgeID = array('id' => $result['id']);
285 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
286 }
287
288 /**
289 * Test that pledge with weekly schedule calculates dates correctly.
290 */
291 public function testCreatePledgeWeeklySchedule() {
292 $params = array(
293 'scheduled_date' => '20110510',
294 'frequency_unit' => 'week',
295 'frequency_day' => 3,
296 'frequency_interval' => 2,
297 );
298 $params = array_merge($this->_params, $params);
299 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
300 //ensure that correct number of payments created & last payment has the right date
301 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
302 'pledge_id' => $pledge['id'],
303 'sequential' => 1,
304 ));
305 $this->assertEquals($payments['count'], 5);
306 $this->assertEquals('2011-07-06 00:00:00', $payments['values'][4]['scheduled_date']);
307
308 $this->callAPISuccess('pledge', 'delete', array('pledge_id' => $pledge['id']));
309 }
310
311 /**
312 * Test that pledge with weekly schedule calculates dates correctly.
313 */
314 public function testCreatePledgeMontlySchedule() {
315 $params = array(
316 'scheduled_date' => '20110510',
317 'frequency_unit' => 'Month',
318 'frequency_day' => 3,
319 'frequency_interval' => 2,
320 );
321 $params = array_merge($this->_params, $params);
322 $apiResult = $this->callAPISuccess('pledge', 'create', $params);
323 }
324
325
326 /**
327 * Test creation of pledge with only one payment.
328 *
329 * Pledge status id left empty as it is not a required field
330 * http://issues.civicrm.org/jira/browse/CRM-8551
331 */
332 public function testCreatePledgeSinglePayment() {
333 $params = array(
334 'scheduled_date' => '20110510',
335 'frequency_unit' => 'week',
336 'frequency_day' => 3,
337 'frequency_interval' => 2,
338 'installments' => 1,
339 );
340
341 $params = array_merge($this->_params, $params);
342 unset($params['pledge_status_id']);
343 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
344 //ensure that correct number of payments created & last payment has the right date
345 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
346 'pledge_id' => $pledge['id'],
347 'sequential' => 1,
348 ));
349 $this->assertEquals(1, $payments['count']);
350 $this->assertEquals(2, $payments['values'][0]['status_id']);
351 $pledgeID = array('id' => $pledge['id']);
352 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
353 }
354
355 /**
356 * Test that using original_installment_amount rather than pledge_original_installment_amount works.
357 *
358 * Pledge field behaviour is a bit random & so pledge has come to try to handle both unique & non -unique fields.
359 */
360 public function testCreatePledgeWithNonUnique() {
361 $params = $this->_params;
362 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
363
364 unset($params['pledge_original_installment_amount']);
365 $result = $this->callAPISuccess('pledge', 'create', $params);
366 $pledgeDetails = $this->callAPISuccess('Pledge', 'Get', array('id' => $result['id'], 'sequential' => 1));
367 $pledge = $pledgeDetails['values'][0];
368 $this->assertEquals(100.00, $pledge['pledge_amount']);
369 $this->assertEquals('year', $pledge['pledge_frequency_unit']);
370 $this->assertEquals(5, $pledge['pledge_frequency_interval']);
371 $this->assertEquals(20, $pledge['pledge_next_pay_amount']);
372
373 $pledgeID = array('id' => $result['id']);
374 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
375 }
376
377 /**
378 * Test cancelling a pledge.
379 */
380 public function testCreateCancelPledge() {
381
382 $result = $this->callAPISuccess('pledge', 'create', $this->_params);
383 $this->assertEquals(2, $result['values'][0]['status_id']);
384 $cancelParams = array(
385 'sequential' => 1,
386 'id' => $result['id'],
387 'pledge_status_id' => 3,
388 );
389 $result = $this->callAPISuccess('pledge', 'create', $cancelParams);
390 $this->assertEquals(3, $result['values'][0]['status_id']);
391 $pledgeID = array('id' => $result['id']);
392 $this->callAPISuccess('pledge', 'delete', $pledgeID);
393 }
394
395 /**
396 * Test that status is set to pending.
397 */
398 public function testCreatePledgeNoStatus() {
399
400 $params = $this->_params;
401 unset($params['status_id']);
402 unset($params['pledge_status_id']);
403 $result = $this->callAPISuccess('pledge', 'create', $params);
404 $this->assertAPISuccess($result);
405 $this->assertEquals(2, $result['values'][0]['status_id']);
406 $pledgeID = array('pledge_id' => $result['id']);
407 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
408 }
409
410 /**
411 * Update Pledge.
412 */
413 public function testCreateUpdatePledge() {
414 // we test 'sequential' param here too
415 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId));
416 $old_params = array(
417 'id' => $pledgeID,
418 'sequential' => 1,
419 );
420 $original = $this->callAPISuccess('pledge', 'get', $old_params);
421 //Make sure it came back
422 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID);
423 //set up list of old params, verify
424 $old_contact_id = $original['values'][0]['contact_id'];
425 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
426 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
427 $old_status_id = $original['values'][0]['pledge_status'];
428
429 //check against values in CiviUnitTestCase::createPledge()
430 $this->assertEquals($old_contact_id, $this->_individualId);
431 $this->assertEquals($old_frequency_unit, 'year');
432 $this->assertEquals($old_frequency_interval, 5);
433 $this->assertEquals($old_status_id, 'Pending');
434 $params = array(
435 'id' => $pledgeID,
436 'contact_id' => $this->_individualId,
437 'pledge_status_id' => 3,
438 'amount' => 100,
439 'financial_type_id' => 1,
440 'start_date' => date('Ymd'),
441 'installments' => 10,
442 );
443
444 $pledge = $this->callAPISuccess('pledge', 'create', $params);
445 $new_params = array(
446 'id' => $pledge['id'],
447 );
448 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
449 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId);
450 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled');
451 $pledge = $this->callAPISuccess('pledge', 'delete', $new_params);
452 }
453
454 /**
455 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax.
456 *
457 * ie contribution_type_id.
458 *
459 * We test 'sequential' param here too.
460 */
461 public function testCreateUpdatePledgeLegacy() {
462 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId));
463 $old_params = array(
464 'id' => $pledgeID,
465 'sequential' => 1,
466 );
467 $original = $this->callAPISuccess('pledge', 'get', $old_params);
468 // Make sure it came back.
469 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID);
470 // Set up list of old params, verify.
471 $old_contact_id = $original['values'][0]['contact_id'];
472 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
473 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
474 $old_status_id = $original['values'][0]['pledge_status'];
475
476 // Check against values in CiviUnitTestCase::createPledge().
477 $this->assertEquals($old_contact_id, $this->_individualId);
478 $this->assertEquals($old_frequency_unit, 'year');
479 $this->assertEquals($old_frequency_interval, 5);
480 $this->assertEquals($old_status_id, 'Pending');
481 $params = array(
482 'id' => $pledgeID,
483 'contact_id' => $this->_individualId,
484 'pledge_status_id' => 3,
485 'amount' => 100,
486 'contribution_type_id' => 1,
487 'start_date' => date('Ymd'),
488 'installments' => 10,
489 );
490
491 $pledge = $this->callAPISuccess('pledge', 'create', $params);
492 $new_params = array(
493 'id' => $pledge['id'],
494 );
495 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
496 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId);
497 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled');
498 $this->callAPISuccess('pledge', 'delete', $new_params);
499 }
500
501 /**
502 * Failure test for delete without id.
503 */
504 public function testDeleteEmptyParamsPledge() {
505 $this->callAPIFailure('pledge', 'delete', array(), 'Mandatory key(s) missing from params array: id');
506 }
507
508
509 /**
510 * Failure test for invalid pledge id.
511 */
512 public function testDeleteWrongParamPledge() {
513 $params = array(
514 'pledge_source' => 'SSF',
515 );
516 $this->callAPIFailure('pledge', 'delete', $params, 'Mandatory key(s) missing from params array: id');
517 }
518
519 /**
520 * Legacy support for pledge_id.
521 */
522 public function testDeletePledge() {
523
524 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId));
525 $params = array(
526 'pledge_id' => $pledgeID,
527 );
528 $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__, __FILE__);
529 }
530
531 /**
532 * Standard is to accept id.
533 */
534 public function testDeletePledgeUseID() {
535
536 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId));
537 $params = array(
538 'id' => $pledgeID,
539 );
540 $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__, __FILE__);
541 }
542
543 /**
544 * Test to make sure empty get returns nothing.
545 *
546 * Note that the function gives incorrect results if no pledges exist as it does a
547 * contact search instead - test only checks that the get finds the one existing
548 */
549 public function testGetEmpty() {
550 $this->callAPISuccess('pledge', 'create', $this->_params);
551 $result = $this->callAPISuccess('pledge', 'get', array());
552 $this->assertAPISuccess($result, "This test is failing because it's acting like a contact get when no params set. Not sure the fix");
553 $this->assertEquals(1, $result['count']);
554 $pledgeID = array('id' => $result['id']);
555 $this->callAPISuccess('pledge', 'delete', $pledgeID);
556 }
557
558 }