3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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;
47 public $_eNoticeCompliant = FALSE;
50 $this->_apiversion
= 3;
52 $this->quickCleanup(array('civicrm_pledge', 'civicrm_pledge_payment'));
53 //need to set scheduled payment in advance we are running test @ midnight & it becomes unexpectedly overdue
54 //due to timezone issues
55 $this->scheduled_date
= date('Ymd', mktime(0, 0, 0, date("m"), date("d") +
2, date("y")));
56 $this->_entity
= 'Pledge';
57 $this->_individualId
= $this->individualCreate(NULL);
58 $this->_params
= array(
59 'contact_id' => $this->_individualId
,
60 'pledge_create_date' => date('Ymd'),
61 'start_date' => date('Ymd'),
62 'scheduled_date' => $this->scheduled_date
,
64 'pledge_status_id' => '2',
65 'pledge_financial_type_id' => '1',
66 'pledge_original_installment_amount' => 20,
67 'frequency_interval' => 5,
68 'frequency_unit' => 'year',
69 'frequency_day' => 15,
72 'version' => $this->_apiversion
,
77 $this->contactDelete($this->_individualId
);
80 ///////////////// civicrm_pledge_get methods
83 * check with complete array + custom field
84 * Note that the test is written on purpose without any
85 * variables specific to participant so it can be replicated into other entities
86 * and / or moved to the automated test suite
88 function testCreateWithCustom() {
89 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
91 $params = $this->_params
;
92 $params['custom_' . $ids['custom_field_id']] = "custom string";
94 $result = civicrm_api($this->_entity
, 'create', $params);
95 $this->assertAPISuccess($result, " testCreateWithCustom ");
96 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
97 $getparams = array('version' => 3, 'id' => $result['id'], 'return.custom_' . $ids['custom_field_id'] => 1);
98 $check = civicrm_api($this->_entity
, 'get', $getparams);
99 civicrm_api('pledge', 'delete', array('id' => $check['values'][$check['id']], 'version' => 3));
100 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
102 $this->customFieldDelete($ids['custom_field_id']);
103 $this->customGroupDelete($ids['custom_group_id']);
109 function testgetfieldspledge() {
110 $result = civicrm_api('pledge', 'getfields', array('version' => 3, 'action' => 'get'));
111 $this->assertEquals(1, $result['values']['next_pay_date']['api.return']);
114 function testGetPledge() {
117 $this->_pledge
= civicrm_api('pledge', 'create', $this->_params
);
119 'pledge_id' => $this->_pledge
['id'],
120 'version' => $this->_apiversion
,
122 $result = civicrm_api('pledge', 'get', $params);
123 $pledge = $result['values'][$this->_pledge
['id']];
124 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
125 $this->assertEquals($this->_individualId
, $pledge['contact_id'], 'in line' . __LINE__
);
126 $this->assertEquals($this->_pledge
['id'], $pledge['pledge_id'], 'in line' . __LINE__
);
127 $this->assertEquals(date('Y-m-d') . ' 00:00:00', $pledge['pledge_create_date'], 'in line' . __LINE__
);
128 $this->assertEquals(100.00, $pledge['pledge_amount'], 'in line' . __LINE__
);
129 $this->assertEquals('Pending', $pledge['pledge_status'], 'in line' . __LINE__
);
130 $this->assertEquals(5, $pledge['pledge_frequency_interval'], 'in line' . __LINE__
);
131 $this->assertEquals('year', $pledge['pledge_frequency_unit'], 'in line' . __LINE__
);
132 $this->assertEquals(date('Y-m-d', strtotime($this->scheduled_date
)) . ' 00:00:00', $pledge['pledge_next_pay_date'], 'in line' . __LINE__
);
133 $this->assertEquals($pledge['pledge_next_pay_amount'], 20.00, 'in line' . __LINE__
);
136 'pledge_id' => $this->_pledge
['id'],
137 'version' => $this->_apiversion
,
139 $pledge = civicrm_api('pledge', 'delete', $params2);
142 * test 'return.pledge_financial_type' => 1 works
144 function testGetPledgewithReturn() {
146 $this->_pledge
= civicrm_api('pledge', 'create', $this->_params
);
148 'pledge_id' => $this->_pledge
['id'],
149 'version' => $this->_apiversion
,
150 'return.pledge_financial_type' => 1,
152 $result = civicrm_api('pledge', 'get', $params);
153 $pledge = $result['values'][$this->_pledge
['id']];
154 civicrm_api('pledge', 'delete', $pledge);
155 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
158 * test 'return.pledge_contribution_type' => 1 works
159 * This is for legacy compatibility
161 function testGetPledgewithReturnLegacy() {
163 $this->_pledge
= civicrm_api('pledge', 'create', $this->_params
);
165 'pledge_id' => $this->_pledge
['id'],
166 'version' => $this->_apiversion
,
167 'return.pledge_financial_type' => 1,
169 $result = civicrm_api('pledge', 'get', $params);
170 $pledge = $result['values'][$this->_pledge
['id']];
171 civicrm_api('pledge', 'delete', $pledge);
172 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
175 function testPledgeGetReturnFilters() {
176 $oldPledge = civicrm_api('pledge', 'create', $this->_params
);
178 $overdueParams = array(
179 'scheduled_date' => 'first saturday of march last year',
180 'start_date' => 'first saturday of march last year',
182 $oldPledge = civicrm_api('pledge', 'create', array_merge($this->_params
, $overdueParams));
184 $pledgeGetParams = array('version' => 3);
185 $allPledges = civicrm_api('pledge', 'getcount', $pledgeGetParams);
187 $this->assertEquals(2, $allPledges, 'Check we have 2 pledges to place with in line ' . __LINE__
);
188 $pledgeGetParams['pledge_start_date_high'] = date('YmdHis', strtotime('2 days ago'));
189 $earlyPledge = civicrm_api('pledge', 'get', $pledgeGetParams);
190 $this->documentMe($pledgeGetParams, $earlyPledge, __FUNCTION__
, __FILE__
, "demonstrates high date filter", "GetFilterHighDate");
191 $this->assertEquals(1, $earlyPledge['count'], ' check only one returned with start date filter in line ' . __LINE__
);
192 $this->assertEquals($oldPledge['id'], $earlyPledge['id'], ' check correct pledge returned ' . __LINE__
);
195 * create 2 pledges - see if we can get by status id
197 function testGetOverduePledge() {
198 $overdueParams = array(
199 'scheduled_date' => 'first saturday of march last year',
200 'start_date' => 'first saturday of march last year',
202 $this->_pledge
= civicrm_api('pledge', 'create', array_merge($this->_params
, $overdueParams));
204 'version' => $this->_apiversion
,
205 'pledge_status_id' => '6',
207 $result = civicrm_api('pledge', 'get', $params);
208 $emptyResult = civicrm_api('pledge', 'get', array(
209 'version' => $this->_apiversion
,
210 'pledge_status_id' => '1',
212 $pledge = $result['values'][$this->_pledge
['id']];
213 civicrm_api('pledge', 'delete', $pledge);
214 $this->assertEquals(1, $result['count']);
215 $this->assertEquals(0, $emptyResult['count']);
220 * create 2 pledges - see if we can get by status id
222 function testSortParamPledge() {
223 $pledge1 = civicrm_api('pledge', 'create', $this->_params
);
224 $overdueParams = array(
225 'scheduled_date' => 'first saturday of march last year',
226 'start_date' => 'first saturday of march last year',
227 'create_date' => 'first saturday of march last year',
229 $pledge2 = civicrm_api('pledge', 'create', array_merge($this->_params
, $overdueParams));
231 'version' => $this->_apiversion
,
232 'pledge_is_test' => 0,
235 $result = civicrm_api('pledge', 'get', $params);
237 $resultSortedAsc = civicrm_api('pledge', 'get', array(
238 'version' => $this->_apiversion
,
240 'sort' => 'start_date ASC',
242 $resultSortedDesc = civicrm_api('pledge', 'get', array(
243 'version' => $this->_apiversion
,
245 'sort' => 'start_date DESC',
248 $this->assertEquals($pledge1['id'], $result['id'], 'pledge get gets first created pledge in line ' . __LINE__
);
249 $this->assertEquals($pledge2['id'], $resultSortedAsc['id'], 'Ascending pledge sort works');
250 $this->assertEquals($pledge1['id'], $resultSortedDesc['id'], 'Decending pledge sort works');
251 civicrm_api('pledge', 'delete', array('version' => 3, 'id' => $pledge1['id']));
252 civicrm_api('pledge', 'delete', array('version' => 3, 'id' => $pledge2['id']));
255 function testCreatePledge() {
257 $result = civicrm_api('pledge', 'create', $this->_params
);
258 $this->documentMe($this->_params
, $result, __FUNCTION__
, __FILE__
);
259 $this->assertAPISuccess($result, "in line " . __LINE__
);
260 $this->assertEquals($result['values'][0]['amount'], 100.00, 'In line ' . __LINE__
);
261 $this->assertEquals($result['values'][0]['installments'], 5, 'In line ' . __LINE__
);
262 $this->assertEquals($result['values'][0]['frequency_unit'], 'year', 'In line ' . __LINE__
);
263 $this->assertEquals($result['values'][0]['frequency_interval'], 5, 'In line ' . __LINE__
);
264 $this->assertEquals($result['values'][0]['frequency_day'], 15, 'In line ' . __LINE__
);
265 $this->assertEquals($result['values'][0]['original_installment_amount'], 20, 'In line ' . __LINE__
);
266 $this->assertEquals($result['values'][0]['status_id'], 2, 'In line ' . __LINE__
);
267 $this->assertEquals($result['values'][0]['create_date'], date('Ymd') . '000000', 'In line ' . __LINE__
);
268 $this->assertEquals($result['values'][0]['start_date'], date('Ymd') . '000000', 'In line ' . __LINE__
);
269 $this->assertEquals($result['is_error'], 0, 'In line ' . __LINE__
);
270 $payments = civicrm_api('PledgePayment', 'Get', array('version' => 3, 'pledge_id' => $result['id'], 'sequential' => 1));
271 $this->assertAPISuccess($payments, 'In line ' . __LINE__
);
272 $this->assertEquals($payments['count'], 5, 'In line ' . __LINE__
);
273 require_once 'CRM/Utils/Date.php';
274 $shouldBeDate = CRM_Utils_Date
::format(CRM_Utils_Date
::intervalAdd('year', 5 * 4, $this->scheduled_date
), "-");
275 $this->assertEquals(substr($shouldBeDate, 0, 10), substr($payments['values'][4]['scheduled_date'], 0, 10), 'In line ' . __LINE__
);
277 $pledgeID = array('id' => $result['id'], 'version' => 3);
278 $pledge = civicrm_api('pledge', 'delete', $pledgeID);
282 * Test that pledge with weekly schedule calculates dates correctly
284 function testCreatePledgeWeeklySchedule() {
286 'scheduled_date' => '20110510',
287 'frequency_unit' => 'week',
288 'frequency_day' => 3,
289 'frequency_interval' => 2,
291 $params = array_merge($this->_params
, $params);
292 $pledge = civicrm_api('Pledge', 'Create', $params);
293 //ensure that correct number of payments created & last payment has the right date
294 $payments = civicrm_api('PledgePayment', 'Get', array(
296 'pledge_id' => $pledge['id'],
298 $this->assertEquals($payments['is_error'], 0, 'In line ' . __LINE__
);
299 $this->assertEquals($payments['count'], 5, 'In line ' . __LINE__
);
300 require_once 'CRM/Utils/Date.php';
301 $this->assertEquals('2011-07-06 00:00:00', $payments['values'][4]['scheduled_date'], 'In line ' . __LINE__
);
303 $pledgeID = array('pledge_id' => $pledge['id'], 'version' => 3);
304 $pledge = civicrm_api('pledge', 'delete', $pledgeID);
307 * Test that pledge with weekly schedule calculates dates correctly
309 function testCreatePledgeMontlySchedule() {
311 'scheduled_date' => '20110510',
312 'frequency_unit' => 'Month',
313 'frequency_day' => 3,
314 'frequency_interval' => 2,
316 $params = array_merge($this->_params
, $params);
317 $apiResult = civicrm_api('pledge', 'create', $params);
318 $this->assertAPISuccess($apiResult);
323 * Test creation of pledge with only one payment.
325 * Pledge status id left empty as it is not a required field
326 * http://issues.civicrm.org/jira/browse/CRM-8551
329 function testCreatePledgeSinglePayment() {
331 'scheduled_date' => '20110510',
332 'frequency_unit' => 'week',
333 'frequency_day' => 3,
334 'frequency_interval' => 2,
338 $params = array_merge($this->_params
, $params);
339 unset($params['pledge_status_id']);
340 $pledge = civicrm_api('Pledge', 'Create', $params);
341 //ensure that correct number of payments created & last payment has the right date
342 $payments = civicrm_api('PledgePayment', 'Get', array(
344 'pledge_id' => $pledge['id'],
347 $this->assertEquals($payments['is_error'], 0, 'In line ' . __LINE__
);
348 $this->assertEquals(1, $payments['count'], 'In line ' . __LINE__
);
349 $this->assertEquals(2, $payments['values'][0]['status_id'], 'In line ' . __LINE__
);
350 $pledgeID = array('id' => $pledge['id'], 'version' => 3);
351 $pledge = civicrm_api('pledge', 'delete', $pledgeID);
355 * test that using original_installment_amount rather than pledge_original_installment_amount works
356 * Pledge field behaviour is a bit random & so pledge has come to try to handle both unique & non -unique fields
358 function testCreatePledgeWithNonUnique() {
359 $params = $this->_params
;
360 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
362 unset($params['pledge_original_installment_amount']);
363 $result = civicrm_api('pledge', 'create', $params);
364 $pledgeDetails = civicrm_api('Pledge', 'Get', array('version' => 3, 'id' => $result['id'], 'sequential' => 1));
365 $pledge = $pledgeDetails['values'][0];
366 $this->assertEquals(0, $result['is_error'], "in line " . __LINE__
);
367 $this->assertEquals(100.00, $pledge['pledge_amount'], 'In line ' . __LINE__
);
368 $this->assertEquals('year', $pledge['pledge_frequency_unit'], 'In line ' . __LINE__
);
369 $this->assertEquals(5, $pledge['pledge_frequency_interval'], 'In line ' . __LINE__
);
370 $this->assertEquals(20, $pledge['pledge_next_pay_amount'], 'In line ' . __LINE__
);
372 $pledgeID = array('id' => $result['id'], 'version' => 3);
373 $pledge = civicrm_api('pledge', 'delete', $pledgeID);
376 function testCreateCancelPledge() {
379 $result = civicrm_api('pledge', 'create', $this->_params
);
380 $this->assertEquals(0, $result['is_error'], "in line " . __LINE__
);
381 $this->assertEquals(2, $result['values'][0]['status_id'], "in line " . __LINE__
);
382 $cancelparams = array('sequential' => 1, 'version' => $this->_apiversion
, 'id' => $result['id'], 'pledge_status_id' => 3);
383 $result = civicrm_api('pledge', 'create', $cancelparams);
384 $this->assertEquals(3, $result['values'][0]['status_id'], "in line " . __LINE__
);
385 $pledgeID = array('id' => $result['id'], 'version' => 3);
386 $pledge = civicrm_api('pledge', 'delete', $pledgeID);
390 * test that status is set to pending
392 function testCreatePledgeNoStatus() {
394 $params = $this->_params
;
395 unset($params['status_id']);
396 unset($params['pledge_status_id']);
397 $result = civicrm_api('pledge', 'create', $params);
398 $this->assertAPISuccess($result, "in line " . __LINE__
);
399 $this->assertEquals(2, $result['values'][0]['status_id'], "in line " . __LINE__
);
400 $pledgeID = array('pledge_id' => $result['id'], 'version' => 3);
401 $pledge = civicrm_api('pledge', 'delete', $pledgeID);
405 function testCreateUpdatePledge() {
407 // we test 'sequential' param here too
408 $pledgeID = $this->pledgeCreate($this->_individualId
);
412 'version' => $this->_apiversion
,
414 $original = civicrm_api('pledge', 'get', $old_params);
415 //Make sure it came back
416 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID, 'In line ' . __LINE__
);
417 //set up list of old params, verify
418 $old_contact_id = $original['values'][0]['contact_id'];
419 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
420 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
421 $old_status_id = $original['values'][0]['pledge_status'];
424 //check against values in CiviUnitTestCase::createPledge()
425 $this->assertEquals($old_contact_id, $this->_individualId
, 'In line ' . __LINE__
);
426 $this->assertEquals($old_frequency_unit, 'year', 'In line ' . __LINE__
);
427 $this->assertEquals($old_frequency_interval, 5, 'In line ' . __LINE__
);
428 $this->assertEquals($old_status_id, 'Pending', 'In line ' . __LINE__
);
431 'contact_id' => $this->_individualId
,
432 'pledge_status_id' => 3,
434 'financial_type_id' => 1,
435 'start_date' => date('Ymd'),
436 'installments' => 10,
437 'version' => $this->_apiversion
,
440 $pledge = civicrm_api('pledge', 'create', $params);
441 $this->assertEquals($pledge['is_error'], 0);
443 'id' => $pledge['id'],
444 'version' => $this->_apiversion
,
446 $pledge = civicrm_api('pledge', 'get', $new_params);
447 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
448 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled', 'In line ' . __LINE__
);
449 $pledge = civicrm_api('pledge', 'delete', $new_params);
450 $this->assertEquals($pledge['is_error'], 0, 'In line ' . __LINE__
);
453 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax
454 * ie contribution_type_id
457 function testCreateUpdatePledgeLegacy() {
459 // we test 'sequential' param here too
460 $pledgeID = $this->pledgeCreate($this->_individualId
);
464 'version' => $this->_apiversion
,
466 $original = civicrm_api('pledge', 'get', $old_params);
467 //Make sure it came back
468 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID, 'In line ' . __LINE__
);
469 //set up list of old params, verify
470 $old_contact_id = $original['values'][0]['contact_id'];
471 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
472 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
473 $old_status_id = $original['values'][0]['pledge_status'];
476 //check against values in CiviUnitTestCase::createPledge()
477 $this->assertEquals($old_contact_id, $this->_individualId
, 'In line ' . __LINE__
);
478 $this->assertEquals($old_frequency_unit, 'year', 'In line ' . __LINE__
);
479 $this->assertEquals($old_frequency_interval, 5, 'In line ' . __LINE__
);
480 $this->assertEquals($old_status_id, 'Pending', 'In line ' . __LINE__
);
483 'contact_id' => $this->_individualId
,
484 'pledge_status_id' => 3,
486 'contribution_type_id' => 1,
487 'start_date' => date('Ymd'),
488 'installments' => 10,
489 'version' => $this->_apiversion
,
492 $pledge = civicrm_api('pledge', 'create', $params);
493 $this->assertEquals($pledge['is_error'], 0);
495 'id' => $pledge['id'],
496 'version' => $this->_apiversion
,
498 $pledge = civicrm_api('pledge', 'get', $new_params);
499 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
500 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled', 'In line ' . __LINE__
);
501 $pledge = civicrm_api('pledge', 'delete', $new_params);
502 $this->assertEquals($pledge['is_error'], 0, 'In line ' . __LINE__
);
505 ///////////////// civicrm_pledge_delete methods
506 function testDeleteEmptyParamsPledge() {
508 $params = array('version' => $this->_apiversion
);
509 $pledge = civicrm_api('pledge', 'delete', $params);
510 $this->assertEquals($pledge['is_error'], 1);
511 $this->assertEquals($pledge['error_message'], 'Mandatory key(s) missing from params array: id');
514 function testDeleteParamsNotArrayPledge() {
515 $params = 'pledge_id= 1';
516 $pledge = civicrm_api('pledge', 'delete', $params);
517 $this->assertEquals($pledge['is_error'], 1);
518 $this->assertEquals($pledge['error_message'], 'Input variable `params` is not an array');
521 function testDeleteWrongParamPledge() {
523 'pledge_source' => 'SSF',
524 'version' => $this->_apiversion
,
526 $pledge = civicrm_api('pledge', 'delete', $params);
527 $this->assertEquals($pledge['is_error'], 1);
528 $this->assertEquals($pledge['error_message'], 'Mandatory key(s) missing from params array: id');
532 * legacy support for pledge_id
534 function testDeletePledge() {
536 $pledgeID = $this->pledgeCreate($this->_individualId
);
538 'pledge_id' => $pledgeID,
539 'version' => $this->_apiversion
,
541 $result = civicrm_api('pledge', 'delete', $params);
542 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
543 $this->assertEquals($result['is_error'], 0);
547 * std is to accept id
549 function testDeletePledgeUseID() {
551 $pledgeID = $this->pledgeCreate($this->_individualId
);
554 'version' => $this->_apiversion
,
556 $result = civicrm_api('pledge', 'delete', $params);
557 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
558 $this->assertEquals($result['is_error'], 0);
561 * test to make sure empty get returns nothing
562 * Note that the function gives incorrect results if no pledges exist as it does a
563 * contact search instead - test only checks that the get finds the one existing
565 function testGetEmpty() {
566 $result = civicrm_api('pledge', 'create', $this->_params
);
567 $result = civicrm_api('pledge', 'get', array('version' => 3));
568 $this->assertAPISuccess($result, "This test is failing because it's acting like a contact get when no params set. Not sure the fix");
569 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
570 $pledgeID = array('id' => $result['id'], 'version' => 3);
571 $pledge = civicrm_api('pledge', 'delete', $pledgeID);