3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
38 class api_v3_ContributionTest
extends CiviUnitTestCase
{
41 * Assume empty database with just civicrm_data
43 protected $_individualId;
44 protected $_contribution;
45 protected $_financialTypeId = 1;
46 protected $_apiversion;
47 protected $_entity = 'Contribution';
50 protected $_ids = array();
51 protected $_pageParams = array();
56 public function setUp() {
59 $this->_apiversion
= 3;
60 $this->_individualId
= $this->individualCreate();
61 $paymentProcessor = $this->processorCreate();
62 $this->_params
= array(
63 'contact_id' => $this->_individualId
,
64 'receive_date' => '20120511',
65 'total_amount' => 100.00,
66 'financial_type_id' => $this->_financialTypeId
,
67 'non_deductible_amount' => 10.00,
69 'net_amount' => 95.00,
71 'contribution_status_id' => 1,
73 $this->_processorParams
= array(
76 'payment_processor_type_id' => 10,
77 'financial_account_id' => 12,
80 'url_site' => 'http://dummy.com',
81 'url_recur' => 'http://dummy.com',
84 $this->_pageParams
= array(
85 'title' => 'Test Contribution Page',
86 'financial_type_id' => 1,
88 'financial_account_id' => 1,
89 'payment_processor' => $paymentProcessor->id
,
91 'is_allow_other_amount' => 1,
98 * Clean up after each test.
100 public function tearDown() {
101 $this->quickCleanUpFinancialEntities();
107 public function testGetContribution() {
109 'contact_id' => $this->_individualId
,
110 'receive_date' => '2010-01-20',
111 'total_amount' => 100.00,
112 'financial_type_id' => $this->_financialTypeId
,
113 'non_deductible_amount' => 10.00,
114 'fee_amount' => 5.00,
115 'net_amount' => 95.00,
117 'invoice_id' => 78910,
119 'contribution_status_id' => 1,
121 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
124 'contribution_id' => $this->_contribution
['id'],
127 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
128 $financialParams['id'] = $this->_financialTypeId
;
130 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
132 $this->assertEquals(1, $contribution['count']);
133 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
134 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
135 // Passing back a string rather than an id seems like an error/cruft.
136 // 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));
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 public 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 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');
225 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
226 $p['trxn_id'] = '3847';
227 $p['invoice_id'] = '3847';
229 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
231 // now we have 2 - test getcount
232 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
233 $this->assertEquals(2, $contribution);
234 //test id only format
235 $contribution = $this->callAPISuccess('contribution', 'get', array(
236 'id' => $this->_contribution
['id'],
237 'format.only_id' => 1,
239 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
240 //test id only format
241 $contribution = $this->callAPISuccess('contribution', 'get', array(
242 'id' => $contribution2['id'],
243 'format.only_id' => 1,
245 $this->assertEquals($contribution2['id'], $contribution);
246 $contribution = $this->callAPISuccess('contribution', 'get', array(
247 'id' => $this->_contribution
['id'],
250 $this->assertEquals(1, $contribution['count']);
251 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
252 //test get by contact id works
253 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
255 $this->assertEquals(2, $contribution['count']);
256 $this->callAPISuccess('Contribution', 'Delete', array(
257 'id' => $this->_contribution
['id'],
259 $this->callAPISuccess('Contribution', 'Delete', array(
260 'id' => $contribution2['id'],
264 ///////////////// civicrm_contribution_
265 public function testCreateEmptyContributionIDUseDonation() {
267 'contribution_id' => FALSE,
270 'check_permissions' => FALSE,
271 'financial_type_id' => 'Donation',
273 $this->callAPISuccess('contribution', 'create', $params);
276 * ensure we continue to support contribution_type_id as part of the api commitment to
278 *///////////////// civicrm_contribution_
280 public function testCreateLegacyBehaviour() {
282 'contribution_id' => FALSE,
285 'check_permissions' => FALSE,
286 'contribution_type_id' => 3,
288 $contribution = $this->callAPISuccess('contribution', 'create', $params);
289 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
290 $this->assertEquals(3, $contribution['financial_type_id']);
294 * Check with complete array + custom field
295 * Note that the test is written on purpose without any
296 * variables specific to participant so it can be replicated into other entities
297 * and / or moved to the automated test suite
299 public function testCreateWithCustom() {
300 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
302 $params = $this->_params
;
303 $params['custom_' . $ids['custom_field_id']] = "custom string";
305 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
306 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
307 $check = $this->callAPISuccess($this->_entity
, 'get', array(
308 'return.custom_' . $ids['custom_field_id'] => 1,
309 'id' => $result['id'],
311 $this->customFieldDelete($ids['custom_field_id']);
312 $this->customGroupDelete($ids['custom_group_id']);
313 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
317 * Check with complete array + custom field
318 * Note that the test is written on purpose without any
319 * variables specific to participant so it can be replicated into other entities
320 * and / or moved to the automated test suite
322 public function testCreateGetFieldsWithCustom() {
323 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
324 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
325 $result = $this->callAPISuccess('Contribution', 'getfields', array());
326 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
327 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
328 $this->customFieldDelete($ids['custom_field_id']);
329 $this->customGroupDelete($ids['custom_group_id']);
330 $this->customFieldDelete($idsContact['custom_field_id']);
331 $this->customGroupDelete($idsContact['custom_group_id']);
334 public function testCreateContributionNoLineItems() {
337 'contact_id' => $this->_individualId
,
338 'receive_date' => '20120511',
339 'total_amount' => 100.00,
340 'financial_type_id' => $this->_financialTypeId
,
341 'payment_instrument_id' => 1,
342 'non_deductible_amount' => 10.00,
343 'fee_amount' => 50.00,
344 'net_amount' => 90.00,
346 'invoice_id' => 67890,
348 'contribution_status_id' => 1,
352 $contribution = $this->callAPISuccess('contribution', 'create', $params);
353 $lineItems = $this->callAPISuccess('line_item', 'get', array(
354 'entity_id' => $contribution['id'],
355 'entity_table' => 'civicrm_contribution',
358 $this->assertEquals(0, $lineItems['count']);
362 * Test checks that passing in line items suppresses the create mechanism
364 public function testCreateContributionChainedLineItems() {
367 'contact_id' => $this->_individualId
,
368 'receive_date' => '20120511',
369 'total_amount' => 100.00,
370 'financial_type_id' => $this->_financialTypeId
,
371 'payment_instrument_id' => 1,
372 'non_deductible_amount' => 10.00,
373 'fee_amount' => 50.00,
374 'net_amount' => 90.00,
376 'invoice_id' => 67890,
378 'contribution_status_id' => 1,
380 'api.line_item.create' => array(
382 'price_field_id' => 1,
384 'line_total' => '20',
385 'unit_price' => '10',
388 'price_field_id' => 1,
390 'line_total' => '80',
391 'unit_price' => '80',
396 $description = "Create Contribution with Nested Line Items";
397 $subfile = "CreateWithNestedLineItems";
398 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
400 $lineItems = $this->callAPISuccess('line_item', 'get', array(
401 'entity_id' => $contribution['id'],
402 'contribution_id' => $contribution['id'],
403 'entity_table' => 'civicrm_contribution',
406 $this->assertEquals(2, $lineItems['count']);
409 public function testCreateContributionOffline() {
411 'contact_id' => $this->_individualId
,
412 'receive_date' => '20120511',
413 'total_amount' => 100.00,
414 'financial_type_id' => 1,
416 'invoice_id' => 67890,
418 'contribution_status_id' => 1,
421 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
422 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
423 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
424 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
425 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
426 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
427 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
428 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
429 $lineItems = $this->callAPISuccess('line_item', 'get', array(
430 'entity_id' => $contribution['id'],
431 'contribution_id' => $contribution['id'],
432 'entity_table' => 'civicrm_contribution',
435 $this->assertEquals(1, $lineItems['count']);
436 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
437 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
438 $this->_checkFinancialRecords($contribution, 'offline');
439 $this->contributionGetnCheck($params, $contribution['id']);
443 * Test create with valid payment instument
445 public function testCreateContributionWithPaymentInstrument() {
446 $params = $this->_params +
array('payment_instrument' => 'EFT');
447 $contribution = $this->callAPISuccess('contribution', 'create', $params);
448 $contribution = $this->callAPISuccess('contribution', 'get', array(
450 'id' => $contribution['id'],
452 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
453 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
455 $this->callAPISuccess('contribution', 'create', array(
456 'id' => $contribution['id'],
457 'payment_instrument' => 'Credit Card',
459 $contribution = $this->callAPISuccess('contribution', 'get', array(
461 'id' => $contribution['id'],
463 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
464 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
467 public function testGetContributionByPaymentInstrument() {
468 $params = $this->_params +
array('payment_instrument' => 'EFT');
469 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
470 $this->callAPISuccess('contribution', 'create', $params);
471 $this->callAPISuccess('contribution', 'create', $params2);
472 $contribution = $this->callAPISuccess('contribution', 'get', array(
474 'contribution_payment_instrument' => 'Cash',
476 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
477 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
478 $this->assertEquals(1,$contribution['count']);
479 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument' => 'Cash'));
480 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
481 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
482 $this->assertEquals(1, $contribution['count']);
483 $contribution = $this->callAPISuccess('contribution', 'get', array(
485 'payment_instrument_id' => 5,
487 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
488 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
489 $this->assertEquals(1, $contribution['count']);
490 $contribution = $this->callAPISuccess('contribution', 'get', array(
492 'payment_instrument' => 'EFT',
494 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
495 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
496 $this->assertEquals(1, $contribution['count']);
497 $contribution = $this->callAPISuccess('contribution', 'create', array(
498 'id' => $contribution['id'],
499 'payment_instrument' => 'Credit Card',
501 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
502 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
503 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
504 $this->assertEquals(1, $contribution['count']);
508 * Create test with unique field name on source
510 public function testCreateContributionSource() {
513 'contact_id' => $this->_individualId
,
514 'receive_date' => date('Ymd'),
515 'total_amount' => 100.00,
516 'financial_type_id' => $this->_financialTypeId
,
517 'payment_instrument_id' => 1,
518 'non_deductible_amount' => 10.00,
519 'fee_amount' => 50.00,
520 'net_amount' => 90.00,
522 'invoice_id' => 67890,
523 'contribution_source' => 'SSF',
524 'contribution_status_id' => 1,
527 $contribution = $this->callAPISuccess('contribution', 'create', $params);
528 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
529 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
533 * Create test with unique field name on source
535 public function testCreateDefaultNow() {
537 $params = $this->_params
;
538 unset($params['receive_date']);
540 $contribution = $this->callAPISuccess('contribution', 'create', $params);
541 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
542 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
546 * Create test with unique field name on source.
548 public function testCreateContributionSourceInvalidContact() {
552 'receive_date' => date('Ymd'),
553 'total_amount' => 100.00,
554 'financial_type_id' => $this->_financialTypeId
,
555 'payment_instrument_id' => 1,
556 'non_deductible_amount' => 10.00,
557 'fee_amount' => 50.00,
558 'net_amount' => 90.00,
560 'invoice_id' => 67890,
561 'contribution_source' => 'SSF',
562 'contribution_status_id' => 1,
565 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
568 public function testCreateContributionSourceInvalidContContact() {
571 'contribution_contact_id' => 999,
572 'receive_date' => date('Ymd'),
573 'total_amount' => 100.00,
574 'financial_type_id' => $this->_financialTypeId
,
575 'payment_instrument_id' => 1,
576 'non_deductible_amount' => 10.00,
577 'fee_amount' => 50.00,
578 'net_amount' => 90.00,
580 'invoice_id' => 67890,
581 'contribution_source' => 'SSF',
582 'contribution_status_id' => 1,
585 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
589 * Test note created correctly
591 public function testCreateContributionWithNote() {
592 $description = "Demonstrates creating contribution with Note Entity";
593 $subfile = "ContributionCreateWithNote";
595 'contact_id' => $this->_individualId
,
596 'receive_date' => '2012-01-01',
597 'total_amount' => 100.00,
598 'financial_type_id' => $this->_financialTypeId
,
599 'payment_instrument_id' => 1,
600 'non_deductible_amount' => 10.00,
601 'fee_amount' => 50.00,
602 'net_amount' => 90.00,
604 'invoice_id' => 67890,
606 'contribution_status_id' => 1,
607 'note' => 'my contribution note',
610 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
611 $result = $this->callAPISuccess('note', 'get', array(
612 'entity_table' => 'civicrm_contribution',
613 'entity_id' => $contribution['id'],
616 $this->assertEquals('my contribution note', $result['values'][0]['note']);
617 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
620 public function testCreateContributionWithNoteUniqueNameAliases() {
622 'contact_id' => $this->_individualId
,
623 'receive_date' => '2012-01-01',
624 'total_amount' => 100.00,
625 'financial_type_id' => $this->_financialTypeId
,
626 'payment_instrument_id' => 1,
627 'non_deductible_amount' => 10.00,
628 'fee_amount' => 50.00,
629 'net_amount' => 90.00,
631 'invoice_id' => 67890,
633 'contribution_status_id' => 1,
634 'contribution_note' => 'my contribution note',
637 $contribution = $this->callAPISuccess('contribution', 'create', $params);
638 $result = $this->callAPISuccess('note', 'get', array(
639 'entity_table' => 'civicrm_contribution',
640 'entity_id' => $contribution['id'],
643 $this->assertEquals('my contribution note', $result['values'][0]['note']);
644 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
648 * This is the test for creating soft credits.
650 public function testCreateContributionWithSoftCredit() {
651 $description = "Demonstrates creating contribution with SoftCredit";
652 $subfile = "ContributionCreateWithSoftCredit";
653 $contact2 = $this->callAPISuccess('Contact', 'create', array(
654 'display_name' => 'superman',
655 'contact_type' => 'Individual',
658 'contact_id' => $contact2['id'],
660 'soft_credit_type_id' => 3,
663 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
664 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
665 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
667 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
668 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
669 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
671 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
672 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
675 public function testCreateContributionWithSoftCreditDefaults() {
676 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
677 $subfile = "ContributionCreateWithSoftCreditDefaults";
678 $contact2 = $this->callAPISuccess('Contact', 'create', array(
679 'display_name' => 'superman',
680 'contact_type' => 'Individual',
682 $params = $this->_params +
array(
683 'soft_credit_to' => $contact2['id'],
685 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
686 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
688 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
689 // Default soft credit amount = contribution.total_amount
690 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
691 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
693 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
694 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
697 public function testCreateContributionWithHonoreeContact() {
698 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
699 $subfile = "ContributionCreateWithHonoreeContact";
700 $contact2 = $this->callAPISuccess('Contact', 'create', array(
701 'display_name' => 'superman',
702 'contact_type' => 'Individual',
704 $params = $this->_params +
array(
705 'honor_contact_id' => $contact2['id'],
707 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
708 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
710 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
711 // Default soft credit amount = contribution.total_amount
712 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
713 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
714 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
716 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
717 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
721 * Test using example code
723 public function testContributionCreateExample() {
724 //make sure at least on page exists since there is a truncate in tear down
725 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
726 $this->assertAPISuccess($page);
727 require_once 'api/v3/examples/Contribution/Create.php';
728 $result = contribution_create_example();
729 $this->assertAPISuccess($result);
730 $contributionId = $result['id'];
731 $expectedResult = contribution_create_expectedresult();
732 $this->checkArrayEquals($expectedResult, $result);
733 $this->contributionDelete($contributionId);
737 * Function tests that additional financial records are created when fee amount is recorded
739 public function testCreateContributionWithFee() {
741 'contact_id' => $this->_individualId
,
742 'receive_date' => '20120511',
743 'total_amount' => 100.00,
745 'financial_type_id' => 1,
747 'invoice_id' => 67890,
749 'contribution_status_id' => 1,
752 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
753 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
754 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
755 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
756 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
757 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
758 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
759 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
760 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
761 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
763 $lineItems = $this->callAPISuccess('line_item', 'get', array(
765 'entity_id' => $contribution['id'],
766 'entity_table' => 'civicrm_contribution',
769 $this->assertEquals(1, $lineItems['count']);
770 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
771 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
772 $lineItems = $this->callAPISuccess('line_item', 'get', array(
774 'entity_id' => $contribution['id'],
775 'contribution_id' => $contribution['id'],
776 'entity_table' => 'civicrm_contribution',
779 $this->assertEquals(1, $lineItems['count']);
780 $this->_checkFinancialRecords($contribution, 'feeAmount');
785 * Function tests that additional financial records are created when online contribution is created
787 public function testCreateContributionOnline() {
788 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
789 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
790 $this->assertAPISuccess($contributionPage);
792 'contact_id' => $this->_individualId
,
793 'receive_date' => '20120511',
794 'total_amount' => 100.00,
795 'financial_type_id' => 1,
796 'contribution_page_id' => $contributionPage['id'],
797 'payment_processor' => 1,
799 'invoice_id' => 67890,
801 'contribution_status_id' => 1,
805 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
806 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
807 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
808 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
809 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
810 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
811 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
812 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
813 $this->_checkFinancialRecords($contribution, 'online');
817 * In the interests of removing financial type / contribution type checks from
818 * legacy format function lets test that the api is doing this for us
820 public function testCreateInvalidFinancialType() {
821 $params = $this->_params
;
822 $params['financial_type_id'] = 99999;
823 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
827 * In the interests of removing financial type / contribution type checks from
828 * legacy format function lets test that the api is doing this for us
830 public function testValidNamedFinancialType() {
831 $params = $this->_params
;
832 $params['financial_type_id'] = 'Donation';
833 $this->callAPISuccess($this->_entity
, 'create', $params);
837 * Function tests that additional financial records are created when online contribution with pay later option
840 public function testCreateContributionPayLaterOnline() {
841 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
842 $this->_pageParams
['is_pay_later'] = 1;
843 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
844 $this->assertAPISuccess($contributionPage);
846 'contact_id' => $this->_individualId
,
847 'receive_date' => '20120511',
848 'total_amount' => 100.00,
849 'financial_type_id' => 1,
850 'contribution_page_id' => $contributionPage['id'],
853 'invoice_id' => 67890,
855 'contribution_status_id' => 2,
859 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
860 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
861 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
862 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
863 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
864 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
865 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
866 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
867 $this->_checkFinancialRecords($contribution, 'payLater');
871 * Function tests that additional financial records are created when online contribution with pending option
874 public function testCreateContributionPendingOnline() {
875 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
876 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
877 $this->assertAPISuccess($contributionPage);
879 'contact_id' => $this->_individualId
,
880 'receive_date' => '20120511',
881 'total_amount' => 100.00,
882 'financial_type_id' => 1,
883 'contribution_page_id' => $contributionPage['id'],
885 'invoice_id' => 67890,
887 'contribution_status_id' => 2,
890 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
891 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
892 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
893 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
894 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
895 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
896 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
897 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
898 $this->_checkFinancialRecords($contribution, 'pending');
902 * Test that BAO defaults work
904 public function testCreateBAODefaults() {
905 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
906 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
907 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
908 'id' => $contribution['id'],
909 'api.contribution.delete' => 1,
911 $this->assertEquals(1, $contribution['contribution_status_id']);
912 $this->assertEquals('Check', $contribution['payment_instrument']);
916 * Function tests that line items, financial records are updated when contribution amount is changed
918 public function testCreateUpdateContributionChangeTotal() {
919 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
920 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
922 'entity_id' => $contribution['id'],
923 'entity_table' => 'civicrm_contribution',
925 'return' => 'line_total',
927 $this->assertEquals('100.00', $lineItems);
928 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
929 // Financial trxn SUM = 100 + 5 (fee)
930 $this->assertEquals('105.00', $trxnAmount);
933 'id' => $contribution['id'],
934 'total_amount' => '125',
936 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
938 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
940 'entity_id' => $contribution['id'],
941 'entity_table' => 'civicrm_contribution',
943 'return' => 'line_total',
946 $this->assertEquals('125.00', $lineItems);
947 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
948 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
949 // Financial trxn SUM = 125 + 5 (fee)
950 $this->assertEquals('130.00', $trxnAmount);
951 $this->assertEquals('125.00', $fitemAmount);
955 * Function tests that line items, financial records are updated when pay later contribution is received
957 public function testCreateUpdateContributionPayLater() {
958 $contribParams = array(
959 'contact_id' => $this->_individualId
,
960 'receive_date' => '2012-01-01',
961 'total_amount' => 100.00,
962 'financial_type_id' => $this->_financialTypeId
,
963 'payment_instrument_id' => 1,
964 'contribution_status_id' => 2,
968 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
970 $newParams = array_merge($contribParams, array(
971 'id' => $contribution['id'],
972 'contribution_status_id' => 1,
975 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
976 $contribution = $contribution['values'][$contribution['id']];
977 $this->assertEquals($contribution['contribution_status_id'], '1');
978 $this->_checkFinancialItem($contribution['id'], 'paylater');
979 $this->_checkFinancialTrxn($contribution, 'payLater');
983 * Function tests that financial records are updated when Payment Instrument is changed
985 public function testCreateUpdateContributionPaymentInstrument() {
986 $instrumentId = $this->_addPaymentInstrument();
987 $contribParams = array(
988 'contact_id' => $this->_individualId
,
989 'total_amount' => 100.00,
990 'financial_type_id' => $this->_financialTypeId
,
991 'payment_instrument_id' => 4,
992 'contribution_status_id' => 1,
995 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
997 $newParams = array_merge($contribParams, array(
998 'id' => $contribution['id'],
999 'payment_instrument_id' => $instrumentId,
1002 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1003 $this->assertAPISuccess($contribution);
1004 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1008 * Function tests that financial records are added when Contribution is Refunded
1010 public function testCreateUpdateContributionRefund() {
1011 $contribParams = array(
1012 'contact_id' => $this->_individualId
,
1013 'receive_date' => '2012-01-01',
1014 'total_amount' => 100.00,
1015 'financial_type_id' => $this->_financialTypeId
,
1016 'payment_instrument_id' => 4,
1017 'contribution_status_id' => 1,
1020 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1021 $newParams = array_merge($contribParams, array(
1022 'id' => $contribution['id'],
1023 'contribution_status_id' => 7,
1027 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1028 $this->_checkFinancialTrxn($contribution, 'refund');
1029 $this->_checkFinancialItem($contribution['id'], 'refund');
1033 * Function tests invalid contribution status change
1035 public function testCreateUpdateContributionInValidStatusChange() {
1036 $contribParams = array(
1038 'receive_date' => '2012-01-01',
1039 'total_amount' => 100.00,
1040 'financial_type_id' => 1,
1041 'payment_instrument_id' => 1,
1042 'contribution_status_id' => 1,
1044 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1045 $newParams = array_merge($contribParams, array(
1046 'id' => $contribution['id'],
1047 'contribution_status_id' => 2,
1050 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1055 * Function tests that financial records are added when Pending Contribution is Canceled
1057 public function testCreateUpdateContributionCancelPending() {
1058 $contribParams = array(
1059 'contact_id' => $this->_individualId
,
1060 'receive_date' => '2012-01-01',
1061 'total_amount' => 100.00,
1062 'financial_type_id' => $this->_financialTypeId
,
1063 'payment_instrument_id' => 1,
1064 'contribution_status_id' => 2,
1065 'is_pay_later' => 1,
1068 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1069 $newParams = array_merge($contribParams, array(
1070 'id' => $contribution['id'],
1071 'contribution_status_id' => 3,
1074 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1075 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1076 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1080 * Function tests that financial records are added when Financial Type is Changed
1082 public function testCreateUpdateContributionChangeFinancialType() {
1083 $contribParams = array(
1084 'contact_id' => $this->_individualId
,
1085 'receive_date' => '2012-01-01',
1086 'total_amount' => 100.00,
1087 'financial_type_id' => 1,
1088 'payment_instrument_id' => 1,
1089 'contribution_status_id' => 1,
1092 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1093 $newParams = array_merge($contribParams, array(
1094 'id' => $contribution['id'],
1095 'financial_type_id' => 3,
1098 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1099 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1100 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1104 * Test that update does not change status id CRM-15105
1106 public function testCreateUpdateWithoutChangingPendingStatus() {
1107 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1108 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1109 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1110 'id' => $contribution['id'],
1111 'api.contribution.delete' => 1,
1113 $this->assertEquals(2, $contribution['contribution_status_id']);
1115 //To Update Contribution
1116 //CHANGE: we require the API to do an incremental update
1117 public function testCreateUpdateContribution() {
1119 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1120 $old_params = array(
1121 'contribution_id' => $contributionID,
1124 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1125 //Make sure it came back
1126 $this->assertAPISuccess($original);
1127 $this->assertEquals($original['id'], $contributionID);
1128 //set up list of old params, verify
1130 //This should not be required on update:
1131 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1132 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1133 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1134 $old_source = $original['values'][$contributionID]['contribution_source'];
1136 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1137 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1139 //check against values in CiviUnitTestCase::createContribution()
1140 $this->assertEquals($old_contact_id, $this->_individualId
);
1141 $this->assertEquals($old_fee_amount, 5.00);
1142 $this->assertEquals($old_source, 'SSF');
1143 $this->assertEquals($old_trxn_id, 212355);
1144 $this->assertEquals($old_invoice_id, 'idofsh');
1146 'id' => $contributionID,
1147 'contact_id' => $this->_individualId
,
1148 'total_amount' => 110.00,
1149 'financial_type_id' => $this->_financialTypeId
,
1150 'non_deductible_amount' => 10.00,
1151 'net_amount' => 100.00,
1152 'contribution_status_id' => 1,
1153 'note' => 'Donating for Nobel Cause',
1157 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1159 $new_params = array(
1160 'contribution_id' => $contribution['id'],
1163 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1165 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1166 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1167 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
);
1168 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1169 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1170 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1171 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1172 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1173 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1174 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1175 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1177 'contribution_id' => $contributionID,
1180 $result = $this->callAPISuccess('contribution', 'delete', $params);
1181 $this->assertAPISuccess($result);
1184 ///////////////// civicrm_contribution_delete methods
1185 public function testDeleteEmptyParamsContribution() {
1187 $this->callAPIFailure('contribution', 'delete', $params);
1190 public function testDeleteParamsNotArrayContribution() {
1191 $params = 'contribution_id= 1';
1192 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1193 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1196 public function testDeleteWrongParamContribution() {
1198 'contribution_source' => 'SSF',
1201 $this->callAPIFailure('contribution', 'delete', $params);
1204 public function testDeleteContribution() {
1206 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1208 'id' => $contributionID,
1210 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1214 * Test civicrm_contribution_search with empty params.
1215 * All available contributions expected.
1217 public function testSearchEmptyParams() {
1221 'contact_id' => $this->_individualId
,
1222 'receive_date' => date('Ymd'),
1223 'total_amount' => 100.00,
1224 'financial_type_id' => $this->_financialTypeId
,
1225 'non_deductible_amount' => 10.00,
1226 'fee_amount' => 5.00,
1227 'net_amount' => 95.00,
1229 'invoice_id' => 78910,
1231 'contribution_status_id' => 1,
1234 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1236 $result = $this->callAPISuccess('contribution', 'get', $params);
1237 // We're taking the first element.
1238 $res = $result['values'][$contribution['id']];
1240 $this->assertEquals($p['contact_id'], $res['contact_id']);
1241 $this->assertEquals($p['total_amount'], $res['total_amount']);
1242 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1243 $this->assertEquals($p['net_amount'], $res['net_amount']);
1244 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1245 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1246 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1247 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1248 $this->assertEquals($p['source'], $res['contribution_source']);
1249 // contribution_status_id = 1 => Completed
1250 $this->assertEquals('Completed', $res['contribution_status']);
1252 $this->contributionDelete($contribution['id']);
1256 * Test civicrm_contribution_search. Success expected.
1258 public function testSearch() {
1260 'contact_id' => $this->_individualId
,
1261 'receive_date' => date('Ymd'),
1262 'total_amount' => 100.00,
1263 'financial_type_id' => $this->_financialTypeId
,
1264 'non_deductible_amount' => 10.00,
1265 'contribution_status_id' => 1,
1268 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1271 'contact_id' => $this->_individualId
,
1272 'receive_date' => date('Ymd'),
1273 'total_amount' => 200.00,
1274 'financial_type_id' => $this->_financialTypeId
,
1275 'non_deductible_amount' => 20.00,
1276 'trxn_id' => 5454565,
1277 'invoice_id' => 1212124,
1278 'fee_amount' => 50.00,
1279 'net_amount' => 60.00,
1280 'contribution_status_id' => 2,
1283 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1286 'contribution_id' => $contribution2['id'],
1289 $result = $this->callAPISuccess('contribution', 'get', $params);
1290 $res = $result['values'][$contribution2['id']];
1292 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1293 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1294 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1295 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1296 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1297 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1298 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1299 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1300 // contribution_status_id = 2 => Pending
1301 $this->assertEquals('Pending', $res['contribution_status']);
1303 $this->contributionDelete($contribution1['id']);
1304 $this->contributionDelete($contribution2['id']);
1308 * Test completing a transaction via the API
1310 * Note that we are creating a logged in user because email goes out from
1313 public function testCompleteTransaction() {
1314 $mut = new CiviMailUtils($this, TRUE);
1315 $this->createLoggedInUser();
1316 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1317 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1318 $this->callAPISuccess('contribution', 'completetransaction', array(
1319 'id' => $contribution['id'],
1321 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1322 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1323 $mut->checkMailLog(array(
1324 'Receipt - Contribution',
1325 'Please print this confirmation for your records.',
1332 * Test completing a transaction via the API
1336 public function testCompleteTransactionWithReceiptDateSet() {
1337 $mut = new CiviMailUtils($this, TRUE);
1338 $this->createLoggedInUser();
1339 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1340 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1341 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1342 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1343 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1344 $mut->checkMailLog(array(
1345 'Receipt - Contribution',
1346 'Please print this confirmation for your records.',
1352 * Test completing a transaction with an event via the API
1354 * Note that we are creating a logged in user because email goes out from
1357 public function testCompleteTransactionWithParticipantRecord() {
1358 $mut = new CiviMailUtils($this, TRUE);
1359 $mut->clearMessages();
1360 $this->createLoggedInUser();
1361 $contributionID = $this->createPendingParticipantContribution();
1362 $this->callAPISuccess('contribution', 'completetransaction', array(
1363 'id' => $contributionID,
1366 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1367 'id' => $this->_ids
['participant'],
1368 'return' => 'participant_status_id',
1370 $this->assertEquals(1, $participantStatus);
1371 $mut->checkMailLog(array(
1372 'Annual CiviCRM meet',
1374 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1380 * Test membership is renewed when transaction completed
1382 public function testCompleteTransactionMembershipPriceSet() {
1383 $this->createPriceSetWithPage('membership');
1384 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1385 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1386 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1387 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1388 $this->cleanUpAfterPriceSets();
1392 * Test membership is renewed when transaction completed
1394 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1395 $this->createPriceSetWithPage('membership');
1396 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1397 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1398 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1399 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1400 $this->cleanUpAfterPriceSets();
1403 public function cleanUpAfterPriceSets() {
1404 $this->quickCleanUpFinancialEntities();
1405 $this->contactDelete($this->_ids
['contact']);
1406 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1411 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1412 * on parent class at some point (fn is not in 4.4)
1414 * @param array $params
1416 public function createPriceSetWithPage($entity, $params = array()) {
1417 $membershipTypeID = $this->membershipTypeCreate();
1418 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1419 'title' => "Test Contribution Page",
1420 'financial_type_id' => 1,
1421 'currency' => 'NZD',
1422 'goal_amount' => 50,
1423 'is_pay_later' => 1,
1424 'is_monetary' => TRUE,
1425 'is_email_receipt' => FALSE,
1427 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1428 'is_quick_config' => 0,
1429 'extends' => 'CiviMember',
1430 'financial_type_id' => 1,
1431 'title' => 'my Page',
1433 $priceSetID = $priceSet['id'];
1435 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1436 $priceField = $this->callAPISuccess('price_field', 'create', array(
1437 'price_set_id' => $priceSetID,
1438 'label' => 'Goat Breed',
1439 'html_type' => 'Radio',
1441 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1442 'price_set_id' => $priceSetID,
1443 'price_field_id' => $priceField['id'],
1444 'label' => 'Long Haired Goat',
1446 'membership_type_id' => $membershipTypeID,
1447 'membership_num_terms' => 1,
1450 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1451 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1452 'price_set_id' => $priceSetID,
1453 'price_field_id' => $priceField['id'],
1454 'label' => 'Shoe-eating Goat',
1456 'membership_type_id' => $membershipTypeID,
1457 'membership_num_terms' => 2,
1460 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1461 $this->_ids
['price_set'] = $priceSetID;
1462 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1463 $this->_ids
['price_field'] = array($priceField['id']);
1465 $this->_ids
['membership_type'] = $membershipTypeID;
1469 * Set up a pending transaction with a specific price field id
1470 * @param int $priceFieldValueID
1472 public function setUpPendingContribution($priceFieldValueID) {
1473 $contactID = $this->individualCreate();
1474 $membership = $this->callAPISuccess('membership', 'create', array(
1475 'contact_id' => $contactID,
1476 'membership_type_id' => $this->_ids
['membership_type'],
1477 'start_date' => 'yesterday - 1 year',
1478 'end_date' => 'yesterday',
1480 $contribution = $this->callAPISuccess('contribution', 'create', array(
1482 'contact_id' => $contactID,
1483 'receive_date' => date('Ymd'),
1484 'total_amount' => 100.00,
1485 'financial_type_id' => 1,
1486 'payment_instrument_id' => 'Credit Card',
1487 'non_deductible_amount' => 10.00,
1488 'trxn_id' => 'jdhfi88',
1489 'invoice_id' => 'djfhiewuyr',
1491 'contribution_status_id' => 2,
1492 'contribution_page_id' => $this->_ids
['contribution_page'],
1493 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1496 $this->callAPISuccess('line_item', 'create', array(
1497 'entity_id' => $contribution['id'],
1498 'entity_table' => 'civicrm_contribution',
1499 'contribution_id' => $contribution['id'],
1500 'price_field_id' => $this->_ids
['price_field'][0],
1504 'financial_type_id' => 1,
1505 'price_field_value_id' => $priceFieldValueID,
1507 $this->_ids
['contact'] = $contactID;
1508 $this->_ids
['contribution'] = $contribution['id'];
1509 $this->_ids
['membership'] = $membership['id'];
1513 * Test sending a mail via the API
1515 public function testSendMail() {
1516 $mut = new CiviMailUtils($this, TRUE);
1517 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1518 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1519 'id' => $contribution['id'],
1520 'receipt_from_email' => 'api@civicrm.org',
1523 $mut->checkMailLog(array(
1525 'Contribution Information',
1526 'Please print this confirmation for your records',
1535 * Test sending a mail via the API
1537 public function testSendMailEvent() {
1538 $mut = new CiviMailUtils($this, TRUE);
1539 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1540 $event = $this->eventCreate(array(
1541 'is_email_confirm' => 1,
1542 'confirm_from_email' => 'test@civicrm.org',
1544 $this->_eventID
= $event['id'];
1545 $participantParams = array(
1546 'contact_id' => $this->_individualId
,
1547 'event_id' => $this->_eventID
,
1550 // to ensure it matches later on
1551 'register_date' => '2007-07-21 00:00:00',
1552 'source' => 'Online Event Registration: API Testing',
1555 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1556 $this->callAPISuccess('participant_payment', 'create', array(
1557 'participant_id' => $participant['id'],
1558 'contribution_id' => $contribution['id'],
1560 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1561 'id' => $contribution['id'],
1562 'receipt_from_email' => 'api@civicrm.org',
1566 $mut->checkMailLog(array(
1567 'Annual CiviCRM meet',
1569 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1576 * This function does a GET & compares the result against the $params
1577 * Use as a double check on Creates
1580 * @param int $delete
1582 public function contributionGetnCheck($params, $id, $delete = 1) {
1584 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1590 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1592 $this->assertAPISuccess($contribution, 0);
1593 $values = $contribution['values'][$contribution['id']];
1594 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1595 // this is not returned in id format
1596 unset($params['payment_instrument_id']);
1597 $params['contribution_source'] = $params['source'];
1598 unset($params['source']);
1599 foreach ($params as $key => $value) {
1600 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1605 * Create a pending contribution & linked pending participant record
1606 * (along with an event)
1608 public function createPendingParticipantContribution() {
1609 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1610 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1611 $this->_ids
['participant'] = $participantID;
1612 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1613 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1614 $this->callAPISuccess('participant_payment', 'create', array(
1615 'contribution_id' => $contribution['id'],
1616 'participant_id' => $participantID,
1618 $this->callAPISuccess('line_item', 'get', array(
1619 'entity_id' => $contribution['id'],
1620 'entity_table' => 'civicrm_contribution',
1621 'api.line_item.create' => array(
1622 'entity_id' => $participantID,
1623 'entity_table' => 'civicrm_participant',
1626 return $contribution['id'];
1630 * @param int $contId
1632 * @return null|string
1634 function _getFinancialTrxnAmount($contId) {
1636 SUM( ft.total_amount ) AS total
1637 FROM civicrm_financial_trxn AS ft
1638 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1639 WHERE ceft.entity_table = 'civicrm_contribution'
1640 AND ceft.entity_id = {$contId}";
1642 $result = CRM_Core_DAO
::singleValueQuery($query);
1647 * @param int $contId
1649 * @return null|string
1651 function _getFinancialItemAmount($contId) {
1652 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1655 FROM civicrm_financial_item
1656 WHERE entity_table = 'civicrm_line_item'
1657 AND entity_id = {$lineItem}";
1658 $result = CRM_Core_DAO
::singleValueQuery($query);
1663 * @param int $contId
1666 public function _checkFinancialItem($contId, $context) {
1667 if ($context != 'paylater') {
1669 'entity_id' => $contId,
1670 'entity_table' => 'civicrm_contribution',
1672 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1673 $entityParams = array(
1674 'financial_trxn_id' => $trxn['financial_trxn_id'],
1675 'entity_table' => 'civicrm_financial_item',
1677 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1679 'id' => $entityTrxn['entity_id'],
1682 if ($context == 'paylater') {
1683 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1684 foreach ($lineItems as $key => $item) {
1686 'entity_id' => $key,
1687 'entity_table' => 'civicrm_line_item',
1689 $compareParams = array('status_id' => 1);
1690 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1693 elseif ($context == 'refund') {
1694 $compareParams = array(
1696 'financial_account_id' => 1,
1700 elseif ($context == 'cancelPending') {
1701 $compareParams = array(
1703 'financial_account_id' => 1,
1707 elseif ($context == 'changeFinancial') {
1708 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1710 'entity_id' => $lineKey,
1713 $compareParams = array(
1714 'financial_account_id' => 1,
1716 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1718 'financial_account_id' => 3,
1719 'entity_id' => $lineKey,
1721 $compareParams = array(
1725 if ($context != 'paylater') {
1726 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1731 * @param $contribution
1733 * @param int $instrumentId
1735 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1736 $trxnParams = array(
1737 'entity_id' => $contribution['id'],
1738 'entity_table' => 'civicrm_contribution',
1740 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1742 'id' => $trxn['financial_trxn_id'],
1744 if ($context == 'payLater') {
1745 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1746 $compareParams = array(
1748 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1751 elseif ($context == 'refund') {
1752 $compareParams = array(
1753 'to_financial_account_id' => 6,
1754 'total_amount' => -100,
1758 elseif ($context == 'cancelPending') {
1759 $compareParams = array(
1760 'from_financial_account_id' => 7,
1761 'total_amount' => -100,
1765 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1766 $entityParams = array(
1767 'entity_id' => $contribution['id'],
1768 'entity_table' => 'civicrm_contribution',
1771 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1772 $trxnParams1 = array(
1773 'id' => $trxn['financial_trxn_id'],
1775 $compareParams = array(
1776 'total_amount' => -100,
1779 if ($context == 'paymentInstrument') {
1780 $compareParams +
= array(
1781 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1782 'payment_instrument_id' => 4,
1786 $compareParams['to_financial_account_id'] = 12;
1788 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1789 $compareParams['total_amount'] = 100;
1790 if ($context == 'paymentInstrument') {
1791 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1792 $compareParams['payment_instrument_id'] = $instrumentId;
1795 $compareParams['to_financial_account_id'] = 12;
1799 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1805 public function _addPaymentInstrument() {
1806 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1807 $optionParams = array(
1808 'option_group_id' => $gId,
1809 'label' => 'Test Card',
1810 'name' => 'Test Card',
1815 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1816 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1817 $financialParams = array(
1818 'entity_table' => 'civicrm_option_value',
1819 'entity_id' => $optionValue['id'],
1820 'account_relationship' => $relationTypeId,
1821 'financial_account_id' => 7,
1823 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1824 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1825 return $optionValue['values'][$optionValue['id']]['value'];
1829 * @param array $params
1832 public function _checkFinancialRecords($params, $context) {
1833 $entityParams = array(
1834 'entity_id' => $params['id'],
1835 'entity_table' => 'civicrm_contribution',
1837 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1838 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1839 if ($context == 'pending') {
1840 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1841 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1844 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1845 $trxnParams = array(
1846 'id' => $trxn['financial_trxn_id'],
1848 if ($context != 'online' && $context != 'payLater') {
1849 $compareParams = array(
1850 'to_financial_account_id' => 6,
1851 'total_amount' => 100,
1855 if ($context == 'feeAmount') {
1856 $compareParams['fee_amount'] = 50;
1858 elseif ($context == 'online') {
1859 $compareParams = array(
1860 'to_financial_account_id' => 12,
1861 'total_amount' => 100,
1865 elseif ($context == 'payLater') {
1866 $compareParams = array(
1867 'to_financial_account_id' => 7,
1868 'total_amount' => 100,
1872 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1873 $entityParams = array(
1874 'financial_trxn_id' => $trxn['financial_trxn_id'],
1875 'entity_table' => 'civicrm_financial_item',
1877 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1878 $fitemParams = array(
1879 'id' => $entityTrxn['entity_id'],
1881 $compareParams = array(
1884 'financial_account_id' => 1,
1886 if ($context == 'payLater') {
1887 $compareParams = array(
1890 'financial_account_id' => 1,
1893 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1894 if ($context == 'feeAmount') {
1896 'entity_id' => $params['id'],
1897 'entity_table' => 'civicrm_contribution',
1899 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1900 $trxnParams = array(
1901 'id' => $maxTrxn['financial_trxn_id'],
1903 $compareParams = array(
1904 'to_financial_account_id' => 5,
1905 'from_financial_account_id' => 6,
1906 'total_amount' => 50,
1909 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1910 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1911 $fitemParams = array(
1912 'entity_id' => $trxnId['financialTrxnId'],
1913 'entity_table' => 'civicrm_financial_trxn',
1915 $compareParams = array(
1918 'financial_account_id' => 5,
1920 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);