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']));
611 * This is the test for creating soft credits - however a 'get' is not yet possible via API
612 * 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 * Function tests that line items, financial records are updated when contribution amount is changed
860 function testCreateUpdateContributionChangeTotal() {
861 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
862 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
864 'entity_id' => $contribution['id'],
865 'entity_table' => 'civicrm_contribution',
867 'return' => 'line_total',
869 $this->assertEquals('100.00', $lineItems);
870 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
871 // Financial trxn SUM = 100 + 5 (fee)
872 $this->assertEquals('105.00', $trxnAmount);
875 'id' => $contribution['id'],
876 'total_amount' => '125');
877 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
879 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
881 'entity_id' => $contribution['id'],
882 'entity_table' => 'civicrm_contribution',
884 'return' => 'line_total',
887 $this->assertEquals('125.00', $lineItems);
888 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
889 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
890 // Financial trxn SUM = 125 + 5 (fee)
891 $this->assertEquals('130.00', $trxnAmount);
892 $this->assertEquals('125.00', $fitemAmount);
896 * Function tests that line items, financial records are updated when pay later contribution is received
898 function testCreateUpdateContributionPayLater() {
899 $contribParams = array(
900 'contact_id' => $this->_individualId
,
901 'receive_date' => '2012-01-01',
902 'total_amount' => 100.00,
903 'financial_type_id' => $this->_financialTypeId
,
904 'payment_instrument_id' => 1,
905 'contribution_status_id' => 2,
909 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
911 $newParams = array_merge($contribParams, array(
912 'id' => $contribution['id'],
913 'contribution_status_id' => 1,)
915 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
916 $contribution = $contribution['values'][$contribution['id']];
917 $this->assertEquals($contribution['contribution_status_id'],'1');
918 $this->_checkFinancialItem($contribution['id'], 'paylater');
919 $this->_checkFinancialTrxn($contribution, 'payLater');
923 * Function tests that financial records are updated when Payment Instrument is changed
925 function testCreateUpdateContributionPaymentInstrument() {
926 $instrumentId = $this->_addPaymentInstrument();
927 $contribParams = array(
928 'contact_id' => $this->_individualId
,
929 'total_amount' => 100.00,
930 'financial_type_id' => $this->_financialTypeId
,
931 'payment_instrument_id' => 4,
932 'contribution_status_id' => 1,
935 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
937 $newParams = array_merge($contribParams, array(
938 'id' => $contribution['id'],
939 'payment_instrument_id' => $instrumentId,)
941 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
942 $this->assertAPISuccess($contribution);
943 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
947 * Function tests that financial records are added when Contribution is Refunded
949 function testCreateUpdateContributionRefund() {
950 $contribParams = array(
951 'contact_id' => $this->_individualId
,
952 'receive_date' => '2012-01-01',
953 'total_amount' => 100.00,
954 'financial_type_id' => $this->_financialTypeId
,
955 'payment_instrument_id' => 4,
956 'contribution_status_id' => 1,
959 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
960 $newParams = array_merge($contribParams, array(
961 'id' => $contribution['id'],
962 'contribution_status_id' => 7,
966 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
967 $this->_checkFinancialTrxn($contribution, 'refund');
968 $this->_checkFinancialItem($contribution['id'], 'refund');
972 * Function tests invalid contribution status change
974 function testCreateUpdateContributionInValidStatusChange() {
975 $contribParams = array(
977 'receive_date' => '2012-01-01',
978 'total_amount' => 100.00,
979 'financial_type_id' => 1,
980 'payment_instrument_id' => 1,
981 'contribution_status_id' => 1,
983 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
984 $newParams = array_merge($contribParams, array(
985 'id' => $contribution['id'],
986 'contribution_status_id' => 2,
989 $this->callAPIFailure('contribution', 'create', $newParams,ts('Cannot change contribution status from Completed to Pending.'));
994 * Function tests that financial records are added when Pending Contribution is Canceled
996 function testCreateUpdateContributionCancelPending() {
997 $contribParams = array(
998 'contact_id' => $this->_individualId
,
999 'receive_date' => '2012-01-01',
1000 'total_amount' => 100.00,
1001 'financial_type_id' => $this->_financialTypeId
,
1002 'payment_instrument_id' => 1,
1003 'contribution_status_id' => 2,
1004 'is_pay_later' => 1,
1007 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1008 $newParams = array_merge($contribParams, array(
1009 'id' => $contribution['id'],
1010 'contribution_status_id' => 3,
1013 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1014 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1015 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1019 * Function tests that financial records are added when Financial Type is Changed
1021 function testCreateUpdateContributionChangeFinancialType() {
1022 $contribParams = array(
1023 'contact_id' => $this->_individualId
,
1024 'receive_date' => '2012-01-01',
1025 'total_amount' => 100.00,
1026 'financial_type_id' => 1,
1027 'payment_instrument_id' => 1,
1028 'contribution_status_id' => 1,
1031 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1032 $newParams = array_merge($contribParams, array(
1033 'id' => $contribution['id'],
1034 'financial_type_id' => 3,
1037 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1038 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1039 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1043 * test that update does not change status id CRM-15105
1045 function testCreateUpdateWithoutChangingPendingStatus() {
1046 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1047 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1048 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1049 $this->assertEquals(2, $contribution['contribution_status_id']);
1051 //To Update Contribution
1052 //CHANGE: we require the API to do an incremental update
1053 function testCreateUpdateContribution() {
1055 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1056 $old_params = array(
1057 'contribution_id' => $contributionID,
1060 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1061 //Make sure it came back
1062 $this->assertAPISuccess($original);
1063 $this->assertEquals($original['id'], $contributionID);
1064 //set up list of old params, verify
1066 //This should not be required on update:
1067 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1068 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1069 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1070 $old_source = $original['values'][$contributionID]['contribution_source'];
1072 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1073 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1075 //check against values in CiviUnitTestCase::createContribution()
1076 $this->assertEquals($old_contact_id, $this->_individualId
);
1077 $this->assertEquals($old_fee_amount, 5.00);
1078 $this->assertEquals($old_source, 'SSF');
1079 $this->assertEquals($old_trxn_id, 212355);
1080 $this->assertEquals($old_invoice_id, 'idofsh');
1082 'id' => $contributionID,
1083 'contact_id' => $this->_individualId
,
1084 'total_amount' => 110.00,
1085 'financial_type_id' => $this->_financialTypeId
,
1086 'non_deductible_amount' => 10.00,
1087 'net_amount' => 100.00,
1088 'contribution_status_id' => 1,
1089 'note' => 'Donating for Nobel Cause',
1093 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1095 $new_params = array(
1096 'contribution_id' => $contribution['id'],
1099 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1101 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
);
1102 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1103 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
);
1104 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1105 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1106 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1107 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1108 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1109 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1110 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1111 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1113 'contribution_id' => $contributionID,
1116 $result = $this->callAPISuccess('contribution', 'delete', $params);
1117 $this->assertAPISuccess($result, 'in line' . __LINE__
);
1120 ///////////////// civicrm_contribution_delete methods
1121 function testDeleteEmptyParamsContribution() {
1123 $this->callAPIFailure('contribution', 'delete', $params);
1126 function testDeleteParamsNotArrayContribution() {
1127 $params = 'contribution_id= 1';
1128 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1129 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1132 function testDeleteWrongParamContribution() {
1134 'contribution_source' => 'SSF',
1137 $this->callAPIFailure('contribution', 'delete', $params);
1140 function testDeleteContribution() {
1142 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1144 'id' => $contributionID,
1146 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1150 * Test civicrm_contribution_search with empty params.
1151 * All available contributions expected.
1153 function testSearchEmptyParams() {
1157 'contact_id' => $this->_individualId
,
1158 'receive_date' => date('Ymd'),
1159 'total_amount' => 100.00,
1160 'financial_type_id' => $this->_financialTypeId
,
1161 'non_deductible_amount' => 10.00,
1162 'fee_amount' => 5.00,
1163 'net_amount' => 95.00,
1165 'invoice_id' => 78910,
1167 'contribution_status_id' => 1,
1170 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1172 $result = $this->callAPISuccess('contribution', 'get', $params);
1173 // We're taking the first element.
1174 $res = $result['values'][$contribution['id']];
1176 $this->assertEquals($p['contact_id'], $res['contact_id']);
1177 $this->assertEquals($p['total_amount'], $res['total_amount']);
1178 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'] );
1179 $this->assertEquals($p['net_amount'], $res['net_amount']);
1180 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1181 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1182 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1183 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1184 $this->assertEquals($p['source'], $res['contribution_source']);
1185 // contribution_status_id = 1 => Completed
1186 $this->assertEquals('Completed', $res['contribution_status']);
1188 $this->contributionDelete($contribution['id']);
1192 * Test civicrm_contribution_search. Success expected.
1194 function testSearch() {
1196 'contact_id' => $this->_individualId
,
1197 'receive_date' => date('Ymd'),
1198 'total_amount' => 100.00,
1199 'financial_type_id' => $this->_financialTypeId
,
1200 'non_deductible_amount' => 10.00,
1201 'contribution_status_id' => 1,
1204 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1207 'contact_id' => $this->_individualId
,
1208 'receive_date' => date('Ymd'),
1209 'total_amount' => 200.00,
1210 'financial_type_id' => $this->_financialTypeId
,
1211 'non_deductible_amount' => 20.00,
1212 'trxn_id' => 5454565,
1213 'invoice_id' => 1212124,
1214 'fee_amount' => 50.00,
1215 'net_amount' => 60.00,
1216 'contribution_status_id' => 2,
1219 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1222 'contribution_id' => $contribution2['id'],
1225 $result = $this->callAPISuccess('contribution', 'get', $params);
1226 $res = $result['values'][$contribution2['id']];
1228 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1229 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1230 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'] );
1231 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1232 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1233 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1234 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1235 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1236 // contribution_status_id = 2 => Pending
1237 $this->assertEquals('Pending', $res['contribution_status']);
1239 $this->contributionDelete($contribution1['id']);
1240 $this->contributionDelete($contribution2['id']);
1244 * Test completing a transaction via the API
1246 * Note that we are creating a logged in user because email goes out from
1249 function testCompleteTransaction() {
1250 $mut = new CiviMailUtils( $this, true );
1251 $this->createLoggedInUser();
1252 $params = array_merge($this->_params
, array('contribution_status_id' => 2,));
1253 $contribution = $this->callAPISuccess('contribution','create', $params);
1254 $this->callAPISuccess('contribution', 'completetransaction', array(
1255 'id' => $contribution['id'],
1257 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1258 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1259 $mut->checkMailLog(array(
1260 'Receipt - Contribution',
1261 'Please print this confirmation for your records.',
1268 * Test completing a transaction via the API
1272 function testCompleteTransactionWithReceiptDateSet() {
1273 $mut = new CiviMailUtils( $this, true );
1274 $this->createLoggedInUser();
1275 $params = array_merge($this->_params
, array('contribution_status_id' => 2,'receipt_date' => 'now'));
1276 $contribution = $this->callAPISuccess('contribution','create', $params);
1277 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'],));
1278 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1279 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1280 $mut->checkMailLog(array(
1281 'Receipt - Contribution',
1282 'Please print this confirmation for your records.',
1288 * Test completing a transaction with an event via the API
1290 * Note that we are creating a logged in user because email goes out from
1293 function testCompleteTransactionWithParticipantRecord() {
1294 $mut = new CiviMailUtils( $this, true );
1295 $mut->clearMessages();
1296 $this->createLoggedInUser();
1297 $contributionID = $this->createPendingParticipantContribution();
1298 $this->callAPISuccess('contribution', 'completetransaction', array(
1299 'id' => $contributionID,)
1301 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->_ids
['participant'], 'return' => 'participant_status_id'));
1302 $this->assertEquals(1, $participantStatus);
1303 $mut->checkMailLog(array(
1304 'Annual CiviCRM meet',
1306 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1312 * test membership is renewed when transaction completed
1314 function testCompleteTransactionMembershipPriceSet() {
1315 $this->createPriceSetWithPage('membership');
1316 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1317 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1318 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1319 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1320 $this->cleanUpAfterPriceSets();
1324 * test membership is renewed when transaction completed
1326 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1327 $this->createPriceSetWithPage('membership');
1328 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1329 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1330 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1331 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1332 $this->cleanUpAfterPriceSets();
1335 function cleanUpAfterPriceSets() {
1336 $this->quickCleanUpFinancialEntities();
1337 $this->contactDelete($this->_ids
['contact']);
1338 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1343 * this could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1344 * on parent class at some point (fn is not in 4.4)
1346 * @param array $params
1348 function createPriceSetWithPage($entity, $params = array()) {
1349 $membershipTypeID = $this->membershipTypeCreate();
1350 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1351 'title' => "Test Contribution Page",
1352 'financial_type_id' => 1,
1353 'currency' => 'NZD',
1354 'goal_amount' => 50,
1355 'is_pay_later' => 1,
1356 'is_monetary' => TRUE,
1357 'is_email_receipt' => FALSE,
1359 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1360 'is_quick_config' => 0,
1361 'extends' => 'CiviMember',
1362 'financial_type_id' => 1,
1363 'title' => 'my Page'
1365 $priceSetID = $priceSet['id'];
1367 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1368 $priceField = $this->callAPISuccess('price_field', 'create', array(
1369 'price_set_id' => $priceSetID ,
1370 'label' => 'Goat Breed',
1371 'html_type' => 'Radio',
1373 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1374 'price_set_id' => $priceSetID ,
1375 'price_field_id' => $priceField['id'],
1376 'label' => 'Long Haired Goat',
1378 'membership_type_id' => $membershipTypeID,
1379 'membership_num_terms' => 1,
1382 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1383 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1384 'price_set_id' => $priceSetID ,
1385 'price_field_id' => $priceField['id'],
1386 'label' => 'Shoe-eating Goat',
1388 'membership_type_id' => $membershipTypeID,
1389 'membership_num_terms' => 2,
1392 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1393 $this->_ids
['price_set'] = $priceSetID;
1394 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1395 $this->_ids
['price_field'] = array($priceField['id']);
1397 $this->_ids
['membership_type'] = $membershipTypeID;
1401 * Set up a pending transaction with a specific price field id
1402 * @param $priceFieldValueID
1404 function setUpPendingContribution($priceFieldValueID){
1405 $contactID = $this->individualCreate();
1406 $membership = $this->callAPISuccess('membership', 'create', array(
1407 'contact_id' => $contactID,
1408 'membership_type_id' => $this->_ids
['membership_type'],
1409 'start_date' => 'yesterday - 1 year',
1410 'end_date' => 'yesterday',
1412 $contribution = $this->callAPISuccess('contribution', 'create', array(
1414 'contact_id' => $contactID,
1415 'receive_date' => date('Ymd'),
1416 'total_amount' => 100.00,
1417 'financial_type_id' => 1,
1418 'payment_instrument_id' => 'Credit Card',
1419 'non_deductible_amount' => 10.00,
1420 'trxn_id' => 'jdhfi88',
1421 'invoice_id' => 'djfhiewuyr',
1423 'contribution_status_id' => 2,
1424 'contribution_page_id' => $this->_ids
['contribution_page'],
1425 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1428 $this->callAPISuccess('line_item', 'create', array(
1429 'entity_id' => $contribution['id'],
1430 'entity_table' => 'civicrm_contribution',
1431 'contribution_id' => $contribution['id'],
1432 'price_field_id' => $this->_ids
['price_field'][0],
1436 'financial_type_id' => 1,
1437 'price_field_value_id' => $priceFieldValueID,
1439 $this->_ids
['contact'] = $contactID;
1440 $this->_ids
['contribution'] = $contribution['id'];
1441 $this->_ids
['membership'] = $membership['id'];
1445 * Test sending a mail via the API
1447 function testSendMail() {
1448 $mut = new CiviMailUtils( $this, true );
1449 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1450 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1451 'id' => $contribution['id'],
1452 'receipt_from_email' => 'api@civicrm.org',
1455 $mut->checkMailLog(array(
1457 'Contribution Information',
1458 'Please print this confirmation for your records',
1467 * Test sending a mail via the API
1469 function testSendMailEvent() {
1470 $mut = new CiviMailUtils( $this, true );
1471 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1472 $event = $this->eventCreate(array(
1473 'is_email_confirm' => 1,
1474 'confirm_from_email' => 'test@civicrm.org',
1476 $this->_eventID
= $event['id'];
1477 $participantParams = array(
1478 'contact_id' => $this->_individualId
,
1479 'event_id' => $this->_eventID
,
1482 // to ensure it matches later on
1483 'register_date' => '2007-07-21 00:00:00',
1484 'source' => 'Online Event Registration: API Testing',
1487 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1488 $this->callAPISuccess('participant_payment', 'create', array(
1489 'participant_id' => $participant['id'],
1490 'contribution_id' => $contribution['id'],
1492 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1493 'id' => $contribution['id'],
1494 'receipt_from_email' => 'api@civicrm.org',
1498 $mut->checkMailLog(array(
1499 'Annual CiviCRM meet',
1501 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1510 * This function does a GET & compares the result against the $params
1511 * Use as a double check on Creates
1513 function contributionGetnCheck($params, $id, $delete = 1) {
1515 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1521 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1523 $this->assertAPISuccess($contribution, 0);
1524 $values = $contribution['values'][$contribution['id']];
1525 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1526 // this is not returned in id format
1527 unset($params['payment_instrument_id']);
1528 $params['contribution_source'] = $params['source'];
1529 unset($params['source']);
1530 foreach ($params as $key => $value) {
1531 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
1536 * Create a pending contribution & linked pending participant record
1537 * (along with an event)
1539 function createPendingParticipantContribution(){
1540 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1541 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1542 $this->_ids
['participant'] = $participantID;
1543 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1544 $contribution = $this->callAPISuccess('contribution','create', $params);
1545 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1546 $this->callAPISuccess('line_item', 'get', array(
1547 'entity_id' => $contribution['id'],
1548 'entity_table' => 'civicrm_contribution',
1549 'api.line_item.create' => array(
1550 'entity_id' => $participantID,
1551 'entity_table' => 'civicrm_participant',
1554 return $contribution['id'];
1560 * @return null|string
1561 */function _getFinancialTrxnAmount($contId) {
1563 SUM( ft.total_amount ) AS total
1564 FROM civicrm_financial_trxn AS ft
1565 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1566 WHERE ceft.entity_table = 'civicrm_contribution'
1567 AND ceft.entity_id = {$contId}";
1569 $result = CRM_Core_DAO
::singleValueQuery($query);
1576 * @return null|string
1577 */function _getFinancialItemAmount($contId) {
1578 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1581 FROM civicrm_financial_item
1582 WHERE entity_table = 'civicrm_line_item'
1583 AND entity_id = {$lineItem}";
1584 $result = CRM_Core_DAO
::singleValueQuery($query);
1592 function _checkFinancialItem($contId, $context) {
1593 if ($context != 'paylater') {
1595 'entity_id' => $contId,
1596 'entity_table' => 'civicrm_contribution',
1598 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1599 $entityParams = array(
1600 'financial_trxn_id' => $trxn['financial_trxn_id'],
1601 'entity_table' => 'civicrm_financial_item',
1603 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1605 'id' => $entityTrxn['entity_id'],
1608 if ($context == 'paylater') {
1609 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1610 foreach ($lineItems as $key=>$item) {
1612 'entity_id' => $key,
1613 'entity_table' => 'civicrm_line_item',
1615 $compareParams = array ('status_id' => 1);
1616 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1619 elseif ($context == 'refund') {
1620 $compareParams = array(
1622 'financial_account_id' => 1,
1626 elseif ($context == 'cancelPending') {
1627 $compareParams = array(
1629 'financial_account_id' => 1,
1633 elseif ($context == 'changeFinancial') {
1634 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1636 'entity_id' => $lineKey,
1639 $compareParams = array(
1640 'financial_account_id' => 1,
1642 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1644 'financial_account_id' => 3,
1645 'entity_id' => $lineKey,
1647 $compareParams = array(
1651 if ($context != 'paylater') {
1652 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1657 * @param $contribution
1659 * @param null $instrumentId
1661 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1662 $trxnParams = array(
1663 'entity_id' => $contribution['id'],
1664 'entity_table' => 'civicrm_contribution',
1666 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1668 'id' => $trxn['financial_trxn_id'],
1670 if ($context == 'payLater') {
1671 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1672 $compareParams = array(
1674 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1677 elseif ($context == 'refund') {
1678 $compareParams = array(
1679 'to_financial_account_id' => 6,
1680 'total_amount' => -100,
1684 elseif ($context == 'cancelPending') {
1685 $compareParams = array(
1686 'from_financial_account_id' => 7,
1687 'total_amount' => -100,
1691 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1692 $entityParams = array(
1693 'entity_id' => $contribution['id'],
1694 'entity_table' => 'civicrm_contribution',
1697 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1698 $trxnParams1 = array(
1699 'id' => $trxn['financial_trxn_id'],
1701 $compareParams = array(
1702 'total_amount' => -100,
1705 if ($context == 'paymentInstrument') {
1706 $compareParams +
= array(
1707 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1708 'payment_instrument_id' => 4,
1712 $compareParams['to_financial_account_id'] = 12;
1714 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1715 $compareParams['total_amount'] = 100;
1716 if ($context == 'paymentInstrument') {
1717 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1718 $compareParams['payment_instrument_id'] = $instrumentId;
1721 $compareParams['to_financial_account_id'] = 12;
1725 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1731 function _addPaymentInstrument () {
1732 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1733 $optionParams = array(
1734 'option_group_id' => $gId,
1735 'label' => 'Test Card',
1736 'name' => 'Test Card',
1741 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1742 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1743 $financialParams = array(
1744 'entity_table' => 'civicrm_option_value',
1745 'entity_id' => $optionValue['id'],
1746 'account_relationship' => $relationTypeId,
1747 'financial_account_id' => 7,
1749 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1750 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1751 return $optionValue['values'][$optionValue['id']]['value'];
1758 function _checkFinancialRecords($params,$context) {
1759 $entityParams = array(
1760 'entity_id' => $params['id'],
1761 'entity_table' => 'civicrm_contribution',
1763 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1764 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1765 if ($context == 'pending') {
1766 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1767 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1770 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1771 $trxnParams = array(
1772 'id' => $trxn['financial_trxn_id'],
1774 if ($context != 'online' && $context != 'payLater') {
1775 $compareParams = array(
1776 'to_financial_account_id' => 6,
1777 'total_amount' => 100,
1781 if ($context == 'feeAmount') {
1782 $compareParams['fee_amount'] = 50;
1784 elseif ($context == 'online') {
1785 $compareParams = array(
1786 'to_financial_account_id' => 12,
1787 'total_amount' => 100,
1791 elseif ($context == 'payLater') {
1792 $compareParams = array(
1793 'to_financial_account_id' => 7,
1794 'total_amount' => 100,
1798 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams, $compareParams);
1799 $entityParams = array(
1800 'financial_trxn_id' => $trxn['financial_trxn_id'],
1801 'entity_table' => 'civicrm_financial_item',
1803 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1804 $fitemParams = array(
1805 'id' => $entityTrxn['entity_id'],
1807 $compareParams = array(
1810 'financial_account_id' => 1,
1812 if ($context == 'payLater') {
1813 $compareParams = array(
1816 'financial_account_id' => 1,
1819 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1820 if ($context == 'feeAmount') {
1822 'entity_id' => $params['id'],
1823 'entity_table' => 'civicrm_contribution',
1825 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1826 $trxnParams = array(
1827 'id' => $maxTrxn['financial_trxn_id'],
1829 $compareParams = array(
1830 'to_financial_account_id' => 5,
1831 'from_financial_account_id' => 6,
1832 'total_amount' => 50,
1835 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1836 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1837 $fitemParams = array(
1838 'entity_id' => $trxnId['financialTrxnId'],
1839 'entity_table' => 'civicrm_financial_trxn',
1841 $compareParams = array(
1844 'financial_account_id' => 5,
1846 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);