test failure fix2
[civicrm-core.git] / tests / phpunit / api / v3 / PledgeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
48 public function setUp() {
49 $this->_apiversion = 3;
50 parent::setUp();
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,
62 'amount' => 100.00,
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,
69 'installments' => 5,
70 'sequential' => 1,
71 );
72 }
73
74 public function tearDown() {
75 $this->contactDelete($this->_individualId);
76 }
77
78 ///////////////// civicrm_pledge_get methods
79
80 /**
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
85 */
86 public function testCreateWithCustom() {
87 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
88
89 $params = $this->_params;
90 $params['custom_' . $ids['custom_field_id']] = "custom string";
91
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__);
99
100 $this->customFieldDelete($ids['custom_field_id']);
101 $this->customGroupDelete($ids['custom_group_id']);
102 }
103
104 /*
105 */
106 public function testgetfieldspledge() {
107 $result = $this->callAPISuccess('pledge', 'getfields', array('action' => 'get'));
108 $this->assertEquals(1, $result['values']['next_pay_date']['api.return']);
109 }
110
111 public function testGetPledge() {
112
113 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
114 $params = array(
115 'pledge_id' => $this->_pledge['id'],
116 );
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__);
128
129 $params2 = array(
130 'pledge_id' => $this->_pledge['id'],
131 );
132 $pledge = $this->callAPISuccess('pledge', 'delete', $params2);
133 }
134
135 /**
136 * Test 'return.pledge_financial_type' => 1 works
137 */
138 public function testGetPledgewithReturn() {
139
140 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
141 $params = array(
142 'pledge_id' => $this->_pledge['id'],
143 '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 /**
152 * Test 'return.pledge_contribution_type' => 1 works
153 * This is for legacy compatibility
154 */
155 public function testGetPledgewithReturnLegacy() {
156
157 $this->_pledge = $this->callAPISuccess('pledge', 'create', $this->_params);
158 $params = array(
159 'pledge_id' => $this->_pledge['id'],
160 'return.pledge_financial_type' => 1,
161 );
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']);
166 }
167
168 public function testPledgeGetReturnFilters() {
169 $oldPledge = $this->callAPISuccess('pledge', 'create', $this->_params);
170
171 $overdueParams = array(
172 'scheduled_date' => 'first saturday of march last year',
173 'start_date' => 'first saturday of march last year',
174 );
175 $oldPledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
176
177 $pledgeGetParams = array();
178 $allPledges = $this->callAPISuccess('pledge', 'getcount', $pledgeGetParams);
179
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__);
185 }
186
187 /*
188 * create 2 pledges - see if we can get by status id
189 */
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',
194 );
195 $this->_pledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
196 $params = array(
197 'pledge_status_id' => '6',
198 );
199 $result = $this->callAPISuccess('pledge', 'get', $params);
200 $emptyResult = $this->callAPISuccess('pledge', 'get', array(
201 'pledge_status_id' => '1',
202 ));
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']);
207 }
208
209
210 /*
211 * create 2 pledges - see if we can get by status id
212 */
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',
219 );
220 $pledge2 = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
221 $params = array(
222 'pledge_is_test' => 0,
223 'rowCount' => 1,
224 );
225 $result = $this->callAPISuccess('pledge', 'get', $params);
226
227 $resultSortedAsc = $this->callAPISuccess('pledge', 'get', array(
228 'rowCount' => 1,
229 'sort' => 'start_date ASC',
230 ));
231 $resultSortedDesc = $this->callAPISuccess('pledge', 'get', array(
232 'rowCount' => 1,
233 'sort' => 'start_date DESC',
234 ));
235
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']));
241 }
242
243 public function testCreatePledge() {
244
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__);
262
263 $pledgeID = array('id' => $result['id']);
264 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
265 }
266
267 /*
268 * Test that pledge with weekly schedule calculates dates correctly
269 */
270 public function testCreatePledgeWeeklySchedule() {
271 $params = array(
272 'scheduled_date' => '20110510',
273 'frequency_unit' => 'week',
274 'frequency_day' => 3,
275 'frequency_interval' => 2,
276 );
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'],
282 'sequential' => 1
283 ));
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__);
286
287 $this->callAPISuccess('pledge', 'delete', array('pledge_id' => $pledge['id']));
288 }
289
290 /*
291 * Test that pledge with weekly schedule calculates dates correctly
292 */
293 public function testCreatePledgeMontlySchedule() {
294 $params = array(
295 'scheduled_date' => '20110510',
296 'frequency_unit' => 'Month',
297 'frequency_day' => 3,
298 'frequency_interval' => 2,
299 );
300 $params = array_merge($this->_params, $params);
301 $apiResult = $this->callAPISuccess('pledge', 'create', $params);
302 }
303
304
305 /**
306 * Test creation of pledge with only one payment.
307 *
308 * Pledge status id left empty as it is not a required field
309 * http://issues.civicrm.org/jira/browse/CRM-8551
310 */
311 public function testCreatePledgeSinglePayment() {
312 $params = array(
313 'scheduled_date' => '20110510',
314 'frequency_unit' => 'week',
315 'frequency_day' => 3,
316 'frequency_interval' => 2,
317 'installments' => 1,
318 );
319
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'],
326 'sequential' => 1,
327 ));
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);
332 }
333
334 /*
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
337 */
338 public function testCreatePledgeWithNonUnique() {
339 $params = $this->_params;
340 $params['original_installment_amount'] = $params['pledge_original_installment_amount'];
341
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__);
350
351 $pledgeID = array('id' => $result['id']);
352 $pledge = $this->callAPISuccess('pledge', 'delete', $pledgeID);
353 }
354
355 public function testCreateCancelPledge() {
356
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);
364 }
365
366 /**
367 * Test that status is set to pending
368 */
369 public function testCreatePledgeNoStatus() {
370
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);
379 }
380
381 /**
382 * Update Pledge
383 */
384 public function testCreateUpdatePledge() {
385 // we test 'sequential' param here too
386 $pledgeID = $this->pledgeCreate($this->_individualId);
387 $old_params = array(
388 'id' => $pledgeID,
389 'sequential' => 1,
390 );
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'];
399
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__);
405 $params = array(
406 'id' => $pledgeID,
407 'contact_id' => $this->_individualId,
408 'pledge_status_id' => 3,
409 'amount' => 100,
410 'financial_type_id' => 1,
411 'start_date' => date('Ymd'),
412 'installments' => 10,
413 );
414
415 $pledge = $this->callAPISuccess('pledge', 'create', $params);
416 $new_params = array(
417 'id' => $pledge['id'],
418 );
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);
423 }
424
425 /**
426 * Here we ensure we are maintaining our 'contract' & supporting previously working syntax
427 * ie contribution_type_id
428 */
429 public function testCreateUpdatePledgeLegacy() {
430
431 // we test 'sequential' param here too
432 $pledgeID = $this->pledgeCreate($this->_individualId);
433 $old_params = array(
434 'id' => $pledgeID,
435 'sequential' => 1,
436 );
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'];
445
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__);
451 $params = array(
452 'id' => $pledgeID,
453 'contact_id' => $this->_individualId,
454 'pledge_status_id' => 3,
455 'amount' => 100,
456 'contribution_type_id' => 1,
457 'start_date' => date('Ymd'),
458 'installments' => 10,
459 );
460
461 $pledge = $this->callAPISuccess('pledge', 'create', $params);
462 $new_params = array(
463 'id' => $pledge['id'],
464 );
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);
469 }
470
471 ///////////////// civicrm_pledge_delete methods
472 public function testDeleteEmptyParamsPledge() {
473 $pledge = $this->callAPIFailure('pledge', 'delete', array(), 'Mandatory key(s) missing from params array: id');
474 }
475
476
477 public function testDeleteWrongParamPledge() {
478 $params = array(
479 'pledge_source' => 'SSF',
480 );
481 $pledge = $this->callAPIFailure('pledge', 'delete', $params, 'Mandatory key(s) missing from params array: id');
482 }
483
484 /**
485 * Legacy support for pledge_id
486 */
487 public function testDeletePledge() {
488
489 $pledgeID = $this->pledgeCreate($this->_individualId);
490 $params = array(
491 'pledge_id' => $pledgeID,
492 );
493 $result = $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__, __FILE__);
494 }
495
496 /**
497 * Std is to accept id
498 */
499 public function testDeletePledgeUseID() {
500
501 $pledgeID = $this->pledgeCreate($this->_individualId);
502 $params = array(
503 'id' => $pledgeID,
504 );
505 $result = $this->callAPIAndDocument('pledge', 'delete', $params, __FUNCTION__, __FILE__);
506 }
507
508 /**
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
512 */
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);
520 }
521 }