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