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