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);
277 * Check with complete array + custom field
278 * Note that the test is written on purpose without any
279 * variables specific to participant so it can be replicated into other entities
280 * and / or moved to the automated test suite
282 public function testCreateWithCustom() {
283 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
285 $params = $this->_params
;
286 $params['custom_' . $ids['custom_field_id']] = "custom string";
288 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
289 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
290 $check = $this->callAPISuccess($this->_entity
, 'get', array(
291 'return.custom_' . $ids['custom_field_id'] => 1,
292 'id' => $result['id'],
294 $this->customFieldDelete($ids['custom_field_id']);
295 $this->customGroupDelete($ids['custom_group_id']);
296 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
300 * Check with complete array + custom field.
302 * Note that the test is written on purpose without any
303 * variables specific to participant so it can be replicated into other entities
304 * and / or moved to the automated test suite
306 public function testCreateGetFieldsWithCustom() {
307 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
308 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
309 $result = $this->callAPISuccess('Contribution', 'getfields', array());
310 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
311 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
312 $this->customFieldDelete($ids['custom_field_id']);
313 $this->customGroupDelete($ids['custom_group_id']);
314 $this->customFieldDelete($idsContact['custom_field_id']);
315 $this->customGroupDelete($idsContact['custom_group_id']);
318 public function testCreateContributionNoLineItems() {
321 'contact_id' => $this->_individualId
,
322 'receive_date' => '20120511',
323 'total_amount' => 100.00,
324 'financial_type_id' => $this->_financialTypeId
,
325 'payment_instrument_id' => 1,
326 'non_deductible_amount' => 10.00,
327 'fee_amount' => 50.00,
328 'net_amount' => 90.00,
330 'invoice_id' => 67890,
332 'contribution_status_id' => 1,
336 $contribution = $this->callAPISuccess('contribution', 'create', $params);
337 $lineItems = $this->callAPISuccess('line_item', 'get', array(
338 'entity_id' => $contribution['id'],
339 'entity_table' => 'civicrm_contribution',
342 $this->assertEquals(0, $lineItems['count']);
346 * Test checks that passing in line items suppresses the create mechanism
348 public function testCreateContributionChainedLineItems() {
351 'contact_id' => $this->_individualId
,
352 'receive_date' => '20120511',
353 'total_amount' => 100.00,
354 'financial_type_id' => $this->_financialTypeId
,
355 'payment_instrument_id' => 1,
356 'non_deductible_amount' => 10.00,
357 'fee_amount' => 50.00,
358 'net_amount' => 90.00,
360 'invoice_id' => 67890,
362 'contribution_status_id' => 1,
364 'api.line_item.create' => array(
366 'price_field_id' => 1,
368 'line_total' => '20',
369 'unit_price' => '10',
372 'price_field_id' => 1,
374 'line_total' => '80',
375 'unit_price' => '80',
380 $description = "Create Contribution with Nested Line Items";
381 $subfile = "CreateWithNestedLineItems";
382 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
384 $lineItems = $this->callAPISuccess('line_item', 'get', array(
385 'entity_id' => $contribution['id'],
386 'contribution_id' => $contribution['id'],
387 'entity_table' => 'civicrm_contribution',
390 $this->assertEquals(2, $lineItems['count']);
393 public function testCreateContributionOffline() {
395 'contact_id' => $this->_individualId
,
396 'receive_date' => '20120511',
397 'total_amount' => 100.00,
398 'financial_type_id' => 1,
400 'invoice_id' => 67890,
402 'contribution_status_id' => 1,
405 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
406 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
407 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
408 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
409 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
410 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
411 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
412 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
413 $lineItems = $this->callAPISuccess('line_item', 'get', array(
414 'entity_id' => $contribution['id'],
415 'contribution_id' => $contribution['id'],
416 'entity_table' => 'civicrm_contribution',
419 $this->assertEquals(1, $lineItems['count']);
420 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
421 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
422 $this->_checkFinancialRecords($contribution, 'offline');
423 $this->contributionGetnCheck($params, $contribution['id']);
427 * Test create with valid payment instument
429 public function testCreateContributionWithPaymentInstrument() {
430 $params = $this->_params +
array('payment_instrument' => 'EFT');
431 $contribution = $this->callAPISuccess('contribution', 'create', $params);
432 $contribution = $this->callAPISuccess('contribution', 'get', array(
434 'id' => $contribution['id'],
436 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
437 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
439 $this->callAPISuccess('contribution', 'create', array(
440 'id' => $contribution['id'],
441 'payment_instrument' => 'Credit Card',
443 $contribution = $this->callAPISuccess('contribution', 'get', array(
445 'id' => $contribution['id'],
447 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
448 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
451 public function testGetContributionByPaymentInstrument() {
452 $params = $this->_params +
array('payment_instrument' => 'EFT');
453 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
454 $this->callAPISuccess('contribution', 'create', $params);
455 $this->callAPISuccess('contribution', 'create', $params2);
456 $contribution = $this->callAPISuccess('contribution', 'get', array(
458 'contribution_payment_instrument' => 'Cash',
460 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
461 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
462 $this->assertEquals(1,$contribution['count']);
463 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument' => 'Cash'));
464 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
465 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
466 $this->assertEquals(1, $contribution['count']);
467 $contribution = $this->callAPISuccess('contribution', 'get', array(
469 'payment_instrument_id' => 5,
471 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
472 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
473 $this->assertEquals(1, $contribution['count']);
474 $contribution = $this->callAPISuccess('contribution', 'get', array(
476 '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(
482 'id' => $contribution['id'],
483 'payment_instrument' => 'Credit Card',
485 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
486 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
487 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
488 $this->assertEquals(1, $contribution['count']);
492 * Create test with unique field name on source
494 public function testCreateContributionSource() {
497 'contact_id' => $this->_individualId
,
498 'receive_date' => date('Ymd'),
499 'total_amount' => 100.00,
500 'financial_type_id' => $this->_financialTypeId
,
501 'payment_instrument_id' => 1,
502 'non_deductible_amount' => 10.00,
503 'fee_amount' => 50.00,
504 'net_amount' => 90.00,
506 'invoice_id' => 67890,
507 'contribution_source' => 'SSF',
508 'contribution_status_id' => 1,
511 $contribution = $this->callAPISuccess('contribution', 'create', $params);
512 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
513 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
517 * Create test with unique field name on source
519 public function testCreateDefaultNow() {
521 $params = $this->_params
;
522 unset($params['receive_date']);
524 $contribution = $this->callAPISuccess('contribution', 'create', $params);
525 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
526 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
530 * Create test with unique field name on source.
532 public function testCreateContributionSourceInvalidContact() {
536 'receive_date' => date('Ymd'),
537 'total_amount' => 100.00,
538 'financial_type_id' => $this->_financialTypeId
,
539 'payment_instrument_id' => 1,
540 'non_deductible_amount' => 10.00,
541 'fee_amount' => 50.00,
542 'net_amount' => 90.00,
544 'invoice_id' => 67890,
545 'contribution_source' => 'SSF',
546 'contribution_status_id' => 1,
549 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
552 public function testCreateContributionSourceInvalidContContact() {
555 'contribution_contact_id' => 999,
556 'receive_date' => date('Ymd'),
557 'total_amount' => 100.00,
558 'financial_type_id' => $this->_financialTypeId
,
559 'payment_instrument_id' => 1,
560 'non_deductible_amount' => 10.00,
561 'fee_amount' => 50.00,
562 'net_amount' => 90.00,
564 'invoice_id' => 67890,
565 'contribution_source' => 'SSF',
566 'contribution_status_id' => 1,
569 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
573 * Test note created correctly.
575 public function testCreateContributionWithNote() {
576 $description = "Demonstrates creating contribution with Note Entity";
577 $subfile = "ContributionCreateWithNote";
579 'contact_id' => $this->_individualId
,
580 'receive_date' => '2012-01-01',
581 'total_amount' => 100.00,
582 'financial_type_id' => $this->_financialTypeId
,
583 'payment_instrument_id' => 1,
584 'non_deductible_amount' => 10.00,
585 'fee_amount' => 50.00,
586 'net_amount' => 90.00,
588 'invoice_id' => 67890,
590 'contribution_status_id' => 1,
591 'note' => 'my contribution note',
594 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
595 $result = $this->callAPISuccess('note', 'get', array(
596 'entity_table' => 'civicrm_contribution',
597 'entity_id' => $contribution['id'],
600 $this->assertEquals('my contribution note', $result['values'][0]['note']);
601 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
604 public function testCreateContributionWithNoteUniqueNameAliases() {
606 'contact_id' => $this->_individualId
,
607 'receive_date' => '2012-01-01',
608 'total_amount' => 100.00,
609 'financial_type_id' => $this->_financialTypeId
,
610 'payment_instrument_id' => 1,
611 'non_deductible_amount' => 10.00,
612 'fee_amount' => 50.00,
613 'net_amount' => 90.00,
615 'invoice_id' => 67890,
617 'contribution_status_id' => 1,
618 'contribution_note' => 'my contribution note',
621 $contribution = $this->callAPISuccess('contribution', 'create', $params);
622 $result = $this->callAPISuccess('note', 'get', array(
623 'entity_table' => 'civicrm_contribution',
624 'entity_id' => $contribution['id'],
627 $this->assertEquals('my contribution note', $result['values'][0]['note']);
628 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
632 * This is the test for creating soft credits.
634 public function testCreateContributionWithSoftCredit() {
635 $description = "Demonstrates creating contribution with SoftCredit";
636 $subfile = "ContributionCreateWithSoftCredit";
637 $contact2 = $this->callAPISuccess('Contact', 'create', array(
638 'display_name' => 'superman',
639 'contact_type' => 'Individual',
642 'contact_id' => $contact2['id'],
644 'soft_credit_type_id' => 3,
647 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
648 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
649 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
651 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
652 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
653 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
655 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
656 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
659 public function testCreateContributionWithSoftCreditDefaults() {
660 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
661 $subfile = "ContributionCreateWithSoftCreditDefaults";
662 $contact2 = $this->callAPISuccess('Contact', 'create', array(
663 'display_name' => 'superman',
664 'contact_type' => 'Individual',
666 $params = $this->_params +
array(
667 'soft_credit_to' => $contact2['id'],
669 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
670 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
672 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
673 // Default soft credit amount = contribution.total_amount
674 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
675 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
677 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
678 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
681 public function testCreateContributionWithHonoreeContact() {
682 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
683 $subfile = "ContributionCreateWithHonoreeContact";
684 $contact2 = $this->callAPISuccess('Contact', 'create', array(
685 'display_name' => 'superman',
686 'contact_type' => 'Individual',
688 $params = $this->_params +
array(
689 'honor_contact_id' => $contact2['id'],
691 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
692 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
694 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
695 // Default soft credit amount = contribution.total_amount
696 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
697 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
698 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
700 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
701 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
705 * Test using example code.
707 public function testContributionCreateExample() {
708 //make sure at least on page exists since there is a truncate in tear down
709 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
710 $this->assertAPISuccess($page);
711 require_once 'api/v3/examples/Contribution/Create.php';
712 $result = contribution_create_example();
713 $this->assertAPISuccess($result);
714 $contributionId = $result['id'];
715 $expectedResult = contribution_create_expectedresult();
716 $this->checkArrayEquals($expectedResult, $result);
717 $this->contributionDelete($contributionId);
721 * Function tests that additional financial records are created when fee amount is recorded
723 public function testCreateContributionWithFee() {
725 'contact_id' => $this->_individualId
,
726 'receive_date' => '20120511',
727 'total_amount' => 100.00,
729 'financial_type_id' => 1,
731 'invoice_id' => 67890,
733 'contribution_status_id' => 1,
736 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
737 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
738 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
739 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
740 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
741 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
742 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
743 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
744 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
745 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
747 $lineItems = $this->callAPISuccess('line_item', 'get', array(
749 'entity_id' => $contribution['id'],
750 'entity_table' => 'civicrm_contribution',
753 $this->assertEquals(1, $lineItems['count']);
754 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
755 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
756 $lineItems = $this->callAPISuccess('line_item', 'get', array(
758 'entity_id' => $contribution['id'],
759 'contribution_id' => $contribution['id'],
760 'entity_table' => 'civicrm_contribution',
763 $this->assertEquals(1, $lineItems['count']);
764 $this->_checkFinancialRecords($contribution, 'feeAmount');
769 * Function tests that additional financial records are created when online contribution is created.
771 public function testCreateContributionOnline() {
772 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
773 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
774 $this->assertAPISuccess($contributionPage);
776 'contact_id' => $this->_individualId
,
777 'receive_date' => '20120511',
778 'total_amount' => 100.00,
779 'financial_type_id' => 1,
780 'contribution_page_id' => $contributionPage['id'],
781 'payment_processor' => 1,
783 'invoice_id' => 67890,
785 'contribution_status_id' => 1,
789 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
790 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
791 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
792 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
793 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
794 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
795 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
796 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
797 $this->_checkFinancialRecords($contribution, 'online');
801 * Check handling of financial type.
803 * In the interests of removing financial type / contribution type checks from
804 * legacy format function lets test that the api is doing this for us
806 public function testCreateInvalidFinancialType() {
807 $params = $this->_params
;
808 $params['financial_type_id'] = 99999;
809 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
813 * Check handling of financial type.
815 * In the interests of removing financial type / contribution type checks from
816 * legacy format function lets test that the api is doing this for us
818 public function testValidNamedFinancialType() {
819 $params = $this->_params
;
820 $params['financial_type_id'] = 'Donation';
821 $this->callAPISuccess($this->_entity
, 'create', $params);
825 * Function tests that additional financial records are created when online contribution with pay later option
828 public function testCreateContributionPayLaterOnline() {
829 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
830 $this->_pageParams
['is_pay_later'] = 1;
831 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
832 $this->assertAPISuccess($contributionPage);
834 'contact_id' => $this->_individualId
,
835 'receive_date' => '20120511',
836 'total_amount' => 100.00,
837 'financial_type_id' => 1,
838 'contribution_page_id' => $contributionPage['id'],
841 'invoice_id' => 67890,
843 'contribution_status_id' => 2,
847 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
848 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
849 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
850 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
851 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
852 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
853 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
854 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
855 $this->_checkFinancialRecords($contribution, 'payLater');
859 * Function tests that additional financial records are created when online contribution with pending option
862 public function testCreateContributionPendingOnline() {
863 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
864 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
865 $this->assertAPISuccess($contributionPage);
867 'contact_id' => $this->_individualId
,
868 'receive_date' => '20120511',
869 'total_amount' => 100.00,
870 'financial_type_id' => 1,
871 'contribution_page_id' => $contributionPage['id'],
873 'invoice_id' => 67890,
875 'contribution_status_id' => 2,
878 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
879 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
880 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
881 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
882 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
883 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
884 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
885 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
886 $this->_checkFinancialRecords($contribution, 'pending');
890 * Test that BAO defaults work
892 public function testCreateBAODefaults() {
893 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
894 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
895 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
896 'id' => $contribution['id'],
897 'api.contribution.delete' => 1,
899 $this->assertEquals(1, $contribution['contribution_status_id']);
900 $this->assertEquals('Check', $contribution['payment_instrument']);
904 * Function tests that line items, financial records are updated when contribution amount is changed
906 public function testCreateUpdateContributionChangeTotal() {
907 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
908 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
910 'entity_id' => $contribution['id'],
911 'entity_table' => 'civicrm_contribution',
913 'return' => 'line_total',
915 $this->assertEquals('100.00', $lineItems);
916 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
917 // Financial trxn SUM = 100 + 5 (fee)
918 $this->assertEquals('105.00', $trxnAmount);
921 'id' => $contribution['id'],
922 'total_amount' => '125',
924 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
926 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
928 'entity_id' => $contribution['id'],
929 'entity_table' => 'civicrm_contribution',
931 'return' => 'line_total',
934 $this->assertEquals('125.00', $lineItems);
935 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
936 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
937 // Financial trxn SUM = 125 + 5 (fee)
938 $this->assertEquals('130.00', $trxnAmount);
939 $this->assertEquals('125.00', $fitemAmount);
943 * Function tests that line items, financial records are updated when pay later contribution is received
945 public function testCreateUpdateContributionPayLater() {
946 $contribParams = array(
947 'contact_id' => $this->_individualId
,
948 'receive_date' => '2012-01-01',
949 'total_amount' => 100.00,
950 'financial_type_id' => $this->_financialTypeId
,
951 'payment_instrument_id' => 1,
952 'contribution_status_id' => 2,
956 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
958 $newParams = array_merge($contribParams, array(
959 'id' => $contribution['id'],
960 'contribution_status_id' => 1,
963 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
964 $contribution = $contribution['values'][$contribution['id']];
965 $this->assertEquals($contribution['contribution_status_id'], '1');
966 $this->_checkFinancialItem($contribution['id'], 'paylater');
967 $this->_checkFinancialTrxn($contribution, 'payLater');
971 * Function tests that financial records are updated when Payment Instrument is changed
973 public function testCreateUpdateContributionPaymentInstrument() {
974 $instrumentId = $this->_addPaymentInstrument();
975 $contribParams = array(
976 'contact_id' => $this->_individualId
,
977 'total_amount' => 100.00,
978 'financial_type_id' => $this->_financialTypeId
,
979 'payment_instrument_id' => 4,
980 'contribution_status_id' => 1,
983 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
985 $newParams = array_merge($contribParams, array(
986 'id' => $contribution['id'],
987 'payment_instrument_id' => $instrumentId,
990 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
991 $this->assertAPISuccess($contribution);
992 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
996 * Function tests that financial records are added when Contribution is Refunded
998 public function testCreateUpdateContributionRefund() {
999 $contribParams = array(
1000 'contact_id' => $this->_individualId
,
1001 'receive_date' => '2012-01-01',
1002 'total_amount' => 100.00,
1003 'financial_type_id' => $this->_financialTypeId
,
1004 'payment_instrument_id' => 4,
1005 'contribution_status_id' => 1,
1008 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1009 $newParams = array_merge($contribParams, array(
1010 'id' => $contribution['id'],
1011 'contribution_status_id' => 7,
1015 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1016 $this->_checkFinancialTrxn($contribution, 'refund');
1017 $this->_checkFinancialItem($contribution['id'], 'refund');
1021 * Function tests invalid contribution status change
1023 public function testCreateUpdateContributionInValidStatusChange() {
1024 $contribParams = array(
1026 'receive_date' => '2012-01-01',
1027 'total_amount' => 100.00,
1028 'financial_type_id' => 1,
1029 'payment_instrument_id' => 1,
1030 'contribution_status_id' => 1,
1032 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1033 $newParams = array_merge($contribParams, array(
1034 'id' => $contribution['id'],
1035 'contribution_status_id' => 2,
1038 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1043 * Function tests that financial records are added when Pending Contribution is Canceled
1045 public function testCreateUpdateContributionCancelPending() {
1046 $contribParams = array(
1047 'contact_id' => $this->_individualId
,
1048 'receive_date' => '2012-01-01',
1049 'total_amount' => 100.00,
1050 'financial_type_id' => $this->_financialTypeId
,
1051 'payment_instrument_id' => 1,
1052 'contribution_status_id' => 2,
1053 'is_pay_later' => 1,
1056 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1057 $newParams = array_merge($contribParams, array(
1058 'id' => $contribution['id'],
1059 'contribution_status_id' => 3,
1062 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1063 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1064 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1068 * Function tests that financial records are added when Financial Type is Changed
1070 public function testCreateUpdateContributionChangeFinancialType() {
1071 $contribParams = array(
1072 'contact_id' => $this->_individualId
,
1073 'receive_date' => '2012-01-01',
1074 'total_amount' => 100.00,
1075 'financial_type_id' => 1,
1076 'payment_instrument_id' => 1,
1077 'contribution_status_id' => 1,
1080 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1081 $newParams = array_merge($contribParams, array(
1082 'id' => $contribution['id'],
1083 'financial_type_id' => 3,
1086 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1087 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1088 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1092 * Test that update does not change status id CRM-15105
1094 public function testCreateUpdateWithoutChangingPendingStatus() {
1095 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1096 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1097 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1098 'id' => $contribution['id'],
1099 'api.contribution.delete' => 1,
1101 $this->assertEquals(2, $contribution['contribution_status_id']);
1103 //To Update Contribution
1104 //CHANGE: we require the API to do an incremental update
1105 public function testCreateUpdateContribution() {
1107 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1108 $old_params = array(
1109 'contribution_id' => $contributionID,
1112 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1113 //Make sure it came back
1114 $this->assertAPISuccess($original);
1115 $this->assertEquals($original['id'], $contributionID);
1116 //set up list of old params, verify
1118 //This should not be required on update:
1119 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1120 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1121 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1122 $old_source = $original['values'][$contributionID]['contribution_source'];
1124 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1125 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1127 //check against values in CiviUnitTestCase::createContribution()
1128 $this->assertEquals($old_contact_id, $this->_individualId
);
1129 $this->assertEquals($old_fee_amount, 5.00);
1130 $this->assertEquals($old_source, 'SSF');
1131 $this->assertEquals($old_trxn_id, 212355);
1132 $this->assertEquals($old_invoice_id, 'idofsh');
1134 'id' => $contributionID,
1135 'contact_id' => $this->_individualId
,
1136 'total_amount' => 110.00,
1137 'financial_type_id' => $this->_financialTypeId
,
1138 'non_deductible_amount' => 10.00,
1139 'net_amount' => 100.00,
1140 'contribution_status_id' => 1,
1141 'note' => 'Donating for Nobel Cause',
1145 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1147 $new_params = array(
1148 'contribution_id' => $contribution['id'],
1151 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1153 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1154 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1155 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
);
1156 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1157 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1158 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1159 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1160 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1161 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1162 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1163 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1165 'contribution_id' => $contributionID,
1168 $result = $this->callAPISuccess('contribution', 'delete', $params);
1169 $this->assertAPISuccess($result);
1172 ///////////////// civicrm_contribution_delete methods
1173 public function testDeleteEmptyParamsContribution() {
1175 $this->callAPIFailure('contribution', 'delete', $params);
1178 public function testDeleteParamsNotArrayContribution() {
1179 $params = 'contribution_id= 1';
1180 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1181 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1184 public function testDeleteWrongParamContribution() {
1186 'contribution_source' => 'SSF',
1189 $this->callAPIFailure('contribution', 'delete', $params);
1192 public function testDeleteContribution() {
1194 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1196 'id' => $contributionID,
1198 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1202 * Test civicrm_contribution_search with empty params.
1203 * All available contributions expected.
1205 public function testSearchEmptyParams() {
1209 'contact_id' => $this->_individualId
,
1210 'receive_date' => date('Ymd'),
1211 'total_amount' => 100.00,
1212 'financial_type_id' => $this->_financialTypeId
,
1213 'non_deductible_amount' => 10.00,
1214 'fee_amount' => 5.00,
1215 'net_amount' => 95.00,
1217 'invoice_id' => 78910,
1219 'contribution_status_id' => 1,
1222 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1224 $result = $this->callAPISuccess('contribution', 'get', $params);
1225 // We're taking the first element.
1226 $res = $result['values'][$contribution['id']];
1228 $this->assertEquals($p['contact_id'], $res['contact_id']);
1229 $this->assertEquals($p['total_amount'], $res['total_amount']);
1230 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1231 $this->assertEquals($p['net_amount'], $res['net_amount']);
1232 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1233 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1234 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1235 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1236 $this->assertEquals($p['source'], $res['contribution_source']);
1237 // contribution_status_id = 1 => Completed
1238 $this->assertEquals('Completed', $res['contribution_status']);
1240 $this->contributionDelete($contribution['id']);
1244 * Test civicrm_contribution_search. Success expected.
1246 public function testSearch() {
1248 'contact_id' => $this->_individualId
,
1249 'receive_date' => date('Ymd'),
1250 'total_amount' => 100.00,
1251 'financial_type_id' => $this->_financialTypeId
,
1252 'non_deductible_amount' => 10.00,
1253 'contribution_status_id' => 1,
1256 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1259 'contact_id' => $this->_individualId
,
1260 'receive_date' => date('Ymd'),
1261 'total_amount' => 200.00,
1262 'financial_type_id' => $this->_financialTypeId
,
1263 'non_deductible_amount' => 20.00,
1264 'trxn_id' => 5454565,
1265 'invoice_id' => 1212124,
1266 'fee_amount' => 50.00,
1267 'net_amount' => 60.00,
1268 'contribution_status_id' => 2,
1271 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1274 'contribution_id' => $contribution2['id'],
1277 $result = $this->callAPISuccess('contribution', 'get', $params);
1278 $res = $result['values'][$contribution2['id']];
1280 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1281 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1282 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1283 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1284 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1285 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1286 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1287 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1288 // contribution_status_id = 2 => Pending
1289 $this->assertEquals('Pending', $res['contribution_status']);
1291 $this->contributionDelete($contribution1['id']);
1292 $this->contributionDelete($contribution2['id']);
1296 * Test completing a transaction via the API
1298 * Note that we are creating a logged in user because email goes out from
1301 public function testCompleteTransaction() {
1302 $mut = new CiviMailUtils($this, TRUE);
1303 $this->createLoggedInUser();
1304 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1305 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1306 $this->callAPISuccess('contribution', 'completetransaction', array(
1307 'id' => $contribution['id'],
1309 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1310 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1311 $mut->checkMailLog(array(
1312 'Receipt - Contribution',
1313 'Please print this confirmation for your records.',
1320 * Test completing a transaction via the API
1324 public function testCompleteTransactionWithReceiptDateSet() {
1325 $mut = new CiviMailUtils($this, TRUE);
1326 $this->createLoggedInUser();
1327 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1328 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1329 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1330 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1331 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1332 $mut->checkMailLog(array(
1333 'Receipt - Contribution',
1334 'Please print this confirmation for your records.',
1340 * Test completing a transaction with an event via the API
1342 * Note that we are creating a logged in user because email goes out from
1345 public function testCompleteTransactionWithParticipantRecord() {
1346 $mut = new CiviMailUtils($this, TRUE);
1347 $mut->clearMessages();
1348 $this->createLoggedInUser();
1349 $contributionID = $this->createPendingParticipantContribution();
1350 $this->callAPISuccess('contribution', 'completetransaction', array(
1351 'id' => $contributionID,
1354 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1355 'id' => $this->_ids
['participant'],
1356 'return' => 'participant_status_id',
1358 $this->assertEquals(1, $participantStatus);
1359 $mut->checkMailLog(array(
1360 'Annual CiviCRM meet',
1362 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1368 * Test membership is renewed when transaction completed
1370 public function testCompleteTransactionMembershipPriceSet() {
1371 $this->createPriceSetWithPage('membership');
1372 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1373 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1374 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1375 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1376 $this->cleanUpAfterPriceSets();
1380 * Test membership is renewed when transaction completed
1382 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1383 $this->createPriceSetWithPage('membership');
1384 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
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 + 2 years')), $membership['end_date']);
1388 $this->cleanUpAfterPriceSets();
1391 public function cleanUpAfterPriceSets() {
1392 $this->quickCleanUpFinancialEntities();
1393 $this->contactDelete($this->_ids
['contact']);
1394 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1399 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1400 * on parent class at some point (fn is not in 4.4)
1402 * @param array $params
1404 public function createPriceSetWithPage($entity, $params = array()) {
1405 $membershipTypeID = $this->membershipTypeCreate();
1406 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1407 'title' => "Test Contribution Page",
1408 'financial_type_id' => 1,
1409 'currency' => 'NZD',
1410 'goal_amount' => 50,
1411 'is_pay_later' => 1,
1412 'is_monetary' => TRUE,
1413 'is_email_receipt' => FALSE,
1415 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1416 'is_quick_config' => 0,
1417 'extends' => 'CiviMember',
1418 'financial_type_id' => 1,
1419 'title' => 'my Page',
1421 $priceSetID = $priceSet['id'];
1423 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1424 $priceField = $this->callAPISuccess('price_field', 'create', array(
1425 'price_set_id' => $priceSetID,
1426 'label' => 'Goat Breed',
1427 'html_type' => 'Radio',
1429 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1430 'price_set_id' => $priceSetID,
1431 'price_field_id' => $priceField['id'],
1432 'label' => 'Long Haired Goat',
1434 'membership_type_id' => $membershipTypeID,
1435 'membership_num_terms' => 1,
1438 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1439 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1440 'price_set_id' => $priceSetID,
1441 'price_field_id' => $priceField['id'],
1442 'label' => 'Shoe-eating Goat',
1444 'membership_type_id' => $membershipTypeID,
1445 'membership_num_terms' => 2,
1448 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1449 $this->_ids
['price_set'] = $priceSetID;
1450 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1451 $this->_ids
['price_field'] = array($priceField['id']);
1453 $this->_ids
['membership_type'] = $membershipTypeID;
1457 * Set up a pending transaction with a specific price field id
1458 * @param int $priceFieldValueID
1460 public function setUpPendingContribution($priceFieldValueID) {
1461 $contactID = $this->individualCreate();
1462 $membership = $this->callAPISuccess('membership', 'create', array(
1463 'contact_id' => $contactID,
1464 'membership_type_id' => $this->_ids
['membership_type'],
1465 'start_date' => 'yesterday - 1 year',
1466 'end_date' => 'yesterday',
1468 $contribution = $this->callAPISuccess('contribution', 'create', array(
1470 'contact_id' => $contactID,
1471 'receive_date' => date('Ymd'),
1472 'total_amount' => 100.00,
1473 'financial_type_id' => 1,
1474 'payment_instrument_id' => 'Credit Card',
1475 'non_deductible_amount' => 10.00,
1476 'trxn_id' => 'jdhfi88',
1477 'invoice_id' => 'djfhiewuyr',
1479 'contribution_status_id' => 2,
1480 'contribution_page_id' => $this->_ids
['contribution_page'],
1481 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1484 $this->callAPISuccess('line_item', 'create', array(
1485 'entity_id' => $contribution['id'],
1486 'entity_table' => 'civicrm_contribution',
1487 'contribution_id' => $contribution['id'],
1488 'price_field_id' => $this->_ids
['price_field'][0],
1492 'financial_type_id' => 1,
1493 'price_field_value_id' => $priceFieldValueID,
1495 $this->_ids
['contact'] = $contactID;
1496 $this->_ids
['contribution'] = $contribution['id'];
1497 $this->_ids
['membership'] = $membership['id'];
1501 * Test sending a mail via the API
1503 public function testSendMail() {
1504 $mut = new CiviMailUtils($this, TRUE);
1505 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1506 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1507 'id' => $contribution['id'],
1508 'receipt_from_email' => 'api@civicrm.org',
1511 $mut->checkMailLog(array(
1513 'Contribution Information',
1514 'Please print this confirmation for your records',
1523 * Test sending a mail via the API
1525 public function testSendMailEvent() {
1526 $mut = new CiviMailUtils($this, TRUE);
1527 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1528 $event = $this->eventCreate(array(
1529 'is_email_confirm' => 1,
1530 'confirm_from_email' => 'test@civicrm.org',
1532 $this->_eventID
= $event['id'];
1533 $participantParams = array(
1534 'contact_id' => $this->_individualId
,
1535 'event_id' => $this->_eventID
,
1538 // to ensure it matches later on
1539 'register_date' => '2007-07-21 00:00:00',
1540 'source' => 'Online Event Registration: API Testing',
1543 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1544 $this->callAPISuccess('participant_payment', 'create', array(
1545 'participant_id' => $participant['id'],
1546 'contribution_id' => $contribution['id'],
1548 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1549 'id' => $contribution['id'],
1550 'receipt_from_email' => 'api@civicrm.org',
1554 $mut->checkMailLog(array(
1555 'Annual CiviCRM meet',
1557 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1564 * This function does a GET & compares the result against the $params
1565 * Use as a double check on Creates
1568 * @param int $delete
1570 public function contributionGetnCheck($params, $id, $delete = 1) {
1572 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1578 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1580 $this->assertAPISuccess($contribution, 0);
1581 $values = $contribution['values'][$contribution['id']];
1582 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1583 // this is not returned in id format
1584 unset($params['payment_instrument_id']);
1585 $params['contribution_source'] = $params['source'];
1586 unset($params['source']);
1587 foreach ($params as $key => $value) {
1588 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1593 * Create a pending contribution & linked pending participant record
1594 * (along with an event)
1596 public function createPendingParticipantContribution() {
1597 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1598 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1599 $this->_ids
['participant'] = $participantID;
1600 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1601 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1602 $this->callAPISuccess('participant_payment', 'create', array(
1603 'contribution_id' => $contribution['id'],
1604 'participant_id' => $participantID,
1606 $this->callAPISuccess('line_item', 'get', array(
1607 'entity_id' => $contribution['id'],
1608 'entity_table' => 'civicrm_contribution',
1609 'api.line_item.create' => array(
1610 'entity_id' => $participantID,
1611 'entity_table' => 'civicrm_participant',
1614 return $contribution['id'];
1618 * @param int $contId
1620 * @return null|string
1622 function _getFinancialTrxnAmount($contId) {
1624 SUM( ft.total_amount ) AS total
1625 FROM civicrm_financial_trxn AS ft
1626 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1627 WHERE ceft.entity_table = 'civicrm_contribution'
1628 AND ceft.entity_id = {$contId}";
1630 $result = CRM_Core_DAO
::singleValueQuery($query);
1635 * @param int $contId
1637 * @return null|string
1639 function _getFinancialItemAmount($contId) {
1640 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1643 FROM civicrm_financial_item
1644 WHERE entity_table = 'civicrm_line_item'
1645 AND entity_id = {$lineItem}";
1646 $result = CRM_Core_DAO
::singleValueQuery($query);
1651 * @param int $contId
1654 public function _checkFinancialItem($contId, $context) {
1655 if ($context != 'paylater') {
1657 'entity_id' => $contId,
1658 'entity_table' => 'civicrm_contribution',
1660 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1661 $entityParams = array(
1662 'financial_trxn_id' => $trxn['financial_trxn_id'],
1663 'entity_table' => 'civicrm_financial_item',
1665 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1667 'id' => $entityTrxn['entity_id'],
1670 if ($context == 'paylater') {
1671 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1672 foreach ($lineItems as $key => $item) {
1674 'entity_id' => $key,
1675 'entity_table' => 'civicrm_line_item',
1677 $compareParams = array('status_id' => 1);
1678 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1681 elseif ($context == 'refund') {
1682 $compareParams = array(
1684 'financial_account_id' => 1,
1688 elseif ($context == 'cancelPending') {
1689 $compareParams = array(
1691 'financial_account_id' => 1,
1695 elseif ($context == 'changeFinancial') {
1696 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1698 'entity_id' => $lineKey,
1701 $compareParams = array(
1702 'financial_account_id' => 1,
1704 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1706 'financial_account_id' => 3,
1707 'entity_id' => $lineKey,
1709 $compareParams = array(
1713 if ($context != 'paylater') {
1714 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1719 * @param $contribution
1721 * @param int $instrumentId
1723 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1724 $trxnParams = array(
1725 'entity_id' => $contribution['id'],
1726 'entity_table' => 'civicrm_contribution',
1728 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1730 'id' => $trxn['financial_trxn_id'],
1732 if ($context == 'payLater') {
1733 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1734 $compareParams = array(
1736 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1739 elseif ($context == 'refund') {
1740 $compareParams = array(
1741 'to_financial_account_id' => 6,
1742 'total_amount' => -100,
1746 elseif ($context == 'cancelPending') {
1747 $compareParams = array(
1748 'from_financial_account_id' => 7,
1749 'total_amount' => -100,
1753 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1754 $entityParams = array(
1755 'entity_id' => $contribution['id'],
1756 'entity_table' => 'civicrm_contribution',
1759 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1760 $trxnParams1 = array(
1761 'id' => $trxn['financial_trxn_id'],
1763 $compareParams = array(
1764 'total_amount' => -100,
1767 if ($context == 'paymentInstrument') {
1768 $compareParams +
= array(
1769 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1770 'payment_instrument_id' => 4,
1774 $compareParams['to_financial_account_id'] = 12;
1776 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1777 $compareParams['total_amount'] = 100;
1778 if ($context == 'paymentInstrument') {
1779 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1780 $compareParams['payment_instrument_id'] = $instrumentId;
1783 $compareParams['to_financial_account_id'] = 12;
1787 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1793 public function _addPaymentInstrument() {
1794 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1795 $optionParams = array(
1796 'option_group_id' => $gId,
1797 'label' => 'Test Card',
1798 'name' => 'Test Card',
1803 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1804 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1805 $financialParams = array(
1806 'entity_table' => 'civicrm_option_value',
1807 'entity_id' => $optionValue['id'],
1808 'account_relationship' => $relationTypeId,
1809 'financial_account_id' => 7,
1811 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1812 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1813 return $optionValue['values'][$optionValue['id']]['value'];
1817 * @param array $params
1820 public function _checkFinancialRecords($params, $context) {
1821 $entityParams = array(
1822 'entity_id' => $params['id'],
1823 'entity_table' => 'civicrm_contribution',
1825 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1826 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1827 if ($context == 'pending') {
1828 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1829 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1832 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1833 $trxnParams = array(
1834 'id' => $trxn['financial_trxn_id'],
1836 if ($context != 'online' && $context != 'payLater') {
1837 $compareParams = array(
1838 'to_financial_account_id' => 6,
1839 'total_amount' => 100,
1843 if ($context == 'feeAmount') {
1844 $compareParams['fee_amount'] = 50;
1846 elseif ($context == 'online') {
1847 $compareParams = array(
1848 'to_financial_account_id' => 12,
1849 'total_amount' => 100,
1853 elseif ($context == 'payLater') {
1854 $compareParams = array(
1855 'to_financial_account_id' => 7,
1856 'total_amount' => 100,
1860 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1861 $entityParams = array(
1862 'financial_trxn_id' => $trxn['financial_trxn_id'],
1863 'entity_table' => 'civicrm_financial_item',
1865 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1866 $fitemParams = array(
1867 'id' => $entityTrxn['entity_id'],
1869 $compareParams = array(
1872 'financial_account_id' => 1,
1874 if ($context == 'payLater') {
1875 $compareParams = array(
1878 'financial_account_id' => 1,
1881 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1882 if ($context == 'feeAmount') {
1884 'entity_id' => $params['id'],
1885 'entity_table' => 'civicrm_contribution',
1887 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1888 $trxnParams = array(
1889 'id' => $maxTrxn['financial_trxn_id'],
1891 $compareParams = array(
1892 'to_financial_account_id' => 5,
1893 'from_financial_account_id' => 6,
1894 'total_amount' => 50,
1897 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1898 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1899 $fitemParams = array(
1900 'entity_id' => $trxnId['financialTrxnId'],
1901 'entity_table' => 'civicrm_financial_trxn',
1903 $compareParams = array(
1906 'financial_account_id' => 5,
1908 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);