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