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