3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CiviTest/CiviMailUtils.php';
33 * Test APIv3 civicrm_contribute_* functions
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
39 class api_v3_ContributionTest
extends CiviUnitTestCase
{
42 * Assume empty database with just civicrm_data
44 protected $_individualId;
45 protected $_contribution;
46 protected $_financialTypeId = 1;
47 protected $_apiversion;
48 protected $_entity = 'Contribution';
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,
65 'net_amount' => 95.00,
67 'contribution_status_id' => 1,
69 $this->_processorParams
= array(
72 'payment_processor_type_id' => 10,
73 'financial_account_id' => 12,
76 'url_site' => 'http://dummy.com',
77 'url_recur' => 'http://dummy.com',
80 $this->_pageParams
= array(
81 'title' => 'Test Contribution Page',
82 'financial_type_id' => 1,
84 'financial_account_id' => 1,
85 'payment_processor' => $paymentProcessor->id
,
87 'is_allow_other_amount' => 1,
94 $this->quickCleanup(array(
95 'civicrm_contribution',
96 'civicrm_contribution_soft',
98 'civicrm_contribution_page',
99 'civicrm_participant',
100 'civicrm_participant_payment',
102 'civicrm_financial_trxn',
103 'civicrm_financial_item',
104 'civicrm_entity_financial_trxn',
109 function testGetContribution() {
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,
119 'invoice_id' => 78910,
121 'contribution_status_id' => 1,
123 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
126 'contribution_id' => $this->_contribution
['id'],
128 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
129 $financialParams['id'] = $this->_financialTypeId
;
131 $financialType = CRM_Financial_BAO_FinancialType
::retrieve($financialParams,$default);
133 $this->assertEquals(1,$contribution['count']);
134 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
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);
139 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
140 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
141 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
142 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
143 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
144 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
145 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
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';
150 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
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,
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,
166 $this->assertEquals($contribution2['id'], $contribution);
168 $contribution = $this->callAPISuccess('contribution', 'get', array(
169 'id' => $this->_contribution
['id'],
171 $this->assertEquals(1, $contribution['count']);
173 //test get by contact id works
174 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
176 $this->assertEquals(2, $contribution['count']);
177 $this->callAPISuccess('Contribution', 'Delete', array(
178 'id' => $this->_contribution
['id'],
180 $this->callAPISuccess('Contribution', 'Delete', array(
181 'id' => $contribution2['id'],
186 * We need to ensure previous tested behaviour still works as part of the api contract
188 function testGetContributionLegacyBehaviour() {
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,
198 'invoice_id' => 78910,
200 'contribution_status_id' => 1,
202 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
205 'contribution_id' => $this->_contribution
['id'],
207 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
208 $financialParams['id'] = $this->_financialTypeId
;
210 $financialType = CRM_Financial_BAO_FinancialType
::retrieve($financialParams,$default);
212 $this->assertEquals(1,$contribution['count']);
213 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
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);
217 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
218 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
219 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
220 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
221 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
222 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
223 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
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';
228 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
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,
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,
244 $this->assertEquals($contribution2['id'], $contribution);
245 $contribution = $this->callAPISuccess('contribution', 'get', array(
246 'id' => $this->_contribution
['id'],
249 $this->assertEquals(1, $contribution['count']);
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
));
254 $this->assertEquals(2, $contribution['count']);
255 $this->callAPISuccess('Contribution', 'Delete', array(
256 'id' => $this->_contribution
['id'],
258 $this->callAPISuccess('Contribution', 'Delete', array(
259 'id' => $contribution2['id'],
262 ///////////////// civicrm_contribution_
263 function testCreateEmptyContributionIDUseDonation() {
265 'contribution_id' => FALSE,
268 'check_permissions' => false,
269 'financial_type_id' => 'Donation',
271 $contribution = $this->callAPISuccess('contribution', 'create', $params);
274 * ensure we continue to support contribution_type_id as part of the api commitment to
276 *///////////////// civicrm_contribution_
278 function testCreateLegacyBehaviour() {
280 'contribution_id' => FALSE,
283 'check_permissions' => false,
284 'contribution_type_id' => 3,
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']);
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
297 function testCreateWithCustom() {
298 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
300 $params = $this->_params
;
301 $params['custom_' . $ids['custom_field_id']] = "custom string";
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'],
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__
);
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
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']);
332 function testCreateContributionNoLineItems() {
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,
344 'invoice_id' => 67890,
346 'contribution_status_id' => 1,
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',
356 $this->assertEquals(0, $lineItems['count']);
359 * Test checks that passing in line items suppresses the create mechanism
361 function testCreateContributionChainedLineItems() {
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,
373 'invoice_id' => 67890,
375 'contribution_status_id' => 1,
377 'api.line_item.create' => array(
379 'price_field_id' => 1,
381 'line_total' => '20',
382 'unit_price' => '10',
385 'price_field_id' => 1,
387 'line_total' => '80',
388 'unit_price' => '80',
393 $description = "Create Contribution with Nested Line Items";
394 $subfile = "CreateWithNestedLineItems";
395 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
,__FILE__
, $description, $subfile);
397 $lineItems = $this->callAPISuccess('line_item','get',array(
398 'entity_id' => $contribution['id'],
399 'contribution_id' => $contribution['id'],
400 'entity_table' => 'civicrm_contribution',
403 $this->assertEquals(2, $lineItems['count']);
406 function testCreateContributionOffline() {
408 'contact_id' => $this->_individualId
,
409 'receive_date' => '20120511',
410 'total_amount' => 100.00,
411 'financial_type_id' => 1,
413 'invoice_id' => 67890,
415 'contribution_status_id' => 1,
418 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
419 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
420 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
421 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
422 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
423 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
424 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
425 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
426 $lineItems = $this->callAPISuccess('line_item','get',array(
427 'entity_id' => $contribution['id'],
428 'contribution_id' => $contribution['id'],
429 'entity_table' => 'civicrm_contribution',
432 $this->assertEquals(1, $lineItems['count']);
433 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
434 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
435 $this->_checkFinancialRecords($contribution, 'offline');
436 $this->contributionGetnCheck($params, $contribution['id']);
439 * test create with valid payment instument
441 function testCreateContributionWithPaymentInstrument() {
442 $params = $this->_params +
array('payment_instrument' => 'EFT');
443 $contribution = $this->callAPISuccess('contribution', 'create', $params);
444 $contribution = $this->callAPISuccess('contribution','get', array(
446 'id' => $contribution['id']
448 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
449 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
451 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
452 $contribution = $this->callAPISuccess('contribution','get', array(
454 'id' => $contribution['id']
456 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
457 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
460 function testGetContributionByPaymentInstrument() {
461 $params = $this->_params +
array('payment_instrument' => 'EFT');
462 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
463 $this->callAPISuccess('contribution','create',$params);
464 $this->callAPISuccess('contribution','create',$params2);
465 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash'));
466 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
467 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
468 $this->assertEquals(1,$contribution['count']);
469 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
470 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
471 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
472 $this->assertEquals(1, $contribution['count']);
473 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument_id' => 5));
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' => 'EFT'));
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', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
482 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
483 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
484 $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']);
485 $this->assertEquals(1,$contribution['count']);
489 * Create test with unique field name on source
491 function testCreateContributionSource() {
494 'contact_id' => $this->_individualId
,
495 'receive_date' => date('Ymd'),
496 'total_amount' => 100.00,
497 'financial_type_id' => $this->_financialTypeId
,
498 'payment_instrument_id' => 1,
499 'non_deductible_amount' => 10.00,
500 'fee_amount' => 50.00,
501 'net_amount' => 90.00,
503 'invoice_id' => 67890,
504 'contribution_source' => 'SSF',
505 'contribution_status_id' => 1,
508 $contribution = $this->callAPISuccess('contribution', 'create', $params);
509 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
510 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
514 * Create test with unique field name on source
516 function testCreateDefaultNow() {
518 $params = $this->_params
;
519 unset($params['receive_date']);
521 $contribution = $this->callAPISuccess('contribution', 'create', $params);
522 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
523 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
527 * Create test with unique field name on source
529 function testCreateContributionSourceInvalidContac() {
533 'receive_date' => date('Ymd'),
534 'total_amount' => 100.00,
535 'financial_type_id' => $this->_financialTypeId
,
536 'payment_instrument_id' => 1,
537 'non_deductible_amount' => 10.00,
538 'fee_amount' => 50.00,
539 'net_amount' => 90.00,
541 'invoice_id' => 67890,
542 'contribution_source' => 'SSF',
543 'contribution_status_id' => 1,
546 $contribution = $this->callAPIFailure('contribution', 'create', $params,
547 'contact_id is not valid : 999');
550 function testCreateContributionSourceInvalidContContac() {
553 'contribution_contact_id' => 999,
554 'receive_date' => date('Ymd'),
555 'total_amount' => 100.00,
556 'financial_type_id' => $this->_financialTypeId
,
557 'payment_instrument_id' => 1,
558 'non_deductible_amount' => 10.00,
559 'fee_amount' => 50.00,
560 'net_amount' => 90.00,
562 'invoice_id' => 67890,
563 'contribution_source' => 'SSF',
564 'contribution_status_id' => 1,
567 $contribution = $this->callAPIFailure('contribution', 'create', $params,
568 'contact_id is not valid : 999');
571 function testCreateContributionWithNote() {
572 $description = "Demonstrates creating contribution with Note Entity";
573 $subfile = "ContributionCreateWithNote";
575 'contact_id' => $this->_individualId
,
576 'receive_date' => '2012-01-01',
577 'total_amount' => 100.00,
578 'financial_type_id' => $this->_financialTypeId
,
579 'payment_instrument_id' => 1,
580 'non_deductible_amount' => 10.00,
581 'fee_amount' => 50.00,
582 'net_amount' => 90.00,
584 'invoice_id' => 67890,
586 'contribution_status_id' => 1,
587 'note' => 'my contribution note',
590 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
591 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
592 $this->assertEquals('my contribution note', $result['values'][0]['note']);
593 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
596 function testCreateContributionWithNoteUniqueNameAliases() {
598 'contact_id' => $this->_individualId
,
599 'receive_date' => '2012-01-01',
600 'total_amount' => 100.00,
601 'financial_type_id' => $this->_financialTypeId
,
602 'payment_instrument_id' => 1,
603 'non_deductible_amount' => 10.00,
604 'fee_amount' => 50.00,
605 'net_amount' => 90.00,
607 'invoice_id' => 67890,
609 'contribution_status_id' => 1,
610 'contribution_note' => 'my contribution note',
613 $contribution = $this->callAPISuccess('contribution', 'create', $params);
614 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
615 $this->assertEquals('my contribution note', $result['values'][0]['note']);
616 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
619 * This is the test for creating soft credits - however a 'get' is not yet possible via API
620 * as the current BAO functions are contact-centric (from what I can find)
623 function testCreateContributionWithSoftCredt() {
624 $description = "Demonstrates creating contribution with SoftCredit";
625 $subfile = "ContributionCreateWithSoftCredit";
626 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
628 'contact_id' => $contact2['id'],
630 'soft_credit_type_id' => 3
633 $params = $this->_params +
array('soft_credit' => array(1 => $softparams));
634 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
635 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
637 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
638 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
639 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
641 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
642 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
645 function testCreateContributionWithSoftCreditDefaults() {
646 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
647 $subfile = "ContributionCreateWithSoftCreditDefaults";
648 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
649 $params = $this->_params +
array(
650 'soft_credit_to' => $contact2['id'],
652 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
653 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
655 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
656 // Default soft credit amount = contribution.total_amount
657 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
658 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
660 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
661 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
664 function testCreateContributionWithHonoreeContact() {
665 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
666 $subfile = "ContributionCreateWithHonoreeContact";
667 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
668 $params = $this->_params +
array(
669 'honor_contact_id' => $contact2['id'],
671 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
672 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
674 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
675 // Default soft credit amount = contribution.total_amount
676 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
677 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
678 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
680 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
681 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
685 * Test using example code
687 function testContributionCreateExample() {
688 //make sure at least on page exists since there is a truncate in tear down
689 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
690 $this->assertAPISuccess($page);
691 require_once 'api/v3/examples/Contribution/Create.php';
692 $result = contribution_create_example();
693 $this->assertAPISuccess($result);
694 $contributionId = $result['id'];
695 $expectedResult = contribution_create_expectedresult();
696 $this->checkArrayEquals($expectedResult, $result);
697 $this->contributionDelete($contributionId);
701 * Function tests that additional financial records are created when fee amount is recorded
703 function testCreateContributionWithFee() {
705 'contact_id' => $this->_individualId
,
706 'receive_date' => '20120511',
707 'total_amount' => 100.00,
709 'financial_type_id' => 1,
711 'invoice_id' => 67890,
713 'contribution_status_id' => 1,
716 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
717 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
718 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
719 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
720 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
721 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
722 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
723 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
724 $lineItems = $this->callAPISuccess('line_item','get',array(
726 'entity_id' => $contribution['id'],
727 'entity_table' => 'civicrm_contribution',
730 $this->assertEquals(1, $lineItems['count']);
731 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
732 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
733 $lineItems = $this->callAPISuccess('line_item','get',array(
735 'entity_id' => $contribution['id'],
736 'contribution_id' => $contribution['id'],
737 'entity_table' => 'civicrm_contribution',
740 $this->assertEquals(1, $lineItems['count']);
741 $this->_checkFinancialRecords($contribution, 'feeAmount');
746 * Function tests that additional financial records are created when online contribution is created
748 function testCreateContributionOnline() {
749 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
750 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams
);
751 $this->assertAPISuccess($contributionPage);
753 'contact_id' => $this->_individualId
,
754 'receive_date' => '20120511',
755 'total_amount' => 100.00,
756 'financial_type_id' => 1,
757 'contribution_page_id' => $contributionPage['id'],
758 'payment_processor' => 1,
760 'invoice_id' => 67890,
762 'contribution_status_id' => 1,
766 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
767 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
768 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
769 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
770 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
771 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
772 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
773 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
774 $this->_checkFinancialRecords($contribution, 'online');
778 * in the interests of removing financial type / contribution type checks from
779 * legacy format function lets test that the api is doing this for us
781 function testCreateInvalidFinancialType() {
782 $params = $this->_params
;
783 $params['financial_type_id'] = 99999;
784 $result = $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
788 * in the interests of removing financial type / contribution type checks from
789 * legacy format function lets test that the api is doing this for us
791 function testValidNamedFinancialType() {
792 $params = $this->_params
;
793 $params['financial_type_id'] = 'Donation';
794 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
798 * Function tests that additional financial records are created when online contribution with pay later option
801 function testCreateContributionPayLaterOnline() {
802 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
803 $this->_pageParams
['is_pay_later'] = 1;
804 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams
);
805 $this->assertAPISuccess($contributionPage);
807 'contact_id' => $this->_individualId
,
808 'receive_date' => '20120511',
809 'total_amount' => 100.00,
810 'financial_type_id' => 1,
811 'contribution_page_id' => $contributionPage['id'],
814 'invoice_id' => 67890,
816 'contribution_status_id' => 2,
820 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
821 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
822 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
823 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
824 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
825 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
826 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
827 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
828 $this->_checkFinancialRecords($contribution, 'payLater');
832 * Function tests that additional financial records are created when online contribution with pending option
835 function testCreateContributionPendingOnline() {
836 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
837 $contributionPage = $this->callAPISuccess( 'contribution_page', 'create', $this->_pageParams
);
838 $this->assertAPISuccess($contributionPage);
840 'contact_id' => $this->_individualId
,
841 'receive_date' => '20120511',
842 'total_amount' => 100.00,
843 'financial_type_id' => 1,
844 'contribution_page_id' => $contributionPage['id'],
846 'invoice_id' => 67890,
848 'contribution_status_id' => 2,
851 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
852 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
853 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
854 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
855 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
856 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
857 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
858 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
859 $this->_checkFinancialRecords($contribution, 'pending');
863 * Function tests that line items, financial records are updated when contribution amount is changed
865 function testCreateUpdateContributionChangeTotal() {
866 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
867 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
869 'entity_id' => $contribution['id'],
870 'entity_table' => 'civicrm_contribution',
872 'return' => 'line_total',
874 $this->assertEquals('100.00', $lineItems);
875 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
876 // Financial trxn SUM = 100 + 5 (fee)
877 $this->assertEquals('105.00', $trxnAmount);
880 'id' => $contribution['id'],
881 'total_amount' => '125');
882 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
884 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
886 'entity_id' => $contribution['id'],
887 'entity_table' => 'civicrm_contribution',
889 'return' => 'line_total',
892 $this->assertEquals('125.00', $lineItems);
893 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
894 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
895 // Financial trxn SUM = 125 + 5 (fee)
896 $this->assertEquals('130.00', $trxnAmount);
897 $this->assertEquals('125.00', $fitemAmount);
901 * Function tests that line items, financial records are updated when pay later contribution is received
903 function testCreateUpdateContributionPayLater() {
904 $contribParams = array(
905 'contact_id' => $this->_individualId
,
906 'receive_date' => '2012-01-01',
907 'total_amount' => 100.00,
908 'financial_type_id' => $this->_financialTypeId
,
909 'payment_instrument_id' => 1,
910 'contribution_status_id' => 2,
914 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
916 $newParams = array_merge($contribParams, array(
917 'id' => $contribution['id'],
918 'contribution_status_id' => 1,)
920 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
921 $contribution = $contribution['values'][$contribution['id']];
922 $this->assertEquals($contribution['contribution_status_id'],'1');
923 $this->_checkFinancialItem($contribution['id'], 'paylater');
924 $this->_checkFinancialTrxn($contribution, 'payLater');
928 * Function tests that financial records are updated when Payment Instrument is changed
930 function testCreateUpdateContributionPaymentInstrument() {
931 $instrumentId = $this->_addPaymentInstrument();
932 $contribParams = array(
933 'contact_id' => $this->_individualId
,
934 'total_amount' => 100.00,
935 'financial_type_id' => $this->_financialTypeId
,
936 'payment_instrument_id' => 4,
937 'contribution_status_id' => 1,
940 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
942 $newParams = array_merge($contribParams, array(
943 'id' => $contribution['id'],
944 'payment_instrument_id' => $instrumentId,)
946 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
947 $this->assertAPISuccess($contribution);
948 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
952 * Function tests that financial records are added when Contribution is Refunded
954 function testCreateUpdateContributionRefund() {
955 $contribParams = array(
956 'contact_id' => $this->_individualId
,
957 'receive_date' => '2012-01-01',
958 'total_amount' => 100.00,
959 'financial_type_id' => $this->_financialTypeId
,
960 'payment_instrument_id' => 4,
961 'contribution_status_id' => 1,
964 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
965 $newParams = array_merge($contribParams, array(
966 'id' => $contribution['id'],
967 'contribution_status_id' => 7,
971 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
972 $this->_checkFinancialTrxn($contribution, 'refund');
973 $this->_checkFinancialItem($contribution['id'], 'refund');
977 * Function tests invalid contribution status change
979 function testCreateUpdateContributionInValidStatusChange() {
980 $contribParams = array(
982 'receive_date' => '2012-01-01',
983 'total_amount' => 100.00,
984 'financial_type_id' => 1,
985 'payment_instrument_id' => 1,
986 'contribution_status_id' => 1,
988 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
989 $newParams = array_merge($contribParams, array(
990 'id' => $contribution['id'],
991 'contribution_status_id' => 2,
994 $contribution = $this->callAPIFailure('contribution', 'create', $newParams,
995 ts('Cannot change contribution status from Completed to Pending.')
1001 * Function tests that financial records are added when Pending Contribution is Canceled
1003 function testCreateUpdateContributionCancelPending() {
1004 $contribParams = array(
1005 'contact_id' => $this->_individualId
,
1006 'receive_date' => '2012-01-01',
1007 'total_amount' => 100.00,
1008 'financial_type_id' => $this->_financialTypeId
,
1009 'payment_instrument_id' => 1,
1010 'contribution_status_id' => 2,
1011 'is_pay_later' => 1,
1014 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1015 $newParams = array_merge($contribParams, array(
1016 'id' => $contribution['id'],
1017 'contribution_status_id' => 3,
1020 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1021 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1022 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1026 * Function tests that financial records are added when Financial Type is Changed
1028 function testCreateUpdateContributionChangeFinancialType() {
1029 $contribParams = array(
1030 'contact_id' => $this->_individualId
,
1031 'receive_date' => '2012-01-01',
1032 'total_amount' => 100.00,
1033 'financial_type_id' => 1,
1034 'payment_instrument_id' => 1,
1035 'contribution_status_id' => 1,
1038 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1039 $newParams = array_merge($contribParams, array(
1040 'id' => $contribution['id'],
1041 'financial_type_id' => 3,
1044 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1045 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1046 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1050 * test that update does not change status id CRM-15105
1052 function testCreateUpdateWithoutChangingPendingStatus() {
1053 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1054 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1055 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1056 $this->assertEquals(2, $contribution['contribution_status_id']);
1058 //To Update Contribution
1059 //CHANGE: we require the API to do an incremental update
1060 function testCreateUpdateContribution() {
1062 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1063 $old_params = array(
1064 'contribution_id' => $contributionID,
1067 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1068 //Make sure it came back
1069 $this->assertAPISuccess($original);
1070 $this->assertEquals($original['id'], $contributionID);
1071 //set up list of old params, verify
1073 //This should not be required on update:
1074 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1075 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1076 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1077 $old_source = $original['values'][$contributionID]['contribution_source'];
1079 //note: current behavior is to return ISO. Is this
1080 //documented behavior? Is this correct
1081 $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date']));
1083 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1084 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1086 //check against values in CiviUnitTestCase::createContribution()
1087 $this->assertEquals($old_contact_id, $this->_individualId
);
1088 $this->assertEquals($old_fee_amount, 5.00);
1089 $this->assertEquals($old_source, 'SSF');
1090 $this->assertEquals($old_trxn_id, 212355);
1091 $this->assertEquals($old_invoice_id, 'idofsh');
1093 'id' => $contributionID,
1094 'contact_id' => $this->_individualId
,
1095 'total_amount' => 110.00,
1096 'financial_type_id' => $this->_financialTypeId
,
1097 'non_deductible_amount' => 10.00,
1098 'net_amount' => 100.00,
1099 'contribution_status_id' => 1,
1100 'note' => 'Donating for Nobel Cause',
1104 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1106 $new_params = array(
1107 'contribution_id' => $contribution['id'],
1110 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1112 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1113 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1114 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
);
1115 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1116 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1117 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1118 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1119 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1120 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1121 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1122 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1124 'contribution_id' => $contributionID,
1127 $result = $this->callAPISuccess('contribution', 'delete', $params);
1128 $this->assertAPISuccess($result, 'in line' . __LINE__
);
1131 ///////////////// civicrm_contribution_delete methods
1132 function testDeleteEmptyParamsContribution() {
1134 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1137 function testDeleteParamsNotArrayContribution() {
1138 $params = 'contribution_id= 1';
1139 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1140 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1143 function testDeleteWrongParamContribution() {
1145 'contribution_source' => 'SSF',
1148 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1151 function testDeleteContribution() {
1153 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1155 'id' => $contributionID,
1157 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1161 * Test civicrm_contribution_search with empty params.
1162 * All available contributions expected.
1164 function testSearchEmptyParams() {
1168 'contact_id' => $this->_individualId
,
1169 'receive_date' => date('Ymd'),
1170 'total_amount' => 100.00,
1171 'financial_type_id' => $this->_financialTypeId
,
1172 'non_deductible_amount' => 10.00,
1173 'fee_amount' => 5.00,
1174 'net_amount' => 95.00,
1176 'invoice_id' => 78910,
1178 'contribution_status_id' => 1,
1181 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1183 $result = $this->callAPISuccess('contribution', 'get', $params);
1184 // We're taking the first element.
1185 $res = $result['values'][$contribution['id']];
1187 $this->assertEquals($p['contact_id'], $res['contact_id']);
1188 $this->assertEquals($p['total_amount'], $res['total_amount']);
1189 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'] );
1190 $this->assertEquals($p['net_amount'], $res['net_amount']);
1191 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1192 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1193 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1194 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1195 $this->assertEquals($p['source'], $res['contribution_source']);
1196 // contribution_status_id = 1 => Completed
1197 $this->assertEquals('Completed', $res['contribution_status']);
1199 $this->contributionDelete($contribution['id']);
1203 * Test civicrm_contribution_search. Success expected.
1205 function testSearch() {
1207 'contact_id' => $this->_individualId
,
1208 'receive_date' => date('Ymd'),
1209 'total_amount' => 100.00,
1210 'financial_type_id' => $this->_financialTypeId
,
1211 'non_deductible_amount' => 10.00,
1212 'contribution_status_id' => 1,
1215 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1218 'contact_id' => $this->_individualId
,
1219 'receive_date' => date('Ymd'),
1220 'total_amount' => 200.00,
1221 'financial_type_id' => $this->_financialTypeId
,
1222 'non_deductible_amount' => 20.00,
1223 'trxn_id' => 5454565,
1224 'invoice_id' => 1212124,
1225 'fee_amount' => 50.00,
1226 'net_amount' => 60.00,
1227 'contribution_status_id' => 2,
1230 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1233 'contribution_id' => $contribution2['id'],
1236 $result = $this->callAPISuccess('contribution', 'get', $params);
1237 $res = $result['values'][$contribution2['id']];
1239 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1240 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1241 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'] );
1242 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1243 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1244 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1245 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1246 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1247 // contribution_status_id = 2 => Pending
1248 $this->assertEquals('Pending', $res['contribution_status']);
1250 $this->contributionDelete($contribution1['id']);
1251 $this->contributionDelete($contribution2['id']);
1255 * Test completing a transaction via the API
1257 * Note that we are creating a logged in user because email goes out from
1260 function testCompleteTransaction() {
1261 $mut = new CiviMailUtils( $this, true );
1262 $this->createLoggedInUser();
1263 $params = array_merge($this->_params
, array('contribution_status_id' => 2,));
1264 $contribution = $this->callAPISuccess('contribution','create', $params);
1265 $this->callAPISuccess('contribution', 'completetransaction', array(
1266 'id' => $contribution['id'],
1268 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1269 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1270 $mut->checkMailLog(array(
1271 'Receipt - Contribution',
1272 'Please print this confirmation for your records.',
1279 * Test completing a transaction via the API
1283 function testCompleteTransactionWithReceiptDateSet() {
1284 $mut = new CiviMailUtils( $this, true );
1285 $this->createLoggedInUser();
1286 $params = array_merge($this->_params
, array('contribution_status_id' => 2,'receipt_date' => 'now'));
1287 $contribution = $this->callAPISuccess('contribution','create', $params);
1288 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1289 'id' => $contribution['id'],
1292 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1293 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1294 $mut->checkMailLog(array(
1295 'Receipt - Contribution',
1296 'Please print this confirmation for your records.',
1302 * Test completing a transaction with an event via the API
1304 * Note that we are creating a logged in user because email goes out from
1307 function testCompleteTransactionWithParticipantRecord() {
1308 $mut = new CiviMailUtils( $this, true );
1309 $mut->clearMessages();
1310 $this->createLoggedInUser();
1311 $contributionID = $this->createPendingParticipantContribution();
1312 $this->callAPISuccess('contribution', 'completetransaction', array(
1313 'id' => $contributionID,)
1315 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids
['participant'], 'return' => 'participant_status_id'));
1316 $this->assertEquals(1, $participantStatus);
1317 $mut->checkMailLog(array(
1318 'Annual CiviCRM meet',
1320 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1326 * test membership is renewed when transaction completed
1328 function testCompleteTransactionMembershipPriceSet() {
1329 $this->createPriceSetWithPage('membership');
1330 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1331 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1332 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1333 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1334 $this->cleanUpAfterPriceSets();
1338 * test membership is renewed when transaction completed
1340 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1341 $this->createPriceSetWithPage('membership');
1342 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1343 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1344 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1345 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1346 $this->cleanUpAfterPriceSets();
1349 function cleanUpAfterPriceSets() {
1350 $this->quickCleanUpFinancialEntities();
1351 $this->contactDelete($this->_ids
['contact']);
1352 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1357 * this could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1358 * on parent class at some point (fn is not in 4.4)
1360 * @param array $params
1362 function createPriceSetWithPage($entity, $params = array()) {
1363 $membershipTypeID = $this->membershipTypeCreate();
1364 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1365 'title' => "Test Contribution Page",
1366 'financial_type_id' => 1,
1367 'currency' => 'NZD',
1368 'goal_amount' => 50,
1369 'is_pay_later' => 1,
1370 'is_monetary' => TRUE,
1371 'is_email_receipt' => FALSE,
1373 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1374 'is_quick_config' => 0,
1375 'extends' => 'CiviMember',
1376 'financial_type_id' => 1,
1377 'title' => 'my Page'
1379 $priceSetID = $priceSet['id'];
1381 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1382 $priceField = $this->callAPISuccess('price_field', 'create', array(
1383 'price_set_id' => $priceSetID ,
1384 'label' => 'Goat Breed',
1385 'html_type' => 'Radio',
1387 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1388 'price_set_id' => $priceSetID ,
1389 'price_field_id' => $priceField['id'],
1390 'label' => 'Long Haired Goat',
1392 'membership_type_id' => $membershipTypeID,
1393 'membership_num_terms' => 1,
1396 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1397 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1398 'price_set_id' => $priceSetID ,
1399 'price_field_id' => $priceField['id'],
1400 'label' => 'Shoe-eating Goat',
1402 'membership_type_id' => $membershipTypeID,
1403 'membership_num_terms' => 2,
1406 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1407 $this->_ids
['price_set'] = $priceSetID;
1408 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1409 $this->_ids
['price_field'] = array($priceField['id']);
1411 $this->_ids
['membership_type'] = $membershipTypeID;
1415 * Set up a pending transaction with a specific price field id
1416 * @param $priceFieldValueID
1418 function setUpPendingContribution($priceFieldValueID){
1419 $contactID = $this->individualCreate();
1420 $membership = $this->callAPISuccess('membership', 'create', array(
1421 'contact_id' => $contactID,
1422 'membership_type_id' => $this->_ids
['membership_type'],
1423 'start_date' => 'yesterday - 1 year',
1424 'end_date' => 'yesterday',
1426 $contribution = $this->callAPISuccess('contribution', 'create', array(
1428 'contact_id' => $contactID,
1429 'receive_date' => date('Ymd'),
1430 'total_amount' => 100.00,
1431 'financial_type_id' => 1,
1432 'payment_instrument_id' => 'Credit Card',
1433 'non_deductible_amount' => 10.00,
1434 'trxn_id' => 'jdhfi88',
1435 'invoice_id' => 'djfhiewuyr',
1437 'contribution_status_id' => 2,
1438 'contribution_page_id' => $this->_ids
['contribution_page'],
1439 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1442 $this->callAPISuccess('line_item', 'create', array(
1443 'entity_id' => $contribution['id'],
1444 'entity_table' => 'civicrm_contribution',
1445 'contribution_id' => $contribution['id'],
1446 'price_field_id' => $this->_ids
['price_field'][0],
1450 'financial_type_id' => 1,
1451 'price_field_value_id' => $priceFieldValueID,
1453 $this->_ids
['contact'] = $contactID;
1454 $this->_ids
['contribution'] = $contribution['id'];
1455 $this->_ids
['membership'] = $membership['id'];
1459 * Test sending a mail via the API
1461 function testSendMail() {
1462 $mut = new CiviMailUtils( $this, true );
1463 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1464 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
1466 'id' => $contribution['id'],
1467 'receipt_from_email' => 'api@civicrm.org',
1470 $mut->checkMailLog(array(
1472 'Contribution Information',
1473 'Please print this confirmation for your records',
1482 * Test sending a mail via the API
1484 function testSendMailEvent() {
1485 $mut = new CiviMailUtils( $this, true );
1486 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1487 $event = $this->eventCreate(array(
1488 'is_email_confirm' => 1,
1489 'confirm_from_email' => 'test@civicrm.org',
1491 $this->_eventID
= $event['id'];
1492 $participantParams = array(
1493 'contact_id' => $this->_individualId
,
1494 'event_id' => $this->_eventID
,
1497 // to ensure it matches later on
1498 'register_date' => '2007-07-21 00:00:00',
1499 'source' => 'Online Event Registration: API Testing',
1502 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1503 $this->callAPISuccess('participant_payment', 'create', array(
1504 'participant_id' => $participant['id'],
1505 'contribution_id' => $contribution['id'],
1507 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1508 'id' => $contribution['id'],
1509 'receipt_from_email' => 'api@civicrm.org',
1513 $mut->checkMailLog(array(
1514 'Annual CiviCRM meet',
1516 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1525 * This function does a GET & compares the result against the $params
1526 * Use as a double check on Creates
1528 function contributionGetnCheck($params, $id, $delete = 1) {
1530 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1536 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1538 $this->assertAPISuccess($contribution, 0);
1539 $values = $contribution['values'][$contribution['id']];
1540 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1541 // this is not returned in id format
1542 unset($params['payment_instrument_id']);
1543 $params['contribution_source'] = $params['source'];
1544 unset($params['source']);
1545 foreach ($params as $key => $value) {
1546 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
1551 * Create a pending contribution & linked pending participant record
1552 * (along with an event)
1554 function createPendingParticipantContribution(){
1555 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1556 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1557 $this->ids
['participant'] = $participantID;
1558 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1559 $contribution = $this->callAPISuccess('contribution','create', $params);
1560 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1561 $lineItem = $this->callAPISuccess('line_item', 'get', array(
1562 'entity_id' => $contribution['id'],
1563 'entity_table' => 'civicrm_contribution',
1564 'api.line_item.create' => array(
1565 'entity_id' => $participantID,
1566 'entity_table' => 'civicrm_participant',
1569 return $contribution['id'];
1575 * @return null|string
1576 */function _getFinancialTrxnAmount($contId) {
1578 SUM( ft.total_amount ) AS total
1579 FROM civicrm_financial_trxn AS ft
1580 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1581 WHERE ceft.entity_table = 'civicrm_contribution'
1582 AND ceft.entity_id = {$contId}";
1584 $result = CRM_Core_DAO
::singleValueQuery($query);
1591 * @return null|string
1592 */function _getFinancialItemAmount($contId) {
1593 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1596 FROM civicrm_financial_item
1597 WHERE entity_table = 'civicrm_line_item'
1598 AND entity_id = {$lineItem}";
1599 $result = CRM_Core_DAO
::singleValueQuery($query);
1607 function _checkFinancialItem($contId, $context) {
1608 if ($context != 'paylater') {
1610 'entity_id' => $contId,
1611 'entity_table' => 'civicrm_contribution',
1613 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1614 $entityParams = array(
1615 'financial_trxn_id' => $trxn['financial_trxn_id'],
1616 'entity_table' => 'civicrm_financial_item',
1618 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1620 'id' => $entityTrxn['entity_id'],
1623 if ($context == 'paylater') {
1624 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1625 foreach ($lineItems as $key=>$item) {
1627 'entity_id' => $key,
1628 'entity_table' => 'civicrm_line_item',
1630 $compareParams = array ('status_id' => 1);
1631 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1634 elseif ($context == 'refund') {
1635 $compareParams = array(
1637 'financial_account_id' => 1,
1641 elseif ($context == 'cancelPending') {
1642 $compareParams = array(
1644 'financial_account_id' => 1,
1648 elseif ($context == 'changeFinancial') {
1649 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1651 'entity_id' => $lineKey,
1654 $compareParams = array(
1655 'financial_account_id' => 1,
1657 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1659 'financial_account_id' => 3,
1660 'entity_id' => $lineKey,
1662 $compareParams = array(
1666 if ($context != 'paylater') {
1667 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1672 * @param $contribution
1674 * @param null $instrumentId
1676 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1677 $trxnParams = array(
1678 'entity_id' => $contribution['id'],
1679 'entity_table' => 'civicrm_contribution',
1681 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1683 'id' => $trxn['financial_trxn_id'],
1685 if ($context == 'payLater') {
1686 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1687 $compareParams = array(
1689 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1692 elseif ($context == 'refund') {
1693 $compareParams = array(
1694 'to_financial_account_id' => 6,
1695 'total_amount' => -100,
1699 elseif ($context == 'cancelPending') {
1700 $compareParams = array(
1701 'from_financial_account_id' => 7,
1702 'total_amount' => -100,
1706 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1707 $entityParams = array(
1708 'entity_id' => $contribution['id'],
1709 'entity_table' => 'civicrm_contribution',
1712 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1713 $trxnParams1 = array(
1714 'id' => $trxn['financial_trxn_id'],
1716 $compareParams = array(
1717 'total_amount' => -100,
1720 if ($context == 'paymentInstrument') {
1721 $compareParams +
= array(
1722 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1723 'payment_instrument_id' => 4,
1727 $compareParams['to_financial_account_id'] = 12;
1729 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1730 $compareParams['total_amount'] = 100;
1731 if ($context == 'paymentInstrument') {
1732 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1733 $compareParams['payment_instrument_id'] = $instrumentId;
1736 $compareParams['to_financial_account_id'] = 12;
1740 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1746 function _addPaymentInstrument () {
1747 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1748 $optionParams = array(
1749 'option_group_id' => $gId,
1750 'label' => 'Test Card',
1751 'name' => 'Test Card',
1756 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1757 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1758 $financialParams = array(
1759 'entity_table' => 'civicrm_option_value',
1760 'entity_id' => $optionValue['id'],
1761 'account_relationship' => $relationTypeId,
1762 'financial_account_id' => 7,
1764 $financialType = CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1765 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1766 return $optionValue['values'][$optionValue['id']]['value'];
1773 function _checkFinancialRecords($params,$context) {
1774 $entityParams = array(
1775 'entity_id' => $params['id'],
1776 'entity_table' => 'civicrm_contribution',
1778 if ($context == 'pending') {
1779 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1780 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1783 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1784 $trxnParams = array(
1785 'id' => $trxn['financial_trxn_id'],
1787 if ($context != 'online' && $context != 'payLater') {
1788 $compareParams = array(
1789 'to_financial_account_id' => 6,
1790 'total_amount' => 100,
1794 if ($context == 'feeAmount') {
1795 $compareParams['fee_amount'] = 50;
1797 elseif ($context == 'online') {
1798 $compareParams = array(
1799 'to_financial_account_id' => 12,
1800 'total_amount' => 100,
1804 elseif ($context == 'payLater') {
1805 $compareParams = array(
1806 'to_financial_account_id' => 7,
1807 'total_amount' => 100,
1811 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams);
1812 $entityParams = array(
1813 'financial_trxn_id' => $trxn['financial_trxn_id'],
1814 'entity_table' => 'civicrm_financial_item',
1816 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1817 $fitemParams = array(
1818 'id' => $entityTrxn['entity_id'],
1820 $compareParams = array(
1823 'financial_account_id' => 1,
1825 if ($context == 'payLater') {
1826 $compareParams = array(
1829 'financial_account_id' => 1,
1832 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1833 if ($context == 'feeAmount') {
1835 'entity_id' => $params['id'],
1836 'entity_table' => 'civicrm_contribution',
1838 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1839 $trxnParams = array(
1840 'id' => $maxTrxn['financial_trxn_id'],
1842 $compareParams = array(
1843 'to_financial_account_id' => 5,
1844 'from_financial_account_id' => 6,
1845 'total_amount' => 50,
1848 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1849 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1850 $fitemParams = array(
1851 'entity_id' => $trxnId['financialTrxnId'],
1852 'entity_table' => 'civicrm_financial_trxn',
1854 $compareParams = array(
1857 'financial_account_id' => 5,
1859 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);