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