3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
39 class api_v3_ContributionTest
extends CiviUnitTestCase
{
42 * Assume empty database with just civicrm_data
44 protected $_individualId;
45 protected $_contribution;
46 protected $_financialTypeId = 1;
47 protected $_apiversion;
48 protected $_entity = 'Contribution';
51 protected $_ids = array();
52 protected $_pageParams = array();
57 $this->_apiversion
= 3;
58 $this->_individualId
= $this->individualCreate();
59 $paymentProcessor = $this->processorCreate();
60 $this->_params
= array(
61 'contact_id' => $this->_individualId
,
62 'receive_date' => '20120511',
63 'total_amount' => 100.00,
64 'financial_type_id' => $this->_financialTypeId
,
65 'non_deductible_amount' => 10.00,
67 'net_amount' => 95.00,
69 'contribution_status_id' => 1,
71 $this->_processorParams
= array(
74 'payment_processor_type_id' => 10,
75 'financial_account_id' => 12,
78 'url_site' => 'http://dummy.com',
79 'url_recur' => 'http://dummy.com',
82 $this->_pageParams
= array(
83 'title' => 'Test Contribution Page',
84 'financial_type_id' => 1,
86 'financial_account_id' => 1,
87 'payment_processor' => $paymentProcessor->id
,
89 'is_allow_other_amount' => 1,
96 $this->quickCleanUpFinancialEntities();
99 function testGetContribution() {
101 'contact_id' => $this->_individualId
,
102 'receive_date' => '2010-01-20',
103 'total_amount' => 100.00,
104 'financial_type_id' => $this->_financialTypeId
,
105 'non_deductible_amount' => 10.00,
106 'fee_amount' => 5.00,
107 'net_amount' => 95.00,
109 'invoice_id' => 78910,
111 'contribution_status_id' => 1,
113 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
116 'contribution_id' => $this->_contribution
['id'],
119 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
120 $financialParams['id'] = $this->_financialTypeId
;
122 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
124 $this->assertEquals(1, $contribution['count']);
125 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
126 // note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
127 // passing back a string rather than an id seems like an error / cruft - & if it is to be introduced we should discuss
128 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
129 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
130 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
131 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
132 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
133 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
134 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
135 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
136 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
137 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
138 $p['trxn_id'] = '3847';
139 $p['invoice_id'] = '3847';
141 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
143 // now we have 2 - test getcount
144 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
145 $this->assertEquals(2, $contribution);
146 //test id only format
147 $contribution = $this->callAPISuccess('contribution', 'get', array(
148 'id' => $this->_contribution
['id'],
149 'format.only_id' => 1,
151 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__
);
152 //test id only format
153 $contribution = $this->callAPISuccess('contribution', 'get', array(
154 'id' => $contribution2['id'],
155 'format.only_id' => 1,
157 $this->assertEquals($contribution2['id'], $contribution);
159 $contribution = $this->callAPISuccess('contribution', 'get', array(
160 'id' => $this->_contribution
['id'],
162 $this->assertEquals(1, $contribution['count']);
164 //test get by contact id works
165 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
167 $this->assertEquals(2, $contribution['count']);
168 $this->callAPISuccess('Contribution', 'Delete', array(
169 'id' => $this->_contribution
['id'],
171 $this->callAPISuccess('Contribution', 'Delete', array(
172 'id' => $contribution2['id'],
177 * We need to ensure previous tested behaviour still works as part of the api contract
179 function testGetContributionLegacyBehaviour() {
181 'contact_id' => $this->_individualId
,
182 'receive_date' => '2010-01-20',
183 'total_amount' => 100.00,
184 'contribution_type_id' => $this->_financialTypeId
,
185 'non_deductible_amount' => 10.00,
186 'fee_amount' => 5.00,
187 'net_amount' => 95.00,
189 'invoice_id' => 78910,
191 'contribution_status_id' => 1,
193 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
196 'contribution_id' => $this->_contribution
['id'],
198 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
199 $financialParams['id'] = $this->_financialTypeId
;
201 CRM_Financial_BAO_FinancialType
::retrieve($financialParams, $default);
203 $this->assertEquals(1,$contribution['count']);
204 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
205 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
206 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
207 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
208 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
209 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
210 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
211 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
212 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
213 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
214 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
215 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
216 $p['trxn_id'] = '3847';
217 $p['invoice_id'] = '3847';
219 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
221 // now we have 2 - test getcount
222 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
223 $this->assertEquals(2, $contribution);
224 //test id only format
225 $contribution = $this->callAPISuccess('contribution', 'get', array(
226 'id' => $this->_contribution
['id'],
227 'format.only_id' => 1,
229 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__
);
230 //test id only format
231 $contribution = $this->callAPISuccess('contribution', 'get', array(
232 'id' => $contribution2['id'],
233 'format.only_id' => 1,
235 $this->assertEquals($contribution2['id'], $contribution);
236 $contribution = $this->callAPISuccess('contribution', 'get', array(
237 'id' => $this->_contribution
['id'],
240 $this->assertEquals(1, $contribution['count']);
241 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
242 //test get by contact id works
243 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
245 $this->assertEquals(2, $contribution['count']);
246 $this->callAPISuccess('Contribution', 'Delete', array(
247 'id' => $this->_contribution
['id'],
249 $this->callAPISuccess('Contribution', 'Delete', array(
250 'id' => $contribution2['id'],
253 ///////////////// civicrm_contribution_
254 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 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 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 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 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']);
350 * Test checks that passing in line items suppresses the create mechanism
352 function testCreateContributionChainedLineItems() {
355 'contact_id' => $this->_individualId
,
356 'receive_date' => '20120511',
357 'total_amount' => 100.00,
358 'financial_type_id' => $this->_financialTypeId
,
359 'payment_instrument_id' => 1,
360 'non_deductible_amount' => 10.00,
361 'fee_amount' => 50.00,
362 'net_amount' => 90.00,
364 'invoice_id' => 67890,
366 'contribution_status_id' => 1,
368 'api.line_item.create' => array(
370 'price_field_id' => 1,
372 'line_total' => '20',
373 'unit_price' => '10',
376 'price_field_id' => 1,
378 'line_total' => '80',
379 'unit_price' => '80',
384 $description = "Create Contribution with Nested Line Items";
385 $subfile = "CreateWithNestedLineItems";
386 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
,__FILE__
, $description, $subfile);
388 $lineItems = $this->callAPISuccess('line_item','get',array(
389 'entity_id' => $contribution['id'],
390 'contribution_id' => $contribution['id'],
391 'entity_table' => 'civicrm_contribution',
394 $this->assertEquals(2, $lineItems['count']);
397 function testCreateContributionOffline() {
399 'contact_id' => $this->_individualId
,
400 'receive_date' => '20120511',
401 'total_amount' => 100.00,
402 'financial_type_id' => 1,
404 'invoice_id' => 67890,
406 'contribution_status_id' => 1,
409 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
410 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
411 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
412 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
413 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
414 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
415 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
416 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
417 $lineItems = $this->callAPISuccess('line_item','get',array(
418 'entity_id' => $contribution['id'],
419 'contribution_id' => $contribution['id'],
420 'entity_table' => 'civicrm_contribution',
423 $this->assertEquals(1, $lineItems['count']);
424 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
425 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
426 $this->_checkFinancialRecords($contribution, 'offline');
427 $this->contributionGetnCheck($params, $contribution['id']);
430 * Test create with valid payment instument
432 function testCreateContributionWithPaymentInstrument() {
433 $params = $this->_params +
array('payment_instrument' => 'EFT');
434 $contribution = $this->callAPISuccess('contribution', 'create', $params);
435 $contribution = $this->callAPISuccess('contribution','get', array(
437 'id' => $contribution['id']
439 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
440 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
442 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
443 $contribution = $this->callAPISuccess('contribution','get', array(
445 'id' => $contribution['id']
447 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
448 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
451 function testGetContributionByPaymentInstrument() {
452 $params = $this->_params +
array('payment_instrument' => 'EFT');
453 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
454 $this->callAPISuccess('contribution','create',$params);
455 $this->callAPISuccess('contribution','create',$params2);
456 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash'));
457 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
458 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
459 $this->assertEquals(1,$contribution['count']);
460 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
461 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
462 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
463 $this->assertEquals(1, $contribution['count']);
464 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument_id' => 5));
465 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
466 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
467 $this->assertEquals(1,$contribution['count']);
468 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument' => 'EFT'));
469 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
470 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
471 $this->assertEquals(1, $contribution['count']);
472 $contribution = $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
473 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
474 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
475 $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']);
476 $this->assertEquals(1,$contribution['count']);
480 * Create test with unique field name on source
482 function testCreateContributionSource() {
485 'contact_id' => $this->_individualId
,
486 'receive_date' => date('Ymd'),
487 'total_amount' => 100.00,
488 'financial_type_id' => $this->_financialTypeId
,
489 'payment_instrument_id' => 1,
490 'non_deductible_amount' => 10.00,
491 'fee_amount' => 50.00,
492 'net_amount' => 90.00,
494 'invoice_id' => 67890,
495 'contribution_source' => 'SSF',
496 'contribution_status_id' => 1,
499 $contribution = $this->callAPISuccess('contribution', 'create', $params);
500 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
501 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
505 * Create test with unique field name on source
507 function testCreateDefaultNow() {
509 $params = $this->_params
;
510 unset($params['receive_date']);
512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
513 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
514 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
518 * Create test with unique field name on source
520 function testCreateContributionSourceInvalidContac() {
524 'receive_date' => date('Ymd'),
525 'total_amount' => 100.00,
526 'financial_type_id' => $this->_financialTypeId
,
527 'payment_instrument_id' => 1,
528 'non_deductible_amount' => 10.00,
529 'fee_amount' => 50.00,
530 'net_amount' => 90.00,
532 'invoice_id' => 67890,
533 'contribution_source' => 'SSF',
534 'contribution_status_id' => 1,
537 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
540 function testCreateContributionSourceInvalidContContact() {
543 'contribution_contact_id' => 999,
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');
561 * Test note created correctly
563 function testCreateContributionWithNote() {
564 $description = "Demonstrates creating contribution with Note Entity";
565 $subfile = "ContributionCreateWithNote";
567 'contact_id' => $this->_individualId
,
568 'receive_date' => '2012-01-01',
569 'total_amount' => 100.00,
570 'financial_type_id' => $this->_financialTypeId
,
571 'payment_instrument_id' => 1,
572 'non_deductible_amount' => 10.00,
573 'fee_amount' => 50.00,
574 'net_amount' => 90.00,
576 'invoice_id' => 67890,
578 'contribution_status_id' => 1,
579 'note' => 'my contribution note',
582 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
583 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
584 $this->assertEquals('my contribution note', $result['values'][0]['note']);
585 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
588 function testCreateContributionWithNoteUniqueNameAliases() {
590 'contact_id' => $this->_individualId
,
591 'receive_date' => '2012-01-01',
592 'total_amount' => 100.00,
593 'financial_type_id' => $this->_financialTypeId
,
594 'payment_instrument_id' => 1,
595 'non_deductible_amount' => 10.00,
596 'fee_amount' => 50.00,
597 'net_amount' => 90.00,
599 'invoice_id' => 67890,
601 'contribution_status_id' => 1,
602 'contribution_note' => 'my contribution note',
605 $contribution = $this->callAPISuccess('contribution', 'create', $params);
606 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
607 $this->assertEquals('my contribution note', $result['values'][0]['note']);
608 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
612 * This is the test for creating soft credits - however a 'get' is not yet possible via API
613 * as the current BAO functions are contact-centric (from what I can find)
615 function testCreateContributionWithSoftCredt() {
616 $description = "Demonstrates creating contribution with SoftCredit";
617 $subfile = "ContributionCreateWithSoftCredit";
618 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
620 'contact_id' => $contact2['id'],
622 'soft_credit_type_id' => 3
625 $params = $this->_params +
array('soft_credit' => array(1 => $softparams));
626 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
627 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
629 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
630 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
631 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
633 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
634 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
637 function testCreateContributionWithSoftCreditDefaults() {
638 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
639 $subfile = "ContributionCreateWithSoftCreditDefaults";
640 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
641 $params = $this->_params +
array(
642 'soft_credit_to' => $contact2['id'],
644 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
645 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
647 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
648 // Default soft credit amount = contribution.total_amount
649 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
650 $this->assertEquals(CRM_Core_OptionGroup
::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
652 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
653 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
656 function testCreateContributionWithHonoreeContact() {
657 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
658 $subfile = "ContributionCreateWithHonoreeContact";
659 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
660 $params = $this->_params +
array(
661 'honor_contact_id' => $contact2['id'],
663 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
664 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
666 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
667 // Default soft credit amount = contribution.total_amount
668 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
669 $this->assertEquals($this->_params
['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
670 $this->assertEquals(CRM_Core_OptionGroup
::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
672 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
673 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
677 * Test using example code
679 function testContributionCreateExample() {
680 //make sure at least on page exists since there is a truncate in tear down
681 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
682 $this->assertAPISuccess($page);
683 require_once 'api/v3/examples/Contribution/Create.php';
684 $result = contribution_create_example();
685 $this->assertAPISuccess($result);
686 $contributionId = $result['id'];
687 $expectedResult = contribution_create_expectedresult();
688 $this->checkArrayEquals($expectedResult, $result);
689 $this->contributionDelete($contributionId);
693 * Function tests that additional financial records are created when fee amount is recorded
695 function testCreateContributionWithFee() {
697 'contact_id' => $this->_individualId
,
698 'receive_date' => '20120511',
699 'total_amount' => 100.00,
701 'financial_type_id' => 1,
703 'invoice_id' => 67890,
705 'contribution_status_id' => 1,
708 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
709 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
710 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
711 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
712 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
713 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
714 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
715 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
716 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
717 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
719 $lineItems = $this->callAPISuccess('line_item','get',array(
721 'entity_id' => $contribution['id'],
722 'entity_table' => 'civicrm_contribution',
725 $this->assertEquals(1, $lineItems['count']);
726 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
727 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
728 $lineItems = $this->callAPISuccess('line_item','get',array(
730 'entity_id' => $contribution['id'],
731 'contribution_id' => $contribution['id'],
732 'entity_table' => 'civicrm_contribution',
735 $this->assertEquals(1, $lineItems['count']);
736 $this->_checkFinancialRecords($contribution, 'feeAmount');
741 * Function tests that additional financial records are created when online contribution is created
743 function testCreateContributionOnline() {
744 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
745 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams
);
746 $this->assertAPISuccess($contributionPage);
748 'contact_id' => $this->_individualId
,
749 'receive_date' => '20120511',
750 'total_amount' => 100.00,
751 'financial_type_id' => 1,
752 'contribution_page_id' => $contributionPage['id'],
753 'payment_processor' => 1,
755 'invoice_id' => 67890,
757 'contribution_status_id' => 1,
761 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
762 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
763 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
764 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
765 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
766 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
767 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
768 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
769 $this->_checkFinancialRecords($contribution, 'online');
773 * In the interests of removing financial type / contribution type checks from
774 * legacy format function lets test that the api is doing this for us
776 function testCreateInvalidFinancialType() {
777 $params = $this->_params
;
778 $params['financial_type_id'] = 99999;
779 $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
783 * In the interests of removing financial type / contribution type checks from
784 * legacy format function lets test that the api is doing this for us
786 function testValidNamedFinancialType() {
787 $params = $this->_params
;
788 $params['financial_type_id'] = 'Donation';
789 $this->callAPISuccess($this->_entity
, 'create', $params);
793 * Function tests that additional financial records are created when online contribution with pay later option
796 function testCreateContributionPayLaterOnline() {
797 CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
798 $this->_pageParams
['is_pay_later'] = 1;
799 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams
);
800 $this->assertAPISuccess($contributionPage);
802 'contact_id' => $this->_individualId
,
803 'receive_date' => '20120511',
804 'total_amount' => 100.00,
805 'financial_type_id' => 1,
806 'contribution_page_id' => $contributionPage['id'],
809 'invoice_id' => 67890,
811 'contribution_status_id' => 2,
815 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
816 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
817 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
818 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
819 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
820 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
821 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
822 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
823 $this->_checkFinancialRecords($contribution, 'payLater');
827 * Function tests that additional financial records are created when online contribution with pending option
830 function testCreateContributionPendingOnline() {
831 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
832 $contributionPage = $this->callAPISuccess( 'contribution_page', 'create', $this->_pageParams
);
833 $this->assertAPISuccess($contributionPage);
835 'contact_id' => $this->_individualId
,
836 'receive_date' => '20120511',
837 'total_amount' => 100.00,
838 'financial_type_id' => 1,
839 'contribution_page_id' => $contributionPage['id'],
841 'invoice_id' => 67890,
843 'contribution_status_id' => 2,
846 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
847 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
848 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
849 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
850 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
851 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
852 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
853 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
854 $this->_checkFinancialRecords($contribution, 'pending');
858 * Test that BAO defaults work
860 function testCreateBAODefaults() {
861 unset($this->_params
['contribution_source_id'], $this->_params
['payment_instrument_id']);
862 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
863 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
864 $this->assertEquals(1, $contribution['contribution_status_id']);
865 $this->assertEquals('Check', $contribution['payment_instrument']);
869 * Function tests that line items, financial records are updated when contribution amount is changed
871 function testCreateUpdateContributionChangeTotal() {
872 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
873 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
875 'entity_id' => $contribution['id'],
876 'entity_table' => 'civicrm_contribution',
878 'return' => 'line_total',
880 $this->assertEquals('100.00', $lineItems);
881 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
882 // Financial trxn SUM = 100 + 5 (fee)
883 $this->assertEquals('105.00', $trxnAmount);
886 'id' => $contribution['id'],
887 'total_amount' => '125');
888 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
890 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
892 'entity_id' => $contribution['id'],
893 'entity_table' => 'civicrm_contribution',
895 'return' => 'line_total',
898 $this->assertEquals('125.00', $lineItems);
899 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
900 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
901 // Financial trxn SUM = 125 + 5 (fee)
902 $this->assertEquals('130.00', $trxnAmount);
903 $this->assertEquals('125.00', $fitemAmount);
907 * Function tests that line items, financial records are updated when pay later contribution is received
909 function testCreateUpdateContributionPayLater() {
910 $contribParams = array(
911 'contact_id' => $this->_individualId
,
912 'receive_date' => '2012-01-01',
913 'total_amount' => 100.00,
914 'financial_type_id' => $this->_financialTypeId
,
915 'payment_instrument_id' => 1,
916 'contribution_status_id' => 2,
920 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
922 $newParams = array_merge($contribParams, array(
923 'id' => $contribution['id'],
924 'contribution_status_id' => 1,)
926 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
927 $contribution = $contribution['values'][$contribution['id']];
928 $this->assertEquals($contribution['contribution_status_id'],'1');
929 $this->_checkFinancialItem($contribution['id'], 'paylater');
930 $this->_checkFinancialTrxn($contribution, 'payLater');
934 * Function tests that financial records are updated when Payment Instrument is changed
936 function testCreateUpdateContributionPaymentInstrument() {
937 $instrumentId = $this->_addPaymentInstrument();
938 $contribParams = array(
939 'contact_id' => $this->_individualId
,
940 'total_amount' => 100.00,
941 'financial_type_id' => $this->_financialTypeId
,
942 'payment_instrument_id' => 4,
943 'contribution_status_id' => 1,
946 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
948 $newParams = array_merge($contribParams, array(
949 'id' => $contribution['id'],
950 'payment_instrument_id' => $instrumentId,)
952 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
953 $this->assertAPISuccess($contribution);
954 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
958 * Function tests that financial records are added when Contribution is Refunded
960 function testCreateUpdateContributionRefund() {
961 $contribParams = array(
962 'contact_id' => $this->_individualId
,
963 'receive_date' => '2012-01-01',
964 'total_amount' => 100.00,
965 'financial_type_id' => $this->_financialTypeId
,
966 'payment_instrument_id' => 4,
967 'contribution_status_id' => 1,
970 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
971 $newParams = array_merge($contribParams, array(
972 'id' => $contribution['id'],
973 'contribution_status_id' => 7,
977 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
978 $this->_checkFinancialTrxn($contribution, 'refund');
979 $this->_checkFinancialItem($contribution['id'], 'refund');
983 * Function tests invalid contribution status change
985 function testCreateUpdateContributionInValidStatusChange() {
986 $contribParams = array(
988 'receive_date' => '2012-01-01',
989 'total_amount' => 100.00,
990 'financial_type_id' => 1,
991 'payment_instrument_id' => 1,
992 'contribution_status_id' => 1,
994 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
995 $newParams = array_merge($contribParams, array(
996 'id' => $contribution['id'],
997 'contribution_status_id' => 2,
1000 $this->callAPIFailure('contribution', 'create', $newParams,ts('Cannot change contribution status from Completed to Pending.'));
1005 * Function tests that financial records are added when Pending Contribution is Canceled
1007 function testCreateUpdateContributionCancelPending() {
1008 $contribParams = array(
1009 'contact_id' => $this->_individualId
,
1010 'receive_date' => '2012-01-01',
1011 'total_amount' => 100.00,
1012 'financial_type_id' => $this->_financialTypeId
,
1013 'payment_instrument_id' => 1,
1014 'contribution_status_id' => 2,
1015 'is_pay_later' => 1,
1018 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1019 $newParams = array_merge($contribParams, array(
1020 'id' => $contribution['id'],
1021 'contribution_status_id' => 3,
1024 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1025 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1026 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1030 * Function tests that financial records are added when Financial Type is Changed
1032 function testCreateUpdateContributionChangeFinancialType() {
1033 $contribParams = array(
1034 'contact_id' => $this->_individualId
,
1035 'receive_date' => '2012-01-01',
1036 'total_amount' => 100.00,
1037 'financial_type_id' => 1,
1038 'payment_instrument_id' => 1,
1039 'contribution_status_id' => 1,
1042 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1043 $newParams = array_merge($contribParams, array(
1044 'id' => $contribution['id'],
1045 'financial_type_id' => 3,
1048 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1049 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1050 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1054 * Test that update does not change status id CRM-15105
1056 function testCreateUpdateWithoutChangingPendingStatus() {
1057 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1058 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1059 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1060 $this->assertEquals(2, $contribution['contribution_status_id']);
1062 //To Update Contribution
1063 //CHANGE: we require the API to do an incremental update
1064 function testCreateUpdateContribution() {
1066 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1067 $old_params = array(
1068 'contribution_id' => $contributionID,
1071 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1072 //Make sure it came back
1073 $this->assertAPISuccess($original);
1074 $this->assertEquals($original['id'], $contributionID);
1075 //set up list of old params, verify
1077 //This should not be required on update:
1078 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1079 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1080 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1081 $old_source = $original['values'][$contributionID]['contribution_source'];
1083 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1084 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1086 //check against values in CiviUnitTestCase::createContribution()
1087 $this->assertEquals($old_contact_id, $this->_individualId
);
1088 $this->assertEquals($old_fee_amount, 5.00);
1089 $this->assertEquals($old_source, 'SSF');
1090 $this->assertEquals($old_trxn_id, 212355);
1091 $this->assertEquals($old_invoice_id, 'idofsh');
1093 'id' => $contributionID,
1094 'contact_id' => $this->_individualId
,
1095 'total_amount' => 110.00,
1096 'financial_type_id' => $this->_financialTypeId
,
1097 'non_deductible_amount' => 10.00,
1098 'net_amount' => 100.00,
1099 'contribution_status_id' => 1,
1100 'note' => 'Donating for Nobel Cause',
1104 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1106 $new_params = array(
1107 'contribution_id' => $contribution['id'],
1110 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1112 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1113 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1114 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
);
1115 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1116 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1117 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1118 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1119 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1120 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1121 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1122 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1124 'contribution_id' => $contributionID,
1127 $result = $this->callAPISuccess('contribution', 'delete', $params);
1128 $this->assertAPISuccess($result, 'in line' . __LINE__
);
1131 ///////////////// civicrm_contribution_delete methods
1132 function testDeleteEmptyParamsContribution() {
1134 $this->callAPIFailure('contribution', 'delete', $params);
1137 function testDeleteParamsNotArrayContribution() {
1138 $params = 'contribution_id= 1';
1139 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1140 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1143 function testDeleteWrongParamContribution() {
1145 'contribution_source' => 'SSF',
1148 $this->callAPIFailure('contribution', 'delete', $params);
1151 function testDeleteContribution() {
1153 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1155 'id' => $contributionID,
1157 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1161 * Test civicrm_contribution_search with empty params.
1162 * All available contributions expected.
1164 function testSearchEmptyParams() {
1168 'contact_id' => $this->_individualId
,
1169 'receive_date' => date('Ymd'),
1170 'total_amount' => 100.00,
1171 'financial_type_id' => $this->_financialTypeId
,
1172 'non_deductible_amount' => 10.00,
1173 'fee_amount' => 5.00,
1174 'net_amount' => 95.00,
1176 'invoice_id' => 78910,
1178 'contribution_status_id' => 1,
1181 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1183 $result = $this->callAPISuccess('contribution', 'get', $params);
1184 // We're taking the first element.
1185 $res = $result['values'][$contribution['id']];
1187 $this->assertEquals($p['contact_id'], $res['contact_id']);
1188 $this->assertEquals($p['total_amount'], $res['total_amount']);
1189 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'] );
1190 $this->assertEquals($p['net_amount'], $res['net_amount']);
1191 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1192 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1193 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1194 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1195 $this->assertEquals($p['source'], $res['contribution_source']);
1196 // contribution_status_id = 1 => Completed
1197 $this->assertEquals('Completed', $res['contribution_status']);
1199 $this->contributionDelete($contribution['id']);
1203 * Test civicrm_contribution_search. Success expected.
1205 function testSearch() {
1207 'contact_id' => $this->_individualId
,
1208 'receive_date' => date('Ymd'),
1209 'total_amount' => 100.00,
1210 'financial_type_id' => $this->_financialTypeId
,
1211 'non_deductible_amount' => 10.00,
1212 'contribution_status_id' => 1,
1215 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1218 'contact_id' => $this->_individualId
,
1219 'receive_date' => date('Ymd'),
1220 'total_amount' => 200.00,
1221 'financial_type_id' => $this->_financialTypeId
,
1222 'non_deductible_amount' => 20.00,
1223 'trxn_id' => 5454565,
1224 'invoice_id' => 1212124,
1225 'fee_amount' => 50.00,
1226 'net_amount' => 60.00,
1227 'contribution_status_id' => 2,
1230 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1233 'contribution_id' => $contribution2['id'],
1236 $result = $this->callAPISuccess('contribution', 'get', $params);
1237 $res = $result['values'][$contribution2['id']];
1239 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1240 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1241 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'] );
1242 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1243 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1244 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1245 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1246 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1247 // contribution_status_id = 2 => Pending
1248 $this->assertEquals('Pending', $res['contribution_status']);
1250 $this->contributionDelete($contribution1['id']);
1251 $this->contributionDelete($contribution2['id']);
1255 * Test completing a transaction via the API
1257 * Note that we are creating a logged in user because email goes out from
1260 function testCompleteTransaction() {
1261 $mut = new CiviMailUtils( $this, true );
1262 $this->createLoggedInUser();
1263 $params = array_merge($this->_params
, array('contribution_status_id' => 2,));
1264 $contribution = $this->callAPISuccess('contribution','create', $params);
1265 $this->callAPISuccess('contribution', 'completetransaction', array(
1266 'id' => $contribution['id'],
1268 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1269 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1270 $mut->checkMailLog(array(
1271 'Receipt - Contribution',
1272 'Please print this confirmation for your records.',
1279 * Test completing a transaction via the API
1283 function testCompleteTransactionWithReceiptDateSet() {
1284 $mut = new CiviMailUtils( $this, true );
1285 $this->createLoggedInUser();
1286 $params = array_merge($this->_params
, array('contribution_status_id' => 2,'receipt_date' => 'now'));
1287 $contribution = $this->callAPISuccess('contribution','create', $params);
1288 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'],));
1289 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1290 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1291 $mut->checkMailLog(array(
1292 'Receipt - Contribution',
1293 'Please print this confirmation for your records.',
1299 * Test completing a transaction with an event via the API
1301 * Note that we are creating a logged in user because email goes out from
1304 function testCompleteTransactionWithParticipantRecord() {
1305 $mut = new CiviMailUtils( $this, true );
1306 $mut->clearMessages();
1307 $this->createLoggedInUser();
1308 $contributionID = $this->createPendingParticipantContribution();
1309 $this->callAPISuccess('contribution', 'completetransaction', array(
1310 'id' => $contributionID,)
1312 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->_ids
['participant'], 'return' => 'participant_status_id'));
1313 $this->assertEquals(1, $participantStatus);
1314 $mut->checkMailLog(array(
1315 'Annual CiviCRM meet',
1317 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1323 * Test membership is renewed when transaction completed
1325 function testCompleteTransactionMembershipPriceSet() {
1326 $this->createPriceSetWithPage('membership');
1327 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1328 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1329 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1330 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1331 $this->cleanUpAfterPriceSets();
1335 * Test membership is renewed when transaction completed
1337 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1338 $this->createPriceSetWithPage('membership');
1339 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1340 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1341 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1342 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1343 $this->cleanUpAfterPriceSets();
1346 function cleanUpAfterPriceSets() {
1347 $this->quickCleanUpFinancialEntities();
1348 $this->contactDelete($this->_ids
['contact']);
1349 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1354 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1355 * on parent class at some point (fn is not in 4.4)
1357 * @param array $params
1359 function createPriceSetWithPage($entity, $params = array()) {
1360 $membershipTypeID = $this->membershipTypeCreate();
1361 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1362 'title' => "Test Contribution Page",
1363 'financial_type_id' => 1,
1364 'currency' => 'NZD',
1365 'goal_amount' => 50,
1366 'is_pay_later' => 1,
1367 'is_monetary' => TRUE,
1368 'is_email_receipt' => FALSE,
1370 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1371 'is_quick_config' => 0,
1372 'extends' => 'CiviMember',
1373 'financial_type_id' => 1,
1374 'title' => 'my Page'
1376 $priceSetID = $priceSet['id'];
1378 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1379 $priceField = $this->callAPISuccess('price_field', 'create', array(
1380 'price_set_id' => $priceSetID ,
1381 'label' => 'Goat Breed',
1382 'html_type' => 'Radio',
1384 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1385 'price_set_id' => $priceSetID ,
1386 'price_field_id' => $priceField['id'],
1387 'label' => 'Long Haired Goat',
1389 'membership_type_id' => $membershipTypeID,
1390 'membership_num_terms' => 1,
1393 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1394 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1395 'price_set_id' => $priceSetID ,
1396 'price_field_id' => $priceField['id'],
1397 'label' => 'Shoe-eating Goat',
1399 'membership_type_id' => $membershipTypeID,
1400 'membership_num_terms' => 2,
1403 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1404 $this->_ids
['price_set'] = $priceSetID;
1405 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1406 $this->_ids
['price_field'] = array($priceField['id']);
1408 $this->_ids
['membership_type'] = $membershipTypeID;
1412 * Set up a pending transaction with a specific price field id
1413 * @param int $priceFieldValueID
1415 function setUpPendingContribution($priceFieldValueID){
1416 $contactID = $this->individualCreate();
1417 $membership = $this->callAPISuccess('membership', 'create', array(
1418 'contact_id' => $contactID,
1419 'membership_type_id' => $this->_ids
['membership_type'],
1420 'start_date' => 'yesterday - 1 year',
1421 'end_date' => 'yesterday',
1423 $contribution = $this->callAPISuccess('contribution', 'create', array(
1425 'contact_id' => $contactID,
1426 'receive_date' => date('Ymd'),
1427 'total_amount' => 100.00,
1428 'financial_type_id' => 1,
1429 'payment_instrument_id' => 'Credit Card',
1430 'non_deductible_amount' => 10.00,
1431 'trxn_id' => 'jdhfi88',
1432 'invoice_id' => 'djfhiewuyr',
1434 'contribution_status_id' => 2,
1435 'contribution_page_id' => $this->_ids
['contribution_page'],
1436 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1439 $this->callAPISuccess('line_item', 'create', array(
1440 'entity_id' => $contribution['id'],
1441 'entity_table' => 'civicrm_contribution',
1442 'contribution_id' => $contribution['id'],
1443 'price_field_id' => $this->_ids
['price_field'][0],
1447 'financial_type_id' => 1,
1448 'price_field_value_id' => $priceFieldValueID,
1450 $this->_ids
['contact'] = $contactID;
1451 $this->_ids
['contribution'] = $contribution['id'];
1452 $this->_ids
['membership'] = $membership['id'];
1456 * Test sending a mail via the API
1458 function testSendMail() {
1459 $mut = new CiviMailUtils( $this, true );
1460 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1461 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1462 'id' => $contribution['id'],
1463 'receipt_from_email' => 'api@civicrm.org',
1466 $mut->checkMailLog(array(
1468 'Contribution Information',
1469 'Please print this confirmation for your records',
1478 * Test sending a mail via the API
1480 function testSendMailEvent() {
1481 $mut = new CiviMailUtils( $this, true );
1482 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1483 $event = $this->eventCreate(array(
1484 'is_email_confirm' => 1,
1485 'confirm_from_email' => 'test@civicrm.org',
1487 $this->_eventID
= $event['id'];
1488 $participantParams = array(
1489 'contact_id' => $this->_individualId
,
1490 'event_id' => $this->_eventID
,
1493 // to ensure it matches later on
1494 'register_date' => '2007-07-21 00:00:00',
1495 'source' => 'Online Event Registration: API Testing',
1498 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1499 $this->callAPISuccess('participant_payment', 'create', array(
1500 'participant_id' => $participant['id'],
1501 'contribution_id' => $contribution['id'],
1503 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1504 'id' => $contribution['id'],
1505 'receipt_from_email' => 'api@civicrm.org',
1509 $mut->checkMailLog(array(
1510 'Annual CiviCRM meet',
1512 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1521 * This function does a GET & compares the result against the $params
1522 * Use as a double check on Creates
1524 function contributionGetnCheck($params, $id, $delete = 1) {
1526 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1532 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1534 $this->assertAPISuccess($contribution, 0);
1535 $values = $contribution['values'][$contribution['id']];
1536 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1537 // this is not returned in id format
1538 unset($params['payment_instrument_id']);
1539 $params['contribution_source'] = $params['source'];
1540 unset($params['source']);
1541 foreach ($params as $key => $value) {
1542 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
1547 * Create a pending contribution & linked pending participant record
1548 * (along with an event)
1550 function createPendingParticipantContribution(){
1551 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1552 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1553 $this->_ids
['participant'] = $participantID;
1554 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1555 $contribution = $this->callAPISuccess('contribution','create', $params);
1556 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1557 $this->callAPISuccess('line_item', 'get', array(
1558 'entity_id' => $contribution['id'],
1559 'entity_table' => 'civicrm_contribution',
1560 'api.line_item.create' => array(
1561 'entity_id' => $participantID,
1562 'entity_table' => 'civicrm_participant',
1565 return $contribution['id'];
1569 * @param int $contId
1571 * @return null|string
1572 */function _getFinancialTrxnAmount($contId) {
1574 SUM( ft.total_amount ) AS total
1575 FROM civicrm_financial_trxn AS ft
1576 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1577 WHERE ceft.entity_table = 'civicrm_contribution'
1578 AND ceft.entity_id = {$contId}";
1580 $result = CRM_Core_DAO
::singleValueQuery($query);
1585 * @param int $contId
1587 * @return null|string
1588 */function _getFinancialItemAmount($contId) {
1589 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1592 FROM civicrm_financial_item
1593 WHERE entity_table = 'civicrm_line_item'
1594 AND entity_id = {$lineItem}";
1595 $result = CRM_Core_DAO
::singleValueQuery($query);
1600 * @param int $contId
1603 function _checkFinancialItem($contId, $context) {
1604 if ($context != 'paylater') {
1606 'entity_id' => $contId,
1607 'entity_table' => 'civicrm_contribution',
1609 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1610 $entityParams = array(
1611 'financial_trxn_id' => $trxn['financial_trxn_id'],
1612 'entity_table' => 'civicrm_financial_item',
1614 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1616 'id' => $entityTrxn['entity_id'],
1619 if ($context == 'paylater') {
1620 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1621 foreach ($lineItems as $key=>$item) {
1623 'entity_id' => $key,
1624 'entity_table' => 'civicrm_line_item',
1626 $compareParams = array ('status_id' => 1);
1627 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1630 elseif ($context == 'refund') {
1631 $compareParams = array(
1633 'financial_account_id' => 1,
1637 elseif ($context == 'cancelPending') {
1638 $compareParams = array(
1640 'financial_account_id' => 1,
1644 elseif ($context == 'changeFinancial') {
1645 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1647 'entity_id' => $lineKey,
1650 $compareParams = array(
1651 'financial_account_id' => 1,
1653 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1655 'financial_account_id' => 3,
1656 'entity_id' => $lineKey,
1658 $compareParams = array(
1662 if ($context != 'paylater') {
1663 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1668 * @param $contribution
1670 * @param int $instrumentId
1672 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1673 $trxnParams = array(
1674 'entity_id' => $contribution['id'],
1675 'entity_table' => 'civicrm_contribution',
1677 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1679 'id' => $trxn['financial_trxn_id'],
1681 if ($context == 'payLater') {
1682 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1683 $compareParams = array(
1685 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1688 elseif ($context == 'refund') {
1689 $compareParams = array(
1690 'to_financial_account_id' => 6,
1691 'total_amount' => -100,
1695 elseif ($context == 'cancelPending') {
1696 $compareParams = array(
1697 'from_financial_account_id' => 7,
1698 'total_amount' => -100,
1702 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1703 $entityParams = array(
1704 'entity_id' => $contribution['id'],
1705 'entity_table' => 'civicrm_contribution',
1708 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1709 $trxnParams1 = array(
1710 'id' => $trxn['financial_trxn_id'],
1712 $compareParams = array(
1713 'total_amount' => -100,
1716 if ($context == 'paymentInstrument') {
1717 $compareParams +
= array(
1718 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1719 'payment_instrument_id' => 4,
1723 $compareParams['to_financial_account_id'] = 12;
1725 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1726 $compareParams['total_amount'] = 100;
1727 if ($context == 'paymentInstrument') {
1728 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1729 $compareParams['payment_instrument_id'] = $instrumentId;
1732 $compareParams['to_financial_account_id'] = 12;
1736 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1742 function _addPaymentInstrument () {
1743 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1744 $optionParams = array(
1745 'option_group_id' => $gId,
1746 'label' => 'Test Card',
1747 'name' => 'Test Card',
1752 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1753 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1754 $financialParams = array(
1755 'entity_table' => 'civicrm_option_value',
1756 'entity_id' => $optionValue['id'],
1757 'account_relationship' => $relationTypeId,
1758 'financial_account_id' => 7,
1760 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1761 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1762 return $optionValue['values'][$optionValue['id']]['value'];
1766 * @param array $params
1769 function _checkFinancialRecords($params,$context) {
1770 $entityParams = array(
1771 'entity_id' => $params['id'],
1772 'entity_table' => 'civicrm_contribution',
1774 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1775 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1776 if ($context == 'pending') {
1777 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1778 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1781 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1782 $trxnParams = array(
1783 'id' => $trxn['financial_trxn_id'],
1785 if ($context != 'online' && $context != 'payLater') {
1786 $compareParams = array(
1787 'to_financial_account_id' => 6,
1788 'total_amount' => 100,
1792 if ($context == 'feeAmount') {
1793 $compareParams['fee_amount'] = 50;
1795 elseif ($context == 'online') {
1796 $compareParams = array(
1797 'to_financial_account_id' => 12,
1798 'total_amount' => 100,
1802 elseif ($context == 'payLater') {
1803 $compareParams = array(
1804 'to_financial_account_id' => 7,
1805 'total_amount' => 100,
1809 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams, $compareParams);
1810 $entityParams = array(
1811 'financial_trxn_id' => $trxn['financial_trxn_id'],
1812 'entity_table' => 'civicrm_financial_item',
1814 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1815 $fitemParams = array(
1816 'id' => $entityTrxn['entity_id'],
1818 $compareParams = array(
1821 'financial_account_id' => 1,
1823 if ($context == 'payLater') {
1824 $compareParams = array(
1827 'financial_account_id' => 1,
1830 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1831 if ($context == 'feeAmount') {
1833 'entity_id' => $params['id'],
1834 'entity_table' => 'civicrm_contribution',
1836 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1837 $trxnParams = array(
1838 'id' => $maxTrxn['financial_trxn_id'],
1840 $compareParams = array(
1841 'to_financial_account_id' => 5,
1842 'from_financial_account_id' => 6,
1843 'total_amount' => 50,
1846 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1847 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1848 $fitemParams = array(
1849 'entity_id' => $trxnId['financialTrxnId'],
1850 'entity_table' => 'civicrm_financial_trxn',
1852 $compareParams = array(
1855 'financial_account_id' => 5,
1857 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);