CRM-15466 add test to ensure net_amount is set correctly
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CiviTest/CiviMailUtils.php';
30
31
32 /**
33 * Test APIv3 civicrm_contribute_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
37 */
38
39 class api_v3_ContributionTest extends CiviUnitTestCase {
40
41 /**
42 * Assume empty database with just civicrm_data
43 */
44 protected $_individualId;
45 protected $_contribution;
46 protected $_financialTypeId = 1;
47 protected $_apiversion;
48 protected $_entity = 'Contribution';
49 public $debug = 0;
50 protected $_params;
51 protected $_ids = array();
52 protected $_pageParams = array();
53
54 function setUp() {
55 parent::setUp();
56
57 $this->_apiversion = 3;
58 $this->_individualId = $this->individualCreate();
59 $paymentProcessor = $this->processorCreate();
60 $this->_params = array(
61 'contact_id' => $this->_individualId,
62 'receive_date' => '20120511',
63 'total_amount' => 100.00,
64 'financial_type_id' => $this->_financialTypeId,
65 'non_deductible_amount' => 10.00,
66 'fee_amount' => 5.00,
67 'net_amount' => 95.00,
68 'source' => 'SSF',
69 'contribution_status_id' => 1,
70 );
71 $this->_processorParams = array(
72 'domain_id' => 1,
73 'name' => 'Dummy',
74 'payment_processor_type_id' => 10,
75 'financial_account_id' => 12,
76 'is_active' => 1,
77 'user_name' => '',
78 'url_site' => 'http://dummy.com',
79 'url_recur' => 'http://dummy.com',
80 'billing_mode' => 1,
81 );
82 $this->_pageParams = array(
83 'title' => 'Test Contribution Page',
84 'financial_type_id' => 1,
85 'currency' => 'USD',
86 'financial_account_id' => 1,
87 'payment_processor' => $paymentProcessor->id,
88 'is_active' => 1,
89 'is_allow_other_amount' => 1,
90 'min_amount' => 10,
91 'max_amount' => 1000,
92 );
93 }
94
95 function tearDown() {
96 $this->quickCleanUpFinancialEntities();
97 }
98
99 function testGetContribution() {
100 $p = array(
101 'contact_id' => $this->_individualId,
102 'receive_date' => '2010-01-20',
103 'total_amount' => 100.00,
104 'financial_type_id' => $this->_financialTypeId,
105 'non_deductible_amount' => 10.00,
106 'fee_amount' => 5.00,
107 'net_amount' => 95.00,
108 'trxn_id' => 23456,
109 'invoice_id' => 78910,
110 'source' => 'SSF',
111 'contribution_status_id' => 1,
112 );
113 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
114
115 $params = array(
116 'contribution_id' => $this->_contribution['id'],
117 );
118
119 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
120 $financialParams['id'] = $this->_financialTypeId;
121 $default = null;
122 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
123
124 $this->assertEquals(1, $contribution['count']);
125 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
126 // note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
127 // passing back a string rather than an id seems like an error / cruft - & if it is to be introduced we should discuss
128 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
129 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
130 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
131 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
132 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
133 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
134 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
135 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
136 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
137 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
138 $p['trxn_id'] = '3847';
139 $p['invoice_id'] = '3847';
140
141 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
142
143 // now we have 2 - test getcount
144 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
145 $this->assertEquals(2, $contribution);
146 //test id only format
147 $contribution = $this->callAPISuccess('contribution', 'get', array(
148 'id' => $this->_contribution['id'],
149 'format.only_id' => 1,
150 ));
151 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__);
152 //test id only format
153 $contribution = $this->callAPISuccess('contribution', 'get', array(
154 'id' => $contribution2['id'],
155 'format.only_id' => 1,
156 ));
157 $this->assertEquals($contribution2['id'], $contribution);
158 //test id as field
159 $contribution = $this->callAPISuccess('contribution', 'get', array(
160 'id' => $this->_contribution['id'],
161 ));
162 $this->assertEquals(1, $contribution['count']);
163
164 //test get by contact id works
165 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
166
167 $this->assertEquals(2, $contribution['count']);
168 $this->callAPISuccess('Contribution', 'Delete', array(
169 'id' => $this->_contribution['id'],
170 ));
171 $this->callAPISuccess('Contribution', 'Delete', array(
172 'id' => $contribution2['id'],
173 ));
174 }
175
176 /**
177 * We need to ensure previous tested behaviour still works as part of the api contract
178 */
179 function testGetContributionLegacyBehaviour() {
180 $p = array(
181 'contact_id' => $this->_individualId,
182 'receive_date' => '2010-01-20',
183 'total_amount' => 100.00,
184 'contribution_type_id' => $this->_financialTypeId,
185 'non_deductible_amount' => 10.00,
186 'fee_amount' => 5.00,
187 'net_amount' => 95.00,
188 'trxn_id' => 23456,
189 'invoice_id' => 78910,
190 'source' => 'SSF',
191 'contribution_status_id' => 1,
192 );
193 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
194
195 $params = array(
196 'contribution_id' => $this->_contribution['id'],
197 );
198 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
199 $financialParams['id'] = $this->_financialTypeId;
200 $default = null;
201 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
202
203 $this->assertEquals(1,$contribution['count']);
204 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
205 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
206 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
207 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
208 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
209 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
210 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
211 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
212 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
213 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
214 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
215 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
216 $p['trxn_id'] = '3847';
217 $p['invoice_id'] = '3847';
218
219 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
220
221 // now we have 2 - test getcount
222 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
223 $this->assertEquals(2, $contribution);
224 //test id only format
225 $contribution = $this->callAPISuccess('contribution', 'get', array(
226 'id' => $this->_contribution['id'],
227 'format.only_id' => 1,
228 ));
229 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__);
230 //test id only format
231 $contribution = $this->callAPISuccess('contribution', 'get', array(
232 'id' => $contribution2['id'],
233 'format.only_id' => 1,
234 ));
235 $this->assertEquals($contribution2['id'], $contribution);
236 $contribution = $this->callAPISuccess('contribution', 'get', array(
237 'id' => $this->_contribution['id'],
238 ));
239 //test id as field
240 $this->assertEquals(1, $contribution['count']);
241 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
242 //test get by contact id works
243 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
244
245 $this->assertEquals(2, $contribution['count']);
246 $this->callAPISuccess('Contribution', 'Delete', array(
247 'id' => $this->_contribution['id'],
248 ));
249 $this->callAPISuccess('Contribution', 'Delete', array(
250 'id' => $contribution2['id'],
251 ));
252 }
253 ///////////////// civicrm_contribution_
254 function testCreateEmptyContributionIDUseDonation() {
255 $params = array(
256 'contribution_id' => FALSE,
257 'contact_id' => 1,
258 'total_amount' => 1,
259 'check_permissions' => false,
260 'financial_type_id' => 'Donation',
261 );
262 $this->callAPISuccess('contribution', 'create', $params);
263 }
264 /*
265 * ensure we continue to support contribution_type_id as part of the api commitment to
266 * stability
267 *///////////////// civicrm_contribution_
268
269 function testCreateLegacyBehaviour() {
270 $params = array(
271 'contribution_id' => FALSE,
272 'contact_id' => 1,
273 'total_amount' => 1,
274 'check_permissions' => false,
275 'contribution_type_id' => 3,
276 );
277 $contribution = $this->callAPISuccess('contribution', 'create', $params);
278 $contribution = $this->callAPISuccess('contribution', 'getsingle', array( 'id' => $contribution['id']));
279 $this->assertEquals(3, $contribution['financial_type_id']);
280 }
281
282 /**
283 * check with complete array + custom field
284 * Note that the test is written on purpose without any
285 * variables specific to participant so it can be replicated into other entities
286 * and / or moved to the automated test suite
287 */
288 function testCreateWithCustom() {
289 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
290
291 $params = $this->_params;
292 $params['custom_' . $ids['custom_field_id']] = "custom string";
293
294 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
295 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
296 $check = $this->callAPISuccess($this->_entity, 'get', array(
297 'return.custom_' . $ids['custom_field_id'] => 1,
298 'id' => $result['id'],
299 ));
300 $this->customFieldDelete($ids['custom_field_id']);
301 $this->customGroupDelete($ids['custom_group_id']);
302 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
303 }
304
305 /**
306 * check with complete array + custom field
307 * Note that the test is written on purpose without any
308 * variables specific to participant so it can be replicated into other entities
309 * and / or moved to the automated test suite
310 */
311 function testCreateGetFieldsWithCustom() {
312 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
313 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
314 $result = $this->callAPISuccess('Contribution', 'getfields', array());
315 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
316 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
317 $this->customFieldDelete($ids['custom_field_id']);
318 $this->customGroupDelete($ids['custom_group_id']);
319 $this->customFieldDelete($idsContact['custom_field_id']);
320 $this->customGroupDelete($idsContact['custom_group_id']);
321 }
322
323 function testCreateContributionNoLineItems() {
324
325 $params = array(
326 'contact_id' => $this->_individualId,
327 'receive_date' => '20120511',
328 'total_amount' => 100.00,
329 'financial_type_id' => $this->_financialTypeId,
330 'payment_instrument_id' => 1,
331 'non_deductible_amount' => 10.00,
332 'fee_amount' => 50.00,
333 'net_amount' => 90.00,
334 'trxn_id' => 12345,
335 'invoice_id' => 67890,
336 'source' => 'SSF',
337 'contribution_status_id' => 1,
338 'skipLineItem' => 1,
339 );
340
341 $contribution = $this->callAPISuccess('contribution', 'create', $params);
342 $lineItems = $this->callAPISuccess('line_item','get',array(
343 'entity_id' => $contribution['id'],
344 'entity_table' => 'civicrm_contribution',
345 'sequential' => 1,
346 ));
347 $this->assertEquals(0, $lineItems['count']);
348 }
349 /*
350 * Test checks that passing in line items suppresses the create mechanism
351 */
352 function testCreateContributionChainedLineItems() {
353
354 $params = array(
355 'contact_id' => $this->_individualId,
356 'receive_date' => '20120511',
357 'total_amount' => 100.00,
358 'financial_type_id' => $this->_financialTypeId,
359 'payment_instrument_id' => 1,
360 'non_deductible_amount' => 10.00,
361 'fee_amount' => 50.00,
362 'net_amount' => 90.00,
363 'trxn_id' => 12345,
364 'invoice_id' => 67890,
365 'source' => 'SSF',
366 'contribution_status_id' => 1,
367 'skipLineItem' => 1,
368 'api.line_item.create' => array(
369 array(
370 'price_field_id' => 1,
371 'qty' => 2,
372 'line_total' => '20',
373 'unit_price' => '10',
374 ),
375 array(
376 'price_field_id' => 1,
377 'qty' => 1,
378 'line_total' => '80',
379 'unit_price' => '80',
380 ),
381 ),
382 );
383
384 $description = "Create Contribution with Nested Line Items";
385 $subfile = "CreateWithNestedLineItems";
386 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__,__FILE__, $description, $subfile);
387
388 $lineItems = $this->callAPISuccess('line_item','get',array(
389 'entity_id' => $contribution['id'],
390 'contribution_id' => $contribution['id'],
391 'entity_table' => 'civicrm_contribution',
392 'sequential' => 1,
393 ));
394 $this->assertEquals(2, $lineItems['count']);
395 }
396
397 function testCreateContributionOffline() {
398 $params = array(
399 'contact_id' => $this->_individualId,
400 'receive_date' => '20120511',
401 'total_amount' => 100.00,
402 'financial_type_id' => 1,
403 'trxn_id' => 12345,
404 'invoice_id' => 67890,
405 'source' => 'SSF',
406 'contribution_status_id' => 1,
407 );
408
409 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
410 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
411 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
412 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
413 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
414 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
415 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
416 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
417 $lineItems = $this->callAPISuccess('line_item','get',array(
418 'entity_id' => $contribution['id'],
419 'contribution_id' => $contribution['id'],
420 'entity_table' => 'civicrm_contribution',
421 'sequential' => 1,
422 ));
423 $this->assertEquals(1, $lineItems['count']);
424 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
425 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
426 $this->_checkFinancialRecords($contribution, 'offline');
427 $this->contributionGetnCheck($params, $contribution['id']);
428 }
429 /**
430 * test create with valid payment instument
431 */
432 function testCreateContributionWithPaymentInstrument() {
433 $params = $this->_params + array('payment_instrument' => 'EFT');
434 $contribution = $this->callAPISuccess('contribution', 'create', $params);
435 $contribution = $this->callAPISuccess('contribution','get', array(
436 'sequential' => 1,
437 'id' => $contribution['id']
438 ));
439 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
440 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
441
442 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
443 $contribution = $this->callAPISuccess('contribution','get', array(
444 'sequential' => 1,
445 'id' => $contribution['id']
446 ));
447 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
448 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
449 }
450
451 function testGetContributionByPaymentInstrument() {
452 $params = $this->_params + array('payment_instrument' => 'EFT');
453 $params2 = $this->_params + array('payment_instrument' => 'Cash');
454 $this->callAPISuccess('contribution','create',$params);
455 $this->callAPISuccess('contribution','create',$params2);
456 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash'));
457 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
458 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
459 $this->assertEquals(1,$contribution['count']);
460 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
461 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
462 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
463 $this->assertEquals(1, $contribution['count']);
464 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument_id' => 5));
465 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
466 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
467 $this->assertEquals(1,$contribution['count']);
468 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument' => 'EFT'));
469 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
470 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
471 $this->assertEquals(1, $contribution['count']);
472 $contribution = $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
473 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
474 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
475 $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']);
476 $this->assertEquals(1,$contribution['count']);
477 }
478
479 /*
480 * Create test with unique field name on source
481 */
482 function testCreateContributionSource() {
483
484 $params = array(
485 'contact_id' => $this->_individualId,
486 'receive_date' => date('Ymd'),
487 'total_amount' => 100.00,
488 'financial_type_id' => $this->_financialTypeId,
489 'payment_instrument_id' => 1,
490 'non_deductible_amount' => 10.00,
491 'fee_amount' => 50.00,
492 'net_amount' => 90.00,
493 'trxn_id' => 12345,
494 'invoice_id' => 67890,
495 'contribution_source' => 'SSF',
496 'contribution_status_id' => 1,
497 );
498
499 $contribution = $this->callAPISuccess('contribution', 'create', $params);
500 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
501 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
502 }
503
504 /*
505 * Create test with unique field name on source
506 */
507 function testCreateDefaultNow() {
508
509 $params = $this->_params;
510 unset($params['receive_date']);
511
512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
513 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
514 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
515 }
516
517 /*
518 * Create test with unique field name on source
519 */
520 function testCreateContributionSourceInvalidContac() {
521
522 $params = array(
523 'contact_id' => 999,
524 'receive_date' => date('Ymd'),
525 'total_amount' => 100.00,
526 'financial_type_id' => $this->_financialTypeId,
527 'payment_instrument_id' => 1,
528 'non_deductible_amount' => 10.00,
529 'fee_amount' => 50.00,
530 'net_amount' => 90.00,
531 'trxn_id' => 12345,
532 'invoice_id' => 67890,
533 'contribution_source' => 'SSF',
534 'contribution_status_id' => 1,
535 );
536
537 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
538 }
539
540 function testCreateContributionSourceInvalidContContact() {
541
542 $params = array(
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,
551 'trxn_id' => 12345,
552 'invoice_id' => 67890,
553 'contribution_source' => 'SSF',
554 'contribution_status_id' => 1,
555 );
556
557 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
558 }
559
560 /**
561 * test note created correctly
562 */
563 function testCreateContributionWithNote() {
564 $description = "Demonstrates creating contribution with Note Entity";
565 $subfile = "ContributionCreateWithNote";
566 $params = array(
567 'contact_id' => $this->_individualId,
568 'receive_date' => '2012-01-01',
569 'total_amount' => 100.00,
570 'financial_type_id' => $this->_financialTypeId,
571 'payment_instrument_id' => 1,
572 'non_deductible_amount' => 10.00,
573 'fee_amount' => 50.00,
574 'net_amount' => 90.00,
575 'trxn_id' => 12345,
576 'invoice_id' => 67890,
577 'source' => 'SSF',
578 'contribution_status_id' => 1,
579 'note' => 'my contribution note',
580 );
581
582 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
583 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
584 $this->assertEquals('my contribution note', $result['values'][0]['note']);
585 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
586 }
587
588 function testCreateContributionWithNoteUniqueNameAliases() {
589 $params = array(
590 'contact_id' => $this->_individualId,
591 'receive_date' => '2012-01-01',
592 'total_amount' => 100.00,
593 'financial_type_id' => $this->_financialTypeId,
594 'payment_instrument_id' => 1,
595 'non_deductible_amount' => 10.00,
596 'fee_amount' => 50.00,
597 'net_amount' => 90.00,
598 'trxn_id' => 12345,
599 'invoice_id' => 67890,
600 'source' => 'SSF',
601 'contribution_status_id' => 1,
602 'contribution_note' => 'my contribution note',
603 );
604
605 $contribution = $this->callAPISuccess('contribution', 'create', $params);
606 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
607 $this->assertEquals('my contribution note', $result['values'][0]['note']);
608 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
609 }
610 /*
611 * This is the test for creating soft credits - however a 'get' is not yet possible via API
612 * as the current BAO functions are contact-centric (from what I can find)
613 *
614 */
615 function testCreateContributionWithSoftCredt() {
616 $description = "Demonstrates creating contribution with SoftCredit";
617 $subfile = "ContributionCreateWithSoftCredit";
618 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
619 $softparams = array(
620 'contact_id' => $contact2['id'],
621 'amount' => 50,
622 'soft_credit_type_id' => 3
623 );
624
625 $params = $this->_params + array('soft_credit' => array(1 => $softparams));
626 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
627 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
628
629 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
630 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
631 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
632
633 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
634 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
635 }
636
637 function testCreateContributionWithSoftCreditDefaults() {
638 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
639 $subfile = "ContributionCreateWithSoftCreditDefaults";
640 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
641 $params = $this->_params + array(
642 'soft_credit_to' => $contact2['id'],
643 );
644 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
645 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
646
647 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
648 // Default soft credit amount = contribution.total_amount
649 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
650 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
651
652 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
653 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
654 }
655
656 function testCreateContributionWithHonoreeContact() {
657 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
658 $subfile = "ContributionCreateWithHonoreeContact";
659 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
660 $params = $this->_params + array(
661 'honor_contact_id' => $contact2['id'],
662 );
663 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
664 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
665
666 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
667 // Default soft credit amount = contribution.total_amount
668 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
669 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
670 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
671
672 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
673 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
674 }
675
676 /**
677 * Test using example code
678 */
679 function testContributionCreateExample() {
680 //make sure at least on page exists since there is a truncate in tear down
681 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
682 $this->assertAPISuccess($page);
683 require_once 'api/v3/examples/Contribution/Create.php';
684 $result = contribution_create_example();
685 $this->assertAPISuccess($result);
686 $contributionId = $result['id'];
687 $expectedResult = contribution_create_expectedresult();
688 $this->checkArrayEquals($expectedResult, $result);
689 $this->contributionDelete($contributionId);
690 }
691
692 /*
693 * Function tests that additional financial records are created when fee amount is recorded
694 */
695 function testCreateContributionWithFee() {
696 $params = array(
697 'contact_id' => $this->_individualId,
698 'receive_date' => '20120511',
699 'total_amount' => 100.00,
700 'fee_amount' => 50,
701 'financial_type_id' => 1,
702 'trxn_id' => 12345,
703 'invoice_id' => 67890,
704 'source' => 'SSF',
705 'contribution_status_id' => 1,
706 );
707
708 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
709 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
710 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
711 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
712 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
713 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
714 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
715 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
716 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
717 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
718
719 $lineItems = $this->callAPISuccess('line_item','get',array(
720
721 'entity_id' => $contribution['id'],
722 'entity_table' => 'civicrm_contribution',
723 'sequential' => 1,
724 ));
725 $this->assertEquals(1, $lineItems['count']);
726 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
727 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
728 $lineItems = $this->callAPISuccess('line_item','get',array(
729
730 'entity_id' => $contribution['id'],
731 'contribution_id' => $contribution['id'],
732 'entity_table' => 'civicrm_contribution',
733 'sequential' => 1,
734 ));
735 $this->assertEquals(1, $lineItems['count']);
736 $this->_checkFinancialRecords($contribution, 'feeAmount');
737 }
738
739
740 /**
741 * Function tests that additional financial records are created when online contribution is created
742 */
743 function testCreateContributionOnline() {
744 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
745 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams );
746 $this->assertAPISuccess($contributionPage);
747 $params = array(
748 'contact_id' => $this->_individualId,
749 'receive_date' => '20120511',
750 'total_amount' => 100.00,
751 'financial_type_id' => 1,
752 'contribution_page_id' => $contributionPage['id'],
753 'payment_processor' => 1,
754 'trxn_id' => 12345,
755 'invoice_id' => 67890,
756 'source' => 'SSF',
757 'contribution_status_id' => 1,
758
759 );
760
761 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
762 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
763 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
764 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
765 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
766 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
767 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
768 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
769 $this->_checkFinancialRecords($contribution, 'online');
770 }
771
772 /**
773 * in the interests of removing financial type / contribution type checks from
774 * legacy format function lets test that the api is doing this for us
775 */
776 function testCreateInvalidFinancialType() {
777 $params = $this->_params;
778 $params['financial_type_id'] = 99999;
779 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
780 }
781
782 /**
783 * in the interests of removing financial type / contribution type checks from
784 * legacy format function lets test that the api is doing this for us
785 */
786 function testValidNamedFinancialType() {
787 $params = $this->_params;
788 $params['financial_type_id'] = 'Donation';
789 $this->callAPISuccess($this->_entity, 'create', $params);
790 }
791
792 /**
793 * Function tests that additional financial records are created when online contribution with pay later option
794 * is created
795 */
796 function testCreateContributionPayLaterOnline() {
797 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
798 $this->_pageParams['is_pay_later'] = 1;
799 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams );
800 $this->assertAPISuccess($contributionPage);
801 $params = array(
802 'contact_id' => $this->_individualId,
803 'receive_date' => '20120511',
804 'total_amount' => 100.00,
805 'financial_type_id' => 1,
806 'contribution_page_id' => $contributionPage['id'],
807 'trxn_id' => 12345,
808 'is_pay_later' => 1,
809 'invoice_id' => 67890,
810 'source' => 'SSF',
811 'contribution_status_id' => 2,
812
813 );
814
815 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
816 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
817 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
818 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
819 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
820 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
821 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
822 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
823 $this->_checkFinancialRecords($contribution, 'payLater');
824 }
825
826 /*
827 * Function tests that additional financial records are created when online contribution with pending option
828 * is created
829 */
830 function testCreateContributionPendingOnline() {
831 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
832 $contributionPage = $this->callAPISuccess( 'contribution_page', 'create', $this->_pageParams );
833 $this->assertAPISuccess($contributionPage);
834 $params = array(
835 'contact_id' => $this->_individualId,
836 'receive_date' => '20120511',
837 'total_amount' => 100.00,
838 'financial_type_id' => 1,
839 'contribution_page_id' => $contributionPage['id'],
840 'trxn_id' => 12345,
841 'invoice_id' => 67890,
842 'source' => 'SSF',
843 'contribution_status_id' => 2,
844 );
845
846 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
847 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
848 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
849 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
850 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
851 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
852 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
853 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
854 $this->_checkFinancialRecords($contribution, 'pending');
855 }
856
857 /*
858 * Function tests that line items, financial records are updated when contribution amount is changed
859 */
860 function testCreateUpdateContributionChangeTotal() {
861 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
862 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
863
864 'entity_id' => $contribution['id'],
865 'entity_table' => 'civicrm_contribution',
866 'sequential' => 1,
867 'return' => 'line_total',
868 ));
869 $this->assertEquals('100.00', $lineItems);
870 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
871 // Financial trxn SUM = 100 + 5 (fee)
872 $this->assertEquals('105.00', $trxnAmount);
873 $newParams = array(
874
875 'id' => $contribution['id'],
876 'total_amount' => '125');
877 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
878
879 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
880
881 'entity_id' => $contribution['id'],
882 'entity_table' => 'civicrm_contribution',
883 'sequential' => 1,
884 'return' => 'line_total',
885 ));
886
887 $this->assertEquals('125.00', $lineItems);
888 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
889 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
890 // Financial trxn SUM = 125 + 5 (fee)
891 $this->assertEquals('130.00', $trxnAmount);
892 $this->assertEquals('125.00', $fitemAmount);
893 }
894
895 /*
896 * Function tests that line items, financial records are updated when pay later contribution is received
897 */
898 function testCreateUpdateContributionPayLater() {
899 $contribParams = array(
900 'contact_id' => $this->_individualId,
901 'receive_date' => '2012-01-01',
902 'total_amount' => 100.00,
903 'financial_type_id' => $this->_financialTypeId,
904 'payment_instrument_id' => 1,
905 'contribution_status_id' => 2,
906 'is_pay_later' => 1,
907
908 );
909 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
910
911 $newParams = array_merge($contribParams, array(
912 'id' => $contribution['id'],
913 'contribution_status_id' => 1,)
914 );
915 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
916 $contribution = $contribution['values'][$contribution['id']];
917 $this->assertEquals($contribution['contribution_status_id'],'1');
918 $this->_checkFinancialItem($contribution['id'], 'paylater');
919 $this->_checkFinancialTrxn($contribution, 'payLater');
920 }
921
922 /*
923 * Function tests that financial records are updated when Payment Instrument is changed
924 */
925 function testCreateUpdateContributionPaymentInstrument() {
926 $instrumentId = $this->_addPaymentInstrument();
927 $contribParams = array(
928 'contact_id' => $this->_individualId,
929 'total_amount' => 100.00,
930 'financial_type_id' => $this->_financialTypeId,
931 'payment_instrument_id' => 4,
932 'contribution_status_id' => 1,
933
934 );
935 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
936
937 $newParams = array_merge($contribParams, array(
938 'id' => $contribution['id'],
939 'payment_instrument_id' => $instrumentId,)
940 );
941 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
942 $this->assertAPISuccess($contribution);
943 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
944 }
945
946 /*
947 * Function tests that financial records are added when Contribution is Refunded
948 */
949 function testCreateUpdateContributionRefund() {
950 $contribParams = array(
951 'contact_id' => $this->_individualId,
952 'receive_date' => '2012-01-01',
953 'total_amount' => 100.00,
954 'financial_type_id' => $this->_financialTypeId,
955 'payment_instrument_id' => 4,
956 'contribution_status_id' => 1,
957
958 );
959 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
960 $newParams = array_merge($contribParams, array(
961 'id' => $contribution['id'],
962 'contribution_status_id' => 7,
963 )
964 );
965
966 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
967 $this->_checkFinancialTrxn($contribution, 'refund');
968 $this->_checkFinancialItem($contribution['id'], 'refund');
969 }
970
971 /*
972 * Function tests invalid contribution status change
973 */
974 function testCreateUpdateContributionInValidStatusChange() {
975 $contribParams = array(
976 'contact_id' => 1,
977 'receive_date' => '2012-01-01',
978 'total_amount' => 100.00,
979 'financial_type_id' => 1,
980 'payment_instrument_id' => 1,
981 'contribution_status_id' => 1,
982 );
983 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
984 $newParams = array_merge($contribParams, array(
985 'id' => $contribution['id'],
986 'contribution_status_id' => 2,
987 )
988 );
989 $this->callAPIFailure('contribution', 'create', $newParams,ts('Cannot change contribution status from Completed to Pending.'));
990
991 }
992
993 /*
994 * Function tests that financial records are added when Pending Contribution is Canceled
995 */
996 function testCreateUpdateContributionCancelPending() {
997 $contribParams = array(
998 'contact_id' => $this->_individualId,
999 'receive_date' => '2012-01-01',
1000 'total_amount' => 100.00,
1001 'financial_type_id' => $this->_financialTypeId,
1002 'payment_instrument_id' => 1,
1003 'contribution_status_id' => 2,
1004 'is_pay_later' => 1,
1005
1006 );
1007 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1008 $newParams = array_merge($contribParams, array(
1009 'id' => $contribution['id'],
1010 'contribution_status_id' => 3,
1011 )
1012 );
1013 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1014 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1015 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1016 }
1017
1018 /*
1019 * Function tests that financial records are added when Financial Type is Changed
1020 */
1021 function testCreateUpdateContributionChangeFinancialType() {
1022 $contribParams = array(
1023 'contact_id' => $this->_individualId,
1024 'receive_date' => '2012-01-01',
1025 'total_amount' => 100.00,
1026 'financial_type_id' => 1,
1027 'payment_instrument_id' => 1,
1028 'contribution_status_id' => 1,
1029
1030 );
1031 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1032 $newParams = array_merge($contribParams, array(
1033 'id' => $contribution['id'],
1034 'financial_type_id' => 3,
1035 )
1036 );
1037 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1038 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1039 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1040 }
1041
1042 /**
1043 * test that update does not change status id CRM-15105
1044 */
1045 function testCreateUpdateWithoutChangingPendingStatus() {
1046 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1047 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1048 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1049 $this->assertEquals(2, $contribution['contribution_status_id']);
1050 }
1051 //To Update Contribution
1052 //CHANGE: we require the API to do an incremental update
1053 function testCreateUpdateContribution() {
1054
1055 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
1056 $old_params = array(
1057 'contribution_id' => $contributionID,
1058
1059 );
1060 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1061 //Make sure it came back
1062 $this->assertAPISuccess($original);
1063 $this->assertEquals($original['id'], $contributionID);
1064 //set up list of old params, verify
1065
1066 //This should not be required on update:
1067 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1068 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1069 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1070 $old_source = $original['values'][$contributionID]['contribution_source'];
1071
1072 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1073 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1074
1075 //check against values in CiviUnitTestCase::createContribution()
1076 $this->assertEquals($old_contact_id, $this->_individualId);
1077 $this->assertEquals($old_fee_amount, 5.00);
1078 $this->assertEquals($old_source, 'SSF');
1079 $this->assertEquals($old_trxn_id, 212355);
1080 $this->assertEquals($old_invoice_id, 'idofsh');
1081 $params = array(
1082 'id' => $contributionID,
1083 'contact_id' => $this->_individualId,
1084 'total_amount' => 110.00,
1085 'financial_type_id' => $this->_financialTypeId,
1086 'non_deductible_amount' => 10.00,
1087 'net_amount' => 100.00,
1088 'contribution_status_id' => 1,
1089 'note' => 'Donating for Nobel Cause',
1090
1091 );
1092
1093 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1094
1095 $new_params = array(
1096 'contribution_id' => $contribution['id'],
1097
1098 );
1099 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1100
1101 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1102 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1103 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId );
1104 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1105 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1106 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1107 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1108 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1109 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1110 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1111 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1112 $params = array(
1113 'contribution_id' => $contributionID,
1114
1115 );
1116 $result = $this->callAPISuccess('contribution', 'delete', $params);
1117 $this->assertAPISuccess($result, 'in line' . __LINE__);
1118 }
1119
1120 ///////////////// civicrm_contribution_delete methods
1121 function testDeleteEmptyParamsContribution() {
1122 $params = array();
1123 $this->callAPIFailure('contribution', 'delete', $params);
1124 }
1125
1126 function testDeleteParamsNotArrayContribution() {
1127 $params = 'contribution_id= 1';
1128 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1129 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1130 }
1131
1132 function testDeleteWrongParamContribution() {
1133 $params = array(
1134 'contribution_source' => 'SSF',
1135
1136 );
1137 $this->callAPIFailure('contribution', 'delete', $params);
1138 }
1139
1140 function testDeleteContribution() {
1141
1142 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
1143 $params = array(
1144 'id' => $contributionID,
1145 );
1146 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1147 }
1148
1149 /**
1150 * Test civicrm_contribution_search with empty params.
1151 * All available contributions expected.
1152 */
1153 function testSearchEmptyParams() {
1154 $params = array();
1155
1156 $p = array(
1157 'contact_id' => $this->_individualId,
1158 'receive_date' => date('Ymd'),
1159 'total_amount' => 100.00,
1160 'financial_type_id' => $this->_financialTypeId,
1161 'non_deductible_amount' => 10.00,
1162 'fee_amount' => 5.00,
1163 'net_amount' => 95.00,
1164 'trxn_id' => 23456,
1165 'invoice_id' => 78910,
1166 'source' => 'SSF',
1167 'contribution_status_id' => 1,
1168
1169 );
1170 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1171
1172 $result = $this->callAPISuccess('contribution', 'get', $params);
1173 // We're taking the first element.
1174 $res = $result['values'][$contribution['id']];
1175
1176 $this->assertEquals($p['contact_id'], $res['contact_id']);
1177 $this->assertEquals($p['total_amount'], $res['total_amount']);
1178 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'] );
1179 $this->assertEquals($p['net_amount'], $res['net_amount']);
1180 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1181 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1182 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1183 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1184 $this->assertEquals($p['source'], $res['contribution_source']);
1185 // contribution_status_id = 1 => Completed
1186 $this->assertEquals('Completed', $res['contribution_status']);
1187
1188 $this->contributionDelete($contribution['id']);
1189 }
1190
1191 /**
1192 * Test civicrm_contribution_search. Success expected.
1193 */
1194 function testSearch() {
1195 $p1 = array(
1196 'contact_id' => $this->_individualId,
1197 'receive_date' => date('Ymd'),
1198 'total_amount' => 100.00,
1199 'financial_type_id' => $this->_financialTypeId,
1200 'non_deductible_amount' => 10.00,
1201 'contribution_status_id' => 1,
1202
1203 );
1204 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1205
1206 $p2 = array(
1207 'contact_id' => $this->_individualId,
1208 'receive_date' => date('Ymd'),
1209 'total_amount' => 200.00,
1210 'financial_type_id' => $this->_financialTypeId,
1211 'non_deductible_amount' => 20.00,
1212 'trxn_id' => 5454565,
1213 'invoice_id' => 1212124,
1214 'fee_amount' => 50.00,
1215 'net_amount' => 60.00,
1216 'contribution_status_id' => 2,
1217
1218 );
1219 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1220
1221 $params = array(
1222 'contribution_id' => $contribution2['id'],
1223
1224 );
1225 $result = $this->callAPISuccess('contribution', 'get', $params);
1226 $res = $result['values'][$contribution2['id']];
1227
1228 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1229 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1230 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'] );
1231 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1232 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1233 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1234 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1235 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1236 // contribution_status_id = 2 => Pending
1237 $this->assertEquals('Pending', $res['contribution_status']);
1238
1239 $this->contributionDelete($contribution1['id']);
1240 $this->contributionDelete($contribution2['id']);
1241 }
1242
1243 /**
1244 * Test completing a transaction via the API
1245 *
1246 * Note that we are creating a logged in user because email goes out from
1247 * that person
1248 */
1249 function testCompleteTransaction() {
1250 $mut = new CiviMailUtils( $this, true );
1251 $this->createLoggedInUser();
1252 $params = array_merge($this->_params, array('contribution_status_id' => 2,));
1253 $contribution = $this->callAPISuccess('contribution','create', $params);
1254 $this->callAPISuccess('contribution', 'completetransaction', array(
1255 'id' => $contribution['id'],
1256 ));
1257 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1258 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1259 $mut->checkMailLog(array(
1260 'Receipt - Contribution',
1261 'Please print this confirmation for your records.',
1262 ));
1263 $mut->stop();
1264 }
1265
1266 /**
1267 * CRM-14151
1268 * Test completing a transaction via the API
1269 *
1270 * tests.
1271 */
1272 function testCompleteTransactionWithReceiptDateSet() {
1273 $mut = new CiviMailUtils( $this, true );
1274 $this->createLoggedInUser();
1275 $params = array_merge($this->_params, array('contribution_status_id' => 2,'receipt_date' => 'now'));
1276 $contribution = $this->callAPISuccess('contribution','create', $params);
1277 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'],));
1278 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1279 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1280 $mut->checkMailLog(array(
1281 'Receipt - Contribution',
1282 'Please print this confirmation for your records.',
1283 ));
1284 $mut->stop();
1285 }
1286
1287 /**
1288 * Test completing a transaction with an event via the API
1289 *
1290 * Note that we are creating a logged in user because email goes out from
1291 * that person
1292 */
1293 function testCompleteTransactionWithParticipantRecord() {
1294 $mut = new CiviMailUtils( $this, true );
1295 $mut->clearMessages();
1296 $this->createLoggedInUser();
1297 $contributionID = $this->createPendingParticipantContribution();
1298 $this->callAPISuccess('contribution', 'completetransaction', array(
1299 'id' => $contributionID,)
1300 );
1301 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->_ids['participant'], 'return' => 'participant_status_id'));
1302 $this->assertEquals(1, $participantStatus);
1303 $mut->checkMailLog(array(
1304 'Annual CiviCRM meet',
1305 'Event',
1306 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1307 ));
1308 $mut->stop();
1309 }
1310
1311 /**
1312 * test membership is renewed when transaction completed
1313 */
1314 function testCompleteTransactionMembershipPriceSet() {
1315 $this->createPriceSetWithPage('membership');
1316 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
1317 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1318 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1319 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1320 $this->cleanUpAfterPriceSets();
1321 }
1322
1323 /**
1324 * test membership is renewed when transaction completed
1325 */
1326 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1327 $this->createPriceSetWithPage('membership');
1328 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
1329 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1330 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1331 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1332 $this->cleanUpAfterPriceSets();
1333 }
1334
1335 function cleanUpAfterPriceSets() {
1336 $this->quickCleanUpFinancialEntities();
1337 $this->contactDelete($this->_ids['contact']);
1338 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set']));
1339 }
1340
1341
1342 /**
1343 * this could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1344 * on parent class at some point (fn is not in 4.4)
1345 * @param $entity
1346 * @param array $params
1347 */
1348 function createPriceSetWithPage($entity, $params = array()) {
1349 $membershipTypeID = $this->membershipTypeCreate();
1350 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1351 'title' => "Test Contribution Page",
1352 'financial_type_id' => 1,
1353 'currency' => 'NZD',
1354 'goal_amount' => 50,
1355 'is_pay_later' => 1,
1356 'is_monetary' => TRUE,
1357 'is_email_receipt' => FALSE,
1358 ));
1359 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1360 'is_quick_config' => 0,
1361 'extends' => 'CiviMember',
1362 'financial_type_id' => 1,
1363 'title' => 'my Page'
1364 ));
1365 $priceSetID = $priceSet['id'];
1366
1367 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1368 $priceField = $this->callAPISuccess('price_field', 'create', array(
1369 'price_set_id' => $priceSetID ,
1370 'label' => 'Goat Breed',
1371 'html_type' => 'Radio',
1372 ));
1373 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1374 'price_set_id' => $priceSetID ,
1375 'price_field_id' => $priceField['id'],
1376 'label' => 'Long Haired Goat',
1377 'amount' => 20,
1378 'membership_type_id' => $membershipTypeID,
1379 'membership_num_terms' => 1,
1380 )
1381 );
1382 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1383 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1384 'price_set_id' => $priceSetID ,
1385 'price_field_id' => $priceField['id'],
1386 'label' => 'Shoe-eating Goat',
1387 'amount' => 10,
1388 'membership_type_id' => $membershipTypeID,
1389 'membership_num_terms' => 2,
1390 )
1391 );
1392 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
1393 $this->_ids['price_set'] = $priceSetID;
1394 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1395 $this->_ids['price_field'] = array($priceField['id']);
1396
1397 $this->_ids['membership_type'] = $membershipTypeID;
1398 }
1399
1400 /**
1401 * Set up a pending transaction with a specific price field id
1402 * @param $priceFieldValueID
1403 */
1404 function setUpPendingContribution($priceFieldValueID){
1405 $contactID = $this->individualCreate();
1406 $membership = $this->callAPISuccess('membership', 'create', array(
1407 'contact_id' => $contactID,
1408 'membership_type_id' => $this->_ids['membership_type'],
1409 'start_date' => 'yesterday - 1 year',
1410 'end_date' => 'yesterday',
1411 ));
1412 $contribution = $this->callAPISuccess('contribution', 'create', array(
1413 'domain_id' => 1,
1414 'contact_id' => $contactID,
1415 'receive_date' => date('Ymd'),
1416 'total_amount' => 100.00,
1417 'financial_type_id' => 1,
1418 'payment_instrument_id' => 'Credit Card',
1419 'non_deductible_amount' => 10.00,
1420 'trxn_id' => 'jdhfi88',
1421 'invoice_id' => 'djfhiewuyr',
1422 'source' => 'SSF',
1423 'contribution_status_id' => 2,
1424 'contribution_page_id' => $this->_ids['contribution_page'],
1425 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1426 ));
1427
1428 $this->callAPISuccess('line_item', 'create', array(
1429 'entity_id' => $contribution['id'],
1430 'entity_table' => 'civicrm_contribution',
1431 'contribution_id' => $contribution['id'],
1432 'price_field_id' => $this->_ids['price_field'][0],
1433 'qty' => 1,
1434 'unit_price' => 20,
1435 'line_total' => 20,
1436 'financial_type_id' => 1,
1437 'price_field_value_id' => $priceFieldValueID,
1438 ));
1439 $this->_ids['contact'] = $contactID;
1440 $this->_ids['contribution'] = $contribution['id'];
1441 $this->_ids['membership'] = $membership['id'];
1442 }
1443
1444 /**
1445 * Test sending a mail via the API
1446 */
1447 function testSendMail() {
1448 $mut = new CiviMailUtils( $this, true );
1449 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1450 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1451 'id' => $contribution['id'],
1452 'receipt_from_email' => 'api@civicrm.org',
1453 )
1454 );
1455 $mut->checkMailLog(array(
1456 '$ 100.00',
1457 'Contribution Information',
1458 'Please print this confirmation for your records',
1459 ), array(
1460 'Event'
1461 )
1462 );
1463 $mut->stop();
1464 }
1465
1466 /**
1467 * Test sending a mail via the API
1468 */
1469 function testSendMailEvent() {
1470 $mut = new CiviMailUtils( $this, true );
1471 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1472 $event = $this->eventCreate(array(
1473 'is_email_confirm' => 1,
1474 'confirm_from_email' => 'test@civicrm.org',
1475 ));
1476 $this->_eventID = $event['id'];
1477 $participantParams = array(
1478 'contact_id' => $this->_individualId,
1479 'event_id' => $this->_eventID,
1480 'status_id' => 1,
1481 'role_id' => 1,
1482 // to ensure it matches later on
1483 'register_date' => '2007-07-21 00:00:00',
1484 'source' => 'Online Event Registration: API Testing',
1485
1486 );
1487 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1488 $this->callAPISuccess('participant_payment', 'create', array(
1489 'participant_id' => $participant['id'],
1490 'contribution_id' => $contribution['id'],
1491 ));
1492 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1493 'id' => $contribution['id'],
1494 'receipt_from_email' => 'api@civicrm.org',
1495 )
1496 );
1497
1498 $mut->checkMailLog(array(
1499 'Annual CiviCRM meet',
1500 'Event',
1501 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1502 ), array(
1503
1504 )
1505 );
1506 $mut->stop();
1507 }
1508
1509 /**
1510 * This function does a GET & compares the result against the $params
1511 * Use as a double check on Creates
1512 */
1513 function contributionGetnCheck($params, $id, $delete = 1) {
1514
1515 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1516 'id' => $id,
1517
1518 ));
1519
1520 if ($delete) {
1521 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1522 }
1523 $this->assertAPISuccess($contribution, 0);
1524 $values = $contribution['values'][$contribution['id']];
1525 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1526 // this is not returned in id format
1527 unset($params['payment_instrument_id']);
1528 $params['contribution_source'] = $params['source'];
1529 unset($params['source']);
1530 foreach ($params as $key => $value) {
1531 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
1532 }
1533 }
1534
1535 /**
1536 * Create a pending contribution & linked pending participant record
1537 * (along with an event)
1538 */
1539 function createPendingParticipantContribution(){
1540 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1541 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1542 $this->_ids['participant'] = $participantID;
1543 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1544 $contribution = $this->callAPISuccess('contribution','create', $params);
1545 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1546 $this->callAPISuccess('line_item', 'get', array(
1547 'entity_id' => $contribution['id'],
1548 'entity_table' => 'civicrm_contribution',
1549 'api.line_item.create' => array(
1550 'entity_id' => $participantID,
1551 'entity_table' => 'civicrm_participant',
1552 ),
1553 ));
1554 return $contribution['id'];
1555 }
1556
1557 /**
1558 * @param $contId
1559 *
1560 * @return null|string
1561 */function _getFinancialTrxnAmount($contId) {
1562 $query = "SELECT
1563 SUM( ft.total_amount ) AS total
1564 FROM civicrm_financial_trxn AS ft
1565 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1566 WHERE ceft.entity_table = 'civicrm_contribution'
1567 AND ceft.entity_id = {$contId}";
1568
1569 $result = CRM_Core_DAO::singleValueQuery($query);
1570 return $result;
1571 }
1572
1573 /**
1574 * @param $contId
1575 *
1576 * @return null|string
1577 */function _getFinancialItemAmount($contId) {
1578 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1579 $query = "SELECT
1580 SUM(amount)
1581 FROM civicrm_financial_item
1582 WHERE entity_table = 'civicrm_line_item'
1583 AND entity_id = {$lineItem}";
1584 $result = CRM_Core_DAO::singleValueQuery($query);
1585 return $result;
1586 }
1587
1588 /**
1589 * @param $contId
1590 * @param $context
1591 */
1592 function _checkFinancialItem($contId, $context) {
1593 if ($context != 'paylater') {
1594 $params = array (
1595 'entity_id' => $contId,
1596 'entity_table' => 'civicrm_contribution',
1597 );
1598 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1599 $entityParams = array(
1600 'financial_trxn_id' => $trxn['financial_trxn_id'],
1601 'entity_table' => 'civicrm_financial_item',
1602 );
1603 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1604 $params = array(
1605 'id' => $entityTrxn['entity_id'],
1606 );
1607 }
1608 if ($context == 'paylater') {
1609 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1610 foreach ($lineItems as $key=>$item) {
1611 $params = array(
1612 'entity_id' => $key,
1613 'entity_table' => 'civicrm_line_item',
1614 );
1615 $compareParams = array ('status_id' => 1);
1616 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1617 }
1618 }
1619 elseif ($context == 'refund') {
1620 $compareParams = array(
1621 'status_id' => 1,
1622 'financial_account_id' => 1,
1623 'amount' => -100,
1624 );
1625 }
1626 elseif ($context == 'cancelPending') {
1627 $compareParams = array(
1628 'status_id' => 3,
1629 'financial_account_id' => 1,
1630 'amount' => -100,
1631 );
1632 }
1633 elseif ($context == 'changeFinancial') {
1634 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1635 $params = array(
1636 'entity_id' => $lineKey,
1637 'amount' => -100,
1638 );
1639 $compareParams = array(
1640 'financial_account_id' => 1,
1641 );
1642 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1643 $params = array(
1644 'financial_account_id' => 3,
1645 'entity_id' => $lineKey,
1646 );
1647 $compareParams = array(
1648 'amount' => 100,
1649 );
1650 }
1651 if ($context != 'paylater') {
1652 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1653 }
1654 }
1655
1656 /**
1657 * @param $contribution
1658 * @param $context
1659 * @param null $instrumentId
1660 */
1661 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1662 $trxnParams = array(
1663 'entity_id' => $contribution['id'],
1664 'entity_table' => 'civicrm_contribution',
1665 );
1666 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1667 $params = array(
1668 'id' => $trxn['financial_trxn_id'],
1669 );
1670 if ($context == 'payLater') {
1671 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1672 $compareParams = array(
1673 'status_id' => 1,
1674 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1675 );
1676 }
1677 elseif ($context == 'refund') {
1678 $compareParams = array(
1679 'to_financial_account_id' => 6,
1680 'total_amount' => -100,
1681 'status_id' => 7,
1682 );
1683 }
1684 elseif ($context == 'cancelPending') {
1685 $compareParams = array(
1686 'from_financial_account_id' => 7,
1687 'total_amount' => -100,
1688 'status_id' => 3,
1689 );
1690 }
1691 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
1692 $entityParams = array(
1693 'entity_id' => $contribution['id'],
1694 'entity_table' => 'civicrm_contribution',
1695 'amount' => -100,
1696 );
1697 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1698 $trxnParams1 = array(
1699 'id' => $trxn['financial_trxn_id'],
1700 );
1701 $compareParams = array(
1702 'total_amount' => -100,
1703 'status_id' => 1,
1704 );
1705 if ($context == 'paymentInstrument') {
1706 $compareParams += array(
1707 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1708 'payment_instrument_id' => 4,
1709 );
1710 }
1711 else {
1712 $compareParams['to_financial_account_id'] = 12;
1713 }
1714 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1715 $compareParams['total_amount'] = 100;
1716 if ($context == 'paymentInstrument') {
1717 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1718 $compareParams['payment_instrument_id'] = $instrumentId;
1719 }
1720 else {
1721 $compareParams['to_financial_account_id'] = 12;
1722 }
1723 }
1724
1725 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1726 }
1727
1728 /**
1729 * @return mixed
1730 */
1731 function _addPaymentInstrument () {
1732 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1733 $optionParams = array(
1734 'option_group_id' => $gId,
1735 'label' => 'Test Card',
1736 'name' => 'Test Card',
1737 'value' => '6',
1738 'weight' => '6',
1739 'is_active' => 1,
1740 );
1741 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1742 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1743 $financialParams = array(
1744 'entity_table' => 'civicrm_option_value',
1745 'entity_id' => $optionValue['id'],
1746 'account_relationship' => $relationTypeId,
1747 'financial_account_id' => 7,
1748 );
1749 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1750 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1751 return $optionValue['values'][$optionValue['id']]['value'];
1752 }
1753
1754 /**
1755 * @param $params
1756 * @param $context
1757 */
1758 function _checkFinancialRecords($params,$context) {
1759 $entityParams = array(
1760 'entity_id' => $params['id'],
1761 'entity_table' => 'civicrm_contribution',
1762 );
1763 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1764 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1765 if ($context == 'pending') {
1766 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1767 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1768 return;
1769 }
1770 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1771 $trxnParams = array(
1772 'id' => $trxn['financial_trxn_id'],
1773 );
1774 if ($context != 'online' && $context != 'payLater') {
1775 $compareParams = array(
1776 'to_financial_account_id' => 6,
1777 'total_amount' => 100,
1778 'status_id' => 1,
1779 );
1780 }
1781 if ($context == 'feeAmount') {
1782 $compareParams['fee_amount'] = 50;
1783 }
1784 elseif ($context == 'online') {
1785 $compareParams = array(
1786 'to_financial_account_id' => 12,
1787 'total_amount' => 100,
1788 'status_id' => 1,
1789 );
1790 }
1791 elseif ($context == 'payLater') {
1792 $compareParams = array(
1793 'to_financial_account_id' => 7,
1794 'total_amount' => 100,
1795 'status_id' => 2,
1796 );
1797 }
1798 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams, $compareParams);
1799 $entityParams = array(
1800 'financial_trxn_id' => $trxn['financial_trxn_id'],
1801 'entity_table' => 'civicrm_financial_item',
1802 );
1803 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1804 $fitemParams = array(
1805 'id' => $entityTrxn['entity_id'],
1806 );
1807 $compareParams = array(
1808 'amount' => 100,
1809 'status_id' => 1,
1810 'financial_account_id' => 1,
1811 );
1812 if ($context == 'payLater') {
1813 $compareParams = array(
1814 'amount' => 100,
1815 'status_id' => 3,
1816 'financial_account_id' => 1,
1817 );
1818 }
1819 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1820 if ($context == 'feeAmount') {
1821 $maxParams = array(
1822 'entity_id' => $params['id'],
1823 'entity_table' => 'civicrm_contribution',
1824 );
1825 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1826 $trxnParams = array(
1827 'id' => $maxTrxn['financial_trxn_id'],
1828 );
1829 $compareParams = array(
1830 'to_financial_account_id' => 5,
1831 'from_financial_account_id' => 6,
1832 'total_amount' => 50,
1833 'status_id' => 1,
1834 );
1835 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1836 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1837 $fitemParams = array(
1838 'entity_id' => $trxnId['financialTrxnId'],
1839 'entity_table' => 'civicrm_financial_trxn',
1840 );
1841 $compareParams = array(
1842 'amount' => 50,
1843 'status_id' => 1,
1844 'financial_account_id' => 5,
1845 );
1846 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1847 }
1848 }
1849 }
1850