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