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