Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-21-08-12-12
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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']);
4ab7d517 485 $contribution = $this->callAPISuccess('contribution', 'update', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
486 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
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'));
bcc03b98 631 $softparams = array(
632 'contact_id' => $contact2['id'],
633 'amount' => 50,
634 'soft_credit_type_id' => 3
6a488035
TO
635 );
636
bcc03b98 637 $params = $this->_params + array('soft_credit' => array(1 => $softparams));
4ab7d517 638 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
bcc03b98 639 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
a1c68fd2 640
bcc03b98 641 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
642 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
643 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a488035 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');
4ab7d517 846 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
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 );
4ab7d517 884 $contribution = $this->callAPISuccess('contribution', 'update', $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 );
4ab7d517 910 $contribution = $this->callAPISuccess('contribution', 'update', $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
4ab7d517 935 $contribution = $this->callAPISuccess('contribution', 'update', $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 );
4ab7d517 958 $contribution = $this->callAPIFailure('contribution', 'update', $newParams,
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 );
4ab7d517 984 $contribution = $this->callAPISuccess('contribution', 'update', $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 );
4ab7d517 1008 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
6a488035
TO
1009 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1010 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1011 }
1012
1013 //To Update Contribution
1014 //CHANGE: we require the API to do an incremental update
1015 function testCreateUpdateContribution() {
1016
4ab7d517 1017 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
6a488035
TO
1018 $old_params = array(
1019 'contribution_id' => $contributionID,
4ab7d517 1020
6a488035 1021 );
4ab7d517 1022 $original = $this->callAPISuccess('contribution', 'get', $old_params);
fc928539 1023 //Make sure it came back
1024 $this->assertAPISuccess($original, 'In line ' . __LINE__);
6a488035
TO
1025 $this->assertEquals($original['id'], $contributionID, 'In line ' . __LINE__);
1026 //set up list of old params, verify
1027
1028 //This should not be required on update:
1029 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1030 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1031 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1032 $old_source = $original['values'][$contributionID]['contribution_source'];
1033
1034 //note: current behavior is to return ISO. Is this
1035 //documented behavior? Is this correct
1036 $old_receive_date = date('Ymd', strtotime($original['values'][$contributionID]['receive_date']));
1037
1038 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1039 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1040
1041 //check against values in CiviUnitTestCase::createContribution()
1042 $this->assertEquals($old_contact_id, $this->_individualId, 'In line ' . __LINE__);
1043 $this->assertEquals($old_fee_amount, 5.00, 'In line ' . __LINE__);
1044 $this->assertEquals($old_source, 'SSF', 'In line ' . __LINE__);
1045 $this->assertEquals($old_trxn_id, 212355, 'In line ' . __LINE__);
1046 $this->assertEquals($old_invoice_id, 'idofsh', 'In line ' . __LINE__);
1047 $params = array(
1048 'id' => $contributionID,
1049 'contact_id' => $this->_individualId,
1050 'total_amount' => 110.00,
4ab7d517 1051 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1052 'non_deductible_amount' => 10.00,
1053 'net_amount' => 100.00,
1054 'contribution_status_id' => 1,
1055 'note' => 'Donating for Nobel Cause',
4ab7d517 1056
6a488035
TO
1057 );
1058
4ab7d517 1059 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1060
1061 $new_params = array(
1062 'contribution_id' => $contribution['id'],
4ab7d517 1063
6a488035 1064 );
4ab7d517 1065 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
6a488035
TO
1066
1067 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId, 'In line ' . __LINE__);
1068 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00, 'In line ' . __LINE__);
4ab7d517 1069 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId, 'In line ' . __LINE__ );
6a488035
TO
1070 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument, 'In line ' . __LINE__);
1071 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00, 'In line ' . __LINE__);
1072 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount, 'In line ' . __LINE__);
1073 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00, 'In line ' . __LINE__);
1074 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id, 'In line ' . __LINE__);
1075 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id, 'In line ' . __LINE__);
1076 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source, 'In line ' . __LINE__);
1077 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed', 'In line ' . __LINE__);
1078 $params = array(
1079 'contribution_id' => $contributionID,
4ab7d517 1080
6a488035 1081 );
4ab7d517 1082 $result = $this->callAPISuccess('contribution', 'delete', $params);
fc928539 1083 $this->assertAPISuccess($result, 'in line' . __LINE__);
6a488035
TO
1084 }
1085
1086 ///////////////// civicrm_contribution_delete methods
1087 function testDeleteEmptyParamsContribution() {
4ab7d517 1088 $params = array();
d0e1eff2 1089 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1090 }
1091
1092 function testDeleteParamsNotArrayContribution() {
1093 $params = 'contribution_id= 1';
d0e1eff2 1094 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1095 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1096 }
1097
1098 function testDeleteWrongParamContribution() {
1099 $params = array(
1100 'contribution_source' => 'SSF',
4ab7d517 1101
6a488035 1102 );
d0e1eff2 1103 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1104 }
1105
1106 function testDeleteContribution() {
1107
4ab7d517 1108 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
6a488035
TO
1109 $params = array(
1110 'id' => $contributionID,
6a488035 1111 );
4ab7d517 1112 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1113 }
1114
1115 /**
1116 * Test civicrm_contribution_search with empty params.
1117 * All available contributions expected.
1118 */
1119 function testSearchEmptyParams() {
4ab7d517 1120 $params = array();
6a488035
TO
1121
1122 $p = array(
1123 'contact_id' => $this->_individualId,
1124 'receive_date' => date('Ymd'),
1125 'total_amount' => 100.00,
4ab7d517 1126 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1127 'non_deductible_amount' => 10.00,
1128 'fee_amount' => 5.00,
1129 'net_amount' => 95.00,
1130 'trxn_id' => 23456,
1131 'invoice_id' => 78910,
1132 'source' => 'SSF',
1133 'contribution_status_id' => 1,
4ab7d517 1134
6a488035 1135 );
4ab7d517 1136 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1137
4ab7d517 1138 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1139 // We're taking the first element.
1140 $res = $result['values'][$contribution['id']];
1141
1142 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
1143 $this->assertEquals($p['total_amount'], $res['total_amount'], 'In line ' . __LINE__);
1144 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ );
1145 $this->assertEquals($p['net_amount'], $res['net_amount'], 'In line ' . __LINE__);
1146 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__);
1147 $this->assertEquals($p['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__);
1148 $this->assertEquals($p['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__);
1149 $this->assertEquals($p['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__);
1150 $this->assertEquals($p['source'], $res['contribution_source'], 'In line ' . __LINE__);
1151 // contribution_status_id = 1 => Completed
1152 $this->assertEquals('Completed', $res['contribution_status'], 'In line ' . __LINE__);
1153
1154 $this->contributionDelete($contribution['id']);
1155 }
1156
1157 /**
1158 * Test civicrm_contribution_search. Success expected.
1159 */
1160 function testSearch() {
1161 $p1 = array(
1162 'contact_id' => $this->_individualId,
1163 'receive_date' => date('Ymd'),
1164 'total_amount' => 100.00,
4ab7d517 1165 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1166 'non_deductible_amount' => 10.00,
1167 'contribution_status_id' => 1,
4ab7d517 1168
6a488035 1169 );
4ab7d517 1170 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1171
1172 $p2 = array(
1173 'contact_id' => $this->_individualId,
1174 'receive_date' => date('Ymd'),
1175 'total_amount' => 200.00,
4ab7d517 1176 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1177 'non_deductible_amount' => 20.00,
1178 'trxn_id' => 5454565,
1179 'invoice_id' => 1212124,
1180 'fee_amount' => 50.00,
1181 'net_amount' => 60.00,
1182 'contribution_status_id' => 2,
4ab7d517 1183
6a488035 1184 );
2f45e1c2 1185 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1186
1187 $params = array(
1188 'contribution_id' => $contribution2['id'],
4ab7d517 1189
6a488035 1190 );
2f45e1c2 1191 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1192 $res = $result['values'][$contribution2['id']];
1193
1194 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
1195 $this->assertEquals($p2['total_amount'], $res['total_amount'], 'In line ' . __LINE__);
1196 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'], 'In line ' . __LINE__ );
1197 $this->assertEquals($p2['net_amount'], $res['net_amount'], 'In line ' . __LINE__);
1198 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount'], 'In line ' . __LINE__);
1199 $this->assertEquals($p2['fee_amount'], $res['fee_amount'], 'In line ' . __LINE__);
1200 $this->assertEquals($p2['trxn_id'], $res['trxn_id'], 'In line ' . __LINE__);
1201 $this->assertEquals($p2['invoice_id'], $res['invoice_id'], 'In line ' . __LINE__);
1202 // contribution_status_id = 2 => Pending
1203 $this->assertEquals('Pending', $res['contribution_status'], 'In line ' . __LINE__);
1204
1205 $this->contributionDelete($contribution1['id']);
1206 $this->contributionDelete($contribution2['id']);
1207 }
2f45e1c2 1208
0efa8efe 1209 /**
1210 * Test completing a transaction via the API
1211 *
1212 * Note that we are creating a logged in user because email goes out from
1213 * that person
1214 */
1215 function testCompleteTransaction() {
1216 $mut = new CiviMailUtils( $this, true );
1217 $this->createLoggedInUser();
1218 $params = array_merge($this->_params, array('contribution_status_id' => 1,));
1219 $contribution = $this->callAPISuccess('contribution','create', $params);
1220 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1221 'id' => $contribution['id'],
1222 )
1223 );
1224 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1225 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1226 $mut->checkMailLog(array(
1227 'Receipt - Contribution',
1228 'Please print this confirmation for your records.',
1229 ));
46fa5206
EM
1230 $mut->stop();
1231 }
1232
1233 /**
1234 * CRM-14151
1235 * Test completing a transaction via the API
1236 *
1237 * For wierd caching-y reasons this test performs differently in isolation than with other
1238 * tests.
1239 */
1240 function testCompleteTransactionWithReceiptDateSet() {
1241 $mut = new CiviMailUtils( $this, true );
1242 $this->createLoggedInUser();
1243 $params = array_merge($this->_params, array('contribution_status_id' => 1,'receipt_date' => 'now'));
1244 $contribution = $this->callAPISuccess('contribution','create', $params);
1245 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1246 'id' => $contribution['id'],
1247 )
1248 );
1249 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1250 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1251 $mut->checkMailLog(array(
1252 'Receipt - Contribution',
1253 'Please print this confirmation for your records.',
1254 ));
0efa8efe 1255 $mut->stop();
1256 }
1257
1258 /**
1259 * Test completing a transaction with an event via the API
1260 *
1261 * Note that we are creating a logged in user because email goes out from
1262 * that person
1263 */
1264 function testCompleteTransactionWithParticipantRecord() {
1265 $mut = new CiviMailUtils( $this, true );
1266 $mut->clearMessages();
1267 $this->createLoggedInUser();
1268 $contributionID = $this->createPendingParticipantContribution();
1269 $apiResult = $this->callAPISuccess('contribution', 'completetransaction', array(
1270 'id' => $contributionID,
1271 )
1272 );
1273 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->ids['participant'], 'return' => 'participant_status_id'));
1274 $this->assertEquals(1, $participantStatus);
1275 $mut->checkMailLog(array(
1276 'Annual CiviCRM meet',
1277 'Event',
afc59fef 1278 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 1279 ));
1280 $mut->stop();
1281
1282 }
1283
2f45e1c2 1284 /**
6a488035
TO
1285 * Test sending a mail via the API
1286 */
1287 function testSendMail() {
1288 $mut = new CiviMailUtils( $this, true );
2f45e1c2 1289 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1290 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
4ab7d517 1291
6a488035
TO
1292 'id' => $contribution['id'],
1293 'receipt_from_email' => 'api@civicrm.org',
1294 )
1295 );
6a488035
TO
1296 $mut->checkMailLog(array(
1297 '$ 100.00',
1298 'Contribution Information',
1299 'Please print this confirmation for your records',
1300 ), array(
1301 'Event'
1302 )
1303 );
1304 $mut->stop();
1305 }
1306
2f45e1c2 1307 /**
6a488035
TO
1308 * Test sending a mail via the API
1309 */
1310 function testSendMailEvent() {
1311 $mut = new CiviMailUtils( $this, true );
2f45e1c2 1312 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1313 $event = $this->eventCreate(array(
6a488035
TO
1314 'is_email_confirm' => 1,
1315 'confirm_from_email' => 'test@civicrm.org',
1316 ));
1317 $this->_eventID = $event['id'];
1318 $participantParams = array(
1319 'contact_id' => $this->_individualId,
1320 'event_id' => $this->_eventID,
1321 'status_id' => 1,
1322 'role_id' => 1,
1323 // to ensure it matches later on
1324 'register_date' => '2007-07-21 00:00:00',
1325 'source' => 'Online Event Registration: API Testing',
4ab7d517 1326
6a488035 1327 );
2f45e1c2 1328 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1329 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
1330 'participant_id' => $participant['id'],
1331 'contribution_id' => $contribution['id'],
2f45e1c2 1332 ));
1333 $apiResult = $this->callAPISuccess('contribution', 'sendconfirmation', array(
4ab7d517 1334
6a488035
TO
1335 'id' => $contribution['id'],
1336 'receipt_from_email' => 'api@civicrm.org',
1337 )
1338 );
1339
6a488035
TO
1340 $mut->checkMailLog(array(
1341 'Annual CiviCRM meet',
1342 'Event',
1343 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1344 ), array(
1345
1346 )
1347 );
1348 $mut->stop();
1349 }
1350
4302618d 1351 /**
1352 * This function does a GET & compares the result against the $params
1353 * Use as a double check on Creates
1354 */
6a488035
TO
1355 function contributionGetnCheck($params, $id, $delete = 1) {
1356
4ab7d517 1357 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 1358 'id' => $id,
4ab7d517 1359
6a488035
TO
1360 ));
1361
1362 if ($delete) {
4ab7d517 1363 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 1364 }
fc928539 1365 $this->assertAPISuccess($contribution, 0, 'In line ' . __LINE__);
6a488035
TO
1366 $values = $contribution['values'][$contribution['id']];
1367 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1368 // this is not returned in id format
1369 unset($params['payment_instrument_id']);
1370 $params['contribution_source'] = $params['source'];
1371 unset($params['source']);
1372 foreach ($params as $key => $value) {
6a488035
TO
1373 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
1374 }
1375 }
1376
0efa8efe 1377 /**
1378 * Create a pending contribution & linked pending participant record
1379 * (along with an event)
1380 */
1381 function createPendingParticipantContribution(){
1382 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1383 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
1384 $this->ids['participant'] = $participantID;
1385 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1386 $contribution = $this->callAPISuccess('contribution','create', $params);
1387 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
1388 $lineItem = $this->callAPISuccess('line_item', 'get', array(
1389 'entity_id' => $contribution['id'],
1390 'entity_table' => 'civicrm_contribution',
1391 'api.line_item.create' => array(
1392 'entity_id' => $participantID,
1393 'entity_table' => 'civicrm_participant',
1394 ),
1395 ));
1396 return $contribution['id'];
1397 }
1398
6a488035
TO
1399 function _getFinancialTrxnAmount($contId) {
1400 $query = "SELECT
1401 SUM( ft.total_amount ) AS total
1402 FROM civicrm_financial_trxn AS ft
1403 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1404 WHERE ceft.entity_table = 'civicrm_contribution'
1405 AND ceft.entity_id = {$contId}";
1406
1407 $result = CRM_Core_DAO::singleValueQuery($query);
1408 return $result;
1409 }
1410
1411 function _getFinancialItemAmount($contId) {
1412 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1413 $query = "SELECT
1414 SUM(amount)
1415 FROM civicrm_financial_item
1416 WHERE entity_table = 'civicrm_line_item'
1417 AND entity_id = {$lineItem}";
1418 $result = CRM_Core_DAO::singleValueQuery($query);
1419 return $result;
1420 }
1421
1422 function _checkFinancialItem($contId, $context) {
1423 if ($context != 'paylater') {
1424 $params = array (
1425 'entity_id' => $contId,
1426 'entity_table' => 'civicrm_contribution',
1427 );
1428 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1429 $entityParams = array(
1430 'financial_trxn_id' => $trxn['financial_trxn_id'],
1431 'entity_table' => 'civicrm_financial_item',
1432 );
1433 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1434 $params = array(
1435 'id' => $entityTrxn['entity_id'],
1436 );
1437 }
1438 if ($context == 'paylater') {
1439 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1440 foreach ($lineItems as $key=>$item) {
1441 $params = array(
1442 'entity_id' => $key,
1443 'entity_table' => 'civicrm_line_item',
1444 );
1445 $compareParams = array ('status_id' => 1);
1446 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1447 }
1448 }
1449 elseif ($context == 'refund') {
1450 $compareParams = array(
1451 'status_id' => 1,
1452 'financial_account_id' => 1,
1453 'amount' => -100,
1454 );
1455 }
1456 elseif ($context == 'cancelPending') {
1457 $compareParams = array(
1458 'status_id' => 3,
1459 'financial_account_id' => 1,
1460 'amount' => -100,
1461 );
1462 }
1463 elseif ($context == 'changeFinancial') {
1464 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1465 $params = array(
1466 'entity_id' => $lineKey,
1467 'amount' => -100,
1468 );
1469 $compareParams = array(
1470 'financial_account_id' => 1,
1471 );
1472 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1473 $params = array(
1474 'financial_account_id' => 3,
1475 'entity_id' => $lineKey,
1476 );
1477 $compareParams = array(
1478 'amount' => 100,
1479 );
1480 }
1481 if ($context != 'paylater') {
1482 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1483 }
1484 }
1485
4ecc6d4b 1486 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
6a488035
TO
1487 $trxnParams = array(
1488 'entity_id' => $contribution['id'],
1489 'entity_table' => 'civicrm_contribution',
1490 );
1491 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1492 $params = array(
1493 'id' => $trxn['financial_trxn_id'],
1494 );
1495 if ($context == 'payLater') {
f743a6eb 1496 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
6a488035
TO
1497 $compareParams = array(
1498 'status_id' => 1,
1499 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1500 );
1501 }
6a488035
TO
1502 elseif ($context == 'refund') {
1503 $compareParams = array(
1504 'to_financial_account_id' => 6,
1505 'total_amount' => -100,
1506 'status_id' => 7,
1507 );
1508 }
1509 elseif ($context == 'cancelPending') {
1510 $compareParams = array(
1511 'from_financial_account_id' => 7,
1512 'total_amount' => -100,
1513 'status_id' => 3,
1514 );
1515 }
4ecc6d4b 1516 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
6a488035
TO
1517 $entityParams = array(
1518 'entity_id' => $contribution['id'],
1519 'entity_table' => 'civicrm_contribution',
1520 'amount' => -100,
1521 );
1522 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1523 $trxnParams1 = array(
1524 'id' => $trxn['financial_trxn_id'],
1525 );
1526 $compareParams = array(
6a488035
TO
1527 'total_amount' => -100,
1528 'status_id' => 1,
1529 );
4ecc6d4b
PN
1530 if ($context == 'paymentInstrument') {
1531 $compareParams += array(
1532 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1533 'payment_instrument_id' => 4,
1534 );
1535 }
1536 else {
1537 $compareParams['to_financial_account_id'] = 12;
1538 }
6a488035 1539 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
4ecc6d4b
PN
1540 $compareParams['total_amount'] = 100;
1541 if ($context == 'paymentInstrument') {
1542 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1543 $compareParams['payment_instrument_id'] = $instrumentId;
1544 }
1545 else {
1546 $compareParams['to_financial_account_id'] = 12;
1547 }
6a488035
TO
1548 }
1549
1550 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1551 }
1552
1553 function _addPaymentInstrument () {
1554 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1555 $optionParams = array(
1556 'option_group_id' => $gId,
1557 'label' => 'Test Card',
1558 'name' => 'Test Card',
1559 'value' => '6',
1560 'weight' => '6',
1561 'is_active' => 1,
6a488035 1562);
4ab7d517 1563 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
f743a6eb 1564 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
1565 $financialParams = array(
1566 'entity_table' => 'civicrm_option_value',
1567 'entity_id' => $optionValue['id'],
1568 'account_relationship' => $relationTypeId,
1569 'financial_account_id' => 7,
1570 );
1571 $financialType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
1572 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1573 return $optionValue['values'][$optionValue['id']]['value'];
1574 }
1575
1576 function _checkFinancialRecords($params,$context) {
1577 $entityParams = array(
1578 'entity_id' => $params['id'],
1579 'entity_table' => 'civicrm_contribution',
1580 );
1581 if ($context == 'pending') {
1582 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1583 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1584 return;
1585 }
1586 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1587 $trxnParams = array(
1588 'id' => $trxn['financial_trxn_id'],
1589 );
1590 if ($context != 'online' && $context != 'payLater') {
1591 $compareParams = array(
1592 'to_financial_account_id' => 6,
1593 'total_amount' => 100,
1594 'status_id' => 1,
1595 );
1596 }
1597 if ($context == 'feeAmount') {
1598 $compareParams['fee_amount'] = 50;
1599 }
1600 elseif ($context == 'online') {
1601 $compareParams = array(
1602 'to_financial_account_id' => 12,
1603 'total_amount' => 100,
1604 'status_id' => 1,
1605 );
1606 }
1607 elseif ($context == 'payLater') {
1608 $compareParams = array(
1609 'to_financial_account_id' => 7,
1610 'total_amount' => 100,
1611 'status_id' => 2,
1612 );
1613 }
1614 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams,$compareParams);
1615 $entityParams = array(
1616 'financial_trxn_id' => $trxn['financial_trxn_id'],
1617 'entity_table' => 'civicrm_financial_item',
1618 );
1619 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1620 $fitemParams = array(
1621 'id' => $entityTrxn['entity_id'],
1622 );
1623 $compareParams = array(
1624 'amount' => 100,
1625 'status_id' => 1,
1626 'financial_account_id' => 1,
1627 );
1628 if ($context == 'payLater') {
1629 $compareParams = array(
1630 'amount' => 100,
1631 'status_id' => 3,
1632 'financial_account_id' => 1,
1633 );
1634 }
1635 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1636 if ($context == 'feeAmount') {
1637 $maxParams = array(
1638 'entity_id' => $params['id'],
1639 'entity_table' => 'civicrm_contribution',
1640 );
1641 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1642 $trxnParams = array(
1643 'id' => $maxTrxn['financial_trxn_id'],
1644 );
1645 $compareParams = array(
1646 'to_financial_account_id' => 5,
1647 'from_financial_account_id' => 6,
1648 'total_amount' => 50,
1649 'status_id' => 1,
1650 );
1651 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1652 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1653 $fitemParams = array(
1654 'entity_id' => $trxnId['financialTrxnId'],
1655 'entity_table' => 'civicrm_financial_trxn',
1656 );
1657 $compareParams = array(
1658 'amount' => 50,
1659 'status_id' => 1,
1660 'financial_account_id' => 5,
1661 );
1662 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1663 }
1664 }
1665}
1666