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 * Tests that additional financial records are created.
827 * Checks when online contribution with pay later option is created
829 public function testCreateContributionPayLaterOnline() {
830 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
831 $this->_pageParams
['is_pay_later'] = 1;
832 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
833 $this->assertAPISuccess($contributionPage);
835 'contact_id' => $this->_individualId
,
836 'receive_date' => '20120511',
837 'total_amount' => 100.00,
838 'financial_type_id' => 1,
839 'contribution_page_id' => $contributionPage['id'],
842 'invoice_id' => 67890,
844 'contribution_status_id' => 2,
848 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
849 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
850 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
851 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
852 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
853 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
854 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
855 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
856 $this->_checkFinancialRecords($contribution, 'payLater');
860 * Function tests that additional financial records are created when online contribution with pending option
863 public function testCreateContributionPendingOnline() {
864 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
865 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
866 $this->assertAPISuccess($contributionPage);
868 'contact_id' => $this->_individualId
,
869 'receive_date' => '20120511',
870 'total_amount' => 100.00,
871 'financial_type_id' => 1,
872 'contribution_page_id' => $contributionPage['id'],
874 'invoice_id' => 67890,
876 'contribution_status_id' => 2,
879 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
880 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
881 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
882 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
883 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
884 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
885 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
886 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
887 $this->_checkFinancialRecords($contribution, 'pending');
891 * Test that BAO defaults work.
893 public function testCreateBAODefaults() {
894 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
895 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
896 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
897 'id' => $contribution['id'],
898 'api.contribution.delete' => 1,
900 $this->assertEquals(1, $contribution['contribution_status_id']);
901 $this->assertEquals('Check', $contribution['payment_instrument']);
905 * Function tests that line items, financial records are updated when contribution amount is changed
907 public function testCreateUpdateContributionChangeTotal() {
908 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
909 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
911 'entity_id' => $contribution['id'],
912 'entity_table' => 'civicrm_contribution',
914 'return' => 'line_total',
916 $this->assertEquals('100.00', $lineItems);
917 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
918 // Financial trxn SUM = 100 + 5 (fee)
919 $this->assertEquals('105.00', $trxnAmount);
922 'id' => $contribution['id'],
923 'total_amount' => '125',
925 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
927 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
929 'entity_id' => $contribution['id'],
930 'entity_table' => 'civicrm_contribution',
932 'return' => 'line_total',
935 $this->assertEquals('125.00', $lineItems);
936 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
937 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
938 // Financial trxn SUM = 125 + 5 (fee)
939 $this->assertEquals('130.00', $trxnAmount);
940 $this->assertEquals('125.00', $fitemAmount);
944 * Function tests that line items, financial records are updated when pay later contribution is received
946 public function testCreateUpdateContributionPayLater() {
947 $contribParams = array(
948 'contact_id' => $this->_individualId
,
949 'receive_date' => '2012-01-01',
950 'total_amount' => 100.00,
951 'financial_type_id' => $this->_financialTypeId
,
952 'payment_instrument_id' => 1,
953 'contribution_status_id' => 2,
957 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
959 $newParams = array_merge($contribParams, array(
960 'id' => $contribution['id'],
961 'contribution_status_id' => 1,
964 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
965 $contribution = $contribution['values'][$contribution['id']];
966 $this->assertEquals($contribution['contribution_status_id'], '1');
967 $this->_checkFinancialItem($contribution['id'], 'paylater');
968 $this->_checkFinancialTrxn($contribution, 'payLater');
972 * Function tests that financial records are updated when Payment Instrument is changed
974 public function testCreateUpdateContributionPaymentInstrument() {
975 $instrumentId = $this->_addPaymentInstrument();
976 $contribParams = array(
977 'contact_id' => $this->_individualId
,
978 'total_amount' => 100.00,
979 'financial_type_id' => $this->_financialTypeId
,
980 'payment_instrument_id' => 4,
981 'contribution_status_id' => 1,
984 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
986 $newParams = array_merge($contribParams, array(
987 'id' => $contribution['id'],
988 'payment_instrument_id' => $instrumentId,
991 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
992 $this->assertAPISuccess($contribution);
993 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
997 * Function tests that financial records are added when Contribution is Refunded
999 public function testCreateUpdateContributionRefund() {
1000 $contribParams = array(
1001 'contact_id' => $this->_individualId
,
1002 'receive_date' => '2012-01-01',
1003 'total_amount' => 100.00,
1004 'financial_type_id' => $this->_financialTypeId
,
1005 'payment_instrument_id' => 4,
1006 'contribution_status_id' => 1,
1009 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1010 $newParams = array_merge($contribParams, array(
1011 'id' => $contribution['id'],
1012 'contribution_status_id' => 7,
1016 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1017 $this->_checkFinancialTrxn($contribution, 'refund');
1018 $this->_checkFinancialItem($contribution['id'], 'refund');
1022 * Function tests invalid contribution status change
1024 public function testCreateUpdateContributionInValidStatusChange() {
1025 $contribParams = array(
1027 'receive_date' => '2012-01-01',
1028 'total_amount' => 100.00,
1029 'financial_type_id' => 1,
1030 'payment_instrument_id' => 1,
1031 'contribution_status_id' => 1,
1033 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1034 $newParams = array_merge($contribParams, array(
1035 'id' => $contribution['id'],
1036 'contribution_status_id' => 2,
1039 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1044 * Function tests that financial records are added when Pending Contribution is Canceled
1046 public function testCreateUpdateContributionCancelPending() {
1047 $contribParams = array(
1048 'contact_id' => $this->_individualId
,
1049 'receive_date' => '2012-01-01',
1050 'total_amount' => 100.00,
1051 'financial_type_id' => $this->_financialTypeId
,
1052 'payment_instrument_id' => 1,
1053 'contribution_status_id' => 2,
1054 'is_pay_later' => 1,
1057 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1058 $newParams = array_merge($contribParams, array(
1059 'id' => $contribution['id'],
1060 'contribution_status_id' => 3,
1063 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1064 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1065 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1069 * Function tests that financial records are added when Financial Type is Changed
1071 public function testCreateUpdateContributionChangeFinancialType() {
1072 $contribParams = array(
1073 'contact_id' => $this->_individualId
,
1074 'receive_date' => '2012-01-01',
1075 'total_amount' => 100.00,
1076 'financial_type_id' => 1,
1077 'payment_instrument_id' => 1,
1078 'contribution_status_id' => 1,
1081 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1082 $newParams = array_merge($contribParams, array(
1083 'id' => $contribution['id'],
1084 'financial_type_id' => 3,
1087 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1088 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1089 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1093 * Test that update does not change status id CRM-15105
1095 public function testCreateUpdateWithoutChangingPendingStatus() {
1096 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1097 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1098 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1099 'id' => $contribution['id'],
1100 'api.contribution.delete' => 1,
1102 $this->assertEquals(2, $contribution['contribution_status_id']);
1104 //To Update Contribution
1105 //CHANGE: we require the API to do an incremental update
1106 public function testCreateUpdateContribution() {
1108 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1109 $old_params = array(
1110 'contribution_id' => $contributionID,
1113 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1114 //Make sure it came back
1115 $this->assertAPISuccess($original);
1116 $this->assertEquals($original['id'], $contributionID);
1117 //set up list of old params, verify
1119 //This should not be required on update:
1120 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1121 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1122 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1123 $old_source = $original['values'][$contributionID]['contribution_source'];
1125 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1126 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1128 //check against values in CiviUnitTestCase::createContribution()
1129 $this->assertEquals($old_contact_id, $this->_individualId
);
1130 $this->assertEquals($old_fee_amount, 5.00);
1131 $this->assertEquals($old_source, 'SSF');
1132 $this->assertEquals($old_trxn_id, 212355);
1133 $this->assertEquals($old_invoice_id, 'idofsh');
1135 'id' => $contributionID,
1136 'contact_id' => $this->_individualId
,
1137 'total_amount' => 110.00,
1138 'financial_type_id' => $this->_financialTypeId
,
1139 'non_deductible_amount' => 10.00,
1140 'net_amount' => 100.00,
1141 'contribution_status_id' => 1,
1142 'note' => 'Donating for Nobel Cause',
1146 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1148 $new_params = array(
1149 'contribution_id' => $contribution['id'],
1152 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1154 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1155 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1156 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId
);
1157 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1158 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1159 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1160 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1161 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1162 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1163 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1164 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1166 'contribution_id' => $contributionID,
1169 $result = $this->callAPISuccess('contribution', 'delete', $params);
1170 $this->assertAPISuccess($result);
1173 ///////////////// civicrm_contribution_delete methods
1174 public function testDeleteEmptyParamsContribution() {
1176 $this->callAPIFailure('contribution', 'delete', $params);
1179 public function testDeleteParamsNotArrayContribution() {
1180 $params = 'contribution_id= 1';
1181 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1182 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1185 public function testDeleteWrongParamContribution() {
1187 'contribution_source' => 'SSF',
1190 $this->callAPIFailure('contribution', 'delete', $params);
1193 public function testDeleteContribution() {
1195 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1197 'id' => $contributionID,
1199 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1203 * Test civicrm_contribution_search with empty params.
1204 * All available contributions expected.
1206 public function testSearchEmptyParams() {
1210 'contact_id' => $this->_individualId
,
1211 'receive_date' => date('Ymd'),
1212 'total_amount' => 100.00,
1213 'financial_type_id' => $this->_financialTypeId
,
1214 'non_deductible_amount' => 10.00,
1215 'fee_amount' => 5.00,
1216 'net_amount' => 95.00,
1218 'invoice_id' => 78910,
1220 'contribution_status_id' => 1,
1223 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1225 $result = $this->callAPISuccess('contribution', 'get', $params);
1226 // We're taking the first element.
1227 $res = $result['values'][$contribution['id']];
1229 $this->assertEquals($p['contact_id'], $res['contact_id']);
1230 $this->assertEquals($p['total_amount'], $res['total_amount']);
1231 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1232 $this->assertEquals($p['net_amount'], $res['net_amount']);
1233 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1234 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1235 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1236 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1237 $this->assertEquals($p['source'], $res['contribution_source']);
1238 // contribution_status_id = 1 => Completed
1239 $this->assertEquals('Completed', $res['contribution_status']);
1241 $this->contributionDelete($contribution['id']);
1245 * Test civicrm_contribution_search. Success expected.
1247 public function testSearch() {
1249 'contact_id' => $this->_individualId
,
1250 'receive_date' => date('Ymd'),
1251 'total_amount' => 100.00,
1252 'financial_type_id' => $this->_financialTypeId
,
1253 'non_deductible_amount' => 10.00,
1254 'contribution_status_id' => 1,
1257 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1260 'contact_id' => $this->_individualId
,
1261 'receive_date' => date('Ymd'),
1262 'total_amount' => 200.00,
1263 'financial_type_id' => $this->_financialTypeId
,
1264 'non_deductible_amount' => 20.00,
1265 'trxn_id' => 5454565,
1266 'invoice_id' => 1212124,
1267 'fee_amount' => 50.00,
1268 'net_amount' => 60.00,
1269 'contribution_status_id' => 2,
1272 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1275 'contribution_id' => $contribution2['id'],
1278 $result = $this->callAPISuccess('contribution', 'get', $params);
1279 $res = $result['values'][$contribution2['id']];
1281 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1282 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1283 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1284 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1285 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1286 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1287 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1288 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1289 // contribution_status_id = 2 => Pending
1290 $this->assertEquals('Pending', $res['contribution_status']);
1292 $this->contributionDelete($contribution1['id']);
1293 $this->contributionDelete($contribution2['id']);
1297 * Test completing a transaction via the API
1299 * Note that we are creating a logged in user because email goes out from
1302 public function testCompleteTransaction() {
1303 $mut = new CiviMailUtils($this, TRUE);
1304 $this->createLoggedInUser();
1305 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1306 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1307 $this->callAPISuccess('contribution', 'completetransaction', array(
1308 'id' => $contribution['id'],
1310 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1311 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1312 $mut->checkMailLog(array(
1313 'Receipt - Contribution',
1314 'Please print this confirmation for your records.',
1321 * Test completing a transaction via the API
1325 public function testCompleteTransactionWithReceiptDateSet() {
1326 $mut = new CiviMailUtils($this, TRUE);
1327 $this->createLoggedInUser();
1328 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1329 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1330 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1331 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1332 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1333 $mut->checkMailLog(array(
1334 'Receipt - Contribution',
1335 'Please print this confirmation for your records.',
1341 * Test completing a transaction with an event via the API
1343 * Note that we are creating a logged in user because email goes out from
1346 public function testCompleteTransactionWithParticipantRecord() {
1347 $mut = new CiviMailUtils($this, TRUE);
1348 $mut->clearMessages();
1349 $this->createLoggedInUser();
1350 $contributionID = $this->createPendingParticipantContribution();
1351 $this->callAPISuccess('contribution', 'completetransaction', array(
1352 'id' => $contributionID,
1355 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1356 'id' => $this->_ids
['participant'],
1357 'return' => 'participant_status_id',
1359 $this->assertEquals(1, $participantStatus);
1360 $mut->checkMailLog(array(
1361 'Annual CiviCRM meet',
1363 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1369 * Test membership is renewed when transaction completed
1371 public function testCompleteTransactionMembershipPriceSet() {
1372 $this->createPriceSetWithPage('membership');
1373 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1374 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1375 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1376 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1377 $this->cleanUpAfterPriceSets();
1381 * Test membership is renewed when transaction completed
1383 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1384 $this->createPriceSetWithPage('membership');
1385 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1386 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1387 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1388 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1389 $this->cleanUpAfterPriceSets();
1392 public function cleanUpAfterPriceSets() {
1393 $this->quickCleanUpFinancialEntities();
1394 $this->contactDelete($this->_ids
['contact']);
1395 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1400 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1401 * on parent class at some point (fn is not in 4.4)
1403 * @param array $params
1405 public function createPriceSetWithPage($entity, $params = array()) {
1406 $membershipTypeID = $this->membershipTypeCreate();
1407 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1408 'title' => "Test Contribution Page",
1409 'financial_type_id' => 1,
1410 'currency' => 'NZD',
1411 'goal_amount' => 50,
1412 'is_pay_later' => 1,
1413 'is_monetary' => TRUE,
1414 'is_email_receipt' => FALSE,
1416 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1417 'is_quick_config' => 0,
1418 'extends' => 'CiviMember',
1419 'financial_type_id' => 1,
1420 'title' => 'my Page',
1422 $priceSetID = $priceSet['id'];
1424 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1425 $priceField = $this->callAPISuccess('price_field', 'create', array(
1426 'price_set_id' => $priceSetID,
1427 'label' => 'Goat Breed',
1428 'html_type' => 'Radio',
1430 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1431 'price_set_id' => $priceSetID,
1432 'price_field_id' => $priceField['id'],
1433 'label' => 'Long Haired Goat',
1435 'membership_type_id' => $membershipTypeID,
1436 'membership_num_terms' => 1,
1439 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1440 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1441 'price_set_id' => $priceSetID,
1442 'price_field_id' => $priceField['id'],
1443 'label' => 'Shoe-eating Goat',
1445 'membership_type_id' => $membershipTypeID,
1446 'membership_num_terms' => 2,
1449 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1450 $this->_ids
['price_set'] = $priceSetID;
1451 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1452 $this->_ids
['price_field'] = array($priceField['id']);
1454 $this->_ids
['membership_type'] = $membershipTypeID;
1458 * Set up a pending transaction with a specific price field id
1459 * @param int $priceFieldValueID
1461 public function setUpPendingContribution($priceFieldValueID) {
1462 $contactID = $this->individualCreate();
1463 $membership = $this->callAPISuccess('membership', 'create', array(
1464 'contact_id' => $contactID,
1465 'membership_type_id' => $this->_ids
['membership_type'],
1466 'start_date' => 'yesterday - 1 year',
1467 'end_date' => 'yesterday',
1469 $contribution = $this->callAPISuccess('contribution', 'create', array(
1471 'contact_id' => $contactID,
1472 'receive_date' => date('Ymd'),
1473 'total_amount' => 100.00,
1474 'financial_type_id' => 1,
1475 'payment_instrument_id' => 'Credit Card',
1476 'non_deductible_amount' => 10.00,
1477 'trxn_id' => 'jdhfi88',
1478 'invoice_id' => 'djfhiewuyr',
1480 'contribution_status_id' => 2,
1481 'contribution_page_id' => $this->_ids
['contribution_page'],
1482 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1485 $this->callAPISuccess('line_item', 'create', array(
1486 'entity_id' => $contribution['id'],
1487 'entity_table' => 'civicrm_contribution',
1488 'contribution_id' => $contribution['id'],
1489 'price_field_id' => $this->_ids
['price_field'][0],
1493 'financial_type_id' => 1,
1494 'price_field_value_id' => $priceFieldValueID,
1496 $this->_ids
['contact'] = $contactID;
1497 $this->_ids
['contribution'] = $contribution['id'];
1498 $this->_ids
['membership'] = $membership['id'];
1502 * Test sending a mail via the API
1504 public function testSendMail() {
1505 $mut = new CiviMailUtils($this, TRUE);
1506 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1507 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1508 'id' => $contribution['id'],
1509 'receipt_from_email' => 'api@civicrm.org',
1512 $mut->checkMailLog(array(
1514 'Contribution Information',
1515 'Please print this confirmation for your records',
1524 * Test sending a mail via the API
1526 public function testSendMailEvent() {
1527 $mut = new CiviMailUtils($this, TRUE);
1528 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1529 $event = $this->eventCreate(array(
1530 'is_email_confirm' => 1,
1531 'confirm_from_email' => 'test@civicrm.org',
1533 $this->_eventID
= $event['id'];
1534 $participantParams = array(
1535 'contact_id' => $this->_individualId
,
1536 'event_id' => $this->_eventID
,
1539 // to ensure it matches later on
1540 'register_date' => '2007-07-21 00:00:00',
1541 'source' => 'Online Event Registration: API Testing',
1544 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1545 $this->callAPISuccess('participant_payment', 'create', array(
1546 'participant_id' => $participant['id'],
1547 'contribution_id' => $contribution['id'],
1549 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1550 'id' => $contribution['id'],
1551 'receipt_from_email' => 'api@civicrm.org',
1555 $mut->checkMailLog(array(
1556 'Annual CiviCRM meet',
1558 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1565 * This function does a GET & compares the result against the $params
1566 * Use as a double check on Creates
1569 * @param int $delete
1571 public function contributionGetnCheck($params, $id, $delete = 1) {
1573 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1579 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1581 $this->assertAPISuccess($contribution, 0);
1582 $values = $contribution['values'][$contribution['id']];
1583 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1584 // this is not returned in id format
1585 unset($params['payment_instrument_id']);
1586 $params['contribution_source'] = $params['source'];
1587 unset($params['source']);
1588 foreach ($params as $key => $value) {
1589 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1594 * Create a pending contribution & linked pending participant record
1595 * (along with an event)
1597 public function createPendingParticipantContribution() {
1598 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1599 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1600 $this->_ids
['participant'] = $participantID;
1601 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1602 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1603 $this->callAPISuccess('participant_payment', 'create', array(
1604 'contribution_id' => $contribution['id'],
1605 'participant_id' => $participantID,
1607 $this->callAPISuccess('line_item', 'get', array(
1608 'entity_id' => $contribution['id'],
1609 'entity_table' => 'civicrm_contribution',
1610 'api.line_item.create' => array(
1611 'entity_id' => $participantID,
1612 'entity_table' => 'civicrm_participant',
1615 return $contribution['id'];
1619 * @param int $contId
1621 * @return null|string
1623 function _getFinancialTrxnAmount($contId) {
1625 SUM( ft.total_amount ) AS total
1626 FROM civicrm_financial_trxn AS ft
1627 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1628 WHERE ceft.entity_table = 'civicrm_contribution'
1629 AND ceft.entity_id = {$contId}";
1631 $result = CRM_Core_DAO
::singleValueQuery($query);
1636 * @param int $contId
1638 * @return null|string
1640 function _getFinancialItemAmount($contId) {
1641 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1644 FROM civicrm_financial_item
1645 WHERE entity_table = 'civicrm_line_item'
1646 AND entity_id = {$lineItem}";
1647 $result = CRM_Core_DAO
::singleValueQuery($query);
1652 * @param int $contId
1655 public function _checkFinancialItem($contId, $context) {
1656 if ($context != 'paylater') {
1658 'entity_id' => $contId,
1659 'entity_table' => 'civicrm_contribution',
1661 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1662 $entityParams = array(
1663 'financial_trxn_id' => $trxn['financial_trxn_id'],
1664 'entity_table' => 'civicrm_financial_item',
1666 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1668 'id' => $entityTrxn['entity_id'],
1671 if ($context == 'paylater') {
1672 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1673 foreach ($lineItems as $key => $item) {
1675 'entity_id' => $key,
1676 'entity_table' => 'civicrm_line_item',
1678 $compareParams = array('status_id' => 1);
1679 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1682 elseif ($context == 'refund') {
1683 $compareParams = array(
1685 'financial_account_id' => 1,
1689 elseif ($context == 'cancelPending') {
1690 $compareParams = array(
1692 'financial_account_id' => 1,
1696 elseif ($context == 'changeFinancial') {
1697 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1699 'entity_id' => $lineKey,
1702 $compareParams = array(
1703 'financial_account_id' => 1,
1705 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1707 'financial_account_id' => 3,
1708 'entity_id' => $lineKey,
1710 $compareParams = array(
1714 if ($context != 'paylater') {
1715 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1720 * @param $contribution
1722 * @param int $instrumentId
1724 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1725 $trxnParams = array(
1726 'entity_id' => $contribution['id'],
1727 'entity_table' => 'civicrm_contribution',
1729 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1731 'id' => $trxn['financial_trxn_id'],
1733 if ($context == 'payLater') {
1734 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1735 $compareParams = array(
1737 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1740 elseif ($context == 'refund') {
1741 $compareParams = array(
1742 'to_financial_account_id' => 6,
1743 'total_amount' => -100,
1747 elseif ($context == 'cancelPending') {
1748 $compareParams = array(
1749 'from_financial_account_id' => 7,
1750 'total_amount' => -100,
1754 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1755 $entityParams = array(
1756 'entity_id' => $contribution['id'],
1757 'entity_table' => 'civicrm_contribution',
1760 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1761 $trxnParams1 = array(
1762 'id' => $trxn['financial_trxn_id'],
1764 $compareParams = array(
1765 'total_amount' => -100,
1768 if ($context == 'paymentInstrument') {
1769 $compareParams +
= array(
1770 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1771 'payment_instrument_id' => 4,
1775 $compareParams['to_financial_account_id'] = 12;
1777 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1778 $compareParams['total_amount'] = 100;
1779 if ($context == 'paymentInstrument') {
1780 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1781 $compareParams['payment_instrument_id'] = $instrumentId;
1784 $compareParams['to_financial_account_id'] = 12;
1788 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1794 public function _addPaymentInstrument() {
1795 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1796 $optionParams = array(
1797 'option_group_id' => $gId,
1798 'label' => 'Test Card',
1799 'name' => 'Test Card',
1804 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1805 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1806 $financialParams = array(
1807 'entity_table' => 'civicrm_option_value',
1808 'entity_id' => $optionValue['id'],
1809 'account_relationship' => $relationTypeId,
1810 'financial_account_id' => 7,
1812 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1813 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1814 return $optionValue['values'][$optionValue['id']]['value'];
1818 * @param array $params
1821 public function _checkFinancialRecords($params, $context) {
1822 $entityParams = array(
1823 'entity_id' => $params['id'],
1824 'entity_table' => 'civicrm_contribution',
1826 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1827 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1828 if ($context == 'pending') {
1829 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1830 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1833 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1834 $trxnParams = array(
1835 'id' => $trxn['financial_trxn_id'],
1837 if ($context != 'online' && $context != 'payLater') {
1838 $compareParams = array(
1839 'to_financial_account_id' => 6,
1840 'total_amount' => 100,
1844 if ($context == 'feeAmount') {
1845 $compareParams['fee_amount'] = 50;
1847 elseif ($context == 'online') {
1848 $compareParams = array(
1849 'to_financial_account_id' => 12,
1850 'total_amount' => 100,
1854 elseif ($context == 'payLater') {
1855 $compareParams = array(
1856 'to_financial_account_id' => 7,
1857 'total_amount' => 100,
1861 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1862 $entityParams = array(
1863 'financial_trxn_id' => $trxn['financial_trxn_id'],
1864 'entity_table' => 'civicrm_financial_item',
1866 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1867 $fitemParams = array(
1868 'id' => $entityTrxn['entity_id'],
1870 $compareParams = array(
1873 'financial_account_id' => 1,
1875 if ($context == 'payLater') {
1876 $compareParams = array(
1879 'financial_account_id' => 1,
1882 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1883 if ($context == 'feeAmount') {
1885 'entity_id' => $params['id'],
1886 'entity_table' => 'civicrm_contribution',
1888 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1889 $trxnParams = array(
1890 'id' => $maxTrxn['financial_trxn_id'],
1892 $compareParams = array(
1893 'to_financial_account_id' => 5,
1894 'from_financial_account_id' => 6,
1895 'total_amount' => 50,
1898 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1899 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1900 $fitemParams = array(
1901 'entity_id' => $trxnId['financialTrxnId'],
1902 'entity_table' => 'civicrm_financial_trxn',
1904 $compareParams = array(
1907 'financial_account_id' => 5,
1909 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);