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