3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CiviTest/CiviMailUtils.php';
33 * Test APIv3 civicrm_contribute_* functions
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
39 class api_v3_ContributionTest
extends CiviUnitTestCase
{
42 * Assume empty database with just civicrm_data
44 protected $_individualId;
45 protected $_contribution;
46 protected $_financialTypeId = 1;
47 protected $_apiversion;
48 protected $_entity = 'Contribution';
51 public $_eNoticeCompliant = TRUE;
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 'entity_table' => 'civicrm_contribution',
402 $this->assertEquals(2, $lineItems['count']);
405 function testCreateContributionOffline() {
407 'contact_id' => $this->_individualId
,
408 'receive_date' => '20120511',
409 'total_amount' => 100.00,
410 'financial_type_id' => 1,
412 'invoice_id' => 67890,
414 'contribution_status_id' => 1,
417 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
418 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
419 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
420 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
421 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
422 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
423 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
424 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__
);
425 $lineItems = $this->callAPISuccess('line_item','get',array(
426 'entity_id' => $contribution['id'],
427 'entity_table' => 'civicrm_contribution',
430 $this->assertEquals(1, $lineItems['count']);
431 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
432 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
433 $lineItems = $this->callAPISuccess('line_item','get',array(
434 'entity_id' => $contribution['id'],
435 'entity_table' => 'civicrm_contribution',
438 $this->assertEquals(1, $lineItems['count']);
439 $this->_checkFinancialRecords($contribution, 'offline');
440 $this->contributionGetnCheck($params, $contribution['id']);
443 * test create with valid payment instument
445 function testCreateContributionWithPaymentInstrument() {
446 $params = $this->_params +
array('payment_instrument' => 'EFT');
447 $contribution = $this->callAPISuccess('contribution', 'create', $params);
448 $contribution = $this->callAPISuccess('contribution','get', array(
450 'id' => $contribution['id']
452 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
453 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
455 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
456 $contribution = $this->callAPISuccess('contribution','get', array(
458 'id' => $contribution['id']
460 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
461 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
464 function testGetContributionByPaymentInstrument() {
465 $params = $this->_params +
array('payment_instrument' => 'EFT');
466 $params2 = $this->_params +
array('payment_instrument' => 'Cash');
467 $this->callAPISuccess('contribution','create',$params);
468 $this->callAPISuccess('contribution','create',$params2);
469 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash'));
470 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
471 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
472 $this->assertEquals(1,$contribution['count']);
473 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
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_id' => 5));
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','get',array( 'sequential' => 1, 'payment_instrument' => 'EFT'));
482 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
483 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
484 $this->assertEquals(1, $contribution['count']);
485 $contribution = $this->callAPISuccess('contribution', 'update', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
486 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
487 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
488 $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']);
489 $this->assertEquals(1,$contribution['count']);
493 * Create test with unique field name on source
495 function testCreateContributionSource() {
498 'contact_id' => $this->_individualId
,
499 'receive_date' => date('Ymd'),
500 'total_amount' => 100.00,
501 'financial_type_id' => $this->_financialTypeId
,
502 'payment_instrument_id' => 1,
503 'non_deductible_amount' => 10.00,
504 'fee_amount' => 50.00,
505 'net_amount' => 90.00,
507 'invoice_id' => 67890,
508 'contribution_source' => 'SSF',
509 'contribution_status_id' => 1,
512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
513 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
514 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
517 * Create test with unique field name on source
519 function testCreateContributionSourceInvalidContac() {
523 'receive_date' => date('Ymd'),
524 'total_amount' => 100.00,
525 'financial_type_id' => $this->_financialTypeId
,
526 'payment_instrument_id' => 1,
527 'non_deductible_amount' => 10.00,
528 'fee_amount' => 50.00,
529 'net_amount' => 90.00,
531 'invoice_id' => 67890,
532 'contribution_source' => 'SSF',
533 'contribution_status_id' => 1,
536 $contribution = $this->callAPIFailure('contribution', 'create', $params,
537 'contact_id is not valid : 999');
540 function testCreateContributionSourceInvalidContContac() {
543 'contribution_contact_id' => 999,
544 'receive_date' => date('Ymd'),
545 'total_amount' => 100.00,
546 'financial_type_id' => $this->_financialTypeId
,
547 'payment_instrument_id' => 1,
548 'non_deductible_amount' => 10.00,
549 'fee_amount' => 50.00,
550 'net_amount' => 90.00,
552 'invoice_id' => 67890,
553 'contribution_source' => 'SSF',
554 'contribution_status_id' => 1,
557 $contribution = $this->callAPIFailure('contribution', 'create', $params,
558 'contact_id is not valid : 999', 'In line ' . __LINE__
);
561 function testCreateContributionWithNote() {
562 $description = "Demonstrates creating contribution with Note Entity";
563 $subfile = "ContributionCreateWithNote";
565 'contact_id' => $this->_individualId
,
566 'receive_date' => '2012-01-01',
567 'total_amount' => 100.00,
568 'financial_type_id' => $this->_financialTypeId
,
569 'payment_instrument_id' => 1,
570 'non_deductible_amount' => 10.00,
571 'fee_amount' => 50.00,
572 'net_amount' => 90.00,
574 'invoice_id' => 67890,
576 'contribution_status_id' => 1,
577 'note' => 'my contribution note',
580 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
581 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
582 $this->assertEquals('my contribution note', $result['values'][0]['note']);
583 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
586 function testCreateContributionWithNoteUniqueNameAliases() {
588 'contact_id' => $this->_individualId
,
589 'receive_date' => '2012-01-01',
590 'total_amount' => 100.00,
591 'financial_type_id' => $this->_financialTypeId
,
592 'payment_instrument_id' => 1,
593 'non_deductible_amount' => 10.00,
594 'fee_amount' => 50.00,
595 'net_amount' => 90.00,
597 'invoice_id' => 67890,
599 'contribution_status_id' => 1,
600 'contribution_note' => 'my contribution note',
603 $contribution = $this->callAPISuccess('contribution', 'create', $params);
604 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
605 $this->assertEquals('my contribution note', $result['values'][0]['note']);
606 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
609 * This is the test for creating soft credits - however a 'get' is not yet possible via API
610 * as the current BAO functions are contact-centric (from what I can find)
613 function testCreateContributionWithSoftCredt() {
614 $description = "Demonstrates creating contribution with SoftCredit";
615 $subfile = "ContributionCreateWithSoftCredit";
616 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
617 $params = $this->_params +
array(
618 'soft_credit_to' => $contact2['id'],
622 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
623 // $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit_to', 'sequential' => 1));
624 // $this->assertEquals($contact2['id'], $result['values'][$result['id']]['soft_credit_to']) ;
625 // well - the above doesn't work yet so lets do SQL
626 $query = "SELECT count(*) FROM civicrm_contribution_soft WHERE contact_id = " . $contact2['id'];
628 $count = CRM_Core_DAO
::singleValueQuery($query);
629 $this->assertEquals(1, $count);
631 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
632 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
636 * Test using example code
638 function testContributionCreateExample() {
639 //make sure at least on page exists since there is a truncate in tear down
640 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams
);
641 require_once 'api/v3/examples/ContributionCreate.php';
642 $result = contribution_create_example();
643 $this->assertAPISuccess($result);
644 $contributionId = $result['id'];
645 $expectedResult = contribution_create_expectedresult();
646 $this->checkArrayEquals($result, $expectedResult);
647 $this->contributionDelete($contributionId);
651 * Function tests that additional financial records are created when fee amount is recorded
653 function testCreateContributionWithFee() {
655 'contact_id' => $this->_individualId
,
656 'receive_date' => '20120511',
657 'total_amount' => 100.00,
659 'financial_type_id' => 1,
661 'invoice_id' => 67890,
663 'contribution_status_id' => 1,
666 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
667 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
668 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
669 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
670 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
671 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
672 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
673 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__
);
674 $lineItems = $this->callAPISuccess('line_item','get',array(
676 'entity_id' => $contribution['id'],
677 'entity_table' => 'civicrm_contribution',
680 $this->assertEquals(1, $lineItems['count']);
681 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
682 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
683 $lineItems = $this->callAPISuccess('line_item','get',array(
685 'entity_id' => $contribution['id'],
686 'entity_table' => 'civicrm_contribution',
689 $this->assertEquals(1, $lineItems['count']);
690 $this->_checkFinancialRecords($contribution, 'feeAmount');
695 * Function tests that additional financial records are created when online contribution is created
697 function testCreateContributionOnline() {
698 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
699 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams
);
701 'contact_id' => $this->_individualId
,
702 'receive_date' => '20120511',
703 'total_amount' => 100.00,
704 'financial_type_id' => 1,
705 'contribution_page_id' => $contributionPage['id'],
706 'payment_processor' => 1,
708 'invoice_id' => 67890,
710 'contribution_status_id' => 1,
714 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
715 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
716 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
717 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
718 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
719 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
720 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
721 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__
);
722 $this->_checkFinancialRecords($contribution, 'online');
726 * in the interests of removing financial type / contribution type checks from
727 * legacy format function lets test that the api is doing this for us
729 function testCreateInvalidFinancialType() {
730 $params = $this->_params
;
731 $params['financial_type_id'] = 99999;
732 $result = $this->callAPIFailure($this->_entity
, 'create', $params, "'99999' is not a valid option for field financial_type_id");
736 * in the interests of removing financial type / contribution type checks from
737 * legacy format function lets test that the api is doing this for us
739 function testValidNamedFinancialType() {
740 $params = $this->_params
;
741 $params['financial_type_id'] = 'Donation';
742 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
746 * Function tests that additional financial records are created when online contribution with pay later option
749 function testCreateContributionPayLaterOnline() {
750 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
751 $this->_pageParams
['is_pay_later'] = 1;
752 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams
);
754 'contact_id' => $this->_individualId
,
755 'receive_date' => '20120511',
756 'total_amount' => 100.00,
757 'financial_type_id' => 1,
758 'contribution_page_id' => $contributionPage['id'],
761 'invoice_id' => 67890,
763 'contribution_status_id' => 2,
767 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
768 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
769 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
770 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
771 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
772 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
773 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
774 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__
);
775 $this->_checkFinancialRecords($contribution, 'payLater');
779 * Function tests that additional financial records are created when online contribution with pending option
782 function testCreateContributionPendingOnline() {
783 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor
::create($this->_processorParams
);
784 $contributionPage = $this->callAPISuccess( 'contribution_page', 'create', $this->_pageParams
);
786 'contact_id' => $this->_individualId
,
787 'receive_date' => '20120511',
788 'total_amount' => 100.00,
789 'financial_type_id' => 1,
790 'contribution_page_id' => $contributionPage['id'],
792 'invoice_id' => 67890,
794 'contribution_status_id' => 2,
797 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
);
798 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
799 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__
);
800 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__
);
801 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__
);
802 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__
);
803 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__
);
804 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__
);
805 $this->_checkFinancialRecords($contribution, 'pending');
809 * Function tests that line items, financial records are updated when contribution amount is changed
811 function testCreateUpdateContributionChangeTotal() {
812 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params
);
813 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
815 'entity_id' => $contribution['id'],
816 'entity_table' => 'civicrm_contribution',
818 'return' => 'line_total',
820 $this->assertEquals('100.00', $lineItems);
821 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
822 // Financial trxn SUM = 100 + 5 (fee)
823 $this->assertEquals('105.00', $trxnAmount);
826 'id' => $contribution['id'],
827 'total_amount' => '125');
828 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
830 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
832 'entity_id' => $contribution['id'],
833 'entity_table' => 'civicrm_contribution',
835 'return' => 'line_total',
838 $this->assertEquals('125.00', $lineItems);
839 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
840 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
841 // Financial trxn SUM = 125 + 5 (fee)
842 $this->assertEquals('130.00', $trxnAmount);
843 $this->assertEquals('125.00', $fitemAmount);
847 * Function tests that line items, financial records are updated when pay later contribution is received
849 function testCreateUpdateContributionPayLater() {
850 $contribParams = array(
851 'contact_id' => $this->_individualId
,
852 'receive_date' => '2012-01-01',
853 'total_amount' => 100.00,
854 'financial_type_id' => $this->_financialTypeId
,
855 'payment_instrument_id' => 1,
856 'contribution_status_id' => 2,
860 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
862 $newParams = array_merge($contribParams, array(
863 'id' => $contribution['id'],
864 'contribution_status_id' => 1,)
866 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
867 $contribution = $contribution['values'][$contribution['id']];
868 $this->assertEquals($contribution['contribution_status_id'],'1');
869 $this->_checkFinancialItem($contribution['id'], 'paylater');
870 $this->_checkFinancialTrxn($contribution, 'payLater');
874 * Function tests that financial records are updated when Payment Instrument is changed
876 function testCreateUpdateContributionPaymentInstrument() {
877 $instrumentId = $this->_addPaymentInstrument();
878 $contribParams = array(
879 'contact_id' => $this->_individualId
,
880 'total_amount' => 100.00,
881 'financial_type_id' => $this->_financialTypeId
,
882 'payment_instrument_id' => 4,
883 'contribution_status_id' => 1,
886 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
888 $newParams = array_merge($contribParams, array(
889 'id' => $contribution['id'],
890 'payment_instrument_id' => $instrumentId,)
892 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
893 $this->_checkFinancialTrxn($contribution, 'paymentInstrument');
897 * Function tests that financial records are added when Contribution is Refunded
899 function testCreateUpdateContributionRefund() {
900 $contribParams = array(
901 'contact_id' => $this->_individualId
,
902 'receive_date' => '2012-01-01',
903 'total_amount' => 100.00,
904 'financial_type_id' => $this->_financialTypeId
,
905 'payment_instrument_id' => 4,
906 'contribution_status_id' => 1,
909 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
910 $newParams = array_merge($contribParams, array(
911 'id' => $contribution['id'],
912 'contribution_status_id' => 7,
916 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
917 $this->_checkFinancialTrxn($contribution, 'refund');
918 $this->_checkFinancialItem($contribution['id'], 'refund');
922 * Function tests invalid contribution status change
924 function testCreateUpdateContributionInValidStatusChange() {
925 $contribParams = array(
927 'receive_date' => '2012-01-01',
928 'total_amount' => 100.00,
929 'financial_type_id' => 1,
930 'payment_instrument_id' => 1,
931 'contribution_status_id' => 1,
933 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
934 $newParams = array_merge($contribParams, array(
935 'id' => $contribution['id'],
936 'contribution_status_id' => 2,
939 $contribution = $this->callAPIFailure('contribution', 'update', $newParams,
940 ts('Cannot change contribution status from Completed to Pending.')
946 * Function tests that financial records are added when Pending Contribution is Canceled
948 function testCreateUpdateContributionCancelPending() {
949 $contribParams = array(
950 'contact_id' => $this->_individualId
,
951 'receive_date' => '2012-01-01',
952 'total_amount' => 100.00,
953 'financial_type_id' => $this->_financialTypeId
,
954 'payment_instrument_id' => 1,
955 'contribution_status_id' => 2,
959 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
960 $newParams = array_merge($contribParams, array(
961 'id' => $contribution['id'],
962 'contribution_status_id' => 3,
965 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
966 $this->_checkFinancialTrxn($contribution, 'cancelPending');
967 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
971 * Function tests that financial records are added when Financial Type is Changed
973 function testCreateUpdateContributionChangeFinancialType() {
974 $contribParams = array(
975 'contact_id' => $this->_individualId
,
976 'receive_date' => '2012-01-01',
977 'total_amount' => 100.00,
978 'financial_type_id' => 1,
979 'payment_instrument_id' => 1,
980 'contribution_status_id' => 1,
983 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
984 $newParams = array_merge($contribParams, array(
985 'id' => $contribution['id'],
986 'financial_type_id' => 3,
989 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
990 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
991 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
994 //To Update Contribution
995 //CHANGE: we require the API to do an incremental update
996 function testCreateUpdateContribution() {
998 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'idofsh', 212355);
1000 'contribution_id' => $contributionID,
1003 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1004 $this->assertEquals($original['id'], $contributionID, 'In line ' . __LINE__
);
1005 //set up list of old params, verify
1007 //This should not be required on update:
1008 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1009 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1010 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1011 $old_source = $original['values'][$contributionID]['contribution_source'];
1013 //note: current behavior is to return ISO. Is this
1014 //documented behavior? Is this correct
1015 $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date']));
1017 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1018 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1020 //check against values in CiviUnitTestCase::createContribution()
1021 $this->assertEquals($old_contact_id, $this->_individualId
, 'In line ' . __LINE__
);
1022 $this->assertEquals($old_fee_amount, 5.00, 'In line ' . __LINE__
);
1023 $this->assertEquals($old_source, 'SSF', 'In line ' . __LINE__
);
1024 $this->assertEquals($old_trxn_id, 212355, 'In line ' . __LINE__
);
1025 $this->assertEquals($old_invoice_id, 'idofsh', 'In line ' . __LINE__
);
1027 'id' => $contributionID,
1028 'contact_id' => $this->_individualId
,
1029 'total_amount' => 110.00,
1030 'financial_type_id' => $this->_financialTypeId
,
1031 'non_deductible_amount' => 10.00,
1032 'net_amount' => 100.00,
1033 'contribution_status_id' => 1,
1034 'note' => 'Donating for Nobel Cause',
1038 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1040 $new_params = array(
1041 'contribution_id' => $contribution['id'],
1044 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1046 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId
, 'In line ' . __LINE__
);
1047 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00, 'In line ' . __LINE__
);
1048 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId
, 'In line ' . __LINE__
);
1049 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument, 'In line ' . __LINE__
);
1050 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00, 'In line ' . __LINE__
);
1051 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount, 'In line ' . __LINE__
);
1052 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00, 'In line ' . __LINE__
);
1053 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id, 'In line ' . __LINE__
);
1054 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id, 'In line ' . __LINE__
);
1055 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source, 'In line ' . __LINE__
);
1056 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed', 'In line ' . __LINE__
);
1058 'contribution_id' => $contributionID,
1061 $result = $this->callAPISuccess('contribution', 'delete', $params);
1064 ///////////////// civicrm_contribution_delete methods
1065 function testDeleteEmptyParamsContribution() {
1067 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1070 function testDeleteParamsNotArrayContribution() {
1071 $params = 'contribution_id= 1';
1072 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1073 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1076 function testDeleteWrongParamContribution() {
1078 'contribution_source' => 'SSF',
1081 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1084 function testDeleteContribution() {
1086 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 'dfsdf', 12389);
1088 'id' => $contributionID,
1090 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__
, __FILE__
);
1094 * Test civicrm_contribution_search with empty params.
1095 * All available contributions expected.
1097 function testSearchEmptyParams() {
1101 'contact_id' => $this->_individualId
,
1102 'receive_date' => date('Ymd'),
1103 'total_amount' => 100.00,
1104 'financial_type_id' => $this->_financialTypeId
,
1105 'non_deductible_amount' => 10.00,
1106 'fee_amount' => 5.00,
1107 'net_amount' => 95.00,
1109 'invoice_id' => 78910,
1111 'contribution_status_id' => 1,
1114 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1116 $result = $this->callAPISuccess('contribution', 'get', $params);
1117 // We're taking the first element.
1118 $res = $result['values'][$contribution['id']];
1120 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
1121 $this->assertEquals($p['total_amount'], $res['total_amount'], 'In line ' . __LINE__
);
1122 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__
);
1123 $this->assertEquals($p['net_amount'], $res['net_amount'], 'In line ' . __LINE__
);
1124 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__
);
1125 $this->assertEquals($p['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__
);
1126 $this->assertEquals($p['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__
);
1127 $this->assertEquals($p['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__
);
1128 $this->assertEquals($p['source'], $res['contribution_source'], 'In line ' . __LINE__
);
1129 // contribution_status_id = 1 => Completed
1130 $this->assertEquals('Completed', $res['contribution_status'], 'In line ' . __LINE__
);
1132 $this->contributionDelete($contribution['id']);
1136 * Test civicrm_contribution_search. Success expected.
1138 function testSearch() {
1140 'contact_id' => $this->_individualId
,
1141 'receive_date' => date('Ymd'),
1142 'total_amount' => 100.00,
1143 'financial_type_id' => $this->_financialTypeId
,
1144 'non_deductible_amount' => 10.00,
1145 'contribution_status_id' => 1,
1148 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1151 'contact_id' => $this->_individualId
,
1152 'receive_date' => date('Ymd'),
1153 'total_amount' => 200.00,
1154 'financial_type_id' => $this->_financialTypeId
,
1155 'non_deductible_amount' => 20.00,
1156 'trxn_id' => 5454565,
1157 'invoice_id' => 1212124,
1158 'fee_amount' => 50.00,
1159 'net_amount' => 60.00,
1160 'contribution_status_id' => 2,
1163 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1166 'contribution_id' => $contribution2['id'],
1169 $result = $this->callAPISuccess('contribution', 'get', $params);
1170 $res = $result['values'][$contribution2['id']];
1172 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
1173 $this->assertEquals($p2['total_amount'], $res['total_amount'], 'In line ' . __LINE__
);
1174 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__
);
1175 $this->assertEquals($p2['net_amount'], $res['net_amount'], 'In line ' . __LINE__
);
1176 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__
);
1177 $this->assertEquals($p2['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__
);
1178 $this->assertEquals($p2['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__
);
1179 $this->assertEquals($p2['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__
);
1180 // contribution_status_id = 2 => Pending
1181 $this->assertEquals('Pending', $res['contribution_status'], 'In line ' . __LINE__
);
1183 $this->contributionDelete($contribution1['id']);
1184 $this->contributionDelete($contribution2['id']);
1188 * Test completing a transaction via the API
1190 * Note that we are creating a logged in user because email goes out from
1193 function testCompleteTransaction() {
1194 $mut = new CiviMailUtils( $this, true );
1195 $this->createLoggedInUser();
1196 $params = array_merge($this->_params
, array('contribution_status_id' => 1,));
1197 $contribution = $this->callAPISuccess('contribution','create', $params);
1198 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1199 'id' => $contribution['id'],
1202 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1203 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1204 $mut->checkMailLog(array(
1205 'Receipt - Contribution',
1206 'Please print this confirmation for your records.',
1212 * Test completing a transaction with an event via the API
1214 * Note that we are creating a logged in user because email goes out from
1217 function testCompleteTransactionWithParticipantRecord() {
1218 $mut = new CiviMailUtils( $this, true );
1219 $mut->clearMessages();
1220 $this->createLoggedInUser();
1221 $contributionID = $this->createPendingParticipantContribution();
1222 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1223 'id' => $contributionID,
1226 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids
['participant'], 'return' => 'participant_status_id'));
1227 $this->assertEquals(1, $participantStatus);
1228 $mut->checkMailLog(array(
1229 'Annual CiviCRM meet',
1231 'This letter is a confirmation that your registration has been received and your status has been updated to registered for the following',
1238 * Test sending a mail via the API
1240 function testSendMail() {
1241 $mut = new CiviMailUtils( $this, true );
1242 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1243 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
1245 'id' => $contribution['id'],
1246 'receipt_from_email' => 'api@civicrm.org',
1249 $mut->checkMailLog(array(
1251 'Contribution Information',
1252 'Please print this confirmation for your records',
1261 * Test sending a mail via the API
1263 function testSendMailEvent() {
1264 $mut = new CiviMailUtils( $this, true );
1265 $contribution = $this->callAPISuccess('contribution','create',$this->_params
);
1266 $event = $this->eventCreate(array(
1267 'is_email_confirm' => 1,
1268 'confirm_from_email' => 'test@civicrm.org',
1270 $this->_eventID
= $event['id'];
1271 $participantParams = array(
1272 'contact_id' => $this->_individualId
,
1273 'event_id' => $this->_eventID
,
1276 // to ensure it matches later on
1277 'register_date' => '2007-07-21 00:00:00',
1278 'source' => 'Online Event Registration: API Testing',
1281 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1282 $this->callAPISuccess('participant_payment', 'create', array(
1283 'participant_id' => $participant['id'],
1284 'contribution_id' => $contribution['id'],
1286 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
1288 'id' => $contribution['id'],
1289 'receipt_from_email' => 'api@civicrm.org',
1293 $mut->checkMailLog(array(
1294 'Annual CiviCRM meet',
1296 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1305 * This function does a GET & compares the result against the $params
1306 * Use as a double check on Creates
1308 function contributionGetnCheck($params, $id, $delete = 1) {
1310 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1316 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1318 $values = $contribution['values'][$contribution['id']];
1319 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1320 // this is not returned in id format
1321 unset($params['payment_instrument_id']);
1322 $params['contribution_source'] = $params['source'];
1323 unset($params['source']);
1324 foreach ($params as $key => $value) {
1325 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__
);
1330 * Create a pending contribution & linked pending participant record
1331 * (along with an event)
1333 function createPendingParticipantContribution(){
1334 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1335 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1336 $this->ids
['participant'] = $participantID;
1337 $params = array_merge($this->_params
, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1338 $contribution = $this->callAPISuccess('contribution','create', $params);
1339 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1340 $lineItem = $this->callAPISuccess('line_item', 'get', array(
1341 'entity_id' => $contribution['id'],
1342 'entity_table' => 'civicrm_contribution',
1343 'api.line_item.create' => array(
1344 'entity_id' => $participantID,
1345 'entity_table' => 'civicrm_participant',
1348 return $contribution['id'];
1351 function _getFinancialTrxnAmount($contId) {
1353 SUM( ft.total_amount ) AS total
1354 FROM civicrm_financial_trxn AS ft
1355 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1356 WHERE ceft.entity_table = 'civicrm_contribution'
1357 AND ceft.entity_id = {$contId}";
1359 $result = CRM_Core_DAO
::singleValueQuery($query);
1363 function _getFinancialItemAmount($contId) {
1364 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1367 FROM civicrm_financial_item
1368 WHERE entity_table = 'civicrm_line_item'
1369 AND entity_id = {$lineItem}";
1370 $result = CRM_Core_DAO
::singleValueQuery($query);
1374 function _checkFinancialItem($contId, $context) {
1375 if ($context != 'paylater') {
1377 'entity_id' => $contId,
1378 'entity_table' => 'civicrm_contribution',
1380 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($params, TRUE));
1381 $entityParams = array(
1382 'financial_trxn_id' => $trxn['financial_trxn_id'],
1383 'entity_table' => 'civicrm_financial_item',
1385 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1387 'id' => $entityTrxn['entity_id'],
1390 if ($context == 'paylater') {
1391 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution');
1392 foreach ($lineItems as $key=>$item) {
1394 'entity_id' => $key,
1395 'entity_table' => 'civicrm_line_item',
1397 $compareParams = array ('status_id' => 1);
1398 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1401 elseif ($context == 'refund') {
1402 $compareParams = array(
1404 'financial_account_id' => 1,
1408 elseif ($context == 'cancelPending') {
1409 $compareParams = array(
1411 'financial_account_id' => 1,
1415 elseif ($context == 'changeFinancial') {
1416 $lineKey = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
1418 'entity_id' => $lineKey,
1421 $compareParams = array(
1422 'financial_account_id' => 1,
1424 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1426 'financial_account_id' => 3,
1427 'entity_id' => $lineKey,
1429 $compareParams = array(
1433 if ($context != 'paylater') {
1434 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1438 function _checkFinancialTrxn($contribution, $context) {
1439 $trxnParams = array(
1440 'entity_id' => $contribution['id'],
1441 'entity_table' => 'civicrm_contribution',
1443 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1445 'id' => $trxn['financial_trxn_id'],
1447 if ($context == 'payLater') {
1448 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1449 $compareParams = array(
1451 'from_financial_account_id' => CRM_Contribute_PseudoConstant
::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1454 elseif ($context == 'paymentInstrument') {
1455 $compareParams = array(
1456 'from_financial_account_id' => 6,
1457 'to_financial_account_id' => 7,
1460 elseif ($context == 'refund') {
1461 $compareParams = array(
1462 'to_financial_account_id' => 6,
1463 'total_amount' => -100,
1467 elseif ($context == 'cancelPending') {
1468 $compareParams = array(
1469 'from_financial_account_id' => 7,
1470 'total_amount' => -100,
1474 elseif ($context == 'changeFinancial') {
1475 $entityParams = array(
1476 'entity_id' => $contribution['id'],
1477 'entity_table' => 'civicrm_contribution',
1480 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1481 $trxnParams1 = array(
1482 'id' => $trxn['financial_trxn_id'],
1484 $compareParams = array(
1485 'to_financial_account_id' => 12,
1486 'total_amount' => -100,
1489 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1490 $compareParams = array(
1491 'to_financial_account_id' => 12,
1492 'total_amount' => 100,
1497 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1500 function _addPaymentInstrument () {
1501 $gId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1502 $optionParams = array(
1503 'option_group_id' => $gId,
1504 'label' => 'Test Card',
1505 'name' => 'Test Card',
1510 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1511 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1512 $financialParams = array(
1513 'entity_table' => 'civicrm_option_value',
1514 'entity_id' => $optionValue['id'],
1515 'account_relationship' => $relationTypeId,
1516 'financial_account_id' => 7,
1518 $financialType = CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams, CRM_Core_DAO
::$_nullArray);
1519 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1520 return $optionValue['values'][$optionValue['id']]['value'];
1523 function _checkFinancialRecords($params,$context) {
1524 $entityParams = array(
1525 'entity_id' => $params['id'],
1526 'entity_table' => 'civicrm_contribution',
1528 if ($context == 'pending') {
1529 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
1530 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1533 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1534 $trxnParams = array(
1535 'id' => $trxn['financial_trxn_id'],
1537 if ($context != 'online' && $context != 'payLater') {
1538 $compareParams = array(
1539 'to_financial_account_id' => 6,
1540 'total_amount' => 100,
1544 if ($context == 'feeAmount') {
1545 $compareParams['fee_amount'] = 50;
1547 elseif ($context == 'online') {
1548 $compareParams = array(
1549 'to_financial_account_id' => 12,
1550 'total_amount' => 100,
1554 elseif ($context == 'payLater') {
1555 $compareParams = array(
1556 'to_financial_account_id' => 7,
1557 'total_amount' => 100,
1561 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams);
1562 $entityParams = array(
1563 'financial_trxn_id' => $trxn['financial_trxn_id'],
1564 'entity_table' => 'civicrm_financial_item',
1566 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
1567 $fitemParams = array(
1568 'id' => $entityTrxn['entity_id'],
1570 $compareParams = array(
1573 'financial_account_id' => 1,
1575 if ($context == 'payLater') {
1576 $compareParams = array(
1579 'financial_account_id' => 1,
1582 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1583 if ($context == 'feeAmount') {
1585 'entity_id' => $params['id'],
1586 'entity_table' => 'civicrm_contribution',
1588 $maxTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($maxParams, TRUE));
1589 $trxnParams = array(
1590 'id' => $maxTrxn['financial_trxn_id'],
1592 $compareParams = array(
1593 'to_financial_account_id' => 5,
1594 'from_financial_account_id' => 6,
1595 'total_amount' => 50,
1598 $trxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['id'], 'DESC');
1599 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1600 $fitemParams = array(
1601 'entity_id' => $trxnId['financialTrxnId'],
1602 'entity_table' => 'civicrm_financial_trxn',
1604 $compareParams = array(
1607 'financial_account_id' => 5,
1609 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);