api example updates
[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;
858d0bf8
EM
51 protected $_ids = array();
52 protected $_pageParams = array();
b7c9bc4c 53
6a488035
TO
54 function setUp() {
55 parent::setUp();
56
57 $this->_apiversion = 3;
58 $this->_individualId = $this->individualCreate();
59 $paymentProcessor = $this->processorCreate();
60 $this->_params = array(
61 'contact_id' => $this->_individualId,
62 'receive_date' => '20120511',
63 'total_amount' => 100.00,
4ab7d517 64 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
65 'non_deductible_amount' => 10.00,
66 'fee_amount' => 5.00,
67 'net_amount' => 95.00,
68 'source' => 'SSF',
69 'contribution_status_id' => 1,
6a488035
TO
70 );
71 $this->_processorParams = array(
72 'domain_id' => 1,
73 'name' => 'Dummy',
74 'payment_processor_type_id' => 10,
75 'financial_account_id' => 12,
76 'is_active' => 1,
77 'user_name' => '',
78 'url_site' => 'http://dummy.com',
79 'url_recur' => 'http://dummy.com',
80 'billing_mode' => 1,
81 );
82 $this->_pageParams = array(
6a488035
TO
83 'title' => 'Test Contribution Page',
84 'financial_type_id' => 1,
85 'currency' => 'USD',
86 'financial_account_id' => 1,
87 'payment_processor' => $paymentProcessor->id,
88 'is_active' => 1,
89 'is_allow_other_amount' => 1,
90 'min_amount' => 10,
91 'max_amount' => 1000,
92 );
93 }
94
95 function tearDown() {
39d632fd 96 $this->quickCleanUpFinancialEntities();
6a488035
TO
97 }
98
6a488035
TO
99 function testGetContribution() {
100 $p = array(
101 'contact_id' => $this->_individualId,
102 'receive_date' => '2010-01-20',
103 'total_amount' => 100.00,
4ab7d517 104 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
105 'non_deductible_amount' => 10.00,
106 'fee_amount' => 5.00,
107 'net_amount' => 95.00,
108 'trxn_id' => 23456,
109 'invoice_id' => 78910,
110 'source' => 'SSF',
111 'contribution_status_id' => 1,
6a488035 112 );
2f45e1c2 113 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
114
115 $params = array(
116 'contribution_id' => $this->_contribution['id'],
6a488035 117 );
e0e3c51b 118
2f45e1c2 119 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 120 $financialParams['id'] = $this->_financialTypeId;
6a488035 121 $default = null;
858d0bf8 122 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 123
e0e3c51b 124 $this->assertEquals(1, $contribution['count']);
2bfae985 125 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
6a488035
TO
126 // note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
127 // passing back a string rather than an id seems like an error / cruft - & if it is to be introduced we should discuss
128 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
129 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
130 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
131 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
132 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
133 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
134 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
135 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
136 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
6a488035
TO
137 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
138 $p['trxn_id'] = '3847';
139 $p['invoice_id'] = '3847';
140
2f45e1c2 141 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
142
6a488035 143 // now we have 2 - test getcount
4ab7d517 144 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
145 $this->assertEquals(2, $contribution);
146 //test id only format
4ab7d517 147 $contribution = $this->callAPISuccess('contribution', 'get', array(
148 'id' => $this->_contribution['id'],
149 'format.only_id' => 1,
150 ));
6a488035
TO
151 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__);
152 //test id only format
2f45e1c2 153 $contribution = $this->callAPISuccess('contribution', 'get', array(
4ab7d517 154 'id' => $contribution2['id'],
155 'format.only_id' => 1,
156 ));
157 $this->assertEquals($contribution2['id'], $contribution);
6a488035 158 //test id as field
4ab7d517 159 $contribution = $this->callAPISuccess('contribution', 'get', array(
160 'id' => $this->_contribution['id'],
161 ));
2bfae985 162 $this->assertEquals(1, $contribution['count']);
4ab7d517 163
6a488035 164 //test get by contact id works
4ab7d517 165 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 166
2bfae985 167 $this->assertEquals(2, $contribution['count']);
2f45e1c2 168 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 169 'id' => $this->_contribution['id'],
4ab7d517 170 ));
2f45e1c2 171 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 172 'id' => $contribution2['id'],
4ab7d517 173 ));
6a488035
TO
174 }
175
176/**
177 * We need to ensure previous tested behaviour still works as part of the api contract
178 */
179 function testGetContributionLegacyBehaviour() {
180 $p = array(
181 'contact_id' => $this->_individualId,
182 'receive_date' => '2010-01-20',
183 'total_amount' => 100.00,
4ab7d517 184 'contribution_type_id' => $this->_financialTypeId,
6a488035
TO
185 'non_deductible_amount' => 10.00,
186 'fee_amount' => 5.00,
187 'net_amount' => 95.00,
188 'trxn_id' => 23456,
189 'invoice_id' => 78910,
190 'source' => 'SSF',
191 'contribution_status_id' => 1,
6a488035 192 );
2f45e1c2 193 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
194
195 $params = array(
196 'contribution_id' => $this->_contribution['id'],
6a488035 197 );
2f45e1c2 198 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 199 $financialParams['id'] = $this->_financialTypeId;
6a488035 200 $default = null;
858d0bf8 201 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 202
6a488035 203 $this->assertEquals(1,$contribution['count']);
2bfae985 204 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
4ab7d517 205 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
206 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
2bfae985
EM
207 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
208 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
209 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
210 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
211 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
212 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
213 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
214 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
6a488035
TO
215 //create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id)
216 $p['trxn_id'] = '3847';
217 $p['invoice_id'] = '3847';
218
2f45e1c2 219 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
6a488035 220
6a488035 221 // now we have 2 - test getcount
4ab7d517 222 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
223 $this->assertEquals(2, $contribution);
224 //test id only format
4ab7d517 225 $contribution = $this->callAPISuccess('contribution', 'get', array(
226 'id' => $this->_contribution['id'],
227 'format.only_id' => 1,
228 ));
6a488035
TO
229 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution,true) . " in line " . __LINE__);
230 //test id only format
4ab7d517 231 $contribution = $this->callAPISuccess('contribution', 'get', array(
232 'id' => $contribution2['id'],
233 'format.only_id' => 1,
234 ));
6a488035 235 $this->assertEquals($contribution2['id'], $contribution);
2f45e1c2 236 $contribution = $this->callAPISuccess('contribution', 'get', array(
6a488035
TO
237 'id' => $this->_contribution['id'],
238 ));
239 //test id as field
2bfae985 240 $this->assertEquals(1, $contribution['count']);
6a488035
TO
241 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
242 //test get by contact id works
4ab7d517 243 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 244
2bfae985 245 $this->assertEquals(2, $contribution['count']);
2f45e1c2 246 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 247 'id' => $this->_contribution['id'],
6a488035 248 ));
2f45e1c2 249 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 250 'id' => $contribution2['id'],
6a488035
TO
251 ));
252 }
253 ///////////////// civicrm_contribution_
254 function testCreateEmptyContributionIDUseDonation() {
255 $params = array(
256 'contribution_id' => FALSE,
257 'contact_id' => 1,
258 'total_amount' => 1,
6a488035
TO
259 'check_permissions' => false,
260 'financial_type_id' => 'Donation',
261 );
858d0bf8 262 $this->callAPISuccess('contribution', 'create', $params);
2f45e1c2 263 }
6a488035
TO
264 /*
265 * ensure we continue to support contribution_type_id as part of the api commitment to
266 * stability
267 *///////////////// civicrm_contribution_
268
269 function testCreateLegacyBehaviour() {
270 $params = array(
271 'contribution_id' => FALSE,
272 'contact_id' => 1,
273 'total_amount' => 1,
6a488035
TO
274 'check_permissions' => false,
275 'contribution_type_id' => 3,
276 );
2f45e1c2 277 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4ab7d517 278 $contribution = $this->callAPISuccess('contribution', 'getsingle', array( 'id' => $contribution['id']));
6a488035
TO
279 $this->assertEquals(3, $contribution['financial_type_id']);
280 }
281
6a488035
TO
282 /**
283 * check with complete array + custom field
284 * Note that the test is written on purpose without any
285 * variables specific to participant so it can be replicated into other entities
286 * and / or moved to the automated test suite
287 */
288 function testCreateWithCustom() {
289 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
290
291 $params = $this->_params;
292 $params['custom_' . $ids['custom_field_id']] = "custom string";
293
2f45e1c2 294 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 295 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
2f45e1c2 296 $check = $this->callAPISuccess($this->_entity, 'get', array(
4ab7d517 297 'return.custom_' . $ids['custom_field_id'] => 1,
298 'id' => $result['id'],
299 ));
6a488035
TO
300 $this->customFieldDelete($ids['custom_field_id']);
301 $this->customGroupDelete($ids['custom_group_id']);
302 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
303 }
304
305 /**
306 * check with complete array + custom field
307 * Note that the test is written on purpose without any
308 * variables specific to participant so it can be replicated into other entities
309 * and / or moved to the automated test suite
310 */
311 function testCreateGetFieldsWithCustom() {
312 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
313 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
4ab7d517 314 $result = $this->callAPISuccess('Contribution', 'getfields', array());
6a488035
TO
315 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
316 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
317 $this->customFieldDelete($ids['custom_field_id']);
318 $this->customGroupDelete($ids['custom_group_id']);
319 $this->customFieldDelete($idsContact['custom_field_id']);
320 $this->customGroupDelete($idsContact['custom_group_id']);
321 }
322
323 function testCreateContributionNoLineItems() {
324
325 $params = array(
326 'contact_id' => $this->_individualId,
327 'receive_date' => '20120511',
328 'total_amount' => 100.00,
4ab7d517 329 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
330 'payment_instrument_id' => 1,
331 'non_deductible_amount' => 10.00,
332 'fee_amount' => 50.00,
333 'net_amount' => 90.00,
334 'trxn_id' => 12345,
335 'invoice_id' => 67890,
336 'source' => 'SSF',
337 'contribution_status_id' => 1,
6a488035
TO
338 'skipLineItem' => 1,
339 );
340
2f45e1c2 341 $contribution = $this->callAPISuccess('contribution', 'create', $params);
342 $lineItems = $this->callAPISuccess('line_item','get',array(
6a488035
TO
343 'entity_id' => $contribution['id'],
344 'entity_table' => 'civicrm_contribution',
345 'sequential' => 1,
346 ));
347 $this->assertEquals(0, $lineItems['count']);
348 }
349 /*
350 * Test checks that passing in line items suppresses the create mechanism
351 */
352 function testCreateContributionChainedLineItems() {
353
354 $params = array(
355 'contact_id' => $this->_individualId,
356 'receive_date' => '20120511',
357 'total_amount' => 100.00,
4ab7d517 358 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
359 'payment_instrument_id' => 1,
360 'non_deductible_amount' => 10.00,
361 'fee_amount' => 50.00,
362 'net_amount' => 90.00,
363 'trxn_id' => 12345,
364 'invoice_id' => 67890,
365 'source' => 'SSF',
366 'contribution_status_id' => 1,
6a488035
TO
367 'skipLineItem' => 1,
368 'api.line_item.create' => array(
369 array(
370 'price_field_id' => 1,
371 'qty' => 2,
372 'line_total' => '20',
373 'unit_price' => '10',
374 ),
375 array(
376 'price_field_id' => 1,
377 'qty' => 1,
378 'line_total' => '80',
379 'unit_price' => '80',
380 ),
381 ),
382 );
383
6a488035
TO
384 $description = "Create Contribution with Nested Line Items";
385 $subfile = "CreateWithNestedLineItems";
2f45e1c2 386 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__,__FILE__, $description, $subfile);
387
388 $lineItems = $this->callAPISuccess('line_item','get',array(
6a488035 389 'entity_id' => $contribution['id'],
4ede4532 390 'contribution_id' => $contribution['id'],
6a488035
TO
391 'entity_table' => 'civicrm_contribution',
392 'sequential' => 1,
393 ));
394 $this->assertEquals(2, $lineItems['count']);
395 }
396
397 function testCreateContributionOffline() {
398 $params = array(
399 'contact_id' => $this->_individualId,
400 'receive_date' => '20120511',
401 'total_amount' => 100.00,
402 'financial_type_id' => 1,
403 'trxn_id' => 12345,
404 'invoice_id' => 67890,
405 'source' => 'SSF',
406 'contribution_status_id' => 1,
6a488035
TO
407 );
408
4ab7d517 409 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
410 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
411 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
412 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
413 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
414 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
415 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
416 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
4ab7d517 417 $lineItems = $this->callAPISuccess('line_item','get',array(
6a488035 418 'entity_id' => $contribution['id'],
4ede4532 419 'contribution_id' => $contribution['id'],
6a488035
TO
420 'entity_table' => 'civicrm_contribution',
421 'sequential' => 1,
422 ));
423 $this->assertEquals(1, $lineItems['count']);
424 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 425 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6a488035
TO
426 $this->_checkFinancialRecords($contribution, 'offline');
427 $this->contributionGetnCheck($params, $contribution['id']);
428 }
f70a6752 429 /**
430 * test create with valid payment instument
6a488035
TO
431 */
432 function testCreateContributionWithPaymentInstrument() {
433 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813
CW
434 $contribution = $this->callAPISuccess('contribution', 'create', $params);
435 $contribution = $this->callAPISuccess('contribution','get', array(
436 'sequential' => 1,
437 'id' => $contribution['id']
438 ));
6a488035 439 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
440 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
441
442 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
443 $contribution = $this->callAPISuccess('contribution','get', array(
444 'sequential' => 1,
445 'id' => $contribution['id']
446 ));
447 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
448 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
6a488035
TO
449 }
450
451 function testGetContributionByPaymentInstrument() {
6a488035 452 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 453 $params2 = $this->_params + array('payment_instrument' => 'Cash');
4ab7d517 454 $this->callAPISuccess('contribution','create',$params);
455 $this->callAPISuccess('contribution','create',$params2);
456 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'contribution_payment_instrument_id' => 'Cash'));
6a488035
TO
457 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
458 $this->assertEquals('Cash',$contribution['values'][0]['payment_instrument']);
459 $this->assertEquals(1,$contribution['count']);
4ab7d517 460 $contribution = $this->callAPISuccess('contribution','get',array('sequential' => 1, 'payment_instrument_id' => 'EFT'));
6a488035
TO
461 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
462 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
463 $this->assertEquals(1, $contribution['count']);
4ab7d517 464 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument_id' => 5));
6a488035
TO
465 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
466 $this->assertEquals('EFT',$contribution['values'][0]['payment_instrument']);
467 $this->assertEquals(1,$contribution['count']);
4ab7d517 468 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'payment_instrument' => 'EFT'));
6a488035 469 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
470 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
471 $this->assertEquals(1, $contribution['count']);
694769da 472 $contribution = $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'payment_instrument' => 'Credit Card'));
4ab7d517 473 $contribution = $this->callAPISuccess('contribution','get',array( 'sequential' => 1, 'id' => $contribution['id'], ));
6a488035
TO
474 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
475 $this->assertEquals('Credit Card',$contribution['values'][0]['payment_instrument']);
476 $this->assertEquals(1,$contribution['count']);
477 }
478
479 /*
480 * Create test with unique field name on source
481 */
482 function testCreateContributionSource() {
483
484 $params = array(
485 'contact_id' => $this->_individualId,
486 'receive_date' => date('Ymd'),
487 'total_amount' => 100.00,
4ab7d517 488 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
489 'payment_instrument_id' => 1,
490 'non_deductible_amount' => 10.00,
491 'fee_amount' => 50.00,
492 'net_amount' => 90.00,
493 'trxn_id' => 12345,
494 'invoice_id' => 67890,
495 'contribution_source' => 'SSF',
496 'contribution_status_id' => 1,
6a488035
TO
497 );
498
4ab7d517 499 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
500 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
501 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
6a488035 502 }
d5d148ab
EM
503
504 /*
505 * Create test with unique field name on source
506 */
507 function testCreateDefaultNow() {
508
509 $params = $this->_params;
510 unset($params['receive_date']);
511
512 $contribution = $this->callAPISuccess('contribution', 'create', $params);
513 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
514 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
515 }
516
6a488035
TO
517 /*
518 * Create test with unique field name on source
519 */
520 function testCreateContributionSourceInvalidContac() {
521
522 $params = array(
523 'contact_id' => 999,
524 'receive_date' => date('Ymd'),
525 'total_amount' => 100.00,
4ab7d517 526 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
527 'payment_instrument_id' => 1,
528 'non_deductible_amount' => 10.00,
529 'fee_amount' => 50.00,
530 'net_amount' => 90.00,
531 'trxn_id' => 12345,
532 'invoice_id' => 67890,
533 'contribution_source' => 'SSF',
534 'contribution_status_id' => 1,
6a488035
TO
535 );
536
858d0bf8 537 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
538 }
539
858d0bf8 540 function testCreateContributionSourceInvalidContContact() {
6a488035
TO
541
542 $params = array(
543 'contribution_contact_id' => 999,
544 'receive_date' => date('Ymd'),
545 'total_amount' => 100.00,
4ab7d517 546 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
547 'payment_instrument_id' => 1,
548 'non_deductible_amount' => 10.00,
549 'fee_amount' => 50.00,
550 'net_amount' => 90.00,
551 'trxn_id' => 12345,
552 'invoice_id' => 67890,
553 'contribution_source' => 'SSF',
554 'contribution_status_id' => 1,
6a488035
TO
555 );
556
858d0bf8 557 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
558 }
559
858d0bf8
EM
560 /**
561 * test note created correctly
562 */
6a488035
TO
563 function testCreateContributionWithNote() {
564 $description = "Demonstrates creating contribution with Note Entity";
565 $subfile = "ContributionCreateWithNote";
566 $params = array(
567 'contact_id' => $this->_individualId,
568 'receive_date' => '2012-01-01',
569 'total_amount' => 100.00,
4ab7d517 570 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
571 'payment_instrument_id' => 1,
572 'non_deductible_amount' => 10.00,
573 'fee_amount' => 50.00,
574 'net_amount' => 90.00,
575 'trxn_id' => 12345,
576 'invoice_id' => 67890,
577 'source' => 'SSF',
578 'contribution_status_id' => 1,
6a488035
TO
579 'note' => 'my contribution note',
580 );
581
4ab7d517 582 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
583 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
6a488035 584 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 585 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
586 }
587
588 function testCreateContributionWithNoteUniqueNameAliases() {
589 $params = array(
590 'contact_id' => $this->_individualId,
591 'receive_date' => '2012-01-01',
592 'total_amount' => 100.00,
4ab7d517 593 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
594 'payment_instrument_id' => 1,
595 'non_deductible_amount' => 10.00,
596 'fee_amount' => 50.00,
597 'net_amount' => 90.00,
598 'trxn_id' => 12345,
599 'invoice_id' => 67890,
600 'source' => 'SSF',
601 'contribution_status_id' => 1,
6a488035
TO
602 'contribution_note' => 'my contribution note',
603 );
604
4ab7d517 605 $contribution = $this->callAPISuccess('contribution', 'create', $params);
606 $result = $this->callAPISuccess('note', 'get', array('entity_table' => 'civicrm_contribution', 'entity_id' => $contribution['id'], 'sequential' => 1));
6a488035 607 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 608 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
609 }
610 /*
611 * This is the test for creating soft credits - however a 'get' is not yet possible via API
612 * as the current BAO functions are contact-centric (from what I can find)
613 *
614 */
615 function testCreateContributionWithSoftCredt() {
616 $description = "Demonstrates creating contribution with SoftCredit";
617 $subfile = "ContributionCreateWithSoftCredit";
4ab7d517 618 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
bcc03b98 619 $softparams = array(
620 'contact_id' => $contact2['id'],
621 'amount' => 50,
622 'soft_credit_type_id' => 3
6a488035
TO
623 );
624
bcc03b98 625 $params = $this->_params + array('soft_credit' => array(1 => $softparams));
4ab7d517 626 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
bcc03b98 627 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
a1c68fd2 628
bcc03b98 629 $this->assertEquals($softparams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
630 $this->assertEquals($softparams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
631 $this->assertEquals($softparams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a516bd6
DG
632
633 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
634 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
635 }
636
637 function testCreateContributionWithSoftCreditDefaults() {
638 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type";
639 $subfile = "ContributionCreateWithSoftCreditDefaults";
640 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
641 $params = $this->_params + array(
642 'soft_credit_to' => $contact2['id'],
643 );
644 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
645 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
646
647 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
648 // Default soft credit amount = contribution.total_amount
649 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
650 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
651
652 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
653 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
654 }
655
656 function testCreateContributionWithHonoreeContact() {
657 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id";
658 $subfile = "ContributionCreateWithHonoreeContact";
659 $contact2 = $this->callAPISuccess('Contact', 'create', array('display_name' => 'superman', 'contact_type' => 'Individual'));
660 $params = $this->_params + array(
661 'honor_contact_id' => $contact2['id'],
662 );
663 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
664 $result = $this->callAPISuccess('contribution','get', array('return'=> 'soft_credit', 'sequential' => 1));
665
666 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
667 // Default soft credit amount = contribution.total_amount
668 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
669 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
670 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a488035 671
4ab7d517 672 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
673 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
6a488035
TO
674 }
675
676 /**
677 * Test using example code
678 */
679 function testContributionCreateExample() {
680 //make sure at least on page exists since there is a truncate in tear down
4ab7d517 681 $page = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 682 $this->assertAPISuccess($page);
3ec6e38d 683 require_once 'api/v3/examples/Contribution/Create.php';
fc928539 684 $result = contribution_create_example();
6a488035
TO
685 $this->assertAPISuccess($result);
686 $contributionId = $result['id'];
687 $expectedResult = contribution_create_expectedresult();
8e342a79 688 $this->checkArrayEquals($expectedResult, $result);
6a488035
TO
689 $this->contributionDelete($contributionId);
690 }
691
692 /*
693 * Function tests that additional financial records are created when fee amount is recorded
694 */
695 function testCreateContributionWithFee() {
696 $params = array(
697 'contact_id' => $this->_individualId,
698 'receive_date' => '20120511',
699 'total_amount' => 100.00,
700 'fee_amount' => 50,
701 'financial_type_id' => 1,
702 'trxn_id' => 12345,
703 'invoice_id' => 67890,
704 'source' => 'SSF',
705 'contribution_status_id' => 1,
6a488035
TO
706 );
707
4ab7d517 708 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
709 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
710 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
e0e3c51b
EM
711 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
712 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
2bfae985
EM
713 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
714 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
715 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
716 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
717 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
e0e3c51b 718
4ab7d517 719 $lineItems = $this->callAPISuccess('line_item','get',array(
720
6a488035
TO
721 'entity_id' => $contribution['id'],
722 'entity_table' => 'civicrm_contribution',
723 'sequential' => 1,
724 ));
725 $this->assertEquals(1, $lineItems['count']);
726 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 727 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
4ab7d517 728 $lineItems = $this->callAPISuccess('line_item','get',array(
729
6a488035 730 'entity_id' => $contribution['id'],
4ede4532 731 'contribution_id' => $contribution['id'],
6a488035
TO
732 'entity_table' => 'civicrm_contribution',
733 'sequential' => 1,
734 ));
735 $this->assertEquals(1, $lineItems['count']);
736 $this->_checkFinancialRecords($contribution, 'feeAmount');
737 }
738
739
f70a6752 740 /**
6a488035
TO
741 * Function tests that additional financial records are created when online contribution is created
742 */
743 function testCreateContributionOnline() {
858d0bf8 744 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
4ab7d517 745 $contributionPage = $this->callAPISuccess( 'contribution_page','create', $this->_pageParams );
fc928539 746 $this->assertAPISuccess($contributionPage);
6a488035
TO
747 $params = array(
748 'contact_id' => $this->_individualId,
749 'receive_date' => '20120511',
750 'total_amount' => 100.00,
751 'financial_type_id' => 1,
752 'contribution_page_id' => $contributionPage['id'],
753 'payment_processor' => 1,
754 'trxn_id' => 12345,
755 'invoice_id' => 67890,
756 'source' => 'SSF',
757 'contribution_status_id' => 1,
4ab7d517 758
6a488035
TO
759 );
760
4ab7d517 761 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
762 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
763 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
764 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
765 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
766 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
767 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
768 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
6a488035
TO
769 $this->_checkFinancialRecords($contribution, 'online');
770 }
771
f70a6752 772 /**
773 * in the interests of removing financial type / contribution type checks from
774 * legacy format function lets test that the api is doing this for us
775 */
776 function testCreateInvalidFinancialType() {
777 $params = $this->_params;
778 $params['financial_type_id'] = 99999;
858d0bf8 779 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
f70a6752 780 }
781
4302618d 782 /**
783 * in the interests of removing financial type / contribution type checks from
784 * legacy format function lets test that the api is doing this for us
785 */
786 function testValidNamedFinancialType() {
787 $params = $this->_params;
788 $params['financial_type_id'] = 'Donation';
858d0bf8 789 $this->callAPISuccess($this->_entity, 'create', $params);
4302618d 790 }
791
f70a6752 792 /**
6a488035
TO
793 * Function tests that additional financial records are created when online contribution with pay later option
794 * is created
795 */
796 function testCreateContributionPayLaterOnline() {
858d0bf8 797 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
6a488035 798 $this->_pageParams['is_pay_later'] = 1;
4ab7d517 799 $contributionPage = $this->callAPISuccess( 'contribution_page','create',$this->_pageParams );
fc928539 800 $this->assertAPISuccess($contributionPage);
6a488035
TO
801 $params = array(
802 'contact_id' => $this->_individualId,
803 'receive_date' => '20120511',
804 'total_amount' => 100.00,
805 'financial_type_id' => 1,
806 'contribution_page_id' => $contributionPage['id'],
807 'trxn_id' => 12345,
808 'is_pay_later' => 1,
809 'invoice_id' => 67890,
810 'source' => 'SSF',
811 'contribution_status_id' => 2,
4ab7d517 812
6a488035
TO
813 );
814
4ab7d517 815 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
816 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
817 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
818 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
819 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
820 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
821 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
822 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
823 $this->_checkFinancialRecords($contribution, 'payLater');
824 }
825
826 /*
827 * Function tests that additional financial records are created when online contribution with pending option
828 * is created
829 */
830 function testCreateContributionPendingOnline() {
831 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
4ab7d517 832 $contributionPage = $this->callAPISuccess( 'contribution_page', 'create', $this->_pageParams );
fc928539 833 $this->assertAPISuccess($contributionPage);
6a488035
TO
834 $params = array(
835 'contact_id' => $this->_individualId,
836 'receive_date' => '20120511',
837 'total_amount' => 100.00,
838 'financial_type_id' => 1,
839 'contribution_page_id' => $contributionPage['id'],
840 'trxn_id' => 12345,
841 'invoice_id' => 67890,
842 'source' => 'SSF',
843 'contribution_status_id' => 2,
6a488035
TO
844 );
845
4ab7d517 846 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
847 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
848 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
849 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'],1 );
850 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
851 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
852 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
853 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
854 $this->_checkFinancialRecords($contribution, 'pending');
855 }
856
e748bf60
EM
857 /**
858 * test that BAO defaults work
859 */
860 function testCreateBAODefaults() {
861 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
862 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
863 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
864 $this->assertEquals(1, $contribution['contribution_status_id']);
865 $this->assertEquals(1, $contribution['payment_instrument_id']);
866 }
867
6a488035
TO
868 /*
869 * Function tests that line items, financial records are updated when contribution amount is changed
870 */
871 function testCreateUpdateContributionChangeTotal() {
4ab7d517 872 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
873 $lineItems = $this->callAPISuccess('line_item','getvalue', array(
874
6a488035
TO
875 'entity_id' => $contribution['id'],
876 'entity_table' => 'civicrm_contribution',
877 'sequential' => 1,
878 'return' => 'line_total',
879 ));
880 $this->assertEquals('100.00', $lineItems);
881 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
882 // Financial trxn SUM = 100 + 5 (fee)
883 $this->assertEquals('105.00', $trxnAmount);
884 $newParams = array(
4ab7d517 885
6a488035
TO
886 'id' => $contribution['id'],
887 'total_amount' => '125');
694769da 888 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
4ab7d517 889
890 $lineItems = $this->callAPISuccess('line_item','getvalue',array(
6a488035 891
6a488035
TO
892 'entity_id' => $contribution['id'],
893 'entity_table' => 'civicrm_contribution',
894 'sequential' => 1,
895 'return' => 'line_total',
896 ));
897
898 $this->assertEquals('125.00', $lineItems);
899 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
900 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
901 // Financial trxn SUM = 125 + 5 (fee)
902 $this->assertEquals('130.00', $trxnAmount);
903 $this->assertEquals('125.00', $fitemAmount);
904 }
905
906 /*
907 * Function tests that line items, financial records are updated when pay later contribution is received
908 */
909 function testCreateUpdateContributionPayLater() {
910 $contribParams = array(
911 'contact_id' => $this->_individualId,
912 'receive_date' => '2012-01-01',
913 'total_amount' => 100.00,
4ab7d517 914 'financial_type_id' => $this->_financialTypeId,
6a488035 915 'payment_instrument_id' => 1,
8f39a111 916 'contribution_status_id' => 2,
917 'is_pay_later' => 1,
4ab7d517 918
6a488035 919 );
4ab7d517 920 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
921
922 $newParams = array_merge($contribParams, array(
c71ae314
PN
923 'id' => $contribution['id'],
924 'contribution_status_id' => 1,)
925 );
694769da 926 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
927 $contribution = $contribution['values'][$contribution['id']];
928 $this->assertEquals($contribution['contribution_status_id'],'1');
929 $this->_checkFinancialItem($contribution['id'], 'paylater');
930 $this->_checkFinancialTrxn($contribution, 'payLater');
931 }
932
933 /*
934 * Function tests that financial records are updated when Payment Instrument is changed
935 */
936 function testCreateUpdateContributionPaymentInstrument() {
937 $instrumentId = $this->_addPaymentInstrument();
938 $contribParams = array(
939 'contact_id' => $this->_individualId,
940 'total_amount' => 100.00,
4ab7d517 941 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
942 'payment_instrument_id' => 4,
943 'contribution_status_id' => 1,
4ab7d517 944
6a488035 945 );
4ab7d517 946 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
947
948 $newParams = array_merge($contribParams, array(
949 'id' => $contribution['id'],
950 'payment_instrument_id' => $instrumentId,)
951 );
694769da 952 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
fc928539 953 $this->assertAPISuccess($contribution);
4ecc6d4b 954 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
6a488035
TO
955 }
956
957 /*
958 * Function tests that financial records are added when Contribution is Refunded
959 */
960 function testCreateUpdateContributionRefund() {
961 $contribParams = array(
962 'contact_id' => $this->_individualId,
963 'receive_date' => '2012-01-01',
964 'total_amount' => 100.00,
4ab7d517 965 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
966 'payment_instrument_id' => 4,
967 'contribution_status_id' => 1,
4ab7d517 968
6a488035 969 );
4ab7d517 970 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
971 $newParams = array_merge($contribParams, array(
972 'id' => $contribution['id'],
973 'contribution_status_id' => 7,
974 )
975 );
976
694769da 977 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
978 $this->_checkFinancialTrxn($contribution, 'refund');
979 $this->_checkFinancialItem($contribution['id'], 'refund');
8f39a111 980 }
c71ae314
PN
981
982 /*
8f39a111 983 * Function tests invalid contribution status change
c71ae314
PN
984 */
985 function testCreateUpdateContributionInValidStatusChange() {
986 $contribParams = array(
987 'contact_id' => 1,
988 'receive_date' => '2012-01-01',
989 'total_amount' => 100.00,
990 'financial_type_id' => 1,
991 'payment_instrument_id' => 1,
992 'contribution_status_id' => 1,
c71ae314 993 );
4ab7d517 994 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
c71ae314
PN
995 $newParams = array_merge($contribParams, array(
996 'id' => $contribution['id'],
997 'contribution_status_id' => 2,
998 )
999 );
858d0bf8 1000 $this->callAPIFailure('contribution', 'create', $newParams,ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1001
6a488035
TO
1002 }
1003
1004 /*
1005 * Function tests that financial records are added when Pending Contribution is Canceled
1006 */
1007 function testCreateUpdateContributionCancelPending() {
1008 $contribParams = array(
1009 'contact_id' => $this->_individualId,
1010 'receive_date' => '2012-01-01',
1011 'total_amount' => 100.00,
4ab7d517 1012 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1013 'payment_instrument_id' => 1,
1014 'contribution_status_id' => 2,
c71ae314 1015 'is_pay_later' => 1,
4ab7d517 1016
6a488035 1017 );
4ab7d517 1018 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1019 $newParams = array_merge($contribParams, array(
1020 'id' => $contribution['id'],
1021 'contribution_status_id' => 3,
1022 )
1023 );
694769da 1024 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1025 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1026 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1027 }
1028
1029 /*
1030 * Function tests that financial records are added when Financial Type is Changed
1031 */
1032 function testCreateUpdateContributionChangeFinancialType() {
1033 $contribParams = array(
1034 'contact_id' => $this->_individualId,
1035 'receive_date' => '2012-01-01',
1036 'total_amount' => 100.00,
1037 'financial_type_id' => 1,
1038 'payment_instrument_id' => 1,
1039 'contribution_status_id' => 1,
4ab7d517 1040
6a488035 1041 );
4ab7d517 1042 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1043 $newParams = array_merge($contribParams, array(
1044 'id' => $contribution['id'],
1045 'financial_type_id' => 3,
1046 )
1047 );
694769da 1048 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1049 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1050 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1051 }
1052
694769da
VU
1053 /**
1054 * test that update does not change status id CRM-15105
1055 */
1056 function testCreateUpdateWithoutChangingPendingStatus() {
1057 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1058 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1059 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'api.contribution.delete' => 1));
1060 $this->assertEquals(2, $contribution['contribution_status_id']);
1061 }
6a488035
TO
1062 //To Update Contribution
1063 //CHANGE: we require the API to do an incremental update
1064 function testCreateUpdateContribution() {
1065
4ab7d517 1066 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'idofsh', 212355);
6a488035
TO
1067 $old_params = array(
1068 'contribution_id' => $contributionID,
4ab7d517 1069
6a488035 1070 );
4ab7d517 1071 $original = $this->callAPISuccess('contribution', 'get', $old_params);
fc928539 1072 //Make sure it came back
2bfae985
EM
1073 $this->assertAPISuccess($original);
1074 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1075 //set up list of old params, verify
1076
1077 //This should not be required on update:
1078 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1079 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1080 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1081 $old_source = $original['values'][$contributionID]['contribution_source'];
1082
6a488035
TO
1083 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1084 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1085
1086 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1087 $this->assertEquals($old_contact_id, $this->_individualId);
1088 $this->assertEquals($old_fee_amount, 5.00);
1089 $this->assertEquals($old_source, 'SSF');
1090 $this->assertEquals($old_trxn_id, 212355);
1091 $this->assertEquals($old_invoice_id, 'idofsh');
6a488035
TO
1092 $params = array(
1093 'id' => $contributionID,
1094 'contact_id' => $this->_individualId,
1095 'total_amount' => 110.00,
4ab7d517 1096 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1097 'non_deductible_amount' => 10.00,
1098 'net_amount' => 100.00,
1099 'contribution_status_id' => 1,
1100 'note' => 'Donating for Nobel Cause',
4ab7d517 1101
6a488035
TO
1102 );
1103
4ab7d517 1104 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1105
1106 $new_params = array(
1107 'contribution_id' => $contribution['id'],
4ab7d517 1108
6a488035 1109 );
4ab7d517 1110 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
6a488035 1111
2bfae985
EM
1112 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1113 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1114 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'],$this->_financialTypeId );
1115 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1116 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1117 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1118 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1119 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1120 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1121 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1122 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
6a488035
TO
1123 $params = array(
1124 'contribution_id' => $contributionID,
4ab7d517 1125
6a488035 1126 );
4ab7d517 1127 $result = $this->callAPISuccess('contribution', 'delete', $params);
fc928539 1128 $this->assertAPISuccess($result, 'in line' . __LINE__);
6a488035
TO
1129 }
1130
1131 ///////////////// civicrm_contribution_delete methods
1132 function testDeleteEmptyParamsContribution() {
4ab7d517 1133 $params = array();
858d0bf8 1134 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1135 }
1136
1137 function testDeleteParamsNotArrayContribution() {
1138 $params = 'contribution_id= 1';
d0e1eff2 1139 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1140 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1141 }
1142
1143 function testDeleteWrongParamContribution() {
1144 $params = array(
1145 'contribution_source' => 'SSF',
4ab7d517 1146
6a488035 1147 );
858d0bf8 1148 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1149 }
1150
1151 function testDeleteContribution() {
1152
4ab7d517 1153 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 'dfsdf', 12389);
6a488035
TO
1154 $params = array(
1155 'id' => $contributionID,
6a488035 1156 );
4ab7d517 1157 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1158 }
1159
1160 /**
1161 * Test civicrm_contribution_search with empty params.
1162 * All available contributions expected.
1163 */
1164 function testSearchEmptyParams() {
4ab7d517 1165 $params = array();
6a488035
TO
1166
1167 $p = array(
1168 'contact_id' => $this->_individualId,
1169 'receive_date' => date('Ymd'),
1170 'total_amount' => 100.00,
4ab7d517 1171 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1172 'non_deductible_amount' => 10.00,
1173 'fee_amount' => 5.00,
1174 'net_amount' => 95.00,
1175 'trxn_id' => 23456,
1176 'invoice_id' => 78910,
1177 'source' => 'SSF',
1178 'contribution_status_id' => 1,
4ab7d517 1179
6a488035 1180 );
4ab7d517 1181 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1182
4ab7d517 1183 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1184 // We're taking the first element.
1185 $res = $result['values'][$contribution['id']];
1186
2bfae985
EM
1187 $this->assertEquals($p['contact_id'], $res['contact_id']);
1188 $this->assertEquals($p['total_amount'], $res['total_amount']);
1189 $this->assertEquals($p['financial_type_id'], $res['financial_type_id'] );
1190 $this->assertEquals($p['net_amount'], $res['net_amount']);
1191 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1192 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1193 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1194 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1195 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1196 // contribution_status_id = 1 => Completed
2bfae985 1197 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1198
1199 $this->contributionDelete($contribution['id']);
1200 }
1201
1202 /**
1203 * Test civicrm_contribution_search. Success expected.
1204 */
1205 function testSearch() {
1206 $p1 = array(
1207 'contact_id' => $this->_individualId,
1208 'receive_date' => date('Ymd'),
1209 'total_amount' => 100.00,
4ab7d517 1210 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1211 'non_deductible_amount' => 10.00,
1212 'contribution_status_id' => 1,
4ab7d517 1213
6a488035 1214 );
4ab7d517 1215 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1216
1217 $p2 = array(
1218 'contact_id' => $this->_individualId,
1219 'receive_date' => date('Ymd'),
1220 'total_amount' => 200.00,
4ab7d517 1221 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1222 'non_deductible_amount' => 20.00,
1223 'trxn_id' => 5454565,
1224 'invoice_id' => 1212124,
1225 'fee_amount' => 50.00,
1226 'net_amount' => 60.00,
1227 'contribution_status_id' => 2,
4ab7d517 1228
6a488035 1229 );
2f45e1c2 1230 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1231
1232 $params = array(
1233 'contribution_id' => $contribution2['id'],
4ab7d517 1234
6a488035 1235 );
2f45e1c2 1236 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1237 $res = $result['values'][$contribution2['id']];
1238
2bfae985
EM
1239 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1240 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1241 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id'] );
1242 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1243 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1244 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1245 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1246 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
6a488035 1247 // contribution_status_id = 2 => Pending
2bfae985 1248 $this->assertEquals('Pending', $res['contribution_status']);
6a488035
TO
1249
1250 $this->contributionDelete($contribution1['id']);
1251 $this->contributionDelete($contribution2['id']);
1252 }
2f45e1c2 1253
0efa8efe 1254 /**
1255 * Test completing a transaction via the API
1256 *
1257 * Note that we are creating a logged in user because email goes out from
1258 * that person
1259 */
1260 function testCompleteTransaction() {
1261 $mut = new CiviMailUtils( $this, true );
1262 $this->createLoggedInUser();
c6684214 1263 $params = array_merge($this->_params, array('contribution_status_id' => 2,));
0efa8efe 1264 $contribution = $this->callAPISuccess('contribution','create', $params);
66d3f9be 1265 $this->callAPISuccess('contribution', 'completetransaction', array(
0efa8efe 1266 'id' => $contribution['id'],
66d3f9be 1267 ));
0efa8efe 1268 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1269 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1270 $mut->checkMailLog(array(
1271 'Receipt - Contribution',
1272 'Please print this confirmation for your records.',
1273 ));
46fa5206
EM
1274 $mut->stop();
1275 }
1276
1277 /**
1278 * CRM-14151
1279 * Test completing a transaction via the API
1280 *
46fa5206
EM
1281 * tests.
1282 */
1283 function testCompleteTransactionWithReceiptDateSet() {
1284 $mut = new CiviMailUtils( $this, true );
1285 $this->createLoggedInUser();
c6684214 1286 $params = array_merge($this->_params, array('contribution_status_id' => 2,'receipt_date' => 'now'));
46fa5206 1287 $contribution = $this->callAPISuccess('contribution','create', $params);
858d0bf8 1288 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'],));
46fa5206
EM
1289 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1,));
1290 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1291 $mut->checkMailLog(array(
1292 'Receipt - Contribution',
1293 'Please print this confirmation for your records.',
1294 ));
0efa8efe 1295 $mut->stop();
1296 }
1297
1298 /**
1299 * Test completing a transaction with an event via the API
1300 *
1301 * Note that we are creating a logged in user because email goes out from
1302 * that person
1303 */
1304 function testCompleteTransactionWithParticipantRecord() {
1305 $mut = new CiviMailUtils( $this, true );
1306 $mut->clearMessages();
1307 $this->createLoggedInUser();
1308 $contributionID = $this->createPendingParticipantContribution();
66d3f9be
EM
1309 $this->callAPISuccess('contribution', 'completetransaction', array(
1310 'id' => $contributionID,)
0efa8efe 1311 );
858d0bf8 1312 $participantStatus = $this->callAPISuccessGetValue('participant', array('id' => $this->_ids['participant'], 'return' => 'participant_status_id'));
0efa8efe 1313 $this->assertEquals(1, $participantStatus);
1314 $mut->checkMailLog(array(
1315 'Annual CiviCRM meet',
1316 'Event',
afc59fef 1317 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 1318 ));
66d3f9be
EM
1319 $mut->stop();
1320 }
1321
1322 /**
1323 * test membership is renewed when transaction completed
1324 */
1325 function testCompleteTransactionMembershipPriceSet() {
1326 $this->createPriceSetWithPage('membership');
1327 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
1328 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1329 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1330 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
1331 $this->cleanUpAfterPriceSets();
1332 }
1333
1334 /**
1335 * test membership is renewed when transaction completed
1336 */
1337 function testCompleteTransactionMembershipPriceSetTwoTerms() {
1338 $this->createPriceSetWithPage('membership');
1339 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
1340 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
1341 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1342 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1343 $this->cleanUpAfterPriceSets();
1344 }
1345
1346 function cleanUpAfterPriceSets() {
1cf3c2b1 1347 $this->quickCleanUpFinancialEntities();
66d3f9be 1348 $this->contactDelete($this->_ids['contact']);
66d3f9be 1349 $this->callAPISuccess('price_set', 'delete', array('id' => $this->_ids['price_set']));
66d3f9be
EM
1350 }
1351
1352
1353 /**
1354 * this could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
1355 * on parent class at some point (fn is not in 4.4)
1356 * @param $entity
1357 * @param array $params
1358 */
1359 function createPriceSetWithPage($entity, $params = array()) {
1360 $membershipTypeID = $this->membershipTypeCreate();
1361 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1362 'title' => "Test Contribution Page",
1363 'financial_type_id' => 1,
1364 'currency' => 'NZD',
1365 'goal_amount' => 50,
1366 'is_pay_later' => 1,
1367 'is_monetary' => TRUE,
1368 'is_email_receipt' => FALSE,
1369 ));
1370 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1371 'is_quick_config' => 0,
1372 'extends' => 'CiviMember',
1373 'financial_type_id' => 1,
1374 'title' => 'my Page'
1375 ));
1376 $priceSetID = $priceSet['id'];
1377
1378 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
1379 $priceField = $this->callAPISuccess('price_field', 'create', array(
1380 'price_set_id' => $priceSetID ,
1381 'label' => 'Goat Breed',
1382 'html_type' => 'Radio',
1383 ));
1384 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1385 'price_set_id' => $priceSetID ,
1386 'price_field_id' => $priceField['id'],
1387 'label' => 'Long Haired Goat',
1388 'amount' => 20,
1389 'membership_type_id' => $membershipTypeID,
1390 'membership_num_terms' => 1,
1391 )
1392 );
1393 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1394 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
1395 'price_set_id' => $priceSetID ,
1396 'price_field_id' => $priceField['id'],
1397 'label' => 'Shoe-eating Goat',
1398 'amount' => 10,
1399 'membership_type_id' => $membershipTypeID,
1400 'membership_num_terms' => 2,
1401 )
1402 );
1403 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
1404 $this->_ids['price_set'] = $priceSetID;
1405 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1406 $this->_ids['price_field'] = array($priceField['id']);
0efa8efe 1407
66d3f9be
EM
1408 $this->_ids['membership_type'] = $membershipTypeID;
1409 }
1410
1411 /**
1412 * Set up a pending transaction with a specific price field id
1413 * @param $priceFieldValueID
1414 */
1415 function setUpPendingContribution($priceFieldValueID){
1416 $contactID = $this->individualCreate();
1417 $membership = $this->callAPISuccess('membership', 'create', array(
1418 'contact_id' => $contactID,
1419 'membership_type_id' => $this->_ids['membership_type'],
1420 'start_date' => 'yesterday - 1 year',
1421 'end_date' => 'yesterday',
1422 ));
1423 $contribution = $this->callAPISuccess('contribution', 'create', array(
1424 'domain_id' => 1,
1425 'contact_id' => $contactID,
1426 'receive_date' => date('Ymd'),
1427 'total_amount' => 100.00,
1428 'financial_type_id' => 1,
1429 'payment_instrument_id' => 'Credit Card',
1430 'non_deductible_amount' => 10.00,
1431 'trxn_id' => 'jdhfi88',
1432 'invoice_id' => 'djfhiewuyr',
1433 'source' => 'SSF',
1434 'contribution_status_id' => 2,
1435 'contribution_page_id' => $this->_ids['contribution_page'],
1436 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1437 ));
1438
1439 $this->callAPISuccess('line_item', 'create', array(
1440 'entity_id' => $contribution['id'],
1441 'entity_table' => 'civicrm_contribution',
1274acef 1442 'contribution_id' => $contribution['id'],
66d3f9be
EM
1443 'price_field_id' => $this->_ids['price_field'][0],
1444 'qty' => 1,
1445 'unit_price' => 20,
1446 'line_total' => 20,
1447 'financial_type_id' => 1,
1448 'price_field_value_id' => $priceFieldValueID,
1449 ));
1450 $this->_ids['contact'] = $contactID;
1451 $this->_ids['contribution'] = $contribution['id'];
1452 $this->_ids['membership'] = $membership['id'];
0efa8efe 1453 }
1454
2f45e1c2 1455 /**
6a488035
TO
1456 * Test sending a mail via the API
1457 */
1458 function testSendMail() {
1459 $mut = new CiviMailUtils( $this, true );
2f45e1c2 1460 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
858d0bf8 1461 $this->callAPISuccess('contribution', 'sendconfirmation', array(
6a488035
TO
1462 'id' => $contribution['id'],
1463 'receipt_from_email' => 'api@civicrm.org',
1464 )
1465 );
6a488035
TO
1466 $mut->checkMailLog(array(
1467 '$ 100.00',
1468 'Contribution Information',
1469 'Please print this confirmation for your records',
1470 ), array(
1471 'Event'
1472 )
1473 );
1474 $mut->stop();
1475 }
1476
2f45e1c2 1477 /**
6a488035
TO
1478 * Test sending a mail via the API
1479 */
1480 function testSendMailEvent() {
1481 $mut = new CiviMailUtils( $this, true );
2f45e1c2 1482 $contribution = $this->callAPISuccess('contribution','create',$this->_params);
1483 $event = $this->eventCreate(array(
6a488035
TO
1484 'is_email_confirm' => 1,
1485 'confirm_from_email' => 'test@civicrm.org',
1486 ));
1487 $this->_eventID = $event['id'];
1488 $participantParams = array(
1489 'contact_id' => $this->_individualId,
1490 'event_id' => $this->_eventID,
1491 'status_id' => 1,
1492 'role_id' => 1,
1493 // to ensure it matches later on
1494 'register_date' => '2007-07-21 00:00:00',
1495 'source' => 'Online Event Registration: API Testing',
4ab7d517 1496
6a488035 1497 );
2f45e1c2 1498 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1499 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
1500 'participant_id' => $participant['id'],
1501 'contribution_id' => $contribution['id'],
2f45e1c2 1502 ));
66d3f9be 1503 $this->callAPISuccess('contribution', 'sendconfirmation', array(
6a488035
TO
1504 'id' => $contribution['id'],
1505 'receipt_from_email' => 'api@civicrm.org',
1506 )
1507 );
1508
6a488035
TO
1509 $mut->checkMailLog(array(
1510 'Annual CiviCRM meet',
1511 'Event',
1512 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
1513 ), array(
1514
1515 )
1516 );
1517 $mut->stop();
1518 }
1519
4302618d 1520 /**
1521 * This function does a GET & compares the result against the $params
1522 * Use as a double check on Creates
1523 */
6a488035
TO
1524 function contributionGetnCheck($params, $id, $delete = 1) {
1525
4ab7d517 1526 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 1527 'id' => $id,
4ab7d517 1528
6a488035
TO
1529 ));
1530
1531 if ($delete) {
4ab7d517 1532 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 1533 }
2bfae985 1534 $this->assertAPISuccess($contribution, 0);
6a488035
TO
1535 $values = $contribution['values'][$contribution['id']];
1536 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1537 // this is not returned in id format
1538 unset($params['payment_instrument_id']);
1539 $params['contribution_source'] = $params['source'];
1540 unset($params['source']);
1541 foreach ($params as $key => $value) {
6a488035
TO
1542 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE) . 'in line' . __LINE__);
1543 }
1544 }
1545
0efa8efe 1546 /**
1547 * Create a pending contribution & linked pending participant record
1548 * (along with an event)
1549 */
1550 function createPendingParticipantContribution(){
1551 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org',));
1552 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
858d0bf8 1553 $this->_ids['participant'] = $participantID;
0efa8efe 1554 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
1555 $contribution = $this->callAPISuccess('contribution','create', $params);
1556 $this->callAPISuccess('participant_payment', 'create', array('contribution_id' => $contribution['id'], 'participant_id' => $participantID));
858d0bf8 1557 $this->callAPISuccess('line_item', 'get', array(
0efa8efe 1558 'entity_id' => $contribution['id'],
1559 'entity_table' => 'civicrm_contribution',
1560 'api.line_item.create' => array(
1561 'entity_id' => $participantID,
1562 'entity_table' => 'civicrm_participant',
1563 ),
1564 ));
1565 return $contribution['id'];
1566 }
1567
4cbe18b8
EM
1568 /**
1569 * @param $contId
1570 *
1571 * @return null|string
1572 */function _getFinancialTrxnAmount($contId) {
6a488035
TO
1573 $query = "SELECT
1574 SUM( ft.total_amount ) AS total
1575 FROM civicrm_financial_trxn AS ft
1576 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
1577 WHERE ceft.entity_table = 'civicrm_contribution'
1578 AND ceft.entity_id = {$contId}";
1579
1580 $result = CRM_Core_DAO::singleValueQuery($query);
1581 return $result;
1582 }
1583
4cbe18b8
EM
1584 /**
1585 * @param $contId
1586 *
1587 * @return null|string
1588 */function _getFinancialItemAmount($contId) {
6a488035
TO
1589 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1590 $query = "SELECT
1591 SUM(amount)
1592 FROM civicrm_financial_item
1593 WHERE entity_table = 'civicrm_line_item'
1594 AND entity_id = {$lineItem}";
1595 $result = CRM_Core_DAO::singleValueQuery($query);
1596 return $result;
1597 }
1598
4cbe18b8
EM
1599 /**
1600 * @param $contId
1601 * @param $context
1602 */
1603 function _checkFinancialItem($contId, $context) {
6a488035
TO
1604 if ($context != 'paylater') {
1605 $params = array (
1606 'entity_id' => $contId,
1607 'entity_table' => 'civicrm_contribution',
1608 );
1609 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
1610 $entityParams = array(
1611 'financial_trxn_id' => $trxn['financial_trxn_id'],
1612 'entity_table' => 'civicrm_financial_item',
1613 );
1614 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1615 $params = array(
1616 'id' => $entityTrxn['entity_id'],
1617 );
1618 }
1619 if ($context == 'paylater') {
1620 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
1621 foreach ($lineItems as $key=>$item) {
1622 $params = array(
1623 'entity_id' => $key,
1624 'entity_table' => 'civicrm_line_item',
1625 );
1626 $compareParams = array ('status_id' => 1);
1627 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1628 }
1629 }
1630 elseif ($context == 'refund') {
1631 $compareParams = array(
1632 'status_id' => 1,
1633 'financial_account_id' => 1,
1634 'amount' => -100,
1635 );
1636 }
1637 elseif ($context == 'cancelPending') {
1638 $compareParams = array(
1639 'status_id' => 3,
1640 'financial_account_id' => 1,
1641 'amount' => -100,
1642 );
1643 }
1644 elseif ($context == 'changeFinancial') {
1645 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
1646 $params = array(
1647 'entity_id' => $lineKey,
1648 'amount' => -100,
1649 );
1650 $compareParams = array(
1651 'financial_account_id' => 1,
1652 );
1653 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1654 $params = array(
1655 'financial_account_id' => 3,
1656 'entity_id' => $lineKey,
1657 );
1658 $compareParams = array(
1659 'amount' => 100,
1660 );
1661 }
1662 if ($context != 'paylater') {
1663 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
1664 }
1665 }
1666
4cbe18b8
EM
1667 /**
1668 * @param $contribution
1669 * @param $context
1670 * @param null $instrumentId
1671 */
1672 function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
6a488035
TO
1673 $trxnParams = array(
1674 'entity_id' => $contribution['id'],
1675 'entity_table' => 'civicrm_contribution',
1676 );
1677 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
1678 $params = array(
1679 'id' => $trxn['financial_trxn_id'],
1680 );
1681 if ($context == 'payLater') {
f743a6eb 1682 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
6a488035
TO
1683 $compareParams = array(
1684 'status_id' => 1,
1685 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
1686 );
1687 }
6a488035
TO
1688 elseif ($context == 'refund') {
1689 $compareParams = array(
1690 'to_financial_account_id' => 6,
1691 'total_amount' => -100,
1692 'status_id' => 7,
1693 );
1694 }
1695 elseif ($context == 'cancelPending') {
1696 $compareParams = array(
1697 'from_financial_account_id' => 7,
1698 'total_amount' => -100,
1699 'status_id' => 3,
1700 );
1701 }
4ecc6d4b 1702 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
6a488035
TO
1703 $entityParams = array(
1704 'entity_id' => $contribution['id'],
1705 'entity_table' => 'civicrm_contribution',
1706 'amount' => -100,
1707 );
1708 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1709 $trxnParams1 = array(
1710 'id' => $trxn['financial_trxn_id'],
1711 );
1712 $compareParams = array(
6a488035
TO
1713 'total_amount' => -100,
1714 'status_id' => 1,
1715 );
4ecc6d4b
PN
1716 if ($context == 'paymentInstrument') {
1717 $compareParams += array(
1718 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
1719 'payment_instrument_id' => 4,
1720 );
1721 }
1722 else {
1723 $compareParams['to_financial_account_id'] = 12;
1724 }
6a488035 1725 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
4ecc6d4b
PN
1726 $compareParams['total_amount'] = 100;
1727 if ($context == 'paymentInstrument') {
1728 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
1729 $compareParams['payment_instrument_id'] = $instrumentId;
1730 }
1731 else {
1732 $compareParams['to_financial_account_id'] = 12;
1733 }
6a488035
TO
1734 }
1735
1736 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
1737 }
1738
4cbe18b8
EM
1739 /**
1740 * @return mixed
1741 */
1742 function _addPaymentInstrument () {
6a488035
TO
1743 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
1744 $optionParams = array(
1745 'option_group_id' => $gId,
1746 'label' => 'Test Card',
1747 'name' => 'Test Card',
1748 'value' => '6',
1749 'weight' => '6',
1750 'is_active' => 1,
6a488035 1751);
4ab7d517 1752 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
f743a6eb 1753 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
1754 $financialParams = array(
1755 'entity_table' => 'civicrm_option_value',
1756 'entity_id' => $optionValue['id'],
1757 'account_relationship' => $relationTypeId,
1758 'financial_account_id' => 7,
1759 );
858d0bf8 1760 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
6a488035
TO
1761 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
1762 return $optionValue['values'][$optionValue['id']]['value'];
1763 }
1764
4cbe18b8
EM
1765 /**
1766 * @param $params
1767 * @param $context
1768 */
1769 function _checkFinancialRecords($params,$context) {
6a488035
TO
1770 $entityParams = array(
1771 'entity_id' => $params['id'],
1772 'entity_table' => 'civicrm_contribution',
1773 );
e0e3c51b
EM
1774 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
1775 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
6a488035
TO
1776 if ($context == 'pending') {
1777 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
1778 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
1779 return;
1780 }
1781 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1782 $trxnParams = array(
1783 'id' => $trxn['financial_trxn_id'],
1784 );
1785 if ($context != 'online' && $context != 'payLater') {
1786 $compareParams = array(
1787 'to_financial_account_id' => 6,
1788 'total_amount' => 100,
1789 'status_id' => 1,
1790 );
1791 }
1792 if ($context == 'feeAmount') {
1793 $compareParams['fee_amount'] = 50;
1794 }
1795 elseif ($context == 'online') {
1796 $compareParams = array(
1797 'to_financial_account_id' => 12,
1798 'total_amount' => 100,
1799 'status_id' => 1,
1800 );
1801 }
1802 elseif ($context == 'payLater') {
1803 $compareParams = array(
1804 'to_financial_account_id' => 7,
1805 'total_amount' => 100,
1806 'status_id' => 2,
1807 );
1808 }
858d0bf8 1809 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn',$trxnParams, $compareParams);
6a488035
TO
1810 $entityParams = array(
1811 'financial_trxn_id' => $trxn['financial_trxn_id'],
1812 'entity_table' => 'civicrm_financial_item',
1813 );
1814 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
1815 $fitemParams = array(
1816 'id' => $entityTrxn['entity_id'],
1817 );
1818 $compareParams = array(
1819 'amount' => 100,
1820 'status_id' => 1,
1821 'financial_account_id' => 1,
1822 );
1823 if ($context == 'payLater') {
1824 $compareParams = array(
1825 'amount' => 100,
1826 'status_id' => 3,
1827 'financial_account_id' => 1,
1828 );
1829 }
1830 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1831 if ($context == 'feeAmount') {
1832 $maxParams = array(
1833 'entity_id' => $params['id'],
1834 'entity_table' => 'civicrm_contribution',
1835 );
1836 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
1837 $trxnParams = array(
1838 'id' => $maxTrxn['financial_trxn_id'],
1839 );
1840 $compareParams = array(
1841 'to_financial_account_id' => 5,
1842 'from_financial_account_id' => 6,
1843 'total_amount' => 50,
1844 'status_id' => 1,
1845 );
1846 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
1847 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
1848 $fitemParams = array(
1849 'entity_id' => $trxnId['financialTrxnId'],
1850 'entity_table' => 'civicrm_financial_trxn',
1851 );
1852 $compareParams = array(
1853 'amount' => 50,
1854 'status_id' => 1,
1855 'financial_account_id' => 5,
1856 );
1857 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
1858 }
1859 }
1860}
1861