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