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