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