Merge pull request #5047 from colemanw/CRM-15898
[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
266 /**
267 * Create an contribution_id=FALSE and financial_type_id=Donation.
268 */
269 public function testCreateEmptyContributionIDUseDonation() {
270 $params = array(
271 'contribution_id' => FALSE,
272 'contact_id' => 1,
273 'total_amount' => 1,
274 'check_permissions' => FALSE,
275 'financial_type_id' => 'Donation',
276 );
277 $this->callAPISuccess('contribution', 'create', $params);
278 }
279
280 /**
281 * Check with complete array + custom field
282 * Note that the test is written on purpose without any
283 * variables specific to participant so it can be replicated into other entities
284 * and / or moved to the automated test suite
285 */
286 public function testCreateWithCustom() {
287 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
288
289 $params = $this->_params;
290 $params['custom_' . $ids['custom_field_id']] = "custom string";
291
292 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
293 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
294 $check = $this->callAPISuccess($this->_entity, 'get', array(
295 'return.custom_' . $ids['custom_field_id'] => 1,
296 'id' => $result['id'],
297 ));
298 $this->customFieldDelete($ids['custom_field_id']);
299 $this->customGroupDelete($ids['custom_group_id']);
300 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
301 }
302
303 /**
304 * Check with complete array + custom field.
305 *
306 * Note that the test is written on purpose without any
307 * variables specific to participant so it can be replicated into other entities
308 * and / or moved to the automated test suite
309 */
310 public function testCreateGetFieldsWithCustom() {
311 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
312 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
313 $result = $this->callAPISuccess('Contribution', 'getfields', array());
314 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
315 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
316 $this->customFieldDelete($ids['custom_field_id']);
317 $this->customGroupDelete($ids['custom_group_id']);
318 $this->customFieldDelete($idsContact['custom_field_id']);
319 $this->customGroupDelete($idsContact['custom_group_id']);
320 }
321
322 public function testCreateContributionNoLineItems() {
323
324 $params = array(
325 'contact_id' => $this->_individualId,
326 'receive_date' => '20120511',
327 'total_amount' => 100.00,
328 'financial_type_id' => $this->_financialTypeId,
329 'payment_instrument_id' => 1,
330 'non_deductible_amount' => 10.00,
331 'fee_amount' => 50.00,
332 'net_amount' => 90.00,
333 'trxn_id' => 12345,
334 'invoice_id' => 67890,
335 'source' => 'SSF',
336 'contribution_status_id' => 1,
337 'skipLineItem' => 1,
338 );
339
340 $contribution = $this->callAPISuccess('contribution', 'create', $params);
341 $lineItems = $this->callAPISuccess('line_item', 'get', array(
342 'entity_id' => $contribution['id'],
343 'entity_table' => 'civicrm_contribution',
344 'sequential' => 1,
345 ));
346 $this->assertEquals(0, $lineItems['count']);
347 }
348
349 /**
350 * Test checks that passing in line items suppresses the create mechanism
351 */
352 public function testCreateContributionChainedLineItems() {
353 $params = array(
354 'contact_id' => $this->_individualId,
355 'receive_date' => '20120511',
356 'total_amount' => 100.00,
357 'financial_type_id' => $this->_financialTypeId,
358 'payment_instrument_id' => 1,
359 'non_deductible_amount' => 10.00,
360 'fee_amount' => 50.00,
361 'net_amount' => 90.00,
362 'trxn_id' => 12345,
363 'invoice_id' => 67890,
364 'source' => 'SSF',
365 'contribution_status_id' => 1,
366 'skipLineItem' => 1,
367 'api.line_item.create' => array(
368 array(
369 'price_field_id' => 1,
370 'qty' => 2,
371 'line_total' => '20',
372 'unit_price' => '10',
373 ),
374 array(
375 'price_field_id' => 1,
376 'qty' => 1,
377 'line_total' => '80',
378 'unit_price' => '80',
379 ),
380 ),
381 );
382
383 $description = "Create Contribution with Nested Line Items";
384 $subfile = "CreateWithNestedLineItems";
385 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
386
387 $lineItems = $this->callAPISuccess('line_item', 'get', array(
388 'entity_id' => $contribution['id'],
389 'contribution_id' => $contribution['id'],
390 'entity_table' => 'civicrm_contribution',
391 'sequential' => 1,
392 ));
393 $this->assertEquals(2, $lineItems['count']);
394 }
395
396 public function testCreateContributionOffline() {
397 $params = array(
398 'contact_id' => $this->_individualId,
399 'receive_date' => '20120511',
400 'total_amount' => 100.00,
401 'financial_type_id' => 1,
402 'trxn_id' => 12345,
403 'invoice_id' => 67890,
404 'source' => 'SSF',
405 'contribution_status_id' => 1,
406 );
407
408 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
409 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
410 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
411 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
412 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
413 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
414 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
415 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
416 $lineItems = $this->callAPISuccess('line_item', 'get', array(
417 'entity_id' => $contribution['id'],
418 'contribution_id' => $contribution['id'],
419 'entity_table' => 'civicrm_contribution',
420 'sequential' => 1,
421 ));
422 $this->assertEquals(1, $lineItems['count']);
423 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
424 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
425 $this->_checkFinancialRecords($contribution, 'offline');
426 $this->contributionGetnCheck($params, $contribution['id']);
427 }
428
429 /**
430 * Test create with valid payment instument
431 */
432 public function testCreateContributionWithPaymentInstrument() {
433 $params = $this->_params + array('payment_instrument' => 'EFT');
434 $contribution = $this->callAPISuccess('contribution', 'create', $params);
435 $contribution = $this->callAPISuccess('contribution', 'get', array(
436 'sequential' => 1,
437 'id' => $contribution['id'],
438 ));
439 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
440 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
441
442 $this->callAPISuccess('contribution', 'create', array(
443 'id' => $contribution['id'],
444 'payment_instrument' => 'Credit Card',
445 ));
446 $contribution = $this->callAPISuccess('contribution', 'get', array(
447 'sequential' => 1,
448 'id' => $contribution['id'],
449 ));
450 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
451 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
452 }
453
454 public function testGetContributionByPaymentInstrument() {
455 $params = $this->_params + array('payment_instrument' => 'EFT');
456 $params2 = $this->_params + array('payment_instrument' => 'Cash');
457 $this->callAPISuccess('contribution', 'create', $params);
458 $this->callAPISuccess('contribution', 'create', $params2);
459 $contribution = $this->callAPISuccess('contribution', 'get', array(
460 'sequential' => 1,
461 'contribution_payment_instrument' => 'Cash',
462 ));
463 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
464 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
465 $this->assertEquals(1, $contribution['count']);
466 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
467 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
468 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
469 $this->assertEquals(1, $contribution['count']);
470 $contribution = $this->callAPISuccess('contribution', 'get', array(
471 'sequential' => 1,
472 'payment_instrument_id' => 5,
473 ));
474 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
475 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
476 $this->assertEquals(1, $contribution['count']);
477 $contribution = $this->callAPISuccess('contribution', 'get', array(
478 'sequential' => 1,
479 'payment_instrument' => 'EFT',
480 ));
481 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
482 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
483 $this->assertEquals(1, $contribution['count']);
484 $contribution = $this->callAPISuccess('contribution', 'create', array(
485 'id' => $contribution['id'],
486 'payment_instrument' => 'Credit Card',
487 ));
488 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
489 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
490 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
491 $this->assertEquals(1, $contribution['count']);
492 }
493
494 /**
495 * Create test with unique field name on source
496 */
497 public function testCreateContributionSource() {
498
499 $params = array(
500 'contact_id' => $this->_individualId,
501 'receive_date' => date('Ymd'),
502 'total_amount' => 100.00,
503 'financial_type_id' => $this->_financialTypeId,
504 'payment_instrument_id' => 1,
505 'non_deductible_amount' => 10.00,
506 'fee_amount' => 50.00,
507 'net_amount' => 90.00,
508 'trxn_id' => 12345,
509 'invoice_id' => 67890,
510 'contribution_source' => 'SSF',
511 'contribution_status_id' => 1,
512 );
513
514 $contribution = $this->callAPISuccess('contribution', 'create', $params);
515 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
516 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
517 }
518
519 /**
520 * Create test with unique field name on source
521 */
522 public function testCreateDefaultNow() {
523
524 $params = $this->_params;
525 unset($params['receive_date']);
526
527 $contribution = $this->callAPISuccess('contribution', 'create', $params);
528 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
529 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
530 }
531
532 /**
533 * Create test with unique field name on source.
534 */
535 public function testCreateContributionSourceInvalidContact() {
536
537 $params = array(
538 'contact_id' => 999,
539 'receive_date' => date('Ymd'),
540 'total_amount' => 100.00,
541 'financial_type_id' => $this->_financialTypeId,
542 'payment_instrument_id' => 1,
543 'non_deductible_amount' => 10.00,
544 'fee_amount' => 50.00,
545 'net_amount' => 90.00,
546 'trxn_id' => 12345,
547 'invoice_id' => 67890,
548 'contribution_source' => 'SSF',
549 'contribution_status_id' => 1,
550 );
551
552 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
553 }
554
555 public function testCreateContributionSourceInvalidContContact() {
556
557 $params = array(
558 'contribution_contact_id' => 999,
559 'receive_date' => date('Ymd'),
560 'total_amount' => 100.00,
561 'financial_type_id' => $this->_financialTypeId,
562 'payment_instrument_id' => 1,
563 'non_deductible_amount' => 10.00,
564 'fee_amount' => 50.00,
565 'net_amount' => 90.00,
566 'trxn_id' => 12345,
567 'invoice_id' => 67890,
568 'contribution_source' => 'SSF',
569 'contribution_status_id' => 1,
570 );
571
572 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
573 }
574
575 /**
576 * Test note created correctly.
577 */
578 public function testCreateContributionWithNote() {
579 $description = "Demonstrates creating contribution with Note Entity";
580 $subfile = "ContributionCreateWithNote";
581 $params = array(
582 'contact_id' => $this->_individualId,
583 'receive_date' => '2012-01-01',
584 'total_amount' => 100.00,
585 'financial_type_id' => $this->_financialTypeId,
586 'payment_instrument_id' => 1,
587 'non_deductible_amount' => 10.00,
588 'fee_amount' => 50.00,
589 'net_amount' => 90.00,
590 'trxn_id' => 12345,
591 'invoice_id' => 67890,
592 'source' => 'SSF',
593 'contribution_status_id' => 1,
594 'note' => 'my contribution note',
595 );
596
597 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
598 $result = $this->callAPISuccess('note', 'get', array(
599 'entity_table' => 'civicrm_contribution',
600 'entity_id' => $contribution['id'],
601 'sequential' => 1,
602 ));
603 $this->assertEquals('my contribution note', $result['values'][0]['note']);
604 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
605 }
606
607 public function testCreateContributionWithNoteUniqueNameAliases() {
608 $params = array(
609 'contact_id' => $this->_individualId,
610 'receive_date' => '2012-01-01',
611 'total_amount' => 100.00,
612 'financial_type_id' => $this->_financialTypeId,
613 'payment_instrument_id' => 1,
614 'non_deductible_amount' => 10.00,
615 'fee_amount' => 50.00,
616 'net_amount' => 90.00,
617 'trxn_id' => 12345,
618 'invoice_id' => 67890,
619 'source' => 'SSF',
620 'contribution_status_id' => 1,
621 'contribution_note' => 'my contribution note',
622 );
623
624 $contribution = $this->callAPISuccess('contribution', 'create', $params);
625 $result = $this->callAPISuccess('note', 'get', array(
626 'entity_table' => 'civicrm_contribution',
627 'entity_id' => $contribution['id'],
628 'sequential' => 1,
629 ));
630 $this->assertEquals('my contribution note', $result['values'][0]['note']);
631 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
632 }
633
634 /**
635 * This is the test for creating soft credits.
636 */
637 public function testCreateContributionWithSoftCredit() {
638 $description = "Demonstrates creating contribution with SoftCredit";
639 $subfile = "ContributionCreateWithSoftCredit";
640 $contact2 = $this->callAPISuccess('Contact', 'create', array(
641 'display_name' => 'superman',
642 'contact_type' => 'Individual',
643 ));
644 $softParams = array(
645 'contact_id' => $contact2['id'],
646 'amount' => 50,
647 'soft_credit_type_id' => 3,
648 );
649
650 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
651 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
652 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
653
654 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
655 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
656 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
657
658 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
659 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
660 }
661
662 public function testCreateContributionWithSoftCreditDefaults() {
663 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
664 $subfile = "ContributionCreateWithSoftCreditDefaults";
665 $contact2 = $this->callAPISuccess('Contact', 'create', array(
666 'display_name' => 'superman',
667 'contact_type' => 'Individual',
668 ));
669 $params = $this->_params + array(
670 'soft_credit_to' => $contact2['id'],
671 );
672 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
673 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
674
675 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
676 // Default soft credit amount = contribution.total_amount
677 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
678 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
679
680 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
681 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
682 }
683
684 public function testCreateContributionWithHonoreeContact() {
685 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
686 $subfile = "ContributionCreateWithHonoreeContact";
687 $contact2 = $this->callAPISuccess('Contact', 'create', array(
688 'display_name' => 'superman',
689 'contact_type' => 'Individual',
690 ));
691 $params = $this->_params + array(
692 'honor_contact_id' => $contact2['id'],
693 );
694 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
695 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
696
697 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
698 // Default soft credit amount = contribution.total_amount
699 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
700 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
701 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
702
703 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
704 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
705 }
706
707 /**
708 * Test using example code.
709 */
710 public function testContributionCreateExample() {
711 //make sure at least on page exists since there is a truncate in tear down
712 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
713 require_once 'api/v3/examples/Contribution/Create.php';
714 $result = contribution_create_example();
715 $id = $result['id'];
716 $expectedResult = contribution_create_expectedresult();
717 $this->checkArrayEquals($expectedResult, $result);
718 $this->contributionDelete($id);
719 }
720
721 /**
722 * Function tests that additional financial records are created when fee amount is recorded.
723 */
724 public function testCreateContributionWithFee() {
725 $params = array(
726 'contact_id' => $this->_individualId,
727 'receive_date' => '20120511',
728 'total_amount' => 100.00,
729 'fee_amount' => 50,
730 'financial_type_id' => 1,
731 'trxn_id' => 12345,
732 'invoice_id' => 67890,
733 'source' => 'SSF',
734 'contribution_status_id' => 1,
735 );
736
737 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
738 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
739 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
740 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
741 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
742 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
743 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
744 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
745 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
746 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
747
748 $lineItems = $this->callAPISuccess('line_item', 'get', array(
749
750 'entity_id' => $contribution['id'],
751 'entity_table' => 'civicrm_contribution',
752 'sequential' => 1,
753 ));
754 $this->assertEquals(1, $lineItems['count']);
755 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
756 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
757 $lineItems = $this->callAPISuccess('line_item', 'get', array(
758
759 'entity_id' => $contribution['id'],
760 'contribution_id' => $contribution['id'],
761 'entity_table' => 'civicrm_contribution',
762 'sequential' => 1,
763 ));
764 $this->assertEquals(1, $lineItems['count']);
765 $this->_checkFinancialRecords($contribution, 'feeAmount');
766 }
767
768
769 /**
770 * Function tests that additional financial records are created when online contribution is created.
771 */
772 public function testCreateContributionOnline() {
773 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
774 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
775 $this->assertAPISuccess($contributionPage);
776 $params = array(
777 'contact_id' => $this->_individualId,
778 'receive_date' => '20120511',
779 'total_amount' => 100.00,
780 'financial_type_id' => 1,
781 'contribution_page_id' => $contributionPage['id'],
782 'payment_processor' => 1,
783 'trxn_id' => 12345,
784 'invoice_id' => 67890,
785 'source' => 'SSF',
786 'contribution_status_id' => 1,
787
788 );
789
790 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
791 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
792 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
793 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
794 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
795 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
796 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
797 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
798 $this->_checkFinancialRecords($contribution, 'online');
799 }
800
801 /**
802 * Check handling of financial type.
803 *
804 * In the interests of removing financial type / contribution type checks from
805 * legacy format function lets test that the api is doing this for us
806 */
807 public function testCreateInvalidFinancialType() {
808 $params = $this->_params;
809 $params['financial_type_id'] = 99999;
810 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
811 }
812
813 /**
814 * Check handling of financial type.
815 *
816 * In the interests of removing financial type / contribution type checks from
817 * legacy format function lets test that the api is doing this for us
818 */
819 public function testValidNamedFinancialType() {
820 $params = $this->_params;
821 $params['financial_type_id'] = 'Donation';
822 $this->callAPISuccess($this->_entity, 'create', $params);
823 }
824
825 /**
826 * Tests that additional financial records are created.
827 *
828 * Checks when online contribution with pay later option 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
1106 /**
1107 * To Update Contribution
1108 * CHANGE: we require the API to do an incremental update
1109 */
1110 public function testCreateUpdateContribution() {
1111
1112 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
1113 $old_params = array(
1114 'contribution_id' => $contributionID,
1115
1116 );
1117 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1118 //Make sure it came back
1119 $this->assertAPISuccess($original);
1120 $this->assertEquals($original['id'], $contributionID);
1121 //set up list of old params, verify
1122
1123 //This should not be required on update:
1124 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1125 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1126 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1127 $old_source = $original['values'][$contributionID]['contribution_source'];
1128
1129 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1130 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1131
1132 //check against values in CiviUnitTestCase::createContribution()
1133 $this->assertEquals($old_contact_id, $this->_individualId);
1134 $this->assertEquals($old_fee_amount, 5.00);
1135 $this->assertEquals($old_source, 'SSF');
1136 $this->assertEquals($old_trxn_id, 212355);
1137 $this->assertEquals($old_invoice_id, 'idofsh');
1138 $params = array(
1139 'id' => $contributionID,
1140 'contact_id' => $this->_individualId,
1141 'total_amount' => 110.00,
1142 'financial_type_id' => $this->_financialTypeId,
1143 'non_deductible_amount' => 10.00,
1144 'net_amount' => 100.00,
1145 'contribution_status_id' => 1,
1146 'note' => 'Donating for Nobel Cause',
1147
1148 );
1149
1150 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1151
1152 $new_params = array(
1153 'contribution_id' => $contribution['id'],
1154
1155 );
1156 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1157
1158 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1159 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1160 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
1161 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1162 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1163 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1164 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1165 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1166 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1167 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1168 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1169 $params = array(
1170 'contribution_id' => $contributionID,
1171
1172 );
1173 $result = $this->callAPISuccess('contribution', 'delete', $params);
1174 $this->assertAPISuccess($result);
1175 }
1176
1177 ///////////////// civicrm_contribution_delete methods
1178
1179 /**
1180 * Attempt (but fail) to delete a contribution without parameters.
1181 */
1182 public function testDeleteEmptyParamsContribution() {
1183 $params = array();
1184 $this->callAPIFailure('contribution', 'delete', $params);
1185 }
1186
1187 public function testDeleteParamsNotArrayContribution() {
1188 $params = 'contribution_id= 1';
1189 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1190 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1191 }
1192
1193 public function testDeleteWrongParamContribution() {
1194 $params = array(
1195 'contribution_source' => 'SSF',
1196
1197 );
1198 $this->callAPIFailure('contribution', 'delete', $params);
1199 }
1200
1201 public function testDeleteContribution() {
1202
1203 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
1204 $params = array(
1205 'id' => $contributionID,
1206 );
1207 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1208 }
1209
1210 /**
1211 * Test civicrm_contribution_search with empty params.
1212 * All available contributions expected.
1213 */
1214 public function testSearchEmptyParams() {
1215 $params = array();
1216
1217 $p = array(
1218 'contact_id' => $this->_individualId,
1219 'receive_date' => date('Ymd'),
1220 'total_amount' => 100.00,
1221 'financial_type_id' => $this->_financialTypeId,
1222 'non_deductible_amount' => 10.00,
1223 'fee_amount' => 5.00,
1224 'net_amount' => 95.00,
1225 'trxn_id' => 23456,
1226 'invoice_id' => 78910,
1227 'source' => 'SSF',
1228 'contribution_status_id' => 1,
1229
1230 );
1231 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1232
1233 $result = $this->callAPISuccess('contribution', 'get', $params);
1234 // We're taking the first element.
1235 $res = $result['values'][$contribution['id']];
1236
1237 $this->assertEquals($p['contact_id'], $res['contact_id']);
1238 $this->assertEquals($p['total_amount'], $res['total_amount']);
1239 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1240 $this->assertEquals($p['net_amount'], $res['net_amount']);
1241 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1242 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1243 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1244 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1245 $this->assertEquals($p['source'], $res['contribution_source']);
1246 // contribution_status_id = 1 => Completed
1247 $this->assertEquals('Completed', $res['contribution_status']);
1248
1249 $this->contributionDelete($contribution['id']);
1250 }
1251
1252 /**
1253 * Test civicrm_contribution_search. Success expected.
1254 */
1255 public function testSearch() {
1256 $p1 = array(
1257 'contact_id' => $this->_individualId,
1258 'receive_date' => date('Ymd'),
1259 'total_amount' => 100.00,
1260 'financial_type_id' => $this->_financialTypeId,
1261 'non_deductible_amount' => 10.00,
1262 'contribution_status_id' => 1,
1263
1264 );
1265 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1266
1267 $p2 = array(
1268 'contact_id' => $this->_individualId,
1269 'receive_date' => date('Ymd'),
1270 'total_amount' => 200.00,
1271 'financial_type_id' => $this->_financialTypeId,
1272 'non_deductible_amount' => 20.00,
1273 'trxn_id' => 5454565,
1274 'invoice_id' => 1212124,
1275 'fee_amount' => 50.00,
1276 'net_amount' => 60.00,
1277 'contribution_status_id' => 2,
1278
1279 );
1280 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1281
1282 $params = array(
1283 'contribution_id' => $contribution2['id'],
1284
1285 );
1286 $result = $this->callAPISuccess('contribution', 'get', $params);
1287 $res = $result['values'][$contribution2['id']];
1288
1289 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1290 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1291 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1292 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1293 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1294 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1295 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1296 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1297 // contribution_status_id = 2 => Pending
1298 $this->assertEquals('Pending', $res['contribution_status']);
1299
1300 $this->contributionDelete($contribution1['id']);
1301 $this->contributionDelete($contribution2['id']);
1302 }
1303
1304 /**
1305 * Test completing a transaction via the API
1306 *
1307 * Note that we are creating a logged in user because email goes out from
1308 * that person
1309 */
1310 public function testCompleteTransaction() {
1311 $mut = new CiviMailUtils($this, TRUE);
1312 $this->createLoggedInUser();
1313 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1314 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1315 $this->callAPISuccess('contribution', 'completetransaction', array(
1316 'id' => $contribution['id'],
1317 ));
1318 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1319 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1320 $mut->checkMailLog(array(
1321 'Receipt - Contribution',
1322 'Please print this confirmation for your records.',
1323 ));
1324 $mut->stop();
1325 }
1326
1327 /**
1328 * CRM-14151
1329 * Test completing a transaction via the API
1330 *
1331 * tests.
1332 */
1333 public function testCompleteTransactionWithReceiptDateSet() {
1334 $mut = new CiviMailUtils($this, TRUE);
1335 $this->createLoggedInUser();
1336 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1337 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1338 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1339 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1340 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1341 $mut->checkMailLog(array(
1342 'Receipt - Contribution',
1343 'Please print this confirmation for your records.',
1344 ));
1345 $mut->stop();
1346 }
1347
1348 /**
1349 * Test completing a transaction with an event via the API
1350 *
1351 * Note that we are creating a logged in user because email goes out from
1352 * that person
1353 */
1354 public function testCompleteTransactionWithParticipantRecord() {
1355 $mut = new CiviMailUtils($this, TRUE);
1356 $mut->clearMessages();
1357 $this->createLoggedInUser();
1358 $contributionID = $this->createPendingParticipantContribution();
1359 $this->callAPISuccess('contribution', 'completetransaction', array(
1360 'id' => $contributionID,
1361 )
1362 );
1363 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1364 'id' => $this->_ids['participant'],
1365 'return' => 'participant_status_id',
1366 ));
1367 $this->assertEquals(1, $participantStatus);
1368 $mut->checkMailLog(array(
1369 'Annual CiviCRM meet',
1370 'Event',
1371 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1372 ));
1373 $mut->stop();
1374 }
1375
1376 /**
1377 * Test membership is renewed when transaction completed
1378 */
1379 public function testCompleteTransactionMembershipPriceSet() {
1380 $this->createPriceSetWithPage('membership');
1381 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
1382 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1383 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1384 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1385 $this->cleanUpAfterPriceSets();
1386 }
1387
1388 /**
1389 * Test membership is renewed when transaction completed
1390 */
1391 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1392 $this->createPriceSetWithPage('membership');
1393 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
1394 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1395 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1396 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1397 $this->cleanUpAfterPriceSets();
1398 }
1399
1400 public function cleanUpAfterPriceSets() {
1401 $this->quickCleanUpFinancialEntities();
1402 $this->contactDelete($this->_ids['contact']);
1403 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set']));
1404 }
1405
1406
1407 /**
1408 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1409 * on parent class at some point (fn is not in 4.4)
1410 * @param $entity
1411 * @param array $params
1412 */
1413 public function createPriceSetWithPage($entity, $params = array()) {
1414 $membershipTypeID = $this->membershipTypeCreate();
1415 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1416 'title' => "Test Contribution Page",
1417 'financial_type_id' => 1,
1418 'currency' => 'NZD',
1419 'goal_amount' => 50,
1420 'is_pay_later' => 1,
1421 'is_monetary' => TRUE,
1422 'is_email_receipt' => FALSE,
1423 ));
1424 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1425 'is_quick_config' => 0,
1426 'extends' => 'CiviMember',
1427 'financial_type_id' => 1,
1428 'title' => 'my Page',
1429 ));
1430 $priceSetID = $priceSet['id'];
1431
1432 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1433 $priceField = $this->callAPISuccess('price_field', 'create', array(
1434 'price_set_id' => $priceSetID,
1435 'label' => 'Goat Breed',
1436 'html_type' => 'Radio',
1437 ));
1438 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1439 'price_set_id' => $priceSetID,
1440 'price_field_id' => $priceField['id'],
1441 'label' => 'Long Haired Goat',
1442 'amount' => 20,
1443 'membership_type_id' => $membershipTypeID,
1444 'membership_num_terms' => 1,
1445 )
1446 );
1447 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1448 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1449 'price_set_id' => $priceSetID,
1450 'price_field_id' => $priceField['id'],
1451 'label' => 'Shoe-eating Goat',
1452 'amount' => 10,
1453 'membership_type_id' => $membershipTypeID,
1454 'membership_num_terms' => 2,
1455 )
1456 );
1457 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
1458 $this->_ids['price_set'] = $priceSetID;
1459 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1460 $this->_ids['price_field'] = array($priceField['id']);
1461
1462 $this->_ids['membership_type'] = $membershipTypeID;
1463 }
1464
1465 /**
1466 * Set up a pending transaction with a specific price field id
1467 * @param int $priceFieldValueID
1468 */
1469 public function setUpPendingContribution($priceFieldValueID) {
1470 $contactID = $this->individualCreate();
1471 $membership = $this->callAPISuccess('membership', 'create', array(
1472 'contact_id' => $contactID,
1473 'membership_type_id' => $this->_ids['membership_type'],
1474 'start_date' => 'yesterday - 1 year',
1475 'end_date' => 'yesterday',
1476 ));
1477 $contribution = $this->callAPISuccess('contribution', 'create', array(
1478 'domain_id' => 1,
1479 'contact_id' => $contactID,
1480 'receive_date' => date('Ymd'),
1481 'total_amount' => 100.00,
1482 'financial_type_id' => 1,
1483 'payment_instrument_id' => 'Credit Card',
1484 'non_deductible_amount' => 10.00,
1485 'trxn_id' => 'jdhfi88',
1486 'invoice_id' => 'djfhiewuyr',
1487 'source' => 'SSF',
1488 'contribution_status_id' => 2,
1489 'contribution_page_id' => $this->_ids['contribution_page'],
1490 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1491 ));
1492
1493 $this->callAPISuccess('line_item', 'create', array(
1494 'entity_id' => $contribution['id'],
1495 'entity_table' => 'civicrm_contribution',
1496 'contribution_id' => $contribution['id'],
1497 'price_field_id' => $this->_ids['price_field'][0],
1498 'qty' => 1,
1499 'unit_price' => 20,
1500 'line_total' => 20,
1501 'financial_type_id' => 1,
1502 'price_field_value_id' => $priceFieldValueID,
1503 ));
1504 $this->_ids['contact'] = $contactID;
1505 $this->_ids['contribution'] = $contribution['id'];
1506 $this->_ids['membership'] = $membership['id'];
1507 }
1508
1509 /**
1510 * Test sending a mail via the API
1511 */
1512 public function testSendMail() {
1513 $mut = new CiviMailUtils($this, TRUE);
1514 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1515 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1516 'id' => $contribution['id'],
1517 'receipt_from_email' => 'api@civicrm.org',
1518 )
1519 );
1520 $mut->checkMailLog(array(
1521 '$ 100.00',
1522 'Contribution Information',
1523 'Please print this confirmation for your records',
1524 ), array(
1525 'Event',
1526 )
1527 );
1528 $mut->stop();
1529 }
1530
1531 /**
1532 * Test sending a mail via the API
1533 */
1534 public function testSendMailEvent() {
1535 $mut = new CiviMailUtils($this, TRUE);
1536 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1537 $event = $this->eventCreate(array(
1538 'is_email_confirm' => 1,
1539 'confirm_from_email' => 'test@civicrm.org',
1540 ));
1541 $this->_eventID = $event['id'];
1542 $participantParams = array(
1543 'contact_id' => $this->_individualId,
1544 'event_id' => $this->_eventID,
1545 'status_id' => 1,
1546 'role_id' => 1,
1547 // to ensure it matches later on
1548 'register_date' => '2007-07-21 00:00:00',
1549 'source' => 'Online Event Registration: API Testing',
1550
1551 );
1552 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1553 $this->callAPISuccess('participant_payment', 'create', array(
1554 'participant_id' => $participant['id'],
1555 'contribution_id' => $contribution['id'],
1556 ));
1557 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1558 'id' => $contribution['id'],
1559 'receipt_from_email' => 'api@civicrm.org',
1560 )
1561 );
1562
1563 $mut->checkMailLog(array(
1564 'Annual CiviCRM meet',
1565 'Event',
1566 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1567 ), array()
1568 );
1569 $mut->stop();
1570 }
1571
1572 /**
1573 * This function does a GET & compares the result against the $params
1574 * Use as a double check on Creates
1575 * @param $params
1576 * @param $id
1577 * @param int $delete
1578 */
1579 public function contributionGetnCheck($params, $id, $delete = 1) {
1580
1581 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1582 'id' => $id,
1583
1584 ));
1585
1586 if ($delete) {
1587 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1588 }
1589 $this->assertAPISuccess($contribution, 0);
1590 $values = $contribution['values'][$contribution['id']];
1591 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1592 // this is not returned in id format
1593 unset($params['payment_instrument_id']);
1594 $params['contribution_source'] = $params['source'];
1595 unset($params['source']);
1596 foreach ($params as $key => $value) {
1597 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1598 }
1599 }
1600
1601 /**
1602 * Create a pending contribution & linked pending participant record
1603 * (along with an event)
1604 */
1605 public function createPendingParticipantContribution() {
1606 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1607 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1608 $this->_ids['participant'] = $participantID;
1609 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1610 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1611 $this->callAPISuccess('participant_payment', 'create', array(
1612 'contribution_id' => $contribution['id'],
1613 'participant_id' => $participantID,
1614 ));
1615 $this->callAPISuccess('line_item', 'get', array(
1616 'entity_id' => $contribution['id'],
1617 'entity_table' => 'civicrm_contribution',
1618 'api.line_item.create' => array(
1619 'entity_id' => $participantID,
1620 'entity_table' => 'civicrm_participant',
1621 ),
1622 ));
1623 return $contribution['id'];
1624 }
1625
1626 /**
1627 * @param int $contId
1628 *
1629 * @return null|string
1630 */
1631 public function _getFinancialTrxnAmount($contId) {
1632 $query = "SELECT
1633 SUM( ft.total_amount ) AS total
1634 FROM civicrm_financial_trxn AS ft
1635 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1636 WHERE ceft.entity_table = 'civicrm_contribution'
1637 AND ceft.entity_id = {$contId}";
1638
1639 $result = CRM_Core_DAO::singleValueQuery($query);
1640 return $result;
1641 }
1642
1643 /**
1644 * @param int $contId
1645 *
1646 * @return null|string
1647 */
1648 public function _getFinancialItemAmount($contId) {
1649 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1650 $query = "SELECT
1651 SUM(amount)
1652 FROM civicrm_financial_item
1653 WHERE entity_table = 'civicrm_line_item'
1654 AND entity_id = {$lineItem}";
1655 $result = CRM_Core_DAO::singleValueQuery($query);
1656 return $result;
1657 }
1658
1659 /**
1660 * @param int $contId
1661 * @param $context
1662 */
1663 public function _checkFinancialItem($contId, $context) {
1664 if ($context != 'paylater') {
1665 $params = array(
1666 'entity_id' => $contId,
1667 'entity_table' => 'civicrm_contribution',
1668 );
1669 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1670 $entityParams = array(
1671 'financial_trxn_id' => $trxn['financial_trxn_id'],
1672 'entity_table' => 'civicrm_financial_item',
1673 );
1674 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1675 $params = array(
1676 'id' => $entityTrxn['entity_id'],
1677 );
1678 }
1679 if ($context == 'paylater') {
1680 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1681 foreach ($lineItems as $key => $item) {
1682 $params = array(
1683 'entity_id' => $key,
1684 'entity_table' => 'civicrm_line_item',
1685 );
1686 $compareParams = array('status_id' => 1);
1687 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1688 }
1689 }
1690 elseif ($context == 'refund') {
1691 $compareParams = array(
1692 'status_id' => 1,
1693 'financial_account_id' => 1,
1694 'amount' => -100,
1695 );
1696 }
1697 elseif ($context == 'cancelPending') {
1698 $compareParams = array(
1699 'status_id' => 3,
1700 'financial_account_id' => 1,
1701 'amount' => -100,
1702 );
1703 }
1704 elseif ($context == 'changeFinancial') {
1705 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1706 $params = array(
1707 'entity_id' => $lineKey,
1708 'amount' => -100,
1709 );
1710 $compareParams = array(
1711 'financial_account_id' => 1,
1712 );
1713 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1714 $params = array(
1715 'financial_account_id' => 3,
1716 'entity_id' => $lineKey,
1717 );
1718 $compareParams = array(
1719 'amount' => 100,
1720 );
1721 }
1722 if ($context != 'paylater') {
1723 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1724 }
1725 }
1726
1727 /**
1728 * @param $contribution
1729 * @param $context
1730 * @param int $instrumentId
1731 */
1732 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1733 $trxnParams = array(
1734 'entity_id' => $contribution['id'],
1735 'entity_table' => 'civicrm_contribution',
1736 );
1737 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1738 $params = array(
1739 'id' => $trxn['financial_trxn_id'],
1740 );
1741 if ($context == 'payLater') {
1742 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1743 $compareParams = array(
1744 'status_id' => 1,
1745 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1746 );
1747 }
1748 elseif ($context == 'refund') {
1749 $compareParams = array(
1750 'to_financial_account_id' => 6,
1751 'total_amount' => -100,
1752 'status_id' => 7,
1753 );
1754 }
1755 elseif ($context == 'cancelPending') {
1756 $compareParams = array(
1757 'from_financial_account_id' => 7,
1758 'total_amount' => -100,
1759 'status_id' => 3,
1760 );
1761 }
1762 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
1763 $entityParams = array(
1764 'entity_id' => $contribution['id'],
1765 'entity_table' => 'civicrm_contribution',
1766 'amount' => -100,
1767 );
1768 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1769 $trxnParams1 = array(
1770 'id' => $trxn['financial_trxn_id'],
1771 );
1772 $compareParams = array(
1773 'total_amount' => -100,
1774 'status_id' => 1,
1775 );
1776 if ($context == 'paymentInstrument') {
1777 $compareParams += array(
1778 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1779 'payment_instrument_id' => 4,
1780 );
1781 }
1782 else {
1783 $compareParams['to_financial_account_id'] = 12;
1784 }
1785 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1786 $compareParams['total_amount'] = 100;
1787 if ($context == 'paymentInstrument') {
1788 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1789 $compareParams['payment_instrument_id'] = $instrumentId;
1790 }
1791 else {
1792 $compareParams['to_financial_account_id'] = 12;
1793 }
1794 }
1795
1796 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1797 }
1798
1799 /**
1800 * @return mixed
1801 */
1802 public function _addPaymentInstrument() {
1803 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1804 $optionParams = array(
1805 'option_group_id' => $gId,
1806 'label' => 'Test Card',
1807 'name' => 'Test Card',
1808 'value' => '6',
1809 'weight' => '6',
1810 'is_active' => 1,
1811 );
1812 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1813 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1814 $financialParams = array(
1815 'entity_table' => 'civicrm_option_value',
1816 'entity_id' => $optionValue['id'],
1817 'account_relationship' => $relationTypeId,
1818 'financial_account_id' => 7,
1819 );
1820 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1821 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1822 return $optionValue['values'][$optionValue['id']]['value'];
1823 }
1824
1825 /**
1826 * @param array $params
1827 * @param $context
1828 */
1829 public function _checkFinancialRecords($params, $context) {
1830 $entityParams = array(
1831 'entity_id' => $params['id'],
1832 'entity_table' => 'civicrm_contribution',
1833 );
1834 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1835 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1836 if ($context == 'pending') {
1837 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1838 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1839 return;
1840 }
1841 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1842 $trxnParams = array(
1843 'id' => $trxn['financial_trxn_id'],
1844 );
1845 if ($context != 'online' && $context != 'payLater') {
1846 $compareParams = array(
1847 'to_financial_account_id' => 6,
1848 'total_amount' => 100,
1849 'status_id' => 1,
1850 );
1851 }
1852 if ($context == 'feeAmount') {
1853 $compareParams['fee_amount'] = 50;
1854 }
1855 elseif ($context == 'online') {
1856 $compareParams = array(
1857 'to_financial_account_id' => 12,
1858 'total_amount' => 100,
1859 'status_id' => 1,
1860 );
1861 }
1862 elseif ($context == 'payLater') {
1863 $compareParams = array(
1864 'to_financial_account_id' => 7,
1865 'total_amount' => 100,
1866 'status_id' => 2,
1867 );
1868 }
1869 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1870 $entityParams = array(
1871 'financial_trxn_id' => $trxn['financial_trxn_id'],
1872 'entity_table' => 'civicrm_financial_item',
1873 );
1874 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1875 $fitemParams = array(
1876 'id' => $entityTrxn['entity_id'],
1877 );
1878 $compareParams = array(
1879 'amount' => 100,
1880 'status_id' => 1,
1881 'financial_account_id' => 1,
1882 );
1883 if ($context == 'payLater') {
1884 $compareParams = array(
1885 'amount' => 100,
1886 'status_id' => 3,
1887 'financial_account_id' => 1,
1888 );
1889 }
1890 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1891 if ($context == 'feeAmount') {
1892 $maxParams = array(
1893 'entity_id' => $params['id'],
1894 'entity_table' => 'civicrm_contribution',
1895 );
1896 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1897 $trxnParams = array(
1898 'id' => $maxTrxn['financial_trxn_id'],
1899 );
1900 $compareParams = array(
1901 'to_financial_account_id' => 5,
1902 'from_financial_account_id' => 6,
1903 'total_amount' => 50,
1904 'status_id' => 1,
1905 );
1906 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1907 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1908 $fitemParams = array(
1909 'entity_id' => $trxnId['financialTrxnId'],
1910 'entity_table' => 'civicrm_financial_trxn',
1911 );
1912 $compareParams = array(
1913 'amount' => 50,
1914 'status_id' => 1,
1915 'financial_account_id' => 5,
1916 );
1917 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1918 }
1919 }
1920
1921 }