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