Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | require_once 'CiviTest/CiviMailUtils.php'; | |
30 | ||
31 | ||
32 | /** | |
33 | * Test APIv3 civicrm_contribute_* functions | |
34 | * | |
35 | * @package CiviCRM_APIv3 | |
36 | * @subpackage API_Contribution | |
37 | */ | |
38 | ||
39 | class api_v3_ContributionTest extends CiviUnitTestCase { | |
40 | ||
41 | /** | |
42 | * Assume empty database with just civicrm_data | |
43 | */ | |
44 | protected $_individualId; | |
45 | protected $_contribution; | |
46 | protected $_contributionTypeId = 1; | |
47 | protected $_apiversion; | |
48 | protected $_entity = 'Contribution'; | |
49 | public $debug = 0; | |
50 | protected $_params; | |
51 | public $_eNoticeCompliant = TRUE; | |
52 | function setUp() { | |
53 | parent::setUp(); | |
54 | ||
55 | $this->_apiversion = 3; | |
56 | $this->_individualId = $this->individualCreate(); | |
57 | $paymentProcessor = $this->processorCreate(); | |
58 | $this->_params = array( | |
59 | 'contact_id' => $this->_individualId, | |
60 | 'receive_date' => '20120511', | |
61 | 'total_amount' => 100.00, | |
62 | 'financial_type_id' => $this->_contributionTypeId, | |
63 | 'non_deductible_amount' => 10.00, | |
64 | 'fee_amount' => 5.00, | |
65 | 'net_amount' => 95.00, | |
66 | 'source' => 'SSF', | |
67 | 'contribution_status_id' => 1, | |
68 | 'version' => $this->_apiversion, | |
69 | ); | |
70 | $this->_processorParams = array( | |
71 | 'domain_id' => 1, | |
72 | 'name' => 'Dummy', | |
73 | 'payment_processor_type_id' => 10, | |
74 | 'financial_account_id' => 12, | |
75 | 'is_active' => 1, | |
76 | 'user_name' => '', | |
77 | 'url_site' => 'http://dummy.com', | |
78 | 'url_recur' => 'http://dummy.com', | |
79 | 'billing_mode' => 1, | |
80 | ); | |
81 | $this->_pageParams = array( | |
82 | 'version' => 3, | |
83 | 'title' => 'Test Contribution Page', | |
84 | 'financial_type_id' => 1, | |
85 | 'currency' => 'USD', | |
86 | 'financial_account_id' => 1, | |
87 | 'payment_processor' => $paymentProcessor->id, | |
88 | 'is_active' => 1, | |
89 | 'is_allow_other_amount' => 1, | |
90 | 'min_amount' => 10, | |
91 | 'max_amount' => 1000, | |
92 | ); | |
93 | } | |
94 | ||
95 | function tearDown() { | |
96 | ||
97 | $this->contributionTypeDelete(); | |
98 | $this->quickCleanup(array( | |
99 | 'civicrm_contribution', | |
100 | 'civicrm_event', | |
101 | 'civicrm_contribution_page', | |
102 | 'civicrm_participant', | |
103 | 'civicrm_participant_payment', | |
104 | 'civicrm_line_item', | |
105 | 'civicrm_financial_trxn', | |
106 | 'civicrm_financial_item', | |
107 | 'civicrm_entity_financial_trxn', | |
108 | 'civicrm_contact', | |
109 | )); | |
110 | } | |
111 | ||
112 | ///////////////// civicrm_contribution_get methods | |
113 | function testGetEmptyParamsContribution() { | |
114 | $params = array(); | |
115 | $contribution = civicrm_api('contribution', 'get', $params); | |
116 | $this->assertEquals($contribution['is_error'], 1); | |
117 | $this->assertEquals($contribution['error_message'], 'Mandatory key(s) missing from params array: version'); | |
118 | } | |
119 | ||
120 | function testGetParamsNotArrayContribution() { | |
121 | $params = 'contact_id= 1'; | |
122 | $contribution = civicrm_api('contribution', 'get', $params); | |
123 | $this->assertEquals($contribution['is_error'], 1); | |
124 | $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array'); | |
125 | } | |
126 | ||
127 | function testGetContribution() { | |
128 | $p = array( | |
129 | 'contact_id' => $this->_individualId, | |
130 | 'receive_date' => '2010-01-20', | |
131 | 'total_amount' => 100.00, | |
132 | 'financial_type_id' => $this->_contributionTypeId, | |
133 | 'non_deductible_amount' => 10.00, | |
134 | 'fee_amount' => 5.00, | |
135 | 'net_amount' => 95.00, | |
136 | 'trxn_id' => 23456, | |
137 | 'invoice_id' => 78910, | |
138 | 'source' => 'SSF', | |
139 | 'contribution_status_id' => 1, | |
140 | 'version' => $this->_apiversion, | |
141 | ); | |
142 | $this->_contribution = civicrm_api('contribution', 'create', $p); | |
143 | $this->assertEquals($this->_contribution['is_error'], 0, 'In line ' . __LINE__); | |
144 | ||
145 | $params = array( | |
146 | 'contribution_id' => $this->_contribution['id'], | |
147 | 'version' => $this->_apiversion, | |
148 | ); | |
149 | $contribution = civicrm_api('contribution', 'get', $params); | |
150 | $financialParams['id'] = $this->_contributionTypeId; | |
151 | $default = null; | |
152 | $financialType = CRM_Financial_BAO_FinancialType::retrieve($financialParams,$default); | |
153 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__); | |
154 | $this->assertEquals(1,$contribution['count']); | |
155 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__); | |
156 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
157 | // note there was an assertion converting financial_type_id to 'Donation' which wasn't working. | |
158 | // passing back a string rather than an id seems like an error / cruft - & if it is to be introduced we should discuss | |
159 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1); | |
160 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
161 | $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00, 'In line ' . __LINE__); | |
162 | $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00, 'In line ' . __LINE__); | |
163 | $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00, 'In line ' . __LINE__); | |
164 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456, 'In line ' . __LINE__); | |
165 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910, 'In line ' . __LINE__); | |
166 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF', 'In line ' . __LINE__); | |
167 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed', 'In line ' . __LINE__); | |
168 | //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id) | |
169 | $p['trxn_id'] = '3847'; | |
170 | $p['invoice_id'] = '3847'; | |
171 | ||
172 | $contribution2 = civicrm_api('contribution', 'create', $p); | |
173 | $this->assertAPISuccess($contribution2, 'In line ' . __LINE__); | |
174 | ||
175 | $params = array( | |
176 | 'version' => $this->_apiversion, | |
177 | ); | |
178 | // now we have 2 - test getcount | |
179 | $contribution = civicrm_api('contribution', 'getcount', array( | |
180 | 'version' => $this->_apiversion, | |
181 | )); | |
182 | $this->assertEquals(2, $contribution); | |
183 | //test id only format | |
184 | $contribution = civicrm_api('contribution', 'get', array | |
185 | ('version' => $this->_apiversion, | |
186 | 'id' => $this->_contribution['id'], | |
187 | 'format.only_id' => 1, | |
188 | ) | |
189 | ); | |
190 | $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__); | |
191 | //test id only format | |
192 | $contribution = civicrm_api('contribution', 'get', array | |
193 | ('version' => $this->_apiversion, | |
194 | 'id' => $contribution2['id'], | |
195 | 'format.only_id' => 1, | |
196 | ) | |
197 | ); | |
198 | $this->assertEquals($contribution2['id'], $contribution); | |
199 | $contribution = civicrm_api('contribution', 'get', array( | |
200 | 'version' => $this->_apiversion, | |
201 | 'id' => $this->_contribution['id'], | |
202 | )); | |
203 | //test id as field | |
204 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__); | |
205 | $this->assertEquals(1, $contribution['count'], 'In line ' . __LINE__); | |
206 | // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ; | |
207 | //test get by contact id works | |
208 | $contribution = civicrm_api('contribution', 'get', array('version' => $this->_apiversion, 'contact_id' => $this->_individualId)); | |
209 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__ . "get with contact_id" . print_r(array('version' => $this->_apiversion, 'contact_id' => $this->_individualId), TRUE)); | |
210 | ||
211 | $this->assertEquals(2, $contribution['count'], 'In line ' . __LINE__); | |
212 | civicrm_api('Contribution', 'Delete', array( | |
213 | 'id' => $this->_contribution['id'], | |
214 | 'version' => $this->_apiversion, | |
215 | )); | |
216 | civicrm_api('Contribution', 'Delete', array( | |
217 | 'id' => $contribution2['id'], | |
218 | 'version' => $this->_apiversion, | |
219 | )); | |
220 | } | |
221 | ||
222 | /** | |
223 | * We need to ensure previous tested behaviour still works as part of the api contract | |
224 | */ | |
225 | function testGetContributionLegacyBehaviour() { | |
226 | $p = array( | |
227 | 'contact_id' => $this->_individualId, | |
228 | 'receive_date' => '2010-01-20', | |
229 | 'total_amount' => 100.00, | |
230 | 'contribution_type_id' => $this->_contributionTypeId, | |
231 | 'non_deductible_amount' => 10.00, | |
232 | 'fee_amount' => 5.00, | |
233 | 'net_amount' => 95.00, | |
234 | 'trxn_id' => 23456, | |
235 | 'invoice_id' => 78910, | |
236 | 'source' => 'SSF', | |
237 | 'contribution_status_id' => 1, | |
238 | 'version' => $this->_apiversion, | |
239 | ); | |
240 | $this->_contribution = civicrm_api('contribution', 'create', $p); | |
241 | $this->assertEquals($this->_contribution['is_error'], 0, 'In line ' . __LINE__); | |
242 | ||
243 | $params = array( | |
244 | 'contribution_id' => $this->_contribution['id'], | |
245 | 'version' => $this->_apiversion, | |
246 | ); | |
247 | $contribution = civicrm_api('contribution', 'get', $params); | |
248 | $financialParams['id'] = $this->_contributionTypeId; | |
249 | $default = null; | |
250 | $financialType = CRM_Financial_BAO_FinancialType::retrieve($financialParams,$default); | |
251 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__); | |
252 | $this->assertEquals(1,$contribution['count']); | |
253 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__); | |
254 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
255 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_contributionTypeId); | |
256 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
257 | $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00, 'In line ' . __LINE__); | |
258 | $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00, 'In line ' . __LINE__); | |
259 | $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00, 'In line ' . __LINE__); | |
260 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456, 'In line ' . __LINE__); | |
261 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910, 'In line ' . __LINE__); | |
262 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF', 'In line ' . __LINE__); | |
263 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed', 'In line ' . __LINE__); | |
264 | //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id) | |
265 | $p['trxn_id'] = '3847'; | |
266 | $p['invoice_id'] = '3847'; | |
267 | ||
268 | $contribution2 = civicrm_api('contribution', 'create', $p); | |
269 | $this->assertAPISuccess($contribution2, 'In line ' . __LINE__); | |
270 | ||
271 | $params = array( | |
272 | 'version' => $this->_apiversion, | |
273 | ); | |
274 | // now we have 2 - test getcount | |
275 | $contribution = civicrm_api('contribution', 'getcount', array( | |
276 | 'version' => $this->_apiversion, | |
277 | )); | |
278 | $this->assertEquals(2, $contribution); | |
279 | //test id only format | |
280 | $contribution = civicrm_api('contribution', 'get', array | |
281 | ('version' => $this->_apiversion, | |
282 | 'id' => $this->_contribution['id'], | |
283 | 'format.only_id' => 1, | |
284 | ) | |
285 | ); | |
286 | $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__); | |
287 | //test id only format | |
288 | $contribution = civicrm_api('contribution', 'get', array | |
289 | ('version' => $this->_apiversion, | |
290 | 'id' => $contribution2['id'], | |
291 | 'format.only_id' => 1, | |
292 | ) | |
293 | ); | |
294 | $this->assertEquals($contribution2['id'], $contribution); | |
295 | $contribution = civicrm_api('contribution', 'get', array( | |
296 | 'version' => $this->_apiversion, | |
297 | 'id' => $this->_contribution['id'], | |
298 | )); | |
299 | //test id as field | |
300 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__); | |
301 | $this->assertEquals(1, $contribution['count'], 'In line ' . __LINE__); | |
302 | // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ; | |
303 | //test get by contact id works | |
304 | $contribution = civicrm_api('contribution', 'get', array('version' => $this->_apiversion, 'contact_id' => $this->_individualId)); | |
305 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__ . "get with contact_id" . print_r(array('version' => $this->_apiversion, 'contact_id' => $this->_individualId), TRUE)); | |
306 | ||
307 | $this->assertEquals(2, $contribution['count'], 'In line ' . __LINE__); | |
308 | civicrm_api('Contribution', 'Delete', array( | |
309 | 'id' => $this->_contribution['id'], | |
310 | 'version' => $this->_apiversion, | |
311 | )); | |
312 | civicrm_api('Contribution', 'Delete', array( | |
313 | 'id' => $contribution2['id'], | |
314 | 'version' => $this->_apiversion, | |
315 | )); | |
316 | } | |
317 | ///////////////// civicrm_contribution_ | |
318 | function testCreateEmptyContributionIDUseDonation() { | |
319 | $params = array( | |
320 | 'contribution_id' => FALSE, | |
321 | 'contact_id' => 1, | |
322 | 'total_amount' => 1, | |
323 | 'version' => 3, | |
324 | 'check_permissions' => false, | |
325 | 'financial_type_id' => 'Donation', | |
326 | ); | |
327 | $contribution = civicrm_api('contribution', 'create', $params); | |
328 | $this->assertEquals($contribution['is_error'], 0, 'In line ' . __LINE__); | |
329 | } | |
330 | /* | |
331 | * ensure we continue to support contribution_type_id as part of the api commitment to | |
332 | * stability | |
333 | *///////////////// civicrm_contribution_ | |
334 | ||
335 | function testCreateLegacyBehaviour() { | |
336 | $params = array( | |
337 | 'contribution_id' => FALSE, | |
338 | 'contact_id' => 1, | |
339 | 'total_amount' => 1, | |
340 | 'version' => 3, | |
341 | 'check_permissions' => false, | |
342 | 'contribution_type_id' => 3, | |
343 | ); | |
344 | $contribution = civicrm_api('contribution', 'create', $params); | |
345 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__); | |
346 | $contribution = civicrm_api('contribution', 'getsingle', array('version' => $this->_apiversion, 'id' => $contribution['id'])); | |
347 | $this->assertEquals(3, $contribution['financial_type_id']); | |
348 | } | |
349 | ||
350 | ///////////////// civicrm_contribution_ | |
351 | function testCreateEmptyParamsContribution() { | |
352 | ||
353 | ||
354 | $params = array('version' => $this->_apiversion); | |
355 | $contribution = civicrm_api('contribution', 'create', $params); | |
356 | $this->assertEquals($contribution['is_error'], 1, 'In line ' . __LINE__); | |
357 | $this->assertEquals($contribution['error_message'], 'Mandatory key(s) missing from params array: financial_type_id, total_amount, contact_id', 'In line ' . __LINE__); | |
358 | } | |
359 | ||
360 | function testCreateParamsNotArrayContribution() { | |
361 | ||
362 | $params = 'contact_id= 1'; | |
363 | $contribution = civicrm_api('contribution', 'create', $params); | |
364 | $this->assertEquals($contribution['is_error'], 1); | |
365 | $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array'); | |
366 | } | |
367 | ||
368 | function testCreateParamsWithoutRequiredKeys() { | |
369 | $params = array('version' => 3); | |
370 | $contribution = civicrm_api('contribution', 'create', $params); | |
371 | $this->assertEquals($contribution['is_error'], 1); | |
372 | $this->assertEquals($contribution['error_message'], 'Mandatory key(s) missing from params array: financial_type_id, total_amount, contact_id'); | |
373 | } | |
374 | ||
375 | /** | |
376 | * check with complete array + custom field | |
377 | * Note that the test is written on purpose without any | |
378 | * variables specific to participant so it can be replicated into other entities | |
379 | * and / or moved to the automated test suite | |
380 | */ | |
381 | function testCreateWithCustom() { | |
382 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
383 | ||
384 | $params = $this->_params; | |
385 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
386 | ||
387 | $result = civicrm_api($this->_entity, 'create', $params); | |
388 | $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); | |
389 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
390 | $this->assertAPISuccess($result, ' in line ' . __LINE__); | |
391 | $check = civicrm_api($this->_entity, 'get', array( | |
392 | 'return.custom_' . $ids['custom_field_id'] => 1, | |
393 | 'version' => 3, | |
394 | 'id' => $result['id'], | |
395 | ) | |
396 | ); | |
397 | $this->customFieldDelete($ids['custom_field_id']); | |
398 | $this->customGroupDelete($ids['custom_group_id']); | |
399 | $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); | |
400 | } | |
401 | ||
402 | /** | |
403 | * check with complete array + custom field | |
404 | * Note that the test is written on purpose without any | |
405 | * variables specific to participant so it can be replicated into other entities | |
406 | * and / or moved to the automated test suite | |
407 | */ | |
408 | function testCreateGetFieldsWithCustom() { | |
409 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); | |
410 | $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php'); | |
411 | $result = civicrm_api('Contribution', 'getfields', array('version' => 3)); | |
412 | $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']); | |
413 | $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']); | |
414 | $this->customFieldDelete($ids['custom_field_id']); | |
415 | $this->customGroupDelete($ids['custom_group_id']); | |
416 | $this->customFieldDelete($idsContact['custom_field_id']); | |
417 | $this->customGroupDelete($idsContact['custom_group_id']); | |
418 | } | |
419 | ||
420 | function testCreateContributionNoLineItems() { | |
421 | ||
422 | $params = array( | |
423 | 'contact_id' => $this->_individualId, | |
424 | 'receive_date' => '20120511', | |
425 | 'total_amount' => 100.00, | |
426 | 'financial_type_id' => $this->_contributionTypeId, | |
427 | 'payment_instrument_id' => 1, | |
428 | 'non_deductible_amount' => 10.00, | |
429 | 'fee_amount' => 50.00, | |
430 | 'net_amount' => 90.00, | |
431 | 'trxn_id' => 12345, | |
432 | 'invoice_id' => 67890, | |
433 | 'source' => 'SSF', | |
434 | 'contribution_status_id' => 1, | |
435 | 'version' => $this->_apiversion, | |
436 | 'skipLineItem' => 1, | |
437 | ); | |
438 | ||
439 | $contribution = civicrm_api('contribution', 'create', $params); | |
440 | $lineItems = civicrm_api('line_item','get',array( | |
441 | 'version' => $this->_apiversion, | |
442 | 'entity_id' => $contribution['id'], | |
443 | 'entity_table' => 'civicrm_contribution', | |
444 | 'sequential' => 1, | |
445 | )); | |
446 | $this->assertEquals(0, $lineItems['count']); | |
447 | } | |
448 | /* | |
449 | * Test checks that passing in line items suppresses the create mechanism | |
450 | */ | |
451 | function testCreateContributionChainedLineItems() { | |
452 | ||
453 | $params = array( | |
454 | 'contact_id' => $this->_individualId, | |
455 | 'receive_date' => '20120511', | |
456 | 'total_amount' => 100.00, | |
457 | 'financial_type_id' => $this->_contributionTypeId, | |
458 | 'payment_instrument_id' => 1, | |
459 | 'non_deductible_amount' => 10.00, | |
460 | 'fee_amount' => 50.00, | |
461 | 'net_amount' => 90.00, | |
462 | 'trxn_id' => 12345, | |
463 | 'invoice_id' => 67890, | |
464 | 'source' => 'SSF', | |
465 | 'contribution_status_id' => 1, | |
466 | 'version' => $this->_apiversion, | |
467 | 'skipLineItem' => 1, | |
468 | 'api.line_item.create' => array( | |
469 | array( | |
470 | 'price_field_id' => 1, | |
471 | 'qty' => 2, | |
472 | 'line_total' => '20', | |
473 | 'unit_price' => '10', | |
474 | ), | |
475 | array( | |
476 | 'price_field_id' => 1, | |
477 | 'qty' => 1, | |
478 | 'line_total' => '80', | |
479 | 'unit_price' => '80', | |
480 | ), | |
481 | ), | |
482 | ); | |
483 | ||
484 | $contribution = civicrm_api('contribution', 'create', $params); | |
485 | $description = "Create Contribution with Nested Line Items"; | |
486 | $subfile = "CreateWithNestedLineItems"; | |
487 | $this->documentMe($params, $contribution, __FUNCTION__,__FILE__, $description, $subfile); | |
488 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__); | |
489 | $lineItems = civicrm_api('line_item','get',array( | |
490 | 'version' => $this->_apiversion, | |
491 | 'entity_id' => $contribution['id'], | |
492 | 'entity_table' => 'civicrm_contribution', | |
493 | 'sequential' => 1, | |
494 | )); | |
495 | $this->assertEquals(2, $lineItems['count']); | |
496 | } | |
497 | ||
498 | function testCreateContributionOffline() { | |
499 | $params = array( | |
500 | 'contact_id' => $this->_individualId, | |
501 | 'receive_date' => '20120511', | |
502 | 'total_amount' => 100.00, | |
503 | 'financial_type_id' => 1, | |
504 | 'trxn_id' => 12345, | |
505 | 'invoice_id' => 67890, | |
506 | 'source' => 'SSF', | |
507 | 'contribution_status_id' => 1, | |
508 | 'version' => $this->_apiversion, | |
509 | ); | |
510 | ||
511 | $contribution = civicrm_api('contribution', 'create', $params); | |
512 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__); | |
513 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
514 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
515 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ ); | |
516 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__); | |
517 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__); | |
518 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__); | |
519 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__); | |
520 | $lineItems = civicrm_api('line_item','get',array( | |
521 | 'version' => $this->_apiversion, | |
522 | 'entity_id' => $contribution['id'], | |
523 | 'entity_table' => 'civicrm_contribution', | |
524 | 'sequential' => 1, | |
525 | )); | |
526 | $this->assertEquals(1, $lineItems['count']); | |
527 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); | |
528 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); | |
529 | $lineItems = civicrm_api('line_item','get',array( | |
530 | 'version' => $this->_apiversion, | |
531 | 'entity_id' => $contribution['id'], | |
532 | 'entity_table' => 'civicrm_contribution', | |
533 | 'sequential' => 1, | |
534 | )); | |
535 | $this->assertEquals(1, $lineItems['count']); | |
536 | $this->_checkFinancialRecords($contribution, 'offline'); | |
537 | $this->contributionGetnCheck($params, $contribution['id']); | |
538 | } | |
539 | /* | |
540 | * | |
541 | */ | |
542 | function testCreateContributionWithPaymentInstrument() { | |
543 | $params = $this->_params + array('payment_instrument' => 'EFT'); | |
544 | $contribution = civicrm_api('contribution','create',$params); | |
545 | $contribution = civicrm_api('contribution','get',array('version'=> 3, 'sequential' => 1, 'id' => $contribution['id'])); | |
546 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); | |
547 | $eftTypeId = CRM_Core_OptionGroup::getValue('payment_instrument', 'EFT'); | |
548 | $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']); | |
549 | $contribution = civicrm_api('contribution', 'update', array('id' => $contribution['id'], 'version' => $this->_apiversion, 'payment_instrument' => 'Credit Card')); | |
550 | $this->assertAPISuccess($contribution); | |
551 | } | |
552 | ||
553 | function testGetContributionByPaymentInstrument() { | |
554 | $eftTypeId = CRM_Core_OptionGroup::getValue('payment_instrument', 'EFT'); | |
555 | $cashTypeId = CRM_Core_OptionGroup::getValue('payment_instrument', 'Cash'); | |
556 | $params = $this->_params + array('payment_instrument' => 'EFT'); | |
557 | $params2 = $this->_params + array('payment_instrument' => 'Cash',); | |
558 | civicrm_api('contribution','create',$params); | |
559 | $contribution = civicrm_api('contribution','create',$params2); | |
560 | $this->assertAPISuccess($contribution); | |
561 | $contribution = civicrm_api('contribution','get',array('version'=> 3, 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash')); | |
562 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); | |
563 | $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']); | |
564 | $this->assertEquals(1,$contribution['count']); | |
565 | $contribution = civicrm_api('contribution','get',array('version'=> 3, 'sequential' => 1, 'payment_instrument_id' => 'EFT')); | |
566 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); | |
567 | $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']); | |
568 | $this->assertEquals(1, $contribution['count']); | |
569 | $contribution = civicrm_api('contribution','get',array('version'=> 3, 'sequential' => 1, 'payment_instrument_id' => 5)); | |
570 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); | |
571 | $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']); | |
572 | $this->assertEquals(1,$contribution['count']); | |
573 | $contribution = civicrm_api('contribution','get',array('version'=> 3, 'sequential' => 1, 'payment_instrument' => 'EFT')); | |
574 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); | |
575 | $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']); | |
576 | $this->assertEquals(1,$contribution['count']); | |
577 | $contribution = civicrm_api('contribution', 'update', array('id' => $contribution['id'], 'version' => $this->_apiversion, 'payment_instrument' => 'Credit Card')); | |
578 | $this->assertAPISuccess($contribution); | |
579 | $contribution = civicrm_api('contribution','get',array('version'=> 3, 'sequential' => 1, 'id' => $contribution['id'], )); | |
580 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); | |
581 | $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']); | |
582 | $this->assertEquals(1,$contribution['count']); | |
583 | } | |
584 | ||
585 | /* | |
586 | * Create test with unique field name on source | |
587 | */ | |
588 | function testCreateContributionSource() { | |
589 | ||
590 | $params = array( | |
591 | 'contact_id' => $this->_individualId, | |
592 | 'receive_date' => date('Ymd'), | |
593 | 'total_amount' => 100.00, | |
594 | 'financial_type_id' => $this->_contributionTypeId, | |
595 | 'payment_instrument_id' => 1, | |
596 | 'non_deductible_amount' => 10.00, | |
597 | 'fee_amount' => 50.00, | |
598 | 'net_amount' => 90.00, | |
599 | 'trxn_id' => 12345, | |
600 | 'invoice_id' => 67890, | |
601 | 'contribution_source' => 'SSF', | |
602 | 'contribution_status_id' => 1, | |
603 | 'version' => $this->_apiversion, | |
604 | ); | |
605 | ||
606 | $contribution = civicrm_api('contribution', 'create', $params); | |
607 | ||
608 | $this->assertAPISuccess($contribution, 'In line ' . __LINE__); | |
609 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
610 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__); | |
611 | } | |
612 | /* | |
613 | * Create test with unique field name on source | |
614 | */ | |
615 | function testCreateContributionSourceInvalidContac() { | |
616 | ||
617 | $params = array( | |
618 | 'contact_id' => 999, | |
619 | 'receive_date' => date('Ymd'), | |
620 | 'total_amount' => 100.00, | |
621 | 'financial_type_id' => $this->_contributionTypeId, | |
622 | 'payment_instrument_id' => 1, | |
623 | 'non_deductible_amount' => 10.00, | |
624 | 'fee_amount' => 50.00, | |
625 | 'net_amount' => 90.00, | |
626 | 'trxn_id' => 12345, | |
627 | 'invoice_id' => 67890, | |
628 | 'contribution_source' => 'SSF', | |
629 | 'contribution_status_id' => 1, | |
630 | 'version' => $this->_apiversion, | |
631 | ); | |
632 | ||
633 | $contribution = civicrm_api('contribution', 'create', $params); | |
634 | $this->assertEquals($contribution['error_message'], 'contact_id is not valid : 999', 'In line ' . __LINE__); | |
635 | } | |
636 | ||
637 | function testCreateContributionSourceInvalidContContac() { | |
638 | ||
639 | $params = array( | |
640 | 'contribution_contact_id' => 999, | |
641 | 'receive_date' => date('Ymd'), | |
642 | 'total_amount' => 100.00, | |
643 | 'financial_type_id' => $this->_contributionTypeId, | |
644 | 'payment_instrument_id' => 1, | |
645 | 'non_deductible_amount' => 10.00, | |
646 | 'fee_amount' => 50.00, | |
647 | 'net_amount' => 90.00, | |
648 | 'trxn_id' => 12345, | |
649 | 'invoice_id' => 67890, | |
650 | 'contribution_source' => 'SSF', | |
651 | 'contribution_status_id' => 1, | |
652 | 'version' => $this->_apiversion, | |
653 | ); | |
654 | ||
655 | $contribution = civicrm_api('contribution', 'create', $params); | |
656 | $this->assertEquals($contribution['error_message'], 'contact_id is not valid : 999', 'In line ' . __LINE__); | |
657 | } | |
658 | ||
659 | function testCreateContributionWithNote() { | |
660 | $description = "Demonstrates creating contribution with Note Entity"; | |
661 | $subfile = "ContributionCreateWithNote"; | |
662 | $params = array( | |
663 | 'contact_id' => $this->_individualId, | |
664 | 'receive_date' => '2012-01-01', | |
665 | 'total_amount' => 100.00, | |
666 | 'financial_type_id' => $this->_contributionTypeId, | |
667 | 'payment_instrument_id' => 1, | |
668 | 'non_deductible_amount' => 10.00, | |
669 | 'fee_amount' => 50.00, | |
670 | 'net_amount' => 90.00, | |
671 | 'trxn_id' => 12345, | |
672 | 'invoice_id' => 67890, | |
673 | 'source' => 'SSF', | |
674 | 'contribution_status_id' => 1, | |
675 | 'version' => $this->_apiversion, | |
676 | 'note' => 'my contribution note', | |
677 | ); | |
678 | ||
679 | $contribution = civicrm_api('contribution', 'create', $params); | |
680 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__, $description, $subfile); | |
681 | $result = civicrm_api('note', 'get', array('version' => 3, 'entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1)); | |
682 | $this->assertAPISuccess($result); | |
683 | $this->assertEquals('my contribution note', $result['values'][0]['note']); | |
684 | civicrm_api('contribution', 'delete', array('version' => 3, 'id' => $contribution['id'])); | |
685 | } | |
686 | ||
687 | function testCreateContributionWithNoteUniqueNameAliases() { | |
688 | $params = array( | |
689 | 'contact_id' => $this->_individualId, | |
690 | 'receive_date' => '2012-01-01', | |
691 | 'total_amount' => 100.00, | |
692 | 'financial_type_id' => $this->_contributionTypeId, | |
693 | 'payment_instrument_id' => 1, | |
694 | 'non_deductible_amount' => 10.00, | |
695 | 'fee_amount' => 50.00, | |
696 | 'net_amount' => 90.00, | |
697 | 'trxn_id' => 12345, | |
698 | 'invoice_id' => 67890, | |
699 | 'source' => 'SSF', | |
700 | 'contribution_status_id' => 1, | |
701 | 'version' => $this->_apiversion, | |
702 | 'contribution_note' => 'my contribution note', | |
703 | ); | |
704 | ||
705 | $contribution = civicrm_api('contribution', 'create', $params); | |
706 | $result = civicrm_api('note', 'get', array('version' => 3, 'entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1)); | |
707 | $this->assertAPISuccess($result); | |
708 | $this->assertEquals('my contribution note', $result['values'][0]['note']); | |
709 | civicrm_api('contribution', 'delete', array('version' => 3, 'id' => $contribution['id'])); | |
710 | } | |
711 | /* | |
712 | * This is the test for creating soft credits - however a 'get' is not yet possible via API | |
713 | * as the current BAO functions are contact-centric (from what I can find) | |
714 | * | |
715 | */ | |
716 | function testCreateContributionWithSoftCredt() { | |
717 | $description = "Demonstrates creating contribution with SoftCredit"; | |
718 | $subfile = "ContributionCreateWithSoftCredit"; | |
719 | $contact2 = civicrm_api('Contact', 'create', array('version' => 3, 'display_name' => 'superman', 'version' => 3, 'contact_type' => 'Individual')); | |
720 | $params = $this->_params + array( | |
721 | 'soft_credit_to' => $contact2['id'], | |
722 | ); | |
723 | ||
724 | $contribution = civicrm_api('contribution', 'create', $params); | |
725 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__, $description, $subfile); | |
726 | // $result = civicrm_api('contribution','get', array('version' => 3,'return'=> 'soft_credit_to', 'sequential' => 1)); | |
727 | // $this->assertAPISuccess($result); | |
728 | // $this->assertEquals($contact2['id'], $result['values'][$result['id']]['soft_credit_to']) ; | |
729 | // well - the above doesn't work yet so lets do SQL | |
730 | $query = "SELECT count(*) FROM civicrm_contribution_soft WHERE contact_id = " . $contact2['id']; | |
731 | $count = CRM_Core_DAO::singleValueQuery($query); | |
732 | $this->assertEquals(1, $count); | |
733 | ||
734 | civicrm_api('contribution', 'delete', array('version' => 3, 'id' => $contribution['id'])); | |
735 | civicrm_api('contact', 'delete', array('version' => 3, 'id' => $contact2['id'])); | |
736 | } | |
737 | ||
738 | /** | |
739 | * Test using example code | |
740 | */ | |
741 | function testContributionCreateExample() { | |
742 | //make sure at least on page exists since there is a truncate in tear down | |
743 | $page = civicrm_api('contribution_page', 'create', $this->_pageParams); | |
744 | $this->assertAPISuccess($page); | |
745 | require_once 'api/v3/examples/ContributionCreate.php'; | |
746 | $result = contribution_create_example(); | |
747 | $this->assertAPISuccess($result); | |
748 | $contributionId = $result['id']; | |
749 | $expectedResult = contribution_create_expectedresult(); | |
750 | $this->checkArrayEquals($result, $expectedResult); | |
751 | $this->contributionDelete($contributionId); | |
752 | } | |
753 | ||
754 | /* | |
755 | * Function tests that additional financial records are created when fee amount is recorded | |
756 | */ | |
757 | function testCreateContributionWithFee() { | |
758 | $params = array( | |
759 | 'contact_id' => $this->_individualId, | |
760 | 'receive_date' => '20120511', | |
761 | 'total_amount' => 100.00, | |
762 | 'fee_amount' => 50, | |
763 | 'financial_type_id' => 1, | |
764 | 'trxn_id' => 12345, | |
765 | 'invoice_id' => 67890, | |
766 | 'source' => 'SSF', | |
767 | 'contribution_status_id' => 1, | |
768 | 'version' => $this->_apiversion, | |
769 | ); | |
770 | ||
771 | $contribution = civicrm_api('contribution', 'create', $params); | |
772 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__); | |
773 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
774 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
775 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ ); | |
776 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__); | |
777 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__); | |
778 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__); | |
779 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__); | |
780 | $lineItems = civicrm_api('line_item','get',array( | |
781 | 'version' => $this->_apiversion, | |
782 | 'entity_id' => $contribution['id'], | |
783 | 'entity_table' => 'civicrm_contribution', | |
784 | 'sequential' => 1, | |
785 | )); | |
786 | $this->assertEquals(1, $lineItems['count']); | |
787 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); | |
788 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); | |
789 | $lineItems = civicrm_api('line_item','get',array( | |
790 | 'version' => $this->_apiversion, | |
791 | 'entity_id' => $contribution['id'], | |
792 | 'entity_table' => 'civicrm_contribution', | |
793 | 'sequential' => 1, | |
794 | )); | |
795 | $this->assertEquals(1, $lineItems['count']); | |
796 | $this->_checkFinancialRecords($contribution, 'feeAmount'); | |
797 | } | |
798 | ||
799 | ||
800 | /* | |
801 | * Function tests that additional financial records are created when online contribution is created | |
802 | */ | |
803 | function testCreateContributionOnline() { | |
804 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); | |
805 | $contributionPage = civicrm_api( 'contribution_page','create', $this->_pageParams ); | |
806 | ||
807 | $params = array( | |
808 | 'contact_id' => $this->_individualId, | |
809 | 'receive_date' => '20120511', | |
810 | 'total_amount' => 100.00, | |
811 | 'financial_type_id' => 1, | |
812 | 'contribution_page_id' => $contributionPage['id'], | |
813 | 'payment_processor' => 1, | |
814 | 'trxn_id' => 12345, | |
815 | 'invoice_id' => 67890, | |
816 | 'source' => 'SSF', | |
817 | 'contribution_status_id' => 1, | |
818 | 'version' => $this->_apiversion, | |
819 | ); | |
820 | ||
821 | $contribution = civicrm_api('contribution', 'create', $params); | |
822 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__); | |
823 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
824 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
825 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ ); | |
826 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__); | |
827 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__); | |
828 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__); | |
829 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__); | |
830 | $this->_checkFinancialRecords($contribution, 'online'); | |
831 | } | |
832 | ||
833 | /* | |
834 | * Function tests that additional financial records are created when online contribution with pay later option | |
835 | * is created | |
836 | */ | |
837 | function testCreateContributionPayLaterOnline() { | |
838 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); | |
839 | $this->_pageParams['is_pay_later'] = 1; | |
840 | $contributionPage = civicrm_api( 'contribution_page','create',$this->_pageParams ); | |
841 | ||
842 | $params = array( | |
843 | 'contact_id' => $this->_individualId, | |
844 | 'receive_date' => '20120511', | |
845 | 'total_amount' => 100.00, | |
846 | 'financial_type_id' => 1, | |
847 | 'contribution_page_id' => $contributionPage['id'], | |
848 | 'trxn_id' => 12345, | |
849 | 'is_pay_later' => 1, | |
850 | 'invoice_id' => 67890, | |
851 | 'source' => 'SSF', | |
852 | 'contribution_status_id' => 2, | |
853 | 'version' => $this->_apiversion, | |
854 | ); | |
855 | ||
856 | $contribution = civicrm_api('contribution', 'create', $params); | |
857 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__); | |
858 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
859 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
860 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ ); | |
861 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__); | |
862 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__); | |
863 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__); | |
864 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__); | |
865 | $this->_checkFinancialRecords($contribution, 'payLater'); | |
866 | } | |
867 | ||
868 | /* | |
869 | * Function tests that additional financial records are created when online contribution with pending option | |
870 | * is created | |
871 | */ | |
872 | function testCreateContributionPendingOnline() { | |
873 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); | |
874 | $contributionPage = civicrm_api( 'contribution_page', 'create', $this->_pageParams ); | |
875 | ||
876 | $params = array( | |
877 | 'contact_id' => $this->_individualId, | |
878 | 'receive_date' => '20120511', | |
879 | 'total_amount' => 100.00, | |
880 | 'financial_type_id' => 1, | |
881 | 'contribution_page_id' => $contributionPage['id'], | |
882 | 'trxn_id' => 12345, | |
883 | 'invoice_id' => 67890, | |
884 | 'source' => 'SSF', | |
885 | 'contribution_status_id' => 2, | |
886 | 'version' => $this->_apiversion, | |
887 | ); | |
888 | ||
889 | $contribution = civicrm_api('contribution', 'create', $params); | |
890 | $this->documentMe($params, $contribution, __FUNCTION__, __FILE__); | |
891 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
892 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__); | |
893 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ ); | |
894 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__); | |
895 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__); | |
896 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__); | |
897 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__); | |
898 | $this->_checkFinancialRecords($contribution, 'pending'); | |
899 | } | |
900 | ||
901 | /* | |
902 | * Function tests that line items, financial records are updated when contribution amount is changed | |
903 | */ | |
904 | function testCreateUpdateContributionChangeTotal() { | |
905 | $contribution = civicrm_api('contribution', 'create', $this->_params); | |
906 | $lineItems = civicrm_api('line_item','getvalue', array( | |
907 | 'version' => $this->_apiversion, | |
908 | 'entity_id' => $contribution['id'], | |
909 | 'entity_table' => 'civicrm_contribution', | |
910 | 'sequential' => 1, | |
911 | 'return' => 'line_total', | |
912 | )); | |
913 | $this->assertEquals('100.00', $lineItems); | |
914 | $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']); | |
915 | // Financial trxn SUM = 100 + 5 (fee) | |
916 | $this->assertEquals('105.00', $trxnAmount); | |
917 | $newParams = array( | |
918 | 'version' => $this->_apiversion, | |
919 | 'id' => $contribution['id'], | |
920 | 'total_amount' => '125'); | |
921 | $contribution = civicrm_api('contribution', 'update', $newParams); | |
922 | ||
923 | $lineItems = civicrm_api('line_item','getvalue',array( | |
924 | 'version' => $this->_apiversion, | |
925 | 'entity_id' => $contribution['id'], | |
926 | 'entity_table' => 'civicrm_contribution', | |
927 | 'sequential' => 1, | |
928 | 'return' => 'line_total', | |
929 | )); | |
930 | ||
931 | $this->assertEquals('125.00', $lineItems); | |
932 | $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']); | |
933 | $fitemAmount = $this->_getFinancialItemAmount($contribution['id']); | |
934 | // Financial trxn SUM = 125 + 5 (fee) | |
935 | $this->assertEquals('130.00', $trxnAmount); | |
936 | $this->assertEquals('125.00', $fitemAmount); | |
937 | } | |
938 | ||
939 | /* | |
940 | * Function tests that line items, financial records are updated when pay later contribution is received | |
941 | */ | |
942 | function testCreateUpdateContributionPayLater() { | |
943 | $contribParams = array( | |
944 | 'contact_id' => $this->_individualId, | |
945 | 'receive_date' => '2012-01-01', | |
946 | 'total_amount' => 100.00, | |
947 | 'financial_type_id' => $this->_contributionTypeId, | |
948 | 'payment_instrument_id' => 1, | |
c71ae314 PN |
949 | 'contribution_status_id' => 2, |
950 | 'is_pay_later' => 1, | |
6a488035 TO |
951 | 'version' => $this->_apiversion, |
952 | ); | |
953 | $contribution = civicrm_api('contribution', 'create', $contribParams); | |
954 | ||
955 | $newParams = array_merge($contribParams, array( | |
c71ae314 PN |
956 | 'id' => $contribution['id'], |
957 | 'contribution_status_id' => 1,) | |
958 | ); | |
6a488035 TO |
959 | $contribution = civicrm_api('contribution', 'update', $newParams); |
960 | $contribution = $contribution['values'][$contribution['id']]; | |
961 | $this->assertEquals($contribution['contribution_status_id'],'1'); | |
962 | $this->_checkFinancialItem($contribution['id'], 'paylater'); | |
963 | $this->_checkFinancialTrxn($contribution, 'payLater'); | |
964 | } | |
965 | ||
966 | /* | |
967 | * Function tests that financial records are updated when Payment Instrument is changed | |
968 | */ | |
969 | function testCreateUpdateContributionPaymentInstrument() { | |
970 | $instrumentId = $this->_addPaymentInstrument(); | |
971 | $contribParams = array( | |
972 | 'contact_id' => $this->_individualId, | |
973 | 'total_amount' => 100.00, | |
974 | 'financial_type_id' => $this->_contributionTypeId, | |
975 | 'payment_instrument_id' => 4, | |
976 | 'contribution_status_id' => 1, | |
977 | 'version' => $this->_apiversion, | |
978 | ); | |
979 | $contribution = civicrm_api('contribution', 'create', $contribParams); | |
980 | ||
981 | $newParams = array_merge($contribParams, array( | |
982 | 'id' => $contribution['id'], | |
983 | 'payment_instrument_id' => $instrumentId,) | |
984 | ); | |
985 | $contribution = civicrm_api('contribution', 'update', $newParams); | |
986 | $this->assertAPISuccess($contribution); | |
987 | $this->_checkFinancialTrxn($contribution, 'paymentInstrument'); | |
988 | } | |
989 | ||
990 | /* | |
991 | * Function tests that financial records are added when Contribution is Refunded | |
992 | */ | |
993 | function testCreateUpdateContributionRefund() { | |
994 | $contribParams = array( | |
995 | 'contact_id' => $this->_individualId, | |
996 | 'receive_date' => '2012-01-01', | |
997 | 'total_amount' => 100.00, | |
998 | 'financial_type_id' => $this->_contributionTypeId, | |
999 | 'payment_instrument_id' => 4, | |
1000 | 'contribution_status_id' => 1, | |
1001 | 'version' => $this->_apiversion, | |
1002 | ); | |
1003 | $contribution = civicrm_api('contribution', 'create', $contribParams); | |
1004 | $newParams = array_merge($contribParams, array( | |
1005 | 'id' => $contribution['id'], | |
1006 | 'contribution_status_id' => 7, | |
1007 | ) | |
1008 | ); | |
1009 | ||
1010 | $contribution = civicrm_api('contribution', 'update', $newParams); | |
1011 | $this->_checkFinancialTrxn($contribution, 'refund'); | |
1012 | $this->_checkFinancialItem($contribution['id'], 'refund'); | |
c71ae314 PN |
1013 | } |
1014 | ||
1015 | /* | |
1016 | * Function tests invalid contribution status change | |
1017 | */ | |
1018 | function testCreateUpdateContributionInValidStatusChange() { | |
1019 | $contribParams = array( | |
1020 | 'contact_id' => 1, | |
1021 | 'receive_date' => '2012-01-01', | |
1022 | 'total_amount' => 100.00, | |
1023 | 'financial_type_id' => 1, | |
1024 | 'payment_instrument_id' => 1, | |
1025 | 'contribution_status_id' => 1, | |
1026 | 'version' => 3, | |
1027 | ); | |
1028 | $contribution = civicrm_api('contribution', 'create', $contribParams); | |
1029 | $newParams = array_merge($contribParams, array( | |
1030 | 'id' => $contribution['id'], | |
1031 | 'contribution_status_id' => 2, | |
1032 | ) | |
1033 | ); | |
1034 | $contribution = civicrm_api('contribution', 'update', $newParams); | |
1035 | $this->assertTrue(!empty($contribution['is_error']), 'In line ' . __LINE__); | |
1036 | $this->assertEquals($contribution['error_message'], ts('Cannot change contribution status from Completed to Pending.'), 'In line ' . __LINE__); | |
1037 | ||
6a488035 TO |
1038 | } |
1039 | ||
1040 | /* | |
1041 | * Function tests that financial records are added when Pending Contribution is Canceled | |
1042 | */ | |
1043 | function testCreateUpdateContributionCancelPending() { | |
1044 | $contribParams = array( | |
1045 | 'contact_id' => $this->_individualId, | |
1046 | 'receive_date' => '2012-01-01', | |
1047 | 'total_amount' => 100.00, | |
1048 | 'financial_type_id' => $this->_contributionTypeId, | |
1049 | 'payment_instrument_id' => 1, | |
1050 | 'contribution_status_id' => 2, | |
c71ae314 | 1051 | 'is_pay_later' => 1, |
6a488035 TO |
1052 | 'version' => $this->_apiversion, |
1053 | ); | |
1054 | $contribution = civicrm_api('contribution', 'create', $contribParams); | |
1055 | $newParams = array_merge($contribParams, array( | |
1056 | 'id' => $contribution['id'], | |
1057 | 'contribution_status_id' => 3, | |
1058 | ) | |
1059 | ); | |
1060 | $contribution = civicrm_api('contribution', 'update', $newParams); | |
1061 | $this->_checkFinancialTrxn($contribution, 'cancelPending'); | |
1062 | $this->_checkFinancialItem($contribution['id'], 'cancelPending'); | |
1063 | } | |
1064 | ||
1065 | /* | |
1066 | * Function tests that financial records are added when Financial Type is Changed | |
1067 | */ | |
1068 | function testCreateUpdateContributionChangeFinancialType() { | |
1069 | $contribParams = array( | |
1070 | 'contact_id' => $this->_individualId, | |
1071 | 'receive_date' => '2012-01-01', | |
1072 | 'total_amount' => 100.00, | |
1073 | 'financial_type_id' => 1, | |
1074 | 'payment_instrument_id' => 1, | |
1075 | 'contribution_status_id' => 1, | |
1076 | 'version' => $this->_apiversion, | |
1077 | ); | |
1078 | $contribution = civicrm_api('contribution', 'create', $contribParams); | |
1079 | $newParams = array_merge($contribParams, array( | |
1080 | 'id' => $contribution['id'], | |
1081 | 'financial_type_id' => 3, | |
1082 | ) | |
1083 | ); | |
1084 | $contribution = civicrm_api('contribution', 'update', $newParams); | |
1085 | $this->_checkFinancialTrxn($contribution, 'changeFinancial'); | |
1086 | $this->_checkFinancialItem($contribution['id'], 'changeFinancial'); | |
1087 | } | |
1088 | ||
1089 | //To Update Contribution | |
1090 | //CHANGE: we require the API to do an incremental update | |
1091 | function testCreateUpdateContribution() { | |
1092 | ||
1093 | $contributionID = $this->contributionCreate($this->_individualId, $this->_contributionTypeId, 'idofsh', 212355); | |
1094 | $old_params = array( | |
1095 | 'contribution_id' => $contributionID, | |
1096 | 'version' => $this->_apiversion, | |
1097 | ); | |
1098 | $original = civicrm_api('contribution', 'get', $old_params); | |
1099 | //Make sure it came back | |
1100 | $this->assertTrue(empty($original['is_error']), 'In line ' . __LINE__); | |
1101 | $this->assertEquals($original['id'], $contributionID, 'In line ' . __LINE__); | |
1102 | //set up list of old params, verify | |
1103 | ||
1104 | //This should not be required on update: | |
1105 | $old_contact_id = $original['values'][$contributionID]['contact_id']; | |
1106 | $old_payment_instrument = $original['values'][$contributionID]['instrument_id']; | |
1107 | $old_fee_amount = $original['values'][$contributionID]['fee_amount']; | |
1108 | $old_source = $original['values'][$contributionID]['contribution_source']; | |
1109 | ||
1110 | //note: current behavior is to return ISO. Is this | |
1111 | //documented behavior? Is this correct | |
1112 | $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date'])); | |
1113 | ||
1114 | $old_trxn_id = $original['values'][$contributionID]['trxn_id']; | |
1115 | $old_invoice_id = $original['values'][$contributionID]['invoice_id']; | |
1116 | ||
1117 | //check against values in CiviUnitTestCase::createContribution() | |
1118 | $this->assertEquals($old_contact_id, $this->_individualId, 'In line ' . __LINE__); | |
1119 | $this->assertEquals($old_fee_amount, 5.00, 'In line ' . __LINE__); | |
1120 | $this->assertEquals($old_source, 'SSF', 'In line ' . __LINE__); | |
1121 | $this->assertEquals($old_trxn_id, 212355, 'In line ' . __LINE__); | |
1122 | $this->assertEquals($old_invoice_id, 'idofsh', 'In line ' . __LINE__); | |
1123 | $params = array( | |
1124 | 'id' => $contributionID, | |
1125 | 'contact_id' => $this->_individualId, | |
1126 | 'total_amount' => 110.00, | |
1127 | 'financial_type_id' => $this->_contributionTypeId, | |
1128 | 'non_deductible_amount' => 10.00, | |
1129 | 'net_amount' => 100.00, | |
1130 | 'contribution_status_id' => 1, | |
1131 | 'note' => 'Donating for Nobel Cause', | |
1132 | 'version' => $this->_apiversion, | |
1133 | ); | |
1134 | ||
1135 | $contribution = civicrm_api('contribution', 'create', $params); | |
1136 | ||
1137 | $new_params = array( | |
1138 | 'contribution_id' => $contribution['id'], | |
1139 | 'version' => $this->_apiversion, | |
1140 | ); | |
1141 | $contribution = civicrm_api('contribution', 'get', $new_params); | |
1142 | ||
1143 | $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId, 'In line ' . __LINE__); | |
1144 | $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00, 'In line ' . __LINE__); | |
1145 | $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_contributionTypeId, 'In line ' . __LINE__ ); | |
1146 | $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument, 'In line ' . __LINE__); | |
1147 | $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00, 'In line ' . __LINE__); | |
1148 | $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount, 'In line ' . __LINE__); | |
1149 | $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00, 'In line ' . __LINE__); | |
1150 | $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id, 'In line ' . __LINE__); | |
1151 | $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id, 'In line ' . __LINE__); | |
1152 | $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source, 'In line ' . __LINE__); | |
1153 | $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed', 'In line ' . __LINE__); | |
1154 | $params = array( | |
1155 | 'contribution_id' => $contributionID, | |
1156 | 'version' => $this->_apiversion, | |
1157 | ); | |
1158 | $result = civicrm_api('contribution', 'delete', $params); | |
1159 | $this->assertEquals($result['is_error'], 0, 'in line' . __LINE__); | |
1160 | } | |
1161 | ||
1162 | ///////////////// civicrm_contribution_delete methods | |
1163 | function testDeleteEmptyParamsContribution() { | |
1164 | $params = array('version' => $this->_apiversion); | |
1165 | $contribution = civicrm_api('contribution', 'delete', $params); | |
1166 | $this->assertEquals($contribution['is_error'], 1); | |
1167 | } | |
1168 | ||
1169 | function testDeleteParamsNotArrayContribution() { | |
1170 | $params = 'contribution_id= 1'; | |
1171 | $contribution = civicrm_api('contribution', 'delete', $params); | |
1172 | $this->assertEquals($contribution['is_error'], 1); | |
1173 | $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array'); | |
1174 | } | |
1175 | ||
1176 | function testDeleteWrongParamContribution() { | |
1177 | $params = array( | |
1178 | 'contribution_source' => 'SSF', | |
1179 | 'version' => $this->_apiversion, | |
1180 | ); | |
1181 | $contribution = civicrm_api('contribution', 'delete', $params); | |
1182 | $this->assertEquals($contribution['is_error'], 1); | |
1183 | } | |
1184 | ||
1185 | function testDeleteContribution() { | |
1186 | ||
1187 | $contributionID = $this->contributionCreate($this->_individualId, $this->_contributionTypeId, 'dfsdf', 12389); | |
1188 | $params = array( | |
1189 | 'id' => $contributionID, | |
1190 | 'version' => $this->_apiversion, | |
1191 | ); | |
1192 | $result = civicrm_api('contribution', 'delete', $params); | |
1193 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
1194 | $this->assertEquals($result['is_error'], 0, 'In line ' . __LINE__); | |
1195 | } | |
1196 | ||
1197 | ///////////////// civicrm_contribution_search methods | |
1198 | ||
1199 | /** | |
1200 | * Test civicrm_contribution_search with wrong params type | |
1201 | */ | |
1202 | function testSearchWrongParamsType() { | |
1203 | $params = 'a string'; | |
1204 | $result = civicrm_api('contribution', 'get', $params); | |
1205 | ||
1206 | $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__); | |
1207 | $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__); | |
1208 | } | |
1209 | ||
1210 | /** | |
1211 | * Test civicrm_contribution_search with empty params. | |
1212 | * All available contributions expected. | |
1213 | */ | |
1214 | function testSearchEmptyParams() { | |
1215 | $params = array('version' => $this->_apiversion); | |
1216 | ||
1217 | $p = array( | |
1218 | 'contact_id' => $this->_individualId, | |
1219 | 'receive_date' => date('Ymd'), | |
1220 | 'total_amount' => 100.00, | |
1221 | 'financial_type_id' => $this->_contributionTypeId, | |
1222 | 'non_deductible_amount' => 10.00, | |
1223 | 'fee_amount' => 5.00, | |
1224 | 'net_amount' => 95.00, | |
1225 | 'trxn_id' => 23456, | |
1226 | 'invoice_id' => 78910, | |
1227 | 'source' => 'SSF', | |
1228 | 'contribution_status_id' => 1, | |
1229 | 'version' => $this->_apiversion, | |
1230 | ); | |
1231 | $contribution = civicrm_api('contribution', 'create', $p); | |
1232 | ||
1233 | $result = civicrm_api('contribution', 'get', $params); | |
1234 | // We're taking the first element. | |
1235 | $res = $result['values'][$contribution['id']]; | |
1236 | ||
1237 | $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__); | |
1238 | $this->assertEquals($p['total_amount'], $res['total_amount'], 'In line ' . __LINE__); | |
1239 | $this->assertEquals($p['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ ); | |
1240 | $this->assertEquals($p['net_amount'], $res['net_amount'], 'In line ' . __LINE__); | |
1241 | $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__); | |
1242 | $this->assertEquals($p['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__); | |
1243 | $this->assertEquals($p['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__); | |
1244 | $this->assertEquals($p['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__); | |
1245 | $this->assertEquals($p['source'], $res['contribution_source'], 'In line ' . __LINE__); | |
1246 | // contribution_status_id = 1 => Completed | |
1247 | $this->assertEquals('Completed', $res['contribution_status'], 'In line ' . __LINE__); | |
1248 | ||
1249 | $this->contributionDelete($contribution['id']); | |
1250 | } | |
1251 | ||
1252 | /** | |
1253 | * Test civicrm_contribution_search. Success expected. | |
1254 | */ | |
1255 | function testSearch() { | |
1256 | $p1 = array( | |
1257 | 'contact_id' => $this->_individualId, | |
1258 | 'receive_date' => date('Ymd'), | |
1259 | 'total_amount' => 100.00, | |
1260 | 'financial_type_id' => $this->_contributionTypeId, | |
1261 | 'non_deductible_amount' => 10.00, | |
1262 | 'contribution_status_id' => 1, | |
1263 | 'version' => $this->_apiversion, | |
1264 | ); | |
1265 | $contribution1 = civicrm_api('contribution', 'create', $p1); | |
1266 | ||
1267 | $p2 = array( | |
1268 | 'contact_id' => $this->_individualId, | |
1269 | 'receive_date' => date('Ymd'), | |
1270 | 'total_amount' => 200.00, | |
1271 | 'financial_type_id' => $this->_contributionTypeId, | |
1272 | 'non_deductible_amount' => 20.00, | |
1273 | 'trxn_id' => 5454565, | |
1274 | 'invoice_id' => 1212124, | |
1275 | 'fee_amount' => 50.00, | |
1276 | 'net_amount' => 60.00, | |
1277 | 'contribution_status_id' => 2, | |
1278 | 'version' => $this->_apiversion, | |
1279 | ); | |
1280 | $contribution2 = civicrm_api('contribution', 'create', $p2); | |
1281 | ||
1282 | $params = array( | |
1283 | 'contribution_id' => $contribution2['id'], | |
1284 | 'version' => $this->_apiversion, | |
1285 | ); | |
1286 | $result = civicrm_api('contribution', 'get', $params); | |
1287 | $res = $result['values'][$contribution2['id']]; | |
1288 | ||
1289 | $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__); | |
1290 | $this->assertEquals($p2['total_amount'], $res['total_amount'], 'In line ' . __LINE__); | |
1291 | $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ ); | |
1292 | $this->assertEquals($p2['net_amount'], $res['net_amount'], 'In line ' . __LINE__); | |
1293 | $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__); | |
1294 | $this->assertEquals($p2['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__); | |
1295 | $this->assertEquals($p2['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__); | |
1296 | $this->assertEquals($p2['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__); | |
1297 | // contribution_status_id = 2 => Pending | |
1298 | $this->assertEquals('Pending', $res['contribution_status'], 'In line ' . __LINE__); | |
1299 | ||
1300 | $this->contributionDelete($contribution1['id']); | |
1301 | $this->contributionDelete($contribution2['id']); | |
1302 | } | |
1303 | /* | |
1304 | * Test sending a mail via the API | |
1305 | */ | |
1306 | function testSendMail() { | |
1307 | $mut = new CiviMailUtils( $this, true ); | |
1308 | $contribution = civicrm_api('contribution','create',$this->_params); | |
1309 | $this->assertAPISuccess($contribution); | |
1310 | $apiResult = civicrm_api('contribution', 'sendconfirmation', array( | |
1311 | 'version' => $this->_apiversion, | |
1312 | 'id' => $contribution['id'], | |
1313 | 'receipt_from_email' => 'api@civicrm.org', | |
1314 | ) | |
1315 | ); | |
1316 | $this->assertAPISuccess($apiResult); | |
1317 | $mut->checkMailLog(array( | |
1318 | '$ 100.00', | |
1319 | 'Contribution Information', | |
1320 | 'Please print this confirmation for your records', | |
1321 | ), array( | |
1322 | 'Event' | |
1323 | ) | |
1324 | ); | |
1325 | $mut->stop(); | |
1326 | } | |
1327 | ||
1328 | /* | |
1329 | * Test sending a mail via the API | |
1330 | */ | |
1331 | function testSendMailEvent() { | |
1332 | $mut = new CiviMailUtils( $this, true ); | |
1333 | $contribution = civicrm_api('contribution','create',$this->_params); | |
1334 | $event = $this->eventCreate(array( | |
1335 | 'is_email_confirm' => 1, | |
1336 | 'confirm_from_email' => 'test@civicrm.org', | |
1337 | )); | |
1338 | $this->_eventID = $event['id']; | |
1339 | $participantParams = array( | |
1340 | 'contact_id' => $this->_individualId, | |
1341 | 'event_id' => $this->_eventID, | |
1342 | 'status_id' => 1, | |
1343 | 'role_id' => 1, | |
1344 | // to ensure it matches later on | |
1345 | 'register_date' => '2007-07-21 00:00:00', | |
1346 | 'source' => 'Online Event Registration: API Testing', | |
1347 | 'version' => $this->_apiversion, | |
1348 | ); | |
1349 | $participant = civicrm_api('participant', 'create', $participantParams); | |
1350 | $this->assertAPISuccess($participant, "participant created in line " . __LINE__); | |
1351 | $this->assertAPISuccess(civicrm_api('participant_payment', 'create', array( | |
1352 | 'version' => 3, | |
1353 | 'participant_id' => $participant['id'], | |
1354 | 'contribution_id' => $contribution['id'], | |
1355 | )), " in line " . __LINE__); | |
1356 | $apiResult = civicrm_api('contribution', 'sendconfirmation', array( | |
1357 | 'version' => $this->_apiversion, | |
1358 | 'id' => $contribution['id'], | |
1359 | 'receipt_from_email' => 'api@civicrm.org', | |
1360 | ) | |
1361 | ); | |
1362 | ||
1363 | $this->assertAPISuccess($apiResult); | |
1364 | $mut->checkMailLog(array( | |
1365 | 'Annual CiviCRM meet', | |
1366 | 'Event', | |
1367 | 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>', | |
1368 | ), array( | |
1369 | ||
1370 | ) | |
1371 | ); | |
1372 | $mut->stop(); | |
1373 | } | |
1374 | ||
1375 | /////////////// _civicrm_contribute_format_params for $create | |
1376 | function testFormatParams() { | |
1377 | require_once 'CRM/Contribute/DAO/Contribution.php'; | |
1378 | $params = array( | |
1379 | 'contact_id' => $this->_individualId, | |
1380 | 'receive_date' => date('Ymd'), | |
1381 | 'total_amount' => 100.00, | |
1382 | 'financial_type_id' => $this->_contributionTypeId, | |
1383 | 'contribution_status_id' => 1, | |
1384 | 'financial_type' => null, | |
1385 | 'note' => 'note', | |
1386 | 'contribution_source' => 'test', | |
1387 | ); | |
1388 | ||
1389 | $values = array(); | |
1390 | $result = _civicrm_api3_contribute_format_params($params, $values, TRUE); | |
1391 | $this->assertEquals($values['total_amount'], 100.00, 'In line ' . __LINE__); | |
1392 | $this->assertEquals($values['contribution_status_id'], 1, 'In line ' . __LINE__); | |
1393 | } | |
1394 | /* | |
1395 | * This function does a GET & compares the result against the $params | |
1396 | * Use as a double check on Creates | |
1397 | */ | |
1398 | function contributionGetnCheck($params, $id, $delete = 1) { | |
1399 | ||
1400 | $contribution = civicrm_api('Contribution', 'Get', array( | |
1401 | 'id' => $id, | |
1402 | 'version' => $this->_apiversion, | |
1403 | )); | |
1404 | ||
1405 | if ($delete) { | |
1406 | civicrm_api('contribution', 'delete', array( | |
1407 | 'id' => $id, | |
1408 | 'version' => $this->_apiversion, | |
1409 | )); | |
1410 | } | |
1411 | $this->assertEquals($contribution['is_error'], 0, 'In line ' . __LINE__); | |
1412 | $values = $contribution['values'][$contribution['id']]; | |
1413 | $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date'])); | |
1414 | // this is not returned in id format | |
1415 | unset($params['payment_instrument_id']); | |
1416 | $params['contribution_source'] = $params['source']; | |
1417 | unset($params['source']); | |
1418 | foreach ($params as $key => $value) { | |
1419 | if ($key == 'version') { | |
1420 | continue; | |
1421 | } | |
1422 | $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__); | |
1423 | } | |
1424 | } | |
1425 | ||
1426 | function _getFinancialTrxnAmount($contId) { | |
1427 | $query = "SELECT | |
1428 | SUM( ft.total_amount ) AS total | |
1429 | FROM civicrm_financial_trxn AS ft | |
1430 | LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id | |
1431 | WHERE ceft.entity_table = 'civicrm_contribution' | |
1432 | AND ceft.entity_id = {$contId}"; | |
1433 | ||
1434 | $result = CRM_Core_DAO::singleValueQuery($query); | |
1435 | return $result; | |
1436 | } | |
1437 | ||
1438 | function _getFinancialItemAmount($contId) { | |
1439 | $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); | |
1440 | $query = "SELECT | |
1441 | SUM(amount) | |
1442 | FROM civicrm_financial_item | |
1443 | WHERE entity_table = 'civicrm_line_item' | |
1444 | AND entity_id = {$lineItem}"; | |
1445 | $result = CRM_Core_DAO::singleValueQuery($query); | |
1446 | return $result; | |
1447 | } | |
1448 | ||
1449 | function _checkFinancialItem($contId, $context) { | |
1450 | if ($context != 'paylater') { | |
1451 | $params = array ( | |
1452 | 'entity_id' => $contId, | |
1453 | 'entity_table' => 'civicrm_contribution', | |
1454 | ); | |
1455 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE)); | |
1456 | $entityParams = array( | |
1457 | 'financial_trxn_id' => $trxn['financial_trxn_id'], | |
1458 | 'entity_table' => 'civicrm_financial_item', | |
1459 | ); | |
1460 | $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
1461 | $params = array( | |
1462 | 'id' => $entityTrxn['entity_id'], | |
1463 | ); | |
1464 | } | |
1465 | if ($context == 'paylater') { | |
1466 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'); | |
1467 | foreach ($lineItems as $key=>$item) { | |
1468 | $params = array( | |
1469 | 'entity_id' => $key, | |
1470 | 'entity_table' => 'civicrm_line_item', | |
1471 | ); | |
1472 | $compareParams = array ('status_id' => 1); | |
1473 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams); | |
1474 | } | |
1475 | } | |
1476 | elseif ($context == 'refund') { | |
1477 | $compareParams = array( | |
1478 | 'status_id' => 1, | |
1479 | 'financial_account_id' => 1, | |
1480 | 'amount' => -100, | |
1481 | ); | |
1482 | } | |
1483 | elseif ($context == 'cancelPending') { | |
1484 | $compareParams = array( | |
1485 | 'status_id' => 3, | |
1486 | 'financial_account_id' => 1, | |
1487 | 'amount' => -100, | |
1488 | ); | |
1489 | } | |
1490 | elseif ($context == 'changeFinancial') { | |
1491 | $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); | |
1492 | $params = array( | |
1493 | 'entity_id' => $lineKey, | |
1494 | 'amount' => -100, | |
1495 | ); | |
1496 | $compareParams = array( | |
1497 | 'financial_account_id' => 1, | |
1498 | ); | |
1499 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams); | |
1500 | $params = array( | |
1501 | 'financial_account_id' => 3, | |
1502 | 'entity_id' => $lineKey, | |
1503 | ); | |
1504 | $compareParams = array( | |
1505 | 'amount' => 100, | |
1506 | ); | |
1507 | } | |
1508 | if ($context != 'paylater') { | |
1509 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams); | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | function _checkFinancialTrxn($contribution, $context) { | |
1514 | $trxnParams = array( | |
1515 | 'entity_id' => $contribution['id'], | |
1516 | 'entity_table' => 'civicrm_contribution', | |
1517 | ); | |
1518 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE)); | |
1519 | $params = array( | |
1520 | 'id' => $trxn['financial_trxn_id'], | |
1521 | ); | |
1522 | if ($context == 'payLater') { | |
1523 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); | |
1524 | $compareParams = array( | |
1525 | 'status_id' => 1, | |
1526 | 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId), | |
1527 | ); | |
1528 | } | |
1529 | elseif ($context == 'paymentInstrument') { | |
1530 | $compareParams = array( | |
1531 | 'from_financial_account_id' => 6, | |
1532 | 'to_financial_account_id' => 7, | |
1533 | ); | |
1534 | } | |
1535 | elseif ($context == 'refund') { | |
1536 | $compareParams = array( | |
1537 | 'to_financial_account_id' => 6, | |
1538 | 'total_amount' => -100, | |
1539 | 'status_id' => 7, | |
1540 | ); | |
1541 | } | |
1542 | elseif ($context == 'cancelPending') { | |
1543 | $compareParams = array( | |
1544 | 'from_financial_account_id' => 7, | |
1545 | 'total_amount' => -100, | |
1546 | 'status_id' => 3, | |
1547 | ); | |
1548 | } | |
1549 | elseif ($context == 'changeFinancial') { | |
1550 | $entityParams = array( | |
1551 | 'entity_id' => $contribution['id'], | |
1552 | 'entity_table' => 'civicrm_contribution', | |
1553 | 'amount' => -100, | |
1554 | ); | |
1555 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
1556 | $trxnParams1 = array( | |
1557 | 'id' => $trxn['financial_trxn_id'], | |
1558 | ); | |
1559 | $compareParams = array( | |
1560 | 'to_financial_account_id' => 12, | |
1561 | 'total_amount' => -100, | |
1562 | 'status_id' => 1, | |
1563 | ); | |
1564 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams); | |
1565 | $compareParams = array( | |
1566 | 'to_financial_account_id' => 12, | |
1567 | 'total_amount' => 100, | |
1568 | 'status_id' => 1, | |
1569 | ); | |
1570 | } | |
1571 | ||
1572 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams); | |
1573 | } | |
1574 | ||
1575 | function _addPaymentInstrument () { | |
1576 | $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name'); | |
1577 | $optionParams = array( | |
1578 | 'option_group_id' => $gId, | |
1579 | 'label' => 'Test Card', | |
1580 | 'name' => 'Test Card', | |
1581 | 'value' => '6', | |
1582 | 'weight' => '6', | |
1583 | 'is_active' => 1, | |
1584 | 'version' => 3, | |
1585 | ); | |
1586 | $optionValue = civicrm_api('option_value', 'create', $optionParams); | |
1587 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); | |
1588 | $financialParams = array( | |
1589 | 'entity_table' => 'civicrm_option_value', | |
1590 | 'entity_id' => $optionValue['id'], | |
1591 | 'account_relationship' => $relationTypeId, | |
1592 | 'financial_account_id' => 7, | |
1593 | ); | |
1594 | $financialType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray); | |
1595 | $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']); | |
1596 | return $optionValue['values'][$optionValue['id']]['value']; | |
1597 | } | |
1598 | ||
1599 | function _checkFinancialRecords($params,$context) { | |
1600 | $entityParams = array( | |
1601 | 'entity_id' => $params['id'], | |
1602 | 'entity_table' => 'civicrm_contribution', | |
1603 | ); | |
1604 | if ($context == 'pending') { | |
1605 | $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams); | |
1606 | $this->assertNull($trxn, 'No Trxn to be created until IPN callback'); | |
1607 | return; | |
1608 | } | |
1609 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
1610 | $trxnParams = array( | |
1611 | 'id' => $trxn['financial_trxn_id'], | |
1612 | ); | |
1613 | if ($context != 'online' && $context != 'payLater') { | |
1614 | $compareParams = array( | |
1615 | 'to_financial_account_id' => 6, | |
1616 | 'total_amount' => 100, | |
1617 | 'status_id' => 1, | |
1618 | ); | |
1619 | } | |
1620 | if ($context == 'feeAmount') { | |
1621 | $compareParams['fee_amount'] = 50; | |
1622 | } | |
1623 | elseif ($context == 'online') { | |
1624 | $compareParams = array( | |
1625 | 'to_financial_account_id' => 12, | |
1626 | 'total_amount' => 100, | |
1627 | 'status_id' => 1, | |
1628 | ); | |
1629 | } | |
1630 | elseif ($context == 'payLater') { | |
1631 | $compareParams = array( | |
1632 | 'to_financial_account_id' => 7, | |
1633 | 'total_amount' => 100, | |
1634 | 'status_id' => 2, | |
1635 | ); | |
1636 | } | |
1637 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams); | |
1638 | $entityParams = array( | |
1639 | 'financial_trxn_id' => $trxn['financial_trxn_id'], | |
1640 | 'entity_table' => 'civicrm_financial_item', | |
1641 | ); | |
1642 | $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
1643 | $fitemParams = array( | |
1644 | 'id' => $entityTrxn['entity_id'], | |
1645 | ); | |
1646 | $compareParams = array( | |
1647 | 'amount' => 100, | |
1648 | 'status_id' => 1, | |
1649 | 'financial_account_id' => 1, | |
1650 | ); | |
1651 | if ($context == 'payLater') { | |
1652 | $compareParams = array( | |
1653 | 'amount' => 100, | |
1654 | 'status_id' => 3, | |
1655 | 'financial_account_id' => 1, | |
1656 | ); | |
1657 | } | |
1658 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams); | |
1659 | if ($context == 'feeAmount') { | |
1660 | $maxParams = array( | |
1661 | 'entity_id' => $params['id'], | |
1662 | 'entity_table' => 'civicrm_contribution', | |
1663 | ); | |
1664 | $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE)); | |
1665 | $trxnParams = array( | |
1666 | 'id' => $maxTrxn['financial_trxn_id'], | |
1667 | ); | |
1668 | $compareParams = array( | |
1669 | 'to_financial_account_id' => 5, | |
1670 | 'from_financial_account_id' => 6, | |
1671 | 'total_amount' => 50, | |
1672 | 'status_id' => 1, | |
1673 | ); | |
1674 | $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC'); | |
1675 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams); | |
1676 | $fitemParams = array( | |
1677 | 'entity_id' => $trxnId['financialTrxnId'], | |
1678 | 'entity_table' => 'civicrm_financial_trxn', | |
1679 | ); | |
1680 | $compareParams = array( | |
1681 | 'amount' => 50, | |
1682 | 'status_id' => 1, | |
1683 | 'financial_account_id' => 5, | |
1684 | ); | |
1685 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams); | |
1686 | } | |
1687 | } | |
1688 | } | |
1689 |