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