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