Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
39 class api_v3_ContributionTest extends CiviUnitTestCase {
40
41 /**
42 * Assume empty database with just civicrm_data
43 */
44 protected $_individualId;
45 protected $_contribution;
46 protected $_financialTypeId = 1;
47 protected $_apiversion;
48 protected $_entity = 'Contribution';
49 public $debug = 0;
50 protected $_params;
51 public $_eNoticeCompliant = TRUE;
52 function setUp() {
53 parent::setUp();
54
55 $this->_apiversion = 3;
56 $this->_individualId = $this->individualCreate();
57 $paymentProcessor = $this->processorCreate();
58 $this->_params = array(
59 'contact_id' => $this->_individualId,
60 'receive_date' => '20120511',
61 'total_amount' => 100.00,
62 'financial_type_id' => $this->_financialTypeId,
63 'non_deductible_amount' => 10.00,
64 'fee_amount' => 5.00,
65 'net_amount' => 95.00,
66 'source' => 'SSF',
67 'contribution_status_id' => 1,
68 );
69 $this->_processorParams = array(
70 'domain_id' => 1,
71 'name' => 'Dummy',
72 'payment_processor_type_id' => 10,
73 'financial_account_id' => 12,
74 'is_active' => 1,
75 'user_name' => '',
76 'url_site' => 'http://dummy.com',
77 'url_recur' => 'http://dummy.com',
78 'billing_mode' => 1,
79 );
80 $this->_pageParams = array(
81 'title' => 'Test Contribution Page',
82 'financial_type_id' => 1,
83 'currency' => 'USD',
84 'financial_account_id' => 1,
85 'payment_processor' => $paymentProcessor->id,
86 'is_active' => 1,
87 'is_allow_other_amount' => 1,
88 'min_amount' => 10,
89 'max_amount' => 1000,
90 );
91 }
92
93 function tearDown() {
94 $this->quickCleanup(array(
95 'civicrm_contribution',
96 'civicrm_contribution_soft',
97 'civicrm_event',
98 'civicrm_contribution_page',
99 'civicrm_participant',
100 'civicrm_participant_payment',
101 'civicrm_line_item',
102 'civicrm_financial_trxn',
103 'civicrm_financial_item',
104 'civicrm_entity_financial_trxn',
105 'civicrm_contact',
106 ));
107 }
108
109 function testGetContribution() {
110 $p = array(
111 'contact_id' => $this->_individualId,
112 'receive_date' => '2010-01-20',
113 'total_amount' => 100.00,
114 'financial_type_id' => $this->_financialTypeId,
115 'non_deductible_amount' => 10.00,
116 'fee_amount' => 5.00,
117 'net_amount' => 95.00,
118 'trxn_id' => 23456,
119 'invoice_id' => 78910,
120 'source' => 'SSF',
121 'contribution_status_id' => 1,
122 );
123 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
124
125 $params = array(
126 'contribution_id' => $this->_contribution['id'],
127 );
128 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
129 $financialParams['id'] = $this->_financialTypeId;
130 $default = null;
131 $financialType = CRM_Financial_BAO_FinancialType::retrieve($financialParams,$default);
132
133 $this->assertEquals(1,$contribution['count']);
134 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
135 // note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
136 // passing back a string rather than an id seems like an error / cruft - & 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, 'In line ' . __LINE__);
139 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00, 'In line ' . __LINE__);
140 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00, 'In line ' . __LINE__);
141 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00, 'In line ' . __LINE__);
142 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456, 'In line ' . __LINE__);
143 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910, 'In line ' . __LINE__);
144 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF', 'In line ' . __LINE__);
145 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed', 'In line ' . __LINE__);
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) . " in line " . __LINE__);
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'], 'In line ' . __LINE__);
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'], 'In line ' . __LINE__);
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 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 $financialType = 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, 'In line ' . __LINE__);
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, 'In line ' . __LINE__);
217 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00, 'In line ' . __LINE__);
218 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00, 'In line ' . __LINE__);
219 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00, 'In line ' . __LINE__);
220 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456, 'In line ' . __LINE__);
221 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910, 'In line ' . __LINE__);
222 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF', 'In line ' . __LINE__);
223 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed', 'In line ' . __LINE__);
224 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
225 $p['trxn_id'] = '3847';
226 $p['invoice_id'] = '3847';
227
228 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
229
230 // now we have 2 - test getcount
231 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
232 $this->assertEquals(2, $contribution);
233 //test id only format
234 $contribution = $this->callAPISuccess('contribution', 'get', array(
235 'id' => $this->_contribution['id'],
236 'format.only_id' => 1,
237 ));
238 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__);
239 //test id only format
240 $contribution = $this->callAPISuccess('contribution', 'get', array(
241 'id' => $contribution2['id'],
242 'format.only_id' => 1,
243 ));
244 $this->assertEquals($contribution2['id'], $contribution);
245 $contribution = $this->callAPISuccess('contribution', 'get', array(
246 'id' => $this->_contribution['id'],
247 ));
248 //test id as field
249 $this->assertEquals(1, $contribution['count'], 'In line ' . __LINE__);
250 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
251 //test get by contact id works
252 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
253
254 $this->assertEquals(2, $contribution['count'], 'In line ' . __LINE__);
255 $this->callAPISuccess('Contribution', 'Delete', array(
256 'id' => $this->_contribution['id'],
257 ));
258 $this->callAPISuccess('Contribution', 'Delete', array(
259 'id' => $contribution2['id'],
260 ));
261 }
262 ///////////////// civicrm_contribution_
263 function testCreateEmptyContributionIDUseDonation() {
264 $params = array(
265 'contribution_id' => FALSE,
266 'contact_id' => 1,
267 'total_amount' => 1,
268 'check_permissions' => false,
269 'financial_type_id' => 'Donation',
270 );
271 $contribution = $this->callAPISuccess('contribution', 'create', $params);
272 }
273 /*
274 * ensure we continue to support contribution_type_id as part of the api commitment to
275 * stability
276 *///////////////// civicrm_contribution_
277
278 function testCreateLegacyBehaviour() {
279 $params = array(
280 'contribution_id' => FALSE,
281 'contact_id' => 1,
282 'total_amount' => 1,
283 'check_permissions' => false,
284 'contribution_type_id' => 3,
285 );
286 $contribution = $this->callAPISuccess('contribution', 'create', $params);
287 $contribution = $this->callAPISuccess('contribution', 'getsingle', array( 'id' => $contribution['id']));
288 $this->assertEquals(3, $contribution['financial_type_id']);
289 }
290
291 /**
292 * check with complete array + custom field
293 * Note that the test is written on purpose without any
294 * variables specific to participant so it can be replicated into other entities
295 * and / or moved to the automated test suite
296 */
297 function testCreateWithCustom() {
298 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
299
300 $params = $this->_params;
301 $params['custom_' . $ids['custom_field_id']] = "custom string";
302
303 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
304 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
305 $check = $this->callAPISuccess($this->_entity, 'get', array(
306 'return.custom_' . $ids['custom_field_id'] => 1,
307 'id' => $result['id'],
308 ));
309 $this->customFieldDelete($ids['custom_field_id']);
310 $this->customGroupDelete($ids['custom_group_id']);
311 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
312 }
313
314 /**
315 * check with complete array + custom field
316 * Note that the test is written on purpose without any
317 * variables specific to participant so it can be replicated into other entities
318 * and / or moved to the automated test suite
319 */
320 function testCreateGetFieldsWithCustom() {
321 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
322 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
323 $result = $this->callAPISuccess('Contribution', 'getfields', array());
324 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
325 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
326 $this->customFieldDelete($ids['custom_field_id']);
327 $this->customGroupDelete($ids['custom_group_id']);
328 $this->customFieldDelete($idsContact['custom_field_id']);
329 $this->customGroupDelete($idsContact['custom_group_id']);
330 }
331
332 function testCreateContributionNoLineItems() {
333
334 $params = array(
335 'contact_id' => $this->_individualId,
336 'receive_date' => '20120511',
337 'total_amount' => 100.00,
338 'financial_type_id' => $this->_financialTypeId,
339 'payment_instrument_id' => 1,
340 'non_deductible_amount' => 10.00,
341 'fee_amount' => 50.00,
342 'net_amount' => 90.00,
343 'trxn_id' => 12345,
344 'invoice_id' => 67890,
345 'source' => 'SSF',
346 'contribution_status_id' => 1,
347 'skipLineItem' => 1,
348 );
349
350 $contribution = $this->callAPISuccess('contribution', 'create', $params);
351 $lineItems = $this->callAPISuccess('line_item','get',array(
352 'entity_id' => $contribution['id'],
353 'entity_table' => 'civicrm_contribution',
354 'sequential' => 1,
355 ));
356 $this->assertEquals(0, $lineItems['count']);
357 }
358 /*
359 * Test checks that passing in line items suppresses the create mechanism
360 */
361 function testCreateContributionChainedLineItems() {
362
363 $params = array(
364 'contact_id' => $this->_individualId,
365 'receive_date' => '20120511',
366 'total_amount' => 100.00,
367 'financial_type_id' => $this->_financialTypeId,
368 'payment_instrument_id' => 1,
369 'non_deductible_amount' => 10.00,
370 'fee_amount' => 50.00,
371 'net_amount' => 90.00,
372 'trxn_id' => 12345,
373 'invoice_id' => 67890,
374 'source' => 'SSF',
375 'contribution_status_id' => 1,
376 'skipLineItem' => 1,
377 'api.line_item.create' => array(
378 array(
379 'price_field_id' => 1,
380 'qty' => 2,
381 'line_total' => '20',
382 'unit_price' => '10',
383 ),
384 array(
385 'price_field_id' => 1,
386 'qty' => 1,
387 'line_total' => '80',
388 'unit_price' => '80',
389 ),
390 ),
391 );
392
393 $description = "Create Contribution with Nested Line Items";
394 $subfile = "CreateWithNestedLineItems";
395 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__,__FILE__, $description, $subfile);
396
397 $lineItems = $this->callAPISuccess('line_item','get',array(
398 'entity_id' => $contribution['id'],
399 'entity_table' => 'civicrm_contribution',
400 'sequential' => 1,
401 ));
402 $this->assertEquals(2, $lineItems['count']);
403 }
404
405 function testCreateContributionOffline() {
406 $params = array(
407 'contact_id' => $this->_individualId,
408 'receive_date' => '20120511',
409 'total_amount' => 100.00,
410 'financial_type_id' => 1,
411 'trxn_id' => 12345,
412 'invoice_id' => 67890,
413 'source' => 'SSF',
414 'contribution_status_id' => 1,
415 );
416
417 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
418 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
419 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
420 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
421 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
422 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
423 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
424 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__);
425 $lineItems = $this->callAPISuccess('line_item','get',array(
426 'entity_id' => $contribution['id'],
427 'entity_table' => 'civicrm_contribution',
428 'sequential' => 1,
429 ));
430 $this->assertEquals(1, $lineItems['count']);
431 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
432 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
433 $lineItems = $this->callAPISuccess('line_item','get',array(
434 'entity_id' => $contribution['id'],
435 'entity_table' => 'civicrm_contribution',
436 'sequential' => 1,
437 ));
438 $this->assertEquals(1, $lineItems['count']);
439 $this->_checkFinancialRecords($contribution, 'offline');
440 $this->contributionGetnCheck($params, $contribution['id']);
441 }
442 /**
443 * test create with valid payment instument
444 */
445 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('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
456 $contribution = $this->callAPISuccess('contribution','get', array(
457 'sequential' => 1,
458 'id' => $contribution['id']
459 ));
460 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
461 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
462 }
463
464 function testGetContributionByPaymentInstrument() {
465 $params = $this->_params + array('payment_instrument' => 'EFT');
466 $params2 = $this->_params + array('payment_instrument' => 'Cash');
467 $this->callAPISuccess('contribution','create',$params);
468 $this->callAPISuccess('contribution','create',$params2);
469 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash'));
470 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
471 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
472 $this->assertEquals(1,$contribution['count']);
473 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
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( 'sequential' => 1, 'payment_instrument_id' => 5));
478 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
479 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
480 $this->assertEquals(1,$contribution['count']);
481 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument' => 'EFT'));
482 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
483 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
484 $this->assertEquals(1, $contribution['count']);
485 $contribution = $this->callAPISuccess('contribution', 'update', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
486 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
487 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
488 $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']);
489 $this->assertEquals(1,$contribution['count']);
490 }
491
492 /*
493 * Create test with unique field name on source
494 */
495 function testCreateContributionSource() {
496
497 $params = array(
498 'contact_id' => $this->_individualId,
499 'receive_date' => date('Ymd'),
500 'total_amount' => 100.00,
501 'financial_type_id' => $this->_financialTypeId,
502 'payment_instrument_id' => 1,
503 'non_deductible_amount' => 10.00,
504 'fee_amount' => 50.00,
505 'net_amount' => 90.00,
506 'trxn_id' => 12345,
507 'invoice_id' => 67890,
508 'contribution_source' => 'SSF',
509 'contribution_status_id' => 1,
510 );
511
512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
513 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
514 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
515 }
516 /*
517 * Create test with unique field name on source
518 */
519 function testCreateContributionSourceInvalidContac() {
520
521 $params = array(
522 'contact_id' => 999,
523 'receive_date' => date('Ymd'),
524 'total_amount' => 100.00,
525 'financial_type_id' => $this->_financialTypeId,
526 'payment_instrument_id' => 1,
527 'non_deductible_amount' => 10.00,
528 'fee_amount' => 50.00,
529 'net_amount' => 90.00,
530 'trxn_id' => 12345,
531 'invoice_id' => 67890,
532 'contribution_source' => 'SSF',
533 'contribution_status_id' => 1,
534 );
535
536 $contribution = $this->callAPIFailure('contribution', 'create', $params,
537 'contact_id is not valid : 999');
538 }
539
540 function testCreateContributionSourceInvalidContContac() {
541
542 $params = array(
543 'contribution_contact_id' => 999,
544 'receive_date' => date('Ymd'),
545 'total_amount' => 100.00,
546 'financial_type_id' => $this->_financialTypeId,
547 'payment_instrument_id' => 1,
548 'non_deductible_amount' => 10.00,
549 'fee_amount' => 50.00,
550 'net_amount' => 90.00,
551 'trxn_id' => 12345,
552 'invoice_id' => 67890,
553 'contribution_source' => 'SSF',
554 'contribution_status_id' => 1,
555 );
556
557 $contribution = $this->callAPIFailure('contribution', 'create', $params,
558 'contact_id is not valid : 999', 'In line ' . __LINE__);
559 }
560
561 function testCreateContributionWithNote() {
562 $description = "Demonstrates creating contribution with Note Entity";
563 $subfile = "ContributionCreateWithNote";
564 $params = array(
565 'contact_id' => $this->_individualId,
566 'receive_date' => '2012-01-01',
567 'total_amount' => 100.00,
568 'financial_type_id' => $this->_financialTypeId,
569 'payment_instrument_id' => 1,
570 'non_deductible_amount' => 10.00,
571 'fee_amount' => 50.00,
572 'net_amount' => 90.00,
573 'trxn_id' => 12345,
574 'invoice_id' => 67890,
575 'source' => 'SSF',
576 'contribution_status_id' => 1,
577 'note' => 'my contribution note',
578 );
579
580 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
581 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
582 $this->assertEquals('my contribution note', $result['values'][0]['note']);
583 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
584 }
585
586 function testCreateContributionWithNoteUniqueNameAliases() {
587 $params = array(
588 'contact_id' => $this->_individualId,
589 'receive_date' => '2012-01-01',
590 'total_amount' => 100.00,
591 'financial_type_id' => $this->_financialTypeId,
592 'payment_instrument_id' => 1,
593 'non_deductible_amount' => 10.00,
594 'fee_amount' => 50.00,
595 'net_amount' => 90.00,
596 'trxn_id' => 12345,
597 'invoice_id' => 67890,
598 'source' => 'SSF',
599 'contribution_status_id' => 1,
600 'contribution_note' => 'my contribution note',
601 );
602
603 $contribution = $this->callAPISuccess('contribution', 'create', $params);
604 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
605 $this->assertEquals('my contribution note', $result['values'][0]['note']);
606 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
607 }
608 /*
609 * This is the test for creating soft credits - however a 'get' is not yet possible via API
610 * as the current BAO functions are contact-centric (from what I can find)
611 *
612 */
613 function testCreateContributionWithSoftCredt() {
614 $description = "Demonstrates creating contribution with SoftCredit";
615 $subfile = "ContributionCreateWithSoftCredit";
616 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
617 $params = $this->_params + array(
618 'soft_credit_to' => $contact2['id'],
619
620 );
621
622 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
623 // $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit_to', 'sequential' => 1));
624 // $this->assertEquals($contact2['id'], $result['values'][$result['id']]['soft_credit_to']) ;
625 // well - the above doesn't work yet so lets do SQL
626 $query = "SELECT count(*) FROM civicrm_contribution_soft WHERE contact_id = " . $contact2['id'];
627
628 $count = CRM_Core_DAO::singleValueQuery($query);
629 $this->assertEquals(1, $count);
630
631 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
632 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
633 }
634
635 /**
636 * Test using example code
637 */
638 function testContributionCreateExample() {
639 //make sure at least on page exists since there is a truncate in tear down
640 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
641 $this->assertAPISuccess($page);
642 require_once 'api/v3/examples/ContributionCreate.php';
643 $result = contribution_create_example();
644 $this->assertAPISuccess($result);
645 $contributionId = $result['id'];
646 $expectedResult = contribution_create_expectedresult();
647 $this->checkArrayEquals($result, $expectedResult);
648 $this->contributionDelete($contributionId);
649 }
650
651 /*
652 * Function tests that additional financial records are created when fee amount is recorded
653 */
654 function testCreateContributionWithFee() {
655 $params = array(
656 'contact_id' => $this->_individualId,
657 'receive_date' => '20120511',
658 'total_amount' => 100.00,
659 'fee_amount' => 50,
660 'financial_type_id' => 1,
661 'trxn_id' => 12345,
662 'invoice_id' => 67890,
663 'source' => 'SSF',
664 'contribution_status_id' => 1,
665 );
666
667 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
668 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
669 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
670 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
671 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
672 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
673 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
674 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__);
675 $lineItems = $this->callAPISuccess('line_item','get',array(
676
677 'entity_id' => $contribution['id'],
678 'entity_table' => 'civicrm_contribution',
679 'sequential' => 1,
680 ));
681 $this->assertEquals(1, $lineItems['count']);
682 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
683 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
684 $lineItems = $this->callAPISuccess('line_item','get',array(
685
686 'entity_id' => $contribution['id'],
687 'entity_table' => 'civicrm_contribution',
688 'sequential' => 1,
689 ));
690 $this->assertEquals(1, $lineItems['count']);
691 $this->_checkFinancialRecords($contribution, 'feeAmount');
692 }
693
694
695 /**
696 * Function tests that additional financial records are created when online contribution is created
697 */
698 function testCreateContributionOnline() {
699 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
700 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams );
701 $this->assertAPISuccess($contributionPage);
702 $params = array(
703 'contact_id' => $this->_individualId,
704 'receive_date' => '20120511',
705 'total_amount' => 100.00,
706 'financial_type_id' => 1,
707 'contribution_page_id' => $contributionPage['id'],
708 'payment_processor' => 1,
709 'trxn_id' => 12345,
710 'invoice_id' => 67890,
711 'source' => 'SSF',
712 'contribution_status_id' => 1,
713
714 );
715
716 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
717 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
718 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
719 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
720 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
721 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
722 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
723 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__);
724 $this->_checkFinancialRecords($contribution, 'online');
725 }
726
727 /**
728 * in the interests of removing financial type / contribution type checks from
729 * legacy format function lets test that the api is doing this for us
730 */
731 function testCreateInvalidFinancialType() {
732 $params = $this->_params;
733 $params['financial_type_id'] = 99999;
734 $result = $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
735 }
736
737 /**
738 * in the interests of removing financial type / contribution type checks from
739 * legacy format function lets test that the api is doing this for us
740 */
741 function testValidNamedFinancialType() {
742 $params = $this->_params;
743 $params['financial_type_id'] = 'Donation';
744 $result = $this->callAPISuccess($this->_entity, 'create', $params);
745 }
746
747 /**
748 * Function tests that additional financial records are created when online contribution with pay later option
749 * is created
750 */
751 function testCreateContributionPayLaterOnline() {
752 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
753 $this->_pageParams['is_pay_later'] = 1;
754 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams );
755 $this->assertAPISuccess($contributionPage);
756 $params = array(
757 'contact_id' => $this->_individualId,
758 'receive_date' => '20120511',
759 'total_amount' => 100.00,
760 'financial_type_id' => 1,
761 'contribution_page_id' => $contributionPage['id'],
762 'trxn_id' => 12345,
763 'is_pay_later' => 1,
764 'invoice_id' => 67890,
765 'source' => 'SSF',
766 'contribution_status_id' => 2,
767
768 );
769
770 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
771 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
772 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
773 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
774 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
775 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
776 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
777 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__);
778 $this->_checkFinancialRecords($contribution, 'payLater');
779 }
780
781 /*
782 * Function tests that additional financial records are created when online contribution with pending option
783 * is created
784 */
785 function testCreateContributionPendingOnline() {
786 $paymentProcessor = 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 'trxn_id' => 12345,
796 'invoice_id' => 67890,
797 'source' => 'SSF',
798 'contribution_status_id' => 2,
799 );
800
801 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
802 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
803 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
804 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
805 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
806 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
807 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
808 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__);
809 $this->_checkFinancialRecords($contribution, 'pending');
810 }
811
812 /*
813 * Function tests that line items, financial records are updated when contribution amount is changed
814 */
815 function testCreateUpdateContributionChangeTotal() {
816 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
817 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
818
819 'entity_id' => $contribution['id'],
820 'entity_table' => 'civicrm_contribution',
821 'sequential' => 1,
822 'return' => 'line_total',
823 ));
824 $this->assertEquals('100.00', $lineItems);
825 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
826 // Financial trxn SUM = 100 + 5 (fee)
827 $this->assertEquals('105.00', $trxnAmount);
828 $newParams = array(
829
830 'id' => $contribution['id'],
831 'total_amount' => '125');
832 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
833
834 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
835
836 'entity_id' => $contribution['id'],
837 'entity_table' => 'civicrm_contribution',
838 'sequential' => 1,
839 'return' => 'line_total',
840 ));
841
842 $this->assertEquals('125.00', $lineItems);
843 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
844 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
845 // Financial trxn SUM = 125 + 5 (fee)
846 $this->assertEquals('130.00', $trxnAmount);
847 $this->assertEquals('125.00', $fitemAmount);
848 }
849
850 /*
851 * Function tests that line items, financial records are updated when pay later contribution is received
852 */
853 function testCreateUpdateContributionPayLater() {
854 $contribParams = array(
855 'contact_id' => $this->_individualId,
856 'receive_date' => '2012-01-01',
857 'total_amount' => 100.00,
858 'financial_type_id' => $this->_financialTypeId,
859 'payment_instrument_id' => 1,
860 'contribution_status_id' => 2,
861 'is_pay_later' => 1,
862
863 );
864 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
865
866 $newParams = array_merge($contribParams, array(
867 'id' => $contribution['id'],
868 'contribution_status_id' => 1,)
869 );
870 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
871 $contribution = $contribution['values'][$contribution['id']];
872 $this->assertEquals($contribution['contribution_status_id'],'1');
873 $this->_checkFinancialItem($contribution['id'], 'paylater');
874 $this->_checkFinancialTrxn($contribution, 'payLater');
875 }
876
877 /*
878 * Function tests that financial records are updated when Payment Instrument is changed
879 */
880 function testCreateUpdateContributionPaymentInstrument() {
881 $instrumentId = $this->_addPaymentInstrument();
882 $contribParams = array(
883 'contact_id' => $this->_individualId,
884 'total_amount' => 100.00,
885 'financial_type_id' => $this->_financialTypeId,
886 'payment_instrument_id' => 4,
887 'contribution_status_id' => 1,
888
889 );
890 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
891
892 $newParams = array_merge($contribParams, array(
893 'id' => $contribution['id'],
894 'payment_instrument_id' => $instrumentId,)
895 );
896 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
897 $this->assertAPISuccess($contribution);
898 $this->_checkFinancialTrxn($contribution, 'paymentInstrument');
899 }
900
901 /*
902 * Function tests that financial records are added when Contribution is Refunded
903 */
904 function testCreateUpdateContributionRefund() {
905 $contribParams = array(
906 'contact_id' => $this->_individualId,
907 'receive_date' => '2012-01-01',
908 'total_amount' => 100.00,
909 'financial_type_id' => $this->_financialTypeId,
910 'payment_instrument_id' => 4,
911 'contribution_status_id' => 1,
912
913 );
914 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
915 $newParams = array_merge($contribParams, array(
916 'id' => $contribution['id'],
917 'contribution_status_id' => 7,
918 )
919 );
920
921 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
922 $this->_checkFinancialTrxn($contribution, 'refund');
923 $this->_checkFinancialItem($contribution['id'], 'refund');
924 }
925
926 /*
927 * Function tests invalid contribution status change
928 */
929 function testCreateUpdateContributionInValidStatusChange() {
930 $contribParams = array(
931 'contact_id' => 1,
932 'receive_date' => '2012-01-01',
933 'total_amount' => 100.00,
934 'financial_type_id' => 1,
935 'payment_instrument_id' => 1,
936 'contribution_status_id' => 1,
937 );
938 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
939 $newParams = array_merge($contribParams, array(
940 'id' => $contribution['id'],
941 'contribution_status_id' => 2,
942 )
943 );
944 $contribution = $this->callAPIFailure('contribution', 'update', $newParams,
945 ts('Cannot change contribution status from Completed to Pending.')
946 );
947
948 }
949
950 /*
951 * Function tests that financial records are added when Pending Contribution is Canceled
952 */
953 function testCreateUpdateContributionCancelPending() {
954 $contribParams = array(
955 'contact_id' => $this->_individualId,
956 'receive_date' => '2012-01-01',
957 'total_amount' => 100.00,
958 'financial_type_id' => $this->_financialTypeId,
959 'payment_instrument_id' => 1,
960 'contribution_status_id' => 2,
961 'is_pay_later' => 1,
962
963 );
964 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
965 $newParams = array_merge($contribParams, array(
966 'id' => $contribution['id'],
967 'contribution_status_id' => 3,
968 )
969 );
970 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
971 $this->_checkFinancialTrxn($contribution, 'cancelPending');
972 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
973 }
974
975 /*
976 * Function tests that financial records are added when Financial Type is Changed
977 */
978 function testCreateUpdateContributionChangeFinancialType() {
979 $contribParams = array(
980 'contact_id' => $this->_individualId,
981 'receive_date' => '2012-01-01',
982 'total_amount' => 100.00,
983 'financial_type_id' => 1,
984 'payment_instrument_id' => 1,
985 'contribution_status_id' => 1,
986
987 );
988 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
989 $newParams = array_merge($contribParams, array(
990 'id' => $contribution['id'],
991 'financial_type_id' => 3,
992 )
993 );
994 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
995 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
996 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
997 }
998
999 //To Update Contribution
1000 //CHANGE: we require the API to do an incremental update
1001 function testCreateUpdateContribution() {
1002
1003 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
1004 $old_params = array(
1005 'contribution_id' => $contributionID,
1006
1007 );
1008 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1009 //Make sure it came back
1010 $this->assertAPISuccess($original, 'In line ' . __LINE__);
1011 $this->assertEquals($original['id'], $contributionID, 'In line ' . __LINE__);
1012 //set up list of old params, verify
1013
1014 //This should not be required on update:
1015 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1016 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1017 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1018 $old_source = $original['values'][$contributionID]['contribution_source'];
1019
1020 //note: current behavior is to return ISO. Is this
1021 //documented behavior? Is this correct
1022 $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date']));
1023
1024 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1025 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1026
1027 //check against values in CiviUnitTestCase::createContribution()
1028 $this->assertEquals($old_contact_id, $this->_individualId, 'In line ' . __LINE__);
1029 $this->assertEquals($old_fee_amount, 5.00, 'In line ' . __LINE__);
1030 $this->assertEquals($old_source, 'SSF', 'In line ' . __LINE__);
1031 $this->assertEquals($old_trxn_id, 212355, 'In line ' . __LINE__);
1032 $this->assertEquals($old_invoice_id, 'idofsh', 'In line ' . __LINE__);
1033 $params = array(
1034 'id' => $contributionID,
1035 'contact_id' => $this->_individualId,
1036 'total_amount' => 110.00,
1037 'financial_type_id' => $this->_financialTypeId,
1038 'non_deductible_amount' => 10.00,
1039 'net_amount' => 100.00,
1040 'contribution_status_id' => 1,
1041 'note' => 'Donating for Nobel Cause',
1042
1043 );
1044
1045 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1046
1047 $new_params = array(
1048 'contribution_id' => $contribution['id'],
1049
1050 );
1051 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1052
1053 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
1054 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00, 'In line ' . __LINE__);
1055 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId, 'In line ' . __LINE__ );
1056 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument, 'In line ' . __LINE__);
1057 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00, 'In line ' . __LINE__);
1058 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount, 'In line ' . __LINE__);
1059 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00, 'In line ' . __LINE__);
1060 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id, 'In line ' . __LINE__);
1061 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id, 'In line ' . __LINE__);
1062 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source, 'In line ' . __LINE__);
1063 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed', 'In line ' . __LINE__);
1064 $params = array(
1065 'contribution_id' => $contributionID,
1066
1067 );
1068 $result = $this->callAPISuccess('contribution', 'delete', $params);
1069 $this->assertAPISuccess($result, 'in line' . __LINE__);
1070 }
1071
1072 ///////////////// civicrm_contribution_delete methods
1073 function testDeleteEmptyParamsContribution() {
1074 $params = array();
1075 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1076 }
1077
1078 function testDeleteParamsNotArrayContribution() {
1079 $params = 'contribution_id= 1';
1080 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1081 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1082 }
1083
1084 function testDeleteWrongParamContribution() {
1085 $params = array(
1086 'contribution_source' => 'SSF',
1087
1088 );
1089 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1090 }
1091
1092 function testDeleteContribution() {
1093
1094 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
1095 $params = array(
1096 'id' => $contributionID,
1097 );
1098 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1099 }
1100
1101 /**
1102 * Test civicrm_contribution_search with empty params.
1103 * All available contributions expected.
1104 */
1105 function testSearchEmptyParams() {
1106 $params = array();
1107
1108 $p = array(
1109 'contact_id' => $this->_individualId,
1110 'receive_date' => date('Ymd'),
1111 'total_amount' => 100.00,
1112 'financial_type_id' => $this->_financialTypeId,
1113 'non_deductible_amount' => 10.00,
1114 'fee_amount' => 5.00,
1115 'net_amount' => 95.00,
1116 'trxn_id' => 23456,
1117 'invoice_id' => 78910,
1118 'source' => 'SSF',
1119 'contribution_status_id' => 1,
1120
1121 );
1122 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1123
1124 $result = $this->callAPISuccess('contribution', 'get', $params);
1125 // We're taking the first element.
1126 $res = $result['values'][$contribution['id']];
1127
1128 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
1129 $this->assertEquals($p['total_amount'], $res['total_amount'], 'In line ' . __LINE__);
1130 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ );
1131 $this->assertEquals($p['net_amount'], $res['net_amount'], 'In line ' . __LINE__);
1132 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__);
1133 $this->assertEquals($p['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__);
1134 $this->assertEquals($p['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__);
1135 $this->assertEquals($p['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__);
1136 $this->assertEquals($p['source'], $res['contribution_source'], 'In line ' . __LINE__);
1137 // contribution_status_id = 1 => Completed
1138 $this->assertEquals('Completed', $res['contribution_status'], 'In line ' . __LINE__);
1139
1140 $this->contributionDelete($contribution['id']);
1141 }
1142
1143 /**
1144 * Test civicrm_contribution_search. Success expected.
1145 */
1146 function testSearch() {
1147 $p1 = array(
1148 'contact_id' => $this->_individualId,
1149 'receive_date' => date('Ymd'),
1150 'total_amount' => 100.00,
1151 'financial_type_id' => $this->_financialTypeId,
1152 'non_deductible_amount' => 10.00,
1153 'contribution_status_id' => 1,
1154
1155 );
1156 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1157
1158 $p2 = array(
1159 'contact_id' => $this->_individualId,
1160 'receive_date' => date('Ymd'),
1161 'total_amount' => 200.00,
1162 'financial_type_id' => $this->_financialTypeId,
1163 'non_deductible_amount' => 20.00,
1164 'trxn_id' => 5454565,
1165 'invoice_id' => 1212124,
1166 'fee_amount' => 50.00,
1167 'net_amount' => 60.00,
1168 'contribution_status_id' => 2,
1169
1170 );
1171 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1172
1173 $params = array(
1174 'contribution_id' => $contribution2['id'],
1175
1176 );
1177 $result = $this->callAPISuccess('contribution', 'get', $params);
1178 $res = $result['values'][$contribution2['id']];
1179
1180 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
1181 $this->assertEquals($p2['total_amount'], $res['total_amount'], 'In line ' . __LINE__);
1182 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ );
1183 $this->assertEquals($p2['net_amount'], $res['net_amount'], 'In line ' . __LINE__);
1184 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__);
1185 $this->assertEquals($p2['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__);
1186 $this->assertEquals($p2['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__);
1187 $this->assertEquals($p2['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__);
1188 // contribution_status_id = 2 => Pending
1189 $this->assertEquals('Pending', $res['contribution_status'], 'In line ' . __LINE__);
1190
1191 $this->contributionDelete($contribution1['id']);
1192 $this->contributionDelete($contribution2['id']);
1193 }
1194
1195 /**
1196 * Test completing a transaction via the API
1197 *
1198 * Note that we are creating a logged in user because email goes out from
1199 * that person
1200 */
1201 function testCompleteTransaction() {
1202 $mut = new CiviMailUtils( $this, true );
1203 $this->createLoggedInUser();
1204 $params = array_merge($this->_params, array('contribution_status_id' => 1,));
1205 $contribution = $this->callAPISuccess('contribution','create', $params);
1206 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1207 'id' => $contribution['id'],
1208 )
1209 );
1210 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1211 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1212 $mut->checkMailLog(array(
1213 'Receipt - Contribution',
1214 'Please print this confirmation for your records.',
1215 ));
1216 $mut->stop();
1217 }
1218
1219 /**
1220 * Test completing a transaction with an event via the API
1221 *
1222 * Note that we are creating a logged in user because email goes out from
1223 * that person
1224 */
1225 function testCompleteTransactionWithParticipantRecord() {
1226 $mut = new CiviMailUtils( $this, true );
1227 $mut->clearMessages();
1228 $this->createLoggedInUser();
1229 $contributionID = $this->createPendingParticipantContribution();
1230 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1231 'id' => $contributionID,
1232 )
1233 );
1234 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids['participant'], 'return' => 'participant_status_id'));
1235 $this->assertEquals(1, $participantStatus);
1236 $mut->checkMailLog(array(
1237 'Annual CiviCRM meet',
1238 'Event',
1239 'This letter is a confirmation that your registration has been received and your status has been updated to registered for the following',
1240 ));
1241 $mut->stop();
1242
1243 }
1244
1245 /**
1246 * Test sending a mail via the API
1247 */
1248 function testSendMail() {
1249 $mut = new CiviMailUtils( $this, true );
1250 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1251 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
1252
1253 'id' => $contribution['id'],
1254 'receipt_from_email' => 'api@civicrm.org',
1255 )
1256 );
1257 $mut->checkMailLog(array(
1258 '$ 100.00',
1259 'Contribution Information',
1260 'Please print this confirmation for your records',
1261 ), array(
1262 'Event'
1263 )
1264 );
1265 $mut->stop();
1266 }
1267
1268 /**
1269 * Test sending a mail via the API
1270 */
1271 function testSendMailEvent() {
1272 $mut = new CiviMailUtils( $this, true );
1273 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1274 $event = $this->eventCreate(array(
1275 'is_email_confirm' => 1,
1276 'confirm_from_email' => 'test@civicrm.org',
1277 ));
1278 $this->_eventID = $event['id'];
1279 $participantParams = array(
1280 'contact_id' => $this->_individualId,
1281 'event_id' => $this->_eventID,
1282 'status_id' => 1,
1283 'role_id' => 1,
1284 // to ensure it matches later on
1285 'register_date' => '2007-07-21 00:00:00',
1286 'source' => 'Online Event Registration: API Testing',
1287
1288 );
1289 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1290 $this->callAPISuccess('participant_payment', 'create', array(
1291 'participant_id' => $participant['id'],
1292 'contribution_id' => $contribution['id'],
1293 ));
1294 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
1295
1296 'id' => $contribution['id'],
1297 'receipt_from_email' => 'api@civicrm.org',
1298 )
1299 );
1300
1301 $mut->checkMailLog(array(
1302 'Annual CiviCRM meet',
1303 'Event',
1304 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1305 ), array(
1306
1307 )
1308 );
1309 $mut->stop();
1310 }
1311
1312 /**
1313 * This function does a GET & compares the result against the $params
1314 * Use as a double check on Creates
1315 */
1316 function contributionGetnCheck($params, $id, $delete = 1) {
1317
1318 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
1319 'id' => $id,
1320
1321 ));
1322
1323 if ($delete) {
1324 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
1325 }
1326 $this->assertAPISuccess($contribution, 0, 'In line ' . __LINE__);
1327 $values = $contribution['values'][$contribution['id']];
1328 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1329 // this is not returned in id format
1330 unset($params['payment_instrument_id']);
1331 $params['contribution_source'] = $params['source'];
1332 unset($params['source']);
1333 foreach ($params as $key => $value) {
1334 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
1335 }
1336 }
1337
1338 /**
1339 * Create a pending contribution & linked pending participant record
1340 * (along with an event)
1341 */
1342 function createPendingParticipantContribution(){
1343 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1344 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1345 $this->ids['participant'] = $participantID;
1346 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1347 $contribution = $this->callAPISuccess('contribution','create', $params);
1348 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1349 $lineItem = $this->callAPISuccess('line_item', 'get', array(
1350 'entity_id' => $contribution['id'],
1351 'entity_table' => 'civicrm_contribution',
1352 'api.line_item.create' => array(
1353 'entity_id' => $participantID,
1354 'entity_table' => 'civicrm_participant',
1355 ),
1356 ));
1357 return $contribution['id'];
1358 }
1359
1360 function _getFinancialTrxnAmount($contId) {
1361 $query = "SELECT
1362 SUM( ft.total_amount ) AS total
1363 FROM civicrm_financial_trxn AS ft
1364 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1365 WHERE ceft.entity_table = 'civicrm_contribution'
1366 AND ceft.entity_id = {$contId}";
1367
1368 $result = CRM_Core_DAO::singleValueQuery($query);
1369 return $result;
1370 }
1371
1372 function _getFinancialItemAmount($contId) {
1373 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1374 $query = "SELECT
1375 SUM(amount)
1376 FROM civicrm_financial_item
1377 WHERE entity_table = 'civicrm_line_item'
1378 AND entity_id = {$lineItem}";
1379 $result = CRM_Core_DAO::singleValueQuery($query);
1380 return $result;
1381 }
1382
1383 function _checkFinancialItem($contId, $context) {
1384 if ($context != 'paylater') {
1385 $params = array (
1386 'entity_id' => $contId,
1387 'entity_table' => 'civicrm_contribution',
1388 );
1389 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1390 $entityParams = array(
1391 'financial_trxn_id' => $trxn['financial_trxn_id'],
1392 'entity_table' => 'civicrm_financial_item',
1393 );
1394 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1395 $params = array(
1396 'id' => $entityTrxn['entity_id'],
1397 );
1398 }
1399 if ($context == 'paylater') {
1400 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1401 foreach ($lineItems as $key=>$item) {
1402 $params = array(
1403 'entity_id' => $key,
1404 'entity_table' => 'civicrm_line_item',
1405 );
1406 $compareParams = array ('status_id' => 1);
1407 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1408 }
1409 }
1410 elseif ($context == 'refund') {
1411 $compareParams = array(
1412 'status_id' => 1,
1413 'financial_account_id' => 1,
1414 'amount' => -100,
1415 );
1416 }
1417 elseif ($context == 'cancelPending') {
1418 $compareParams = array(
1419 'status_id' => 3,
1420 'financial_account_id' => 1,
1421 'amount' => -100,
1422 );
1423 }
1424 elseif ($context == 'changeFinancial') {
1425 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1426 $params = array(
1427 'entity_id' => $lineKey,
1428 'amount' => -100,
1429 );
1430 $compareParams = array(
1431 'financial_account_id' => 1,
1432 );
1433 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1434 $params = array(
1435 'financial_account_id' => 3,
1436 'entity_id' => $lineKey,
1437 );
1438 $compareParams = array(
1439 'amount' => 100,
1440 );
1441 }
1442 if ($context != 'paylater') {
1443 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1444 }
1445 }
1446
1447 function _checkFinancialTrxn($contribution, $context) {
1448 $trxnParams = array(
1449 'entity_id' => $contribution['id'],
1450 'entity_table' => 'civicrm_contribution',
1451 );
1452 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1453 $params = array(
1454 'id' => $trxn['financial_trxn_id'],
1455 );
1456 if ($context == 'payLater') {
1457 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
1458 $compareParams = array(
1459 'status_id' => 1,
1460 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1461 );
1462 }
1463 elseif ($context == 'paymentInstrument') {
1464 $compareParams = array(
1465 'from_financial_account_id' => 6,
1466 'to_financial_account_id' => 7,
1467 );
1468 }
1469 elseif ($context == 'refund') {
1470 $compareParams = array(
1471 'to_financial_account_id' => 6,
1472 'total_amount' => -100,
1473 'status_id' => 7,
1474 );
1475 }
1476 elseif ($context == 'cancelPending') {
1477 $compareParams = array(
1478 'from_financial_account_id' => 7,
1479 'total_amount' => -100,
1480 'status_id' => 3,
1481 );
1482 }
1483 elseif ($context == 'changeFinancial') {
1484 $entityParams = array(
1485 'entity_id' => $contribution['id'],
1486 'entity_table' => 'civicrm_contribution',
1487 'amount' => -100,
1488 );
1489 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1490 $trxnParams1 = array(
1491 'id' => $trxn['financial_trxn_id'],
1492 );
1493 $compareParams = array(
1494 'to_financial_account_id' => 12,
1495 'total_amount' => -100,
1496 'status_id' => 1,
1497 );
1498 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
1499 $compareParams = array(
1500 'to_financial_account_id' => 12,
1501 'total_amount' => 100,
1502 'status_id' => 1,
1503 );
1504 }
1505
1506 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1507 }
1508
1509 function _addPaymentInstrument () {
1510 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1511 $optionParams = array(
1512 'option_group_id' => $gId,
1513 'label' => 'Test Card',
1514 'name' => 'Test Card',
1515 'value' => '6',
1516 'weight' => '6',
1517 'is_active' => 1,
1518 );
1519 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
1520 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
1521 $financialParams = array(
1522 'entity_table' => 'civicrm_option_value',
1523 'entity_id' => $optionValue['id'],
1524 'account_relationship' => $relationTypeId,
1525 'financial_account_id' => 7,
1526 );
1527 $financialType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1528 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1529 return $optionValue['values'][$optionValue['id']]['value'];
1530 }
1531
1532 function _checkFinancialRecords($params,$context) {
1533 $entityParams = array(
1534 'entity_id' => $params['id'],
1535 'entity_table' => 'civicrm_contribution',
1536 );
1537 if ($context == 'pending') {
1538 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1539 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1540 return;
1541 }
1542 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1543 $trxnParams = array(
1544 'id' => $trxn['financial_trxn_id'],
1545 );
1546 if ($context != 'online' && $context != 'payLater') {
1547 $compareParams = array(
1548 'to_financial_account_id' => 6,
1549 'total_amount' => 100,
1550 'status_id' => 1,
1551 );
1552 }
1553 if ($context == 'feeAmount') {
1554 $compareParams['fee_amount'] = 50;
1555 }
1556 elseif ($context == 'online') {
1557 $compareParams = array(
1558 'to_financial_account_id' => 12,
1559 'total_amount' => 100,
1560 'status_id' => 1,
1561 );
1562 }
1563 elseif ($context == 'payLater') {
1564 $compareParams = array(
1565 'to_financial_account_id' => 7,
1566 'total_amount' => 100,
1567 'status_id' => 2,
1568 );
1569 }
1570 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams);
1571 $entityParams = array(
1572 'financial_trxn_id' => $trxn['financial_trxn_id'],
1573 'entity_table' => 'civicrm_financial_item',
1574 );
1575 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1576 $fitemParams = array(
1577 'id' => $entityTrxn['entity_id'],
1578 );
1579 $compareParams = array(
1580 'amount' => 100,
1581 'status_id' => 1,
1582 'financial_account_id' => 1,
1583 );
1584 if ($context == 'payLater') {
1585 $compareParams = array(
1586 'amount' => 100,
1587 'status_id' => 3,
1588 'financial_account_id' => 1,
1589 );
1590 }
1591 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1592 if ($context == 'feeAmount') {
1593 $maxParams = array(
1594 'entity_id' => $params['id'],
1595 'entity_table' => 'civicrm_contribution',
1596 );
1597 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1598 $trxnParams = array(
1599 'id' => $maxTrxn['financial_trxn_id'],
1600 );
1601 $compareParams = array(
1602 'to_financial_account_id' => 5,
1603 'from_financial_account_id' => 6,
1604 'total_amount' => 50,
1605 'status_id' => 1,
1606 );
1607 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1608 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1609 $fitemParams = array(
1610 'entity_id' => $trxnId['financialTrxnId'],
1611 'entity_table' => 'civicrm_financial_trxn',
1612 );
1613 $compareParams = array(
1614 'amount' => 50,
1615 'status_id' => 1,
1616 'financial_account_id' => 5,
1617 );
1618 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1619 }
1620 }
1621 }
1622