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();
53 public function setUp() {
56 $this->_apiversion
= 3;
57 $this->_individualId
= $this->individualCreate();
58 $paymentProcessor = $this->processorCreate();
59 $this->_params
= array(
60 'contact_id' => $this->_individualId
,
61 'receive_date' => '20120511',
62 'total_amount' => 100.00,
63 'financial_type_id' => $this->_financialTypeId
,
64 'non_deductible_amount' => 10.00,
66 'net_amount' => 95.00,
68 'contribution_status_id' => 1,
70 $this->_processorParams
= array(
73 'payment_processor_type_id' => 10,
74 'financial_account_id' => 12,
77 'url_site' => 'http://dummy.com',
78 'url_recur' => 'http://dummy.com',
81 $this->_pageParams
= array(
82 'title' => 'Test Contribution Page',
83 'financial_type_id' => 1,
85 'financial_account_id' => 1,
86 'payment_processor' => $paymentProcessor->id
,
88 'is_allow_other_amount' => 1,
94 public function tearDown() {
95 $this->quickCleanUpFinancialEntities();
98 public function testGetContribution() {
100 'contact_id' => $this->_individualId
,
101 'receive_date' => '2010-01-20',
102 'total_amount' => 100.00,
103 'financial_type_id' => $this->_financialTypeId
,
104 'non_deductible_amount' => 10.00,
105 'fee_amount' => 5.00,
106 'net_amount' => 95.00,
108 'invoice_id' => 78910,
110 'contribution_status_id' => 1,
112 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
115 'contribution_id' => $this->_contribution
['id'],
118 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
119 $financialParams['id'] = $this->_financialTypeId
;
121 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
123 $this->assertEquals(1, $contribution['count']);
124 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
125 // note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
126 // passing back a string rather than an id seems like an error / cruft - & if it is to be introduced we should discuss
127 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
128 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
129 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
130 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
131 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
132 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
133 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
134 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
135 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
136 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
137 $p['trxn_id'] = '3847';
138 $p['invoice_id'] = '3847';
140 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
142 // now we have 2 - test getcount
143 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
144 $this->assertEquals(2, $contribution);
145 //test id only format
146 $contribution = $this->callAPISuccess('contribution', 'get', array(
147 'id' => $this->_contribution
['id'],
148 'format.only_id' => 1,
150 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE) . " in line " . __LINE__
);
151 //test id only format
152 $contribution = $this->callAPISuccess('contribution', 'get', array(
153 'id' => $contribution2['id'],
154 'format.only_id' => 1,
156 $this->assertEquals($contribution2['id'], $contribution);
158 $contribution = $this->callAPISuccess('contribution', 'get', array(
159 'id' => $this->_contribution
['id'],
161 $this->assertEquals(1, $contribution['count']);
163 //test get by contact id works
164 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
166 $this->assertEquals(2, $contribution['count']);
167 $this->callAPISuccess('Contribution', 'Delete', array(
168 'id' => $this->_contribution
['id'],
170 $this->callAPISuccess('Contribution', 'Delete', array(
171 'id' => $contribution2['id'],
176 * We need to ensure previous tested behaviour still works as part of the api contract
178 public function testGetContributionLegacyBehaviour() {
180 'contact_id' => $this->_individualId
,
181 'receive_date' => '2010-01-20',
182 'total_amount' => 100.00,
183 'contribution_type_id' => $this->_financialTypeId
,
184 'non_deductible_amount' => 10.00,
185 'fee_amount' => 5.00,
186 'net_amount' => 95.00,
188 'invoice_id' => 78910,
190 'contribution_status_id' => 1,
192 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
195 'contribution_id' => $this->_contribution
['id'],
197 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
198 $financialParams['id'] = $this->_financialTypeId
;
200 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
202 $this->assertEquals(1, $contribution['count']);
203 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
204 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
205 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
206 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
207 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
208 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
209 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
210 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
211 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
212 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
213 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
214 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
215 $p['trxn_id'] = '3847';
216 $p['invoice_id'] = '3847';
218 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
220 // now we have 2 - test getcount
221 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
222 $this->assertEquals(2, $contribution);
223 //test id only format
224 $contribution = $this->callAPISuccess('contribution', 'get', array(
225 'id' => $this->_contribution
['id'],
226 'format.only_id' => 1,
228 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution, TRUE) . " in line " . __LINE__
);
229 //test id only format
230 $contribution = $this->callAPISuccess('contribution', 'get', array(
231 'id' => $contribution2['id'],
232 'format.only_id' => 1,
234 $this->assertEquals($contribution2['id'], $contribution);
235 $contribution = $this->callAPISuccess('contribution', 'get', array(
236 'id' => $this->_contribution
['id'],
239 $this->assertEquals(1, $contribution['count']);
240 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
241 //test get by contact id works
242 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
244 $this->assertEquals(2, $contribution['count']);
245 $this->callAPISuccess('Contribution', 'Delete', array(
246 'id' => $this->_contribution
['id'],
248 $this->callAPISuccess('Contribution', 'Delete', array(
249 'id' => $contribution2['id'],
253 ///////////////// civicrm_contribution_
254 public function testCreateEmptyContributionIDUseDonation() {
256 'contribution_id' => FALSE,
259 'check_permissions' => FALSE,
260 'financial_type_id' => 'Donation',
262 $this->callAPISuccess('contribution', 'create', $params);
265 * ensure we continue to support contribution_type_id as part of the api commitment to
267 *///////////////// civicrm_contribution_
269 public function testCreateLegacyBehaviour() {
271 'contribution_id' => FALSE,
274 'check_permissions' => FALSE,
275 'contribution_type_id' => 3,
277 $contribution = $this->callAPISuccess('contribution', 'create', $params);
278 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
279 $this->assertEquals(3, $contribution['financial_type_id']);
283 * Check with complete array + custom field
284 * Note that the test is written on purpose without any
285 * variables specific to participant so it can be replicated into other entities
286 * and / or moved to the automated test suite
288 public function testCreateWithCustom() {
289 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
291 $params = $this->_params
;
292 $params['custom_' . $ids['custom_field_id']] = "custom string";
294 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
295 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
296 $check = $this->callAPISuccess($this->_entity
, 'get', array(
297 'return.custom_' . $ids['custom_field_id'] => 1,
298 'id' => $result['id'],
300 $this->customFieldDelete($ids['custom_field_id']);
301 $this->customGroupDelete($ids['custom_group_id']);
302 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
306 * Check with complete array + custom field
307 * Note that the test is written on purpose without any
308 * variables specific to participant so it can be replicated into other entities
309 * and / or moved to the automated test suite
311 public function testCreateGetFieldsWithCustom() {
312 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
313 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
314 $result = $this->callAPISuccess('Contribution', 'getfields', array());
315 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
316 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
317 $this->customFieldDelete($ids['custom_field_id']);
318 $this->customGroupDelete($ids['custom_group_id']);
319 $this->customFieldDelete($idsContact['custom_field_id']);
320 $this->customGroupDelete($idsContact['custom_group_id']);
323 public function testCreateContributionNoLineItems() {
326 'contact_id' => $this->_individualId
,
327 'receive_date' => '20120511',
328 'total_amount' => 100.00,
329 'financial_type_id' => $this->_financialTypeId
,
330 'payment_instrument_id' => 1,
331 'non_deductible_amount' => 10.00,
332 'fee_amount' => 50.00,
333 'net_amount' => 90.00,
335 'invoice_id' => 67890,
337 'contribution_status_id' => 1,
341 $contribution = $this->callAPISuccess('contribution', 'create', $params);
342 $lineItems = $this->callAPISuccess('line_item', 'get', array(
343 'entity_id' => $contribution['id'],
344 'entity_table' => 'civicrm_contribution',
347 $this->assertEquals(0, $lineItems['count']);
351 * Test checks that passing in line items suppresses the create mechanism
353 public function testCreateContributionChainedLineItems() {
356 'contact_id' => $this->_individualId
,
357 'receive_date' => '20120511',
358 'total_amount' => 100.00,
359 'financial_type_id' => $this->_financialTypeId
,
360 'payment_instrument_id' => 1,
361 'non_deductible_amount' => 10.00,
362 'fee_amount' => 50.00,
363 'net_amount' => 90.00,
365 'invoice_id' => 67890,
367 'contribution_status_id' => 1,
369 'api.line_item.create' => array(
371 'price_field_id' => 1,
373 'line_total' => '20',
374 'unit_price' => '10',
377 'price_field_id' => 1,
379 'line_total' => '80',
380 'unit_price' => '80',
385 $description = "Create Contribution with Nested Line Items";
386 $subfile = "CreateWithNestedLineItems";
387 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
389 $lineItems = $this->callAPISuccess('line_item', 'get', array(
390 'entity_id' => $contribution['id'],
391 'contribution_id' => $contribution['id'],
392 'entity_table' => 'civicrm_contribution',
395 $this->assertEquals(2, $lineItems['count']);
398 public function testCreateContributionOffline() {
400 'contact_id' => $this->_individualId
,
401 'receive_date' => '20120511',
402 'total_amount' => 100.00,
403 'financial_type_id' => 1,
405 'invoice_id' => 67890,
407 'contribution_status_id' => 1,
410 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
411 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
412 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
413 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
414 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
415 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
416 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
417 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
418 $lineItems = $this->callAPISuccess('line_item', 'get', array(
419 'entity_id' => $contribution['id'],
420 'contribution_id' => $contribution['id'],
421 'entity_table' => 'civicrm_contribution',
424 $this->assertEquals(1, $lineItems['count']);
425 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
426 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
427 $this->_checkFinancialRecords($contribution, 'offline');
428 $this->contributionGetnCheck($params, $contribution['id']);
432 * Test create with valid payment instument
434 public function testCreateContributionWithPaymentInstrument() {
435 $params = $this->_params +
array('payment_instrument' => 'EFT');
436 $contribution = $this->callAPISuccess('contribution', 'create', $params);
437 $contribution = $this->callAPISuccess('contribution', 'get', array(
439 'id' => $contribution['id'],
441 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
442 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
444 $this->callAPISuccess('contribution', 'create', array(
445 'id' => $contribution['id'],
446 'payment_instrument' => 'Credit Card',
448 $contribution = $this->callAPISuccess('contribution', 'get', array(
450 'id' => $contribution['id'],
452 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
453 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
456 public function testGetContributionByPaymentInstrument() {
457 $params = $this->_params +
array('payment_instrument' => 'EFT');
458 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
459 $this->callAPISuccess('contribution', 'create', $params);
460 $this->callAPISuccess('contribution', 'create', $params2);
461 $contribution = $this->callAPISuccess('contribution', 'get', array(
463 'contribution_payment_instrument_id' => 'Cash',
465 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
466 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
467 $this->assertEquals(1, $contribution['count']);
468 $contribution = $this->callAPISuccess('contribution', 'get', array(
470 'payment_instrument_id' => 'EFT',
472 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
473 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
474 $this->assertEquals(1, $contribution['count']);
475 $contribution = $this->callAPISuccess('contribution', 'get', array(
477 'payment_instrument_id' => 5,
479 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
480 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
481 $this->assertEquals(1, $contribution['count']);
482 $contribution = $this->callAPISuccess('contribution', 'get', array(
484 'payment_instrument' => 'EFT',
486 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
487 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
488 $this->assertEquals(1, $contribution['count']);
489 $contribution = $this->callAPISuccess('contribution', 'create', array(
490 'id' => $contribution['id'],
491 'payment_instrument' => 'Credit Card',
493 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
494 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
495 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
496 $this->assertEquals(1, $contribution['count']);
500 * Create test with unique field name on source
502 public function testCreateContributionSource() {
505 'contact_id' => $this->_individualId
,
506 'receive_date' => date('Ymd'),
507 'total_amount' => 100.00,
508 'financial_type_id' => $this->_financialTypeId
,
509 'payment_instrument_id' => 1,
510 'non_deductible_amount' => 10.00,
511 'fee_amount' => 50.00,
512 'net_amount' => 90.00,
514 'invoice_id' => 67890,
515 'contribution_source' => 'SSF',
516 'contribution_status_id' => 1,
519 $contribution = $this->callAPISuccess('contribution', 'create', $params);
520 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
521 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
525 * Create test with unique field name on source
527 public function testCreateDefaultNow() {
529 $params = $this->_params
;
530 unset($params['receive_date']);
532 $contribution = $this->callAPISuccess('contribution', 'create', $params);
533 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
534 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
538 * Create test with unique field name on source
540 public function testCreateContributionSourceInvalidContac() {
544 'receive_date' => date('Ymd'),
545 'total_amount' => 100.00,
546 'financial_type_id' => $this->_financialTypeId
,
547 'payment_instrument_id' => 1,
548 'non_deductible_amount' => 10.00,
549 'fee_amount' => 50.00,
550 'net_amount' => 90.00,
552 'invoice_id' => 67890,
553 'contribution_source' => 'SSF',
554 'contribution_status_id' => 1,
557 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
560 public function testCreateContributionSourceInvalidContContact() {
563 'contribution_contact_id' => 999,
564 'receive_date' => date('Ymd'),
565 'total_amount' => 100.00,
566 'financial_type_id' => $this->_financialTypeId
,
567 'payment_instrument_id' => 1,
568 'non_deductible_amount' => 10.00,
569 'fee_amount' => 50.00,
570 'net_amount' => 90.00,
572 'invoice_id' => 67890,
573 'contribution_source' => 'SSF',
574 'contribution_status_id' => 1,
577 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
581 * Test note created correctly
583 public function testCreateContributionWithNote() {
584 $description = "Demonstrates creating contribution with Note Entity";
585 $subfile = "ContributionCreateWithNote";
587 'contact_id' => $this->_individualId
,
588 'receive_date' => '2012-01-01',
589 'total_amount' => 100.00,
590 'financial_type_id' => $this->_financialTypeId
,
591 'payment_instrument_id' => 1,
592 'non_deductible_amount' => 10.00,
593 'fee_amount' => 50.00,
594 'net_amount' => 90.00,
596 'invoice_id' => 67890,
598 'contribution_status_id' => 1,
599 'note' => 'my contribution note',
602 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
603 $result = $this->callAPISuccess('note', 'get', array(
604 'entity_table' => 'civicrm_contribution',
605 'entity_id' => $contribution['id'],
608 $this->assertEquals('my contribution note', $result['values'][0]['note']);
609 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
612 public function testCreateContributionWithNoteUniqueNameAliases() {
614 'contact_id' => $this->_individualId
,
615 'receive_date' => '2012-01-01',
616 'total_amount' => 100.00,
617 'financial_type_id' => $this->_financialTypeId
,
618 'payment_instrument_id' => 1,
619 'non_deductible_amount' => 10.00,
620 'fee_amount' => 50.00,
621 'net_amount' => 90.00,
623 'invoice_id' => 67890,
625 'contribution_status_id' => 1,
626 'contribution_note' => 'my contribution note',
629 $contribution = $this->callAPISuccess('contribution', 'create', $params);
630 $result = $this->callAPISuccess('note', 'get', array(
631 'entity_table' => 'civicrm_contribution',
632 'entity_id' => $contribution['id'],
635 $this->assertEquals('my contribution note', $result['values'][0]['note']);
636 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
640 * This is the test for creating soft credits - however a 'get' is not yet possible via API
641 * as the current BAO functions are contact-centric (from what I can find)
643 public function testCreateContributionWithSoftCredt() {
644 $description = "Demonstrates creating contribution with SoftCredit";
645 $subfile = "ContributionCreateWithSoftCredit";
646 $contact2 = $this->callAPISuccess('Contact', 'create', array(
647 'display_name' => 'superman',
648 'contact_type' => 'Individual',
651 'contact_id' => $contact2['id'],
653 'soft_credit_type_id' => 3,
656 $params = $this->_params +
array('soft_credit' => array(1 => $softparams));
657 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
658 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
660 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
661 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
662 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
664 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
665 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
668 public function testCreateContributionWithSoftCreditDefaults() {
669 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
670 $subfile = "ContributionCreateWithSoftCreditDefaults";
671 $contact2 = $this->callAPISuccess('Contact', 'create', array(
672 'display_name' => 'superman',
673 'contact_type' => 'Individual',
675 $params = $this->_params +
array(
676 'soft_credit_to' => $contact2['id'],
678 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
679 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
681 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
682 // Default soft credit amount = contribution.total_amount
683 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
684 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
686 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
687 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
690 public function testCreateContributionWithHonoreeContact() {
691 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
692 $subfile = "ContributionCreateWithHonoreeContact";
693 $contact2 = $this->callAPISuccess('Contact', 'create', array(
694 'display_name' => 'superman',
695 'contact_type' => 'Individual',
697 $params = $this->_params +
array(
698 'honor_contact_id' => $contact2['id'],
700 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
701 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
703 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
704 // Default soft credit amount = contribution.total_amount
705 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
706 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
707 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
709 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
710 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
714 * Test using example code
716 public function testContributionCreateExample() {
717 //make sure at least on page exists since there is a truncate in tear down
718 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
719 $this->assertAPISuccess($page);
720 require_once 'api/v3/examples/Contribution/Create.php';
721 $result = contribution_create_example();
722 $this->assertAPISuccess($result);
723 $contributionId = $result['id'];
724 $expectedResult = contribution_create_expectedresult();
725 $this->checkArrayEquals($expectedResult, $result);
726 $this->contributionDelete($contributionId);
730 * Function tests that additional financial records are created when fee amount is recorded
732 public function testCreateContributionWithFee() {
734 'contact_id' => $this->_individualId
,
735 'receive_date' => '20120511',
736 'total_amount' => 100.00,
738 'financial_type_id' => 1,
740 'invoice_id' => 67890,
742 'contribution_status_id' => 1,
745 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
746 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
747 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
748 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
749 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
750 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
751 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
752 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
753 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
754 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
756 $lineItems = $this->callAPISuccess('line_item', 'get', array(
758 'entity_id' => $contribution['id'],
759 'entity_table' => 'civicrm_contribution',
762 $this->assertEquals(1, $lineItems['count']);
763 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
764 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
765 $lineItems = $this->callAPISuccess('line_item', 'get', array(
767 'entity_id' => $contribution['id'],
768 'contribution_id' => $contribution['id'],
769 'entity_table' => 'civicrm_contribution',
772 $this->assertEquals(1, $lineItems['count']);
773 $this->_checkFinancialRecords($contribution, 'feeAmount');
778 * Function tests that additional financial records are created when online contribution is created
780 public function testCreateContributionOnline() {
781 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
782 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
783 $this->assertAPISuccess($contributionPage);
785 'contact_id' => $this->_individualId
,
786 'receive_date' => '20120511',
787 'total_amount' => 100.00,
788 'financial_type_id' => 1,
789 'contribution_page_id' => $contributionPage['id'],
790 'payment_processor' => 1,
792 'invoice_id' => 67890,
794 'contribution_status_id' => 1,
798 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
799 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
800 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
801 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
802 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
803 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
804 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
805 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
806 $this->_checkFinancialRecords($contribution, 'online');
810 * In the interests of removing financial type / contribution type checks from
811 * legacy format function lets test that the api is doing this for us
813 public function testCreateInvalidFinancialType() {
814 $params = $this->_params
;
815 $params['financial_type_id'] = 99999;
816 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
820 * In the interests of removing financial type / contribution type checks from
821 * legacy format function lets test that the api is doing this for us
823 public function testValidNamedFinancialType() {
824 $params = $this->_params
;
825 $params['financial_type_id'] = 'Donation';
826 $this->callAPISuccess($this->_entity
, 'create', $params);
830 * Function tests that additional financial records are created when online contribution with pay later option
833 public function testCreateContributionPayLaterOnline() {
834 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
835 $this->_pageParams
['is_pay_later'] = 1;
836 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
837 $this->assertAPISuccess($contributionPage);
839 'contact_id' => $this->_individualId
,
840 'receive_date' => '20120511',
841 'total_amount' => 100.00,
842 'financial_type_id' => 1,
843 'contribution_page_id' => $contributionPage['id'],
846 'invoice_id' => 67890,
848 'contribution_status_id' => 2,
852 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
853 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
854 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
855 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
856 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
857 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
858 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
859 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
860 $this->_checkFinancialRecords($contribution, 'payLater');
864 * Function tests that additional financial records are created when online contribution with pending option
867 public function testCreateContributionPendingOnline() {
868 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
869 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
870 $this->assertAPISuccess($contributionPage);
872 'contact_id' => $this->_individualId
,
873 'receive_date' => '20120511',
874 'total_amount' => 100.00,
875 'financial_type_id' => 1,
876 'contribution_page_id' => $contributionPage['id'],
878 'invoice_id' => 67890,
880 'contribution_status_id' => 2,
883 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
884 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
885 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
886 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
887 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
888 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
889 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
890 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
891 $this->_checkFinancialRecords($contribution, 'pending');
895 * Test that BAO defaults work
897 public function testCreateBAODefaults() {
898 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
899 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
900 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
901 'id' => $contribution['id'],
902 'api.contribution.delete' => 1,
904 $this->assertEquals(1, $contribution['contribution_status_id']);
905 $this->assertEquals('Check', $contribution['payment_instrument']);
909 * Function tests that line items, financial records are updated when contribution amount is changed
911 public function testCreateUpdateContributionChangeTotal() {
912 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
913 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
915 'entity_id' => $contribution['id'],
916 'entity_table' => 'civicrm_contribution',
918 'return' => 'line_total',
920 $this->assertEquals('100.00', $lineItems);
921 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
922 // Financial trxn SUM = 100 + 5 (fee)
923 $this->assertEquals('105.00', $trxnAmount);
926 'id' => $contribution['id'],
927 'total_amount' => '125',
929 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
931 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
933 'entity_id' => $contribution['id'],
934 'entity_table' => 'civicrm_contribution',
936 'return' => 'line_total',
939 $this->assertEquals('125.00', $lineItems);
940 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
941 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
942 // Financial trxn SUM = 125 + 5 (fee)
943 $this->assertEquals('130.00', $trxnAmount);
944 $this->assertEquals('125.00', $fitemAmount);
948 * Function tests that line items, financial records are updated when pay later contribution is received
950 public function testCreateUpdateContributionPayLater() {
951 $contribParams = array(
952 'contact_id' => $this->_individualId
,
953 'receive_date' => '2012-01-01',
954 'total_amount' => 100.00,
955 'financial_type_id' => $this->_financialTypeId
,
956 'payment_instrument_id' => 1,
957 'contribution_status_id' => 2,
961 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
963 $newParams = array_merge($contribParams, array(
964 'id' => $contribution['id'],
965 'contribution_status_id' => 1,
968 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
969 $contribution = $contribution['values'][$contribution['id']];
970 $this->assertEquals($contribution['contribution_status_id'], '1');
971 $this->_checkFinancialItem($contribution['id'], 'paylater');
972 $this->_checkFinancialTrxn($contribution, 'payLater');
976 * Function tests that financial records are updated when Payment Instrument is changed
978 public function testCreateUpdateContributionPaymentInstrument() {
979 $instrumentId = $this->_addPaymentInstrument();
980 $contribParams = array(
981 'contact_id' => $this->_individualId
,
982 'total_amount' => 100.00,
983 'financial_type_id' => $this->_financialTypeId
,
984 'payment_instrument_id' => 4,
985 'contribution_status_id' => 1,
988 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
990 $newParams = array_merge($contribParams, array(
991 'id' => $contribution['id'],
992 'payment_instrument_id' => $instrumentId,
995 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
996 $this->assertAPISuccess($contribution);
997 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1001 * Function tests that financial records are added when Contribution is Refunded
1003 public function testCreateUpdateContributionRefund() {
1004 $contribParams = array(
1005 'contact_id' => $this->_individualId
,
1006 'receive_date' => '2012-01-01',
1007 'total_amount' => 100.00,
1008 'financial_type_id' => $this->_financialTypeId
,
1009 'payment_instrument_id' => 4,
1010 'contribution_status_id' => 1,
1013 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1014 $newParams = array_merge($contribParams, array(
1015 'id' => $contribution['id'],
1016 'contribution_status_id' => 7,
1020 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1021 $this->_checkFinancialTrxn($contribution, 'refund');
1022 $this->_checkFinancialItem($contribution['id'], 'refund');
1026 * Function tests invalid contribution status change
1028 public function testCreateUpdateContributionInValidStatusChange() {
1029 $contribParams = array(
1031 'receive_date' => '2012-01-01',
1032 'total_amount' => 100.00,
1033 'financial_type_id' => 1,
1034 'payment_instrument_id' => 1,
1035 'contribution_status_id' => 1,
1037 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1038 $newParams = array_merge($contribParams, array(
1039 'id' => $contribution['id'],
1040 'contribution_status_id' => 2,
1043 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1048 * Function tests that financial records are added when Pending Contribution is Canceled
1050 public function testCreateUpdateContributionCancelPending() {
1051 $contribParams = array(
1052 'contact_id' => $this->_individualId
,
1053 'receive_date' => '2012-01-01',
1054 'total_amount' => 100.00,
1055 'financial_type_id' => $this->_financialTypeId
,
1056 'payment_instrument_id' => 1,
1057 'contribution_status_id' => 2,
1058 'is_pay_later' => 1,
1061 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1062 $newParams = array_merge($contribParams, array(
1063 'id' => $contribution['id'],
1064 'contribution_status_id' => 3,
1067 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1068 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1069 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1073 * Function tests that financial records are added when Financial Type is Changed
1075 public function testCreateUpdateContributionChangeFinancialType() {
1076 $contribParams = array(
1077 'contact_id' => $this->_individualId
,
1078 'receive_date' => '2012-01-01',
1079 'total_amount' => 100.00,
1080 'financial_type_id' => 1,
1081 'payment_instrument_id' => 1,
1082 'contribution_status_id' => 1,
1085 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1086 $newParams = array_merge($contribParams, array(
1087 'id' => $contribution['id'],
1088 'financial_type_id' => 3,
1091 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1092 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1093 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1097 * Test that update does not change status id CRM-15105
1099 public function testCreateUpdateWithoutChangingPendingStatus() {
1100 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1101 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1102 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1103 'id' => $contribution['id'],
1104 'api.contribution.delete' => 1,
1106 $this->assertEquals(2, $contribution['contribution_status_id']);
1108 //To Update Contribution
1109 //CHANGE: we require the API to do an incremental update
1110 public function testCreateUpdateContribution() {
1112 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1113 $old_params = array(
1114 'contribution_id' => $contributionID,
1117 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1118 //Make sure it came back
1119 $this->assertAPISuccess($original);
1120 $this->assertEquals($original['id'], $contributionID);
1121 //set up list of old params, verify
1123 //This should not be required on update:
1124 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1125 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1126 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1127 $old_source = $original['values'][$contributionID]['contribution_source'];
1129 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1130 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1132 //check against values in CiviUnitTestCase::createContribution()
1133 $this->assertEquals($old_contact_id, $this->_individualId
);
1134 $this->assertEquals($old_fee_amount, 5.00);
1135 $this->assertEquals($old_source, 'SSF');
1136 $this->assertEquals($old_trxn_id, 212355);
1137 $this->assertEquals($old_invoice_id, 'idofsh');
1139 'id' => $contributionID,
1140 'contact_id' => $this->_individualId
,
1141 'total_amount' => 110.00,
1142 'financial_type_id' => $this->_financialTypeId
,
1143 'non_deductible_amount' => 10.00,
1144 'net_amount' => 100.00,
1145 'contribution_status_id' => 1,
1146 'note' => 'Donating for Nobel Cause',
1150 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1152 $new_params = array(
1153 'contribution_id' => $contribution['id'],
1156 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1158 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1159 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1160 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId
);
1161 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1162 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1163 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1164 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1165 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1166 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1167 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1168 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1170 'contribution_id' => $contributionID,
1173 $result = $this->callAPISuccess('contribution', 'delete', $params);
1174 $this->assertAPISuccess($result, 'in line' . __LINE__
);
1177 ///////////////// civicrm_contribution_delete methods
1178 public function testDeleteEmptyParamsContribution() {
1180 $this->callAPIFailure('contribution', 'delete', $params);
1183 public function testDeleteParamsNotArrayContribution() {
1184 $params = 'contribution_id= 1';
1185 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1186 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1189 public function testDeleteWrongParamContribution() {
1191 'contribution_source' => 'SSF',
1194 $this->callAPIFailure('contribution', 'delete', $params);
1197 public function testDeleteContribution() {
1199 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1201 'id' => $contributionID,
1203 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1207 * Test civicrm_contribution_search with empty params.
1208 * All available contributions expected.
1210 public function testSearchEmptyParams() {
1214 'contact_id' => $this->_individualId
,
1215 'receive_date' => date('Ymd'),
1216 'total_amount' => 100.00,
1217 'financial_type_id' => $this->_financialTypeId
,
1218 'non_deductible_amount' => 10.00,
1219 'fee_amount' => 5.00,
1220 'net_amount' => 95.00,
1222 'invoice_id' => 78910,
1224 'contribution_status_id' => 1,
1227 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1229 $result = $this->callAPISuccess('contribution', 'get', $params);
1230 // We're taking the first element.
1231 $res = $result['values'][$contribution['id']];
1233 $this->assertEquals($p['contact_id'], $res['contact_id']);
1234 $this->assertEquals($p['total_amount'], $res['total_amount']);
1235 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1236 $this->assertEquals($p['net_amount'], $res['net_amount']);
1237 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1238 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1239 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1240 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1241 $this->assertEquals($p['source'], $res['contribution_source']);
1242 // contribution_status_id = 1 => Completed
1243 $this->assertEquals('Completed', $res['contribution_status']);
1245 $this->contributionDelete($contribution['id']);
1249 * Test civicrm_contribution_search. Success expected.
1251 public function testSearch() {
1253 'contact_id' => $this->_individualId
,
1254 'receive_date' => date('Ymd'),
1255 'total_amount' => 100.00,
1256 'financial_type_id' => $this->_financialTypeId
,
1257 'non_deductible_amount' => 10.00,
1258 'contribution_status_id' => 1,
1261 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1264 'contact_id' => $this->_individualId
,
1265 'receive_date' => date('Ymd'),
1266 'total_amount' => 200.00,
1267 'financial_type_id' => $this->_financialTypeId
,
1268 'non_deductible_amount' => 20.00,
1269 'trxn_id' => 5454565,
1270 'invoice_id' => 1212124,
1271 'fee_amount' => 50.00,
1272 'net_amount' => 60.00,
1273 'contribution_status_id' => 2,
1276 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1279 'contribution_id' => $contribution2['id'],
1282 $result = $this->callAPISuccess('contribution', 'get', $params);
1283 $res = $result['values'][$contribution2['id']];
1285 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1286 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1287 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1288 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1289 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1290 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1291 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1292 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1293 // contribution_status_id = 2 => Pending
1294 $this->assertEquals('Pending', $res['contribution_status']);
1296 $this->contributionDelete($contribution1['id']);
1297 $this->contributionDelete($contribution2['id']);
1301 * Test completing a transaction via the API
1303 * Note that we are creating a logged in user because email goes out from
1306 public function testCompleteTransaction() {
1307 $mut = new CiviMailUtils($this, TRUE);
1308 $this->createLoggedInUser();
1309 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1310 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1311 $this->callAPISuccess('contribution', 'completetransaction', array(
1312 'id' => $contribution['id'],
1314 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1315 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1316 $mut->checkMailLog(array(
1317 'Receipt - Contribution',
1318 'Please print this confirmation for your records.',
1325 * Test completing a transaction via the API
1329 public function testCompleteTransactionWithReceiptDateSet() {
1330 $mut = new CiviMailUtils($this, TRUE);
1331 $this->createLoggedInUser();
1332 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1333 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1334 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1335 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1336 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1337 $mut->checkMailLog(array(
1338 'Receipt - Contribution',
1339 'Please print this confirmation for your records.',
1345 * Test completing a transaction with an event via the API
1347 * Note that we are creating a logged in user because email goes out from
1350 public function testCompleteTransactionWithParticipantRecord() {
1351 $mut = new CiviMailUtils($this, TRUE);
1352 $mut->clearMessages();
1353 $this->createLoggedInUser();
1354 $contributionID = $this->createPendingParticipantContribution();
1355 $this->callAPISuccess('contribution', 'completetransaction', array(
1356 'id' => $contributionID,
1359 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1360 'id' => $this->_ids
['participant'],
1361 'return' => 'participant_status_id',
1363 $this->assertEquals(1, $participantStatus);
1364 $mut->checkMailLog(array(
1365 'Annual CiviCRM meet',
1367 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1373 * Test membership is renewed when transaction completed
1375 public function testCompleteTransactionMembershipPriceSet() {
1376 $this->createPriceSetWithPage('membership');
1377 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1378 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1379 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1380 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1381 $this->cleanUpAfterPriceSets();
1385 * Test membership is renewed when transaction completed
1387 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1388 $this->createPriceSetWithPage('membership');
1389 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1390 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1391 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1392 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1393 $this->cleanUpAfterPriceSets();
1396 public function cleanUpAfterPriceSets() {
1397 $this->quickCleanUpFinancialEntities();
1398 $this->contactDelete($this->_ids
['contact']);
1399 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1404 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1405 * on parent class at some point (fn is not in 4.4)
1407 * @param array $params
1409 public function createPriceSetWithPage($entity, $params = array()) {
1410 $membershipTypeID = $this->membershipTypeCreate();
1411 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1412 'title' => "Test Contribution Page",
1413 'financial_type_id' => 1,
1414 'currency' => 'NZD',
1415 'goal_amount' => 50,
1416 'is_pay_later' => 1,
1417 'is_monetary' => TRUE,
1418 'is_email_receipt' => FALSE,
1420 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1421 'is_quick_config' => 0,
1422 'extends' => 'CiviMember',
1423 'financial_type_id' => 1,
1424 'title' => 'my Page',
1426 $priceSetID = $priceSet['id'];
1428 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1429 $priceField = $this->callAPISuccess('price_field', 'create', array(
1430 'price_set_id' => $priceSetID,
1431 'label' => 'Goat Breed',
1432 'html_type' => 'Radio',
1434 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1435 'price_set_id' => $priceSetID,
1436 'price_field_id' => $priceField['id'],
1437 'label' => 'Long Haired Goat',
1439 'membership_type_id' => $membershipTypeID,
1440 'membership_num_terms' => 1,
1443 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1444 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1445 'price_set_id' => $priceSetID,
1446 'price_field_id' => $priceField['id'],
1447 'label' => 'Shoe-eating Goat',
1449 'membership_type_id' => $membershipTypeID,
1450 'membership_num_terms' => 2,
1453 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1454 $this->_ids
['price_set'] = $priceSetID;
1455 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1456 $this->_ids
['price_field'] = array($priceField['id']);
1458 $this->_ids
['membership_type'] = $membershipTypeID;
1462 * Set up a pending transaction with a specific price field id
1463 * @param int $priceFieldValueID
1465 public function setUpPendingContribution($priceFieldValueID) {
1466 $contactID = $this->individualCreate();
1467 $membership = $this->callAPISuccess('membership', 'create', array(
1468 'contact_id' => $contactID,
1469 'membership_type_id' => $this->_ids
['membership_type'],
1470 'start_date' => 'yesterday - 1 year',
1471 'end_date' => 'yesterday',
1473 $contribution = $this->callAPISuccess('contribution', 'create', array(
1475 'contact_id' => $contactID,
1476 'receive_date' => date('Ymd'),
1477 'total_amount' => 100.00,
1478 'financial_type_id' => 1,
1479 'payment_instrument_id' => 'Credit Card',
1480 'non_deductible_amount' => 10.00,
1481 'trxn_id' => 'jdhfi88',
1482 'invoice_id' => 'djfhiewuyr',
1484 'contribution_status_id' => 2,
1485 'contribution_page_id' => $this->_ids
['contribution_page'],
1486 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1489 $this->callAPISuccess('line_item', 'create', array(
1490 'entity_id' => $contribution['id'],
1491 'entity_table' => 'civicrm_contribution',
1492 'contribution_id' => $contribution['id'],
1493 'price_field_id' => $this->_ids
['price_field'][0],
1497 'financial_type_id' => 1,
1498 'price_field_value_id' => $priceFieldValueID,
1500 $this->_ids
['contact'] = $contactID;
1501 $this->_ids
['contribution'] = $contribution['id'];
1502 $this->_ids
['membership'] = $membership['id'];
1506 * Test sending a mail via the API
1508 public function testSendMail() {
1509 $mut = new CiviMailUtils($this, TRUE);
1510 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1511 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1512 'id' => $contribution['id'],
1513 'receipt_from_email' => 'api@civicrm.org',
1516 $mut->checkMailLog(array(
1518 'Contribution Information',
1519 'Please print this confirmation for your records',
1528 * Test sending a mail via the API
1530 public function testSendMailEvent() {
1531 $mut = new CiviMailUtils($this, TRUE);
1532 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1533 $event = $this->eventCreate(array(
1534 'is_email_confirm' => 1,
1535 'confirm_from_email' => 'test@civicrm.org',
1537 $this->_eventID
= $event['id'];
1538 $participantParams = array(
1539 'contact_id' => $this->_individualId
,
1540 'event_id' => $this->_eventID
,
1543 // to ensure it matches later on
1544 'register_date' => '2007-07-21 00:00:00',
1545 'source' => 'Online Event Registration: API Testing',
1548 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1549 $this->callAPISuccess('participant_payment', 'create', array(
1550 'participant_id' => $participant['id'],
1551 'contribution_id' => $contribution['id'],
1553 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1554 'id' => $contribution['id'],
1555 'receipt_from_email' => 'api@civicrm.org',
1559 $mut->checkMailLog(array(
1560 'Annual CiviCRM meet',
1562 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1569 * This function does a GET & compares the result against the $params
1570 * Use as a double check on Creates
1572 public function contributionGetnCheck($params, $id, $delete = 1) {
1574 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1580 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1582 $this->assertAPISuccess($contribution, 0);
1583 $values = $contribution['values'][$contribution['id']];
1584 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1585 // this is not returned in id format
1586 unset($params['payment_instrument_id']);
1587 $params['contribution_source'] = $params['source'];
1588 unset($params['source']);
1589 foreach ($params as $key => $value) {
1590 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
1595 * Create a pending contribution & linked pending participant record
1596 * (along with an event)
1598 public function createPendingParticipantContribution() {
1599 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1600 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1601 $this->_ids
['participant'] = $participantID;
1602 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1603 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1604 $this->callAPISuccess('participant_payment', 'create', array(
1605 'contribution_id' => $contribution['id'],
1606 'participant_id' => $participantID,
1608 $this->callAPISuccess('line_item', 'get', array(
1609 'entity_id' => $contribution['id'],
1610 'entity_table' => 'civicrm_contribution',
1611 'api.line_item.create' => array(
1612 'entity_id' => $participantID,
1613 'entity_table' => 'civicrm_participant',
1616 return $contribution['id'];
1620 * @param int $contId
1622 * @return null|string
1624 function _getFinancialTrxnAmount($contId) {
1626 SUM( ft.total_amount ) AS total
1627 FROM civicrm_financial_trxn AS ft
1628 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1629 WHERE ceft.entity_table = 'civicrm_contribution'
1630 AND ceft.entity_id = {$contId}";
1632 $result = CRM_Core_DAO
::singleValueQuery($query);
1637 * @param int $contId
1639 * @return null|string
1641 function _getFinancialItemAmount($contId) {
1642 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1645 FROM civicrm_financial_item
1646 WHERE entity_table = 'civicrm_line_item'
1647 AND entity_id = {$lineItem}";
1648 $result = CRM_Core_DAO
::singleValueQuery($query);
1653 * @param int $contId
1656 public function _checkFinancialItem($contId, $context) {
1657 if ($context != 'paylater') {
1659 'entity_id' => $contId,
1660 'entity_table' => 'civicrm_contribution',
1662 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1663 $entityParams = array(
1664 'financial_trxn_id' => $trxn['financial_trxn_id'],
1665 'entity_table' => 'civicrm_financial_item',
1667 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1669 'id' => $entityTrxn['entity_id'],
1672 if ($context == 'paylater') {
1673 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1674 foreach ($lineItems as $key => $item) {
1676 'entity_id' => $key,
1677 'entity_table' => 'civicrm_line_item',
1679 $compareParams = array('status_id' => 1);
1680 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1683 elseif ($context == 'refund') {
1684 $compareParams = array(
1686 'financial_account_id' => 1,
1690 elseif ($context == 'cancelPending') {
1691 $compareParams = array(
1693 'financial_account_id' => 1,
1697 elseif ($context == 'changeFinancial') {
1698 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1700 'entity_id' => $lineKey,
1703 $compareParams = array(
1704 'financial_account_id' => 1,
1706 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1708 'financial_account_id' => 3,
1709 'entity_id' => $lineKey,
1711 $compareParams = array(
1715 if ($context != 'paylater') {
1716 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1721 * @param $contribution
1723 * @param int $instrumentId
1725 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1726 $trxnParams = array(
1727 'entity_id' => $contribution['id'],
1728 'entity_table' => 'civicrm_contribution',
1730 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1732 'id' => $trxn['financial_trxn_id'],
1734 if ($context == 'payLater') {
1735 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1736 $compareParams = array(
1738 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1741 elseif ($context == 'refund') {
1742 $compareParams = array(
1743 'to_financial_account_id' => 6,
1744 'total_amount' => -100,
1748 elseif ($context == 'cancelPending') {
1749 $compareParams = array(
1750 'from_financial_account_id' => 7,
1751 'total_amount' => -100,
1755 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1756 $entityParams = array(
1757 'entity_id' => $contribution['id'],
1758 'entity_table' => 'civicrm_contribution',
1761 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1762 $trxnParams1 = array(
1763 'id' => $trxn['financial_trxn_id'],
1765 $compareParams = array(
1766 'total_amount' => -100,
1769 if ($context == 'paymentInstrument') {
1770 $compareParams +
= array(
1771 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1772 'payment_instrument_id' => 4,
1776 $compareParams['to_financial_account_id'] = 12;
1778 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1779 $compareParams['total_amount'] = 100;
1780 if ($context == 'paymentInstrument') {
1781 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1782 $compareParams['payment_instrument_id'] = $instrumentId;
1785 $compareParams['to_financial_account_id'] = 12;
1789 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1795 public function _addPaymentInstrument() {
1796 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1797 $optionParams = array(
1798 'option_group_id' => $gId,
1799 'label' => 'Test Card',
1800 'name' => 'Test Card',
1805 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1806 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1807 $financialParams = array(
1808 'entity_table' => 'civicrm_option_value',
1809 'entity_id' => $optionValue['id'],
1810 'account_relationship' => $relationTypeId,
1811 'financial_account_id' => 7,
1813 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1814 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1815 return $optionValue['values'][$optionValue['id']]['value'];
1819 * @param array $params
1822 public function _checkFinancialRecords($params, $context) {
1823 $entityParams = array(
1824 'entity_id' => $params['id'],
1825 'entity_table' => 'civicrm_contribution',
1827 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1828 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1829 if ($context == 'pending') {
1830 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1831 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1834 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1835 $trxnParams = array(
1836 'id' => $trxn['financial_trxn_id'],
1838 if ($context != 'online' && $context != 'payLater') {
1839 $compareParams = array(
1840 'to_financial_account_id' => 6,
1841 'total_amount' => 100,
1845 if ($context == 'feeAmount') {
1846 $compareParams['fee_amount'] = 50;
1848 elseif ($context == 'online') {
1849 $compareParams = array(
1850 'to_financial_account_id' => 12,
1851 'total_amount' => 100,
1855 elseif ($context == 'payLater') {
1856 $compareParams = array(
1857 'to_financial_account_id' => 7,
1858 'total_amount' => 100,
1862 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1863 $entityParams = array(
1864 'financial_trxn_id' => $trxn['financial_trxn_id'],
1865 'entity_table' => 'civicrm_financial_item',
1867 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1868 $fitemParams = array(
1869 'id' => $entityTrxn['entity_id'],
1871 $compareParams = array(
1874 'financial_account_id' => 1,
1876 if ($context == 'payLater') {
1877 $compareParams = array(
1880 'financial_account_id' => 1,
1883 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1884 if ($context == 'feeAmount') {
1886 'entity_id' => $params['id'],
1887 'entity_table' => 'civicrm_contribution',
1889 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1890 $trxnParams = array(
1891 'id' => $maxTrxn['financial_trxn_id'],
1893 $compareParams = array(
1894 'to_financial_account_id' => 5,
1895 'from_financial_account_id' => 6,
1896 'total_amount' => 50,
1899 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1900 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1901 $fitemParams = array(
1902 'entity_id' => $trxnId['financialTrxnId'],
1903 'entity_table' => 'civicrm_financial_trxn',
1905 $compareParams = array(
1908 'financial_account_id' => 5,
1910 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);