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