CRM-15105 add test to demonstrate + fix other tests to use
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29require_once 'CiviTest/CiviMailUtils.php';
30
31
32/**
33 * Test APIv3 civicrm_contribute_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
37 */
38
39class api_v3_ContributionTest extends CiviUnitTestCase {
40
41 /**
42 * Assume empty database with just civicrm_data
43 */
44 protected $_individualId;
45 protected $_contribution;
4ab7d517 46 protected $_financialTypeId = 1;
6a488035
TO
47 protected $_apiversion;
48 protected $_entity = 'Contribution';
49 public $debug = 0;
50 protected $_params;
b7c9bc4c 51
6a488035
TO
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,
4ab7d517 62 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
63 'non_deductible_amount' => 10.00,
64 'fee_amount' => 5.00,
65 'net_amount' => 95.00,
66 'source' => 'SSF',
67 'contribution_status_id' => 1,
6a488035
TO
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(
6a488035
TO
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() {
6a488035
TO
94 $this->quickCleanup(array(
95 'civicrm_contribution',
502b3d42 96 'civicrm_contribution_soft',
6a488035
TO
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
6a488035
TO
109 function testGetContribution() {
110 $p = array(
111 'contact_id' => $this->_individualId,
112 'receive_date' => '2010-01-20',
113 'total_amount' => 100.00,
4ab7d517 114 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
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,
6a488035 122 );
2f45e1c2 123 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
124
125 $params = array(
126 'contribution_id' => $this->_contribution['id'],
6a488035 127 );
2f45e1c2 128 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 129 $financialParams['id'] = $this->_financialTypeId;
6a488035
TO
130 $default = null;
131 $financialType = CRM_Financial_BAO_FinancialType::retrieve($financialParams,$default);
2f45e1c2 132
6a488035 133 $this->assertEquals(1,$contribution['count']);
6a488035
TO
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
2f45e1c2 150 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
151
6a488035 152 // now we have 2 - test getcount
4ab7d517 153 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
154 $this->assertEquals(2, $contribution);
155 //test id only format
4ab7d517 156 $contribution = $this->callAPISuccess('contribution', 'get', array(
157 'id' => $this->_contribution['id'],
158 'format.only_id' => 1,
159 ));
6a488035
TO
160 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__);
161 //test id only format
2f45e1c2 162 $contribution = $this->callAPISuccess('contribution', 'get', array(
4ab7d517 163 'id' => $contribution2['id'],
164 'format.only_id' => 1,
165 ));
166 $this->assertEquals($contribution2['id'], $contribution);
6a488035 167 //test id as field
4ab7d517 168 $contribution = $this->callAPISuccess('contribution', 'get', array(
169 'id' => $this->_contribution['id'],
170 ));
6a488035 171 $this->assertEquals(1, $contribution['count'], 'In line ' . __LINE__);
4ab7d517 172
6a488035 173 //test get by contact id works
4ab7d517 174 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035
TO
175
176 $this->assertEquals(2, $contribution['count'], 'In line ' . __LINE__);
2f45e1c2 177 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 178 'id' => $this->_contribution['id'],
4ab7d517 179 ));
2f45e1c2 180 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 181 'id' => $contribution2['id'],
4ab7d517 182 ));
6a488035
TO
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,
4ab7d517 193 'contribution_type_id' => $this->_financialTypeId,
6a488035
TO
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,
6a488035 201 );
2f45e1c2 202 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
203
204 $params = array(
205 'contribution_id' => $this->_contribution['id'],
6a488035 206 );
2f45e1c2 207 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 208 $financialParams['id'] = $this->_financialTypeId;
6a488035
TO
209 $default = null;
210 $financialType = CRM_Financial_BAO_FinancialType::retrieve($financialParams,$default);
2f45e1c2 211
6a488035 212 $this->assertEquals(1,$contribution['count']);
6a488035 213 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
4ab7d517 214 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
215 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
6a488035
TO
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
2f45e1c2 228 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
6a488035 229
6a488035 230 // now we have 2 - test getcount
4ab7d517 231 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
232 $this->assertEquals(2, $contribution);
233 //test id only format
4ab7d517 234 $contribution = $this->callAPISuccess('contribution', 'get', array(
235 'id' => $this->_contribution['id'],
236 'format.only_id' => 1,
237 ));
6a488035
TO
238 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__);
239 //test id only format
4ab7d517 240 $contribution = $this->callAPISuccess('contribution', 'get', array(
241 'id' => $contribution2['id'],
242 'format.only_id' => 1,
243 ));
6a488035 244 $this->assertEquals($contribution2['id'], $contribution);
2f45e1c2 245 $contribution = $this->callAPISuccess('contribution', 'get', array(
6a488035
TO
246 'id' => $this->_contribution['id'],
247 ));
248 //test id as field
6a488035
TO
249 $this->assertEquals(1, $contribution['count'], 'In line ' . __LINE__);
250 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
251 //test get by contact id works
4ab7d517 252 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035
TO
253
254 $this->assertEquals(2, $contribution['count'], 'In line ' . __LINE__);
2f45e1c2 255 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 256 'id' => $this->_contribution['id'],
6a488035 257 ));
2f45e1c2 258 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 259 'id' => $contribution2['id'],
6a488035
TO
260 ));
261 }
262 ///////////////// civicrm_contribution_
263 function testCreateEmptyContributionIDUseDonation() {
264 $params = array(
265 'contribution_id' => FALSE,
266 'contact_id' => 1,
267 'total_amount' => 1,
6a488035
TO
268 'check_permissions' => false,
269 'financial_type_id' => 'Donation',
270 );
2f45e1c2 271 $contribution = $this->callAPISuccess('contribution', 'create', $params);
272 }
6a488035
TO
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,
6a488035
TO
283 'check_permissions' => false,
284 'contribution_type_id' => 3,
285 );
2f45e1c2 286 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4ab7d517 287 $contribution = $this->callAPISuccess('contribution', 'getsingle', array( 'id' => $contribution['id']));
6a488035
TO
288 $this->assertEquals(3, $contribution['financial_type_id']);
289 }
290
6a488035
TO
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
2f45e1c2 303 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 304 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
2f45e1c2 305 $check = $this->callAPISuccess($this->_entity, 'get', array(
4ab7d517 306 'return.custom_' . $ids['custom_field_id'] => 1,
307 'id' => $result['id'],
308 ));
6a488035
TO
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');
4ab7d517 323 $result = $this->callAPISuccess('Contribution', 'getfields', array());
6a488035
TO
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,
4ab7d517 338 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
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,
6a488035
TO
347 'skipLineItem' => 1,
348 );
349
2f45e1c2 350 $contribution = $this->callAPISuccess('contribution', 'create', $params);
351 $lineItems = $this->callAPISuccess('line_item','get',array(
6a488035
TO
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,
4ab7d517 367 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
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,
6a488035
TO
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
6a488035
TO
393 $description = "Create Contribution with Nested Line Items";
394 $subfile = "CreateWithNestedLineItems";
2f45e1c2 395 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__,__FILE__, $description, $subfile);
396
397 $lineItems = $this->callAPISuccess('line_item','get',array(
6a488035
TO
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,
6a488035
TO
415 );
416
4ab7d517 417 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
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__);
4ab7d517 425 $lineItems = $this->callAPISuccess('line_item','get',array(
6a488035
TO
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']);
4ab7d517 433 $lineItems = $this->callAPISuccess('line_item','get',array(
434 'entity_id' => $contribution['id'],
435 'entity_table' => 'civicrm_contribution',
436 'sequential' => 1,
6a488035
TO
437 ));
438 $this->assertEquals(1, $lineItems['count']);
439 $this->_checkFinancialRecords($contribution, 'offline');
440 $this->contributionGetnCheck($params, $contribution['id']);
441 }
f70a6752 442 /**
443 * test create with valid payment instument
6a488035
TO
444 */
445 function testCreateContributionWithPaymentInstrument() {
446 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813
CW
447 $contribution = $this->callAPISuccess('contribution', 'create', $params);
448 $contribution = $this->callAPISuccess('contribution','get', array(
449 'sequential' => 1,
450 'id' => $contribution['id']
451 ));
6a488035 452 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
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']);
6a488035
TO
462 }
463
464 function testGetContributionByPaymentInstrument() {
6a488035 465 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 466 $params2 = $this->_params + array('payment_instrument' => 'Cash');
4ab7d517 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'));
6a488035
TO
470 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
471 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
472 $this->assertEquals(1,$contribution['count']);
4ab7d517 473 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
6a488035
TO
474 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
475 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
476 $this->assertEquals(1, $contribution['count']);
4ab7d517 477 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument_id' => 5));
6a488035
TO
478 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
479 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
480 $this->assertEquals(1,$contribution['count']);
4ab7d517 481 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument' => 'EFT'));
6a488035 482 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
483 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
484 $this->assertEquals(1, $contribution['count']);
694769da 485 $contribution = $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
4ab7d517 486 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
6a488035
TO
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,
4ab7d517 501 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
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,
6a488035
TO
510 );
511
4ab7d517 512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
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 }
d5d148ab
EM
516
517 /*
518 * Create test with unique field name on source
519 */
520 function testCreateDefaultNow() {
521
522 $params = $this->_params;
523 unset($params['receive_date']);
524
525 $contribution = $this->callAPISuccess('contribution', 'create', $params);
526 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
527 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
528 }
529
6a488035
TO
530 /*
531 * Create test with unique field name on source
532 */
533 function testCreateContributionSourceInvalidContac() {
534
535 $params = array(
536 'contact_id' => 999,
537 'receive_date' => date('Ymd'),
538 'total_amount' => 100.00,
4ab7d517 539 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
540 'payment_instrument_id' => 1,
541 'non_deductible_amount' => 10.00,
542 'fee_amount' => 50.00,
543 'net_amount' => 90.00,
544 'trxn_id' => 12345,
545 'invoice_id' => 67890,
546 'contribution_source' => 'SSF',
547 'contribution_status_id' => 1,
6a488035
TO
548 );
549
4ab7d517 550 $contribution = $this->callAPIFailure('contribution', 'create', $params,
551 'contact_id is not valid : 999');
6a488035
TO
552 }
553
554 function testCreateContributionSourceInvalidContContac() {
555
556 $params = array(
557 'contribution_contact_id' => 999,
558 'receive_date' => date('Ymd'),
559 'total_amount' => 100.00,
4ab7d517 560 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
561 'payment_instrument_id' => 1,
562 'non_deductible_amount' => 10.00,
563 'fee_amount' => 50.00,
564 'net_amount' => 90.00,
565 'trxn_id' => 12345,
566 'invoice_id' => 67890,
567 'contribution_source' => 'SSF',
568 'contribution_status_id' => 1,
6a488035
TO
569 );
570
4ab7d517 571 $contribution = $this->callAPIFailure('contribution', 'create', $params,
572 'contact_id is not valid : 999', 'In line ' . __LINE__);
6a488035
TO
573 }
574
575 function testCreateContributionWithNote() {
576 $description = "Demonstrates creating contribution with Note Entity";
577 $subfile = "ContributionCreateWithNote";
578 $params = array(
579 'contact_id' => $this->_individualId,
580 'receive_date' => '2012-01-01',
581 'total_amount' => 100.00,
4ab7d517 582 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
583 'payment_instrument_id' => 1,
584 'non_deductible_amount' => 10.00,
585 'fee_amount' => 50.00,
586 'net_amount' => 90.00,
587 'trxn_id' => 12345,
588 'invoice_id' => 67890,
589 'source' => 'SSF',
590 'contribution_status_id' => 1,
6a488035
TO
591 'note' => 'my contribution note',
592 );
593
4ab7d517 594 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
595 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
6a488035 596 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 597 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
598 }
599
600 function testCreateContributionWithNoteUniqueNameAliases() {
601 $params = array(
602 'contact_id' => $this->_individualId,
603 'receive_date' => '2012-01-01',
604 'total_amount' => 100.00,
4ab7d517 605 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
606 'payment_instrument_id' => 1,
607 'non_deductible_amount' => 10.00,
608 'fee_amount' => 50.00,
609 'net_amount' => 90.00,
610 'trxn_id' => 12345,
611 'invoice_id' => 67890,
612 'source' => 'SSF',
613 'contribution_status_id' => 1,
6a488035
TO
614 'contribution_note' => 'my contribution note',
615 );
616
4ab7d517 617 $contribution = $this->callAPISuccess('contribution', 'create', $params);
618 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
6a488035 619 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 620 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
621 }
622 /*
623 * This is the test for creating soft credits - however a 'get' is not yet possible via API
624 * as the current BAO functions are contact-centric (from what I can find)
625 *
626 */
627 function testCreateContributionWithSoftCredt() {
628 $description = "Demonstrates creating contribution with SoftCredit";
629 $subfile = "ContributionCreateWithSoftCredit";
4ab7d517 630 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
6a488035
TO
631 $params = $this->_params + array(
632 'soft_credit_to' => $contact2['id'],
502b3d42 633
6a488035
TO
634 );
635
4ab7d517 636 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
637 // $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit_to', 'sequential' => 1));
6a488035
TO
638 // $this->assertEquals($contact2['id'], $result['values'][$result['id']]['soft_credit_to']) ;
639 // well - the above doesn't work yet so lets do SQL
640 $query = "SELECT count(*) FROM civicrm_contribution_soft WHERE contact_id = " . $contact2['id'];
a1c68fd2 641
6a488035
TO
642 $count = CRM_Core_DAO::singleValueQuery($query);
643 $this->assertEquals(1, $count);
644
4ab7d517 645 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
646 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
6a488035
TO
647 }
648
649 /**
650 * Test using example code
651 */
652 function testContributionCreateExample() {
653 //make sure at least on page exists since there is a truncate in tear down
4ab7d517 654 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 655 $this->assertAPISuccess($page);
3ec6e38d 656 require_once 'api/v3/examples/Contribution/Create.php';
fc928539 657 $result = contribution_create_example();
6a488035
TO
658 $this->assertAPISuccess($result);
659 $contributionId = $result['id'];
660 $expectedResult = contribution_create_expectedresult();
8e342a79 661 $this->checkArrayEquals($expectedResult, $result);
6a488035
TO
662 $this->contributionDelete($contributionId);
663 }
664
665 /*
666 * Function tests that additional financial records are created when fee amount is recorded
667 */
668 function testCreateContributionWithFee() {
669 $params = array(
670 'contact_id' => $this->_individualId,
671 'receive_date' => '20120511',
672 'total_amount' => 100.00,
673 'fee_amount' => 50,
674 'financial_type_id' => 1,
675 'trxn_id' => 12345,
676 'invoice_id' => 67890,
677 'source' => 'SSF',
678 'contribution_status_id' => 1,
6a488035
TO
679 );
680
4ab7d517 681 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
682 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
683 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
684 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
685 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
686 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
687 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
688 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__);
4ab7d517 689 $lineItems = $this->callAPISuccess('line_item','get',array(
690
6a488035
TO
691 'entity_id' => $contribution['id'],
692 'entity_table' => 'civicrm_contribution',
693 'sequential' => 1,
694 ));
695 $this->assertEquals(1, $lineItems['count']);
696 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
697 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ab7d517 698 $lineItems = $this->callAPISuccess('line_item','get',array(
699
6a488035
TO
700 'entity_id' => $contribution['id'],
701 'entity_table' => 'civicrm_contribution',
702 'sequential' => 1,
703 ));
704 $this->assertEquals(1, $lineItems['count']);
705 $this->_checkFinancialRecords($contribution, 'feeAmount');
706 }
707
708
f70a6752 709 /**
6a488035
TO
710 * Function tests that additional financial records are created when online contribution is created
711 */
712 function testCreateContributionOnline() {
713 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
4ab7d517 714 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams );
fc928539 715 $this->assertAPISuccess($contributionPage);
6a488035
TO
716 $params = array(
717 'contact_id' => $this->_individualId,
718 'receive_date' => '20120511',
719 'total_amount' => 100.00,
720 'financial_type_id' => 1,
721 'contribution_page_id' => $contributionPage['id'],
722 'payment_processor' => 1,
723 'trxn_id' => 12345,
724 'invoice_id' => 67890,
725 'source' => 'SSF',
726 'contribution_status_id' => 1,
4ab7d517 727
6a488035
TO
728 );
729
4ab7d517 730 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
731 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
732 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
733 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
734 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
735 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
736 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
737 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1, 'In line ' . __LINE__);
738 $this->_checkFinancialRecords($contribution, 'online');
739 }
740
f70a6752 741 /**
742 * in the interests of removing financial type / contribution type checks from
743 * legacy format function lets test that the api is doing this for us
744 */
745 function testCreateInvalidFinancialType() {
746 $params = $this->_params;
747 $params['financial_type_id'] = 99999;
748 $result = $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
749 }
750
4302618d 751 /**
752 * in the interests of removing financial type / contribution type checks from
753 * legacy format function lets test that the api is doing this for us
754 */
755 function testValidNamedFinancialType() {
756 $params = $this->_params;
757 $params['financial_type_id'] = 'Donation';
758 $result = $this->callAPISuccess($this->_entity, 'create', $params);
759 }
760
f70a6752 761 /**
6a488035
TO
762 * Function tests that additional financial records are created when online contribution with pay later option
763 * is created
764 */
765 function testCreateContributionPayLaterOnline() {
766 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
767 $this->_pageParams['is_pay_later'] = 1;
4ab7d517 768 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams );
fc928539 769 $this->assertAPISuccess($contributionPage);
6a488035
TO
770 $params = array(
771 'contact_id' => $this->_individualId,
772 'receive_date' => '20120511',
773 'total_amount' => 100.00,
774 'financial_type_id' => 1,
775 'contribution_page_id' => $contributionPage['id'],
776 'trxn_id' => 12345,
777 'is_pay_later' => 1,
778 'invoice_id' => 67890,
779 'source' => 'SSF',
780 'contribution_status_id' => 2,
4ab7d517 781
6a488035
TO
782 );
783
4ab7d517 784 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
785 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
786 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
787 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
788 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
789 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
790 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
791 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__);
792 $this->_checkFinancialRecords($contribution, 'payLater');
793 }
794
795 /*
796 * Function tests that additional financial records are created when online contribution with pending option
797 * is created
798 */
799 function testCreateContributionPendingOnline() {
800 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
4ab7d517 801 $contributionPage = $this->callAPISuccess( 'contribution_page', 'create', $this->_pageParams );
fc928539 802 $this->assertAPISuccess($contributionPage);
6a488035
TO
803 $params = array(
804 'contact_id' => $this->_individualId,
805 'receive_date' => '20120511',
806 'total_amount' => 100.00,
807 'financial_type_id' => 1,
808 'contribution_page_id' => $contributionPage['id'],
809 'trxn_id' => 12345,
810 'invoice_id' => 67890,
811 'source' => 'SSF',
812 'contribution_status_id' => 2,
6a488035
TO
813 );
814
4ab7d517 815 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
816 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
817 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00, 'In line ' . __LINE__);
818 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1, 'In line ' . __LINE__ );
819 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345, 'In line ' . __LINE__);
820 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890, 'In line ' . __LINE__);
821 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF', 'In line ' . __LINE__);
822 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2, 'In line ' . __LINE__);
823 $this->_checkFinancialRecords($contribution, 'pending');
824 }
825
826 /*
827 * Function tests that line items, financial records are updated when contribution amount is changed
828 */
829 function testCreateUpdateContributionChangeTotal() {
4ab7d517 830 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
831 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
832
6a488035
TO
833 'entity_id' => $contribution['id'],
834 'entity_table' => 'civicrm_contribution',
835 'sequential' => 1,
836 'return' => 'line_total',
837 ));
838 $this->assertEquals('100.00', $lineItems);
839 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
840 // Financial trxn SUM = 100 + 5 (fee)
841 $this->assertEquals('105.00', $trxnAmount);
842 $newParams = array(
4ab7d517 843
6a488035
TO
844 'id' => $contribution['id'],
845 'total_amount' => '125');
694769da 846 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
4ab7d517 847
848 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
6a488035 849
6a488035
TO
850 'entity_id' => $contribution['id'],
851 'entity_table' => 'civicrm_contribution',
852 'sequential' => 1,
853 'return' => 'line_total',
854 ));
855
856 $this->assertEquals('125.00', $lineItems);
857 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
858 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
859 // Financial trxn SUM = 125 + 5 (fee)
860 $this->assertEquals('130.00', $trxnAmount);
861 $this->assertEquals('125.00', $fitemAmount);
862 }
863
864 /*
865 * Function tests that line items, financial records are updated when pay later contribution is received
866 */
867 function testCreateUpdateContributionPayLater() {
868 $contribParams = array(
869 'contact_id' => $this->_individualId,
870 'receive_date' => '2012-01-01',
871 'total_amount' => 100.00,
4ab7d517 872 'financial_type_id' => $this->_financialTypeId,
6a488035 873 'payment_instrument_id' => 1,
8f39a111 874 'contribution_status_id' => 2,
875 'is_pay_later' => 1,
4ab7d517 876
6a488035 877 );
4ab7d517 878 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
879
880 $newParams = array_merge($contribParams, array(
c71ae314
PN
881 'id' => $contribution['id'],
882 'contribution_status_id' => 1,)
883 );
694769da 884 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
885 $contribution = $contribution['values'][$contribution['id']];
886 $this->assertEquals($contribution['contribution_status_id'],'1');
887 $this->_checkFinancialItem($contribution['id'], 'paylater');
888 $this->_checkFinancialTrxn($contribution, 'payLater');
889 }
890
891 /*
892 * Function tests that financial records are updated when Payment Instrument is changed
893 */
894 function testCreateUpdateContributionPaymentInstrument() {
895 $instrumentId = $this->_addPaymentInstrument();
896 $contribParams = array(
897 'contact_id' => $this->_individualId,
898 'total_amount' => 100.00,
4ab7d517 899 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
900 'payment_instrument_id' => 4,
901 'contribution_status_id' => 1,
4ab7d517 902
6a488035 903 );
4ab7d517 904 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
905
906 $newParams = array_merge($contribParams, array(
907 'id' => $contribution['id'],
908 'payment_instrument_id' => $instrumentId,)
909 );
694769da 910 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
fc928539 911 $this->assertAPISuccess($contribution);
4ecc6d4b 912 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
6a488035
TO
913 }
914
915 /*
916 * Function tests that financial records are added when Contribution is Refunded
917 */
918 function testCreateUpdateContributionRefund() {
919 $contribParams = array(
920 'contact_id' => $this->_individualId,
921 'receive_date' => '2012-01-01',
922 'total_amount' => 100.00,
4ab7d517 923 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
924 'payment_instrument_id' => 4,
925 'contribution_status_id' => 1,
4ab7d517 926
6a488035 927 );
4ab7d517 928 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
929 $newParams = array_merge($contribParams, array(
930 'id' => $contribution['id'],
931 'contribution_status_id' => 7,
932 )
933 );
934
694769da 935 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
936 $this->_checkFinancialTrxn($contribution, 'refund');
937 $this->_checkFinancialItem($contribution['id'], 'refund');
8f39a111 938 }
c71ae314
PN
939
940 /*
8f39a111 941 * Function tests invalid contribution status change
c71ae314
PN
942 */
943 function testCreateUpdateContributionInValidStatusChange() {
944 $contribParams = array(
945 'contact_id' => 1,
946 'receive_date' => '2012-01-01',
947 'total_amount' => 100.00,
948 'financial_type_id' => 1,
949 'payment_instrument_id' => 1,
950 'contribution_status_id' => 1,
c71ae314 951 );
4ab7d517 952 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
c71ae314
PN
953 $newParams = array_merge($contribParams, array(
954 'id' => $contribution['id'],
955 'contribution_status_id' => 2,
956 )
957 );
694769da 958 $contribution = $this->callAPIFailure('contribution', 'create', $newParams,
4ab7d517 959 ts('Cannot change contribution status from Completed to Pending.')
960 );
c71ae314 961
6a488035
TO
962 }
963
964 /*
965 * Function tests that financial records are added when Pending Contribution is Canceled
966 */
967 function testCreateUpdateContributionCancelPending() {
968 $contribParams = array(
969 'contact_id' => $this->_individualId,
970 'receive_date' => '2012-01-01',
971 'total_amount' => 100.00,
4ab7d517 972 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
973 'payment_instrument_id' => 1,
974 'contribution_status_id' => 2,
c71ae314 975 'is_pay_later' => 1,
4ab7d517 976
6a488035 977 );
4ab7d517 978 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
979 $newParams = array_merge($contribParams, array(
980 'id' => $contribution['id'],
981 'contribution_status_id' => 3,
982 )
983 );
694769da 984 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
985 $this->_checkFinancialTrxn($contribution, 'cancelPending');
986 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
987 }
988
989 /*
990 * Function tests that financial records are added when Financial Type is Changed
991 */
992 function testCreateUpdateContributionChangeFinancialType() {
993 $contribParams = array(
994 'contact_id' => $this->_individualId,
995 'receive_date' => '2012-01-01',
996 'total_amount' => 100.00,
997 'financial_type_id' => 1,
998 'payment_instrument_id' => 1,
999 'contribution_status_id' => 1,
4ab7d517 1000
6a488035 1001 );
4ab7d517 1002 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1003 $newParams = array_merge($contribParams, array(
1004 'id' => $contribution['id'],
1005 'financial_type_id' => 3,
1006 )
1007 );
694769da 1008 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1009 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1010 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1011 }
1012
694769da
VU
1013 /**
1014 * test that update does not change status id CRM-15105
1015 */
1016 function testCreateUpdateWithoutChangingPendingStatus() {
1017 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1018 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1019 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1020 $this->assertEquals(2, $contribution['contribution_status_id']);
1021 }
6a488035
TO
1022 //To Update Contribution
1023 //CHANGE: we require the API to do an incremental update
1024 function testCreateUpdateContribution() {
1025
4ab7d517 1026 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
6a488035
TO
1027 $old_params = array(
1028 'contribution_id' => $contributionID,
4ab7d517 1029
6a488035 1030 );
4ab7d517 1031 $original = $this->callAPISuccess('contribution', 'get', $old_params);
fc928539 1032 //Make sure it came back
1033 $this->assertAPISuccess($original, 'In line ' . __LINE__);
6a488035
TO
1034 $this->assertEquals($original['id'], $contributionID, 'In line ' . __LINE__);
1035 //set up list of old params, verify
1036
1037 //This should not be required on update:
1038 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1039 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1040 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1041 $old_source = $original['values'][$contributionID]['contribution_source'];
1042
1043 //note: current behavior is to return ISO. Is this
1044 //documented behavior? Is this correct
1045 $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date']));
1046
1047 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1048 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1049
1050 //check against values in CiviUnitTestCase::createContribution()
1051 $this->assertEquals($old_contact_id, $this->_individualId, 'In line ' . __LINE__);
1052 $this->assertEquals($old_fee_amount, 5.00, 'In line ' . __LINE__);
1053 $this->assertEquals($old_source, 'SSF', 'In line ' . __LINE__);
1054 $this->assertEquals($old_trxn_id, 212355, 'In line ' . __LINE__);
1055 $this->assertEquals($old_invoice_id, 'idofsh', 'In line ' . __LINE__);
1056 $params = array(
1057 'id' => $contributionID,
1058 'contact_id' => $this->_individualId,
1059 'total_amount' => 110.00,
4ab7d517 1060 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1061 'non_deductible_amount' => 10.00,
1062 'net_amount' => 100.00,
1063 'contribution_status_id' => 1,
1064 'note' => 'Donating for Nobel Cause',
4ab7d517 1065
6a488035
TO
1066 );
1067
4ab7d517 1068 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1069
1070 $new_params = array(
1071 'contribution_id' => $contribution['id'],
4ab7d517 1072
6a488035 1073 );
4ab7d517 1074 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
6a488035
TO
1075
1076 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
1077 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00, 'In line ' . __LINE__);
4ab7d517 1078 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId, 'In line ' . __LINE__ );
6a488035
TO
1079 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument, 'In line ' . __LINE__);
1080 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00, 'In line ' . __LINE__);
1081 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount, 'In line ' . __LINE__);
1082 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00, 'In line ' . __LINE__);
1083 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id, 'In line ' . __LINE__);
1084 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id, 'In line ' . __LINE__);
1085 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source, 'In line ' . __LINE__);
1086 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed', 'In line ' . __LINE__);
1087 $params = array(
1088 'contribution_id' => $contributionID,
4ab7d517 1089
6a488035 1090 );
4ab7d517 1091 $result = $this->callAPISuccess('contribution', 'delete', $params);
fc928539 1092 $this->assertAPISuccess($result, 'in line' . __LINE__);
6a488035
TO
1093 }
1094
1095 ///////////////// civicrm_contribution_delete methods
1096 function testDeleteEmptyParamsContribution() {
4ab7d517 1097 $params = array();
d0e1eff2 1098 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1099 }
1100
1101 function testDeleteParamsNotArrayContribution() {
1102 $params = 'contribution_id= 1';
d0e1eff2 1103 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1104 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1105 }
1106
1107 function testDeleteWrongParamContribution() {
1108 $params = array(
1109 'contribution_source' => 'SSF',
4ab7d517 1110
6a488035 1111 );
d0e1eff2 1112 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1113 }
1114
1115 function testDeleteContribution() {
1116
4ab7d517 1117 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
6a488035
TO
1118 $params = array(
1119 'id' => $contributionID,
6a488035 1120 );
4ab7d517 1121 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1122 }
1123
1124 /**
1125 * Test civicrm_contribution_search with empty params.
1126 * All available contributions expected.
1127 */
1128 function testSearchEmptyParams() {
4ab7d517 1129 $params = array();
6a488035
TO
1130
1131 $p = array(
1132 'contact_id' => $this->_individualId,
1133 'receive_date' => date('Ymd'),
1134 'total_amount' => 100.00,
4ab7d517 1135 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1136 'non_deductible_amount' => 10.00,
1137 'fee_amount' => 5.00,
1138 'net_amount' => 95.00,
1139 'trxn_id' => 23456,
1140 'invoice_id' => 78910,
1141 'source' => 'SSF',
1142 'contribution_status_id' => 1,
4ab7d517 1143
6a488035 1144 );
4ab7d517 1145 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1146
4ab7d517 1147 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1148 // We're taking the first element.
1149 $res = $result['values'][$contribution['id']];
1150
1151 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
1152 $this->assertEquals($p['total_amount'], $res['total_amount'], 'In line ' . __LINE__);
1153 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ );
1154 $this->assertEquals($p['net_amount'], $res['net_amount'], 'In line ' . __LINE__);
1155 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__);
1156 $this->assertEquals($p['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__);
1157 $this->assertEquals($p['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__);
1158 $this->assertEquals($p['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__);
1159 $this->assertEquals($p['source'], $res['contribution_source'], 'In line ' . __LINE__);
1160 // contribution_status_id = 1 => Completed
1161 $this->assertEquals('Completed', $res['contribution_status'], 'In line ' . __LINE__);
1162
1163 $this->contributionDelete($contribution['id']);
1164 }
1165
1166 /**
1167 * Test civicrm_contribution_search. Success expected.
1168 */
1169 function testSearch() {
1170 $p1 = array(
1171 'contact_id' => $this->_individualId,
1172 'receive_date' => date('Ymd'),
1173 'total_amount' => 100.00,
4ab7d517 1174 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1175 'non_deductible_amount' => 10.00,
1176 'contribution_status_id' => 1,
4ab7d517 1177
6a488035 1178 );
4ab7d517 1179 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1180
1181 $p2 = array(
1182 'contact_id' => $this->_individualId,
1183 'receive_date' => date('Ymd'),
1184 'total_amount' => 200.00,
4ab7d517 1185 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1186 'non_deductible_amount' => 20.00,
1187 'trxn_id' => 5454565,
1188 'invoice_id' => 1212124,
1189 'fee_amount' => 50.00,
1190 'net_amount' => 60.00,
1191 'contribution_status_id' => 2,
4ab7d517 1192
6a488035 1193 );
2f45e1c2 1194 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1195
1196 $params = array(
1197 'contribution_id' => $contribution2['id'],
4ab7d517 1198
6a488035 1199 );
2f45e1c2 1200 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1201 $res = $result['values'][$contribution2['id']];
1202
1203 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
1204 $this->assertEquals($p2['total_amount'], $res['total_amount'], 'In line ' . __LINE__);
1205 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ );
1206 $this->assertEquals($p2['net_amount'], $res['net_amount'], 'In line ' . __LINE__);
1207 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__);
1208 $this->assertEquals($p2['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__);
1209 $this->assertEquals($p2['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__);
1210 $this->assertEquals($p2['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__);
1211 // contribution_status_id = 2 => Pending
1212 $this->assertEquals('Pending', $res['contribution_status'], 'In line ' . __LINE__);
1213
1214 $this->contributionDelete($contribution1['id']);
1215 $this->contributionDelete($contribution2['id']);
1216 }
2f45e1c2 1217
0efa8efe 1218 /**
1219 * Test completing a transaction via the API
1220 *
1221 * Note that we are creating a logged in user because email goes out from
1222 * that person
1223 */
1224 function testCompleteTransaction() {
1225 $mut = new CiviMailUtils( $this, true );
1226 $this->createLoggedInUser();
1227 $params = array_merge($this->_params, array('contribution_status_id' => 1,));
1228 $contribution = $this->callAPISuccess('contribution','create', $params);
1229 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1230 'id' => $contribution['id'],
1231 )
1232 );
1233 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1234 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1235 $mut->checkMailLog(array(
1236 'Receipt - Contribution',
1237 'Please print this confirmation for your records.',
1238 ));
46fa5206
EM
1239 $mut->stop();
1240 }
1241
1242 /**
1243 * CRM-14151
1244 * Test completing a transaction via the API
1245 *
1246 * For wierd caching-y reasons this test performs differently in isolation than with other
1247 * tests.
1248 */
1249 function testCompleteTransactionWithReceiptDateSet() {
1250 $mut = new CiviMailUtils( $this, true );
1251 $this->createLoggedInUser();
1252 $params = array_merge($this->_params, array('contribution_status_id' => 1,'receipt_date' => 'now'));
1253 $contribution = $this->callAPISuccess('contribution','create', $params);
1254 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1255 'id' => $contribution['id'],
1256 )
1257 );
1258 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1259 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1260 $mut->checkMailLog(array(
1261 'Receipt - Contribution',
1262 'Please print this confirmation for your records.',
1263 ));
0efa8efe 1264 $mut->stop();
1265 }
1266
1267 /**
1268 * Test completing a transaction with an event via the API
1269 *
1270 * Note that we are creating a logged in user because email goes out from
1271 * that person
1272 */
1273 function testCompleteTransactionWithParticipantRecord() {
1274 $mut = new CiviMailUtils( $this, true );
1275 $mut->clearMessages();
1276 $this->createLoggedInUser();
1277 $contributionID = $this->createPendingParticipantContribution();
1278 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1279 'id' => $contributionID,
1280 )
1281 );
1282 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids['participant'], 'return' => 'participant_status_id'));
1283 $this->assertEquals(1, $participantStatus);
1284 $mut->checkMailLog(array(
1285 'Annual CiviCRM meet',
1286 'Event',
afc59fef 1287 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 1288 ));
1289 $mut->stop();
1290
1291 }
1292
2f45e1c2 1293 /**
6a488035
TO
1294 * Test sending a mail via the API
1295 */
1296 function testSendMail() {
1297 $mut = new CiviMailUtils( $this, true );
2f45e1c2 1298 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1299 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
4ab7d517 1300
6a488035
TO
1301 'id' => $contribution['id'],
1302 'receipt_from_email' => 'api@civicrm.org',
1303 )
1304 );
6a488035
TO
1305 $mut->checkMailLog(array(
1306 '$ 100.00',
1307 'Contribution Information',
1308 'Please print this confirmation for your records',
1309 ), array(
1310 'Event'
1311 )
1312 );
1313 $mut->stop();
1314 }
1315
2f45e1c2 1316 /**
6a488035
TO
1317 * Test sending a mail via the API
1318 */
1319 function testSendMailEvent() {
1320 $mut = new CiviMailUtils( $this, true );
2f45e1c2 1321 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1322 $event = $this->eventCreate(array(
6a488035
TO
1323 'is_email_confirm' => 1,
1324 'confirm_from_email' => 'test@civicrm.org',
1325 ));
1326 $this->_eventID = $event['id'];
1327 $participantParams = array(
1328 'contact_id' => $this->_individualId,
1329 'event_id' => $this->_eventID,
1330 'status_id' => 1,
1331 'role_id' => 1,
1332 // to ensure it matches later on
1333 'register_date' => '2007-07-21 00:00:00',
1334 'source' => 'Online Event Registration: API Testing',
4ab7d517 1335
6a488035 1336 );
2f45e1c2 1337 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1338 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
1339 'participant_id' => $participant['id'],
1340 'contribution_id' => $contribution['id'],
2f45e1c2 1341 ));
1342 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
4ab7d517 1343
6a488035
TO
1344 'id' => $contribution['id'],
1345 'receipt_from_email' => 'api@civicrm.org',
1346 )
1347 );
1348
6a488035
TO
1349 $mut->checkMailLog(array(
1350 'Annual CiviCRM meet',
1351 'Event',
1352 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1353 ), array(
1354
1355 )
1356 );
1357 $mut->stop();
1358 }
1359
4302618d 1360 /**
1361 * This function does a GET & compares the result against the $params
1362 * Use as a double check on Creates
1363 */
6a488035
TO
1364 function contributionGetnCheck($params, $id, $delete = 1) {
1365
4ab7d517 1366 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 1367 'id' => $id,
4ab7d517 1368
6a488035
TO
1369 ));
1370
1371 if ($delete) {
4ab7d517 1372 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 1373 }
fc928539 1374 $this->assertAPISuccess($contribution, 0, 'In line ' . __LINE__);
6a488035
TO
1375 $values = $contribution['values'][$contribution['id']];
1376 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1377 // this is not returned in id format
1378 unset($params['payment_instrument_id']);
1379 $params['contribution_source'] = $params['source'];
1380 unset($params['source']);
1381 foreach ($params as $key => $value) {
6a488035
TO
1382 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
1383 }
1384 }
1385
0efa8efe 1386 /**
1387 * Create a pending contribution & linked pending participant record
1388 * (along with an event)
1389 */
1390 function createPendingParticipantContribution(){
1391 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1392 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1393 $this->ids['participant'] = $participantID;
1394 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1395 $contribution = $this->callAPISuccess('contribution','create', $params);
1396 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1397 $lineItem = $this->callAPISuccess('line_item', 'get', array(
1398 'entity_id' => $contribution['id'],
1399 'entity_table' => 'civicrm_contribution',
1400 'api.line_item.create' => array(
1401 'entity_id' => $participantID,
1402 'entity_table' => 'civicrm_participant',
1403 ),
1404 ));
1405 return $contribution['id'];
1406 }
1407
6a488035
TO
1408 function _getFinancialTrxnAmount($contId) {
1409 $query = "SELECT
1410 SUM( ft.total_amount ) AS total
1411 FROM civicrm_financial_trxn AS ft
1412 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1413 WHERE ceft.entity_table = 'civicrm_contribution'
1414 AND ceft.entity_id = {$contId}";
1415
1416 $result = CRM_Core_DAO::singleValueQuery($query);
1417 return $result;
1418 }
1419
1420 function _getFinancialItemAmount($contId) {
1421 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1422 $query = "SELECT
1423 SUM(amount)
1424 FROM civicrm_financial_item
1425 WHERE entity_table = 'civicrm_line_item'
1426 AND entity_id = {$lineItem}";
1427 $result = CRM_Core_DAO::singleValueQuery($query);
1428 return $result;
1429 }
1430
1431 function _checkFinancialItem($contId, $context) {
1432 if ($context != 'paylater') {
1433 $params = array (
1434 'entity_id' => $contId,
1435 'entity_table' => 'civicrm_contribution',
1436 );
1437 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1438 $entityParams = array(
1439 'financial_trxn_id' => $trxn['financial_trxn_id'],
1440 'entity_table' => 'civicrm_financial_item',
1441 );
1442 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1443 $params = array(
1444 'id' => $entityTrxn['entity_id'],
1445 );
1446 }
1447 if ($context == 'paylater') {
1448 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1449 foreach ($lineItems as $key=>$item) {
1450 $params = array(
1451 'entity_id' => $key,
1452 'entity_table' => 'civicrm_line_item',
1453 );
1454 $compareParams = array ('status_id' => 1);
1455 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1456 }
1457 }
1458 elseif ($context == 'refund') {
1459 $compareParams = array(
1460 'status_id' => 1,
1461 'financial_account_id' => 1,
1462 'amount' => -100,
1463 );
1464 }
1465 elseif ($context == 'cancelPending') {
1466 $compareParams = array(
1467 'status_id' => 3,
1468 'financial_account_id' => 1,
1469 'amount' => -100,
1470 );
1471 }
1472 elseif ($context == 'changeFinancial') {
1473 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1474 $params = array(
1475 'entity_id' => $lineKey,
1476 'amount' => -100,
1477 );
1478 $compareParams = array(
1479 'financial_account_id' => 1,
1480 );
1481 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1482 $params = array(
1483 'financial_account_id' => 3,
1484 'entity_id' => $lineKey,
1485 );
1486 $compareParams = array(
1487 'amount' => 100,
1488 );
1489 }
1490 if ($context != 'paylater') {
1491 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1492 }
1493 }
1494
4ecc6d4b 1495 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
6a488035
TO
1496 $trxnParams = array(
1497 'entity_id' => $contribution['id'],
1498 'entity_table' => 'civicrm_contribution',
1499 );
1500 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1501 $params = array(
1502 'id' => $trxn['financial_trxn_id'],
1503 );
1504 if ($context == 'payLater') {
f743a6eb 1505 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
6a488035
TO
1506 $compareParams = array(
1507 'status_id' => 1,
1508 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1509 );
1510 }
6a488035
TO
1511 elseif ($context == 'refund') {
1512 $compareParams = array(
1513 'to_financial_account_id' => 6,
1514 'total_amount' => -100,
1515 'status_id' => 7,
1516 );
1517 }
1518 elseif ($context == 'cancelPending') {
1519 $compareParams = array(
1520 'from_financial_account_id' => 7,
1521 'total_amount' => -100,
1522 'status_id' => 3,
1523 );
1524 }
4ecc6d4b 1525 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
6a488035
TO
1526 $entityParams = array(
1527 'entity_id' => $contribution['id'],
1528 'entity_table' => 'civicrm_contribution',
1529 'amount' => -100,
1530 );
1531 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1532 $trxnParams1 = array(
1533 'id' => $trxn['financial_trxn_id'],
1534 );
1535 $compareParams = array(
6a488035
TO
1536 'total_amount' => -100,
1537 'status_id' => 1,
1538 );
4ecc6d4b
PN
1539 if ($context == 'paymentInstrument') {
1540 $compareParams += array(
1541 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1542 'payment_instrument_id' => 4,
1543 );
1544 }
1545 else {
1546 $compareParams['to_financial_account_id'] = 12;
1547 }
6a488035 1548 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
4ecc6d4b
PN
1549 $compareParams['total_amount'] = 100;
1550 if ($context == 'paymentInstrument') {
1551 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1552 $compareParams['payment_instrument_id'] = $instrumentId;
1553 }
1554 else {
1555 $compareParams['to_financial_account_id'] = 12;
1556 }
6a488035
TO
1557 }
1558
1559 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1560 }
1561
1562 function _addPaymentInstrument () {
1563 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1564 $optionParams = array(
1565 'option_group_id' => $gId,
1566 'label' => 'Test Card',
1567 'name' => 'Test Card',
1568 'value' => '6',
1569 'weight' => '6',
1570 'is_active' => 1,
6a488035 1571);
4ab7d517 1572 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
f743a6eb 1573 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
1574 $financialParams = array(
1575 'entity_table' => 'civicrm_option_value',
1576 'entity_id' => $optionValue['id'],
1577 'account_relationship' => $relationTypeId,
1578 'financial_account_id' => 7,
1579 );
1580 $financialType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1581 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1582 return $optionValue['values'][$optionValue['id']]['value'];
1583 }
1584
1585 function _checkFinancialRecords($params,$context) {
1586 $entityParams = array(
1587 'entity_id' => $params['id'],
1588 'entity_table' => 'civicrm_contribution',
1589 );
1590 if ($context == 'pending') {
1591 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1592 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1593 return;
1594 }
1595 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1596 $trxnParams = array(
1597 'id' => $trxn['financial_trxn_id'],
1598 );
1599 if ($context != 'online' && $context != 'payLater') {
1600 $compareParams = array(
1601 'to_financial_account_id' => 6,
1602 'total_amount' => 100,
1603 'status_id' => 1,
1604 );
1605 }
1606 if ($context == 'feeAmount') {
1607 $compareParams['fee_amount'] = 50;
1608 }
1609 elseif ($context == 'online') {
1610 $compareParams = array(
1611 'to_financial_account_id' => 12,
1612 'total_amount' => 100,
1613 'status_id' => 1,
1614 );
1615 }
1616 elseif ($context == 'payLater') {
1617 $compareParams = array(
1618 'to_financial_account_id' => 7,
1619 'total_amount' => 100,
1620 'status_id' => 2,
1621 );
1622 }
1623 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams);
1624 $entityParams = array(
1625 'financial_trxn_id' => $trxn['financial_trxn_id'],
1626 'entity_table' => 'civicrm_financial_item',
1627 );
1628 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1629 $fitemParams = array(
1630 'id' => $entityTrxn['entity_id'],
1631 );
1632 $compareParams = array(
1633 'amount' => 100,
1634 'status_id' => 1,
1635 'financial_account_id' => 1,
1636 );
1637 if ($context == 'payLater') {
1638 $compareParams = array(
1639 'amount' => 100,
1640 'status_id' => 3,
1641 'financial_account_id' => 1,
1642 );
1643 }
1644 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1645 if ($context == 'feeAmount') {
1646 $maxParams = array(
1647 'entity_id' => $params['id'],
1648 'entity_table' => 'civicrm_contribution',
1649 );
1650 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1651 $trxnParams = array(
1652 'id' => $maxTrxn['financial_trxn_id'],
1653 );
1654 $compareParams = array(
1655 'to_financial_account_id' => 5,
1656 'from_financial_account_id' => 6,
1657 'total_amount' => 50,
1658 'status_id' => 1,
1659 );
1660 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1661 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1662 $fitemParams = array(
1663 'entity_id' => $trxnId['financialTrxnId'],
1664 'entity_table' => 'civicrm_financial_trxn',
1665 );
1666 $compareParams = array(
1667 'amount' => 50,
1668 'status_id' => 1,
1669 'financial_account_id' => 5,
1670 );
1671 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1672 }
1673 }
1674}
1675