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