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