3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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();
54 * Parameters to create payment processor.
58 protected $_processorParams = array();
61 * ID of created event.
70 public function setUp() {
73 $this->_apiversion
= 3;
74 $this->_individualId
= $this->individualCreate();
75 $paymentProcessor = $this->processorCreate();
76 $this->_params
= array(
77 'contact_id' => $this->_individualId
,
78 'receive_date' => '20120511',
79 'total_amount' => 100.00,
80 'financial_type_id' => $this->_financialTypeId
,
81 'non_deductible_amount' => 10.00,
83 'net_amount' => 95.00,
85 'contribution_status_id' => 1,
87 $this->_processorParams
= array(
90 'payment_processor_type_id' => 10,
91 'financial_account_id' => 12,
94 'url_site' => 'http://dummy.com',
95 'url_recur' => 'http://dummy.com',
98 $this->_pageParams
= array(
99 'title' => 'Test Contribution Page',
100 'financial_type_id' => 1,
102 'financial_account_id' => 1,
103 'payment_processor' => $paymentProcessor->id
,
105 'is_allow_other_amount' => 1,
107 'max_amount' => 1000,
112 * Clean up after each test.
114 public function tearDown() {
115 $this->quickCleanUpFinancialEntities();
116 $this->quickCleanup(array('civicrm_uf_match'));
122 public function testGetContribution() {
124 'contact_id' => $this->_individualId
,
125 'receive_date' => '2010-01-20',
126 'total_amount' => 100.00,
127 'financial_type_id' => $this->_financialTypeId
,
128 'non_deductible_amount' => 10.00,
129 'fee_amount' => 5.00,
130 'net_amount' => 95.00,
132 'invoice_id' => 78910,
134 'contribution_status_id' => 1,
136 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
139 'contribution_id' => $this->_contribution
['id'],
142 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
143 $financialParams['id'] = $this->_financialTypeId
;
145 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
147 $this->assertEquals(1, $contribution['count']);
148 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
149 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
150 // Passing back a string rather than an id seems like an error/cruft.
151 // If it is to be introduced we should discuss.
152 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
153 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
154 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
155 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
156 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
157 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
158 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
159 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
160 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
161 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
162 $p['trxn_id'] = '3847';
163 $p['invoice_id'] = '3847';
165 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
167 // Now we have 2 - test getcount.
168 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
169 $this->assertEquals(2, $contribution);
170 // Test id only format.
171 $contribution = $this->callAPISuccess('contribution', 'get', array(
172 'id' => $this->_contribution
['id'],
173 'format.only_id' => 1,
175 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
176 // Test id only format.
177 $contribution = $this->callAPISuccess('contribution', 'get', array(
178 'id' => $contribution2['id'],
179 'format.only_id' => 1,
181 $this->assertEquals($contribution2['id'], $contribution);
183 $contribution = $this->callAPISuccess('contribution', 'get', array(
184 'id' => $this->_contribution
['id'],
186 $this->assertEquals(1, $contribution['count']);
188 // Test get by contact id works.
189 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
191 $this->assertEquals(2, $contribution['count']);
192 $this->callAPISuccess('Contribution', 'Delete', array(
193 'id' => $this->_contribution
['id'],
195 $this->callAPISuccess('Contribution', 'Delete', array(
196 'id' => $contribution2['id'],
201 * Test that test contributions can be retrieved.
203 public function testGetTestContribution() {
204 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('is_test' => 1)));
205 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
209 * We need to ensure previous tested behaviour still works as part of the api contract.
211 public function testGetContributionLegacyBehaviour() {
213 'contact_id' => $this->_individualId
,
214 'receive_date' => '2010-01-20',
215 'total_amount' => 100.00,
216 'contribution_type_id' => $this->_financialTypeId
,
217 'non_deductible_amount' => 10.00,
218 'fee_amount' => 5.00,
219 'net_amount' => 95.00,
221 'invoice_id' => 78910,
223 'contribution_status_id' => 1,
225 $this->_contribution
= $this->callAPISuccess('Contribution', 'create', $p);
228 'contribution_id' => $this->_contribution
['id'],
230 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
231 $financialParams['id'] = $this->_financialTypeId
;
233 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
235 $this->assertEquals(1, $contribution['count']);
236 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
237 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
238 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
239 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
240 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
241 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
242 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
243 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
244 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
245 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
246 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
248 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
249 $p['trxn_id'] = '3847';
250 $p['invoice_id'] = '3847';
252 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
254 // now we have 2 - test getcount
255 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
256 $this->assertEquals(2, $contribution);
257 //test id only format
258 $contribution = $this->callAPISuccess('contribution', 'get', array(
259 'id' => $this->_contribution
['id'],
260 'format.only_id' => 1,
262 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE));
263 //test id only format
264 $contribution = $this->callAPISuccess('contribution', 'get', array(
265 'id' => $contribution2['id'],
266 'format.only_id' => 1,
268 $this->assertEquals($contribution2['id'], $contribution);
269 $contribution = $this->callAPISuccess('contribution', 'get', array(
270 'id' => $this->_contribution
['id'],
273 $this->assertEquals(1, $contribution['count']);
274 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
275 //test get by contact id works
276 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
278 $this->assertEquals(2, $contribution['count']);
279 $this->callAPISuccess('Contribution', 'Delete', array(
280 'id' => $this->_contribution
['id'],
282 $this->callAPISuccess('Contribution', 'Delete', array(
283 'id' => $contribution2['id'],
288 * Create an contribution_id=FALSE and financial_type_id=Donation.
290 public function testCreateEmptyContributionIDUseDonation() {
292 'contribution_id' => FALSE,
295 'check_permissions' => FALSE,
296 'financial_type_id' => 'Donation',
298 $this->callAPISuccess('contribution', 'create', $params);
302 * Check with complete array + custom field.
304 * Note that the test is written on purpose without any
305 * variables specific to participant so it can be replicated into other entities
306 * and / or moved to the automated test suite
308 public function testCreateWithCustom() {
309 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
311 $params = $this->_params
;
312 $params['custom_' . $ids['custom_field_id']] = "custom string";
314 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
315 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
316 $check = $this->callAPISuccess($this->_entity
, 'get', array(
317 'return.custom_' . $ids['custom_field_id'] => 1,
318 'id' => $result['id'],
320 $this->customFieldDelete($ids['custom_field_id']);
321 $this->customGroupDelete($ids['custom_group_id']);
322 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
326 * Check with complete array + custom field.
328 * Note that the test is written on purpose without any
329 * variables specific to participant so it can be replicated into other entities
330 * and / or moved to the automated test suite
332 public function testCreateGetFieldsWithCustom() {
333 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
334 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
335 $result = $this->callAPISuccess('Contribution', 'getfields', array());
336 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
337 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
338 $this->customFieldDelete($ids['custom_field_id']);
339 $this->customGroupDelete($ids['custom_group_id']);
340 $this->customFieldDelete($idsContact['custom_field_id']);
341 $this->customGroupDelete($idsContact['custom_group_id']);
344 public function testCreateContributionNoLineItems() {
347 'contact_id' => $this->_individualId
,
348 'receive_date' => '20120511',
349 'total_amount' => 100.00,
350 'financial_type_id' => $this->_financialTypeId
,
351 'payment_instrument_id' => 1,
352 'non_deductible_amount' => 10.00,
353 'fee_amount' => 50.00,
354 'net_amount' => 90.00,
356 'invoice_id' => 67890,
358 'contribution_status_id' => 1,
362 $contribution = $this->callAPISuccess('contribution', 'create', $params);
363 $lineItems = $this->callAPISuccess('line_item', 'get', array(
364 'entity_id' => $contribution['id'],
365 'entity_table' => 'civicrm_contribution',
368 $this->assertEquals(0, $lineItems['count']);
372 * Test checks that passing in line items suppresses the create mechanism.
374 public function testCreateContributionChainedLineItems() {
376 'contact_id' => $this->_individualId
,
377 'receive_date' => '20120511',
378 'total_amount' => 100.00,
379 'financial_type_id' => $this->_financialTypeId
,
380 'payment_instrument_id' => 1,
381 'non_deductible_amount' => 10.00,
382 'fee_amount' => 50.00,
383 'net_amount' => 90.00,
385 'invoice_id' => 67890,
387 'contribution_status_id' => 1,
389 'api.line_item.create' => array(
391 'price_field_id' => 1,
393 'line_total' => '20',
394 'unit_price' => '10',
397 'price_field_id' => 1,
399 'line_total' => '80',
400 'unit_price' => '80',
405 $description = "Create Contribution with Nested Line Items.";
406 $subfile = "CreateWithNestedLineItems";
407 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
409 $lineItems = $this->callAPISuccess('line_item', 'get', array(
410 'entity_id' => $contribution['id'],
411 'contribution_id' => $contribution['id'],
412 'entity_table' => 'civicrm_contribution',
415 $this->assertEquals(2, $lineItems['count']);
418 public function testCreateContributionOffline() {
420 'contact_id' => $this->_individualId
,
421 'receive_date' => '20120511',
422 'total_amount' => 100.00,
423 'financial_type_id' => 1,
425 'invoice_id' => 67890,
427 'contribution_status_id' => 1,
430 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
431 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
432 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
433 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
434 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
435 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
436 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
437 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
438 $lineItems = $this->callAPISuccess('line_item', 'get', array(
439 'entity_id' => $contribution['id'],
440 'contribution_id' => $contribution['id'],
441 'entity_table' => 'civicrm_contribution',
444 $this->assertEquals(1, $lineItems['count']);
445 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
446 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
447 $this->_checkFinancialRecords($contribution, 'offline');
448 $this->contributionGetnCheck($params, $contribution['id']);
452 * Test create with valid payment instrument.
454 public function testCreateContributionWithPaymentInstrument() {
455 $params = $this->_params +
array('payment_instrument' => 'EFT');
456 $contribution = $this->callAPISuccess('contribution', 'create', $params);
457 $contribution = $this->callAPISuccess('contribution', 'get', array(
459 'id' => $contribution['id'],
461 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
462 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
464 $this->callAPISuccess('contribution', 'create', array(
465 'id' => $contribution['id'],
466 'payment_instrument' => 'Credit Card',
468 $contribution = $this->callAPISuccess('contribution', 'get', array(
470 'id' => $contribution['id'],
472 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
473 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
476 public function testGetContributionByPaymentInstrument() {
477 $params = $this->_params +
array('payment_instrument' => 'EFT');
478 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
479 $this->callAPISuccess('contribution', 'create', $params);
480 $this->callAPISuccess('contribution', 'create', $params2);
481 $contribution = $this->callAPISuccess('contribution', 'get', array(
483 'contribution_payment_instrument' => 'Cash',
485 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
486 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
487 $this->assertEquals(1, $contribution['count']);
488 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
489 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
490 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
491 $this->assertEquals(1, $contribution['count']);
492 $contribution = $this->callAPISuccess('contribution', 'get', array(
494 'payment_instrument_id' => 5,
496 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
497 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
498 $this->assertEquals(1, $contribution['count']);
499 $contribution = $this->callAPISuccess('contribution', 'get', array(
501 'payment_instrument' => 'EFT',
503 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
504 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
505 $this->assertEquals(1, $contribution['count']);
506 $contribution = $this->callAPISuccess('contribution', 'create', array(
507 'id' => $contribution['id'],
508 'payment_instrument' => 'Credit Card',
510 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
511 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
512 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
513 $this->assertEquals(1, $contribution['count']);
517 * CRM-16227 introduces invoice_id as a parameter.
519 public function testGetContributionByInvoice() {
520 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
, array('invoice_id' => 'curly')));
521 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params
), array('invoice_id' => 'churlish'));
522 $this->callAPISuccessGetCount('Contribution', array(), 2);
523 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
524 // The following don't work. They are the format we are trying to introduce but although the form uses this format
525 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
526 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
527 // come out of convertFormValues
528 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
529 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
530 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
531 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
536 * Create test with unique field name on source.
538 public function testCreateContributionSource() {
541 'contact_id' => $this->_individualId
,
542 'receive_date' => date('Ymd'),
543 'total_amount' => 100.00,
544 'financial_type_id' => $this->_financialTypeId
,
545 'payment_instrument_id' => 1,
546 'non_deductible_amount' => 10.00,
547 'fee_amount' => 50.00,
548 'net_amount' => 90.00,
550 'invoice_id' => 67890,
551 'contribution_source' => 'SSF',
552 'contribution_status_id' => 1,
555 $contribution = $this->callAPISuccess('contribution', 'create', $params);
556 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
557 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
561 * Create test with unique field name on source.
563 public function testCreateDefaultNow() {
565 $params = $this->_params
;
566 unset($params['receive_date']);
568 $contribution = $this->callAPISuccess('contribution', 'create', $params);
569 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
570 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
574 * Create test with unique field name on source.
576 public function testCreateContributionSourceInvalidContact() {
580 'receive_date' => date('Ymd'),
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,
589 'contribution_source' => 'SSF',
590 'contribution_status_id' => 1,
593 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
596 public function testCreateContributionSourceInvalidContContact() {
599 'contribution_contact_id' => 999,
600 'receive_date' => date('Ymd'),
601 'total_amount' => 100.00,
602 'financial_type_id' => $this->_financialTypeId
,
603 'payment_instrument_id' => 1,
604 'non_deductible_amount' => 10.00,
605 'fee_amount' => 50.00,
606 'net_amount' => 90.00,
608 'invoice_id' => 67890,
609 'contribution_source' => 'SSF',
610 'contribution_status_id' => 1,
613 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
617 * Test note created correctly.
619 public function testCreateContributionWithNote() {
620 $description = "Demonstrates creating contribution with Note Entity.";
621 $subfile = "ContributionCreateWithNote";
623 'contact_id' => $this->_individualId
,
624 'receive_date' => '2012-01-01',
625 'total_amount' => 100.00,
626 'financial_type_id' => $this->_financialTypeId
,
627 'payment_instrument_id' => 1,
628 'non_deductible_amount' => 10.00,
629 'fee_amount' => 50.00,
630 'net_amount' => 90.00,
632 'invoice_id' => 67890,
634 'contribution_status_id' => 1,
635 'note' => 'my contribution note',
638 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
639 $result = $this->callAPISuccess('note', 'get', array(
640 'entity_table' => 'civicrm_contribution',
641 'entity_id' => $contribution['id'],
644 $this->assertEquals('my contribution note', $result['values'][0]['note']);
645 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
648 public function testCreateContributionWithNoteUniqueNameAliases() {
650 'contact_id' => $this->_individualId
,
651 'receive_date' => '2012-01-01',
652 'total_amount' => 100.00,
653 'financial_type_id' => $this->_financialTypeId
,
654 'payment_instrument_id' => 1,
655 'non_deductible_amount' => 10.00,
656 'fee_amount' => 50.00,
657 'net_amount' => 90.00,
659 'invoice_id' => 67890,
661 'contribution_status_id' => 1,
662 'contribution_note' => 'my contribution note',
665 $contribution = $this->callAPISuccess('contribution', 'create', $params);
666 $result = $this->callAPISuccess('note', 'get', array(
667 'entity_table' => 'civicrm_contribution',
668 'entity_id' => $contribution['id'],
671 $this->assertEquals('my contribution note', $result['values'][0]['note']);
672 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
676 * This is the test for creating soft credits.
678 public function testCreateContributionWithSoftCredit() {
679 $description = "Demonstrates creating contribution with SoftCredit.";
680 $subfile = "ContributionCreateWithSoftCredit";
681 $contact2 = $this->callAPISuccess('Contact', 'create', array(
682 'display_name' => 'superman',
683 'contact_type' => 'Individual',
686 'contact_id' => $contact2['id'],
688 'soft_credit_type_id' => 3,
691 $params = $this->_params +
array('soft_credit' => array(1 => $softParams));
692 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
693 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
695 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
696 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
697 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
699 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
700 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
703 public function testCreateContributionWithSoftCreditDefaults() {
704 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
705 $subfile = "ContributionCreateWithSoftCreditDefaults";
706 $contact2 = $this->callAPISuccess('Contact', 'create', array(
707 'display_name' => 'superman',
708 'contact_type' => 'Individual',
710 $params = $this->_params +
array(
711 'soft_credit_to' => $contact2['id'],
713 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
714 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
716 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
717 // Default soft credit amount = contribution.total_amount
718 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
719 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
721 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
722 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
725 public function testCreateContributionWithHonoreeContact() {
726 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
727 $subfile = "ContributionCreateWithHonoreeContact";
728 $contact2 = $this->callAPISuccess('Contact', 'create', array(
729 'display_name' => 'superman',
730 'contact_type' => 'Individual',
732 $params = $this->_params +
array(
733 'honor_contact_id' => $contact2['id'],
735 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
736 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
738 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
739 // Default soft credit amount = contribution.total_amount
740 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
741 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
742 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
744 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
745 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
749 * Test using example code.
751 public function testContributionCreateExample() {
752 //make sure at least on page exists since there is a truncate in tear down
753 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
754 require_once 'api/v3/examples/Contribution/Create.php';
755 $result = contribution_create_example();
757 $expectedResult = contribution_create_expectedresult();
758 $this->checkArrayEquals($expectedResult, $result);
759 $this->contributionDelete($id);
763 * Function tests that additional financial records are created when fee amount is recorded.
765 public function testCreateContributionWithFee() {
767 'contact_id' => $this->_individualId
,
768 'receive_date' => '20120511',
769 'total_amount' => 100.00,
771 'financial_type_id' => 1,
773 'invoice_id' => 67890,
775 'contribution_status_id' => 1,
778 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
779 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
780 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
781 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
782 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
783 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
784 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
785 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
786 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
787 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
789 $lineItems = $this->callAPISuccess('line_item', 'get', array(
791 'entity_id' => $contribution['id'],
792 'entity_table' => 'civicrm_contribution',
795 $this->assertEquals(1, $lineItems['count']);
796 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
797 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
798 $lineItems = $this->callAPISuccess('line_item', 'get', array(
800 'entity_id' => $contribution['id'],
801 'contribution_id' => $contribution['id'],
802 'entity_table' => 'civicrm_contribution',
805 $this->assertEquals(1, $lineItems['count']);
806 $this->_checkFinancialRecords($contribution, 'feeAmount');
811 * Function tests that additional financial records are created when online contribution is created.
813 public function testCreateContributionOnline() {
814 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
815 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
816 $this->assertAPISuccess($contributionPage);
818 'contact_id' => $this->_individualId
,
819 'receive_date' => '20120511',
820 'total_amount' => 100.00,
821 'financial_type_id' => 1,
822 'contribution_page_id' => $contributionPage['id'],
823 'payment_processor' => 1,
825 'invoice_id' => 67890,
827 'contribution_status_id' => 1,
831 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
832 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
833 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
834 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
835 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
836 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
837 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
838 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
839 $this->_checkFinancialRecords($contribution, 'online');
843 * Check handling of financial type.
845 * In the interests of removing financial type / contribution type checks from
846 * legacy format function lets test that the api is doing this for us
848 public function testCreateInvalidFinancialType() {
849 $params = $this->_params
;
850 $params['financial_type_id'] = 99999;
851 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
855 * Check handling of financial type.
857 * In the interests of removing financial type / contribution type checks from
858 * legacy format function lets test that the api is doing this for us
860 public function testValidNamedFinancialType() {
861 $params = $this->_params
;
862 $params['financial_type_id'] = 'Donation';
863 $this->callAPISuccess($this->_entity
, 'create', $params);
867 * Tests that additional financial records are created.
869 * Checks when online contribution with pay later option is created
871 public function testCreateContributionPayLaterOnline() {
872 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
873 $this->_pageParams
['is_pay_later'] = 1;
874 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
875 $this->assertAPISuccess($contributionPage);
877 'contact_id' => $this->_individualId
,
878 'receive_date' => '20120511',
879 'total_amount' => 100.00,
880 'financial_type_id' => 1,
881 'contribution_page_id' => $contributionPage['id'],
884 'invoice_id' => 67890,
886 'contribution_status_id' => 2,
890 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
891 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
892 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
893 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
894 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
895 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
896 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
897 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
898 $this->_checkFinancialRecords($contribution, 'payLater');
902 * Function tests that additional financial records are created for online contribution with pending option.
904 public function testCreateContributionPendingOnline() {
905 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
906 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
907 $this->assertAPISuccess($contributionPage);
909 'contact_id' => $this->_individualId
,
910 'receive_date' => '20120511',
911 'total_amount' => 100.00,
912 'financial_type_id' => 1,
913 'contribution_page_id' => $contributionPage['id'],
915 'invoice_id' => 67890,
917 'contribution_status_id' => 2,
920 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
921 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
922 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
923 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
924 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
925 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
926 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
927 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
928 $this->_checkFinancialRecords($contribution, 'pending');
932 * Test that BAO defaults work.
934 public function testCreateBAODefaults() {
935 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
936 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
937 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
938 'id' => $contribution['id'],
939 'api.contribution.delete' => 1,
941 $this->assertEquals(1, $contribution['contribution_status_id']);
942 $this->assertEquals('Check', $contribution['payment_instrument']);
946 * Function tests that line items, financial records are updated when contribution amount is changed.
948 public function testCreateUpdateContributionChangeTotal() {
949 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
950 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
952 'entity_id' => $contribution['id'],
953 'entity_table' => 'civicrm_contribution',
955 'return' => 'line_total',
957 $this->assertEquals('100.00', $lineItems);
958 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
959 // Financial trxn SUM = 100 + 5 (fee)
960 $this->assertEquals('105.00', $trxnAmount);
963 'id' => $contribution['id'],
964 'total_amount' => '125',
966 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
968 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
970 'entity_id' => $contribution['id'],
971 'entity_table' => 'civicrm_contribution',
973 'return' => 'line_total',
976 $this->assertEquals('125.00', $lineItems);
977 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
979 // Financial trxn SUM = 125 + 5 (fee).
980 $this->assertEquals('130.00', $trxnAmount);
981 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
985 * Function tests that line items, financial records are updated when pay later contribution is received.
987 public function testCreateUpdateContributionPayLater() {
988 $contribParams = array(
989 'contact_id' => $this->_individualId
,
990 'receive_date' => '2012-01-01',
991 'total_amount' => 100.00,
992 'financial_type_id' => $this->_financialTypeId
,
993 'payment_instrument_id' => 1,
994 'contribution_status_id' => 2,
998 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1000 $newParams = array_merge($contribParams, array(
1001 'id' => $contribution['id'],
1002 'contribution_status_id' => 1,
1005 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1006 $contribution = $contribution['values'][$contribution['id']];
1007 $this->assertEquals($contribution['contribution_status_id'], '1');
1008 $this->_checkFinancialItem($contribution['id'], 'paylater');
1009 $this->_checkFinancialTrxn($contribution, 'payLater');
1013 * Function tests that financial records are updated when Payment Instrument is changed.
1015 public function testCreateUpdateContributionPaymentInstrument() {
1016 $instrumentId = $this->_addPaymentInstrument();
1017 $contribParams = array(
1018 'contact_id' => $this->_individualId
,
1019 'total_amount' => 100.00,
1020 'financial_type_id' => $this->_financialTypeId
,
1021 'payment_instrument_id' => 4,
1022 'contribution_status_id' => 1,
1025 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1027 $newParams = array_merge($contribParams, array(
1028 'id' => $contribution['id'],
1029 'payment_instrument_id' => $instrumentId,
1032 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1033 $this->assertAPISuccess($contribution);
1034 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1038 * Function tests that financial records are added when Contribution is Refunded.
1040 public function testCreateUpdateContributionRefund() {
1041 $contribParams = array(
1042 'contact_id' => $this->_individualId
,
1043 'receive_date' => '2012-01-01',
1044 'total_amount' => 100.00,
1045 'financial_type_id' => $this->_financialTypeId
,
1046 'payment_instrument_id' => 4,
1047 'contribution_status_id' => 1,
1050 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1051 $newParams = array_merge($contribParams, array(
1052 'id' => $contribution['id'],
1053 'contribution_status_id' => 7,
1057 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1058 $this->_checkFinancialTrxn($contribution, 'refund');
1059 $this->_checkFinancialItem($contribution['id'], 'refund');
1063 * Function tests invalid contribution status change.
1065 public function testCreateUpdateContributionInValidStatusChange() {
1066 $contribParams = array(
1068 'receive_date' => '2012-01-01',
1069 'total_amount' => 100.00,
1070 'financial_type_id' => 1,
1071 'payment_instrument_id' => 1,
1072 'contribution_status_id' => 1,
1074 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1075 $newParams = array_merge($contribParams, array(
1076 'id' => $contribution['id'],
1077 'contribution_status_id' => 2,
1080 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1085 * Function tests that financial records are added when Pending Contribution is Canceled.
1087 public function testCreateUpdateContributionCancelPending() {
1088 $contribParams = array(
1089 'contact_id' => $this->_individualId
,
1090 'receive_date' => '2012-01-01',
1091 'total_amount' => 100.00,
1092 'financial_type_id' => $this->_financialTypeId
,
1093 'payment_instrument_id' => 1,
1094 'contribution_status_id' => 2,
1095 'is_pay_later' => 1,
1098 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1099 $newParams = array_merge($contribParams, array(
1100 'id' => $contribution['id'],
1101 'contribution_status_id' => 3,
1104 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1105 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1106 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1110 * Function tests that financial records are added when Financial Type is Changed.
1112 public function testCreateUpdateContributionChangeFinancialType() {
1113 $contribParams = array(
1114 'contact_id' => $this->_individualId
,
1115 'receive_date' => '2012-01-01',
1116 'total_amount' => 100.00,
1117 'financial_type_id' => 1,
1118 'payment_instrument_id' => 1,
1119 'contribution_status_id' => 1,
1122 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1123 $newParams = array_merge($contribParams, array(
1124 'id' => $contribution['id'],
1125 'financial_type_id' => 3,
1128 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1129 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1130 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1134 * Test that update does not change status id CRM-15105.
1136 public function testCreateUpdateWithoutChangingPendingStatus() {
1137 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1138 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1139 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1140 'id' => $contribution['id'],
1141 'api.contribution.delete' => 1,
1143 $this->assertEquals(2, $contribution['contribution_status_id']);
1147 * Test Updating a Contribution.
1149 * CHANGE: we require the API to do an incremental update
1151 public function testCreateUpdateContribution() {
1153 $contributionID = $this->contributionCreate(array('contact_id' => $this->_individualId
), $this->_financialTypeId
,
1155 $old_params = array(
1156 'contribution_id' => $contributionID,
1159 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1160 // Make sure it came back.
1161 $this->assertAPISuccess($original);
1162 $this->assertEquals($original['id'], $contributionID);
1163 //set up list of old params, verify
1165 //This should not be required on update:
1166 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1167 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1168 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1169 $old_source = $original['values'][$contributionID]['contribution_source'];
1171 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1172 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1174 //check against values in CiviUnitTestCase::createContribution()
1175 $this->assertEquals($old_contact_id, $this->_individualId
);
1176 $this->assertEquals($old_fee_amount, 5.00);
1177 $this->assertEquals($old_source, 'SSF');
1178 $this->assertEquals($old_trxn_id, 212355);
1179 $this->assertEquals($old_invoice_id, 'idofsh');
1181 'id' => $contributionID,
1182 'contact_id' => $this->_individualId
,
1183 'total_amount' => 110.00,
1184 'financial_type_id' => $this->_financialTypeId
,
1185 'non_deductible_amount' => 10.00,
1186 'net_amount' => 100.00,
1187 'contribution_status_id' => 1,
1188 'note' => 'Donating for Nobel Cause',
1192 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1194 $new_params = array(
1195 'contribution_id' => $contribution['id'],
1198 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1200 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1201 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1202 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId
);
1203 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1204 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1205 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1206 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1207 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1208 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1209 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1210 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1212 'contribution_id' => $contributionID,
1215 $result = $this->callAPISuccess('contribution', 'delete', $params);
1216 $this->assertAPISuccess($result);
1219 ///////////////// civicrm_contribution_delete methods
1222 * Attempt (but fail) to delete a contribution without parameters.
1224 public function testDeleteEmptyParamsContribution() {
1226 $this->callAPIFailure('contribution', 'delete', $params);
1229 public function testDeleteParamsNotArrayContribution() {
1230 $params = 'contribution_id= 1';
1231 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1232 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1235 public function testDeleteWrongParamContribution() {
1237 'contribution_source' => 'SSF',
1240 $this->callAPIFailure('contribution', 'delete', $params);
1243 public function testDeleteContribution() {
1245 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1247 'id' => $contributionID,
1249 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1253 * Test civicrm_contribution_search with empty params.
1255 * All available contributions expected.
1257 public function testSearchEmptyParams() {
1261 'contact_id' => $this->_individualId
,
1262 'receive_date' => date('Ymd'),
1263 'total_amount' => 100.00,
1264 'financial_type_id' => $this->_financialTypeId
,
1265 'non_deductible_amount' => 10.00,
1266 'fee_amount' => 5.00,
1267 'net_amount' => 95.00,
1269 'invoice_id' => 78910,
1271 'contribution_status_id' => 1,
1274 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1276 $result = $this->callAPISuccess('contribution', 'get', $params);
1277 // We're taking the first element.
1278 $res = $result['values'][$contribution['id']];
1280 $this->assertEquals($p['contact_id'], $res['contact_id']);
1281 $this->assertEquals($p['total_amount'], $res['total_amount']);
1282 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1283 $this->assertEquals($p['net_amount'], $res['net_amount']);
1284 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1285 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1286 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1287 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1288 $this->assertEquals($p['source'], $res['contribution_source']);
1289 // contribution_status_id = 1 => Completed
1290 $this->assertEquals('Completed', $res['contribution_status']);
1292 $this->contributionDelete($contribution['id']);
1296 * Test civicrm_contribution_search. Success expected.
1298 public function testSearch() {
1300 'contact_id' => $this->_individualId
,
1301 'receive_date' => date('Ymd'),
1302 'total_amount' => 100.00,
1303 'financial_type_id' => $this->_financialTypeId
,
1304 'non_deductible_amount' => 10.00,
1305 'contribution_status_id' => 1,
1308 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1311 'contact_id' => $this->_individualId
,
1312 'receive_date' => date('Ymd'),
1313 'total_amount' => 200.00,
1314 'financial_type_id' => $this->_financialTypeId
,
1315 'non_deductible_amount' => 20.00,
1316 'trxn_id' => 5454565,
1317 'invoice_id' => 1212124,
1318 'fee_amount' => 50.00,
1319 'net_amount' => 60.00,
1320 'contribution_status_id' => 2,
1323 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1326 'contribution_id' => $contribution2['id'],
1329 $result = $this->callAPISuccess('contribution', 'get', $params);
1330 $res = $result['values'][$contribution2['id']];
1332 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1333 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1334 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1335 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1336 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1337 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1338 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1339 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1340 // contribution_status_id = 2 => Pending
1341 $this->assertEquals('Pending', $res['contribution_status']);
1343 $this->contributionDelete($contribution1['id']);
1344 $this->contributionDelete($contribution2['id']);
1348 * Test completing a transaction via the API.
1350 * Note that we are creating a logged in user because email goes out from
1353 public function testCompleteTransaction() {
1354 $mut = new CiviMailUtils($this, TRUE);
1355 $this->createLoggedInUser();
1356 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1357 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1358 $this->callAPISuccess('contribution', 'completetransaction', array(
1359 'id' => $contribution['id'],
1361 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1362 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1363 $mut->checkMailLog(array(
1364 'Receipt - Contribution',
1365 'Please print this confirmation for your records.',
1371 * Test repeat contribution successfully creates line items.
1373 public function testRepeatTransaction() {
1374 $paymentProcessorID = $this->paymentProcessorCreate();
1375 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1376 'contact_id' => $this->_individualId
,
1377 'installments' => '12',
1378 'frequency_interval' => '1',
1380 'contribution_status_id' => 1,
1381 'start_date' => '2012-01-01 00:00:00',
1382 'currency' => 'USD',
1383 'frequency_unit' => 'month',
1384 'payment_processor_id' => $paymentProcessorID,
1386 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1388 array('contribution_recur_id' => $contributionRecur['id']))
1391 $this->callAPISuccess('contribution', 'repeattransaction', array(
1392 'original_contribution_id' => $originalContribution['id'],
1393 'contribution_status_id' => 'Completed',
1394 'trxn_id' => uniqid(),
1396 $lineItemParams = array(
1397 'entity_id' => $originalContribution['id'],
1405 'financial_type_id',
1406 'deductible_amount',
1407 'price_field_value_id',
1411 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1412 'entity_id' => $originalContribution['id'],
1414 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1415 'entity_id' => $originalContribution['id'] +
1,
1417 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1418 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1419 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1421 $this->quickCleanUpFinancialEntities();
1425 * CRM-16397 test appropriate action if total amount has changed for single line items.
1427 public function testRepeatTransactionAlteredAmount() {
1428 $paymentProcessorID = $this->paymentProcessorCreate();
1429 $campaignID = $this->campaignCreate();
1430 $campaignID2 = $this->campaignCreate();
1431 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1432 'contact_id' => $this->_individualId
,
1433 'installments' => '12',
1434 'frequency_interval' => '1',
1436 'contribution_status_id' => 1,
1437 'start_date' => '2012-01-01 00:00:00',
1438 'currency' => 'USD',
1439 'frequency_unit' => 'month',
1440 'payment_processor_id' => $paymentProcessorID,
1442 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1445 'contribution_recur_id' => $contributionRecur['id'],
1446 'campaign_id' => $campaignID,
1450 $this->callAPISuccess('contribution', 'repeattransaction', array(
1451 'original_contribution_id' => $originalContribution['id'],
1452 'contribution_status_id' => 'Completed',
1453 'trxn_id' => uniqid(),
1454 'total_amount' => '400',
1456 'campaign_id' => $campaignID2,
1458 $lineItemParams = array(
1459 'entity_id' => $originalContribution['id'],
1467 'financial_type_id',
1468 'deductible_amount',
1469 'price_field_value_id',
1473 $this->callAPISuccessGetSingle('contribution', array(
1474 'total_amount' => 400,
1475 'campaign_id' => $campaignID2,
1477 'net_amount' => 350,
1479 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1480 'entity_id' => $originalContribution['id'],
1482 $expectedLineItem = array_merge(
1483 $lineItem1['values'][0], array(
1484 'line_total' => '400.00',
1485 'unit_price' => '400.00',
1489 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1490 'entity_id' => $originalContribution['id'] +
1,
1492 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1493 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1494 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1496 $this->quickCleanUpFinancialEntities();
1500 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1502 public function testCompleteTransactionNetAmountOK() {
1503 $this->createLoggedInUser();
1504 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1505 unset($params['net_amount']);
1506 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1507 $this->callAPISuccess('contribution', 'completetransaction', array(
1508 'id' => $contribution['id'],
1510 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1511 $this->assertEquals('Completed', $contribution['contribution_status']);
1512 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1516 * CRM-14151 - Test completing a transaction via the API.
1518 public function testCompleteTransactionWithReceiptDateSet() {
1519 $mut = new CiviMailUtils($this, TRUE);
1520 $this->createLoggedInUser();
1521 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1522 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1523 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1524 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1525 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1526 $mut->checkMailLog(array(
1527 'Receipt - Contribution',
1528 'Please print this confirmation for your records.',
1534 * Test completing a transaction with an event via the API.
1536 * Note that we are creating a logged in user because email goes out from
1539 public function testCompleteTransactionWithParticipantRecord() {
1540 $mut = new CiviMailUtils($this, TRUE);
1541 $mut->clearMessages();
1542 $this->createLoggedInUser();
1543 $contributionID = $this->createPendingParticipantContribution();
1544 $this->callAPISuccess('contribution', 'completetransaction', array(
1545 'id' => $contributionID,
1548 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1549 'id' => $this->_ids
['participant'],
1550 'return' => 'participant_status_id',
1552 $this->assertEquals(1, $participantStatus);
1553 $mut->checkMailLog(array(
1554 'Annual CiviCRM meet',
1556 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1562 * Test membership is renewed when transaction completed.
1564 public function testCompleteTransactionMembershipPriceSet() {
1565 $this->createPriceSetWithPage('membership');
1566 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1567 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1568 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1569 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1570 $this->cleanUpAfterPriceSets();
1574 * Test membership is renewed when transaction completed.
1576 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1577 $this->createPriceSetWithPage('membership');
1578 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1579 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1580 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1581 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1582 $this->cleanUpAfterPriceSets();
1585 public function cleanUpAfterPriceSets() {
1586 $this->quickCleanUpFinancialEntities();
1587 $this->contactDelete($this->_ids
['contact']);
1592 * Create price set with contribution test for test setup.
1594 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1595 * on parent class at some point (fn is not in 4.4).
1598 * @param array $params
1600 public function createPriceSetWithPage($entity, $params = array()) {
1601 $membershipTypeID = $this->membershipTypeCreate();
1602 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1603 'title' => "Test Contribution Page",
1604 'financial_type_id' => 1,
1605 'currency' => 'NZD',
1606 'goal_amount' => 50,
1607 'is_pay_later' => 1,
1608 'is_monetary' => TRUE,
1609 'is_email_receipt' => FALSE,
1611 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1612 'is_quick_config' => 0,
1613 'extends' => 'CiviMember',
1614 'financial_type_id' => 1,
1615 'title' => 'my Page',
1617 $priceSetID = $priceSet['id'];
1619 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1620 $priceField = $this->callAPISuccess('price_field', 'create', array(
1621 'price_set_id' => $priceSetID,
1622 'label' => 'Goat Breed',
1623 'html_type' => 'Radio',
1625 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1626 'price_set_id' => $priceSetID,
1627 'price_field_id' => $priceField['id'],
1628 'label' => 'Long Haired Goat',
1630 'membership_type_id' => $membershipTypeID,
1631 'membership_num_terms' => 1,
1634 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1635 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1636 'price_set_id' => $priceSetID,
1637 'price_field_id' => $priceField['id'],
1638 'label' => 'Shoe-eating Goat',
1640 'membership_type_id' => $membershipTypeID,
1641 'membership_num_terms' => 2,
1644 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1645 $this->_ids
['price_set'] = $priceSetID;
1646 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1647 $this->_ids
['price_field'] = array($priceField['id']);
1649 $this->_ids
['membership_type'] = $membershipTypeID;
1653 * Set up a pending transaction with a specific price field id.
1655 * @param int $priceFieldValueID
1657 public function setUpPendingContribution($priceFieldValueID) {
1658 $contactID = $this->individualCreate();
1659 $membership = $this->callAPISuccess('membership', 'create', array(
1660 'contact_id' => $contactID,
1661 'membership_type_id' => $this->_ids
['membership_type'],
1662 'start_date' => 'yesterday - 1 year',
1663 'end_date' => 'yesterday',
1665 $contribution = $this->callAPISuccess('contribution', 'create', array(
1667 'contact_id' => $contactID,
1668 'receive_date' => date('Ymd'),
1669 'total_amount' => 100.00,
1670 'financial_type_id' => 1,
1671 'payment_instrument_id' => 'Credit Card',
1672 'non_deductible_amount' => 10.00,
1673 'trxn_id' => 'jdhfi88',
1674 'invoice_id' => 'djfhiewuyr',
1676 'contribution_status_id' => 2,
1677 'contribution_page_id' => $this->_ids
['contribution_page'],
1678 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1681 $this->callAPISuccess('line_item', 'create', array(
1682 'entity_id' => $contribution['id'],
1683 'entity_table' => 'civicrm_contribution',
1684 'contribution_id' => $contribution['id'],
1685 'price_field_id' => $this->_ids
['price_field'][0],
1689 'financial_type_id' => 1,
1690 'price_field_value_id' => $priceFieldValueID,
1692 $this->_ids
['contact'] = $contactID;
1693 $this->_ids
['contribution'] = $contribution['id'];
1694 $this->_ids
['membership'] = $membership['id'];
1698 * Test sending a mail via the API.
1700 public function testSendMail() {
1701 $mut = new CiviMailUtils($this, TRUE);
1702 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1703 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1704 'id' => $contribution['id'],
1705 'receipt_from_email' => 'api@civicrm.org',
1708 $mut->checkMailLog(array(
1710 'Contribution Information',
1711 'Please print this confirmation for your records',
1720 * Test sending a mail via the API.
1722 public function testSendMailEvent() {
1723 $mut = new CiviMailUtils($this, TRUE);
1724 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1725 $event = $this->eventCreate(array(
1726 'is_email_confirm' => 1,
1727 'confirm_from_email' => 'test@civicrm.org',
1729 $this->_eventID
= $event['id'];
1730 $participantParams = array(
1731 'contact_id' => $this->_individualId
,
1732 'event_id' => $this->_eventID
,
1735 // to ensure it matches later on
1736 'register_date' => '2007-07-21 00:00:00',
1737 'source' => 'Online Event Registration: API Testing',
1740 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1741 $this->callAPISuccess('participant_payment', 'create', array(
1742 'participant_id' => $participant['id'],
1743 'contribution_id' => $contribution['id'],
1745 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1746 'id' => $contribution['id'],
1747 'receipt_from_email' => 'api@civicrm.org',
1751 $mut->checkMailLog(array(
1752 'Annual CiviCRM meet',
1754 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1761 * This function does a GET & compares the result against the $params.
1763 * Use as a double check on Creates.
1765 * @param array $params
1767 * @param bool $delete
1769 public function contributionGetnCheck($params, $id, $delete = TRUE) {
1771 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1777 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1779 $this->assertAPISuccess($contribution, 0);
1780 $values = $contribution['values'][$contribution['id']];
1781 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1782 // this is not returned in id format
1783 unset($params['payment_instrument_id']);
1784 $params['contribution_source'] = $params['source'];
1785 unset($params['source']);
1786 foreach ($params as $key => $value) {
1787 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1792 * Create a pending contribution & linked pending participant record (along with an event).
1794 public function createPendingParticipantContribution() {
1795 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1796 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1797 $this->_ids
['participant'] = $participantID;
1798 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1799 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1800 $this->callAPISuccess('participant_payment', 'create', array(
1801 'contribution_id' => $contribution['id'],
1802 'participant_id' => $participantID,
1804 $this->callAPISuccess('line_item', 'get', array(
1805 'entity_id' => $contribution['id'],
1806 'entity_table' => 'civicrm_contribution',
1807 'api.line_item.create' => array(
1808 'entity_id' => $participantID,
1809 'entity_table' => 'civicrm_participant',
1812 return $contribution['id'];
1816 * Get financial transaction amount.
1818 * @param int $contId
1820 * @return null|string
1822 public function _getFinancialTrxnAmount($contId) {
1824 SUM( ft.total_amount ) AS total
1825 FROM civicrm_financial_trxn AS ft
1826 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1827 WHERE ceft.entity_table = 'civicrm_contribution'
1828 AND ceft.entity_id = {$contId}";
1830 $result = CRM_Core_DAO
::singleValueQuery($query);
1835 * @param int $contId
1837 * @return null|string
1839 public function _getFinancialItemAmount($contId) {
1840 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1843 FROM civicrm_financial_item
1844 WHERE entity_table = 'civicrm_line_item'
1845 AND entity_id = {$lineItem}";
1846 $result = CRM_Core_DAO
::singleValueQuery($query);
1851 * @param int $contId
1854 public function _checkFinancialItem($contId, $context) {
1855 if ($context != 'paylater') {
1857 'entity_id' => $contId,
1858 'entity_table' => 'civicrm_contribution',
1860 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
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));
1867 'id' => $entityTrxn['entity_id'],
1870 if ($context == 'paylater') {
1871 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1872 foreach ($lineItems as $key => $item) {
1874 'entity_id' => $key,
1875 'entity_table' => 'civicrm_line_item',
1877 $compareParams = array('status_id' => 1);
1878 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1881 elseif ($context == 'refund') {
1882 $compareParams = array(
1884 'financial_account_id' => 1,
1888 elseif ($context == 'cancelPending') {
1889 $compareParams = array(
1891 'financial_account_id' => 1,
1895 elseif ($context == 'changeFinancial') {
1896 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1898 'entity_id' => $lineKey,
1901 $compareParams = array(
1902 'financial_account_id' => 1,
1904 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1906 'financial_account_id' => 3,
1907 'entity_id' => $lineKey,
1909 $compareParams = array(
1913 if ($context != 'paylater') {
1914 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1919 * @param $contribution
1921 * @param int $instrumentId
1923 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1924 $trxnParams = array(
1925 'entity_id' => $contribution['id'],
1926 'entity_table' => 'civicrm_contribution',
1928 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1930 'id' => $trxn['financial_trxn_id'],
1932 if ($context == 'payLater') {
1933 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1934 $compareParams = array(
1936 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1939 elseif ($context == 'refund') {
1940 $compareParams = array(
1941 'to_financial_account_id' => 6,
1942 'total_amount' => -100,
1946 elseif ($context == 'cancelPending') {
1947 $compareParams = array(
1948 'from_financial_account_id' => 7,
1949 'total_amount' => -100,
1953 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1954 $entityParams = array(
1955 'entity_id' => $contribution['id'],
1956 'entity_table' => 'civicrm_contribution',
1959 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1960 $trxnParams1 = array(
1961 'id' => $trxn['financial_trxn_id'],
1963 $compareParams = array(
1964 'total_amount' => -100,
1967 if ($context == 'paymentInstrument') {
1968 $compareParams +
= array(
1969 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1970 'payment_instrument_id' => 4,
1974 $compareParams['to_financial_account_id'] = 12;
1976 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1977 $compareParams['total_amount'] = 100;
1978 if ($context == 'paymentInstrument') {
1979 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1980 $compareParams['payment_instrument_id'] = $instrumentId;
1983 $compareParams['to_financial_account_id'] = 12;
1987 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1993 public function _addPaymentInstrument() {
1994 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1995 $optionParams = array(
1996 'option_group_id' => $gId,
1997 'label' => 'Test Card',
1998 'name' => 'Test Card',
2003 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2004 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2005 $financialParams = array(
2006 'entity_table' => 'civicrm_option_value',
2007 'entity_id' => $optionValue['id'],
2008 'account_relationship' => $relationTypeId,
2009 'financial_account_id' => 7,
2011 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
2012 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2013 return $optionValue['values'][$optionValue['id']]['value'];
2017 * @param array $params
2020 public function _checkFinancialRecords($params, $context) {
2021 $entityParams = array(
2022 'entity_id' => $params['id'],
2023 'entity_table' => 'civicrm_contribution',
2025 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2026 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
2027 if ($context == 'pending') {
2028 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
2029 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2032 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2033 $trxnParams = array(
2034 'id' => $trxn['financial_trxn_id'],
2036 if ($context != 'online' && $context != 'payLater') {
2037 $compareParams = array(
2038 'to_financial_account_id' => 6,
2039 'total_amount' => 100,
2043 if ($context == 'feeAmount') {
2044 $compareParams['fee_amount'] = 50;
2046 elseif ($context == 'online') {
2047 $compareParams = array(
2048 'to_financial_account_id' => 12,
2049 'total_amount' => 100,
2053 elseif ($context == 'payLater') {
2054 $compareParams = array(
2055 'to_financial_account_id' => 7,
2056 'total_amount' => 100,
2060 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2061 $entityParams = array(
2062 'financial_trxn_id' => $trxn['financial_trxn_id'],
2063 'entity_table' => 'civicrm_financial_item',
2065 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
2066 $fitemParams = array(
2067 'id' => $entityTrxn['entity_id'],
2069 $compareParams = array(
2072 'financial_account_id' => 1,
2074 if ($context == 'payLater') {
2075 $compareParams = array(
2078 'financial_account_id' => 1,
2081 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2082 if ($context == 'feeAmount') {
2084 'entity_id' => $params['id'],
2085 'entity_table' => 'civicrm_contribution',
2087 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
2088 $trxnParams = array(
2089 'id' => $maxTrxn['financial_trxn_id'],
2091 $compareParams = array(
2092 'to_financial_account_id' => 5,
2093 'from_financial_account_id' => 6,
2094 'total_amount' => 50,
2097 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
2098 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2099 $fitemParams = array(
2100 'entity_id' => $trxnId['financialTrxnId'],
2101 'entity_table' => 'civicrm_financial_trxn',
2103 $compareParams = array(
2106 'financial_account_id' => 5,
2108 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);