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';
55 $this->_apiversion
= 3;
56 $this->_individualId
= $this->individualCreate();
57 $paymentProcessor = $this->processorCreate();
58 $this->_params
= array(
59 'contact_id' => $this->_individualId
,
60 'receive_date' => '20120511',
61 'total_amount' => 100.00,
62 'financial_type_id' => $this->_financialTypeId
,
63 'non_deductible_amount' => 10.00,
65 'net_amount' => 95.00,
67 'contribution_status_id' => 1,
69 $this->_processorParams
= array(
72 'payment_processor_type_id' => 10,
73 'financial_account_id' => 12,
76 'url_site' => 'http://dummy.com',
77 'url_recur' => 'http://dummy.com',
80 $this->_pageParams
= array(
81 'title' => 'Test Contribution Page',
82 'financial_type_id' => 1,
84 'financial_account_id' => 1,
85 'payment_processor' => $paymentProcessor->id
,
87 'is_allow_other_amount' => 1,
94 $this->quickCleanup(array(
95 'civicrm_contribution',
96 'civicrm_contribution_soft',
98 'civicrm_contribution_page',
99 'civicrm_participant',
100 'civicrm_participant_payment',
102 'civicrm_financial_trxn',
103 'civicrm_financial_item',
104 'civicrm_entity_financial_trxn',
109 function testGetContribution() {
111 'contact_id' => $this->_individualId
,
112 'receive_date' => '2010-01-20',
113 'total_amount' => 100.00,
114 'financial_type_id' => $this->_financialTypeId
,
115 'non_deductible_amount' => 10.00,
116 'fee_amount' => 5.00,
117 'net_amount' => 95.00,
119 'invoice_id' => 78910,
121 'contribution_status_id' => 1,
123 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
126 'contribution_id' => $this->_contribution
['id'],
128 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
129 $financialParams['id'] = $this->_financialTypeId
;
131 $financialType = CRM_Financial_BAO_FinancialType
::retrieve($financialParams,$default);
133 $this->assertEquals(1,$contribution['count']);
134 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
135 // note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
136 // passing back a string rather than an id seems like an error / cruft - & if it is to be introduced we should discuss
137 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
138 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
139 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00, 'In line ' . __LINE__
);
140 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00, 'In line ' . __LINE__
);
141 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00, 'In line ' . __LINE__
);
142 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456, 'In line ' . __LINE__
);
143 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910, 'In line ' . __LINE__
);
144 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF', 'In line ' . __LINE__
);
145 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed', 'In line ' . __LINE__
);
146 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
147 $p['trxn_id'] = '3847';
148 $p['invoice_id'] = '3847';
150 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
152 // now we have 2 - test getcount
153 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
154 $this->assertEquals(2, $contribution);
155 //test id only format
156 $contribution = $this->callAPISuccess('contribution', 'get', array(
157 'id' => $this->_contribution
['id'],
158 'format.only_id' => 1,
160 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__
);
161 //test id only format
162 $contribution = $this->callAPISuccess('contribution', 'get', array(
163 'id' => $contribution2['id'],
164 'format.only_id' => 1,
166 $this->assertEquals($contribution2['id'], $contribution);
168 $contribution = $this->callAPISuccess('contribution', 'get', array(
169 'id' => $this->_contribution
['id'],
171 $this->assertEquals(1, $contribution['count'], 'In line ' . __LINE__
);
173 //test get by contact id works
174 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
176 $this->assertEquals(2, $contribution['count'], 'In line ' . __LINE__
);
177 $this->callAPISuccess('Contribution', 'Delete', array(
178 'id' => $this->_contribution
['id'],
180 $this->callAPISuccess('Contribution', 'Delete', array(
181 'id' => $contribution2['id'],
186 * We need to ensure previous tested behaviour still works as part of the api contract
188 function testGetContributionLegacyBehaviour() {
190 'contact_id' => $this->_individualId
,
191 'receive_date' => '2010-01-20',
192 'total_amount' => 100.00,
193 'contribution_type_id' => $this->_financialTypeId
,
194 'non_deductible_amount' => 10.00,
195 'fee_amount' => 5.00,
196 'net_amount' => 95.00,
198 'invoice_id' => 78910,
200 'contribution_status_id' => 1,
202 $this->_contribution
= $this->callAPISuccess('contribution', 'create', $p);
205 'contribution_id' => $this->_contribution
['id'],
207 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__
, __FILE__
);
208 $financialParams['id'] = $this->_financialTypeId
;
210 $financialType = CRM_Financial_BAO_FinancialType
::retrieve($financialParams,$default);
212 $this->assertEquals(1,$contribution['count']);
213 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
214 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId
);
215 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId
);
216 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
217 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00, 'In line ' . __LINE__
);
218 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00, 'In line ' . __LINE__
);
219 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00, 'In line ' . __LINE__
);
220 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456, 'In line ' . __LINE__
);
221 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910, 'In line ' . __LINE__
);
222 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF', 'In line ' . __LINE__
);
223 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed', 'In line ' . __LINE__
);
224 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
225 $p['trxn_id'] = '3847';
226 $p['invoice_id'] = '3847';
228 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
230 // now we have 2 - test getcount
231 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
232 $this->assertEquals(2, $contribution);
233 //test id only format
234 $contribution = $this->callAPISuccess('contribution', 'get', array(
235 'id' => $this->_contribution
['id'],
236 'format.only_id' => 1,
238 $this->assertEquals($this->_contribution
['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__
);
239 //test id only format
240 $contribution = $this->callAPISuccess('contribution', 'get', array(
241 'id' => $contribution2['id'],
242 'format.only_id' => 1,
244 $this->assertEquals($contribution2['id'], $contribution);
245 $contribution = $this->callAPISuccess('contribution', 'get', array(
246 'id' => $this->_contribution
['id'],
249 $this->assertEquals(1, $contribution['count'], 'In line ' . __LINE__
);
250 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
251 //test get by contact id works
252 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId
));
254 $this->assertEquals(2, $contribution['count'], 'In line ' . __LINE__
);
255 $this->callAPISuccess('Contribution', 'Delete', array(
256 'id' => $this->_contribution
['id'],
258 $this->callAPISuccess('Contribution', 'Delete', array(
259 'id' => $contribution2['id'],
262 ///////////////// civicrm_contribution_
263 function testCreateEmptyContributionIDUseDonation() {
265 'contribution_id' => FALSE,
268 'check_permissions' => false,
269 'financial_type_id' => 'Donation',
271 $contribution = $this->callAPISuccess('contribution', 'create', $params);
274 * ensure we continue to support contribution_type_id as part of the api commitment to
276 *///////////////// civicrm_contribution_
278 function testCreateLegacyBehaviour() {
280 'contribution_id' => FALSE,
283 'check_permissions' => false,
284 'contribution_type_id' => 3,
286 $contribution = $this->callAPISuccess('contribution', 'create', $params);
287 $contribution = $this->callAPISuccess('contribution', 'getsingle', array( 'id' => $contribution['id']));
288 $this->assertEquals(3, $contribution['financial_type_id']);
292 * check with complete array + custom field
293 * Note that the test is written on purpose without any
294 * variables specific to participant so it can be replicated into other entities
295 * and / or moved to the automated test suite
297 function testCreateWithCustom() {
298 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
300 $params = $this->_params
;
301 $params['custom_' . $ids['custom_field_id']] = "custom string";
303 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
304 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
305 $check = $this->callAPISuccess($this->_entity
, 'get', array(
306 'return.custom_' . $ids['custom_field_id'] => 1,
307 'id' => $result['id'],
309 $this->customFieldDelete($ids['custom_field_id']);
310 $this->customGroupDelete($ids['custom_group_id']);
311 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
315 * check with complete array + custom field
316 * Note that the test is written on purpose without any
317 * variables specific to participant so it can be replicated into other entities
318 * and / or moved to the automated test suite
320 function testCreateGetFieldsWithCustom() {
321 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
322 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ContactTest.php');
323 $result = $this->callAPISuccess('Contribution', 'getfields', array());
324 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
325 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
326 $this->customFieldDelete($ids['custom_field_id']);
327 $this->customGroupDelete($ids['custom_group_id']);
328 $this->customFieldDelete($idsContact['custom_field_id']);
329 $this->customGroupDelete($idsContact['custom_group_id']);
332 function testCreateContributionNoLineItems() {
335 'contact_id' => $this->_individualId
,
336 'receive_date' => '20120511',
337 'total_amount' => 100.00,
338 'financial_type_id' => $this->_financialTypeId
,
339 'payment_instrument_id' => 1,
340 'non_deductible_amount' => 10.00,
341 'fee_amount' => 50.00,
342 'net_amount' => 90.00,
344 'invoice_id' => 67890,
346 'contribution_status_id' => 1,
350 $contribution = $this->callAPISuccess('contribution', 'create', $params);
351 $lineItems = $this->callAPISuccess('line_item','get',array(
352 'entity_id' => $contribution['id'],
353 'entity_table' => 'civicrm_contribution',
356 $this->assertEquals(0, $lineItems['count']);
359 * Test checks that passing in line items suppresses the create mechanism
361 function testCreateContributionChainedLineItems() {
364 'contact_id' => $this->_individualId
,
365 'receive_date' => '20120511',
366 'total_amount' => 100.00,
367 'financial_type_id' => $this->_financialTypeId
,
368 'payment_instrument_id' => 1,
369 'non_deductible_amount' => 10.00,
370 'fee_amount' => 50.00,
371 'net_amount' => 90.00,
373 'invoice_id' => 67890,
375 'contribution_status_id' => 1,
377 'api.line_item.create' => array(
379 'price_field_id' => 1,
381 'line_total' => '20',
382 'unit_price' => '10',
385 'price_field_id' => 1,
387 'line_total' => '80',
388 'unit_price' => '80',
393 $description = "Create Contribution with Nested Line Items";
394 $subfile = "CreateWithNestedLineItems";
395 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
,__FILE__
, $description, $subfile);
397 $lineItems = $this->callAPISuccess('line_item','get',array(
398 'entity_id' => $contribution['id'],
399 'contribution_id' => $contribution['id'],
400 'entity_table' => 'civicrm_contribution',
403 $this->assertEquals(2, $lineItems['count']);
406 function testCreateContributionOffline() {
408 'contact_id' => $this->_individualId
,
409 'receive_date' => '20120511',
410 'total_amount' => 100.00,
411 'financial_type_id' => 1,
413 'invoice_id' => 67890,
415 'contribution_status_id' => 1,
418 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
419 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
420 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
421 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
422 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
423 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
424 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
425 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__
);
426 $lineItems = $this->callAPISuccess('line_item','get',array(
427 'entity_id' => $contribution['id'],
428 'contribution_id' => $contribution['id'],
429 'entity_table' => 'civicrm_contribution',
432 $this->assertEquals(1, $lineItems['count']);
433 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
434 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
435 $this->_checkFinancialRecords($contribution, 'offline');
436 $this->contributionGetnCheck($params, $contribution['id']);
439 * test create with valid payment instument
441 function testCreateContributionWithPaymentInstrument() {
442 $params = $this->_params +
array('payment_instrument' => 'EFT');
443 $contribution = $this->callAPISuccess('contribution', 'create', $params);
444 $contribution = $this->callAPISuccess('contribution','get', array(
446 'id' => $contribution['id']
448 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
449 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
451 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
452 $contribution = $this->callAPISuccess('contribution','get', array(
454 'id' => $contribution['id']
456 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
457 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
460 function testGetContributionByPaymentInstrument() {
461 $params = $this->_params +
array('payment_instrument' => 'EFT');
462 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
463 $this->callAPISuccess('contribution','create',$params);
464 $this->callAPISuccess('contribution','create',$params2);
465 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash'));
466 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
467 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
468 $this->assertEquals(1,$contribution['count']);
469 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
470 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
471 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
472 $this->assertEquals(1, $contribution['count']);
473 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument_id' => 5));
474 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
475 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
476 $this->assertEquals(1,$contribution['count']);
477 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument' => 'EFT'));
478 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
479 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
480 $this->assertEquals(1, $contribution['count']);
481 $contribution = $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
482 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
483 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
484 $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']);
485 $this->assertEquals(1,$contribution['count']);
489 * Create test with unique field name on source
491 function testCreateContributionSource() {
494 'contact_id' => $this->_individualId
,
495 'receive_date' => date('Ymd'),
496 'total_amount' => 100.00,
497 'financial_type_id' => $this->_financialTypeId
,
498 'payment_instrument_id' => 1,
499 'non_deductible_amount' => 10.00,
500 'fee_amount' => 50.00,
501 'net_amount' => 90.00,
503 'invoice_id' => 67890,
504 'contribution_source' => 'SSF',
505 'contribution_status_id' => 1,
508 $contribution = $this->callAPISuccess('contribution', 'create', $params);
509 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
510 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
514 * Create test with unique field name on source
516 function testCreateDefaultNow() {
518 $params = $this->_params
;
519 unset($params['receive_date']);
521 $contribution = $this->callAPISuccess('contribution', 'create', $params);
522 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
523 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
527 * Create test with unique field name on source
529 function testCreateContributionSourceInvalidContac() {
533 'receive_date' => date('Ymd'),
534 'total_amount' => 100.00,
535 'financial_type_id' => $this->_financialTypeId
,
536 'payment_instrument_id' => 1,
537 'non_deductible_amount' => 10.00,
538 'fee_amount' => 50.00,
539 'net_amount' => 90.00,
541 'invoice_id' => 67890,
542 'contribution_source' => 'SSF',
543 'contribution_status_id' => 1,
546 $contribution = $this->callAPIFailure('contribution', 'create', $params,
547 'contact_id is not valid : 999');
550 function testCreateContributionSourceInvalidContContac() {
553 'contribution_contact_id' => 999,
554 'receive_date' => date('Ymd'),
555 'total_amount' => 100.00,
556 'financial_type_id' => $this->_financialTypeId
,
557 'payment_instrument_id' => 1,
558 'non_deductible_amount' => 10.00,
559 'fee_amount' => 50.00,
560 'net_amount' => 90.00,
562 'invoice_id' => 67890,
563 'contribution_source' => 'SSF',
564 'contribution_status_id' => 1,
567 $contribution = $this->callAPIFailure('contribution', 'create', $params,
568 'contact_id is not valid : 999', 'In line ' . __LINE__
);
571 function testCreateContributionWithNote() {
572 $description = "Demonstrates creating contribution with Note Entity";
573 $subfile = "ContributionCreateWithNote";
575 'contact_id' => $this->_individualId
,
576 'receive_date' => '2012-01-01',
577 'total_amount' => 100.00,
578 'financial_type_id' => $this->_financialTypeId
,
579 'payment_instrument_id' => 1,
580 'non_deductible_amount' => 10.00,
581 'fee_amount' => 50.00,
582 'net_amount' => 90.00,
584 'invoice_id' => 67890,
586 'contribution_status_id' => 1,
587 'note' => 'my contribution note',
590 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
591 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
592 $this->assertEquals('my contribution note', $result['values'][0]['note']);
593 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
596 function testCreateContributionWithNoteUniqueNameAliases() {
598 'contact_id' => $this->_individualId
,
599 'receive_date' => '2012-01-01',
600 'total_amount' => 100.00,
601 'financial_type_id' => $this->_financialTypeId
,
602 'payment_instrument_id' => 1,
603 'non_deductible_amount' => 10.00,
604 'fee_amount' => 50.00,
605 'net_amount' => 90.00,
607 'invoice_id' => 67890,
609 'contribution_status_id' => 1,
610 'contribution_note' => 'my contribution note',
613 $contribution = $this->callAPISuccess('contribution', 'create', $params);
614 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
615 $this->assertEquals('my contribution note', $result['values'][0]['note']);
616 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
619 * This is the test for creating soft credits - however a 'get' is not yet possible via API
620 * as the current BAO functions are contact-centric (from what I can find)
623 function testCreateContributionWithSoftCredt() {
624 $description = "Demonstrates creating contribution with SoftCredit";
625 $subfile = "ContributionCreateWithSoftCredit";
626 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
628 'contact_id' => $contact2['id'],
630 'soft_credit_type_id' => 3
633 $params = $this->_params +
array('soft_credit' => array(1 => $softparams));
634 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
635 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
637 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
638 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
639 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
641 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
642 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
646 * Test using example code
648 function testContributionCreateExample() {
649 //make sure at least on page exists since there is a truncate in tear down
650 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
651 $this->assertAPISuccess($page);
652 require_once 'api/v3/examples/Contribution/Create.php';
653 $result = contribution_create_example();
654 $this->assertAPISuccess($result);
655 $contributionId = $result['id'];
656 $expectedResult = contribution_create_expectedresult();
657 $this->checkArrayEquals($expectedResult, $result);
658 $this->contributionDelete($contributionId);
662 * Function tests that additional financial records are created when fee amount is recorded
664 function testCreateContributionWithFee() {
666 'contact_id' => $this->_individualId
,
667 'receive_date' => '20120511',
668 'total_amount' => 100.00,
670 'financial_type_id' => 1,
672 'invoice_id' => 67890,
674 'contribution_status_id' => 1,
677 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
678 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
679 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
680 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
681 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
682 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
683 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
684 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__
);
685 $lineItems = $this->callAPISuccess('line_item','get',array(
687 'entity_id' => $contribution['id'],
688 'entity_table' => 'civicrm_contribution',
691 $this->assertEquals(1, $lineItems['count']);
692 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
693 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
694 $lineItems = $this->callAPISuccess('line_item','get',array(
696 'entity_id' => $contribution['id'],
697 'contribution_id' => $contribution['id'],
698 'entity_table' => 'civicrm_contribution',
701 $this->assertEquals(1, $lineItems['count']);
702 $this->_checkFinancialRecords($contribution, 'feeAmount');
707 * Function tests that additional financial records are created when online contribution is created
709 function testCreateContributionOnline() {
710 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
711 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams
);
712 $this->assertAPISuccess($contributionPage);
714 'contact_id' => $this->_individualId
,
715 'receive_date' => '20120511',
716 'total_amount' => 100.00,
717 'financial_type_id' => 1,
718 'contribution_page_id' => $contributionPage['id'],
719 'payment_processor' => 1,
721 'invoice_id' => 67890,
723 'contribution_status_id' => 1,
727 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
728 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
729 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
730 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
731 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
732 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
733 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
734 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__
);
735 $this->_checkFinancialRecords($contribution, 'online');
739 * in the interests of removing financial type / contribution type checks from
740 * legacy format function lets test that the api is doing this for us
742 function testCreateInvalidFinancialType() {
743 $params = $this->_params
;
744 $params['financial_type_id'] = 99999;
745 $result = $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
749 * in the interests of removing financial type / contribution type checks from
750 * legacy format function lets test that the api is doing this for us
752 function testValidNamedFinancialType() {
753 $params = $this->_params
;
754 $params['financial_type_id'] = 'Donation';
755 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
759 * Function tests that additional financial records are created when online contribution with pay later option
762 function testCreateContributionPayLaterOnline() {
763 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
764 $this->_pageParams
['is_pay_later'] = 1;
765 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams
);
766 $this->assertAPISuccess($contributionPage);
768 'contact_id' => $this->_individualId
,
769 'receive_date' => '20120511',
770 'total_amount' => 100.00,
771 'financial_type_id' => 1,
772 'contribution_page_id' => $contributionPage['id'],
775 'invoice_id' => 67890,
777 'contribution_status_id' => 2,
781 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
782 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
783 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
784 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
785 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
786 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
787 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
788 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__
);
789 $this->_checkFinancialRecords($contribution, 'payLater');
793 * Function tests that additional financial records are created when online contribution with pending option
796 function testCreateContributionPendingOnline() {
797 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
798 $contributionPage = $this->callAPISuccess( 'contribution_page', 'create', $this->_pageParams
);
799 $this->assertAPISuccess($contributionPage);
801 'contact_id' => $this->_individualId
,
802 'receive_date' => '20120511',
803 'total_amount' => 100.00,
804 'financial_type_id' => 1,
805 'contribution_page_id' => $contributionPage['id'],
807 'invoice_id' => 67890,
809 'contribution_status_id' => 2,
812 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
813 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
814 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
815 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
816 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
817 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
818 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
819 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__
);
820 $this->_checkFinancialRecords($contribution, 'pending');
824 * Function tests that line items, financial records are updated when contribution amount is changed
826 function testCreateUpdateContributionChangeTotal() {
827 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
828 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
830 'entity_id' => $contribution['id'],
831 'entity_table' => 'civicrm_contribution',
833 'return' => 'line_total',
835 $this->assertEquals('100.00', $lineItems);
836 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
837 // Financial trxn SUM = 100 + 5 (fee)
838 $this->assertEquals('105.00', $trxnAmount);
841 'id' => $contribution['id'],
842 'total_amount' => '125');
843 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
845 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
847 'entity_id' => $contribution['id'],
848 'entity_table' => 'civicrm_contribution',
850 'return' => 'line_total',
853 $this->assertEquals('125.00', $lineItems);
854 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
855 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
856 // Financial trxn SUM = 125 + 5 (fee)
857 $this->assertEquals('130.00', $trxnAmount);
858 $this->assertEquals('125.00', $fitemAmount);
862 * Function tests that line items, financial records are updated when pay later contribution is received
864 function testCreateUpdateContributionPayLater() {
865 $contribParams = array(
866 'contact_id' => $this->_individualId
,
867 'receive_date' => '2012-01-01',
868 'total_amount' => 100.00,
869 'financial_type_id' => $this->_financialTypeId
,
870 'payment_instrument_id' => 1,
871 'contribution_status_id' => 2,
875 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
877 $newParams = array_merge($contribParams, array(
878 'id' => $contribution['id'],
879 'contribution_status_id' => 1,)
881 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
882 $contribution = $contribution['values'][$contribution['id']];
883 $this->assertEquals($contribution['contribution_status_id'],'1');
884 $this->_checkFinancialItem($contribution['id'], 'paylater');
885 $this->_checkFinancialTrxn($contribution, 'payLater');
889 * Function tests that financial records are updated when Payment Instrument is changed
891 function testCreateUpdateContributionPaymentInstrument() {
892 $instrumentId = $this->_addPaymentInstrument();
893 $contribParams = array(
894 'contact_id' => $this->_individualId
,
895 'total_amount' => 100.00,
896 'financial_type_id' => $this->_financialTypeId
,
897 'payment_instrument_id' => 4,
898 'contribution_status_id' => 1,
901 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
903 $newParams = array_merge($contribParams, array(
904 'id' => $contribution['id'],
905 'payment_instrument_id' => $instrumentId,)
907 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
908 $this->assertAPISuccess($contribution);
909 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
913 * Function tests that financial records are added when Contribution is Refunded
915 function testCreateUpdateContributionRefund() {
916 $contribParams = array(
917 'contact_id' => $this->_individualId
,
918 'receive_date' => '2012-01-01',
919 'total_amount' => 100.00,
920 'financial_type_id' => $this->_financialTypeId
,
921 'payment_instrument_id' => 4,
922 'contribution_status_id' => 1,
925 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
926 $newParams = array_merge($contribParams, array(
927 'id' => $contribution['id'],
928 'contribution_status_id' => 7,
932 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
933 $this->_checkFinancialTrxn($contribution, 'refund');
934 $this->_checkFinancialItem($contribution['id'], 'refund');
938 * Function tests invalid contribution status change
940 function testCreateUpdateContributionInValidStatusChange() {
941 $contribParams = array(
943 'receive_date' => '2012-01-01',
944 'total_amount' => 100.00,
945 'financial_type_id' => 1,
946 'payment_instrument_id' => 1,
947 'contribution_status_id' => 1,
949 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
950 $newParams = array_merge($contribParams, array(
951 'id' => $contribution['id'],
952 'contribution_status_id' => 2,
955 $contribution = $this->callAPIFailure('contribution', 'create', $newParams,
956 ts('Cannot change contribution status from Completed to Pending.')
962 * Function tests that financial records are added when Pending Contribution is Canceled
964 function testCreateUpdateContributionCancelPending() {
965 $contribParams = array(
966 'contact_id' => $this->_individualId
,
967 'receive_date' => '2012-01-01',
968 'total_amount' => 100.00,
969 'financial_type_id' => $this->_financialTypeId
,
970 'payment_instrument_id' => 1,
971 'contribution_status_id' => 2,
975 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
976 $newParams = array_merge($contribParams, array(
977 'id' => $contribution['id'],
978 'contribution_status_id' => 3,
981 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
982 $this->_checkFinancialTrxn($contribution, 'cancelPending');
983 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
987 * Function tests that financial records are added when Financial Type is Changed
989 function testCreateUpdateContributionChangeFinancialType() {
990 $contribParams = array(
991 'contact_id' => $this->_individualId
,
992 'receive_date' => '2012-01-01',
993 'total_amount' => 100.00,
994 'financial_type_id' => 1,
995 'payment_instrument_id' => 1,
996 'contribution_status_id' => 1,
999 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1000 $newParams = array_merge($contribParams, array(
1001 'id' => $contribution['id'],
1002 'financial_type_id' => 3,
1005 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1006 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1007 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1011 * test that update does not change status id CRM-15105
1013 function testCreateUpdateWithoutChangingPendingStatus() {
1014 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params
, array('contribution_status_id' => 2)));
1015 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1016 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1017 $this->assertEquals(2, $contribution['contribution_status_id']);
1019 //To Update Contribution
1020 //CHANGE: we require the API to do an incremental update
1021 function testCreateUpdateContribution() {
1023 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1024 $old_params = array(
1025 'contribution_id' => $contributionID,
1028 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1029 //Make sure it came back
1030 $this->assertAPISuccess($original, 'In line ' . __LINE__
);
1031 $this->assertEquals($original['id'], $contributionID, 'In line ' . __LINE__
);
1032 //set up list of old params, verify
1034 //This should not be required on update:
1035 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1036 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1037 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1038 $old_source = $original['values'][$contributionID]['contribution_source'];
1040 //note: current behavior is to return ISO. Is this
1041 //documented behavior? Is this correct
1042 $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date']));
1044 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1045 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1047 //check against values in CiviUnitTestCase::createContribution()
1048 $this->assertEquals($old_contact_id, $this->_individualId
, 'In line ' . __LINE__
);
1049 $this->assertEquals($old_fee_amount, 5.00, 'In line ' . __LINE__
);
1050 $this->assertEquals($old_source, 'SSF', 'In line ' . __LINE__
);
1051 $this->assertEquals($old_trxn_id, 212355, 'In line ' . __LINE__
);
1052 $this->assertEquals($old_invoice_id, 'idofsh', 'In line ' . __LINE__
);
1054 'id' => $contributionID,
1055 'contact_id' => $this->_individualId
,
1056 'total_amount' => 110.00,
1057 'financial_type_id' => $this->_financialTypeId
,
1058 'non_deductible_amount' => 10.00,
1059 'net_amount' => 100.00,
1060 'contribution_status_id' => 1,
1061 'note' => 'Donating for Nobel Cause',
1065 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1067 $new_params = array(
1068 'contribution_id' => $contribution['id'],
1071 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1073 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
1074 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00, 'In line ' . __LINE__
);
1075 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
, 'In line ' . __LINE__
);
1076 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument, 'In line ' . __LINE__
);
1077 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00, 'In line ' . __LINE__
);
1078 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount, 'In line ' . __LINE__
);
1079 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00, 'In line ' . __LINE__
);
1080 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id, 'In line ' . __LINE__
);
1081 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id, 'In line ' . __LINE__
);
1082 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source, 'In line ' . __LINE__
);
1083 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed', 'In line ' . __LINE__
);
1085 'contribution_id' => $contributionID,
1088 $result = $this->callAPISuccess('contribution', 'delete', $params);
1089 $this->assertAPISuccess($result, 'in line' . __LINE__
);
1092 ///////////////// civicrm_contribution_delete methods
1093 function testDeleteEmptyParamsContribution() {
1095 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1098 function testDeleteParamsNotArrayContribution() {
1099 $params = 'contribution_id= 1';
1100 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1101 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1104 function testDeleteWrongParamContribution() {
1106 'contribution_source' => 'SSF',
1109 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1112 function testDeleteContribution() {
1114 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1116 'id' => $contributionID,
1118 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1122 * Test civicrm_contribution_search with empty params.
1123 * All available contributions expected.
1125 function testSearchEmptyParams() {
1129 'contact_id' => $this->_individualId
,
1130 'receive_date' => date('Ymd'),
1131 'total_amount' => 100.00,
1132 'financial_type_id' => $this->_financialTypeId
,
1133 'non_deductible_amount' => 10.00,
1134 'fee_amount' => 5.00,
1135 'net_amount' => 95.00,
1137 'invoice_id' => 78910,
1139 'contribution_status_id' => 1,
1142 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1144 $result = $this->callAPISuccess('contribution', 'get', $params);
1145 // We're taking the first element.
1146 $res = $result['values'][$contribution['id']];
1148 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
1149 $this->assertEquals($p['total_amount'], $res['total_amount'], 'In line ' . __LINE__
);
1150 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__
);
1151 $this->assertEquals($p['net_amount'], $res['net_amount'], 'In line ' . __LINE__
);
1152 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__
);
1153 $this->assertEquals($p['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__
);
1154 $this->assertEquals($p['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__
);
1155 $this->assertEquals($p['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__
);
1156 $this->assertEquals($p['source'], $res['contribution_source'], 'In line ' . __LINE__
);
1157 // contribution_status_id = 1 => Completed
1158 $this->assertEquals('Completed', $res['contribution_status'], 'In line ' . __LINE__
);
1160 $this->contributionDelete($contribution['id']);
1164 * Test civicrm_contribution_search. Success expected.
1166 function testSearch() {
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 'contribution_status_id' => 1,
1176 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1179 'contact_id' => $this->_individualId
,
1180 'receive_date' => date('Ymd'),
1181 'total_amount' => 200.00,
1182 'financial_type_id' => $this->_financialTypeId
,
1183 'non_deductible_amount' => 20.00,
1184 'trxn_id' => 5454565,
1185 'invoice_id' => 1212124,
1186 'fee_amount' => 50.00,
1187 'net_amount' => 60.00,
1188 'contribution_status_id' => 2,
1191 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1194 'contribution_id' => $contribution2['id'],
1197 $result = $this->callAPISuccess('contribution', 'get', $params);
1198 $res = $result['values'][$contribution2['id']];
1200 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
1201 $this->assertEquals($p2['total_amount'], $res['total_amount'], 'In line ' . __LINE__
);
1202 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__
);
1203 $this->assertEquals($p2['net_amount'], $res['net_amount'], 'In line ' . __LINE__
);
1204 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__
);
1205 $this->assertEquals($p2['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__
);
1206 $this->assertEquals($p2['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__
);
1207 $this->assertEquals($p2['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__
);
1208 // contribution_status_id = 2 => Pending
1209 $this->assertEquals('Pending', $res['contribution_status'], 'In line ' . __LINE__
);
1211 $this->contributionDelete($contribution1['id']);
1212 $this->contributionDelete($contribution2['id']);
1216 * Test completing a transaction via the API
1218 * Note that we are creating a logged in user because email goes out from
1221 function testCompleteTransaction() {
1222 $mut = new CiviMailUtils( $this, true );
1223 $this->createLoggedInUser();
1224 $params = array_merge($this->_params
, array('contribution_status_id' => 2,));
1225 $contribution = $this->callAPISuccess('contribution','create', $params);
1226 $this->callAPISuccess('contribution', 'completetransaction', array(
1227 'id' => $contribution['id'],
1229 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1230 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1231 $mut->checkMailLog(array(
1232 'Receipt - Contribution',
1233 'Please print this confirmation for your records.',
1240 * Test completing a transaction via the API
1244 function testCompleteTransactionWithReceiptDateSet() {
1245 $mut = new CiviMailUtils( $this, true );
1246 $this->createLoggedInUser();
1247 $params = array_merge($this->_params
, array('contribution_status_id' => 2,'receipt_date' => 'now'));
1248 $contribution = $this->callAPISuccess('contribution','create', $params);
1249 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1250 'id' => $contribution['id'],
1253 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1254 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1255 $mut->checkMailLog(array(
1256 'Receipt - Contribution',
1257 'Please print this confirmation for your records.',
1263 * Test completing a transaction with an event via the API
1265 * Note that we are creating a logged in user because email goes out from
1268 function testCompleteTransactionWithParticipantRecord() {
1269 $mut = new CiviMailUtils( $this, true );
1270 $mut->clearMessages();
1271 $this->createLoggedInUser();
1272 $contributionID = $this->createPendingParticipantContribution();
1273 $this->callAPISuccess('contribution', 'completetransaction', array(
1274 'id' => $contributionID,)
1276 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids
['participant'], 'return' => 'participant_status_id'));
1277 $this->assertEquals(1, $participantStatus);
1278 $mut->checkMailLog(array(
1279 'Annual CiviCRM meet',
1281 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1287 * test membership is renewed when transaction completed
1289 function testCompleteTransactionMembershipPriceSet() {
1290 $this->createPriceSetWithPage('membership');
1291 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1292 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1293 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1294 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1295 $this->cleanUpAfterPriceSets();
1299 * test membership is renewed when transaction completed
1301 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1302 $this->createPriceSetWithPage('membership');
1303 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1304 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1305 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1306 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1307 $this->cleanUpAfterPriceSets();
1310 function cleanUpAfterPriceSets() {
1311 $this->quickCleanUpFinancialEntities();
1312 $this->contactDelete($this->_ids
['contact']);
1313 $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids
['price_field_value'][0]));
1314 $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids
['price_field_value'][1]));
1315 $this->callAPISuccess('price_field', 'delete', array('id' => $this->_ids
['price_field'][0]));
1316 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1321 * this could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1322 * on parent class at some point (fn is not in 4.4)
1324 * @param array $params
1326 function createPriceSetWithPage($entity, $params = array()) {
1327 $membershipTypeID = $this->membershipTypeCreate();
1328 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1329 'title' => "Test Contribution Page",
1330 'financial_type_id' => 1,
1331 'currency' => 'NZD',
1332 'goal_amount' => 50,
1333 'is_pay_later' => 1,
1334 'is_monetary' => TRUE,
1335 'is_email_receipt' => FALSE,
1337 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1338 'is_quick_config' => 0,
1339 'extends' => 'CiviMember',
1340 'financial_type_id' => 1,
1341 'title' => 'my Page'
1343 $priceSetID = $priceSet['id'];
1345 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1346 $priceField = $this->callAPISuccess('price_field', 'create', array(
1347 'price_set_id' => $priceSetID ,
1348 'label' => 'Goat Breed',
1349 'html_type' => 'Radio',
1351 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1352 'price_set_id' => $priceSetID ,
1353 'price_field_id' => $priceField['id'],
1354 'label' => 'Long Haired Goat',
1356 'membership_type_id' => $membershipTypeID,
1357 'membership_num_terms' => 1,
1360 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1361 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1362 'price_set_id' => $priceSetID ,
1363 'price_field_id' => $priceField['id'],
1364 'label' => 'Shoe-eating Goat',
1366 'membership_type_id' => $membershipTypeID,
1367 'membership_num_terms' => 2,
1370 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1371 $this->_ids
['price_set'] = $priceSetID;
1372 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1373 $this->_ids
['price_field'] = array($priceField['id']);
1375 $this->_ids
['membership_type'] = $membershipTypeID;
1379 * Set up a pending transaction with a specific price field id
1380 * @param $priceFieldValueID
1382 function setUpPendingContribution($priceFieldValueID){
1383 $contactID = $this->individualCreate();
1384 $membership = $this->callAPISuccess('membership', 'create', array(
1385 'contact_id' => $contactID,
1386 'membership_type_id' => $this->_ids
['membership_type'],
1387 'start_date' => 'yesterday - 1 year',
1388 'end_date' => 'yesterday',
1390 $contribution = $this->callAPISuccess('contribution', 'create', array(
1392 'contact_id' => $contactID,
1393 'receive_date' => date('Ymd'),
1394 'total_amount' => 100.00,
1395 'financial_type_id' => 1,
1396 'payment_instrument_id' => 'Credit Card',
1397 'non_deductible_amount' => 10.00,
1398 'trxn_id' => 'jdhfi88',
1399 'invoice_id' => 'djfhiewuyr',
1401 'contribution_status_id' => 2,
1402 'contribution_page_id' => $this->_ids
['contribution_page'],
1403 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1406 $this->callAPISuccess('line_item', 'create', array(
1407 'entity_id' => $contribution['id'],
1408 'entity_table' => 'civicrm_contribution',
1409 'contribution_id' => $contribution['id'],
1410 'price_field_id' => $this->_ids
['price_field'][0],
1414 'financial_type_id' => 1,
1415 'price_field_value_id' => $priceFieldValueID,
1417 $this->_ids
['contact'] = $contactID;
1418 $this->_ids
['contribution'] = $contribution['id'];
1419 $this->_ids
['membership'] = $membership['id'];
1423 * Test sending a mail via the API
1425 function testSendMail() {
1426 $mut = new CiviMailUtils( $this, true );
1427 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1428 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
1430 'id' => $contribution['id'],
1431 'receipt_from_email' => 'api@civicrm.org',
1434 $mut->checkMailLog(array(
1436 'Contribution Information',
1437 'Please print this confirmation for your records',
1446 * Test sending a mail via the API
1448 function testSendMailEvent() {
1449 $mut = new CiviMailUtils( $this, true );
1450 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1451 $event = $this->eventCreate(array(
1452 'is_email_confirm' => 1,
1453 'confirm_from_email' => 'test@civicrm.org',
1455 $this->_eventID
= $event['id'];
1456 $participantParams = array(
1457 'contact_id' => $this->_individualId
,
1458 'event_id' => $this->_eventID
,
1461 // to ensure it matches later on
1462 'register_date' => '2007-07-21 00:00:00',
1463 'source' => 'Online Event Registration: API Testing',
1466 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1467 $this->callAPISuccess('participant_payment', 'create', array(
1468 'participant_id' => $participant['id'],
1469 'contribution_id' => $contribution['id'],
1471 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1472 'id' => $contribution['id'],
1473 'receipt_from_email' => 'api@civicrm.org',
1477 $mut->checkMailLog(array(
1478 'Annual CiviCRM meet',
1480 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1489 * This function does a GET & compares the result against the $params
1490 * Use as a double check on Creates
1492 function contributionGetnCheck($params, $id, $delete = 1) {
1494 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1500 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1502 $this->assertAPISuccess($contribution, 0, 'In line ' . __LINE__
);
1503 $values = $contribution['values'][$contribution['id']];
1504 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1505 // this is not returned in id format
1506 unset($params['payment_instrument_id']);
1507 $params['contribution_source'] = $params['source'];
1508 unset($params['source']);
1509 foreach ($params as $key => $value) {
1510 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
1515 * Create a pending contribution & linked pending participant record
1516 * (along with an event)
1518 function createPendingParticipantContribution(){
1519 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1520 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1521 $this->ids
['participant'] = $participantID;
1522 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1523 $contribution = $this->callAPISuccess('contribution','create', $params);
1524 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1525 $lineItem = $this->callAPISuccess('line_item', 'get', array(
1526 'entity_id' => $contribution['id'],
1527 'entity_table' => 'civicrm_contribution',
1528 'api.line_item.create' => array(
1529 'entity_id' => $participantID,
1530 'entity_table' => 'civicrm_participant',
1533 return $contribution['id'];
1539 * @return null|string
1540 */function _getFinancialTrxnAmount($contId) {
1542 SUM( ft.total_amount ) AS total
1543 FROM civicrm_financial_trxn AS ft
1544 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1545 WHERE ceft.entity_table = 'civicrm_contribution'
1546 AND ceft.entity_id = {$contId}";
1548 $result = CRM_Core_DAO
::singleValueQuery($query);
1555 * @return null|string
1556 */function _getFinancialItemAmount($contId) {
1557 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1560 FROM civicrm_financial_item
1561 WHERE entity_table = 'civicrm_line_item'
1562 AND entity_id = {$lineItem}";
1563 $result = CRM_Core_DAO
::singleValueQuery($query);
1571 function _checkFinancialItem($contId, $context) {
1572 if ($context != 'paylater') {
1574 'entity_id' => $contId,
1575 'entity_table' => 'civicrm_contribution',
1577 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1578 $entityParams = array(
1579 'financial_trxn_id' => $trxn['financial_trxn_id'],
1580 'entity_table' => 'civicrm_financial_item',
1582 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1584 'id' => $entityTrxn['entity_id'],
1587 if ($context == 'paylater') {
1588 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1589 foreach ($lineItems as $key=>$item) {
1591 'entity_id' => $key,
1592 'entity_table' => 'civicrm_line_item',
1594 $compareParams = array ('status_id' => 1);
1595 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1598 elseif ($context == 'refund') {
1599 $compareParams = array(
1601 'financial_account_id' => 1,
1605 elseif ($context == 'cancelPending') {
1606 $compareParams = array(
1608 'financial_account_id' => 1,
1612 elseif ($context == 'changeFinancial') {
1613 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1615 'entity_id' => $lineKey,
1618 $compareParams = array(
1619 'financial_account_id' => 1,
1621 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1623 'financial_account_id' => 3,
1624 'entity_id' => $lineKey,
1626 $compareParams = array(
1630 if ($context != 'paylater') {
1631 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1636 * @param $contribution
1638 * @param null $instrumentId
1640 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1641 $trxnParams = array(
1642 'entity_id' => $contribution['id'],
1643 'entity_table' => 'civicrm_contribution',
1645 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1647 'id' => $trxn['financial_trxn_id'],
1649 if ($context == 'payLater') {
1650 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1651 $compareParams = array(
1653 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1656 elseif ($context == 'refund') {
1657 $compareParams = array(
1658 'to_financial_account_id' => 6,
1659 'total_amount' => -100,
1663 elseif ($context == 'cancelPending') {
1664 $compareParams = array(
1665 'from_financial_account_id' => 7,
1666 'total_amount' => -100,
1670 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1671 $entityParams = array(
1672 'entity_id' => $contribution['id'],
1673 'entity_table' => 'civicrm_contribution',
1676 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1677 $trxnParams1 = array(
1678 'id' => $trxn['financial_trxn_id'],
1680 $compareParams = array(
1681 'total_amount' => -100,
1684 if ($context == 'paymentInstrument') {
1685 $compareParams +
= array(
1686 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1687 'payment_instrument_id' => 4,
1691 $compareParams['to_financial_account_id'] = 12;
1693 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1694 $compareParams['total_amount'] = 100;
1695 if ($context == 'paymentInstrument') {
1696 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1697 $compareParams['payment_instrument_id'] = $instrumentId;
1700 $compareParams['to_financial_account_id'] = 12;
1704 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1710 function _addPaymentInstrument () {
1711 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1712 $optionParams = array(
1713 'option_group_id' => $gId,
1714 'label' => 'Test Card',
1715 'name' => 'Test Card',
1720 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1721 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1722 $financialParams = array(
1723 'entity_table' => 'civicrm_option_value',
1724 'entity_id' => $optionValue['id'],
1725 'account_relationship' => $relationTypeId,
1726 'financial_account_id' => 7,
1728 $financialType = CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1729 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1730 return $optionValue['values'][$optionValue['id']]['value'];
1737 function _checkFinancialRecords($params,$context) {
1738 $entityParams = array(
1739 'entity_id' => $params['id'],
1740 'entity_table' => 'civicrm_contribution',
1742 if ($context == 'pending') {
1743 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1744 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1747 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1748 $trxnParams = array(
1749 'id' => $trxn['financial_trxn_id'],
1751 if ($context != 'online' && $context != 'payLater') {
1752 $compareParams = array(
1753 'to_financial_account_id' => 6,
1754 'total_amount' => 100,
1758 if ($context == 'feeAmount') {
1759 $compareParams['fee_amount'] = 50;
1761 elseif ($context == 'online') {
1762 $compareParams = array(
1763 'to_financial_account_id' => 12,
1764 'total_amount' => 100,
1768 elseif ($context == 'payLater') {
1769 $compareParams = array(
1770 'to_financial_account_id' => 7,
1771 'total_amount' => 100,
1775 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams);
1776 $entityParams = array(
1777 'financial_trxn_id' => $trxn['financial_trxn_id'],
1778 'entity_table' => 'civicrm_financial_item',
1780 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1781 $fitemParams = array(
1782 'id' => $entityTrxn['entity_id'],
1784 $compareParams = array(
1787 'financial_account_id' => 1,
1789 if ($context == 'payLater') {
1790 $compareParams = array(
1793 'financial_account_id' => 1,
1796 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1797 if ($context == 'feeAmount') {
1799 'entity_id' => $params['id'],
1800 'entity_table' => 'civicrm_contribution',
1802 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1803 $trxnParams = array(
1804 'id' => $maxTrxn['financial_trxn_id'],
1806 $compareParams = array(
1807 'to_financial_account_id' => 5,
1808 'from_financial_account_id' => 6,
1809 'total_amount' => 50,
1812 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1813 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1814 $fitemParams = array(
1815 'entity_id' => $trxnId['financialTrxnId'],
1816 'entity_table' => 'civicrm_financial_trxn',
1818 $compareParams = array(
1821 'financial_account_id' => 5,
1823 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);