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