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', 'update', 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', 'update', $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', 'update', $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', 'update', $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', 'update', $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', 'update', $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', 'update', $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', 'update', $newParams);
1006 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1007 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1010 //To Update Contribution
1011 //CHANGE: we require the API to do an incremental update
1012 function testCreateUpdateContribution() {
1014 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1015 $old_params = array(
1016 'contribution_id' => $contributionID,
1019 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1020 //Make sure it came back
1021 $this->assertAPISuccess($original, 'In line ' . __LINE__
);
1022 $this->assertEquals($original['id'], $contributionID, 'In line ' . __LINE__
);
1023 //set up list of old params, verify
1025 //This should not be required on update:
1026 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1027 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1028 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1029 $old_source = $original['values'][$contributionID]['contribution_source'];
1031 //note: current behavior is to return ISO. Is this
1032 //documented behavior? Is this correct
1033 $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date']));
1035 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1036 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1038 //check against values in CiviUnitTestCase::createContribution()
1039 $this->assertEquals($old_contact_id, $this->_individualId
, 'In line ' . __LINE__
);
1040 $this->assertEquals($old_fee_amount, 5.00, 'In line ' . __LINE__
);
1041 $this->assertEquals($old_source, 'SSF', 'In line ' . __LINE__
);
1042 $this->assertEquals($old_trxn_id, 212355, 'In line ' . __LINE__
);
1043 $this->assertEquals($old_invoice_id, 'idofsh', 'In line ' . __LINE__
);
1045 'id' => $contributionID,
1046 'contact_id' => $this->_individualId
,
1047 'total_amount' => 110.00,
1048 'financial_type_id' => $this->_financialTypeId
,
1049 'non_deductible_amount' => 10.00,
1050 'net_amount' => 100.00,
1051 'contribution_status_id' => 1,
1052 'note' => 'Donating for Nobel Cause',
1056 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1058 $new_params = array(
1059 'contribution_id' => $contribution['id'],
1062 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1064 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
1065 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00, 'In line ' . __LINE__
);
1066 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
, 'In line ' . __LINE__
);
1067 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument, 'In line ' . __LINE__
);
1068 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00, 'In line ' . __LINE__
);
1069 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount, 'In line ' . __LINE__
);
1070 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00, 'In line ' . __LINE__
);
1071 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id, 'In line ' . __LINE__
);
1072 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id, 'In line ' . __LINE__
);
1073 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source, 'In line ' . __LINE__
);
1074 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed', 'In line ' . __LINE__
);
1076 'contribution_id' => $contributionID,
1079 $result = $this->callAPISuccess('contribution', 'delete', $params);
1080 $this->assertAPISuccess($result, 'in line' . __LINE__
);
1083 ///////////////// civicrm_contribution_delete methods
1084 function testDeleteEmptyParamsContribution() {
1086 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1089 function testDeleteParamsNotArrayContribution() {
1090 $params = 'contribution_id= 1';
1091 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1092 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1095 function testDeleteWrongParamContribution() {
1097 'contribution_source' => 'SSF',
1100 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1103 function testDeleteContribution() {
1105 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1107 'id' => $contributionID,
1109 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1113 * Test civicrm_contribution_search with empty params.
1114 * All available contributions expected.
1116 function testSearchEmptyParams() {
1120 'contact_id' => $this->_individualId
,
1121 'receive_date' => date('Ymd'),
1122 'total_amount' => 100.00,
1123 'financial_type_id' => $this->_financialTypeId
,
1124 'non_deductible_amount' => 10.00,
1125 'fee_amount' => 5.00,
1126 'net_amount' => 95.00,
1128 'invoice_id' => 78910,
1130 'contribution_status_id' => 1,
1133 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1135 $result = $this->callAPISuccess('contribution', 'get', $params);
1136 // We're taking the first element.
1137 $res = $result['values'][$contribution['id']];
1139 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
1140 $this->assertEquals($p['total_amount'], $res['total_amount'], 'In line ' . __LINE__
);
1141 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__
);
1142 $this->assertEquals($p['net_amount'], $res['net_amount'], 'In line ' . __LINE__
);
1143 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__
);
1144 $this->assertEquals($p['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__
);
1145 $this->assertEquals($p['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__
);
1146 $this->assertEquals($p['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__
);
1147 $this->assertEquals($p['source'], $res['contribution_source'], 'In line ' . __LINE__
);
1148 // contribution_status_id = 1 => Completed
1149 $this->assertEquals('Completed', $res['contribution_status'], 'In line ' . __LINE__
);
1151 $this->contributionDelete($contribution['id']);
1155 * Test civicrm_contribution_search. Success expected.
1157 function testSearch() {
1159 'contact_id' => $this->_individualId
,
1160 'receive_date' => date('Ymd'),
1161 'total_amount' => 100.00,
1162 'financial_type_id' => $this->_financialTypeId
,
1163 'non_deductible_amount' => 10.00,
1164 'contribution_status_id' => 1,
1167 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1170 'contact_id' => $this->_individualId
,
1171 'receive_date' => date('Ymd'),
1172 'total_amount' => 200.00,
1173 'financial_type_id' => $this->_financialTypeId
,
1174 'non_deductible_amount' => 20.00,
1175 'trxn_id' => 5454565,
1176 'invoice_id' => 1212124,
1177 'fee_amount' => 50.00,
1178 'net_amount' => 60.00,
1179 'contribution_status_id' => 2,
1182 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1185 'contribution_id' => $contribution2['id'],
1188 $result = $this->callAPISuccess('contribution', 'get', $params);
1189 $res = $result['values'][$contribution2['id']];
1191 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
1192 $this->assertEquals($p2['total_amount'], $res['total_amount'], 'In line ' . __LINE__
);
1193 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__
);
1194 $this->assertEquals($p2['net_amount'], $res['net_amount'], 'In line ' . __LINE__
);
1195 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__
);
1196 $this->assertEquals($p2['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__
);
1197 $this->assertEquals($p2['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__
);
1198 $this->assertEquals($p2['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__
);
1199 // contribution_status_id = 2 => Pending
1200 $this->assertEquals('Pending', $res['contribution_status'], 'In line ' . __LINE__
);
1202 $this->contributionDelete($contribution1['id']);
1203 $this->contributionDelete($contribution2['id']);
1207 * Test completing a transaction via the API
1209 * Note that we are creating a logged in user because email goes out from
1212 function testCompleteTransaction() {
1213 $mut = new CiviMailUtils( $this, true );
1214 $this->createLoggedInUser();
1215 $params = array_merge($this->_params
, array('contribution_status_id' => 2,));
1216 $contribution = $this->callAPISuccess('contribution','create', $params);
1217 $this->callAPISuccess('contribution', 'completetransaction', array(
1218 'id' => $contribution['id'],
1220 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1221 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1222 $mut->checkMailLog(array(
1223 'Receipt - Contribution',
1224 'Please print this confirmation for your records.',
1231 * Test completing a transaction via the API
1235 function testCompleteTransactionWithReceiptDateSet() {
1236 $mut = new CiviMailUtils( $this, true );
1237 $this->createLoggedInUser();
1238 $params = array_merge($this->_params
, array('contribution_status_id' => 2,'receipt_date' => 'now'));
1239 $contribution = $this->callAPISuccess('contribution','create', $params);
1240 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1241 'id' => $contribution['id'],
1244 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1245 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1246 $mut->checkMailLog(array(
1247 'Receipt - Contribution',
1248 'Please print this confirmation for your records.',
1254 * Test completing a transaction with an event via the API
1256 * Note that we are creating a logged in user because email goes out from
1259 function testCompleteTransactionWithParticipantRecord() {
1260 $mut = new CiviMailUtils( $this, true );
1261 $mut->clearMessages();
1262 $this->createLoggedInUser();
1263 $contributionID = $this->createPendingParticipantContribution();
1264 $this->callAPISuccess('contribution', 'completetransaction', array(
1265 'id' => $contributionID,)
1267 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids
['participant'], 'return' => 'participant_status_id'));
1268 $this->assertEquals(1, $participantStatus);
1269 $mut->checkMailLog(array(
1270 'Annual CiviCRM meet',
1272 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1278 * test membership is renewed when transaction completed
1280 function testCompleteTransactionMembershipPriceSet() {
1281 $this->createPriceSetWithPage('membership');
1282 $this->setUpPendingContribution($this->_ids
['price_field_value'][0]);
1283 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1284 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1285 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1286 $this->cleanUpAfterPriceSets();
1290 * test membership is renewed when transaction completed
1292 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1293 $this->createPriceSetWithPage('membership');
1294 $this->setUpPendingContribution($this->_ids
['price_field_value'][1]);
1295 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids
['contribution']));
1296 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids
['membership']));
1297 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1298 $this->cleanUpAfterPriceSets();
1301 function cleanUpAfterPriceSets() {
1302 $this->quickCleanUpFinancialEntities();
1303 $this->contactDelete($this->_ids
['contact']);
1304 $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids
['price_field_value'][0]));
1305 $this->callAPISuccess('price_field_value', 'delete', array('id' => $this->_ids
['price_field_value'][1]));
1306 $this->callAPISuccess('price_field', 'delete', array('id' => $this->_ids
['price_field'][0]));
1307 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids
['price_set']));
1312 * this could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1313 * on parent class at some point (fn is not in 4.4)
1315 * @param array $params
1317 function createPriceSetWithPage($entity, $params = array()) {
1318 $membershipTypeID = $this->membershipTypeCreate();
1319 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1320 'title' => "Test Contribution Page",
1321 'financial_type_id' => 1,
1322 'currency' => 'NZD',
1323 'goal_amount' => 50,
1324 'is_pay_later' => 1,
1325 'is_monetary' => TRUE,
1326 'is_email_receipt' => FALSE,
1328 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1329 'is_quick_config' => 0,
1330 'extends' => 'CiviMember',
1331 'financial_type_id' => 1,
1332 'title' => 'my Page'
1334 $priceSetID = $priceSet['id'];
1336 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1337 $priceField = $this->callAPISuccess('price_field', 'create', array(
1338 'price_set_id' => $priceSetID ,
1339 'label' => 'Goat Breed',
1340 'html_type' => 'Radio',
1342 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1343 'price_set_id' => $priceSetID ,
1344 'price_field_id' => $priceField['id'],
1345 'label' => 'Long Haired Goat',
1347 'membership_type_id' => $membershipTypeID,
1348 'membership_num_terms' => 1,
1351 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
1352 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1353 'price_set_id' => $priceSetID ,
1354 'price_field_id' => $priceField['id'],
1355 'label' => 'Shoe-eating Goat',
1357 'membership_type_id' => $membershipTypeID,
1358 'membership_num_terms' => 2,
1361 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
1362 $this->_ids
['price_set'] = $priceSetID;
1363 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
1364 $this->_ids
['price_field'] = array($priceField['id']);
1366 $this->_ids
['membership_type'] = $membershipTypeID;
1370 * Set up a pending transaction with a specific price field id
1371 * @param $priceFieldValueID
1373 function setUpPendingContribution($priceFieldValueID){
1374 $contactID = $this->individualCreate();
1375 $membership = $this->callAPISuccess('membership', 'create', array(
1376 'contact_id' => $contactID,
1377 'membership_type_id' => $this->_ids
['membership_type'],
1378 'start_date' => 'yesterday - 1 year',
1379 'end_date' => 'yesterday',
1381 $contribution = $this->callAPISuccess('contribution', 'create', array(
1383 'contact_id' => $contactID,
1384 'receive_date' => date('Ymd'),
1385 'total_amount' => 100.00,
1386 'financial_type_id' => 1,
1387 'payment_instrument_id' => 'Credit Card',
1388 'non_deductible_amount' => 10.00,
1389 'trxn_id' => 'jdhfi88',
1390 'invoice_id' => 'djfhiewuyr',
1392 'contribution_status_id' => 2,
1393 'contribution_page_id' => $this->_ids
['contribution_page'],
1394 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1397 $this->callAPISuccess('line_item', 'create', array(
1398 'entity_id' => $contribution['id'],
1399 'entity_table' => 'civicrm_contribution',
1400 'contribution_id' => $contribution['id'],
1401 'price_field_id' => $this->_ids
['price_field'][0],
1405 'financial_type_id' => 1,
1406 'price_field_value_id' => $priceFieldValueID,
1408 $this->_ids
['contact'] = $contactID;
1409 $this->_ids
['contribution'] = $contribution['id'];
1410 $this->_ids
['membership'] = $membership['id'];
1414 * Test sending a mail via the API
1416 function testSendMail() {
1417 $mut = new CiviMailUtils( $this, true );
1418 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1419 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
1421 'id' => $contribution['id'],
1422 'receipt_from_email' => 'api@civicrm.org',
1425 $mut->checkMailLog(array(
1427 'Contribution Information',
1428 'Please print this confirmation for your records',
1437 * Test sending a mail via the API
1439 function testSendMailEvent() {
1440 $mut = new CiviMailUtils( $this, true );
1441 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1442 $event = $this->eventCreate(array(
1443 'is_email_confirm' => 1,
1444 'confirm_from_email' => 'test@civicrm.org',
1446 $this->_eventID
= $event['id'];
1447 $participantParams = array(
1448 'contact_id' => $this->_individualId
,
1449 'event_id' => $this->_eventID
,
1452 // to ensure it matches later on
1453 'register_date' => '2007-07-21 00:00:00',
1454 'source' => 'Online Event Registration: API Testing',
1457 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1458 $this->callAPISuccess('participant_payment', 'create', array(
1459 'participant_id' => $participant['id'],
1460 'contribution_id' => $contribution['id'],
1462 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1463 'id' => $contribution['id'],
1464 'receipt_from_email' => 'api@civicrm.org',
1468 $mut->checkMailLog(array(
1469 'Annual CiviCRM meet',
1471 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1480 * This function does a GET & compares the result against the $params
1481 * Use as a double check on Creates
1483 function contributionGetnCheck($params, $id, $delete = 1) {
1485 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1491 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1493 $this->assertAPISuccess($contribution, 0, 'In line ' . __LINE__
);
1494 $values = $contribution['values'][$contribution['id']];
1495 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1496 // this is not returned in id format
1497 unset($params['payment_instrument_id']);
1498 $params['contribution_source'] = $params['source'];
1499 unset($params['source']);
1500 foreach ($params as $key => $value) {
1501 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
1506 * Create a pending contribution & linked pending participant record
1507 * (along with an event)
1509 function createPendingParticipantContribution(){
1510 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1511 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1512 $this->ids
['participant'] = $participantID;
1513 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1514 $contribution = $this->callAPISuccess('contribution','create', $params);
1515 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1516 $lineItem = $this->callAPISuccess('line_item', 'get', array(
1517 'entity_id' => $contribution['id'],
1518 'entity_table' => 'civicrm_contribution',
1519 'api.line_item.create' => array(
1520 'entity_id' => $participantID,
1521 'entity_table' => 'civicrm_participant',
1524 return $contribution['id'];
1530 * @return null|string
1531 */function _getFinancialTrxnAmount($contId) {
1533 SUM( ft.total_amount ) AS total
1534 FROM civicrm_financial_trxn AS ft
1535 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1536 WHERE ceft.entity_table = 'civicrm_contribution'
1537 AND ceft.entity_id = {$contId}";
1539 $result = CRM_Core_DAO
::singleValueQuery($query);
1546 * @return null|string
1547 */function _getFinancialItemAmount($contId) {
1548 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1551 FROM civicrm_financial_item
1552 WHERE entity_table = 'civicrm_line_item'
1553 AND entity_id = {$lineItem}";
1554 $result = CRM_Core_DAO
::singleValueQuery($query);
1562 function _checkFinancialItem($contId, $context) {
1563 if ($context != 'paylater') {
1565 'entity_id' => $contId,
1566 'entity_table' => 'civicrm_contribution',
1568 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1569 $entityParams = array(
1570 'financial_trxn_id' => $trxn['financial_trxn_id'],
1571 'entity_table' => 'civicrm_financial_item',
1573 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1575 'id' => $entityTrxn['entity_id'],
1578 if ($context == 'paylater') {
1579 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1580 foreach ($lineItems as $key=>$item) {
1582 'entity_id' => $key,
1583 'entity_table' => 'civicrm_line_item',
1585 $compareParams = array ('status_id' => 1);
1586 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1589 elseif ($context == 'refund') {
1590 $compareParams = array(
1592 'financial_account_id' => 1,
1596 elseif ($context == 'cancelPending') {
1597 $compareParams = array(
1599 'financial_account_id' => 1,
1603 elseif ($context == 'changeFinancial') {
1604 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1606 'entity_id' => $lineKey,
1609 $compareParams = array(
1610 'financial_account_id' => 1,
1612 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1614 'financial_account_id' => 3,
1615 'entity_id' => $lineKey,
1617 $compareParams = array(
1621 if ($context != 'paylater') {
1622 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1627 * @param $contribution
1629 * @param null $instrumentId
1631 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1632 $trxnParams = array(
1633 'entity_id' => $contribution['id'],
1634 'entity_table' => 'civicrm_contribution',
1636 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1638 'id' => $trxn['financial_trxn_id'],
1640 if ($context == 'payLater') {
1641 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1642 $compareParams = array(
1644 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1647 elseif ($context == 'refund') {
1648 $compareParams = array(
1649 'to_financial_account_id' => 6,
1650 'total_amount' => -100,
1654 elseif ($context == 'cancelPending') {
1655 $compareParams = array(
1656 'from_financial_account_id' => 7,
1657 'total_amount' => -100,
1661 elseif ($context == 'changeFinancial' ||
$context == 'paymentInstrument') {
1662 $entityParams = array(
1663 'entity_id' => $contribution['id'],
1664 'entity_table' => 'civicrm_contribution',
1667 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1668 $trxnParams1 = array(
1669 'id' => $trxn['financial_trxn_id'],
1671 $compareParams = array(
1672 'total_amount' => -100,
1675 if ($context == 'paymentInstrument') {
1676 $compareParams +
= array(
1677 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount(4),
1678 'payment_instrument_id' => 4,
1682 $compareParams['to_financial_account_id'] = 12;
1684 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1685 $compareParams['total_amount'] = 100;
1686 if ($context == 'paymentInstrument') {
1687 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($instrumentId);
1688 $compareParams['payment_instrument_id'] = $instrumentId;
1691 $compareParams['to_financial_account_id'] = 12;
1695 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1701 function _addPaymentInstrument () {
1702 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1703 $optionParams = array(
1704 'option_group_id' => $gId,
1705 'label' => 'Test Card',
1706 'name' => 'Test Card',
1711 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1712 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1713 $financialParams = array(
1714 'entity_table' => 'civicrm_option_value',
1715 'entity_id' => $optionValue['id'],
1716 'account_relationship' => $relationTypeId,
1717 'financial_account_id' => 7,
1719 $financialType = CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1720 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1721 return $optionValue['values'][$optionValue['id']]['value'];
1728 function _checkFinancialRecords($params,$context) {
1729 $entityParams = array(
1730 'entity_id' => $params['id'],
1731 'entity_table' => 'civicrm_contribution',
1733 if ($context == 'pending') {
1734 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1735 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1738 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1739 $trxnParams = array(
1740 'id' => $trxn['financial_trxn_id'],
1742 if ($context != 'online' && $context != 'payLater') {
1743 $compareParams = array(
1744 'to_financial_account_id' => 6,
1745 'total_amount' => 100,
1749 if ($context == 'feeAmount') {
1750 $compareParams['fee_amount'] = 50;
1752 elseif ($context == 'online') {
1753 $compareParams = array(
1754 'to_financial_account_id' => 12,
1755 'total_amount' => 100,
1759 elseif ($context == 'payLater') {
1760 $compareParams = array(
1761 'to_financial_account_id' => 7,
1762 'total_amount' => 100,
1766 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams);
1767 $entityParams = array(
1768 'financial_trxn_id' => $trxn['financial_trxn_id'],
1769 'entity_table' => 'civicrm_financial_item',
1771 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1772 $fitemParams = array(
1773 'id' => $entityTrxn['entity_id'],
1775 $compareParams = array(
1778 'financial_account_id' => 1,
1780 if ($context == 'payLater') {
1781 $compareParams = array(
1784 'financial_account_id' => 1,
1787 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1788 if ($context == 'feeAmount') {
1790 'entity_id' => $params['id'],
1791 'entity_table' => 'civicrm_contribution',
1793 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1794 $trxnParams = array(
1795 'id' => $maxTrxn['financial_trxn_id'],
1797 $compareParams = array(
1798 'to_financial_account_id' => 5,
1799 'from_financial_account_id' => 6,
1800 'total_amount' => 50,
1803 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1804 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1805 $fitemParams = array(
1806 'entity_id' => $trxnId['financialTrxnId'],
1807 'entity_table' => 'civicrm_financial_trxn',
1809 $compareParams = array(
1812 'financial_account_id' => 5,
1814 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);