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