Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / PledgeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test class for Pledge API - civicrm_pledge_*
32 *
33 * @package CiviCRM_APIv3
34 */
35 class api_v3_PledgeTest extends CiviUnitTestCase {
36
37 /**
38 * Assume empty database with just civicrm_data
39 */
40 protected $_individualId;
41 protected $_pledge;
42 protected $_apiversion;
43 protected $_params;
44 protected $_entity;
45 protected $scheduled_date;
46 public $DBResetRequired = True;
47 public $_eNoticeCompliant = FALSE;
48
49 function setUp() {
50 $this->_apiversion = 3;
51 parent::setUp();
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();
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,
63 'amount' => 100.00,
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,
70 'installments' => 5,
71 'sequential' => 1,
72 );
73 }
74
75 function tearDown() {
76 $this->contactDelete($this->_individualId);
77 }
78
79 ///////////////// civicrm_pledge_get methods
80
81 /**
82 * check with complete array + custom field
83 * Note that the test is written on purpose without any
84 * variables specific to participant so it can be replicated into other entities
85 * and / or moved to the automated test suite
86 */
87 function testCreateWithCustom() {
88 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
89
90 $params = $this->_params;
91 $params['custom_' . $ids['custom_field_id']] = "custom string";
92
93 $result = $this->callAPISuccess($this->_entity, 'create', $params);
94 $this->assertAPISuccess($result, " testCreateWithCustom ");
95 $this->assertAPISuccess($result, ' in line ' . __LINE__);
96 $getparams = array('id' => $result['id'], 'return.custom_' . $ids['custom_field_id'] => 1);
97 $check = $this->callAPISuccess($this->_entity, 'get', $getparams);
98 $this->callAPISuccess('pledge', 'delete', array('id' => $check['id']));
99 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
100
101 $this->customFieldDelete($ids['custom_field_id']);
102 $this->customGroupDelete($ids['custom_group_id']);
103 }
104
105 /*
106 *
107 */
108 function testgetfieldspledge() {
109 $result = $this->callAPISuccess('pledge', 'getfields', array('action' => 'get'));
110 $this->assertEquals(1, $result['values']['next_pay_date']['api.return']);
111 }
112
113 function testGetPledge() {
114
115
116 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
117 $params = array(
118 'pledge_id' => $this->_pledge['id'],
119 );
120 $result = $this->callAPIAndDocument('pledge', 'get', $params, __FUNCTION__, __FILE__);
121 $pledge = $result['values'][$this->_pledge['id']];
122 $this->assertEquals($this->_individualId, $pledge['contact_id'], 'in line' . __LINE__);
123 $this->assertEquals($this->_pledge['id'], $pledge['pledge_id'], 'in line' . __LINE__);
124 $this->assertEquals(date('Y-m-d') . ' 00:00:00', $pledge['pledge_create_date'], 'in line' . __LINE__);
125 $this->assertEquals(100.00, $pledge['pledge_amount'], 'in line' . __LINE__);
126 $this->assertEquals('Pending', $pledge['pledge_status'], 'in line' . __LINE__);
127 $this->assertEquals(5, $pledge['pledge_frequency_interval'], 'in line' . __LINE__);
128 $this->assertEquals('year', $pledge['pledge_frequency_unit'], 'in line' . __LINE__);
129 $this->assertEquals(date('Y-m-d', strtotime($this->scheduled_date)) . ' 00:00:00', $pledge['pledge_next_pay_date'], 'in line' . __LINE__);
130 $this->assertEquals($pledge['pledge_next_pay_amount'], 20.00, 'in line' . __LINE__);
131
132 $params2 = array(
133 'pledge_id' => $this->_pledge['id'], );
134 $pledge = $this->callAPISuccess('pledge', 'delete', $params2);
135 }
136 /**
137 * test 'return.pledge_financial_type' => 1 works
138 */
139 function testGetPledgewithReturn() {
140
141 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
142 $params = array(
143 'pledge_id' => $this->_pledge['id'], 'return.pledge_financial_type' => 1,
144 );
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']);
149 }
150 /**
151 * test 'return.pledge_contribution_type' => 1 works
152 * This is for legacy compatibility
153 */
154 function testGetPledgewithReturnLegacy() {
155
156 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
157 $params = array(
158 'pledge_id' => $this->_pledge['id'], 'return.pledge_financial_type' => 1,
159 );
160 $result = $this->callAPISuccess('pledge', 'get', $params);
161 $pledge = $result['values'][$this->_pledge['id']];
162 $this->callAPISuccess('pledge', 'delete', $pledge);
163 $this->assertEquals('Donation', $pledge['pledge_financial_type']);
164 }
165
166 function testPledgeGetReturnFilters() {
167 $oldPledge = $this->callAPISuccess('pledge', 'create', $this->_params);
168
169 $overdueParams = array(
170 'scheduled_date' => 'first saturday of march last year',
171 'start_date' => 'first saturday of march last year',
172 );
173 $oldPledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
174
175 $pledgeGetParams = array();
176 $allPledges = $this->callAPISuccess('pledge', 'getcount', $pledgeGetParams);
177
178 $this->assertEquals(2, $allPledges, 'Check we have 2 pledges to place with in line ' . __LINE__);
179 $pledgeGetParams['pledge_start_date_high'] = date('YmdHis', strtotime('2 days ago'));
180 $earlyPledge = $this->callAPIAndDocument('pledge', 'get', $pledgeGetParams, __FUNCTION__, __FILE__, "demonstrates high date filter", "GetFilterHighDate");
181 $this->assertEquals(1, $earlyPledge['count'], ' check only one returned with start date filter in line ' . __LINE__);
182 $this->assertEquals($oldPledge['id'], $earlyPledge['id'], ' check correct pledge returned ' . __LINE__);
183 }
184 /*
185 * create 2 pledges - see if we can get by status id
186 */
187 function testGetOverduePledge() {
188 $overdueParams = array(
189 'scheduled_date' => 'first saturday of march last year',
190 'start_date' => 'first saturday of march last year',
191 );
192 $this->_pledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
193 $params = array( 'pledge_status_id' => '6',
194 );
195 $result = $this->callAPISuccess('pledge', 'get', $params);
196 $emptyResult = $this->callAPISuccess('pledge', 'get', array( 'pledge_status_id' => '1',
197 ));
198 $pledge = $result['values'][$this->_pledge['id']];
199 $this->callAPISuccess('pledge', 'delete', $pledge);
200 $this->assertEquals(1, $result['count']);
201 $this->assertEquals(0, $emptyResult['count']);
202 }
203
204
205 /*
206 * create 2 pledges - see if we can get by status id
207 */
208 function testSortParamPledge() {
209 $pledge1 = $this->callAPISuccess('pledge', 'create', $this->_params);
210 $overdueParams = array(
211 'scheduled_date' => 'first saturday of march last year',
212 'start_date' => 'first saturday of march last year',
213 'create_date' => 'first saturday of march last year',
214 );
215 $pledge2 = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
216 $params = array( 'pledge_is_test' => 0,
217 'rowCount' => 1,
218 );
219 $result = $this->callAPISuccess('pledge', 'get', $params);
220
221 $resultSortedAsc = $this->callAPISuccess('pledge', 'get', array( 'rowCount' => 1,
222 'sort' => 'start_date ASC',
223 ));
224 $resultSortedDesc = $this->callAPISuccess('pledge', 'get', array( 'rowCount' => 1,
225 'sort' => 'start_date DESC',
226 ));
227
228 $this->assertEquals($pledge1['id'], $result['id'], 'pledge get gets first created pledge in line ' . __LINE__);
229 $this->assertEquals($pledge2['id'], $resultSortedAsc['id'], 'Ascending pledge sort works');
230 $this->assertEquals($pledge1['id'], $resultSortedDesc['id'], 'Decending pledge sort works');
231 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge1['id']));
232 $this->callAPISuccess('pledge', 'delete', array('id' => $pledge2['id']));
233 }
234
235 function testCreatePledge() {
236
237 $result = $this->callAPIAndDocument('pledge', 'create', $this->_params, __FUNCTION__, __FILE__);
238 $this->assertEquals($result['values'][0]['amount'], 100.00, 'In line ' . __LINE__);
239 $this->assertEquals($result['values'][0]['installments'], 5, 'In line ' . __LINE__);
240 $this->assertEquals($result['values'][0]['frequency_unit'], 'year', 'In line ' . __LINE__);
241 $this->assertEquals($result['values'][0]['frequency_interval'], 5, 'In line ' . __LINE__);
242 $this->assertEquals($result['values'][0]['frequency_day'], 15, 'In line ' . __LINE__);
243 $this->assertEquals($result['values'][0]['original_installment_amount'], 20, 'In line ' . __LINE__);
244 $this->assertEquals($result['values'][0]['status_id'], 2, 'In line ' . __LINE__);
245 $this->assertEquals($result['values'][0]['create_date'], date('Ymd') . '000000', 'In line ' . __LINE__);
246 $this->assertEquals($result['values'][0]['start_date'], date('Ymd') . '000000', 'In line ' . __LINE__);
247 $this->assertAPISuccess($result, 'In line ' . __LINE__);
248 $payments = $this->callAPISuccess('PledgePayment', 'Get', array('pledge_id' => $result['id'], 'sequential' => 1));
249 $this->assertAPISuccess($payments, 'In line ' . __LINE__);
250 $this->assertEquals($payments['count'], 5, 'In line ' . __LINE__);
251 require_once 'CRM/Utils/Date.php';
252 $shouldBeDate = CRM_Utils_Date::format(CRM_Utils_Date::intervalAdd('year', 5 * 4, $this->scheduled_date), "-");
253 $this->assertEquals(substr($shouldBeDate, 0, 10), substr($payments['values'][4]['scheduled_date'], 0, 10), 'In line ' . __LINE__);
254
255 $pledgeID = array('id' => $result['id']);
256 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
257 }
258
259 /*
260 * Test that pledge with weekly schedule calculates dates correctly
261 */
262 function testCreatePledgeWeeklySchedule() {
263 $params = array(
264 'scheduled_date' => '20110510',
265 'frequency_unit' => 'week',
266 'frequency_day' => 3,
267 'frequency_interval' => 2,
268 );
269 $params = array_merge($this->_params, $params);
270 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
271 //ensure that correct number of payments created & last payment has the right date
272 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
273 'pledge_id' => $pledge['id'],
274 'sequential' => 1));
275 $this->assertEquals($payments['count'], 5, 'In line ' . __LINE__);
276 $this->assertEquals('2011-07-06 00:00:00', $payments['values'][4]['scheduled_date'], 'In line ' . __LINE__);
277
278 $this->callAPISuccess('pledge', 'delete', array('pledge_id' => $pledge['id']));
279 }
280 /*
281 * Test that pledge with weekly schedule calculates dates correctly
282 */
283 function testCreatePledgeMontlySchedule() {
284 $params = array(
285 'scheduled_date' => '20110510',
286 'frequency_unit' => 'Month',
287 'frequency_day' => 3,
288 'frequency_interval' => 2,
289 );
290 $params = array_merge($this->_params, $params);
291 $apiResult = $this->callAPISuccess('pledge', 'create', $params);
292 }
293
294
295 /*
296 * Test creation of pledge with only one payment.
297 *
298 * Pledge status id left empty as it is not a required field
299 * http://issues.civicrm.org/jira/browse/CRM-8551
300 *
301 */
302 function testCreatePledgeSinglePayment() {
303 $params = array(
304 'scheduled_date' => '20110510',
305 'frequency_unit' => 'week',
306 'frequency_day' => 3,
307 'frequency_interval' => 2,
308 'installments' => 1,
309 );
310
311 $params = array_merge($this->_params, $params);
312 unset($params['pledge_status_id']);
313 $pledge = $this->callAPISuccess('Pledge', 'Create', $params);
314 //ensure that correct number of payments created & last payment has the right date
315 $payments = $this->callAPISuccess('PledgePayment', 'Get', array(
316 'pledge_id' => $pledge['id'],
317 'sequential' => 1
318 ));
319 $this->assertEquals(1, $payments['count'], 'In line ' . __LINE__);
320 $this->assertEquals(2, $payments['values'][0]['status_id'], 'In line ' . __LINE__);
321 $pledgeID = array('id' => $pledge['id']);
322 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
323 }
324
325 /*
326 * test that using original_installment_amount rather than pledge_original_installment_amount works
327 * Pledge field behaviour is a bit random & so pledge has come to try to handle both unique & non -unique fields
328 */
329 function testCreatePledgeWithNonUnique() {
330 $params = $this->_params;
331 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
332
333 unset($params['pledge_original_installment_amount']);
334 $result = $this->callAPISuccess('pledge', 'create', $params);
335 $pledgeDetails = $this->callAPISuccess('Pledge', 'Get', array('id' => $result['id'], 'sequential' => 1));
336 $pledge = $pledgeDetails['values'][0];
337 $this->assertEquals(100.00, $pledge['pledge_amount'], 'In line ' . __LINE__);
338 $this->assertEquals('year', $pledge['pledge_frequency_unit'], 'In line ' . __LINE__);
339 $this->assertEquals(5, $pledge['pledge_frequency_interval'], 'In line ' . __LINE__);
340 $this->assertEquals(20, $pledge['pledge_next_pay_amount'], 'In line ' . __LINE__);
341
342 $pledgeID = array('id' => $result['id']);
343 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
344 }
345
346 function testCreateCancelPledge() {
347
348
349 $result = $this->callAPISuccess('pledge', 'create', $this->_params);
350 $this->assertEquals(2, $result['values'][0]['status_id'], "in line " . __LINE__);
351 $cancelparams = array('sequential' => 1, 'id' => $result['id'], 'pledge_status_id' => 3);
352 $result = $this->callAPISuccess('pledge', 'create', $cancelparams);
353 $this->assertEquals(3, $result['values'][0]['status_id'], "in line " . __LINE__);
354 $pledgeID = array('id' => $result['id']);
355 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
356 }
357
358 /*
359 * test that status is set to pending
360 */
361 function testCreatePledgeNoStatus() {
362
363 $params = $this->_params;
364 unset($params['status_id']);
365 unset($params['pledge_status_id']);
366 $result = $this->callAPISuccess('pledge', 'create', $params);
367 $this->assertAPISuccess($result, "in line " . __LINE__);
368 $this->assertEquals(2, $result['values'][0]['status_id'], "in line " . __LINE__);
369 $pledgeID = array('pledge_id' => $result['id']);
370 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
371 }
372
373 //To Update Pledge
374 function testCreateUpdatePledge() {
375
376 // we test 'sequential' param here too
377 $pledgeID = $this->pledgeCreate($this->_individualId);
378 $old_params = array(
379 'id' => $pledgeID,
380 'sequential' => 1, );
381 $original = $this->callAPISuccess('pledge', 'get', $old_params);
382 //Make sure it came back
383 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID, 'In line ' . __LINE__);
384 //set up list of old params, verify
385 $old_contact_id = $original['values'][0]['contact_id'];
386 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
387 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
388 $old_status_id = $original['values'][0]['pledge_status'];
389
390
391 //check against values in CiviUnitTestCase::createPledge()
392 $this->assertEquals($old_contact_id, $this->_individualId, 'In line ' . __LINE__);
393 $this->assertEquals($old_frequency_unit, 'year', 'In line ' . __LINE__);
394 $this->assertEquals($old_frequency_interval, 5, 'In line ' . __LINE__);
395 $this->assertEquals($old_status_id, 'Pending', 'In line ' . __LINE__);
396 $params = array(
397 'id' => $pledgeID,
398 'contact_id' => $this->_individualId,
399 'pledge_status_id' => 3,
400 'amount' => 100,
401 'financial_type_id' => 1,
402 'start_date' => date('Ymd'),
403 'installments' => 10, );
404
405 $pledge = $this->callAPISuccess('pledge', 'create', $params);
406 $new_params = array(
407 'id' => $pledge['id'],
408 );
409 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
410 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
411 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled', 'In line ' . __LINE__);
412 $pledge = $this->callAPISuccess('pledge', 'delete', $new_params);
413 }
414 /**
415 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax
416 * ie contribution_type_id
417 *
418 */
419 function testCreateUpdatePledgeLegacy() {
420
421 // we test 'sequential' param here too
422 $pledgeID = $this->pledgeCreate($this->_individualId);
423 $old_params = array(
424 'id' => $pledgeID,
425 'sequential' => 1, );
426 $original = $this->callAPISuccess('pledge', 'get', $old_params);
427 //Make sure it came back
428 $this->assertEquals($original['values'][0]['pledge_id'], $pledgeID, 'In line ' . __LINE__);
429 //set up list of old params, verify
430 $old_contact_id = $original['values'][0]['contact_id'];
431 $old_frequency_unit = $original['values'][0]['pledge_frequency_unit'];
432 $old_frequency_interval = $original['values'][0]['pledge_frequency_interval'];
433 $old_status_id = $original['values'][0]['pledge_status'];
434
435
436 //check against values in CiviUnitTestCase::createPledge()
437 $this->assertEquals($old_contact_id, $this->_individualId, 'In line ' . __LINE__);
438 $this->assertEquals($old_frequency_unit, 'year', 'In line ' . __LINE__);
439 $this->assertEquals($old_frequency_interval, 5, 'In line ' . __LINE__);
440 $this->assertEquals($old_status_id, 'Pending', 'In line ' . __LINE__);
441 $params = array(
442 'id' => $pledgeID,
443 'contact_id' => $this->_individualId,
444 'pledge_status_id' => 3,
445 'amount' => 100,
446 'contribution_type_id' => 1,
447 'start_date' => date('Ymd'),
448 'installments' => 10,
449 );
450
451 $pledge = $this->callAPISuccess('pledge', 'create', $params);
452 $new_params = array(
453 'id' => $pledge['id'],
454 );
455 $pledge = $this->callAPISuccess('pledge', 'get', $new_params);
456 $this->assertEquals($pledge['values'][$pledgeID]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
457 $this->assertEquals($pledge['values'][$pledgeID]['pledge_status'], 'Cancelled', 'In line ' . __LINE__);
458 $pledge = $this->callAPISuccess('pledge', 'delete', $new_params);
459 }
460
461 ///////////////// civicrm_pledge_delete methods
462 function testDeleteEmptyParamsPledge() {
463 $pledge = $this->callAPIFailure('pledge', 'delete', array(), 'Mandatory key(s) missing from params array: id');
464 }
465
466
467 function testDeleteWrongParamPledge() {
468 $params = array(
469 'pledge_source' => 'SSF',
470 );
471 $pledge = $this->callAPIFailure('pledge', 'delete', $params, 'Mandatory key(s) missing from params array: id');
472 }
473
474 /*
475 * legacy support for pledge_id
476 */
477 function testDeletePledge() {
478
479 $pledgeID = $this->pledgeCreate($this->_individualId);
480 $params = array(
481 'pledge_id' => $pledgeID,
482 );
483 $result = $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__, __FILE__);
484 }
485
486 /*
487 * std is to accept id
488 */
489 function testDeletePledgeUseID() {
490
491 $pledgeID = $this->pledgeCreate($this->_individualId);
492 $params = array(
493 'id' => $pledgeID,
494 );
495 $result = $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__, __FILE__);
496 }
497 /*
498 * test to make sure empty get returns nothing
499 * Note that the function gives incorrect results if no pledges exist as it does a
500 * contact search instead - test only checks that the get finds the one existing
501 */
502 function testGetEmpty() {
503 $result = $this->callAPISuccess('pledge', 'create', $this->_params);
504 $result = $this->callAPISuccess('pledge', 'get', array());
505 $this->assertAPISuccess($result, "This test is failing because it's acting like a contact get when no params set. Not sure the fix");
506 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__);
507 $pledgeID = array('id' => $result['id']);
508 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
509 }
510 }
511