Merge pull request #4949 from eileenmcnaughton/minor-tidies
[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 * Tests that additional financial records are created.
826 *
827 * Checks when online contribution with pay later option is created
828 */
829 public function testCreateContributionPayLaterOnline() {
830 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
831 $this->_pageParams['is_pay_later'] = 1;
832 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
833 $this->assertAPISuccess($contributionPage);
834 $params = array(
835 'contact_id' => $this->_individualId,
836 'receive_date' => '20120511',
837 'total_amount' => 100.00,
838 'financial_type_id' => 1,
839 'contribution_page_id' => $contributionPage['id'],
840 'trxn_id' => 12345,
841 'is_pay_later' => 1,
842 'invoice_id' => 67890,
843 'source' => 'SSF',
844 'contribution_status_id' => 2,
845
846 );
847
848 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
849 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
850 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
851 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
852 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
853 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
854 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
855 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
856 $this->_checkFinancialRecords($contribution, 'payLater');
857 }
858
859 /*
860 * Function tests that additional financial records are created when online contribution with pending option
861 * is created
862 */
863 public function testCreateContributionPendingOnline() {
864 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
865 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
866 $this->assertAPISuccess($contributionPage);
867 $params = array(
868 'contact_id' => $this->_individualId,
869 'receive_date' => '20120511',
870 'total_amount' => 100.00,
871 'financial_type_id' => 1,
872 'contribution_page_id' => $contributionPage['id'],
873 'trxn_id' => 12345,
874 'invoice_id' => 67890,
875 'source' => 'SSF',
876 'contribution_status_id' => 2,
877 );
878
879 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
880 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
881 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
882 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
883 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
884 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
885 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
886 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
887 $this->_checkFinancialRecords($contribution, 'pending');
888 }
889
890 /**
891 * Test that BAO defaults work.
892 */
893 public function testCreateBAODefaults() {
894 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
895 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
896 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
897 'id' => $contribution['id'],
898 'api.contribution.delete' => 1,
899 ));
900 $this->assertEquals(1, $contribution['contribution_status_id']);
901 $this->assertEquals('Check', $contribution['payment_instrument']);
902 }
903
904 /*
905 * Function tests that line items, financial records are updated when contribution amount is changed
906 */
907 public function testCreateUpdateContributionChangeTotal() {
908 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
909 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
910
911 'entity_id' => $contribution['id'],
912 'entity_table' => 'civicrm_contribution',
913 'sequential' => 1,
914 'return' => 'line_total',
915 ));
916 $this->assertEquals('100.00', $lineItems);
917 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
918 // Financial trxn SUM = 100 + 5 (fee)
919 $this->assertEquals('105.00', $trxnAmount);
920 $newParams = array(
921
922 'id' => $contribution['id'],
923 'total_amount' => '125',
924 );
925 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
926
927 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
928
929 'entity_id' => $contribution['id'],
930 'entity_table' => 'civicrm_contribution',
931 'sequential' => 1,
932 'return' => 'line_total',
933 ));
934
935 $this->assertEquals('125.00', $lineItems);
936 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
937 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
938 // Financial trxn SUM = 125 + 5 (fee)
939 $this->assertEquals('130.00', $trxnAmount);
940 $this->assertEquals('125.00', $fitemAmount);
941 }
942
943 /*
944 * Function tests that line items, financial records are updated when pay later contribution is received
945 */
946 public function testCreateUpdateContributionPayLater() {
947 $contribParams = array(
948 'contact_id' => $this->_individualId,
949 'receive_date' => '2012-01-01',
950 'total_amount' => 100.00,
951 'financial_type_id' => $this->_financialTypeId,
952 'payment_instrument_id' => 1,
953 'contribution_status_id' => 2,
954 'is_pay_later' => 1,
955
956 );
957 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
958
959 $newParams = array_merge($contribParams, array(
960 'id' => $contribution['id'],
961 'contribution_status_id' => 1,
962 )
963 );
964 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
965 $contribution = $contribution['values'][$contribution['id']];
966 $this->assertEquals($contribution['contribution_status_id'], '1');
967 $this->_checkFinancialItem($contribution['id'], 'paylater');
968 $this->_checkFinancialTrxn($contribution, 'payLater');
969 }
970
971 /*
972 * Function tests that financial records are updated when Payment Instrument is changed
973 */
974 public function testCreateUpdateContributionPaymentInstrument() {
975 $instrumentId = $this->_addPaymentInstrument();
976 $contribParams = array(
977 'contact_id' => $this->_individualId,
978 'total_amount' => 100.00,
979 'financial_type_id' => $this->_financialTypeId,
980 'payment_instrument_id' => 4,
981 'contribution_status_id' => 1,
982
983 );
984 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
985
986 $newParams = array_merge($contribParams, array(
987 'id' => $contribution['id'],
988 'payment_instrument_id' => $instrumentId,
989 )
990 );
991 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
992 $this->assertAPISuccess($contribution);
993 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
994 }
995
996 /*
997 * Function tests that financial records are added when Contribution is Refunded
998 */
999 public function testCreateUpdateContributionRefund() {
1000 $contribParams = array(
1001 'contact_id' => $this->_individualId,
1002 'receive_date' => '2012-01-01',
1003 'total_amount' => 100.00,
1004 'financial_type_id' => $this->_financialTypeId,
1005 'payment_instrument_id' => 4,
1006 'contribution_status_id' => 1,
1007
1008 );
1009 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1010 $newParams = array_merge($contribParams, array(
1011 'id' => $contribution['id'],
1012 'contribution_status_id' => 7,
1013 )
1014 );
1015
1016 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1017 $this->_checkFinancialTrxn($contribution, 'refund');
1018 $this->_checkFinancialItem($contribution['id'], 'refund');
1019 }
1020
1021 /*
1022 * Function tests invalid contribution status change
1023 */
1024 public function testCreateUpdateContributionInValidStatusChange() {
1025 $contribParams = array(
1026 'contact_id' => 1,
1027 'receive_date' => '2012-01-01',
1028 'total_amount' => 100.00,
1029 'financial_type_id' => 1,
1030 'payment_instrument_id' => 1,
1031 'contribution_status_id' => 1,
1032 );
1033 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1034 $newParams = array_merge($contribParams, array(
1035 'id' => $contribution['id'],
1036 'contribution_status_id' => 2,
1037 )
1038 );
1039 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1040
1041 }
1042
1043 /*
1044 * Function tests that financial records are added when Pending Contribution is Canceled
1045 */
1046 public function testCreateUpdateContributionCancelPending() {
1047 $contribParams = array(
1048 'contact_id' => $this->_individualId,
1049 'receive_date' => '2012-01-01',
1050 'total_amount' => 100.00,
1051 'financial_type_id' => $this->_financialTypeId,
1052 'payment_instrument_id' => 1,
1053 'contribution_status_id' => 2,
1054 'is_pay_later' => 1,
1055
1056 );
1057 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1058 $newParams = array_merge($contribParams, array(
1059 'id' => $contribution['id'],
1060 'contribution_status_id' => 3,
1061 )
1062 );
1063 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1064 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1065 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1066 }
1067
1068 /*
1069 * Function tests that financial records are added when Financial Type is Changed
1070 */
1071 public function testCreateUpdateContributionChangeFinancialType() {
1072 $contribParams = array(
1073 'contact_id' => $this->_individualId,
1074 'receive_date' => '2012-01-01',
1075 'total_amount' => 100.00,
1076 'financial_type_id' => 1,
1077 'payment_instrument_id' => 1,
1078 'contribution_status_id' => 1,
1079
1080 );
1081 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1082 $newParams = array_merge($contribParams, array(
1083 'id' => $contribution['id'],
1084 'financial_type_id' => 3,
1085 )
1086 );
1087 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1088 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1089 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1090 }
1091
1092 /**
1093 * Test that update does not change status id CRM-15105
1094 */
1095 public function testCreateUpdateWithoutChangingPendingStatus() {
1096 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1097 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1098 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1099 'id' => $contribution['id'],
1100 'api.contribution.delete' => 1,
1101 ));
1102 $this->assertEquals(2, $contribution['contribution_status_id']);
1103 }
1104 //To Update Contribution
1105 //CHANGE: we require the API to do an incremental update
1106 public function testCreateUpdateContribution() {
1107
1108 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
1109 $old_params = array(
1110 'contribution_id' => $contributionID,
1111
1112 );
1113 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1114 //Make sure it came back
1115 $this->assertAPISuccess($original);
1116 $this->assertEquals($original['id'], $contributionID);
1117 //set up list of old params, verify
1118
1119 //This should not be required on update:
1120 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1121 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1122 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1123 $old_source = $original['values'][$contributionID]['contribution_source'];
1124
1125 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1126 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1127
1128 //check against values in CiviUnitTestCase::createContribution()
1129 $this->assertEquals($old_contact_id, $this->_individualId);
1130 $this->assertEquals($old_fee_amount, 5.00);
1131 $this->assertEquals($old_source, 'SSF');
1132 $this->assertEquals($old_trxn_id, 212355);
1133 $this->assertEquals($old_invoice_id, 'idofsh');
1134 $params = array(
1135 'id' => $contributionID,
1136 'contact_id' => $this->_individualId,
1137 'total_amount' => 110.00,
1138 'financial_type_id' => $this->_financialTypeId,
1139 'non_deductible_amount' => 10.00,
1140 'net_amount' => 100.00,
1141 'contribution_status_id' => 1,
1142 'note' => 'Donating for Nobel Cause',
1143
1144 );
1145
1146 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1147
1148 $new_params = array(
1149 'contribution_id' => $contribution['id'],
1150
1151 );
1152 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1153
1154 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1155 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1156 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
1157 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1158 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1159 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1160 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1161 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1162 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1163 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1164 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1165 $params = array(
1166 'contribution_id' => $contributionID,
1167
1168 );
1169 $result = $this->callAPISuccess('contribution', 'delete', $params);
1170 $this->assertAPISuccess($result);
1171 }
1172
1173 ///////////////// civicrm_contribution_delete methods
1174 public function testDeleteEmptyParamsContribution() {
1175 $params = array();
1176 $this->callAPIFailure('contribution', 'delete', $params);
1177 }
1178
1179 public function testDeleteParamsNotArrayContribution() {
1180 $params = 'contribution_id= 1';
1181 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1182 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1183 }
1184
1185 public function testDeleteWrongParamContribution() {
1186 $params = array(
1187 'contribution_source' => 'SSF',
1188
1189 );
1190 $this->callAPIFailure('contribution', 'delete', $params);
1191 }
1192
1193 public function testDeleteContribution() {
1194
1195 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
1196 $params = array(
1197 'id' => $contributionID,
1198 );
1199 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1200 }
1201
1202 /**
1203 * Test civicrm_contribution_search with empty params.
1204 * All available contributions expected.
1205 */
1206 public function testSearchEmptyParams() {
1207 $params = array();
1208
1209 $p = array(
1210 'contact_id' => $this->_individualId,
1211 'receive_date' => date('Ymd'),
1212 'total_amount' => 100.00,
1213 'financial_type_id' => $this->_financialTypeId,
1214 'non_deductible_amount' => 10.00,
1215 'fee_amount' => 5.00,
1216 'net_amount' => 95.00,
1217 'trxn_id' => 23456,
1218 'invoice_id' => 78910,
1219 'source' => 'SSF',
1220 'contribution_status_id' => 1,
1221
1222 );
1223 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1224
1225 $result = $this->callAPISuccess('contribution', 'get', $params);
1226 // We're taking the first element.
1227 $res = $result['values'][$contribution['id']];
1228
1229 $this->assertEquals($p['contact_id'], $res['contact_id']);
1230 $this->assertEquals($p['total_amount'], $res['total_amount']);
1231 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1232 $this->assertEquals($p['net_amount'], $res['net_amount']);
1233 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1234 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1235 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1236 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1237 $this->assertEquals($p['source'], $res['contribution_source']);
1238 // contribution_status_id = 1 => Completed
1239 $this->assertEquals('Completed', $res['contribution_status']);
1240
1241 $this->contributionDelete($contribution['id']);
1242 }
1243
1244 /**
1245 * Test civicrm_contribution_search. Success expected.
1246 */
1247 public function testSearch() {
1248 $p1 = array(
1249 'contact_id' => $this->_individualId,
1250 'receive_date' => date('Ymd'),
1251 'total_amount' => 100.00,
1252 'financial_type_id' => $this->_financialTypeId,
1253 'non_deductible_amount' => 10.00,
1254 'contribution_status_id' => 1,
1255
1256 );
1257 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1258
1259 $p2 = array(
1260 'contact_id' => $this->_individualId,
1261 'receive_date' => date('Ymd'),
1262 'total_amount' => 200.00,
1263 'financial_type_id' => $this->_financialTypeId,
1264 'non_deductible_amount' => 20.00,
1265 'trxn_id' => 5454565,
1266 'invoice_id' => 1212124,
1267 'fee_amount' => 50.00,
1268 'net_amount' => 60.00,
1269 'contribution_status_id' => 2,
1270
1271 );
1272 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1273
1274 $params = array(
1275 'contribution_id' => $contribution2['id'],
1276
1277 );
1278 $result = $this->callAPISuccess('contribution', 'get', $params);
1279 $res = $result['values'][$contribution2['id']];
1280
1281 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1282 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1283 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1284 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1285 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1286 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1287 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1288 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1289 // contribution_status_id = 2 => Pending
1290 $this->assertEquals('Pending', $res['contribution_status']);
1291
1292 $this->contributionDelete($contribution1['id']);
1293 $this->contributionDelete($contribution2['id']);
1294 }
1295
1296 /**
1297 * Test completing a transaction via the API
1298 *
1299 * Note that we are creating a logged in user because email goes out from
1300 * that person
1301 */
1302 public function testCompleteTransaction() {
1303 $mut = new CiviMailUtils($this, TRUE);
1304 $this->createLoggedInUser();
1305 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1306 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1307 $this->callAPISuccess('contribution', 'completetransaction', array(
1308 'id' => $contribution['id'],
1309 ));
1310 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1311 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1312 $mut->checkMailLog(array(
1313 'Receipt - Contribution',
1314 'Please print this confirmation for your records.',
1315 ));
1316 $mut->stop();
1317 }
1318
1319 /**
1320 * CRM-14151
1321 * Test completing a transaction via the API
1322 *
1323 * tests.
1324 */
1325 public function testCompleteTransactionWithReceiptDateSet() {
1326 $mut = new CiviMailUtils($this, TRUE);
1327 $this->createLoggedInUser();
1328 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1329 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1330 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1331 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1332 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1333 $mut->checkMailLog(array(
1334 'Receipt - Contribution',
1335 'Please print this confirmation for your records.',
1336 ));
1337 $mut->stop();
1338 }
1339
1340 /**
1341 * Test completing a transaction with an event via the API
1342 *
1343 * Note that we are creating a logged in user because email goes out from
1344 * that person
1345 */
1346 public function testCompleteTransactionWithParticipantRecord() {
1347 $mut = new CiviMailUtils($this, TRUE);
1348 $mut->clearMessages();
1349 $this->createLoggedInUser();
1350 $contributionID = $this->createPendingParticipantContribution();
1351 $this->callAPISuccess('contribution', 'completetransaction', array(
1352 'id' => $contributionID,
1353 )
1354 );
1355 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1356 'id' => $this->_ids['participant'],
1357 'return' => 'participant_status_id',
1358 ));
1359 $this->assertEquals(1, $participantStatus);
1360 $mut->checkMailLog(array(
1361 'Annual CiviCRM meet',
1362 'Event',
1363 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1364 ));
1365 $mut->stop();
1366 }
1367
1368 /**
1369 * Test membership is renewed when transaction completed
1370 */
1371 public function testCompleteTransactionMembershipPriceSet() {
1372 $this->createPriceSetWithPage('membership');
1373 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
1374 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1375 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1376 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1377 $this->cleanUpAfterPriceSets();
1378 }
1379
1380 /**
1381 * Test membership is renewed when transaction completed
1382 */
1383 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1384 $this->createPriceSetWithPage('membership');
1385 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
1386 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1387 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1388 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1389 $this->cleanUpAfterPriceSets();
1390 }
1391
1392 public function cleanUpAfterPriceSets() {
1393 $this->quickCleanUpFinancialEntities();
1394 $this->contactDelete($this->_ids['contact']);
1395 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set']));
1396 }
1397
1398
1399 /**
1400 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1401 * on parent class at some point (fn is not in 4.4)
1402 * @param $entity
1403 * @param array $params
1404 */
1405 public function createPriceSetWithPage($entity, $params = array()) {
1406 $membershipTypeID = $this->membershipTypeCreate();
1407 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1408 'title' => "Test Contribution Page",
1409 'financial_type_id' => 1,
1410 'currency' => 'NZD',
1411 'goal_amount' => 50,
1412 'is_pay_later' => 1,
1413 'is_monetary' => TRUE,
1414 'is_email_receipt' => FALSE,
1415 ));
1416 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1417 'is_quick_config' => 0,
1418 'extends' => 'CiviMember',
1419 'financial_type_id' => 1,
1420 'title' => 'my Page',
1421 ));
1422 $priceSetID = $priceSet['id'];
1423
1424 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1425 $priceField = $this->callAPISuccess('price_field', 'create', array(
1426 'price_set_id' => $priceSetID,
1427 'label' => 'Goat Breed',
1428 'html_type' => 'Radio',
1429 ));
1430 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1431 'price_set_id' => $priceSetID,
1432 'price_field_id' => $priceField['id'],
1433 'label' => 'Long Haired Goat',
1434 'amount' => 20,
1435 'membership_type_id' => $membershipTypeID,
1436 'membership_num_terms' => 1,
1437 )
1438 );
1439 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1440 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1441 'price_set_id' => $priceSetID,
1442 'price_field_id' => $priceField['id'],
1443 'label' => 'Shoe-eating Goat',
1444 'amount' => 10,
1445 'membership_type_id' => $membershipTypeID,
1446 'membership_num_terms' => 2,
1447 )
1448 );
1449 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
1450 $this->_ids['price_set'] = $priceSetID;
1451 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1452 $this->_ids['price_field'] = array($priceField['id']);
1453
1454 $this->_ids['membership_type'] = $membershipTypeID;
1455 }
1456
1457 /**
1458 * Set up a pending transaction with a specific price field id
1459 * @param int $priceFieldValueID
1460 */
1461 public function setUpPendingContribution($priceFieldValueID) {
1462 $contactID = $this->individualCreate();
1463 $membership = $this->callAPISuccess('membership', 'create', array(
1464 'contact_id' => $contactID,
1465 'membership_type_id' => $this->_ids['membership_type'],
1466 'start_date' => 'yesterday - 1 year',
1467 'end_date' => 'yesterday',
1468 ));
1469 $contribution = $this->callAPISuccess('contribution', 'create', array(
1470 'domain_id' => 1,
1471 'contact_id' => $contactID,
1472 'receive_date' => date('Ymd'),
1473 'total_amount' => 100.00,
1474 'financial_type_id' => 1,
1475 'payment_instrument_id' => 'Credit Card',
1476 'non_deductible_amount' => 10.00,
1477 'trxn_id' => 'jdhfi88',
1478 'invoice_id' => 'djfhiewuyr',
1479 'source' => 'SSF',
1480 'contribution_status_id' => 2,
1481 'contribution_page_id' => $this->_ids['contribution_page'],
1482 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1483 ));
1484
1485 $this->callAPISuccess('line_item', 'create', array(
1486 'entity_id' => $contribution['id'],
1487 'entity_table' => 'civicrm_contribution',
1488 'contribution_id' => $contribution['id'],
1489 'price_field_id' => $this->_ids['price_field'][0],
1490 'qty' => 1,
1491 'unit_price' => 20,
1492 'line_total' => 20,
1493 'financial_type_id' => 1,
1494 'price_field_value_id' => $priceFieldValueID,
1495 ));
1496 $this->_ids['contact'] = $contactID;
1497 $this->_ids['contribution'] = $contribution['id'];
1498 $this->_ids['membership'] = $membership['id'];
1499 }
1500
1501 /**
1502 * Test sending a mail via the API
1503 */
1504 public function testSendMail() {
1505 $mut = new CiviMailUtils($this, TRUE);
1506 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1507 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1508 'id' => $contribution['id'],
1509 'receipt_from_email' => 'api@civicrm.org',
1510 )
1511 );
1512 $mut->checkMailLog(array(
1513 '$ 100.00',
1514 'Contribution Information',
1515 'Please print this confirmation for your records',
1516 ), array(
1517 'Event',
1518 )
1519 );
1520 $mut->stop();
1521 }
1522
1523 /**
1524 * Test sending a mail via the API
1525 */
1526 public function testSendMailEvent() {
1527 $mut = new CiviMailUtils($this, TRUE);
1528 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1529 $event = $this->eventCreate(array(
1530 'is_email_confirm' => 1,
1531 'confirm_from_email' => 'test@civicrm.org',
1532 ));
1533 $this->_eventID = $event['id'];
1534 $participantParams = array(
1535 'contact_id' => $this->_individualId,
1536 'event_id' => $this->_eventID,
1537 'status_id' => 1,
1538 'role_id' => 1,
1539 // to ensure it matches later on
1540 'register_date' => '2007-07-21 00:00:00',
1541 'source' => 'Online Event Registration: API Testing',
1542
1543 );
1544 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1545 $this->callAPISuccess('participant_payment', 'create', array(
1546 'participant_id' => $participant['id'],
1547 'contribution_id' => $contribution['id'],
1548 ));
1549 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1550 'id' => $contribution['id'],
1551 'receipt_from_email' => 'api@civicrm.org',
1552 )
1553 );
1554
1555 $mut->checkMailLog(array(
1556 'Annual CiviCRM meet',
1557 'Event',
1558 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1559 ), array()
1560 );
1561 $mut->stop();
1562 }
1563
1564 /**
1565 * This function does a GET & compares the result against the $params
1566 * Use as a double check on Creates
1567 * @param $params
1568 * @param $id
1569 * @param int $delete
1570 */
1571 public function contributionGetnCheck($params, $id, $delete = 1) {
1572
1573 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1574 'id' => $id,
1575
1576 ));
1577
1578 if ($delete) {
1579 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1580 }
1581 $this->assertAPISuccess($contribution, 0);
1582 $values = $contribution['values'][$contribution['id']];
1583 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1584 // this is not returned in id format
1585 unset($params['payment_instrument_id']);
1586 $params['contribution_source'] = $params['source'];
1587 unset($params['source']);
1588 foreach ($params as $key => $value) {
1589 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1590 }
1591 }
1592
1593 /**
1594 * Create a pending contribution & linked pending participant record
1595 * (along with an event)
1596 */
1597 public function createPendingParticipantContribution() {
1598 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1599 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1600 $this->_ids['participant'] = $participantID;
1601 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1602 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1603 $this->callAPISuccess('participant_payment', 'create', array(
1604 'contribution_id' => $contribution['id'],
1605 'participant_id' => $participantID,
1606 ));
1607 $this->callAPISuccess('line_item', 'get', array(
1608 'entity_id' => $contribution['id'],
1609 'entity_table' => 'civicrm_contribution',
1610 'api.line_item.create' => array(
1611 'entity_id' => $participantID,
1612 'entity_table' => 'civicrm_participant',
1613 ),
1614 ));
1615 return $contribution['id'];
1616 }
1617
1618 /**
1619 * @param int $contId
1620 *
1621 * @return null|string
1622 */
1623 function _getFinancialTrxnAmount($contId) {
1624 $query = "SELECT
1625 SUM( ft.total_amount ) AS total
1626 FROM civicrm_financial_trxn AS ft
1627 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1628 WHERE ceft.entity_table = 'civicrm_contribution'
1629 AND ceft.entity_id = {$contId}";
1630
1631 $result = CRM_Core_DAO::singleValueQuery($query);
1632 return $result;
1633 }
1634
1635 /**
1636 * @param int $contId
1637 *
1638 * @return null|string
1639 */
1640 function _getFinancialItemAmount($contId) {
1641 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1642 $query = "SELECT
1643 SUM(amount)
1644 FROM civicrm_financial_item
1645 WHERE entity_table = 'civicrm_line_item'
1646 AND entity_id = {$lineItem}";
1647 $result = CRM_Core_DAO::singleValueQuery($query);
1648 return $result;
1649 }
1650
1651 /**
1652 * @param int $contId
1653 * @param $context
1654 */
1655 public function _checkFinancialItem($contId, $context) {
1656 if ($context != 'paylater') {
1657 $params = array(
1658 'entity_id' => $contId,
1659 'entity_table' => 'civicrm_contribution',
1660 );
1661 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1662 $entityParams = array(
1663 'financial_trxn_id' => $trxn['financial_trxn_id'],
1664 'entity_table' => 'civicrm_financial_item',
1665 );
1666 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1667 $params = array(
1668 'id' => $entityTrxn['entity_id'],
1669 );
1670 }
1671 if ($context == 'paylater') {
1672 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1673 foreach ($lineItems as $key => $item) {
1674 $params = array(
1675 'entity_id' => $key,
1676 'entity_table' => 'civicrm_line_item',
1677 );
1678 $compareParams = array('status_id' => 1);
1679 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1680 }
1681 }
1682 elseif ($context == 'refund') {
1683 $compareParams = array(
1684 'status_id' => 1,
1685 'financial_account_id' => 1,
1686 'amount' => -100,
1687 );
1688 }
1689 elseif ($context == 'cancelPending') {
1690 $compareParams = array(
1691 'status_id' => 3,
1692 'financial_account_id' => 1,
1693 'amount' => -100,
1694 );
1695 }
1696 elseif ($context == 'changeFinancial') {
1697 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1698 $params = array(
1699 'entity_id' => $lineKey,
1700 'amount' => -100,
1701 );
1702 $compareParams = array(
1703 'financial_account_id' => 1,
1704 );
1705 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1706 $params = array(
1707 'financial_account_id' => 3,
1708 'entity_id' => $lineKey,
1709 );
1710 $compareParams = array(
1711 'amount' => 100,
1712 );
1713 }
1714 if ($context != 'paylater') {
1715 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1716 }
1717 }
1718
1719 /**
1720 * @param $contribution
1721 * @param $context
1722 * @param int $instrumentId
1723 */
1724 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1725 $trxnParams = array(
1726 'entity_id' => $contribution['id'],
1727 'entity_table' => 'civicrm_contribution',
1728 );
1729 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1730 $params = array(
1731 'id' => $trxn['financial_trxn_id'],
1732 );
1733 if ($context == 'payLater') {
1734 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1735 $compareParams = array(
1736 'status_id' => 1,
1737 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1738 );
1739 }
1740 elseif ($context == 'refund') {
1741 $compareParams = array(
1742 'to_financial_account_id' => 6,
1743 'total_amount' => -100,
1744 'status_id' => 7,
1745 );
1746 }
1747 elseif ($context == 'cancelPending') {
1748 $compareParams = array(
1749 'from_financial_account_id' => 7,
1750 'total_amount' => -100,
1751 'status_id' => 3,
1752 );
1753 }
1754 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
1755 $entityParams = array(
1756 'entity_id' => $contribution['id'],
1757 'entity_table' => 'civicrm_contribution',
1758 'amount' => -100,
1759 );
1760 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1761 $trxnParams1 = array(
1762 'id' => $trxn['financial_trxn_id'],
1763 );
1764 $compareParams = array(
1765 'total_amount' => -100,
1766 'status_id' => 1,
1767 );
1768 if ($context == 'paymentInstrument') {
1769 $compareParams += array(
1770 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1771 'payment_instrument_id' => 4,
1772 );
1773 }
1774 else {
1775 $compareParams['to_financial_account_id'] = 12;
1776 }
1777 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1778 $compareParams['total_amount'] = 100;
1779 if ($context == 'paymentInstrument') {
1780 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1781 $compareParams['payment_instrument_id'] = $instrumentId;
1782 }
1783 else {
1784 $compareParams['to_financial_account_id'] = 12;
1785 }
1786 }
1787
1788 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1789 }
1790
1791 /**
1792 * @return mixed
1793 */
1794 public function _addPaymentInstrument() {
1795 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1796 $optionParams = array(
1797 'option_group_id' => $gId,
1798 'label' => 'Test Card',
1799 'name' => 'Test Card',
1800 'value' => '6',
1801 'weight' => '6',
1802 'is_active' => 1,
1803 );
1804 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1805 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1806 $financialParams = array(
1807 'entity_table' => 'civicrm_option_value',
1808 'entity_id' => $optionValue['id'],
1809 'account_relationship' => $relationTypeId,
1810 'financial_account_id' => 7,
1811 );
1812 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1813 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1814 return $optionValue['values'][$optionValue['id']]['value'];
1815 }
1816
1817 /**
1818 * @param array $params
1819 * @param $context
1820 */
1821 public function _checkFinancialRecords($params, $context) {
1822 $entityParams = array(
1823 'entity_id' => $params['id'],
1824 'entity_table' => 'civicrm_contribution',
1825 );
1826 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1827 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1828 if ($context == 'pending') {
1829 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1830 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1831 return;
1832 }
1833 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1834 $trxnParams = array(
1835 'id' => $trxn['financial_trxn_id'],
1836 );
1837 if ($context != 'online' && $context != 'payLater') {
1838 $compareParams = array(
1839 'to_financial_account_id' => 6,
1840 'total_amount' => 100,
1841 'status_id' => 1,
1842 );
1843 }
1844 if ($context == 'feeAmount') {
1845 $compareParams['fee_amount'] = 50;
1846 }
1847 elseif ($context == 'online') {
1848 $compareParams = array(
1849 'to_financial_account_id' => 12,
1850 'total_amount' => 100,
1851 'status_id' => 1,
1852 );
1853 }
1854 elseif ($context == 'payLater') {
1855 $compareParams = array(
1856 'to_financial_account_id' => 7,
1857 'total_amount' => 100,
1858 'status_id' => 2,
1859 );
1860 }
1861 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1862 $entityParams = array(
1863 'financial_trxn_id' => $trxn['financial_trxn_id'],
1864 'entity_table' => 'civicrm_financial_item',
1865 );
1866 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1867 $fitemParams = array(
1868 'id' => $entityTrxn['entity_id'],
1869 );
1870 $compareParams = array(
1871 'amount' => 100,
1872 'status_id' => 1,
1873 'financial_account_id' => 1,
1874 );
1875 if ($context == 'payLater') {
1876 $compareParams = array(
1877 'amount' => 100,
1878 'status_id' => 3,
1879 'financial_account_id' => 1,
1880 );
1881 }
1882 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1883 if ($context == 'feeAmount') {
1884 $maxParams = array(
1885 'entity_id' => $params['id'],
1886 'entity_table' => 'civicrm_contribution',
1887 );
1888 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1889 $trxnParams = array(
1890 'id' => $maxTrxn['financial_trxn_id'],
1891 );
1892 $compareParams = array(
1893 'to_financial_account_id' => 5,
1894 'from_financial_account_id' => 6,
1895 'total_amount' => 50,
1896 'status_id' => 1,
1897 );
1898 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1899 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1900 $fitemParams = array(
1901 'entity_id' => $trxnId['financialTrxnId'],
1902 'entity_table' => 'civicrm_financial_trxn',
1903 );
1904 $compareParams = array(
1905 'amount' => 50,
1906 'status_id' => 1,
1907 'financial_account_id' => 5,
1908 );
1909 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1910 }
1911 }
1912 }