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