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' => '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('sequential' => 1, 'payment_instrument' => 'Cash'));
469 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
470 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
471 $this->assertEquals(1, $contribution['count']);
472 $contribution = $this->callAPISuccess('contribution', 'get', array(
474 'payment_instrument_id' => 5,
476 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
477 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
478 $this->assertEquals(1, $contribution['count']);
479 $contribution = $this->callAPISuccess('contribution', 'get', array(
481 'payment_instrument' => 'EFT',
483 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
484 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
485 $this->assertEquals(1, $contribution['count']);
486 $contribution = $this->callAPISuccess('contribution', 'create', array(
487 'id' => $contribution['id'],
488 'payment_instrument' => 'Credit Card',
490 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
491 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
492 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
493 $this->assertEquals(1, $contribution['count']);
497 * Create test with unique field name on source
499 public function testCreateContributionSource() {
502 'contact_id' => $this->_individualId
,
503 'receive_date' => date('Ymd'),
504 'total_amount' => 100.00,
505 'financial_type_id' => $this->_financialTypeId
,
506 'payment_instrument_id' => 1,
507 'non_deductible_amount' => 10.00,
508 'fee_amount' => 50.00,
509 'net_amount' => 90.00,
511 'invoice_id' => 67890,
512 'contribution_source' => 'SSF',
513 'contribution_status_id' => 1,
516 $contribution = $this->callAPISuccess('contribution', 'create', $params);
517 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
518 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
522 * Create test with unique field name on source
524 public function testCreateDefaultNow() {
526 $params = $this->_params
;
527 unset($params['receive_date']);
529 $contribution = $this->callAPISuccess('contribution', 'create', $params);
530 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
531 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
535 * Create test with unique field name on source
537 public function testCreateContributionSourceInvalidContac() {
541 'receive_date' => date('Ymd'),
542 'total_amount' => 100.00,
543 'financial_type_id' => $this->_financialTypeId
,
544 'payment_instrument_id' => 1,
545 'non_deductible_amount' => 10.00,
546 'fee_amount' => 50.00,
547 'net_amount' => 90.00,
549 'invoice_id' => 67890,
550 'contribution_source' => 'SSF',
551 'contribution_status_id' => 1,
554 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
557 public function testCreateContributionSourceInvalidContContact() {
560 'contribution_contact_id' => 999,
561 'receive_date' => date('Ymd'),
562 'total_amount' => 100.00,
563 'financial_type_id' => $this->_financialTypeId
,
564 'payment_instrument_id' => 1,
565 'non_deductible_amount' => 10.00,
566 'fee_amount' => 50.00,
567 'net_amount' => 90.00,
569 'invoice_id' => 67890,
570 'contribution_source' => 'SSF',
571 'contribution_status_id' => 1,
574 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
578 * Test note created correctly
580 public function testCreateContributionWithNote() {
581 $description = "Demonstrates creating contribution with Note Entity";
582 $subfile = "ContributionCreateWithNote";
584 'contact_id' => $this->_individualId
,
585 'receive_date' => '2012-01-01',
586 'total_amount' => 100.00,
587 'financial_type_id' => $this->_financialTypeId
,
588 'payment_instrument_id' => 1,
589 'non_deductible_amount' => 10.00,
590 'fee_amount' => 50.00,
591 'net_amount' => 90.00,
593 'invoice_id' => 67890,
595 'contribution_status_id' => 1,
596 'note' => 'my contribution note',
599 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
600 $result = $this->callAPISuccess('note', 'get', array(
601 'entity_table' => 'civicrm_contribution',
602 'entity_id' => $contribution['id'],
605 $this->assertEquals('my contribution note', $result['values'][0]['note']);
606 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
609 public function testCreateContributionWithNoteUniqueNameAliases() {
611 'contact_id' => $this->_individualId
,
612 'receive_date' => '2012-01-01',
613 'total_amount' => 100.00,
614 'financial_type_id' => $this->_financialTypeId
,
615 'payment_instrument_id' => 1,
616 'non_deductible_amount' => 10.00,
617 'fee_amount' => 50.00,
618 'net_amount' => 90.00,
620 'invoice_id' => 67890,
622 'contribution_status_id' => 1,
623 'contribution_note' => 'my contribution note',
626 $contribution = $this->callAPISuccess('contribution', 'create', $params);
627 $result = $this->callAPISuccess('note', 'get', array(
628 'entity_table' => 'civicrm_contribution',
629 'entity_id' => $contribution['id'],
632 $this->assertEquals('my contribution note', $result['values'][0]['note']);
633 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
637 * This is the test for creating soft credits - however a 'get' is not yet possible via API
638 * as the current BAO functions are contact-centric (from what I can find)
640 public function testCreateContributionWithSoftCredt() {
641 $description = "Demonstrates creating contribution with SoftCredit";
642 $subfile = "ContributionCreateWithSoftCredit";
643 $contact2 = $this->callAPISuccess('Contact', 'create', array(
644 'display_name' => 'superman',
645 'contact_type' => 'Individual',
648 'contact_id' => $contact2['id'],
650 'soft_credit_type_id' => 3,
653 $params = $this->_params +
array('soft_credit' => array(1 => $softparams));
654 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
655 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
657 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
658 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
659 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
661 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
662 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
665 public function testCreateContributionWithSoftCreditDefaults() {
666 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
667 $subfile = "ContributionCreateWithSoftCreditDefaults";
668 $contact2 = $this->callAPISuccess('Contact', 'create', array(
669 'display_name' => 'superman',
670 'contact_type' => 'Individual',
672 $params = $this->_params +
array(
673 'soft_credit_to' => $contact2['id'],
675 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
676 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
678 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
679 // Default soft credit amount = contribution.total_amount
680 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
681 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
683 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
684 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
687 public function testCreateContributionWithHonoreeContact() {
688 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
689 $subfile = "ContributionCreateWithHonoreeContact";
690 $contact2 = $this->callAPISuccess('Contact', 'create', array(
691 'display_name' => 'superman',
692 'contact_type' => 'Individual',
694 $params = $this->_params +
array(
695 'honor_contact_id' => $contact2['id'],
697 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
698 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
700 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
701 // Default soft credit amount = contribution.total_amount
702 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
703 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
704 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
706 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
707 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
711 * Test using example code
713 public function testContributionCreateExample() {
714 //make sure at least on page exists since there is a truncate in tear down
715 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
716 $this->assertAPISuccess($page);
717 require_once 'api/v3/examples/Contribution/Create.php';
718 $result = contribution_create_example();
719 $this->assertAPISuccess($result);
720 $contributionId = $result['id'];
721 $expectedResult = contribution_create_expectedresult();
722 $this->checkArrayEquals($expectedResult, $result);
723 $this->contributionDelete($contributionId);
727 * Function tests that additional financial records are created when fee amount is recorded
729 public function testCreateContributionWithFee() {
731 'contact_id' => $this->_individualId
,
732 'receive_date' => '20120511',
733 'total_amount' => 100.00,
735 'financial_type_id' => 1,
737 'invoice_id' => 67890,
739 'contribution_status_id' => 1,
742 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
743 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
744 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
745 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
746 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
747 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
748 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
749 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
750 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
751 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
753 $lineItems = $this->callAPISuccess('line_item', 'get', array(
755 'entity_id' => $contribution['id'],
756 'entity_table' => 'civicrm_contribution',
759 $this->assertEquals(1, $lineItems['count']);
760 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
761 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
762 $lineItems = $this->callAPISuccess('line_item', 'get', array(
764 'entity_id' => $contribution['id'],
765 'contribution_id' => $contribution['id'],
766 'entity_table' => 'civicrm_contribution',
769 $this->assertEquals(1, $lineItems['count']);
770 $this->_checkFinancialRecords($contribution, 'feeAmount');
775 * Function tests that additional financial records are created when online contribution is created
777 public function testCreateContributionOnline() {
778 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
779 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
780 $this->assertAPISuccess($contributionPage);
782 'contact_id' => $this->_individualId
,
783 'receive_date' => '20120511',
784 'total_amount' => 100.00,
785 'financial_type_id' => 1,
786 'contribution_page_id' => $contributionPage['id'],
787 'payment_processor' => 1,
789 'invoice_id' => 67890,
791 'contribution_status_id' => 1,
795 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
796 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
797 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
798 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
799 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
800 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
801 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
802 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
803 $this->_checkFinancialRecords($contribution, 'online');
807 * In the interests of removing financial type / contribution type checks from
808 * legacy format function lets test that the api is doing this for us
810 public function testCreateInvalidFinancialType() {
811 $params = $this->_params
;
812 $params['financial_type_id'] = 99999;
813 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
817 * In the interests of removing financial type / contribution type checks from
818 * legacy format function lets test that the api is doing this for us
820 public function testValidNamedFinancialType() {
821 $params = $this->_params
;
822 $params['financial_type_id'] = 'Donation';
823 $this->callAPISuccess($this->_entity
, 'create', $params);
827 * Function tests that additional financial records are created when online contribution with pay later option
830 public function testCreateContributionPayLaterOnline() {
831 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
832 $this->_pageParams
['is_pay_later'] = 1;
833 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
834 $this->assertAPISuccess($contributionPage);
836 'contact_id' => $this->_individualId
,
837 'receive_date' => '20120511',
838 'total_amount' => 100.00,
839 'financial_type_id' => 1,
840 'contribution_page_id' => $contributionPage['id'],
843 'invoice_id' => 67890,
845 'contribution_status_id' => 2,
849 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
850 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
851 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
852 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
853 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
854 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
855 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
856 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
857 $this->_checkFinancialRecords($contribution, 'payLater');
861 * Function tests that additional financial records are created when online contribution with pending option
864 public function testCreateContributionPendingOnline() {
865 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
866 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
867 $this->assertAPISuccess($contributionPage);
869 'contact_id' => $this->_individualId
,
870 'receive_date' => '20120511',
871 'total_amount' => 100.00,
872 'financial_type_id' => 1,
873 'contribution_page_id' => $contributionPage['id'],
875 'invoice_id' => 67890,
877 'contribution_status_id' => 2,
880 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
881 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
882 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
883 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
884 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
885 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
886 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
887 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
888 $this->_checkFinancialRecords($contribution, 'pending');
892 * Test that BAO defaults work
894 public function testCreateBAODefaults() {
895 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
896 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
897 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
898 'id' => $contribution['id'],
899 'api.contribution.delete' => 1,
901 $this->assertEquals(1, $contribution['contribution_status_id']);
902 $this->assertEquals('Check', $contribution['payment_instrument']);
906 * Function tests that line items, financial records are updated when contribution amount is changed
908 public function testCreateUpdateContributionChangeTotal() {
909 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
910 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
912 'entity_id' => $contribution['id'],
913 'entity_table' => 'civicrm_contribution',
915 'return' => 'line_total',
917 $this->assertEquals('100.00', $lineItems);
918 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
919 // Financial trxn SUM = 100 + 5 (fee)
920 $this->assertEquals('105.00', $trxnAmount);
923 'id' => $contribution['id'],
924 'total_amount' => '125',
926 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
928 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
930 'entity_id' => $contribution['id'],
931 'entity_table' => 'civicrm_contribution',
933 'return' => 'line_total',
936 $this->assertEquals('125.00', $lineItems);
937 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
938 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
939 // Financial trxn SUM = 125 + 5 (fee)
940 $this->assertEquals('130.00', $trxnAmount);
941 $this->assertEquals('125.00', $fitemAmount);
945 * Function tests that line items, financial records are updated when pay later contribution is received
947 public function testCreateUpdateContributionPayLater() {
948 $contribParams = array(
949 'contact_id' => $this->_individualId
,
950 'receive_date' => '2012-01-01',
951 'total_amount' => 100.00,
952 'financial_type_id' => $this->_financialTypeId
,
953 'payment_instrument_id' => 1,
954 'contribution_status_id' => 2,
958 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
960 $newParams = array_merge($contribParams, array(
961 'id' => $contribution['id'],
962 'contribution_status_id' => 1,
965 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
966 $contribution = $contribution['values'][$contribution['id']];
967 $this->assertEquals($contribution['contribution_status_id'], '1');
968 $this->_checkFinancialItem($contribution['id'], 'paylater');
969 $this->_checkFinancialTrxn($contribution, 'payLater');
973 * Function tests that financial records are updated when Payment Instrument is changed
975 public function testCreateUpdateContributionPaymentInstrument() {
976 $instrumentId = $this->_addPaymentInstrument();
977 $contribParams = array(
978 'contact_id' => $this->_individualId
,
979 'total_amount' => 100.00,
980 'financial_type_id' => $this->_financialTypeId
,
981 'payment_instrument_id' => 4,
982 'contribution_status_id' => 1,
985 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
987 $newParams = array_merge($contribParams, array(
988 'id' => $contribution['id'],
989 'payment_instrument_id' => $instrumentId,
992 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
993 $this->assertAPISuccess($contribution);
994 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
998 * Function tests that financial records are added when Contribution is Refunded
1000 public function testCreateUpdateContributionRefund() {
1001 $contribParams = array(
1002 'contact_id' => $this->_individualId
,
1003 'receive_date' => '2012-01-01',
1004 'total_amount' => 100.00,
1005 'financial_type_id' => $this->_financialTypeId
,
1006 'payment_instrument_id' => 4,
1007 'contribution_status_id' => 1,
1010 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1011 $newParams = array_merge($contribParams, array(
1012 'id' => $contribution['id'],
1013 'contribution_status_id' => 7,
1017 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1018 $this->_checkFinancialTrxn($contribution, 'refund');
1019 $this->_checkFinancialItem($contribution['id'], 'refund');
1023 * Function tests invalid contribution status change
1025 public function testCreateUpdateContributionInValidStatusChange() {
1026 $contribParams = array(
1028 'receive_date' => '2012-01-01',
1029 'total_amount' => 100.00,
1030 'financial_type_id' => 1,
1031 'payment_instrument_id' => 1,
1032 'contribution_status_id' => 1,
1034 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1035 $newParams = array_merge($contribParams, array(
1036 'id' => $contribution['id'],
1037 'contribution_status_id' => 2,
1040 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1045 * Function tests that financial records are added when Pending Contribution is Canceled
1047 public function testCreateUpdateContributionCancelPending() {
1048 $contribParams = array(
1049 'contact_id' => $this->_individualId
,
1050 'receive_date' => '2012-01-01',
1051 'total_amount' => 100.00,
1052 'financial_type_id' => $this->_financialTypeId
,
1053 'payment_instrument_id' => 1,
1054 'contribution_status_id' => 2,
1055 'is_pay_later' => 1,
1058 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1059 $newParams = array_merge($contribParams, array(
1060 'id' => $contribution['id'],
1061 'contribution_status_id' => 3,
1064 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1065 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1066 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1070 * Function tests that financial records are added when Financial Type is Changed
1072 public function testCreateUpdateContributionChangeFinancialType() {
1073 $contribParams = array(
1074 'contact_id' => $this->_individualId
,
1075 'receive_date' => '2012-01-01',
1076 'total_amount' => 100.00,
1077 'financial_type_id' => 1,
1078 'payment_instrument_id' => 1,
1079 'contribution_status_id' => 1,
1082 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1083 $newParams = array_merge($contribParams, array(
1084 'id' => $contribution['id'],
1085 'financial_type_id' => 3,
1088 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1089 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1090 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1094 * Test that update does not change status id CRM-15105
1096 public function testCreateUpdateWithoutChangingPendingStatus() {
1097 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1098 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1099 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1100 'id' => $contribution['id'],
1101 'api.contribution.delete' => 1,
1103 $this->assertEquals(2, $contribution['contribution_status_id']);
1105 //To Update Contribution
1106 //CHANGE: we require the API to do an incremental update
1107 public function testCreateUpdateContribution() {
1109 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1110 $old_params = array(
1111 'contribution_id' => $contributionID,
1114 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1115 //Make sure it came back
1116 $this->assertAPISuccess($original);
1117 $this->assertEquals($original['id'], $contributionID);
1118 //set up list of old params, verify
1120 //This should not be required on update:
1121 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1122 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1123 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1124 $old_source = $original['values'][$contributionID]['contribution_source'];
1126 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1127 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1129 //check against values in CiviUnitTestCase::createContribution()
1130 $this->assertEquals($old_contact_id, $this->_individualId
);
1131 $this->assertEquals($old_fee_amount, 5.00);
1132 $this->assertEquals($old_source, 'SSF');
1133 $this->assertEquals($old_trxn_id, 212355);
1134 $this->assertEquals($old_invoice_id, 'idofsh');
1136 'id' => $contributionID,
1137 'contact_id' => $this->_individualId
,
1138 'total_amount' => 110.00,
1139 'financial_type_id' => $this->_financialTypeId
,
1140 'non_deductible_amount' => 10.00,
1141 'net_amount' => 100.00,
1142 'contribution_status_id' => 1,
1143 'note' => 'Donating for Nobel Cause',
1147 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1149 $new_params = array(
1150 'contribution_id' => $contribution['id'],
1153 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1155 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1156 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1157 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
);
1158 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1159 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1160 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1161 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1162 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1163 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1164 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1165 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1167 'contribution_id' => $contributionID,
1170 $result = $this->callAPISuccess('contribution', 'delete', $params);
1171 $this->assertAPISuccess($result, 'in line' . __LINE__
);
1174 ///////////////// civicrm_contribution_delete methods
1175 public function testDeleteEmptyParamsContribution() {
1177 $this->callAPIFailure('contribution', 'delete', $params);
1180 public function testDeleteParamsNotArrayContribution() {
1181 $params = 'contribution_id= 1';
1182 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1183 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1186 public function testDeleteWrongParamContribution() {
1188 'contribution_source' => 'SSF',
1191 $this->callAPIFailure('contribution', 'delete', $params);
1194 public function testDeleteContribution() {
1196 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1198 'id' => $contributionID,
1200 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1204 * Test civicrm_contribution_search with empty params.
1205 * All available contributions expected.
1207 public function testSearchEmptyParams() {
1211 'contact_id' => $this->_individualId
,
1212 'receive_date' => date('Ymd'),
1213 'total_amount' => 100.00,
1214 'financial_type_id' => $this->_financialTypeId
,
1215 'non_deductible_amount' => 10.00,
1216 'fee_amount' => 5.00,
1217 'net_amount' => 95.00,
1219 'invoice_id' => 78910,
1221 'contribution_status_id' => 1,
1224 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1226 $result = $this->callAPISuccess('contribution', 'get', $params);
1227 // We're taking the first element.
1228 $res = $result['values'][$contribution['id']];
1230 $this->assertEquals($p['contact_id'], $res['contact_id']);
1231 $this->assertEquals($p['total_amount'], $res['total_amount']);
1232 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1233 $this->assertEquals($p['net_amount'], $res['net_amount']);
1234 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1235 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1236 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1237 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1238 $this->assertEquals($p['source'], $res['contribution_source']);
1239 // contribution_status_id = 1 => Completed
1240 $this->assertEquals('Completed', $res['contribution_status']);
1242 $this->contributionDelete($contribution['id']);
1246 * Test civicrm_contribution_search. Success expected.
1248 public function testSearch() {
1250 'contact_id' => $this->_individualId
,
1251 'receive_date' => date('Ymd'),
1252 'total_amount' => 100.00,
1253 'financial_type_id' => $this->_financialTypeId
,
1254 'non_deductible_amount' => 10.00,
1255 'contribution_status_id' => 1,
1258 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1261 'contact_id' => $this->_individualId
,
1262 'receive_date' => date('Ymd'),
1263 'total_amount' => 200.00,
1264 'financial_type_id' => $this->_financialTypeId
,
1265 'non_deductible_amount' => 20.00,
1266 'trxn_id' => 5454565,
1267 'invoice_id' => 1212124,
1268 'fee_amount' => 50.00,
1269 'net_amount' => 60.00,
1270 'contribution_status_id' => 2,
1273 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1276 'contribution_id' => $contribution2['id'],
1279 $result = $this->callAPISuccess('contribution', 'get', $params);
1280 $res = $result['values'][$contribution2['id']];
1282 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1283 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1284 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1285 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1286 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1287 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1288 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1289 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1290 // contribution_status_id = 2 => Pending
1291 $this->assertEquals('Pending', $res['contribution_status']);
1293 $this->contributionDelete($contribution1['id']);
1294 $this->contributionDelete($contribution2['id']);
1298 * Test completing a transaction via the API
1300 * Note that we are creating a logged in user because email goes out from
1303 public function testCompleteTransaction() {
1304 $mut = new CiviMailUtils($this, TRUE);
1305 $this->createLoggedInUser();
1306 $params = array_merge($this->_params
, array('contribution_status_id' => 2));
1307 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1308 $this->callAPISuccess('contribution', 'completetransaction', array(
1309 'id' => $contribution['id'],
1311 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1312 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1313 $mut->checkMailLog(array(
1314 'Receipt - Contribution',
1315 'Please print this confirmation for your records.',
1322 * Test completing a transaction via the API
1326 public function testCompleteTransactionWithReceiptDateSet() {
1327 $mut = new CiviMailUtils($this, TRUE);
1328 $this->createLoggedInUser();
1329 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1330 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1331 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1332 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1333 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1334 $mut->checkMailLog(array(
1335 'Receipt - Contribution',
1336 'Please print this confirmation for your records.',
1342 * Test completing a transaction with an event via the API
1344 * Note that we are creating a logged in user because email goes out from
1347 public function testCompleteTransactionWithParticipantRecord() {
1348 $mut = new CiviMailUtils($this, TRUE);
1349 $mut->clearMessages();
1350 $this->createLoggedInUser();
1351 $contributionID = $this->createPendingParticipantContribution();
1352 $this->callAPISuccess('contribution', 'completetransaction', array(
1353 'id' => $contributionID,
1356 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1357 'id' => $this->_ids
['participant'],
1358 'return' => 'participant_status_id',
1360 $this->assertEquals(1, $participantStatus);
1361 $mut->checkMailLog(array(
1362 'Annual CiviCRM meet',
1364 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1370 * Test membership is renewed when transaction completed
1372 public function testCompleteTransactionMembershipPriceSet() {
1373 $this->createPriceSetWithPage('membership');
1374 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1375 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1376 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1377 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1378 $this->cleanUpAfterPriceSets();
1382 * Test membership is renewed when transaction completed
1384 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1385 $this->createPriceSetWithPage('membership');
1386 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1387 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1388 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1389 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1390 $this->cleanUpAfterPriceSets();
1393 public function cleanUpAfterPriceSets() {
1394 $this->quickCleanUpFinancialEntities();
1395 $this->contactDelete($this->_ids
['contact']);
1396 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1401 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1402 * on parent class at some point (fn is not in 4.4)
1404 * @param array $params
1406 public function createPriceSetWithPage($entity, $params = array()) {
1407 $membershipTypeID = $this->membershipTypeCreate();
1408 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1409 'title' => "Test Contribution Page",
1410 'financial_type_id' => 1,
1411 'currency' => 'NZD',
1412 'goal_amount' => 50,
1413 'is_pay_later' => 1,
1414 'is_monetary' => TRUE,
1415 'is_email_receipt' => FALSE,
1417 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1418 'is_quick_config' => 0,
1419 'extends' => 'CiviMember',
1420 'financial_type_id' => 1,
1421 'title' => 'my Page',
1423 $priceSetID = $priceSet['id'];
1425 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1426 $priceField = $this->callAPISuccess('price_field', 'create', array(
1427 'price_set_id' => $priceSetID,
1428 'label' => 'Goat Breed',
1429 'html_type' => 'Radio',
1431 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1432 'price_set_id' => $priceSetID,
1433 'price_field_id' => $priceField['id'],
1434 'label' => 'Long Haired Goat',
1436 'membership_type_id' => $membershipTypeID,
1437 'membership_num_terms' => 1,
1440 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1441 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1442 'price_set_id' => $priceSetID,
1443 'price_field_id' => $priceField['id'],
1444 'label' => 'Shoe-eating Goat',
1446 'membership_type_id' => $membershipTypeID,
1447 'membership_num_terms' => 2,
1450 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1451 $this->_ids
['price_set'] = $priceSetID;
1452 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1453 $this->_ids
['price_field'] = array($priceField['id']);
1455 $this->_ids
['membership_type'] = $membershipTypeID;
1459 * Set up a pending transaction with a specific price field id
1460 * @param int $priceFieldValueID
1462 public function setUpPendingContribution($priceFieldValueID) {
1463 $contactID = $this->individualCreate();
1464 $membership = $this->callAPISuccess('membership', 'create', array(
1465 'contact_id' => $contactID,
1466 'membership_type_id' => $this->_ids
['membership_type'],
1467 'start_date' => 'yesterday - 1 year',
1468 'end_date' => 'yesterday',
1470 $contribution = $this->callAPISuccess('contribution', 'create', array(
1472 'contact_id' => $contactID,
1473 'receive_date' => date('Ymd'),
1474 'total_amount' => 100.00,
1475 'financial_type_id' => 1,
1476 'payment_instrument_id' => 'Credit Card',
1477 'non_deductible_amount' => 10.00,
1478 'trxn_id' => 'jdhfi88',
1479 'invoice_id' => 'djfhiewuyr',
1481 'contribution_status_id' => 2,
1482 'contribution_page_id' => $this->_ids
['contribution_page'],
1483 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1486 $this->callAPISuccess('line_item', 'create', array(
1487 'entity_id' => $contribution['id'],
1488 'entity_table' => 'civicrm_contribution',
1489 'contribution_id' => $contribution['id'],
1490 'price_field_id' => $this->_ids
['price_field'][0],
1494 'financial_type_id' => 1,
1495 'price_field_value_id' => $priceFieldValueID,
1497 $this->_ids
['contact'] = $contactID;
1498 $this->_ids
['contribution'] = $contribution['id'];
1499 $this->_ids
['membership'] = $membership['id'];
1503 * Test sending a mail via the API
1505 public function testSendMail() {
1506 $mut = new CiviMailUtils($this, TRUE);
1507 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1508 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1509 'id' => $contribution['id'],
1510 'receipt_from_email' => 'api@civicrm.org',
1513 $mut->checkMailLog(array(
1515 'Contribution Information',
1516 'Please print this confirmation for your records',
1525 * Test sending a mail via the API
1527 public function testSendMailEvent() {
1528 $mut = new CiviMailUtils($this, TRUE);
1529 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
1530 $event = $this->eventCreate(array(
1531 'is_email_confirm' => 1,
1532 'confirm_from_email' => 'test@civicrm.org',
1534 $this->_eventID
= $event['id'];
1535 $participantParams = array(
1536 'contact_id' => $this->_individualId
,
1537 'event_id' => $this->_eventID
,
1540 // to ensure it matches later on
1541 'register_date' => '2007-07-21 00:00:00',
1542 'source' => 'Online Event Registration: API Testing',
1545 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1546 $this->callAPISuccess('participant_payment', 'create', array(
1547 'participant_id' => $participant['id'],
1548 'contribution_id' => $contribution['id'],
1550 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1551 'id' => $contribution['id'],
1552 'receipt_from_email' => 'api@civicrm.org',
1556 $mut->checkMailLog(array(
1557 'Annual CiviCRM meet',
1559 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1566 * This function does a GET & compares the result against the $params
1567 * Use as a double check on Creates
1570 * @param int $delete
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);