3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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_PledgeTest
extends CiviUnitTestCase
{
37 * Assume empty database with just civicrm_data.
39 protected $_individualId;
41 protected $_apiversion;
44 protected $scheduled_date;
45 public $DBResetRequired = TRUE;
47 public function setUp() {
48 $this->_apiversion
= 3;
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
,
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,
73 public function tearDown() {
74 $this->contactDelete($this->_individualId
);
78 * Check with complete array + custom field.
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
84 public function testCreateWithCustom() {
85 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
87 $params = $this->_params
;
88 $params['custom_' . $ids['custom_field_id']] = "custom string";
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']]);
98 $this->customFieldDelete($ids['custom_field_id']);
99 $this->customGroupDelete($ids['custom_group_id']);
103 * Test getfields function for pledge.
105 public function testGetfieldsPledge() {
106 $result = $this->callAPISuccess('pledge', 'getfields', array('action' => 'get'));
107 $this->assertEquals(1, $result['values']['next_pay_date']['api.return']);
111 * Test get pledge api.
113 public function testGetPledge() {
115 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
117 'pledge_id' => $this->_pledge
['id'],
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);
132 'pledge_id' => $this->_pledge
['id'],
134 $pledge = $this->callAPISuccess('pledge', 'delete', $params2);
138 * Test 'return.pledge_financial_type' => 1 works.
140 public function testGetPledgeWithReturn() {
142 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
144 'pledge_id' => $this->_pledge
['id'],
145 'return.pledge_financial_type' => 1,
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']);
154 * Test 'return.pledge_contribution_type' => 1 works.
156 * This is for legacy compatibility
158 public function testGetPledgeWithReturnLegacy() {
160 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
162 'pledge_id' => $this->_pledge
['id'],
163 'return.pledge_financial_type' => 1,
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']);
172 * Test date legacy date filters like pledge_start_date_high.
174 public function testPledgeGetReturnFilters() {
175 $this->callAPISuccess('pledge', 'create', $this->_params
);
177 $overdueParams = array(
178 'scheduled_date' => 'first saturday of march last year',
179 'start_date' => 'first saturday of march last year',
181 $oldPledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
183 $pledgeGetParams = array();
184 $allPledges = $this->callAPISuccess('pledge', 'getcount', $pledgeGetParams);
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__
);
194 * Create 2 pledges - see if we can get by status id.
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',
201 $this->_pledge
= $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
203 $result = $this->callAPISuccess('pledge', 'get', array('status_id' => 'Overdue'));
204 $emptyResult = $this->callAPISuccess('pledge', 'get', array(
205 'pledge_status_id' => '1',
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']);
214 * Test pledge_status option group
216 public function testOptionGroupForPledgeStatus() {
217 $pledgeOg = $this->callAPISuccess('OptionGroup', 'get', array(
218 'name' => "pledge_status",
220 $this->assertEquals(1, $pledgeOg['count']);
222 $pledgeOv = $this->callAPISuccess('OptionValue', 'get', array(
224 'option_group_id' => "pledge_status",
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);
233 * Create 2 pledges - see if we can get by status id.
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',
242 $pledge2 = $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
244 'pledge_is_test' => 0,
247 $result = $this->callAPISuccess('pledge', 'get', $params);
249 $resultSortedAsc = $this->callAPISuccess('pledge', 'get', array(
251 'sort' => 'start_date ASC',
253 $resultSortedDesc = $this->callAPISuccess('pledge', 'get', array(
255 'sort' => 'start_date DESC',
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']));
265 public function testCreatePledge() {
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));
284 $pledgeID = array('id' => $result['id']);
285 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
289 * Test that pledge with weekly schedule calculates dates correctly.
291 public function testCreatePledgeWeeklySchedule() {
293 'scheduled_date' => '20110510',
294 'frequency_unit' => 'week',
295 'frequency_day' => 3,
296 'frequency_interval' => 2,
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'],
305 $this->assertEquals($payments['count'], 5);
306 $this->assertEquals('2011-07-06 00:00:00', $payments['values'][4]['scheduled_date']);
308 $this->callAPISuccess('pledge', 'delete', array('pledge_id' => $pledge['id']));
312 * Test that pledge with weekly schedule calculates dates correctly.
314 public function testCreatePledgeMontlySchedule() {
316 'scheduled_date' => '20110510',
317 'frequency_unit' => 'Month',
318 'frequency_day' => 3,
319 'frequency_interval' => 2,
321 $params = array_merge($this->_params
, $params);
322 $apiResult = $this->callAPISuccess('pledge', 'create', $params);
327 * Test creation of pledge with only one payment.
329 * Pledge status id left empty as it is not a required field
330 * http://issues.civicrm.org/jira/browse/CRM-8551
332 public function testCreatePledgeSinglePayment() {
334 'scheduled_date' => '20110510',
335 'frequency_unit' => 'week',
336 'frequency_day' => 3,
337 'frequency_interval' => 2,
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'],
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);
356 * Test that using original_installment_amount rather than pledge_original_installment_amount works.
358 * Pledge field behaviour is a bit random & so pledge has come to try to handle both unique & non -unique fields.
360 public function testCreatePledgeWithNonUnique() {
361 $params = $this->_params
;
362 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
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']);
373 $pledgeID = array('id' => $result['id']);
374 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
378 * Test cancelling a pledge.
380 public function testCreateCancelPledge() {
382 $result = $this->callAPISuccess('pledge', 'create', $this->_params
);
383 $this->assertEquals(2, $result['values'][0]['status_id']);
384 $cancelParams = array(
386 'id' => $result['id'],
387 'pledge_status_id' => 3,
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);
396 * Test that status is set to pending.
398 public function testCreatePledgeNoStatus() {
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);
413 public function testCreateUpdatePledge() {
414 // we test 'sequential' param here too
415 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
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'];
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');
436 'contact_id' => $this->_individualId
,
437 'pledge_status_id' => 3,
439 'financial_type_id' => 1,
440 'start_date' => date('Ymd'),
441 'installments' => 10,
444 $pledge = $this->callAPISuccess('pledge', 'create', $params);
446 'id' => $pledge['id'],
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);
455 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax.
457 * ie contribution_type_id.
459 * We test 'sequential' param here too.
461 public function testCreateUpdatePledgeLegacy() {
462 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
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'];
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');
483 'contact_id' => $this->_individualId
,
484 'pledge_status_id' => 3,
486 'contribution_type_id' => 1,
487 'start_date' => date('Ymd'),
488 'installments' => 10,
491 $pledge = $this->callAPISuccess('pledge', 'create', $params);
493 'id' => $pledge['id'],
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);
502 * Failure test for delete without id.
504 public function testDeleteEmptyParamsPledge() {
505 $this->callAPIFailure('pledge', 'delete', array(), 'Mandatory key(s) missing from params array: id');
510 * Failure test for invalid pledge id.
512 public function testDeleteWrongParamPledge() {
514 'pledge_source' => 'SSF',
516 $this->callAPIFailure('pledge', 'delete', $params, 'Mandatory key(s) missing from params array: id');
520 * Legacy support for pledge_id.
522 public function testDeletePledge() {
524 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
526 'pledge_id' => $pledgeID,
528 $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
532 * Standard is to accept id.
534 public function testDeletePledgeUseID() {
536 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
540 $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
544 * Test to make sure empty get returns nothing.
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
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);