3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 'pledge_status_id' => '6',
205 $result = $this->callAPISuccess('pledge', 'get', $params);
206 $emptyResult = $this->callAPISuccess('pledge', 'get', array(
207 'pledge_status_id' => '1',
209 $pledge = $result['values'][$this->_pledge
['id']];
210 $this->callAPISuccess('pledge', 'delete', $pledge);
211 $this->assertEquals(1, $result['count']);
212 $this->assertEquals(0, $emptyResult['count']);
217 * Create 2 pledges - see if we can get by status id.
219 public function testSortParamPledge() {
220 $pledge1 = $this->callAPISuccess('pledge', 'create', $this->_params
);
221 $overdueParams = array(
222 'scheduled_date' => 'first saturday of march last year',
223 'start_date' => 'first saturday of march last year',
224 'create_date' => 'first saturday of march last year',
226 $pledge2 = $this->callAPISuccess('pledge', 'create', array_merge($this->_params
, $overdueParams));
228 'pledge_is_test' => 0,
231 $result = $this->callAPISuccess('pledge', 'get', $params);
233 $resultSortedAsc = $this->callAPISuccess('pledge', 'get', array(
235 'sort' => 'start_date ASC',
237 $resultSortedDesc = $this->callAPISuccess('pledge', 'get', array(
239 'sort' => 'start_date DESC',
242 $this->assertEquals($pledge1['id'], $result['id'], 'pledge get gets first created pledge in line ' . __LINE__
);
243 $this->assertEquals($pledge2['id'], $resultSortedAsc['id'], 'Ascending pledge sort works');
244 $this->assertEquals($pledge1['id'], $resultSortedDesc['id'], 'Decending pledge sort works');
245 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge1['id']));
246 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge2['id']));
249 public function testCreatePledge() {
251 $result = $this->callAPIAndDocument('pledge', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
252 $this->assertEquals($result['values'][0]['amount'], 100.00);
253 $this->assertEquals($result['values'][0]['installments'], 5);
254 $this->assertEquals($result['values'][0]['frequency_unit'], 'year');
255 $this->assertEquals($result['values'][0]['frequency_interval'], 5);
256 $this->assertEquals($result['values'][0]['frequency_day'], 15);
257 $this->assertEquals($result['values'][0]['original_installment_amount'], 20);
258 $this->assertEquals($result['values'][0]['status_id'], 2);
259 $this->assertEquals($result['values'][0]['create_date'], date('Ymd') . '000000');
260 $this->assertEquals($result['values'][0]['start_date'], date('Ymd') . '000000');
261 $this->assertAPISuccess($result);
262 $payments = $this->callAPISuccess('PledgePayment', 'Get', array('pledge_id' => $result['id'], 'sequential' => 1));
263 $this->assertAPISuccess($payments);
264 $this->assertEquals($payments['count'], 5);
265 $shouldBeDate = CRM_Utils_Date
::format(CRM_Utils_Date
::intervalAdd('year', 5 * 4, $this->scheduled_date
), "-");
266 $this->assertEquals(substr($shouldBeDate, 0, 10), substr($payments['values'][4]['scheduled_date'], 0, 10));
268 $pledgeID = array('id' => $result['id']);
269 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
273 * Test that pledge with weekly schedule calculates dates correctly.
275 public function testCreatePledgeWeeklySchedule() {
277 'scheduled_date' => '20110510',
278 'frequency_unit' => 'week',
279 'frequency_day' => 3,
280 'frequency_interval' => 2,
282 $params = array_merge($this->_params
, $params);
283 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
284 //ensure that correct number of payments created & last payment has the right date
285 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
286 'pledge_id' => $pledge['id'],
289 $this->assertEquals($payments['count'], 5);
290 $this->assertEquals('2011-07-06 00:00:00', $payments['values'][4]['scheduled_date']);
292 $this->callAPISuccess('pledge', 'delete', array('pledge_id' => $pledge['id']));
296 * Test that pledge with weekly schedule calculates dates correctly.
298 public function testCreatePledgeMontlySchedule() {
300 'scheduled_date' => '20110510',
301 'frequency_unit' => 'Month',
302 'frequency_day' => 3,
303 'frequency_interval' => 2,
305 $params = array_merge($this->_params
, $params);
306 $apiResult = $this->callAPISuccess('pledge', 'create', $params);
311 * Test creation of pledge with only one payment.
313 * Pledge status id left empty as it is not a required field
314 * http://issues.civicrm.org/jira/browse/CRM-8551
316 public function testCreatePledgeSinglePayment() {
318 'scheduled_date' => '20110510',
319 'frequency_unit' => 'week',
320 'frequency_day' => 3,
321 'frequency_interval' => 2,
325 $params = array_merge($this->_params
, $params);
326 unset($params['pledge_status_id']);
327 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
328 //ensure that correct number of payments created & last payment has the right date
329 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
330 'pledge_id' => $pledge['id'],
333 $this->assertEquals(1, $payments['count']);
334 $this->assertEquals(2, $payments['values'][0]['status_id']);
335 $pledgeID = array('id' => $pledge['id']);
336 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
340 * Test that using original_installment_amount rather than pledge_original_installment_amount works.
342 * Pledge field behaviour is a bit random & so pledge has come to try to handle both unique & non -unique fields.
344 public function testCreatePledgeWithNonUnique() {
345 $params = $this->_params
;
346 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
348 unset($params['pledge_original_installment_amount']);
349 $result = $this->callAPISuccess('pledge', 'create', $params);
350 $pledgeDetails = $this->callAPISuccess('Pledge', 'Get', array('id' => $result['id'], 'sequential' => 1));
351 $pledge = $pledgeDetails['values'][0];
352 $this->assertEquals(100.00, $pledge['pledge_amount']);
353 $this->assertEquals('year', $pledge['pledge_frequency_unit']);
354 $this->assertEquals(5, $pledge['pledge_frequency_interval']);
355 $this->assertEquals(20, $pledge['pledge_next_pay_amount']);
357 $pledgeID = array('id' => $result['id']);
358 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
362 * Test cancelling a pledge.
364 public function testCreateCancelPledge() {
366 $result = $this->callAPISuccess('pledge', 'create', $this->_params
);
367 $this->assertEquals(2, $result['values'][0]['status_id']);
368 $cancelParams = array(
370 'id' => $result['id'],
371 'pledge_status_id' => 3,
373 $result = $this->callAPISuccess('pledge', 'create', $cancelParams);
374 $this->assertEquals(3, $result['values'][0]['status_id']);
375 $pledgeID = array('id' => $result['id']);
376 $this->callAPISuccess('pledge', 'delete', $pledgeID);
380 * Test that status is set to pending.
382 public function testCreatePledgeNoStatus() {
384 $params = $this->_params
;
385 unset($params['status_id']);
386 unset($params['pledge_status_id']);
387 $result = $this->callAPISuccess('pledge', 'create', $params);
388 $this->assertAPISuccess($result);
389 $this->assertEquals(2, $result['values'][0]['status_id']);
390 $pledgeID = array('pledge_id' => $result['id']);
391 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
397 public function testCreateUpdatePledge() {
398 // we test 'sequential' param here too
399 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
404 $original = $this->callAPISuccess('pledge', 'get', $old_params);
405 //Make sure it came back
406 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID);
407 //set up list of old params, verify
408 $old_contact_id = $original['values'][0]['contact_id'];
409 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
410 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
411 $old_status_id = $original['values'][0]['pledge_status'];
413 //check against values in CiviUnitTestCase::createPledge()
414 $this->assertEquals($old_contact_id, $this->_individualId
);
415 $this->assertEquals($old_frequency_unit, 'year');
416 $this->assertEquals($old_frequency_interval, 5);
417 $this->assertEquals($old_status_id, 'Pending');
420 'contact_id' => $this->_individualId
,
421 'pledge_status_id' => 3,
423 'financial_type_id' => 1,
424 'start_date' => date('Ymd'),
425 'installments' => 10,
428 $pledge = $this->callAPISuccess('pledge', 'create', $params);
430 'id' => $pledge['id'],
432 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
433 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
);
434 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled');
435 $pledge = $this->callAPISuccess('pledge', 'delete', $new_params);
439 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax.
441 * ie contribution_type_id.
443 * We test 'sequential' param here too.
445 public function testCreateUpdatePledgeLegacy() {
446 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
451 $original = $this->callAPISuccess('pledge', 'get', $old_params);
452 // Make sure it came back.
453 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID);
454 // Set up list of old params, verify.
455 $old_contact_id = $original['values'][0]['contact_id'];
456 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
457 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
458 $old_status_id = $original['values'][0]['pledge_status'];
460 // Check against values in CiviUnitTestCase::createPledge().
461 $this->assertEquals($old_contact_id, $this->_individualId
);
462 $this->assertEquals($old_frequency_unit, 'year');
463 $this->assertEquals($old_frequency_interval, 5);
464 $this->assertEquals($old_status_id, 'Pending');
467 'contact_id' => $this->_individualId
,
468 'pledge_status_id' => 3,
470 'contribution_type_id' => 1,
471 'start_date' => date('Ymd'),
472 'installments' => 10,
475 $pledge = $this->callAPISuccess('pledge', 'create', $params);
477 'id' => $pledge['id'],
479 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
480 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId
);
481 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled');
482 $this->callAPISuccess('pledge', 'delete', $new_params);
486 * Failure test for delete without id.
488 public function testDeleteEmptyParamsPledge() {
489 $this->callAPIFailure('pledge', 'delete', array(), 'Mandatory key(s) missing from params array: id');
494 * Failure test for invalid pledge id.
496 public function testDeleteWrongParamPledge() {
498 'pledge_source' => 'SSF',
500 $this->callAPIFailure('pledge', 'delete', $params, 'Mandatory key(s) missing from params array: id');
504 * Legacy support for pledge_id.
506 public function testDeletePledge() {
508 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
510 'pledge_id' => $pledgeID,
512 $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
516 * Standard is to accept id.
518 public function testDeletePledgeUseID() {
520 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId
));
524 $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__
, __FILE__
);
528 * Test to make sure empty get returns nothing.
530 * Note that the function gives incorrect results if no pledges exist as it does a
531 * contact search instead - test only checks that the get finds the one existing
533 public function testGetEmpty() {
534 $this->callAPISuccess('pledge', 'create', $this->_params
);
535 $result = $this->callAPISuccess('pledge', 'get', array());
536 $this->assertAPISuccess($result, "This test is failing because it's acting like a contact get when no params set. Not sure the fix");
537 $this->assertEquals(1, $result['count']);
538 $pledgeID = array('id' => $result['id']);
539 $this->callAPISuccess('pledge', 'delete', $pledgeID);