Merge branch 'master' into findById
[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 /**
612 * This is the test for creating soft credits - however a 'get' is not yet possible via API
613 * as the current BAO functions are contact-centric (from what I can find)
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 * Test that BAO defaults work
859 */
860 function testCreateBAODefaults() {
861 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
862 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
863 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
864 $this->assertEquals(1, $contribution['contribution_status_id']);
865 $this->assertEquals('Check', $contribution['payment_instrument']);
866 }
867
868 /*
869 * Function tests that line items, financial records are updated when contribution amount is changed
870 */
871 function testCreateUpdateContributionChangeTotal() {
872 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
873 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
874
875 'entity_id' => $contribution['id'],
876 'entity_table' => 'civicrm_contribution',
877 'sequential' => 1,
878 'return' => 'line_total',
879 ));
880 $this->assertEquals('100.00', $lineItems);
881 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
882 // Financial trxn SUM = 100 + 5 (fee)
883 $this->assertEquals('105.00', $trxnAmount);
884 $newParams = array(
885
886 'id' => $contribution['id'],
887 'total_amount' => '125');
888 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
889
890 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
891
892 'entity_id' => $contribution['id'],
893 'entity_table' => 'civicrm_contribution',
894 'sequential' => 1,
895 'return' => 'line_total',
896 ));
897
898 $this->assertEquals('125.00', $lineItems);
899 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
900 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
901 // Financial trxn SUM = 125 + 5 (fee)
902 $this->assertEquals('130.00', $trxnAmount);
903 $this->assertEquals('125.00', $fitemAmount);
904 }
905
906 /*
907 * Function tests that line items, financial records are updated when pay later contribution is received
908 */
909 function testCreateUpdateContributionPayLater() {
910 $contribParams = array(
911 'contact_id' => $this->_individualId,
912 'receive_date' => '2012-01-01',
913 'total_amount' => 100.00,
914 'financial_type_id' => $this->_financialTypeId,
915 'payment_instrument_id' => 1,
916 'contribution_status_id' => 2,
917 'is_pay_later' => 1,
918
919 );
920 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
921
922 $newParams = array_merge($contribParams, array(
923 'id' => $contribution['id'],
924 'contribution_status_id' => 1,)
925 );
926 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
927 $contribution = $contribution['values'][$contribution['id']];
928 $this->assertEquals($contribution['contribution_status_id'],'1');
929 $this->_checkFinancialItem($contribution['id'], 'paylater');
930 $this->_checkFinancialTrxn($contribution, 'payLater');
931 }
932
933 /*
934 * Function tests that financial records are updated when Payment Instrument is changed
935 */
936 function testCreateUpdateContributionPaymentInstrument() {
937 $instrumentId = $this->_addPaymentInstrument();
938 $contribParams = array(
939 'contact_id' => $this->_individualId,
940 'total_amount' => 100.00,
941 'financial_type_id' => $this->_financialTypeId,
942 'payment_instrument_id' => 4,
943 'contribution_status_id' => 1,
944
945 );
946 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
947
948 $newParams = array_merge($contribParams, array(
949 'id' => $contribution['id'],
950 'payment_instrument_id' => $instrumentId,)
951 );
952 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
953 $this->assertAPISuccess($contribution);
954 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
955 }
956
957 /*
958 * Function tests that financial records are added when Contribution is Refunded
959 */
960 function testCreateUpdateContributionRefund() {
961 $contribParams = array(
962 'contact_id' => $this->_individualId,
963 'receive_date' => '2012-01-01',
964 'total_amount' => 100.00,
965 'financial_type_id' => $this->_financialTypeId,
966 'payment_instrument_id' => 4,
967 'contribution_status_id' => 1,
968
969 );
970 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
971 $newParams = array_merge($contribParams, array(
972 'id' => $contribution['id'],
973 'contribution_status_id' => 7,
974 )
975 );
976
977 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
978 $this->_checkFinancialTrxn($contribution, 'refund');
979 $this->_checkFinancialItem($contribution['id'], 'refund');
980 }
981
982 /*
983 * Function tests invalid contribution status change
984 */
985 function testCreateUpdateContributionInValidStatusChange() {
986 $contribParams = array(
987 'contact_id' => 1,
988 'receive_date' => '2012-01-01',
989 'total_amount' => 100.00,
990 'financial_type_id' => 1,
991 'payment_instrument_id' => 1,
992 'contribution_status_id' => 1,
993 );
994 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
995 $newParams = array_merge($contribParams, array(
996 'id' => $contribution['id'],
997 'contribution_status_id' => 2,
998 )
999 );
1000 $this->callAPIFailure('contribution', 'create', $newParams,ts('Cannot change contribution status from Completed to Pending.'));
1001
1002 }
1003
1004 /*
1005 * Function tests that financial records are added when Pending Contribution is Canceled
1006 */
1007 function testCreateUpdateContributionCancelPending() {
1008 $contribParams = array(
1009 'contact_id' => $this->_individualId,
1010 'receive_date' => '2012-01-01',
1011 'total_amount' => 100.00,
1012 'financial_type_id' => $this->_financialTypeId,
1013 'payment_instrument_id' => 1,
1014 'contribution_status_id' => 2,
1015 'is_pay_later' => 1,
1016
1017 );
1018 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1019 $newParams = array_merge($contribParams, array(
1020 'id' => $contribution['id'],
1021 'contribution_status_id' => 3,
1022 )
1023 );
1024 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1025 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1026 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1027 }
1028
1029 /*
1030 * Function tests that financial records are added when Financial Type is Changed
1031 */
1032 function testCreateUpdateContributionChangeFinancialType() {
1033 $contribParams = array(
1034 'contact_id' => $this->_individualId,
1035 'receive_date' => '2012-01-01',
1036 'total_amount' => 100.00,
1037 'financial_type_id' => 1,
1038 'payment_instrument_id' => 1,
1039 'contribution_status_id' => 1,
1040
1041 );
1042 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1043 $newParams = array_merge($contribParams, array(
1044 'id' => $contribution['id'],
1045 'financial_type_id' => 3,
1046 )
1047 );
1048 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1049 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1050 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1051 }
1052
1053 /**
1054 * Test that update does not change status id CRM-15105
1055 */
1056 function testCreateUpdateWithoutChangingPendingStatus() {
1057 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1058 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1059 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1060 $this->assertEquals(2, $contribution['contribution_status_id']);
1061 }
1062 //To Update Contribution
1063 //CHANGE: we require the API to do an incremental update
1064 function testCreateUpdateContribution() {
1065
1066 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
1067 $old_params = array(
1068 'contribution_id' => $contributionID,
1069
1070 );
1071 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1072 //Make sure it came back
1073 $this->assertAPISuccess($original);
1074 $this->assertEquals($original['id'], $contributionID);
1075 //set up list of old params, verify
1076
1077 //This should not be required on update:
1078 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1079 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1080 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1081 $old_source = $original['values'][$contributionID]['contribution_source'];
1082
1083 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1084 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1085
1086 //check against values in CiviUnitTestCase::createContribution()
1087 $this->assertEquals($old_contact_id, $this->_individualId);
1088 $this->assertEquals($old_fee_amount, 5.00);
1089 $this->assertEquals($old_source, 'SSF');
1090 $this->assertEquals($old_trxn_id, 212355);
1091 $this->assertEquals($old_invoice_id, 'idofsh');
1092 $params = array(
1093 'id' => $contributionID,
1094 'contact_id' => $this->_individualId,
1095 'total_amount' => 110.00,
1096 'financial_type_id' => $this->_financialTypeId,
1097 'non_deductible_amount' => 10.00,
1098 'net_amount' => 100.00,
1099 'contribution_status_id' => 1,
1100 'note' => 'Donating for Nobel Cause',
1101
1102 );
1103
1104 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1105
1106 $new_params = array(
1107 'contribution_id' => $contribution['id'],
1108
1109 );
1110 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1111
1112 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1113 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1114 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId );
1115 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1116 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1117 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1118 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1119 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1120 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1121 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1122 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1123 $params = array(
1124 'contribution_id' => $contributionID,
1125
1126 );
1127 $result = $this->callAPISuccess('contribution', 'delete', $params);
1128 $this->assertAPISuccess($result, 'in line' . __LINE__);
1129 }
1130
1131 ///////////////// civicrm_contribution_delete methods
1132 function testDeleteEmptyParamsContribution() {
1133 $params = array();
1134 $this->callAPIFailure('contribution', 'delete', $params);
1135 }
1136
1137 function testDeleteParamsNotArrayContribution() {
1138 $params = 'contribution_id= 1';
1139 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1140 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1141 }
1142
1143 function testDeleteWrongParamContribution() {
1144 $params = array(
1145 'contribution_source' => 'SSF',
1146
1147 );
1148 $this->callAPIFailure('contribution', 'delete', $params);
1149 }
1150
1151 function testDeleteContribution() {
1152
1153 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
1154 $params = array(
1155 'id' => $contributionID,
1156 );
1157 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1158 }
1159
1160 /**
1161 * Test civicrm_contribution_search with empty params.
1162 * All available contributions expected.
1163 */
1164 function testSearchEmptyParams() {
1165 $params = array();
1166
1167 $p = array(
1168 'contact_id' => $this->_individualId,
1169 'receive_date' => date('Ymd'),
1170 'total_amount' => 100.00,
1171 'financial_type_id' => $this->_financialTypeId,
1172 'non_deductible_amount' => 10.00,
1173 'fee_amount' => 5.00,
1174 'net_amount' => 95.00,
1175 'trxn_id' => 23456,
1176 'invoice_id' => 78910,
1177 'source' => 'SSF',
1178 'contribution_status_id' => 1,
1179
1180 );
1181 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1182
1183 $result = $this->callAPISuccess('contribution', 'get', $params);
1184 // We're taking the first element.
1185 $res = $result['values'][$contribution['id']];
1186
1187 $this->assertEquals($p['contact_id'], $res['contact_id']);
1188 $this->assertEquals($p['total_amount'], $res['total_amount']);
1189 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'] );
1190 $this->assertEquals($p['net_amount'], $res['net_amount']);
1191 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1192 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1193 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1194 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1195 $this->assertEquals($p['source'], $res['contribution_source']);
1196 // contribution_status_id = 1 => Completed
1197 $this->assertEquals('Completed', $res['contribution_status']);
1198
1199 $this->contributionDelete($contribution['id']);
1200 }
1201
1202 /**
1203 * Test civicrm_contribution_search. Success expected.
1204 */
1205 function testSearch() {
1206 $p1 = array(
1207 'contact_id' => $this->_individualId,
1208 'receive_date' => date('Ymd'),
1209 'total_amount' => 100.00,
1210 'financial_type_id' => $this->_financialTypeId,
1211 'non_deductible_amount' => 10.00,
1212 'contribution_status_id' => 1,
1213
1214 );
1215 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1216
1217 $p2 = array(
1218 'contact_id' => $this->_individualId,
1219 'receive_date' => date('Ymd'),
1220 'total_amount' => 200.00,
1221 'financial_type_id' => $this->_financialTypeId,
1222 'non_deductible_amount' => 20.00,
1223 'trxn_id' => 5454565,
1224 'invoice_id' => 1212124,
1225 'fee_amount' => 50.00,
1226 'net_amount' => 60.00,
1227 'contribution_status_id' => 2,
1228
1229 );
1230 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1231
1232 $params = array(
1233 'contribution_id' => $contribution2['id'],
1234
1235 );
1236 $result = $this->callAPISuccess('contribution', 'get', $params);
1237 $res = $result['values'][$contribution2['id']];
1238
1239 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1240 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1241 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'] );
1242 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1243 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1244 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1245 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1246 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1247 // contribution_status_id = 2 => Pending
1248 $this->assertEquals('Pending', $res['contribution_status']);
1249
1250 $this->contributionDelete($contribution1['id']);
1251 $this->contributionDelete($contribution2['id']);
1252 }
1253
1254 /**
1255 * Test completing a transaction via the API
1256 *
1257 * Note that we are creating a logged in user because email goes out from
1258 * that person
1259 */
1260 function testCompleteTransaction() {
1261 $mut = new CiviMailUtils( $this, true );
1262 $this->createLoggedInUser();
1263 $params = array_merge($this->_params, array('contribution_status_id' => 2,));
1264 $contribution = $this->callAPISuccess('contribution','create', $params);
1265 $this->callAPISuccess('contribution', 'completetransaction', array(
1266 'id' => $contribution['id'],
1267 ));
1268 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1269 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1270 $mut->checkMailLog(array(
1271 'Receipt - Contribution',
1272 'Please print this confirmation for your records.',
1273 ));
1274 $mut->stop();
1275 }
1276
1277 /**
1278 * CRM-14151
1279 * Test completing a transaction via the API
1280 *
1281 * tests.
1282 */
1283 function testCompleteTransactionWithReceiptDateSet() {
1284 $mut = new CiviMailUtils( $this, true );
1285 $this->createLoggedInUser();
1286 $params = array_merge($this->_params, array('contribution_status_id' => 2,'receipt_date' => 'now'));
1287 $contribution = $this->callAPISuccess('contribution','create', $params);
1288 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'],));
1289 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1290 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1291 $mut->checkMailLog(array(
1292 'Receipt - Contribution',
1293 'Please print this confirmation for your records.',
1294 ));
1295 $mut->stop();
1296 }
1297
1298 /**
1299 * Test completing a transaction with an event via the API
1300 *
1301 * Note that we are creating a logged in user because email goes out from
1302 * that person
1303 */
1304 function testCompleteTransactionWithParticipantRecord() {
1305 $mut = new CiviMailUtils( $this, true );
1306 $mut->clearMessages();
1307 $this->createLoggedInUser();
1308 $contributionID = $this->createPendingParticipantContribution();
1309 $this->callAPISuccess('contribution', 'completetransaction', array(
1310 'id' => $contributionID,)
1311 );
1312 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->_ids['participant'], 'return' => 'participant_status_id'));
1313 $this->assertEquals(1, $participantStatus);
1314 $mut->checkMailLog(array(
1315 'Annual CiviCRM meet',
1316 'Event',
1317 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1318 ));
1319 $mut->stop();
1320 }
1321
1322 /**
1323 * Test membership is renewed when transaction completed
1324 */
1325 function testCompleteTransactionMembershipPriceSet() {
1326 $this->createPriceSetWithPage('membership');
1327 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
1328 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1329 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1330 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1331 $this->cleanUpAfterPriceSets();
1332 }
1333
1334 /**
1335 * Test membership is renewed when transaction completed
1336 */
1337 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1338 $this->createPriceSetWithPage('membership');
1339 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
1340 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1341 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1342 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1343 $this->cleanUpAfterPriceSets();
1344 }
1345
1346 function cleanUpAfterPriceSets() {
1347 $this->quickCleanUpFinancialEntities();
1348 $this->contactDelete($this->_ids['contact']);
1349 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set']));
1350 }
1351
1352
1353 /**
1354 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1355 * on parent class at some point (fn is not in 4.4)
1356 * @param $entity
1357 * @param array $params
1358 */
1359 function createPriceSetWithPage($entity, $params = array()) {
1360 $membershipTypeID = $this->membershipTypeCreate();
1361 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1362 'title' => "Test Contribution Page",
1363 'financial_type_id' => 1,
1364 'currency' => 'NZD',
1365 'goal_amount' => 50,
1366 'is_pay_later' => 1,
1367 'is_monetary' => TRUE,
1368 'is_email_receipt' => FALSE,
1369 ));
1370 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1371 'is_quick_config' => 0,
1372 'extends' => 'CiviMember',
1373 'financial_type_id' => 1,
1374 'title' => 'my Page'
1375 ));
1376 $priceSetID = $priceSet['id'];
1377
1378 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1379 $priceField = $this->callAPISuccess('price_field', 'create', array(
1380 'price_set_id' => $priceSetID ,
1381 'label' => 'Goat Breed',
1382 'html_type' => 'Radio',
1383 ));
1384 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1385 'price_set_id' => $priceSetID ,
1386 'price_field_id' => $priceField['id'],
1387 'label' => 'Long Haired Goat',
1388 'amount' => 20,
1389 'membership_type_id' => $membershipTypeID,
1390 'membership_num_terms' => 1,
1391 )
1392 );
1393 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1394 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1395 'price_set_id' => $priceSetID ,
1396 'price_field_id' => $priceField['id'],
1397 'label' => 'Shoe-eating Goat',
1398 'amount' => 10,
1399 'membership_type_id' => $membershipTypeID,
1400 'membership_num_terms' => 2,
1401 )
1402 );
1403 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
1404 $this->_ids['price_set'] = $priceSetID;
1405 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1406 $this->_ids['price_field'] = array($priceField['id']);
1407
1408 $this->_ids['membership_type'] = $membershipTypeID;
1409 }
1410
1411 /**
1412 * Set up a pending transaction with a specific price field id
1413 * @param int $priceFieldValueID
1414 */
1415 function setUpPendingContribution($priceFieldValueID){
1416 $contactID = $this->individualCreate();
1417 $membership = $this->callAPISuccess('membership', 'create', array(
1418 'contact_id' => $contactID,
1419 'membership_type_id' => $this->_ids['membership_type'],
1420 'start_date' => 'yesterday - 1 year',
1421 'end_date' => 'yesterday',
1422 ));
1423 $contribution = $this->callAPISuccess('contribution', 'create', array(
1424 'domain_id' => 1,
1425 'contact_id' => $contactID,
1426 'receive_date' => date('Ymd'),
1427 'total_amount' => 100.00,
1428 'financial_type_id' => 1,
1429 'payment_instrument_id' => 'Credit Card',
1430 'non_deductible_amount' => 10.00,
1431 'trxn_id' => 'jdhfi88',
1432 'invoice_id' => 'djfhiewuyr',
1433 'source' => 'SSF',
1434 'contribution_status_id' => 2,
1435 'contribution_page_id' => $this->_ids['contribution_page'],
1436 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1437 ));
1438
1439 $this->callAPISuccess('line_item', 'create', array(
1440 'entity_id' => $contribution['id'],
1441 'entity_table' => 'civicrm_contribution',
1442 'contribution_id' => $contribution['id'],
1443 'price_field_id' => $this->_ids['price_field'][0],
1444 'qty' => 1,
1445 'unit_price' => 20,
1446 'line_total' => 20,
1447 'financial_type_id' => 1,
1448 'price_field_value_id' => $priceFieldValueID,
1449 ));
1450 $this->_ids['contact'] = $contactID;
1451 $this->_ids['contribution'] = $contribution['id'];
1452 $this->_ids['membership'] = $membership['id'];
1453 }
1454
1455 /**
1456 * Test sending a mail via the API
1457 */
1458 function testSendMail() {
1459 $mut = new CiviMailUtils( $this, true );
1460 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1461 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1462 'id' => $contribution['id'],
1463 'receipt_from_email' => 'api@civicrm.org',
1464 )
1465 );
1466 $mut->checkMailLog(array(
1467 '$ 100.00',
1468 'Contribution Information',
1469 'Please print this confirmation for your records',
1470 ), array(
1471 'Event'
1472 )
1473 );
1474 $mut->stop();
1475 }
1476
1477 /**
1478 * Test sending a mail via the API
1479 */
1480 function testSendMailEvent() {
1481 $mut = new CiviMailUtils( $this, true );
1482 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1483 $event = $this->eventCreate(array(
1484 'is_email_confirm' => 1,
1485 'confirm_from_email' => 'test@civicrm.org',
1486 ));
1487 $this->_eventID = $event['id'];
1488 $participantParams = array(
1489 'contact_id' => $this->_individualId,
1490 'event_id' => $this->_eventID,
1491 'status_id' => 1,
1492 'role_id' => 1,
1493 // to ensure it matches later on
1494 'register_date' => '2007-07-21 00:00:00',
1495 'source' => 'Online Event Registration: API Testing',
1496
1497 );
1498 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1499 $this->callAPISuccess('participant_payment', 'create', array(
1500 'participant_id' => $participant['id'],
1501 'contribution_id' => $contribution['id'],
1502 ));
1503 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1504 'id' => $contribution['id'],
1505 'receipt_from_email' => 'api@civicrm.org',
1506 )
1507 );
1508
1509 $mut->checkMailLog(array(
1510 'Annual CiviCRM meet',
1511 'Event',
1512 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1513 ), array(
1514
1515 )
1516 );
1517 $mut->stop();
1518 }
1519
1520 /**
1521 * This function does a GET & compares the result against the $params
1522 * Use as a double check on Creates
1523 */
1524 function contributionGetnCheck($params, $id, $delete = 1) {
1525
1526 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1527 'id' => $id,
1528
1529 ));
1530
1531 if ($delete) {
1532 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1533 }
1534 $this->assertAPISuccess($contribution, 0);
1535 $values = $contribution['values'][$contribution['id']];
1536 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1537 // this is not returned in id format
1538 unset($params['payment_instrument_id']);
1539 $params['contribution_source'] = $params['source'];
1540 unset($params['source']);
1541 foreach ($params as $key => $value) {
1542 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
1543 }
1544 }
1545
1546 /**
1547 * Create a pending contribution & linked pending participant record
1548 * (along with an event)
1549 */
1550 function createPendingParticipantContribution(){
1551 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1552 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1553 $this->_ids['participant'] = $participantID;
1554 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1555 $contribution = $this->callAPISuccess('contribution','create', $params);
1556 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1557 $this->callAPISuccess('line_item', 'get', array(
1558 'entity_id' => $contribution['id'],
1559 'entity_table' => 'civicrm_contribution',
1560 'api.line_item.create' => array(
1561 'entity_id' => $participantID,
1562 'entity_table' => 'civicrm_participant',
1563 ),
1564 ));
1565 return $contribution['id'];
1566 }
1567
1568 /**
1569 * @param int $contId
1570 *
1571 * @return null|string
1572 */function _getFinancialTrxnAmount($contId) {
1573 $query = "SELECT
1574 SUM( ft.total_amount ) AS total
1575 FROM civicrm_financial_trxn AS ft
1576 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1577 WHERE ceft.entity_table = 'civicrm_contribution'
1578 AND ceft.entity_id = {$contId}";
1579
1580 $result = CRM_Core_DAO::singleValueQuery($query);
1581 return $result;
1582 }
1583
1584 /**
1585 * @param int $contId
1586 *
1587 * @return null|string
1588 */function _getFinancialItemAmount($contId) {
1589 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1590 $query = "SELECT
1591 SUM(amount)
1592 FROM civicrm_financial_item
1593 WHERE entity_table = 'civicrm_line_item'
1594 AND entity_id = {$lineItem}";
1595 $result = CRM_Core_DAO::singleValueQuery($query);
1596 return $result;
1597 }
1598
1599 /**
1600 * @param int $contId
1601 * @param $context
1602 */
1603 function _checkFinancialItem($contId, $context) {
1604 if ($context != 'paylater') {
1605 $params = array (
1606 'entity_id' => $contId,
1607 'entity_table' => 'civicrm_contribution',
1608 );
1609 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1610 $entityParams = array(
1611 'financial_trxn_id' => $trxn['financial_trxn_id'],
1612 'entity_table' => 'civicrm_financial_item',
1613 );
1614 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1615 $params = array(
1616 'id' => $entityTrxn['entity_id'],
1617 );
1618 }
1619 if ($context == 'paylater') {
1620 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1621 foreach ($lineItems as $key=>$item) {
1622 $params = array(
1623 'entity_id' => $key,
1624 'entity_table' => 'civicrm_line_item',
1625 );
1626 $compareParams = array ('status_id' => 1);
1627 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1628 }
1629 }
1630 elseif ($context == 'refund') {
1631 $compareParams = array(
1632 'status_id' => 1,
1633 'financial_account_id' => 1,
1634 'amount' => -100,
1635 );
1636 }
1637 elseif ($context == 'cancelPending') {
1638 $compareParams = array(
1639 'status_id' => 3,
1640 'financial_account_id' => 1,
1641 'amount' => -100,
1642 );
1643 }
1644 elseif ($context == 'changeFinancial') {
1645 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1646 $params = array(
1647 'entity_id' => $lineKey,
1648 'amount' => -100,
1649 );
1650 $compareParams = array(
1651 'financial_account_id' => 1,
1652 );
1653 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1654 $params = array(
1655 'financial_account_id' => 3,
1656 'entity_id' => $lineKey,
1657 );
1658 $compareParams = array(
1659 'amount' => 100,
1660 );
1661 }
1662 if ($context != 'paylater') {
1663 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1664 }
1665 }
1666
1667 /**
1668 * @param $contribution
1669 * @param $context
1670 * @param int $instrumentId
1671 */
1672 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1673 $trxnParams = array(
1674 'entity_id' => $contribution['id'],
1675 'entity_table' => 'civicrm_contribution',
1676 );
1677 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1678 $params = array(
1679 'id' => $trxn['financial_trxn_id'],
1680 );
1681 if ($context == 'payLater') {
1682 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1683 $compareParams = array(
1684 'status_id' => 1,
1685 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1686 );
1687 }
1688 elseif ($context == 'refund') {
1689 $compareParams = array(
1690 'to_financial_account_id' => 6,
1691 'total_amount' => -100,
1692 'status_id' => 7,
1693 );
1694 }
1695 elseif ($context == 'cancelPending') {
1696 $compareParams = array(
1697 'from_financial_account_id' => 7,
1698 'total_amount' => -100,
1699 'status_id' => 3,
1700 );
1701 }
1702 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
1703 $entityParams = array(
1704 'entity_id' => $contribution['id'],
1705 'entity_table' => 'civicrm_contribution',
1706 'amount' => -100,
1707 );
1708 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1709 $trxnParams1 = array(
1710 'id' => $trxn['financial_trxn_id'],
1711 );
1712 $compareParams = array(
1713 'total_amount' => -100,
1714 'status_id' => 1,
1715 );
1716 if ($context == 'paymentInstrument') {
1717 $compareParams += array(
1718 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1719 'payment_instrument_id' => 4,
1720 );
1721 }
1722 else {
1723 $compareParams['to_financial_account_id'] = 12;
1724 }
1725 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1726 $compareParams['total_amount'] = 100;
1727 if ($context == 'paymentInstrument') {
1728 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1729 $compareParams['payment_instrument_id'] = $instrumentId;
1730 }
1731 else {
1732 $compareParams['to_financial_account_id'] = 12;
1733 }
1734 }
1735
1736 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1737 }
1738
1739 /**
1740 * @return mixed
1741 */
1742 function _addPaymentInstrument () {
1743 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1744 $optionParams = array(
1745 'option_group_id' => $gId,
1746 'label' => 'Test Card',
1747 'name' => 'Test Card',
1748 'value' => '6',
1749 'weight' => '6',
1750 'is_active' => 1,
1751 );
1752 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1753 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1754 $financialParams = array(
1755 'entity_table' => 'civicrm_option_value',
1756 'entity_id' => $optionValue['id'],
1757 'account_relationship' => $relationTypeId,
1758 'financial_account_id' => 7,
1759 );
1760 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1761 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1762 return $optionValue['values'][$optionValue['id']]['value'];
1763 }
1764
1765 /**
1766 * @param array $params
1767 * @param $context
1768 */
1769 function _checkFinancialRecords($params,$context) {
1770 $entityParams = array(
1771 'entity_id' => $params['id'],
1772 'entity_table' => 'civicrm_contribution',
1773 );
1774 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1775 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1776 if ($context == 'pending') {
1777 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1778 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1779 return;
1780 }
1781 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1782 $trxnParams = array(
1783 'id' => $trxn['financial_trxn_id'],
1784 );
1785 if ($context != 'online' && $context != 'payLater') {
1786 $compareParams = array(
1787 'to_financial_account_id' => 6,
1788 'total_amount' => 100,
1789 'status_id' => 1,
1790 );
1791 }
1792 if ($context == 'feeAmount') {
1793 $compareParams['fee_amount'] = 50;
1794 }
1795 elseif ($context == 'online') {
1796 $compareParams = array(
1797 'to_financial_account_id' => 12,
1798 'total_amount' => 100,
1799 'status_id' => 1,
1800 );
1801 }
1802 elseif ($context == 'payLater') {
1803 $compareParams = array(
1804 'to_financial_account_id' => 7,
1805 'total_amount' => 100,
1806 'status_id' => 2,
1807 );
1808 }
1809 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams, $compareParams);
1810 $entityParams = array(
1811 'financial_trxn_id' => $trxn['financial_trxn_id'],
1812 'entity_table' => 'civicrm_financial_item',
1813 );
1814 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1815 $fitemParams = array(
1816 'id' => $entityTrxn['entity_id'],
1817 );
1818 $compareParams = array(
1819 'amount' => 100,
1820 'status_id' => 1,
1821 'financial_account_id' => 1,
1822 );
1823 if ($context == 'payLater') {
1824 $compareParams = array(
1825 'amount' => 100,
1826 'status_id' => 3,
1827 'financial_account_id' => 1,
1828 );
1829 }
1830 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1831 if ($context == 'feeAmount') {
1832 $maxParams = array(
1833 'entity_id' => $params['id'],
1834 'entity_table' => 'civicrm_contribution',
1835 );
1836 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1837 $trxnParams = array(
1838 'id' => $maxTrxn['financial_trxn_id'],
1839 );
1840 $compareParams = array(
1841 'to_financial_account_id' => 5,
1842 'from_financial_account_id' => 6,
1843 'total_amount' => 50,
1844 'status_id' => 1,
1845 );
1846 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1847 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1848 $fitemParams = array(
1849 'entity_id' => $trxnId['financialTrxnId'],
1850 'entity_table' => 'civicrm_financial_trxn',
1851 );
1852 $compareParams = array(
1853 'amount' => 50,
1854 'status_id' => 1,
1855 'financial_account_id' => 5,
1856 );
1857 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1858 }
1859 }
1860 }
1861