Merge pull request #5536 from totten/4.5-httpclient
[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-2015 |
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 * Parameters to create payment processor.
55 *
56 * @var array
57 */
58 protected $_processorParams = array();
59
60 /**
61 * ID of created event.
62 *
63 * @var int
64 */
65 protected $_eventID;
66
67 /**
68 * Setup function.
69 */
70 public function setUp() {
71 parent::setUp();
72
73 $this->_apiversion = 3;
74 $this->_individualId = $this->individualCreate();
75 $paymentProcessor = $this->processorCreate();
76 $this->_params = array(
77 'contact_id' => $this->_individualId,
78 'receive_date' => '20120511',
79 'total_amount' => 100.00,
80 'financial_type_id' => $this->_financialTypeId,
81 'non_deductible_amount' => 10.00,
82 'fee_amount' => 5.00,
83 'net_amount' => 95.00,
84 'source' => 'SSF',
85 'contribution_status_id' => 1,
86 );
87 $this->_processorParams = array(
88 'domain_id' => 1,
89 'name' => 'Dummy',
90 'payment_processor_type_id' => 10,
91 'financial_account_id' => 12,
92 'is_active' => 1,
93 'user_name' => '',
94 'url_site' => 'http://dummy.com',
95 'url_recur' => 'http://dummy.com',
96 'billing_mode' => 1,
97 );
98 $this->_pageParams = array(
99 'title' => 'Test Contribution Page',
100 'financial_type_id' => 1,
101 'currency' => 'USD',
102 'financial_account_id' => 1,
103 'payment_processor' => $paymentProcessor->id,
104 'is_active' => 1,
105 'is_allow_other_amount' => 1,
106 'min_amount' => 10,
107 'max_amount' => 1000,
108 );
109 }
110
111 /**
112 * Clean up after each test.
113 */
114 public function tearDown() {
115 $this->quickCleanUpFinancialEntities();
116 }
117
118 /**
119 * Test Get.
120 */
121 public function testGetContribution() {
122 $p = array(
123 'contact_id' => $this->_individualId,
124 'receive_date' => '2010-01-20',
125 'total_amount' => 100.00,
126 'financial_type_id' => $this->_financialTypeId,
127 'non_deductible_amount' => 10.00,
128 'fee_amount' => 5.00,
129 'net_amount' => 95.00,
130 'trxn_id' => 23456,
131 'invoice_id' => 78910,
132 'source' => 'SSF',
133 'contribution_status_id' => 1,
134 );
135 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
136
137 $params = array(
138 'contribution_id' => $this->_contribution['id'],
139 );
140
141 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
142 $financialParams['id'] = $this->_financialTypeId;
143 $default = NULL;
144 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
145
146 $this->assertEquals(1, $contribution['count']);
147 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
148 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
149 // Passing back a string rather than an id seems like an error/cruft.
150 // If it is to be introduced we should discuss.
151 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
152 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
153 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
154 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
155 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
156 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
157 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
158 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
159 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
160 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
161 $p['trxn_id'] = '3847';
162 $p['invoice_id'] = '3847';
163
164 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
165
166 // Now we have 2 - test getcount.
167 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
168 $this->assertEquals(2, $contribution);
169 // Test id only format.
170 $contribution = $this->callAPISuccess('contribution', 'get', array(
171 'id' => $this->_contribution['id'],
172 'format.only_id' => 1,
173 ));
174 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
175 // Test id only format.
176 $contribution = $this->callAPISuccess('contribution', 'get', array(
177 'id' => $contribution2['id'],
178 'format.only_id' => 1,
179 ));
180 $this->assertEquals($contribution2['id'], $contribution);
181 // Test id as field.
182 $contribution = $this->callAPISuccess('contribution', 'get', array(
183 'id' => $this->_contribution['id'],
184 ));
185 $this->assertEquals(1, $contribution['count']);
186
187 // Test get by contact id works.
188 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
189
190 $this->assertEquals(2, $contribution['count']);
191 $this->callAPISuccess('Contribution', 'Delete', array(
192 'id' => $this->_contribution['id'],
193 ));
194 $this->callAPISuccess('Contribution', 'Delete', array(
195 'id' => $contribution2['id'],
196 ));
197 }
198
199 /**
200 * We need to ensure previous tested behaviour still works as part of the api contract.
201 */
202 public function testGetContributionLegacyBehaviour() {
203 $p = array(
204 'contact_id' => $this->_individualId,
205 'receive_date' => '2010-01-20',
206 'total_amount' => 100.00,
207 'contribution_type_id' => $this->_financialTypeId,
208 'non_deductible_amount' => 10.00,
209 'fee_amount' => 5.00,
210 'net_amount' => 95.00,
211 'trxn_id' => 23456,
212 'invoice_id' => 78910,
213 'source' => 'SSF',
214 'contribution_status_id' => 1,
215 );
216 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
217
218 $params = array(
219 'contribution_id' => $this->_contribution['id'],
220 );
221 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
222 $financialParams['id'] = $this->_financialTypeId;
223 $default = NULL;
224 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
225
226 $this->assertEquals(1, $contribution['count']);
227 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
228 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
229 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
230 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
231 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
232 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
233 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
234 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
235 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
236 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
237 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
238
239 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
240 $p['trxn_id'] = '3847';
241 $p['invoice_id'] = '3847';
242
243 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
244
245 // now we have 2 - test getcount
246 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
247 $this->assertEquals(2, $contribution);
248 //test id only format
249 $contribution = $this->callAPISuccess('contribution', 'get', array(
250 'id' => $this->_contribution['id'],
251 'format.only_id' => 1,
252 ));
253 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
254 //test id only format
255 $contribution = $this->callAPISuccess('contribution', 'get', array(
256 'id' => $contribution2['id'],
257 'format.only_id' => 1,
258 ));
259 $this->assertEquals($contribution2['id'], $contribution);
260 $contribution = $this->callAPISuccess('contribution', 'get', array(
261 'id' => $this->_contribution['id'],
262 ));
263 //test id as field
264 $this->assertEquals(1, $contribution['count']);
265 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
266 //test get by contact id works
267 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
268
269 $this->assertEquals(2, $contribution['count']);
270 $this->callAPISuccess('Contribution', 'Delete', array(
271 'id' => $this->_contribution['id'],
272 ));
273 $this->callAPISuccess('Contribution', 'Delete', array(
274 'id' => $contribution2['id'],
275 ));
276 }
277
278 /**
279 * Create an contribution_id=FALSE and financial_type_id=Donation.
280 */
281 public function testCreateEmptyContributionIDUseDonation() {
282 $params = array(
283 'contribution_id' => FALSE,
284 'contact_id' => 1,
285 'total_amount' => 1,
286 'check_permissions' => FALSE,
287 'financial_type_id' => 'Donation',
288 );
289 $this->callAPISuccess('contribution', 'create', $params);
290 }
291
292 /**
293 * Check with complete array + custom field.
294 *
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 *
319 * Note that the test is written on purpose without any
320 * variables specific to participant so it can be replicated into other entities
321 * and / or moved to the automated test suite
322 */
323 public function testCreateGetFieldsWithCustom() {
324 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
325 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
326 $result = $this->callAPISuccess('Contribution', 'getfields', array());
327 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
328 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
329 $this->customFieldDelete($ids['custom_field_id']);
330 $this->customGroupDelete($ids['custom_group_id']);
331 $this->customFieldDelete($idsContact['custom_field_id']);
332 $this->customGroupDelete($idsContact['custom_group_id']);
333 }
334
335 public function testCreateContributionNoLineItems() {
336
337 $params = array(
338 'contact_id' => $this->_individualId,
339 'receive_date' => '20120511',
340 'total_amount' => 100.00,
341 'financial_type_id' => $this->_financialTypeId,
342 'payment_instrument_id' => 1,
343 'non_deductible_amount' => 10.00,
344 'fee_amount' => 50.00,
345 'net_amount' => 90.00,
346 'trxn_id' => 12345,
347 'invoice_id' => 67890,
348 'source' => 'SSF',
349 'contribution_status_id' => 1,
350 'skipLineItem' => 1,
351 );
352
353 $contribution = $this->callAPISuccess('contribution', 'create', $params);
354 $lineItems = $this->callAPISuccess('line_item', 'get', array(
355 'entity_id' => $contribution['id'],
356 'entity_table' => 'civicrm_contribution',
357 'sequential' => 1,
358 ));
359 $this->assertEquals(0, $lineItems['count']);
360 }
361
362 /**
363 * Test checks that passing in line items suppresses the create mechanism.
364 */
365 public function testCreateContributionChainedLineItems() {
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 instrument.
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 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
726 require_once 'api/v3/examples/Contribution/Create.php';
727 $result = contribution_create_example();
728 $id = $result['id'];
729 $expectedResult = contribution_create_expectedresult();
730 $this->checkArrayEquals($expectedResult, $result);
731 $this->contributionDelete($id);
732 }
733
734 /**
735 * Function tests that additional financial records are created when fee amount is recorded.
736 */
737 public function testCreateContributionWithFee() {
738 $params = array(
739 'contact_id' => $this->_individualId,
740 'receive_date' => '20120511',
741 'total_amount' => 100.00,
742 'fee_amount' => 50,
743 'financial_type_id' => 1,
744 'trxn_id' => 12345,
745 'invoice_id' => 67890,
746 'source' => 'SSF',
747 'contribution_status_id' => 1,
748 );
749
750 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
751 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
752 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
753 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
754 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
755 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
756 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
757 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
758 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
759 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
760
761 $lineItems = $this->callAPISuccess('line_item', 'get', array(
762
763 'entity_id' => $contribution['id'],
764 'entity_table' => 'civicrm_contribution',
765 'sequential' => 1,
766 ));
767 $this->assertEquals(1, $lineItems['count']);
768 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
769 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
770 $lineItems = $this->callAPISuccess('line_item', 'get', array(
771
772 'entity_id' => $contribution['id'],
773 'contribution_id' => $contribution['id'],
774 'entity_table' => 'civicrm_contribution',
775 'sequential' => 1,
776 ));
777 $this->assertEquals(1, $lineItems['count']);
778 $this->_checkFinancialRecords($contribution, 'feeAmount');
779 }
780
781
782 /**
783 * Function tests that additional financial records are created when online contribution is created.
784 */
785 public function testCreateContributionOnline() {
786 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
787 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
788 $this->assertAPISuccess($contributionPage);
789 $params = array(
790 'contact_id' => $this->_individualId,
791 'receive_date' => '20120511',
792 'total_amount' => 100.00,
793 'financial_type_id' => 1,
794 'contribution_page_id' => $contributionPage['id'],
795 'payment_processor' => 1,
796 'trxn_id' => 12345,
797 'invoice_id' => 67890,
798 'source' => 'SSF',
799 'contribution_status_id' => 1,
800
801 );
802
803 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
804 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
805 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
806 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
807 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
808 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
809 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
810 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
811 $this->_checkFinancialRecords($contribution, 'online');
812 }
813
814 /**
815 * Check handling of financial type.
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 * Check handling of financial type.
828 *
829 * In the interests of removing financial type / contribution type checks from
830 * legacy format function lets test that the api is doing this for us
831 */
832 public function testValidNamedFinancialType() {
833 $params = $this->_params;
834 $params['financial_type_id'] = 'Donation';
835 $this->callAPISuccess($this->_entity, 'create', $params);
836 }
837
838 /**
839 * Tests that additional financial records are created.
840 *
841 * Checks when online contribution with pay later option is created
842 */
843 public function testCreateContributionPayLaterOnline() {
844 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
845 $this->_pageParams['is_pay_later'] = 1;
846 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
847 $this->assertAPISuccess($contributionPage);
848 $params = array(
849 'contact_id' => $this->_individualId,
850 'receive_date' => '20120511',
851 'total_amount' => 100.00,
852 'financial_type_id' => 1,
853 'contribution_page_id' => $contributionPage['id'],
854 'trxn_id' => 12345,
855 'is_pay_later' => 1,
856 'invoice_id' => 67890,
857 'source' => 'SSF',
858 'contribution_status_id' => 2,
859
860 );
861
862 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
863 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
864 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
865 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
866 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
867 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
868 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
869 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
870 $this->_checkFinancialRecords($contribution, 'payLater');
871 }
872
873 /**
874 * Function tests that additional financial records are created for online contribution with pending option.
875 */
876 public function testCreateContributionPendingOnline() {
877 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
878 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
879 $this->assertAPISuccess($contributionPage);
880 $params = array(
881 'contact_id' => $this->_individualId,
882 'receive_date' => '20120511',
883 'total_amount' => 100.00,
884 'financial_type_id' => 1,
885 'contribution_page_id' => $contributionPage['id'],
886 'trxn_id' => 12345,
887 'invoice_id' => 67890,
888 'source' => 'SSF',
889 'contribution_status_id' => 2,
890 );
891
892 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
893 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
894 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
895 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
896 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
897 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
898 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
899 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
900 $this->_checkFinancialRecords($contribution, 'pending');
901 }
902
903 /**
904 * Test that BAO defaults work.
905 */
906 public function testCreateBAODefaults() {
907 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
908 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
909 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
910 'id' => $contribution['id'],
911 'api.contribution.delete' => 1,
912 ));
913 $this->assertEquals(1, $contribution['contribution_status_id']);
914 $this->assertEquals('Check', $contribution['payment_instrument']);
915 }
916
917 /**
918 * Function tests that line items, financial records are updated when contribution amount is changed.
919 */
920 public function testCreateUpdateContributionChangeTotal() {
921 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
922 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
923
924 'entity_id' => $contribution['id'],
925 'entity_table' => 'civicrm_contribution',
926 'sequential' => 1,
927 'return' => 'line_total',
928 ));
929 $this->assertEquals('100.00', $lineItems);
930 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
931 // Financial trxn SUM = 100 + 5 (fee)
932 $this->assertEquals('105.00', $trxnAmount);
933 $newParams = array(
934
935 'id' => $contribution['id'],
936 'total_amount' => '125',
937 );
938 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
939
940 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
941
942 'entity_id' => $contribution['id'],
943 'entity_table' => 'civicrm_contribution',
944 'sequential' => 1,
945 'return' => 'line_total',
946 ));
947
948 $this->assertEquals('125.00', $lineItems);
949 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
950
951 // Financial trxn SUM = 125 + 5 (fee).
952 $this->assertEquals('130.00', $trxnAmount);
953 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
954 }
955
956 /**
957 * Function tests that line items, financial records are updated when pay later contribution is received.
958 */
959 public function testCreateUpdateContributionPayLater() {
960 $contribParams = array(
961 'contact_id' => $this->_individualId,
962 'receive_date' => '2012-01-01',
963 'total_amount' => 100.00,
964 'financial_type_id' => $this->_financialTypeId,
965 'payment_instrument_id' => 1,
966 'contribution_status_id' => 2,
967 'is_pay_later' => 1,
968
969 );
970 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
971
972 $newParams = array_merge($contribParams, array(
973 'id' => $contribution['id'],
974 'contribution_status_id' => 1,
975 )
976 );
977 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
978 $contribution = $contribution['values'][$contribution['id']];
979 $this->assertEquals($contribution['contribution_status_id'], '1');
980 $this->_checkFinancialItem($contribution['id'], 'paylater');
981 $this->_checkFinancialTrxn($contribution, 'payLater');
982 }
983
984 /**
985 * Function tests that financial records are updated when Payment Instrument is changed.
986 */
987 public function testCreateUpdateContributionPaymentInstrument() {
988 $instrumentId = $this->_addPaymentInstrument();
989 $contribParams = array(
990 'contact_id' => $this->_individualId,
991 'total_amount' => 100.00,
992 'financial_type_id' => $this->_financialTypeId,
993 'payment_instrument_id' => 4,
994 'contribution_status_id' => 1,
995
996 );
997 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
998
999 $newParams = array_merge($contribParams, array(
1000 'id' => $contribution['id'],
1001 'payment_instrument_id' => $instrumentId,
1002 )
1003 );
1004 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1005 $this->assertAPISuccess($contribution);
1006 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1007 }
1008
1009 /**
1010 * Function tests that financial records are added when Contribution is Refunded.
1011 */
1012 public function testCreateUpdateContributionRefund() {
1013 $contribParams = array(
1014 'contact_id' => $this->_individualId,
1015 'receive_date' => '2012-01-01',
1016 'total_amount' => 100.00,
1017 'financial_type_id' => $this->_financialTypeId,
1018 'payment_instrument_id' => 4,
1019 'contribution_status_id' => 1,
1020
1021 );
1022 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1023 $newParams = array_merge($contribParams, array(
1024 'id' => $contribution['id'],
1025 'contribution_status_id' => 7,
1026 )
1027 );
1028
1029 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1030 $this->_checkFinancialTrxn($contribution, 'refund');
1031 $this->_checkFinancialItem($contribution['id'], 'refund');
1032 }
1033
1034 /**
1035 * Function tests invalid contribution status change.
1036 */
1037 public function testCreateUpdateContributionInValidStatusChange() {
1038 $contribParams = array(
1039 'contact_id' => 1,
1040 'receive_date' => '2012-01-01',
1041 'total_amount' => 100.00,
1042 'financial_type_id' => 1,
1043 'payment_instrument_id' => 1,
1044 'contribution_status_id' => 1,
1045 );
1046 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1047 $newParams = array_merge($contribParams, array(
1048 'id' => $contribution['id'],
1049 'contribution_status_id' => 2,
1050 )
1051 );
1052 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1053
1054 }
1055
1056 /**
1057 * Function tests that financial records are added when Pending Contribution is Canceled.
1058 */
1059 public function testCreateUpdateContributionCancelPending() {
1060 $contribParams = array(
1061 'contact_id' => $this->_individualId,
1062 'receive_date' => '2012-01-01',
1063 'total_amount' => 100.00,
1064 'financial_type_id' => $this->_financialTypeId,
1065 'payment_instrument_id' => 1,
1066 'contribution_status_id' => 2,
1067 'is_pay_later' => 1,
1068
1069 );
1070 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1071 $newParams = array_merge($contribParams, array(
1072 'id' => $contribution['id'],
1073 'contribution_status_id' => 3,
1074 )
1075 );
1076 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1077 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1078 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1079 }
1080
1081 /**
1082 * Function tests that financial records are added when Financial Type is Changed.
1083 */
1084 public function testCreateUpdateContributionChangeFinancialType() {
1085 $contribParams = array(
1086 'contact_id' => $this->_individualId,
1087 'receive_date' => '2012-01-01',
1088 'total_amount' => 100.00,
1089 'financial_type_id' => 1,
1090 'payment_instrument_id' => 1,
1091 'contribution_status_id' => 1,
1092
1093 );
1094 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1095 $newParams = array_merge($contribParams, array(
1096 'id' => $contribution['id'],
1097 'financial_type_id' => 3,
1098 )
1099 );
1100 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1101 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1102 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1103 }
1104
1105 /**
1106 * Test that update does not change status id CRM-15105.
1107 */
1108 public function testCreateUpdateWithoutChangingPendingStatus() {
1109 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1110 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1111 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1112 'id' => $contribution['id'],
1113 'api.contribution.delete' => 1,
1114 ));
1115 $this->assertEquals(2, $contribution['contribution_status_id']);
1116 }
1117
1118 /**
1119 * Test Updating a Contribution.
1120 *
1121 * CHANGE: we require the API to do an incremental update
1122 */
1123 public function testCreateUpdateContribution() {
1124
1125 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
1126 $old_params = array(
1127 'contribution_id' => $contributionID,
1128
1129 );
1130 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1131 // Make sure it came back.
1132 $this->assertAPISuccess($original);
1133 $this->assertEquals($original['id'], $contributionID);
1134 //set up list of old params, verify
1135
1136 //This should not be required on update:
1137 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1138 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1139 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1140 $old_source = $original['values'][$contributionID]['contribution_source'];
1141
1142 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1143 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1144
1145 //check against values in CiviUnitTestCase::createContribution()
1146 $this->assertEquals($old_contact_id, $this->_individualId);
1147 $this->assertEquals($old_fee_amount, 5.00);
1148 $this->assertEquals($old_source, 'SSF');
1149 $this->assertEquals($old_trxn_id, 212355);
1150 $this->assertEquals($old_invoice_id, 'idofsh');
1151 $params = array(
1152 'id' => $contributionID,
1153 'contact_id' => $this->_individualId,
1154 'total_amount' => 110.00,
1155 'financial_type_id' => $this->_financialTypeId,
1156 'non_deductible_amount' => 10.00,
1157 'net_amount' => 100.00,
1158 'contribution_status_id' => 1,
1159 'note' => 'Donating for Nobel Cause',
1160
1161 );
1162
1163 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1164
1165 $new_params = array(
1166 'contribution_id' => $contribution['id'],
1167
1168 );
1169 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1170
1171 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1172 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1173 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
1174 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1175 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1176 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1177 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1178 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1179 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1180 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1181 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1182 $params = array(
1183 'contribution_id' => $contributionID,
1184
1185 );
1186 $result = $this->callAPISuccess('contribution', 'delete', $params);
1187 $this->assertAPISuccess($result);
1188 }
1189
1190 ///////////////// civicrm_contribution_delete methods
1191
1192 /**
1193 * Attempt (but fail) to delete a contribution without parameters.
1194 */
1195 public function testDeleteEmptyParamsContribution() {
1196 $params = array();
1197 $this->callAPIFailure('contribution', 'delete', $params);
1198 }
1199
1200 public function testDeleteParamsNotArrayContribution() {
1201 $params = 'contribution_id= 1';
1202 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1203 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1204 }
1205
1206 public function testDeleteWrongParamContribution() {
1207 $params = array(
1208 'contribution_source' => 'SSF',
1209
1210 );
1211 $this->callAPIFailure('contribution', 'delete', $params);
1212 }
1213
1214 public function testDeleteContribution() {
1215
1216 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
1217 $params = array(
1218 'id' => $contributionID,
1219 );
1220 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1221 }
1222
1223 /**
1224 * Test civicrm_contribution_search with empty params.
1225 *
1226 * All available contributions expected.
1227 */
1228 public function testSearchEmptyParams() {
1229 $params = array();
1230
1231 $p = array(
1232 'contact_id' => $this->_individualId,
1233 'receive_date' => date('Ymd'),
1234 'total_amount' => 100.00,
1235 'financial_type_id' => $this->_financialTypeId,
1236 'non_deductible_amount' => 10.00,
1237 'fee_amount' => 5.00,
1238 'net_amount' => 95.00,
1239 'trxn_id' => 23456,
1240 'invoice_id' => 78910,
1241 'source' => 'SSF',
1242 'contribution_status_id' => 1,
1243
1244 );
1245 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1246
1247 $result = $this->callAPISuccess('contribution', 'get', $params);
1248 // We're taking the first element.
1249 $res = $result['values'][$contribution['id']];
1250
1251 $this->assertEquals($p['contact_id'], $res['contact_id']);
1252 $this->assertEquals($p['total_amount'], $res['total_amount']);
1253 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1254 $this->assertEquals($p['net_amount'], $res['net_amount']);
1255 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1256 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1257 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1258 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1259 $this->assertEquals($p['source'], $res['contribution_source']);
1260 // contribution_status_id = 1 => Completed
1261 $this->assertEquals('Completed', $res['contribution_status']);
1262
1263 $this->contributionDelete($contribution['id']);
1264 }
1265
1266 /**
1267 * Test civicrm_contribution_search. Success expected.
1268 */
1269 public function testSearch() {
1270 $p1 = array(
1271 'contact_id' => $this->_individualId,
1272 'receive_date' => date('Ymd'),
1273 'total_amount' => 100.00,
1274 'financial_type_id' => $this->_financialTypeId,
1275 'non_deductible_amount' => 10.00,
1276 'contribution_status_id' => 1,
1277
1278 );
1279 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1280
1281 $p2 = array(
1282 'contact_id' => $this->_individualId,
1283 'receive_date' => date('Ymd'),
1284 'total_amount' => 200.00,
1285 'financial_type_id' => $this->_financialTypeId,
1286 'non_deductible_amount' => 20.00,
1287 'trxn_id' => 5454565,
1288 'invoice_id' => 1212124,
1289 'fee_amount' => 50.00,
1290 'net_amount' => 60.00,
1291 'contribution_status_id' => 2,
1292
1293 );
1294 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1295
1296 $params = array(
1297 'contribution_id' => $contribution2['id'],
1298
1299 );
1300 $result = $this->callAPISuccess('contribution', 'get', $params);
1301 $res = $result['values'][$contribution2['id']];
1302
1303 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1304 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1305 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1306 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1307 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1308 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1309 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1310 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1311 // contribution_status_id = 2 => Pending
1312 $this->assertEquals('Pending', $res['contribution_status']);
1313
1314 $this->contributionDelete($contribution1['id']);
1315 $this->contributionDelete($contribution2['id']);
1316 }
1317
1318 /**
1319 * Test completing a transaction via the API.
1320 *
1321 * Note that we are creating a logged in user because email goes out from
1322 * that person
1323 */
1324 public function testCompleteTransaction() {
1325 $mut = new CiviMailUtils($this, TRUE);
1326 $this->createLoggedInUser();
1327 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1328 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1329 $this->callAPISuccess('contribution', 'completetransaction', array(
1330 'id' => $contribution['id'],
1331 ));
1332 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1333 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1334 $mut->checkMailLog(array(
1335 'Receipt - Contribution',
1336 'Please print this confirmation for your records.',
1337 ));
1338 $mut->stop();
1339 }
1340
1341 /**
1342 * Test repeat contribution successfully creates line items.
1343 */
1344 public function testRepeatTransaction() {
1345 $paymentProcessorID = $this->paymentProcessorCreate();
1346 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1347 'contact_id' => $this->_individualId,
1348 'installments' => '12',
1349 'frequency_interval' => '1',
1350 'amount' => '500',
1351 'contribution_status_id' => 1,
1352 'start_date' => '2012-01-01 00:00:00',
1353 'currency' => 'USD',
1354 'frequency_unit' => 'month',
1355 'payment_processor_id' => $paymentProcessorID,
1356 ));
1357 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1358 $this->_params,
1359 array('contribution_recur_id' => $contributionRecur['id']))
1360 );
1361
1362 $this->callAPISuccess('contribution', 'repeattransaction', array(
1363 'original_contribution_id' => $originalContribution['id'],
1364 'contribution_status_id' => 'Completed',
1365 'trxn_id' => uniqid(),
1366 ));
1367 $lineItemParams = array(
1368 'entity_id' => $originalContribution['id'],
1369 'sequential' => 1,
1370 'return' => array(
1371 'entity_table',
1372 'qty',
1373 'unit_price',
1374 'line_total',
1375 'label',
1376 'financial_type_id',
1377 'deductible_amount',
1378 'price_field_value_id',
1379 'price_field_id',
1380 ),
1381 );
1382 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1383 'entity_id' => $originalContribution['id'],
1384 )));
1385 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1386 'entity_id' => $originalContribution['id'] + 1,
1387 )));
1388 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1389 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1390 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1391
1392 $this->quickCleanUpFinancialEntities();
1393 }
1394
1395 /**
1396 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1397 */
1398 public function testCompleteTransactionNetAmountOK() {
1399 $this->createLoggedInUser();
1400 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1401 unset($params['net_amount']);
1402 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1403 $this->callAPISuccess('contribution', 'completetransaction', array(
1404 'id' => $contribution['id'],
1405 ));
1406 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1407 $this->assertEquals('Completed', $contribution['contribution_status']);
1408 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1409 }
1410
1411 /**
1412 * CRM-14151 - Test completing a transaction via the API.
1413 */
1414 public function testCompleteTransactionWithReceiptDateSet() {
1415 $mut = new CiviMailUtils($this, TRUE);
1416 $this->createLoggedInUser();
1417 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1418 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1419 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
1420 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
1421 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1422 $mut->checkMailLog(array(
1423 'Receipt - Contribution',
1424 'Please print this confirmation for your records.',
1425 ));
1426 $mut->stop();
1427 }
1428
1429 /**
1430 * Test completing a transaction with an event via the API.
1431 *
1432 * Note that we are creating a logged in user because email goes out from
1433 * that person
1434 */
1435 public function testCompleteTransactionWithParticipantRecord() {
1436 $mut = new CiviMailUtils($this, TRUE);
1437 $mut->clearMessages();
1438 $this->createLoggedInUser();
1439 $contributionID = $this->createPendingParticipantContribution();
1440 $this->callAPISuccess('contribution', 'completetransaction', array(
1441 'id' => $contributionID,
1442 )
1443 );
1444 $participantStatus = $this->callAPISuccessGetValue('participant', array(
1445 'id' => $this->_ids['participant'],
1446 'return' => 'participant_status_id',
1447 ));
1448 $this->assertEquals(1, $participantStatus);
1449 $mut->checkMailLog(array(
1450 'Annual CiviCRM meet',
1451 'Event',
1452 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
1453 ));
1454 $mut->stop();
1455 }
1456
1457 /**
1458 * Test membership is renewed when transaction completed.
1459 */
1460 public function testCompleteTransactionMembershipPriceSet() {
1461 $this->createPriceSetWithPage('membership');
1462 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
1463 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1464 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1465 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1466 $this->cleanUpAfterPriceSets();
1467 }
1468
1469 /**
1470 * Test membership is renewed when transaction completed.
1471 */
1472 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
1473 $this->createPriceSetWithPage('membership');
1474 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
1475 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1476 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1477 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1478 $this->cleanUpAfterPriceSets();
1479 }
1480
1481 public function cleanUpAfterPriceSets() {
1482 $this->quickCleanUpFinancialEntities();
1483 $this->contactDelete($this->_ids['contact']);
1484 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set']));
1485 }
1486
1487
1488 /**
1489 * Create price set with contribution test for test setup.
1490 *
1491 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1492 * on parent class at some point (fn is not in 4.4).
1493 *
1494 * @param $entity
1495 * @param array $params
1496 */
1497 public function createPriceSetWithPage($entity, $params = array()) {
1498 $membershipTypeID = $this->membershipTypeCreate();
1499 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1500 'title' => "Test Contribution Page",
1501 'financial_type_id' => 1,
1502 'currency' => 'NZD',
1503 'goal_amount' => 50,
1504 'is_pay_later' => 1,
1505 'is_monetary' => TRUE,
1506 'is_email_receipt' => FALSE,
1507 ));
1508 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1509 'is_quick_config' => 0,
1510 'extends' => 'CiviMember',
1511 'financial_type_id' => 1,
1512 'title' => 'my Page',
1513 ));
1514 $priceSetID = $priceSet['id'];
1515
1516 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
1517 $priceField = $this->callAPISuccess('price_field', 'create', array(
1518 'price_set_id' => $priceSetID,
1519 'label' => 'Goat Breed',
1520 'html_type' => 'Radio',
1521 ));
1522 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1523 'price_set_id' => $priceSetID,
1524 'price_field_id' => $priceField['id'],
1525 'label' => 'Long Haired Goat',
1526 'amount' => 20,
1527 'membership_type_id' => $membershipTypeID,
1528 'membership_num_terms' => 1,
1529 )
1530 );
1531 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1532 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1533 'price_set_id' => $priceSetID,
1534 'price_field_id' => $priceField['id'],
1535 'label' => 'Shoe-eating Goat',
1536 'amount' => 10,
1537 'membership_type_id' => $membershipTypeID,
1538 'membership_num_terms' => 2,
1539 )
1540 );
1541 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
1542 $this->_ids['price_set'] = $priceSetID;
1543 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1544 $this->_ids['price_field'] = array($priceField['id']);
1545
1546 $this->_ids['membership_type'] = $membershipTypeID;
1547 }
1548
1549 /**
1550 * Set up a pending transaction with a specific price field id.
1551 *
1552 * @param int $priceFieldValueID
1553 */
1554 public function setUpPendingContribution($priceFieldValueID) {
1555 $contactID = $this->individualCreate();
1556 $membership = $this->callAPISuccess('membership', 'create', array(
1557 'contact_id' => $contactID,
1558 'membership_type_id' => $this->_ids['membership_type'],
1559 'start_date' => 'yesterday - 1 year',
1560 'end_date' => 'yesterday',
1561 ));
1562 $contribution = $this->callAPISuccess('contribution', 'create', array(
1563 'domain_id' => 1,
1564 'contact_id' => $contactID,
1565 'receive_date' => date('Ymd'),
1566 'total_amount' => 100.00,
1567 'financial_type_id' => 1,
1568 'payment_instrument_id' => 'Credit Card',
1569 'non_deductible_amount' => 10.00,
1570 'trxn_id' => 'jdhfi88',
1571 'invoice_id' => 'djfhiewuyr',
1572 'source' => 'SSF',
1573 'contribution_status_id' => 2,
1574 'contribution_page_id' => $this->_ids['contribution_page'],
1575 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1576 ));
1577
1578 $this->callAPISuccess('line_item', 'create', array(
1579 'entity_id' => $contribution['id'],
1580 'entity_table' => 'civicrm_contribution',
1581 'contribution_id' => $contribution['id'],
1582 'price_field_id' => $this->_ids['price_field'][0],
1583 'qty' => 1,
1584 'unit_price' => 20,
1585 'line_total' => 20,
1586 'financial_type_id' => 1,
1587 'price_field_value_id' => $priceFieldValueID,
1588 ));
1589 $this->_ids['contact'] = $contactID;
1590 $this->_ids['contribution'] = $contribution['id'];
1591 $this->_ids['membership'] = $membership['id'];
1592 }
1593
1594 /**
1595 * Test sending a mail via the API.
1596 */
1597 public function testSendMail() {
1598 $mut = new CiviMailUtils($this, TRUE);
1599 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1600 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1601 'id' => $contribution['id'],
1602 'receipt_from_email' => 'api@civicrm.org',
1603 )
1604 );
1605 $mut->checkMailLog(array(
1606 '$ 100.00',
1607 'Contribution Information',
1608 'Please print this confirmation for your records',
1609 ), array(
1610 'Event',
1611 )
1612 );
1613 $mut->stop();
1614 }
1615
1616 /**
1617 * Test sending a mail via the API.
1618 */
1619 public function testSendMailEvent() {
1620 $mut = new CiviMailUtils($this, TRUE);
1621 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1622 $event = $this->eventCreate(array(
1623 'is_email_confirm' => 1,
1624 'confirm_from_email' => 'test@civicrm.org',
1625 ));
1626 $this->_eventID = $event['id'];
1627 $participantParams = array(
1628 'contact_id' => $this->_individualId,
1629 'event_id' => $this->_eventID,
1630 'status_id' => 1,
1631 'role_id' => 1,
1632 // to ensure it matches later on
1633 'register_date' => '2007-07-21 00:00:00',
1634 'source' => 'Online Event Registration: API Testing',
1635
1636 );
1637 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1638 $this->callAPISuccess('participant_payment', 'create', array(
1639 'participant_id' => $participant['id'],
1640 'contribution_id' => $contribution['id'],
1641 ));
1642 $this->callAPISuccess('contribution', 'sendconfirmation', array(
1643 'id' => $contribution['id'],
1644 'receipt_from_email' => 'api@civicrm.org',
1645 )
1646 );
1647
1648 $mut->checkMailLog(array(
1649 'Annual CiviCRM meet',
1650 'Event',
1651 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1652 ), array()
1653 );
1654 $mut->stop();
1655 }
1656
1657 /**
1658 * This function does a GET & compares the result against the $params.
1659 *
1660 * Use as a double check on Creates.
1661 *
1662 * @param array $params
1663 * @param int $id
1664 * @param bool $delete
1665 */
1666 public function contributionGetnCheck($params, $id, $delete = TRUE) {
1667
1668 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1669 'id' => $id,
1670
1671 ));
1672
1673 if ($delete) {
1674 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1675 }
1676 $this->assertAPISuccess($contribution, 0);
1677 $values = $contribution['values'][$contribution['id']];
1678 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1679 // this is not returned in id format
1680 unset($params['payment_instrument_id']);
1681 $params['contribution_source'] = $params['source'];
1682 unset($params['source']);
1683 foreach ($params as $key => $value) {
1684 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
1685 }
1686 }
1687
1688 /**
1689 * Create a pending contribution & linked pending participant record (along with an event).
1690 */
1691 public function createPendingParticipantContribution() {
1692 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
1693 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1694 $this->_ids['participant'] = $participantID;
1695 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1696 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1697 $this->callAPISuccess('participant_payment', 'create', array(
1698 'contribution_id' => $contribution['id'],
1699 'participant_id' => $participantID,
1700 ));
1701 $this->callAPISuccess('line_item', 'get', array(
1702 'entity_id' => $contribution['id'],
1703 'entity_table' => 'civicrm_contribution',
1704 'api.line_item.create' => array(
1705 'entity_id' => $participantID,
1706 'entity_table' => 'civicrm_participant',
1707 ),
1708 ));
1709 return $contribution['id'];
1710 }
1711
1712 /**
1713 * Get financial transaction amount.
1714 *
1715 * @param int $contId
1716 *
1717 * @return null|string
1718 */
1719 public function _getFinancialTrxnAmount($contId) {
1720 $query = "SELECT
1721 SUM( ft.total_amount ) AS total
1722 FROM civicrm_financial_trxn AS ft
1723 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1724 WHERE ceft.entity_table = 'civicrm_contribution'
1725 AND ceft.entity_id = {$contId}";
1726
1727 $result = CRM_Core_DAO::singleValueQuery($query);
1728 return $result;
1729 }
1730
1731 /**
1732 * @param int $contId
1733 *
1734 * @return null|string
1735 */
1736 public function _getFinancialItemAmount($contId) {
1737 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1738 $query = "SELECT
1739 SUM(amount)
1740 FROM civicrm_financial_item
1741 WHERE entity_table = 'civicrm_line_item'
1742 AND entity_id = {$lineItem}";
1743 $result = CRM_Core_DAO::singleValueQuery($query);
1744 return $result;
1745 }
1746
1747 /**
1748 * @param int $contId
1749 * @param $context
1750 */
1751 public function _checkFinancialItem($contId, $context) {
1752 if ($context != 'paylater') {
1753 $params = array(
1754 'entity_id' => $contId,
1755 'entity_table' => 'civicrm_contribution',
1756 );
1757 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1758 $entityParams = array(
1759 'financial_trxn_id' => $trxn['financial_trxn_id'],
1760 'entity_table' => 'civicrm_financial_item',
1761 );
1762 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1763 $params = array(
1764 'id' => $entityTrxn['entity_id'],
1765 );
1766 }
1767 if ($context == 'paylater') {
1768 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1769 foreach ($lineItems as $key => $item) {
1770 $params = array(
1771 'entity_id' => $key,
1772 'entity_table' => 'civicrm_line_item',
1773 );
1774 $compareParams = array('status_id' => 1);
1775 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1776 }
1777 }
1778 elseif ($context == 'refund') {
1779 $compareParams = array(
1780 'status_id' => 1,
1781 'financial_account_id' => 1,
1782 'amount' => -100,
1783 );
1784 }
1785 elseif ($context == 'cancelPending') {
1786 $compareParams = array(
1787 'status_id' => 3,
1788 'financial_account_id' => 1,
1789 'amount' => -100,
1790 );
1791 }
1792 elseif ($context == 'changeFinancial') {
1793 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1794 $params = array(
1795 'entity_id' => $lineKey,
1796 'amount' => -100,
1797 );
1798 $compareParams = array(
1799 'financial_account_id' => 1,
1800 );
1801 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1802 $params = array(
1803 'financial_account_id' => 3,
1804 'entity_id' => $lineKey,
1805 );
1806 $compareParams = array(
1807 'amount' => 100,
1808 );
1809 }
1810 if ($context != 'paylater') {
1811 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1812 }
1813 }
1814
1815 /**
1816 * @param $contribution
1817 * @param $context
1818 * @param int $instrumentId
1819 */
1820 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
1821 $trxnParams = array(
1822 'entity_id' => $contribution['id'],
1823 'entity_table' => 'civicrm_contribution',
1824 );
1825 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1826 $params = array(
1827 'id' => $trxn['financial_trxn_id'],
1828 );
1829 if ($context == 'payLater') {
1830 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1831 $compareParams = array(
1832 'status_id' => 1,
1833 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1834 );
1835 }
1836 elseif ($context == 'refund') {
1837 $compareParams = array(
1838 'to_financial_account_id' => 6,
1839 'total_amount' => -100,
1840 'status_id' => 7,
1841 );
1842 }
1843 elseif ($context == 'cancelPending') {
1844 $compareParams = array(
1845 'from_financial_account_id' => 7,
1846 'total_amount' => -100,
1847 'status_id' => 3,
1848 );
1849 }
1850 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
1851 $entityParams = array(
1852 'entity_id' => $contribution['id'],
1853 'entity_table' => 'civicrm_contribution',
1854 'amount' => -100,
1855 );
1856 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1857 $trxnParams1 = array(
1858 'id' => $trxn['financial_trxn_id'],
1859 );
1860 $compareParams = array(
1861 'total_amount' => -100,
1862 'status_id' => 1,
1863 );
1864 if ($context == 'paymentInstrument') {
1865 $compareParams += array(
1866 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1867 'payment_instrument_id' => 4,
1868 );
1869 }
1870 else {
1871 $compareParams['to_financial_account_id'] = 12;
1872 }
1873 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1874 $compareParams['total_amount'] = 100;
1875 if ($context == 'paymentInstrument') {
1876 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1877 $compareParams['payment_instrument_id'] = $instrumentId;
1878 }
1879 else {
1880 $compareParams['to_financial_account_id'] = 12;
1881 }
1882 }
1883
1884 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1885 }
1886
1887 /**
1888 * @return mixed
1889 */
1890 public function _addPaymentInstrument() {
1891 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1892 $optionParams = array(
1893 'option_group_id' => $gId,
1894 'label' => 'Test Card',
1895 'name' => 'Test Card',
1896 'value' => '6',
1897 'weight' => '6',
1898 'is_active' => 1,
1899 );
1900 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1901 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1902 $financialParams = array(
1903 'entity_table' => 'civicrm_option_value',
1904 'entity_id' => $optionValue['id'],
1905 'account_relationship' => $relationTypeId,
1906 'financial_account_id' => 7,
1907 );
1908 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1909 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1910 return $optionValue['values'][$optionValue['id']]['value'];
1911 }
1912
1913 /**
1914 * @param array $params
1915 * @param $context
1916 */
1917 public function _checkFinancialRecords($params, $context) {
1918 $entityParams = array(
1919 'entity_id' => $params['id'],
1920 'entity_table' => 'civicrm_contribution',
1921 );
1922 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1923 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
1924 if ($context == 'pending') {
1925 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1926 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1927 return;
1928 }
1929 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1930 $trxnParams = array(
1931 'id' => $trxn['financial_trxn_id'],
1932 );
1933 if ($context != 'online' && $context != 'payLater') {
1934 $compareParams = array(
1935 'to_financial_account_id' => 6,
1936 'total_amount' => 100,
1937 'status_id' => 1,
1938 );
1939 }
1940 if ($context == 'feeAmount') {
1941 $compareParams['fee_amount'] = 50;
1942 }
1943 elseif ($context == 'online') {
1944 $compareParams = array(
1945 'to_financial_account_id' => 12,
1946 'total_amount' => 100,
1947 'status_id' => 1,
1948 );
1949 }
1950 elseif ($context == 'payLater') {
1951 $compareParams = array(
1952 'to_financial_account_id' => 7,
1953 'total_amount' => 100,
1954 'status_id' => 2,
1955 );
1956 }
1957 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1958 $entityParams = array(
1959 'financial_trxn_id' => $trxn['financial_trxn_id'],
1960 'entity_table' => 'civicrm_financial_item',
1961 );
1962 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1963 $fitemParams = array(
1964 'id' => $entityTrxn['entity_id'],
1965 );
1966 $compareParams = array(
1967 'amount' => 100,
1968 'status_id' => 1,
1969 'financial_account_id' => 1,
1970 );
1971 if ($context == 'payLater') {
1972 $compareParams = array(
1973 'amount' => 100,
1974 'status_id' => 3,
1975 'financial_account_id' => 1,
1976 );
1977 }
1978 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1979 if ($context == 'feeAmount') {
1980 $maxParams = array(
1981 'entity_id' => $params['id'],
1982 'entity_table' => 'civicrm_contribution',
1983 );
1984 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1985 $trxnParams = array(
1986 'id' => $maxTrxn['financial_trxn_id'],
1987 );
1988 $compareParams = array(
1989 'to_financial_account_id' => 5,
1990 'from_financial_account_id' => 6,
1991 'total_amount' => 50,
1992 'status_id' => 1,
1993 );
1994 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1995 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1996 $fitemParams = array(
1997 'entity_id' => $trxnId['financialTrxnId'],
1998 'entity_table' => 'civicrm_financial_trxn',
1999 );
2000 $compareParams = array(
2001 'amount' => 50,
2002 'status_id' => 1,
2003 'financial_account_id' => 5,
2004 );
2005 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2006 }
2007 }
2008
2009 }