3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
33 class api_v3_PledgeTest
extends CiviUnitTestCase
{
36 * Assume empty database with just civicrm_data.
38 protected $_individualId;
40 protected $_apiversion;
43 protected $scheduled_date;
44 public $DBResetRequired = TRUE;
46 public function setUp() {
47 $this->_apiversion
= 3;
49 $this->quickCleanup(array('civicrm_pledge', 'civicrm_pledge_payment'));
50 //need to set scheduled payment in advance we are running test @ midnight & it becomes unexpectedly overdue
51 //due to timezone issues
52 $this->scheduled_date
= date('Ymd', mktime(0, 0, 0, date("m"), date("d") +
2, date("y")));
53 $this->_entity
= 'Pledge';
54 $this->_individualId
= $this->individualCreate();
55 $this->_params
= array(
56 'contact_id' => $this->_individualId
,
57 'pledge_create_date' => date('Ymd'),
58 'start_date' => date('Ymd'),
59 'scheduled_date' => $this->scheduled_date
,
61 'pledge_status_id' => '2',
62 'pledge_financial_type_id' => '1',
63 'pledge_original_installment_amount' => 20,
64 'frequency_interval' => 5,
65 'frequency_unit' => 'year',
66 'frequency_day' => 15,
72 public function tearDown() {
73 $this->contactDelete($this->_individualId
);
77 * Check with complete array + custom field.
79 * Note that the test is written on purpose without any
80 * variables specific to participant so it can be replicated into other entities
81 * and / or moved to the automated test suite
83 public function testCreateWithCustom() {
84 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
86 $params = $this->_params
;
87 $params['custom_' . $ids['custom_field_id']] = "custom string";
89 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
90 $this->assertAPISuccess($result, " testCreateWithCustom ");
91 $this->assertAPISuccess($result);
92 $getParams = array('id' => $result['id'], 'return.custom_' . $ids['custom_field_id'] => 1);
93 $check = $this->callAPISuccess($this->_entity
, 'get', $getParams);
94 $this->callAPISuccess('pledge', 'delete', array('id' => $check['id']));
95 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
97 $this->customFieldDelete($ids['custom_field_id']);
98 $this->customGroupDelete($ids['custom_group_id']);
102 * Test getfields function for pledge.
104 public function testGetfieldsPledge() {
105 $result = $this->callAPISuccess('pledge', 'getfields', array('action' => 'get'));
106 $this->assertEquals(1, $result['values']['next_pay_date']['api.return']);
110 * Test get pledge api.
112 public function testGetPledge() {
114 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
116 'pledge_id' => $this->_pledge
['id'],
118 $result = $this->callAPIAndDocument('pledge', 'get', $params, __FUNCTION__
, __FILE__
);
119 $pledge = $result['values'][$this->_pledge
['id']];
120 $this->assertEquals($this->_individualId
, $pledge['contact_id']);
121 $this->assertEquals($this->_pledge
['id'], $pledge['pledge_id']);
122 $this->assertEquals(date('Y-m-d') . ' 00:00:00', $pledge['pledge_create_date']);
123 $this->assertEquals(100.00, $pledge['pledge_amount']);
124 $this->assertEquals('Pending', $pledge['pledge_status']);
125 $this->assertEquals(5, $pledge['pledge_frequency_interval']);
126 $this->assertEquals('year', $pledge['pledge_frequency_unit']);
127 $this->assertEquals(date('Y-m-d', strtotime($this->scheduled_date
)) . ' 00:00:00', $pledge['pledge_next_pay_date']);
128 $this->assertEquals($pledge['pledge_next_pay_amount'], 20.00);
131 'pledge_id' => $this->_pledge
['id'],
133 $pledge = $this->callAPISuccess('pledge', 'delete', $params2);
137 * Test 'return.pledge_financial_type' => 1 works.
139 public function testGetPledgeWithReturn() {
141 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
143 'pledge_id' => $this->_pledge
['id'],
144 'return.pledge_financial_type' => 1,
146 $result = $this->callAPISuccess('pledge', 'get', $params);
147 $pledge = $result['values'][$this->_pledge
['id']];
148 $this->callAPISuccess('pledge', 'delete', $pledge);
149 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
153 * Test 'return.pledge_contribution_type' => 1 works.
155 * This is for legacy compatibility
157 public function testGetPledgeWithReturnLegacy() {
159 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
161 'pledge_id' => $this->_pledge
['id'],
162 'return.pledge_financial_type' => 1,
164 $result = $this->callAPISuccess('pledge', 'get', $params);
165 $pledge = $result['values'][$this->_pledge
['id']];
166 $this->callAPISuccess('pledge', 'delete', $pledge);
167 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
171 * Test date legacy date filters like pledge_start_date_high.
173 public function testPledgeGetReturnFilters() {
174 $this->callAPISuccess('pledge', 'create', $this->_params
);
176 $overdueParams = array(
177 'scheduled_date' => 'first saturday of march last year',
178 'start_date' => 'first saturday of march last year',
180 $oldPledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
182 $pledgeGetParams = array();
183 $allPledges = $this->callAPISuccess('pledge', 'getcount', $pledgeGetParams);
185 $this->assertEquals(2, $allPledges, 'Check we have 2 pledges to place with in line ' . __LINE__
);
186 $pledgeGetParams['pledge_start_date_high'] = date('YmdHis', strtotime('2 days ago'));
187 $earlyPledge = $this->callAPIAndDocument('pledge', 'get', $pledgeGetParams, __FUNCTION__
, __FILE__
, "demonstrates high date filter", "GetFilterHighDate");
188 $this->assertEquals(1, $earlyPledge['count'], ' check only one returned with start date filter in line ' . __LINE__
);
189 $this->assertEquals($oldPledge['id'], $earlyPledge['id'], ' check correct pledge returned ' . __LINE__
);
193 * Create 2 pledges - see if we can get by status id.
195 public function testGetOverduePledge() {
196 $overdueParams = array(
197 'scheduled_date' => 'first saturday of march last year',
198 'start_date' => 'first saturday of march last year',
200 $this->_pledge
= $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
202 'pledge_status_id' => '6',
204 $result = $this->callAPISuccess('pledge', 'get', $params);
205 $emptyResult = $this->callAPISuccess('pledge', 'get', array(
206 'pledge_status_id' => '1',
208 $pledge = $result['values'][$this->_pledge
['id']];
209 $this->callAPISuccess('pledge', 'delete', $pledge);
210 $this->assertEquals(1, $result['count']);
211 $this->assertEquals(0, $emptyResult['count']);
216 * Create 2 pledges - see if we can get by status id.
218 public function testSortParamPledge() {
219 $pledge1 = $this->callAPISuccess('pledge', 'create', $this->_params
);
220 $overdueParams = array(
221 'scheduled_date' => 'first saturday of march last year',
222 'start_date' => 'first saturday of march last year',
223 'create_date' => 'first saturday of march last year',
225 $pledge2 = $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
227 'pledge_is_test' => 0,
230 $result = $this->callAPISuccess('pledge', 'get', $params);
232 $resultSortedAsc = $this->callAPISuccess('pledge', 'get', array(
234 'sort' => 'start_date ASC',
236 $resultSortedDesc = $this->callAPISuccess('pledge', 'get', array(
238 'sort' => 'start_date DESC',
241 $this->assertEquals($pledge1['id'], $result['id'], 'pledge get gets first created pledge in line ' . __LINE__
);
242 $this->assertEquals($pledge2['id'], $resultSortedAsc['id'], 'Ascending pledge sort works');
243 $this->assertEquals($pledge1['id'], $resultSortedDesc['id'], 'Decending pledge sort works');
244 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge1['id']));
245 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge2['id']));
248 public function testCreatePledge() {
250 $result = $this->callAPIAndDocument('pledge', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
251 $this->assertEquals($result['values'][0]['amount'], 100.00);
252 $this->assertEquals($result['values'][0]['installments'], 5);
253 $this->assertEquals($result['values'][0]['frequency_unit'], 'year');
254 $this->assertEquals($result['values'][0]['frequency_interval'], 5);
255 $this->assertEquals($result['values'][0]['frequency_day'], 15);
256 $this->assertEquals($result['values'][0]['original_installment_amount'], 20);
257 $this->assertEquals($result['values'][0]['status_id'], 2);
258 $this->assertEquals($result['values'][0]['create_date'], date('Ymd') . '000000');
259 $this->assertEquals($result['values'][0]['start_date'], date('Ymd') . '000000');
260 $this->assertAPISuccess($result);
261 $payments = $this->callAPISuccess('PledgePayment', 'Get', array('pledge_id' => $result['id'], 'sequential' => 1));
262 $this->assertAPISuccess($payments);
263 $this->assertEquals($payments['count'], 5);
264 $shouldBeDate = CRM_Utils_Date
::format(CRM_Utils_Date
::intervalAdd('year', 5 * 4, $this->scheduled_date
), "-");
265 $this->assertEquals(substr($shouldBeDate, 0, 10), substr($payments['values'][4]['scheduled_date'], 0, 10));
267 $pledgeID = array('id' => $result['id']);
268 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
272 * Test that pledge with weekly schedule calculates dates correctly.
274 public function testCreatePledgeWeeklySchedule() {
276 'scheduled_date' => '20110510',
277 'frequency_unit' => 'week',
278 'frequency_day' => 3,
279 'frequency_interval' => 2,
281 $params = array_merge($this->_params
, $params);
282 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
283 //ensure that correct number of payments created & last payment has the right date
284 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
285 'pledge_id' => $pledge['id'],
288 $this->assertEquals($payments['count'], 5);
289 $this->assertEquals('2011-07-06 00:00:00', $payments['values'][4]['scheduled_date']);
291 $this->callAPISuccess('pledge', 'delete', array('pledge_id' => $pledge['id']));
295 * Test that pledge with weekly schedule calculates dates correctly.
297 public function testCreatePledgeMontlySchedule() {
299 'scheduled_date' => '20110510',
300 'frequency_unit' => 'Month',
301 'frequency_day' => 3,
302 'frequency_interval' => 2,
304 $params = array_merge($this->_params
, $params);
305 $apiResult = $this->callAPISuccess('pledge', 'create', $params);
310 * Test creation of pledge with only one payment.
312 * Pledge status id left empty as it is not a required field
313 * http://issues.civicrm.org/jira/browse/CRM-8551
315 public function testCreatePledgeSinglePayment() {
317 'scheduled_date' => '20110510',
318 'frequency_unit' => 'week',
319 'frequency_day' => 3,
320 'frequency_interval' => 2,
324 $params = array_merge($this->_params
, $params);
325 unset($params['pledge_status_id']);
326 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
327 //ensure that correct number of payments created & last payment has the right date
328 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
329 'pledge_id' => $pledge['id'],
332 $this->assertEquals(1, $payments['count']);
333 $this->assertEquals(2, $payments['values'][0]['status_id']);
334 $pledgeID = array('id' => $pledge['id']);
335 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
339 * Test that using original_installment_amount rather than pledge_original_installment_amount works.
341 * Pledge field behaviour is a bit random & so pledge has come to try to handle both unique & non -unique fields.
343 public function testCreatePledgeWithNonUnique() {
344 $params = $this->_params
;
345 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
347 unset($params['pledge_original_installment_amount']);
348 $result = $this->callAPISuccess('pledge', 'create', $params);
349 $pledgeDetails = $this->callAPISuccess('Pledge', 'Get', array('id' => $result['id'], 'sequential' => 1));
350 $pledge = $pledgeDetails['values'][0];
351 $this->assertEquals(100.00, $pledge['pledge_amount']);
352 $this->assertEquals('year', $pledge['pledge_frequency_unit']);
353 $this->assertEquals(5, $pledge['pledge_frequency_interval']);
354 $this->assertEquals(20, $pledge['pledge_next_pay_amount']);
356 $pledgeID = array('id' => $result['id']);
357 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
361 * Test cancelling a pledge.
363 public function testCreateCancelPledge() {
365 $result = $this->callAPISuccess('pledge', 'create', $this->_params
);
366 $this->assertEquals(2, $result['values'][0]['status_id']);
367 $cancelParams = array(
369 'id' => $result['id'],
370 'pledge_status_id' => 3,
372 $result = $this->callAPISuccess('pledge', 'create', $cancelParams);
373 $this->assertEquals(3, $result['values'][0]['status_id']);
374 $pledgeID = array('id' => $result['id']);
375 $this->callAPISuccess('pledge', 'delete', $pledgeID);
379 * Test that status is set to pending.
381 public function testCreatePledgeNoStatus() {
383 $params = $this->_params
;
384 unset($params['status_id']);
385 unset($params['pledge_status_id']);
386 $result = $this->callAPISuccess('pledge', 'create', $params);
387 $this->assertAPISuccess($result);
388 $this->assertEquals(2, $result['values'][0]['status_id']);
389 $pledgeID = array('pledge_id' => $result['id']);
390 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
396 public function testCreateUpdatePledge() {
397 // we test 'sequential' param here too
398 $pledgeID = $this->pledgeCreate($this->_individualId
);
403 $original = $this->callAPISuccess('pledge', 'get', $old_params);
404 //Make sure it came back
405 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID);
406 //set up list of old params, verify
407 $old_contact_id = $original['values'][0]['contact_id'];
408 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
409 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
410 $old_status_id = $original['values'][0]['pledge_status'];
412 //check against values in CiviUnitTestCase::createPledge()
413 $this->assertEquals($old_contact_id, $this->_individualId
);
414 $this->assertEquals($old_frequency_unit, 'year');
415 $this->assertEquals($old_frequency_interval, 5);
416 $this->assertEquals($old_status_id, 'Pending');
419 'contact_id' => $this->_individualId
,
420 'pledge_status_id' => 3,
422 'financial_type_id' => 1,
423 'start_date' => date('Ymd'),
424 'installments' => 10,
427 $pledge = $this->callAPISuccess('pledge', 'create', $params);
429 'id' => $pledge['id'],
431 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
432 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
);
433 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled');
434 $pledge = $this->callAPISuccess('pledge', 'delete', $new_params);
438 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax.
440 * ie contribution_type_id.
442 * We test 'sequential' param here too.
444 public function testCreateUpdatePledgeLegacy() {
445 $pledgeID = $this->pledgeCreate($this->_individualId
);
450 $original = $this->callAPISuccess('pledge', 'get', $old_params);
451 // Make sure it came back.
452 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID);
453 // Set up list of old params, verify.
454 $old_contact_id = $original['values'][0]['contact_id'];
455 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
456 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
457 $old_status_id = $original['values'][0]['pledge_status'];
459 // Check against values in CiviUnitTestCase::createPledge().
460 $this->assertEquals($old_contact_id, $this->_individualId
);
461 $this->assertEquals($old_frequency_unit, 'year');
462 $this->assertEquals($old_frequency_interval, 5);
463 $this->assertEquals($old_status_id, 'Pending');
466 'contact_id' => $this->_individualId
,
467 'pledge_status_id' => 3,
469 'contribution_type_id' => 1,
470 'start_date' => date('Ymd'),
471 'installments' => 10,
474 $pledge = $this->callAPISuccess('pledge', 'create', $params);
476 'id' => $pledge['id'],
478 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
479 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
);
480 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled');
481 $this->callAPISuccess('pledge', 'delete', $new_params);
485 * Failure test for delete without id.
487 public function testDeleteEmptyParamsPledge() {
488 $this->callAPIFailure('pledge', 'delete', array(), 'Mandatory key(s) missing from params array: id');
493 * Failure test for invalid pledge id.
495 public function testDeleteWrongParamPledge() {
497 'pledge_source' => 'SSF',
499 $this->callAPIFailure('pledge', 'delete', $params, 'Mandatory key(s) missing from params array: id');
503 * Legacy support for pledge_id.
505 public function testDeletePledge() {
507 $pledgeID = $this->pledgeCreate($this->_individualId
);
509 'pledge_id' => $pledgeID,
511 $result = $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
515 * Standard is to accept id.
517 public function testDeletePledgeUseID() {
519 $pledgeID = $this->pledgeCreate($this->_individualId
);
523 $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
527 * Test to make sure empty get returns nothing.
529 * Note that the function gives incorrect results if no pledges exist as it does a
530 * contact search instead - test only checks that the get finds the one existing
532 public function testGetEmpty() {
533 $this->callAPISuccess('pledge', 'create', $this->_params
);
534 $result = $this->callAPISuccess('pledge', 'get', array());
535 $this->assertAPISuccess($result, "This test is failing because it's acting like a contact get when no params set. Not sure the fix");
536 $this->assertEquals(1, $result['count']);
537 $pledgeID = array('id' => $result['id']);
538 $this->callAPISuccess('pledge', 'delete', $pledgeID);