3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test class for Pledge API - civicrm_pledge_*
33 * @package CiviCRM_APIv3
35 class api_v3_PledgeTest
extends CiviUnitTestCase
{
38 * Assume empty database with just civicrm_data
40 protected $_individualId;
42 protected $_apiversion;
45 protected $scheduled_date;
46 public $DBResetRequired = TRUE;
48 public function setUp() {
49 $this->_apiversion
= 3;
51 $this->quickCleanup(array('civicrm_pledge', 'civicrm_pledge_payment'));
52 //need to set scheduled payment in advance we are running test @ midnight & it becomes unexpectedly overdue
53 //due to timezone issues
54 $this->scheduled_date
= date('Ymd', mktime(0, 0, 0, date("m"), date("d") +
2, date("y")));
55 $this->_entity
= 'Pledge';
56 $this->_individualId
= $this->individualCreate();
57 $this->_params
= array(
58 'contact_id' => $this->_individualId
,
59 'pledge_create_date' => date('Ymd'),
60 'start_date' => date('Ymd'),
61 'scheduled_date' => $this->scheduled_date
,
63 'pledge_status_id' => '2',
64 'pledge_financial_type_id' => '1',
65 'pledge_original_installment_amount' => 20,
66 'frequency_interval' => 5,
67 'frequency_unit' => 'year',
68 'frequency_day' => 15,
74 public function tearDown() {
75 $this->contactDelete($this->_individualId
);
78 ///////////////// civicrm_pledge_get methods
81 * Check with complete array + custom field
82 * Note that the test is written on purpose without any
83 * variables specific to participant so it can be replicated into other entities
84 * and / or moved to the automated test suite
86 public function testCreateWithCustom() {
87 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
89 $params = $this->_params
;
90 $params['custom_' . $ids['custom_field_id']] = "custom string";
92 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
93 $this->assertAPISuccess($result, " testCreateWithCustom ");
94 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
95 $getparams = array('id' => $result['id'], 'return.custom_' . $ids['custom_field_id'] => 1);
96 $check = $this->callAPISuccess($this->_entity
, 'get', $getparams);
97 $this->callAPISuccess('pledge', 'delete', array('id' => $check['id']));
98 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
100 $this->customFieldDelete($ids['custom_field_id']);
101 $this->customGroupDelete($ids['custom_group_id']);
106 public function testgetfieldspledge() {
107 $result = $this->callAPISuccess('pledge', 'getfields', array('action' => 'get'));
108 $this->assertEquals(1, $result['values']['next_pay_date']['api.return']);
111 public function testGetPledge() {
113 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
115 'pledge_id' => $this->_pledge
['id'],
117 $result = $this->callAPIAndDocument('pledge', 'get', $params, __FUNCTION__
, __FILE__
);
118 $pledge = $result['values'][$this->_pledge
['id']];
119 $this->assertEquals($this->_individualId
, $pledge['contact_id'], 'in line' . __LINE__
);
120 $this->assertEquals($this->_pledge
['id'], $pledge['pledge_id'], 'in line' . __LINE__
);
121 $this->assertEquals(date('Y-m-d') . ' 00:00:00', $pledge['pledge_create_date'], 'in line' . __LINE__
);
122 $this->assertEquals(100.00, $pledge['pledge_amount'], 'in line' . __LINE__
);
123 $this->assertEquals('Pending', $pledge['pledge_status'], 'in line' . __LINE__
);
124 $this->assertEquals(5, $pledge['pledge_frequency_interval'], 'in line' . __LINE__
);
125 $this->assertEquals('year', $pledge['pledge_frequency_unit'], 'in line' . __LINE__
);
126 $this->assertEquals(date('Y-m-d', strtotime($this->scheduled_date
)) . ' 00:00:00', $pledge['pledge_next_pay_date'], 'in line' . __LINE__
);
127 $this->assertEquals($pledge['pledge_next_pay_amount'], 20.00, 'in line' . __LINE__
);
130 'pledge_id' => $this->_pledge
['id'],
132 $pledge = $this->callAPISuccess('pledge', 'delete', $params2);
136 * Test 'return.pledge_financial_type' => 1 works
138 public function testGetPledgewithReturn() {
140 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
142 'pledge_id' => $this->_pledge
['id'],
143 'return.pledge_financial_type' => 1,
145 $result = $this->callAPISuccess('pledge', 'get', $params);
146 $pledge = $result['values'][$this->_pledge
['id']];
147 $this->callAPISuccess('pledge', 'delete', $pledge);
148 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
152 * Test 'return.pledge_contribution_type' => 1 works
153 * This is for legacy compatibility
155 public function testGetPledgewithReturnLegacy() {
157 $this->_pledge
= $this->callAPISuccess('pledge', 'create', $this->_params
);
159 'pledge_id' => $this->_pledge
['id'],
160 'return.pledge_financial_type' => 1,
162 $result = $this->callAPISuccess('pledge', 'get', $params);
163 $pledge = $result['values'][$this->_pledge
['id']];
164 $this->callAPISuccess('pledge', 'delete', $pledge);
165 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
168 public function testPledgeGetReturnFilters() {
169 $oldPledge = $this->callAPISuccess('pledge', 'create', $this->_params
);
171 $overdueParams = array(
172 'scheduled_date' => 'first saturday of march last year',
173 'start_date' => 'first saturday of march last year',
175 $oldPledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
177 $pledgeGetParams = array();
178 $allPledges = $this->callAPISuccess('pledge', 'getcount', $pledgeGetParams);
180 $this->assertEquals(2, $allPledges, 'Check we have 2 pledges to place with in line ' . __LINE__
);
181 $pledgeGetParams['pledge_start_date_high'] = date('YmdHis', strtotime('2 days ago'));
182 $earlyPledge = $this->callAPIAndDocument('pledge', 'get', $pledgeGetParams, __FUNCTION__
, __FILE__
, "demonstrates high date filter", "GetFilterHighDate");
183 $this->assertEquals(1, $earlyPledge['count'], ' check only one returned with start date filter in line ' . __LINE__
);
184 $this->assertEquals($oldPledge['id'], $earlyPledge['id'], ' check correct pledge returned ' . __LINE__
);
188 * create 2 pledges - see if we can get by status id
190 public function testGetOverduePledge() {
191 $overdueParams = array(
192 'scheduled_date' => 'first saturday of march last year',
193 'start_date' => 'first saturday of march last year',
195 $this->_pledge
= $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
197 'pledge_status_id' => '6',
199 $result = $this->callAPISuccess('pledge', 'get', $params);
200 $emptyResult = $this->callAPISuccess('pledge', 'get', array(
201 'pledge_status_id' => '1',
203 $pledge = $result['values'][$this->_pledge
['id']];
204 $this->callAPISuccess('pledge', 'delete', $pledge);
205 $this->assertEquals(1, $result['count']);
206 $this->assertEquals(0, $emptyResult['count']);
211 * create 2 pledges - see if we can get by status id
213 public function testSortParamPledge() {
214 $pledge1 = $this->callAPISuccess('pledge', 'create', $this->_params
);
215 $overdueParams = array(
216 'scheduled_date' => 'first saturday of march last year',
217 'start_date' => 'first saturday of march last year',
218 'create_date' => 'first saturday of march last year',
220 $pledge2 = $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
222 'pledge_is_test' => 0,
225 $result = $this->callAPISuccess('pledge', 'get', $params);
227 $resultSortedAsc = $this->callAPISuccess('pledge', 'get', array(
229 'sort' => 'start_date ASC',
231 $resultSortedDesc = $this->callAPISuccess('pledge', 'get', array(
233 'sort' => 'start_date DESC',
236 $this->assertEquals($pledge1['id'], $result['id'], 'pledge get gets first created pledge in line ' . __LINE__
);
237 $this->assertEquals($pledge2['id'], $resultSortedAsc['id'], 'Ascending pledge sort works');
238 $this->assertEquals($pledge1['id'], $resultSortedDesc['id'], 'Decending pledge sort works');
239 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge1['id']));
240 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge2['id']));
243 public function testCreatePledge() {
245 $result = $this->callAPIAndDocument('pledge', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
246 $this->assertEquals($result['values'][0]['amount'], 100.00, 'In line ' . __LINE__
);
247 $this->assertEquals($result['values'][0]['installments'], 5, 'In line ' . __LINE__
);
248 $this->assertEquals($result['values'][0]['frequency_unit'], 'year', 'In line ' . __LINE__
);
249 $this->assertEquals($result['values'][0]['frequency_interval'], 5, 'In line ' . __LINE__
);
250 $this->assertEquals($result['values'][0]['frequency_day'], 15, 'In line ' . __LINE__
);
251 $this->assertEquals($result['values'][0]['original_installment_amount'], 20, 'In line ' . __LINE__
);
252 $this->assertEquals($result['values'][0]['status_id'], 2, 'In line ' . __LINE__
);
253 $this->assertEquals($result['values'][0]['create_date'], date('Ymd') . '000000', 'In line ' . __LINE__
);
254 $this->assertEquals($result['values'][0]['start_date'], date('Ymd') . '000000', 'In line ' . __LINE__
);
255 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
256 $payments = $this->callAPISuccess('PledgePayment', 'Get', array('pledge_id' => $result['id'], 'sequential' => 1));
257 $this->assertAPISuccess($payments, 'In line ' . __LINE__
);
258 $this->assertEquals($payments['count'], 5, 'In line ' . __LINE__
);
259 require_once 'CRM/Utils/Date.php';
260 $shouldBeDate = CRM_Utils_Date
::format(CRM_Utils_Date
::intervalAdd('year', 5 * 4, $this->scheduled_date
), "-");
261 $this->assertEquals(substr($shouldBeDate, 0, 10), substr($payments['values'][4]['scheduled_date'], 0, 10), 'In line ' . __LINE__
);
263 $pledgeID = array('id' => $result['id']);
264 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
268 * Test that pledge with weekly schedule calculates dates correctly
270 public function testCreatePledgeWeeklySchedule() {
272 'scheduled_date' => '20110510',
273 'frequency_unit' => 'week',
274 'frequency_day' => 3,
275 'frequency_interval' => 2,
277 $params = array_merge($this->_params
, $params);
278 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
279 //ensure that correct number of payments created & last payment has the right date
280 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
281 'pledge_id' => $pledge['id'],
284 $this->assertEquals($payments['count'], 5, 'In line ' . __LINE__
);
285 $this->assertEquals('2011-07-06 00:00:00', $payments['values'][4]['scheduled_date'], 'In line ' . __LINE__
);
287 $this->callAPISuccess('pledge', 'delete', array('pledge_id' => $pledge['id']));
291 * Test that pledge with weekly schedule calculates dates correctly
293 public function testCreatePledgeMontlySchedule() {
295 'scheduled_date' => '20110510',
296 'frequency_unit' => 'Month',
297 'frequency_day' => 3,
298 'frequency_interval' => 2,
300 $params = array_merge($this->_params
, $params);
301 $apiResult = $this->callAPISuccess('pledge', 'create', $params);
306 * Test creation of pledge with only one payment.
308 * Pledge status id left empty as it is not a required field
309 * http://issues.civicrm.org/jira/browse/CRM-8551
311 public function testCreatePledgeSinglePayment() {
313 'scheduled_date' => '20110510',
314 'frequency_unit' => 'week',
315 'frequency_day' => 3,
316 'frequency_interval' => 2,
320 $params = array_merge($this->_params
, $params);
321 unset($params['pledge_status_id']);
322 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
323 //ensure that correct number of payments created & last payment has the right date
324 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
325 'pledge_id' => $pledge['id'],
328 $this->assertEquals(1, $payments['count'], 'In line ' . __LINE__
);
329 $this->assertEquals(2, $payments['values'][0]['status_id'], 'In line ' . __LINE__
);
330 $pledgeID = array('id' => $pledge['id']);
331 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
335 * test that using original_installment_amount rather than pledge_original_installment_amount works
336 * Pledge field behaviour is a bit random & so pledge has come to try to handle both unique & non -unique fields
338 public function testCreatePledgeWithNonUnique() {
339 $params = $this->_params
;
340 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
342 unset($params['pledge_original_installment_amount']);
343 $result = $this->callAPISuccess('pledge', 'create', $params);
344 $pledgeDetails = $this->callAPISuccess('Pledge', 'Get', array('id' => $result['id'], 'sequential' => 1));
345 $pledge = $pledgeDetails['values'][0];
346 $this->assertEquals(100.00, $pledge['pledge_amount'], 'In line ' . __LINE__
);
347 $this->assertEquals('year', $pledge['pledge_frequency_unit'], 'In line ' . __LINE__
);
348 $this->assertEquals(5, $pledge['pledge_frequency_interval'], 'In line ' . __LINE__
);
349 $this->assertEquals(20, $pledge['pledge_next_pay_amount'], 'In line ' . __LINE__
);
351 $pledgeID = array('id' => $result['id']);
352 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
355 public function testCreateCancelPledge() {
357 $result = $this->callAPISuccess('pledge', 'create', $this->_params
);
358 $this->assertEquals(2, $result['values'][0]['status_id'], "in line " . __LINE__
);
359 $cancelparams = array('sequential' => 1, 'id' => $result['id'], 'pledge_status_id' => 3);
360 $result = $this->callAPISuccess('pledge', 'create', $cancelparams);
361 $this->assertEquals(3, $result['values'][0]['status_id'], "in line " . __LINE__
);
362 $pledgeID = array('id' => $result['id']);
363 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
367 * Test that status is set to pending
369 public function testCreatePledgeNoStatus() {
371 $params = $this->_params
;
372 unset($params['status_id']);
373 unset($params['pledge_status_id']);
374 $result = $this->callAPISuccess('pledge', 'create', $params);
375 $this->assertAPISuccess($result, "in line " . __LINE__
);
376 $this->assertEquals(2, $result['values'][0]['status_id'], "in line " . __LINE__
);
377 $pledgeID = array('pledge_id' => $result['id']);
378 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
384 public function testCreateUpdatePledge() {
385 // we test 'sequential' param here too
386 $pledgeID = $this->pledgeCreate($this->_individualId
);
391 $original = $this->callAPISuccess('pledge', 'get', $old_params);
392 //Make sure it came back
393 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID, 'In line ' . __LINE__
);
394 //set up list of old params, verify
395 $old_contact_id = $original['values'][0]['contact_id'];
396 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
397 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
398 $old_status_id = $original['values'][0]['pledge_status'];
400 //check against values in CiviUnitTestCase::createPledge()
401 $this->assertEquals($old_contact_id, $this->_individualId
, 'In line ' . __LINE__
);
402 $this->assertEquals($old_frequency_unit, 'year', 'In line ' . __LINE__
);
403 $this->assertEquals($old_frequency_interval, 5, 'In line ' . __LINE__
);
404 $this->assertEquals($old_status_id, 'Pending', 'In line ' . __LINE__
);
407 'contact_id' => $this->_individualId
,
408 'pledge_status_id' => 3,
410 'financial_type_id' => 1,
411 'start_date' => date('Ymd'),
412 'installments' => 10,
415 $pledge = $this->callAPISuccess('pledge', 'create', $params);
417 'id' => $pledge['id'],
419 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
420 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
421 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled', 'In line ' . __LINE__
);
422 $pledge = $this->callAPISuccess('pledge', 'delete', $new_params);
426 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax
427 * ie contribution_type_id
429 public function testCreateUpdatePledgeLegacy() {
431 // we test 'sequential' param here too
432 $pledgeID = $this->pledgeCreate($this->_individualId
);
437 $original = $this->callAPISuccess('pledge', 'get', $old_params);
438 //Make sure it came back
439 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID, 'In line ' . __LINE__
);
440 //set up list of old params, verify
441 $old_contact_id = $original['values'][0]['contact_id'];
442 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
443 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
444 $old_status_id = $original['values'][0]['pledge_status'];
446 //check against values in CiviUnitTestCase::createPledge()
447 $this->assertEquals($old_contact_id, $this->_individualId
, 'In line ' . __LINE__
);
448 $this->assertEquals($old_frequency_unit, 'year', 'In line ' . __LINE__
);
449 $this->assertEquals($old_frequency_interval, 5, 'In line ' . __LINE__
);
450 $this->assertEquals($old_status_id, 'Pending', 'In line ' . __LINE__
);
453 'contact_id' => $this->_individualId
,
454 'pledge_status_id' => 3,
456 'contribution_type_id' => 1,
457 'start_date' => date('Ymd'),
458 'installments' => 10,
461 $pledge = $this->callAPISuccess('pledge', 'create', $params);
463 'id' => $pledge['id'],
465 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
466 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
467 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled', 'In line ' . __LINE__
);
468 $pledge = $this->callAPISuccess('pledge', 'delete', $new_params);
471 ///////////////// civicrm_pledge_delete methods
472 public function testDeleteEmptyParamsPledge() {
473 $pledge = $this->callAPIFailure('pledge', 'delete', array(), 'Mandatory key(s) missing from params array: id');
477 public function testDeleteWrongParamPledge() {
479 'pledge_source' => 'SSF',
481 $pledge = $this->callAPIFailure('pledge', 'delete', $params, 'Mandatory key(s) missing from params array: id');
485 * Legacy support for pledge_id
487 public function testDeletePledge() {
489 $pledgeID = $this->pledgeCreate($this->_individualId
);
491 'pledge_id' => $pledgeID,
493 $result = $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
497 * Std is to accept id
499 public function testDeletePledgeUseID() {
501 $pledgeID = $this->pledgeCreate($this->_individualId
);
505 $result = $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
509 * Test to make sure empty get returns nothing
510 * Note that the function gives incorrect results if no pledges exist as it does a
511 * contact search instead - test only checks that the get finds the one existing
513 public function testGetEmpty() {
514 $result = $this->callAPISuccess('pledge', 'create', $this->_params
);
515 $result = $this->callAPISuccess('pledge', 'get', array());
516 $this->assertAPISuccess($result, "This test is failing because it's acting like a contact get when no params set. Not sure the fix");
517 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
518 $pledgeID = array('id' => $result['id']);
519 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);