CRM-17718 update repeattransaction to accept contribution_recur_id instead of origina...
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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 /**
fe482240 41 * Assume empty database with just civicrm_data.
6a488035
TO
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();
1eade77d 52 /**
53 * Payment processor ID (dummy processor).
54 *
55 * @var int
56 */
57 protected $paymentProcessorID;
b7c9bc4c 58
1e52837d
EM
59 /**
60 * Parameters to create payment processor.
61 *
62 * @var array
63 */
64 protected $_processorParams = array();
65
66 /**
67 * ID of created event.
68 *
69 * @var int
70 */
71 protected $_eventID;
72
22f80e87
EM
73 /**
74 * Setup function.
75 */
00be9182 76 public function setUp() {
6a488035
TO
77 parent::setUp();
78
79 $this->_apiversion = 3;
80 $this->_individualId = $this->individualCreate();
6a488035
TO
81 $this->_params = array(
82 'contact_id' => $this->_individualId,
83 'receive_date' => '20120511',
84 'total_amount' => 100.00,
5896d037 85 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
86 'non_deductible_amount' => 10.00,
87 'fee_amount' => 5.00,
88 'net_amount' => 95.00,
89 'source' => 'SSF',
90 'contribution_status_id' => 1,
6a488035
TO
91 );
92 $this->_processorParams = array(
93 'domain_id' => 1,
94 'name' => 'Dummy',
95 'payment_processor_type_id' => 10,
96 'financial_account_id' => 12,
97 'is_active' => 1,
98 'user_name' => '',
99 'url_site' => 'http://dummy.com',
100 'url_recur' => 'http://dummy.com',
101 'billing_mode' => 1,
102 );
1eade77d 103 $this->paymentProcessorID = $this->processorCreate();
6a488035 104 $this->_pageParams = array(
6a488035
TO
105 'title' => 'Test Contribution Page',
106 'financial_type_id' => 1,
107 'currency' => 'USD',
108 'financial_account_id' => 1,
1eade77d 109 'payment_processor' => $this->paymentProcessorID,
6a488035
TO
110 'is_active' => 1,
111 'is_allow_other_amount' => 1,
112 'min_amount' => 10,
113 'max_amount' => 1000,
5896d037 114 );
6a488035
TO
115 }
116
22f80e87
EM
117 /**
118 * Clean up after each test.
119 */
00be9182 120 public function tearDown() {
39d632fd 121 $this->quickCleanUpFinancialEntities();
225d474b 122 $this->quickCleanup(array('civicrm_uf_match'));
6a488035
TO
123 }
124
22f80e87
EM
125 /**
126 * Test Get.
127 */
00be9182 128 public function testGetContribution() {
6a488035
TO
129 $p = array(
130 'contact_id' => $this->_individualId,
131 'receive_date' => '2010-01-20',
132 'total_amount' => 100.00,
4ab7d517 133 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
134 'non_deductible_amount' => 10.00,
135 'fee_amount' => 5.00,
136 'net_amount' => 95.00,
137 'trxn_id' => 23456,
138 'invoice_id' => 78910,
139 'source' => 'SSF',
140 'contribution_status_id' => 1,
6a488035 141 );
2f45e1c2 142 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
143
144 $params = array(
145 'contribution_id' => $this->_contribution['id'],
6a488035 146 );
e0e3c51b 147
2f45e1c2 148 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 149 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 150 $default = NULL;
858d0bf8 151 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 152
e0e3c51b 153 $this->assertEquals(1, $contribution['count']);
2bfae985 154 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
22f80e87
EM
155 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
156 // Passing back a string rather than an id seems like an error/cruft.
157 // If it is to be introduced we should discuss.
6a488035 158 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
159 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
160 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
161 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
162 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
163 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
164 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
165 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
166 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
22f80e87 167 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
168 $p['trxn_id'] = '3847';
169 $p['invoice_id'] = '3847';
170
2f45e1c2 171 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
172
22f80e87 173 // Now we have 2 - test getcount.
4ab7d517 174 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035 175 $this->assertEquals(2, $contribution);
22f80e87 176 // Test id only format.
4ab7d517 177 $contribution = $this->callAPISuccess('contribution', 'get', array(
178 'id' => $this->_contribution['id'],
179 'format.only_id' => 1,
180 ));
22f80e87
EM
181 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
182 // Test id only format.
2f45e1c2 183 $contribution = $this->callAPISuccess('contribution', 'get', array(
4ab7d517 184 'id' => $contribution2['id'],
185 'format.only_id' => 1,
186 ));
187 $this->assertEquals($contribution2['id'], $contribution);
22f80e87 188 // Test id as field.
4ab7d517 189 $contribution = $this->callAPISuccess('contribution', 'get', array(
190 'id' => $this->_contribution['id'],
191 ));
2bfae985 192 $this->assertEquals(1, $contribution['count']);
4ab7d517 193
22f80e87 194 // Test get by contact id works.
4ab7d517 195 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 196
2bfae985 197 $this->assertEquals(2, $contribution['count']);
2f45e1c2 198 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 199 'id' => $this->_contribution['id'],
4ab7d517 200 ));
2f45e1c2 201 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 202 'id' => $contribution2['id'],
4ab7d517 203 ));
6a488035
TO
204 }
205
71d5a412 206 /**
207 * Test that test contributions can be retrieved.
208 */
209 public function testGetTestContribution() {
210 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1)));
211 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
212 }
213
6c6e6187 214 /**
22f80e87 215 * We need to ensure previous tested behaviour still works as part of the api contract.
6c6e6187 216 */
00be9182 217 public function testGetContributionLegacyBehaviour() {
6a488035
TO
218 $p = array(
219 'contact_id' => $this->_individualId,
220 'receive_date' => '2010-01-20',
221 'total_amount' => 100.00,
4ab7d517 222 'contribution_type_id' => $this->_financialTypeId,
6a488035
TO
223 'non_deductible_amount' => 10.00,
224 'fee_amount' => 5.00,
225 'net_amount' => 95.00,
226 'trxn_id' => 23456,
227 'invoice_id' => 78910,
228 'source' => 'SSF',
229 'contribution_status_id' => 1,
6a488035 230 );
71d5a412 231 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
6a488035
TO
232
233 $params = array(
234 'contribution_id' => $this->_contribution['id'],
6a488035 235 );
2f45e1c2 236 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 237 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 238 $default = NULL;
858d0bf8 239 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 240
6c6e6187 241 $this->assertEquals(1, $contribution['count']);
2bfae985 242 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
4ab7d517 243 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
244 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
2bfae985
EM
245 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
246 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
247 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
248 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
249 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
250 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
251 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
252 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
22f80e87
EM
253
254 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
255 $p['trxn_id'] = '3847';
256 $p['invoice_id'] = '3847';
257
2f45e1c2 258 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
6a488035 259
6a488035 260 // now we have 2 - test getcount
4ab7d517 261 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
262 $this->assertEquals(2, $contribution);
263 //test id only format
4ab7d517 264 $contribution = $this->callAPISuccess('contribution', 'get', array(
265 'id' => $this->_contribution['id'],
266 'format.only_id' => 1,
267 ));
22f80e87 268 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
6a488035 269 //test id only format
4ab7d517 270 $contribution = $this->callAPISuccess('contribution', 'get', array(
271 'id' => $contribution2['id'],
272 'format.only_id' => 1,
273 ));
6a488035 274 $this->assertEquals($contribution2['id'], $contribution);
2f45e1c2 275 $contribution = $this->callAPISuccess('contribution', 'get', array(
6a488035
TO
276 'id' => $this->_contribution['id'],
277 ));
278 //test id as field
2bfae985 279 $this->assertEquals(1, $contribution['count']);
6a488035
TO
280 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
281 //test get by contact id works
4ab7d517 282 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 283
2bfae985 284 $this->assertEquals(2, $contribution['count']);
2f45e1c2 285 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 286 'id' => $this->_contribution['id'],
6a488035 287 ));
2f45e1c2 288 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 289 'id' => $contribution2['id'],
6a488035
TO
290 ));
291 }
5896d037 292
a1a2a83d
TO
293 /**
294 * Create an contribution_id=FALSE and financial_type_id=Donation.
295 */
00be9182 296 public function testCreateEmptyContributionIDUseDonation() {
6a488035
TO
297 $params = array(
298 'contribution_id' => FALSE,
299 'contact_id' => 1,
300 'total_amount' => 1,
6c6e6187 301 'check_permissions' => FALSE,
6a488035
TO
302 'financial_type_id' => 'Donation',
303 );
858d0bf8 304 $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 305 }
6a488035 306
6a488035 307 /**
1e52837d
EM
308 * Check with complete array + custom field.
309 *
6a488035
TO
310 * Note that the test is written on purpose without any
311 * variables specific to participant so it can be replicated into other entities
312 * and / or moved to the automated test suite
313 */
00be9182 314 public function testCreateWithCustom() {
6a488035
TO
315 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
316
317 $params = $this->_params;
318 $params['custom_' . $ids['custom_field_id']] = "custom string";
319
6c6e6187 320 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 321 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
2f45e1c2 322 $check = $this->callAPISuccess($this->_entity, 'get', array(
4ab7d517 323 'return.custom_' . $ids['custom_field_id'] => 1,
324 'id' => $result['id'],
325 ));
6a488035
TO
326 $this->customFieldDelete($ids['custom_field_id']);
327 $this->customGroupDelete($ids['custom_group_id']);
22f80e87 328 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
329 }
330
331 /**
fd786d03
EM
332 * Check with complete array + custom field.
333 *
6a488035
TO
334 * Note that the test is written on purpose without any
335 * variables specific to participant so it can be replicated into other entities
336 * and / or moved to the automated test suite
337 */
00be9182 338 public function testCreateGetFieldsWithCustom() {
5896d037 339 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
6a488035 340 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
5896d037 341 $result = $this->callAPISuccess('Contribution', 'getfields', array());
6a488035
TO
342 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
343 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
344 $this->customFieldDelete($ids['custom_field_id']);
345 $this->customGroupDelete($ids['custom_group_id']);
346 $this->customFieldDelete($idsContact['custom_field_id']);
347 $this->customGroupDelete($idsContact['custom_group_id']);
348 }
349
00be9182 350 public function testCreateContributionNoLineItems() {
6a488035
TO
351
352 $params = array(
353 'contact_id' => $this->_individualId,
354 'receive_date' => '20120511',
355 'total_amount' => 100.00,
5896d037 356 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
357 'payment_instrument_id' => 1,
358 'non_deductible_amount' => 10.00,
359 'fee_amount' => 50.00,
360 'net_amount' => 90.00,
361 'trxn_id' => 12345,
362 'invoice_id' => 67890,
363 'source' => 'SSF',
364 'contribution_status_id' => 1,
6a488035
TO
365 'skipLineItem' => 1,
366 );
367
2f45e1c2 368 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 369 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035
TO
370 'entity_id' => $contribution['id'],
371 'entity_table' => 'civicrm_contribution',
372 'sequential' => 1,
373 ));
374 $this->assertEquals(0, $lineItems['count']);
375 }
5896d037 376
a1a2a83d 377 /**
eceb18cc 378 * Test checks that passing in line items suppresses the create mechanism.
6a488035 379 */
00be9182 380 public function testCreateContributionChainedLineItems() {
6a488035
TO
381 $params = array(
382 'contact_id' => $this->_individualId,
383 'receive_date' => '20120511',
384 'total_amount' => 100.00,
4ab7d517 385 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
386 'payment_instrument_id' => 1,
387 'non_deductible_amount' => 10.00,
388 'fee_amount' => 50.00,
389 'net_amount' => 90.00,
390 'trxn_id' => 12345,
391 'invoice_id' => 67890,
392 'source' => 'SSF',
393 'contribution_status_id' => 1,
6a488035
TO
394 'skipLineItem' => 1,
395 'api.line_item.create' => array(
396 array(
397 'price_field_id' => 1,
398 'qty' => 2,
399 'line_total' => '20',
400 'unit_price' => '10',
401 ),
402 array(
403 'price_field_id' => 1,
404 'qty' => 1,
405 'line_total' => '80',
406 'unit_price' => '80',
407 ),
408 ),
409 );
410
5c49fee0 411 $description = "Create Contribution with Nested Line Items.";
6a488035 412 $subfile = "CreateWithNestedLineItems";
6c6e6187 413 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
2f45e1c2 414
6c6e6187 415 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035 416 'entity_id' => $contribution['id'],
4ede4532 417 'contribution_id' => $contribution['id'],
6a488035
TO
418 'entity_table' => 'civicrm_contribution',
419 'sequential' => 1,
420 ));
421 $this->assertEquals(2, $lineItems['count']);
422 }
423
00be9182 424 public function testCreateContributionOffline() {
6a488035
TO
425 $params = array(
426 'contact_id' => $this->_individualId,
427 'receive_date' => '20120511',
428 'total_amount' => 100.00,
429 'financial_type_id' => 1,
430 'trxn_id' => 12345,
431 'invoice_id' => 67890,
432 'source' => 'SSF',
433 'contribution_status_id' => 1,
6a488035
TO
434 );
435
4ab7d517 436 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
437 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
438 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 439 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
440 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
441 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
442 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
443 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
6c6e6187 444 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035 445 'entity_id' => $contribution['id'],
4ede4532 446 'contribution_id' => $contribution['id'],
6a488035
TO
447 'entity_table' => 'civicrm_contribution',
448 'sequential' => 1,
5896d037 449 ));
6a488035
TO
450 $this->assertEquals(1, $lineItems['count']);
451 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 452 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6a488035
TO
453 $this->_checkFinancialRecords($contribution, 'offline');
454 $this->contributionGetnCheck($params, $contribution['id']);
455 }
5896d037 456
f70a6752 457 /**
28de42d1 458 * Test create with valid payment instrument.
6a488035 459 */
00be9182 460 public function testCreateContributionWithPaymentInstrument() {
6a488035 461 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 462 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 463 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 464 'sequential' => 1,
21dfd5f5 465 'id' => $contribution['id'],
53191813 466 ));
6a488035 467 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
468 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
469
5896d037 470 $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
471 'id' => $contribution['id'],
472 'payment_instrument' => 'Credit Card',
473 ));
6c6e6187 474 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 475 'sequential' => 1,
21dfd5f5 476 'id' => $contribution['id'],
53191813
CW
477 ));
478 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
479 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
6a488035
TO
480 }
481
00be9182 482 public function testGetContributionByPaymentInstrument() {
6a488035 483 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 484 $params2 = $this->_params + array('payment_instrument' => 'Cash');
6c6e6187
TO
485 $this->callAPISuccess('contribution', 'create', $params);
486 $this->callAPISuccess('contribution', 'create', $params2);
5896d037 487 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55 488 'sequential' => 1,
7d543448 489 'contribution_payment_instrument' => 'Cash',
92915c55 490 ));
6a488035 491 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
ff977830
EM
492 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
493 $this->assertEquals(1, $contribution['count']);
868e247d 494 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
6a488035 495 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
868e247d 496 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
6a488035 497 $this->assertEquals(1, $contribution['count']);
5896d037 498 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
499 'sequential' => 1,
500 'payment_instrument_id' => 5,
501 ));
6a488035 502 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
503 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
504 $this->assertEquals(1, $contribution['count']);
5896d037 505 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
506 'sequential' => 1,
507 'payment_instrument' => 'EFT',
508 ));
6a488035 509 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
510 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
511 $this->assertEquals(1, $contribution['count']);
5896d037 512 $contribution = $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
513 'id' => $contribution['id'],
514 'payment_instrument' => 'Credit Card',
515 ));
5896d037 516 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
6a488035 517 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
518 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
519 $this->assertEquals(1, $contribution['count']);
6a488035
TO
520 }
521
b5a37491
EM
522 /**
523 * CRM-16227 introduces invoice_id as a parameter.
524 */
525 public function testGetContributionByInvoice() {
526 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
527 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
528 $this->callAPISuccessGetCount('Contribution', array(), 2);
529 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
530 // The following don't work. They are the format we are trying to introduce but although the form uses this format
531 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
532 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
533 // come out of convertFormValues
534 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
535 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
536 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
537 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
538 // 2);
539 }
540
c490a46a 541 /**
eceb18cc 542 * Create test with unique field name on source.
c490a46a 543 */
00be9182 544 public function testCreateContributionSource() {
6a488035
TO
545
546 $params = array(
547 'contact_id' => $this->_individualId,
548 'receive_date' => date('Ymd'),
549 'total_amount' => 100.00,
4ab7d517 550 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
551 'payment_instrument_id' => 1,
552 'non_deductible_amount' => 10.00,
553 'fee_amount' => 50.00,
554 'net_amount' => 90.00,
555 'trxn_id' => 12345,
556 'invoice_id' => 67890,
557 'contribution_source' => 'SSF',
558 'contribution_status_id' => 1,
6a488035
TO
559 );
560
4ab7d517 561 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
562 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
563 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
6a488035 564 }
d5d148ab 565
d424ffde 566 /**
eceb18cc 567 * Create test with unique field name on source.
d424ffde 568 */
00be9182 569 public function testCreateDefaultNow() {
d5d148ab
EM
570
571 $params = $this->_params;
572 unset($params['receive_date']);
573
574 $contribution = $this->callAPISuccess('contribution', 'create', $params);
575 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
576 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
577 }
578
c490a46a 579 /**
55d2c6f1 580 * Create test with unique field name on source.
c490a46a 581 */
55d2c6f1 582 public function testCreateContributionSourceInvalidContact() {
6a488035
TO
583
584 $params = array(
585 'contact_id' => 999,
586 'receive_date' => date('Ymd'),
587 'total_amount' => 100.00,
4ab7d517 588 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
589 'payment_instrument_id' => 1,
590 'non_deductible_amount' => 10.00,
591 'fee_amount' => 50.00,
592 'net_amount' => 90.00,
593 'trxn_id' => 12345,
594 'invoice_id' => 67890,
595 'contribution_source' => 'SSF',
596 'contribution_status_id' => 1,
6a488035
TO
597 );
598
858d0bf8 599 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
600 }
601
00be9182 602 public function testCreateContributionSourceInvalidContContact() {
6a488035
TO
603
604 $params = array(
605 'contribution_contact_id' => 999,
606 'receive_date' => date('Ymd'),
607 'total_amount' => 100.00,
4ab7d517 608 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
609 'payment_instrument_id' => 1,
610 'non_deductible_amount' => 10.00,
611 'fee_amount' => 50.00,
612 'net_amount' => 90.00,
613 'trxn_id' => 12345,
614 'invoice_id' => 67890,
615 'contribution_source' => 'SSF',
616 'contribution_status_id' => 1,
6a488035
TO
617 );
618
858d0bf8 619 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
620 }
621
858d0bf8 622 /**
442cf836 623 * Test note created correctly.
858d0bf8 624 */
00be9182 625 public function testCreateContributionWithNote() {
5c49fee0 626 $description = "Demonstrates creating contribution with Note Entity.";
5896d037
TO
627 $subfile = "ContributionCreateWithNote";
628 $params = array(
6a488035
TO
629 'contact_id' => $this->_individualId,
630 'receive_date' => '2012-01-01',
631 'total_amount' => 100.00,
4ab7d517 632 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
633 'payment_instrument_id' => 1,
634 'non_deductible_amount' => 10.00,
635 'fee_amount' => 50.00,
636 'net_amount' => 90.00,
637 'trxn_id' => 12345,
638 'invoice_id' => 67890,
639 'source' => 'SSF',
640 'contribution_status_id' => 1,
6a488035
TO
641 'note' => 'my contribution note',
642 );
643
4ab7d517 644 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
5896d037 645 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
646 'entity_table' => 'civicrm_contribution',
647 'entity_id' => $contribution['id'],
648 'sequential' => 1,
649 ));
6a488035 650 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 651 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
652 }
653
00be9182 654 public function testCreateContributionWithNoteUniqueNameAliases() {
5896d037 655 $params = array(
6a488035
TO
656 'contact_id' => $this->_individualId,
657 'receive_date' => '2012-01-01',
658 'total_amount' => 100.00,
4ab7d517 659 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
660 'payment_instrument_id' => 1,
661 'non_deductible_amount' => 10.00,
662 'fee_amount' => 50.00,
663 'net_amount' => 90.00,
664 'trxn_id' => 12345,
665 'invoice_id' => 67890,
666 'source' => 'SSF',
667 'contribution_status_id' => 1,
6a488035
TO
668 'contribution_note' => 'my contribution note',
669 );
670
4ab7d517 671 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 672 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
673 'entity_table' => 'civicrm_contribution',
674 'entity_id' => $contribution['id'],
675 'sequential' => 1,
676 ));
6a488035 677 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 678 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035 679 }
c490a46a
CW
680
681 /**
55d2c6f1 682 * This is the test for creating soft credits.
c490a46a 683 */
55d2c6f1 684 public function testCreateContributionWithSoftCredit() {
5c49fee0 685 $description = "Demonstrates creating contribution with SoftCredit.";
5896d037
TO
686 $subfile = "ContributionCreateWithSoftCredit";
687 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
688 'display_name' => 'superman',
689 'contact_type' => 'Individual',
690 ));
55d2c6f1 691 $softParams = array(
bcc03b98 692 'contact_id' => $contact2['id'],
693 'amount' => 50,
21dfd5f5 694 'soft_credit_type_id' => 3,
6a488035
TO
695 );
696
55d2c6f1 697 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
4ab7d517 698 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 699 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
a1c68fd2 700
55d2c6f1
EM
701 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
702 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
703 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a516bd6
DG
704
705 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
706 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
707 }
708
00be9182 709 public function testCreateContributionWithSoftCreditDefaults() {
5c49fee0 710 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
5896d037
TO
711 $subfile = "ContributionCreateWithSoftCreditDefaults";
712 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
713 'display_name' => 'superman',
714 'contact_type' => 'Individual',
715 ));
6a516bd6 716 $params = $this->_params + array(
442cf836
EM
717 'soft_credit_to' => $contact2['id'],
718 );
6a516bd6 719 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 720 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
721
722 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
723 // Default soft credit amount = contribution.total_amount
724 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
725 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
726
727 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
728 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
729 }
730
00be9182 731 public function testCreateContributionWithHonoreeContact() {
5c49fee0 732 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
5896d037
TO
733 $subfile = "ContributionCreateWithHonoreeContact";
734 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
735 'display_name' => 'superman',
736 'contact_type' => 'Individual',
737 ));
6a516bd6 738 $params = $this->_params + array(
442cf836
EM
739 'honor_contact_id' => $contact2['id'],
740 );
6a516bd6 741 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 742 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
743
744 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
745 // Default soft credit amount = contribution.total_amount
746 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
747 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
748 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a488035 749
4ab7d517 750 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
751 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
6a488035
TO
752 }
753
754 /**
92c99a4a 755 * Test using example code.
6a488035 756 */
00be9182 757 public function testContributionCreateExample() {
6a488035 758 //make sure at least on page exists since there is a truncate in tear down
8be629ac 759 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
3ec6e38d 760 require_once 'api/v3/examples/Contribution/Create.php';
5896d037 761 $result = contribution_create_example();
006d6361 762 $id = $result['id'];
6a488035 763 $expectedResult = contribution_create_expectedresult();
8e342a79 764 $this->checkArrayEquals($expectedResult, $result);
006d6361 765 $this->contributionDelete($id);
6a488035
TO
766 }
767
a1a2a83d 768 /**
f55c5fa8 769 * Function tests that additional financial records are created when fee amount is recorded.
6a488035 770 */
00be9182 771 public function testCreateContributionWithFee() {
6a488035
TO
772 $params = array(
773 'contact_id' => $this->_individualId,
774 'receive_date' => '20120511',
775 'total_amount' => 100.00,
776 'fee_amount' => 50,
777 'financial_type_id' => 1,
778 'trxn_id' => 12345,
779 'invoice_id' => 67890,
780 'source' => 'SSF',
781 'contribution_status_id' => 1,
6a488035
TO
782 );
783
4ab7d517 784 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
785 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
786 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
e0e3c51b
EM
787 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
788 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
5896d037 789 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
790 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
791 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
792 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
793 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
e0e3c51b 794
6c6e6187 795 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 796
6a488035
TO
797 'entity_id' => $contribution['id'],
798 'entity_table' => 'civicrm_contribution',
799 'sequential' => 1,
5896d037 800 ));
6a488035
TO
801 $this->assertEquals(1, $lineItems['count']);
802 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 803 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6c6e6187 804 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 805
5896d037
TO
806 'entity_id' => $contribution['id'],
807 'contribution_id' => $contribution['id'],
808 'entity_table' => 'civicrm_contribution',
809 'sequential' => 1,
6a488035
TO
810 ));
811 $this->assertEquals(1, $lineItems['count']);
812 $this->_checkFinancialRecords($contribution, 'feeAmount');
813 }
814
815
f70a6752 816 /**
442cf836 817 * Function tests that additional financial records are created when online contribution is created.
6a488035 818 */
00be9182 819 public function testCreateContributionOnline() {
858d0bf8 820 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 821 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 822 $this->assertAPISuccess($contributionPage);
6a488035
TO
823 $params = array(
824 'contact_id' => $this->_individualId,
825 'receive_date' => '20120511',
826 'total_amount' => 100.00,
827 'financial_type_id' => 1,
828 'contribution_page_id' => $contributionPage['id'],
829 'payment_processor' => 1,
830 'trxn_id' => 12345,
831 'invoice_id' => 67890,
832 'source' => 'SSF',
833 'contribution_status_id' => 1,
4ab7d517 834
6a488035
TO
835 );
836
4ab7d517 837 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
838 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
839 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 840 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
841 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
842 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
843 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
844 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
6a488035
TO
845 $this->_checkFinancialRecords($contribution, 'online');
846 }
847
f70a6752 848 /**
442cf836
EM
849 * Check handling of financial type.
850 *
100fef9d 851 * In the interests of removing financial type / contribution type checks from
f70a6752 852 * legacy format function lets test that the api is doing this for us
853 */
00be9182 854 public function testCreateInvalidFinancialType() {
f70a6752 855 $params = $this->_params;
856 $params['financial_type_id'] = 99999;
858d0bf8 857 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
f70a6752 858 }
859
4302618d 860 /**
442cf836
EM
861 * Check handling of financial type.
862 *
100fef9d 863 * In the interests of removing financial type / contribution type checks from
4302618d 864 * legacy format function lets test that the api is doing this for us
865 */
00be9182 866 public function testValidNamedFinancialType() {
4302618d 867 $params = $this->_params;
868 $params['financial_type_id'] = 'Donation';
858d0bf8 869 $this->callAPISuccess($this->_entity, 'create', $params);
4302618d 870 }
871
f70a6752 872 /**
92c99a4a
EM
873 * Tests that additional financial records are created.
874 *
875 * Checks when online contribution with pay later option is created
6a488035 876 */
00be9182 877 public function testCreateContributionPayLaterOnline() {
858d0bf8 878 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
6a488035 879 $this->_pageParams['is_pay_later'] = 1;
5896d037 880 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 881 $this->assertAPISuccess($contributionPage);
6a488035
TO
882 $params = array(
883 'contact_id' => $this->_individualId,
884 'receive_date' => '20120511',
885 'total_amount' => 100.00,
886 'financial_type_id' => 1,
887 'contribution_page_id' => $contributionPage['id'],
888 'trxn_id' => 12345,
889 'is_pay_later' => 1,
890 'invoice_id' => 67890,
891 'source' => 'SSF',
892 'contribution_status_id' => 2,
4ab7d517 893
6a488035
TO
894 );
895
4ab7d517 896 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
897 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
898 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 899 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
900 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
901 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
902 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
903 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
904 $this->_checkFinancialRecords($contribution, 'payLater');
905 }
906
a1a2a83d 907 /**
1e52837d 908 * Function tests that additional financial records are created for online contribution with pending option.
6a488035 909 */
00be9182 910 public function testCreateContributionPendingOnline() {
6a488035 911 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 912 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 913 $this->assertAPISuccess($contributionPage);
6a488035
TO
914 $params = array(
915 'contact_id' => $this->_individualId,
916 'receive_date' => '20120511',
917 'total_amount' => 100.00,
918 'financial_type_id' => 1,
919 'contribution_page_id' => $contributionPage['id'],
920 'trxn_id' => 12345,
921 'invoice_id' => 67890,
922 'source' => 'SSF',
923 'contribution_status_id' => 2,
6a488035
TO
924 );
925
4ab7d517 926 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
927 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
928 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 929 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
930 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
931 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
932 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
933 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
934 $this->_checkFinancialRecords($contribution, 'pending');
935 }
936
e748bf60 937 /**
92c99a4a 938 * Test that BAO defaults work.
e748bf60 939 */
00be9182 940 public function testCreateBAODefaults() {
e748bf60
EM
941 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
942 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
5896d037 943 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
944 'id' => $contribution['id'],
945 'api.contribution.delete' => 1,
946 ));
e748bf60 947 $this->assertEquals(1, $contribution['contribution_status_id']);
12879069 948 $this->assertEquals('Check', $contribution['payment_instrument']);
e748bf60
EM
949 }
950
a1a2a83d 951 /**
1e52837d 952 * Function tests that line items, financial records are updated when contribution amount is changed.
6a488035 953 */
00be9182 954 public function testCreateUpdateContributionChangeTotal() {
4ab7d517 955 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
6c6e6187 956 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
4ab7d517 957
6a488035
TO
958 'entity_id' => $contribution['id'],
959 'entity_table' => 'civicrm_contribution',
960 'sequential' => 1,
961 'return' => 'line_total',
962 ));
963 $this->assertEquals('100.00', $lineItems);
964 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
965 // Financial trxn SUM = 100 + 5 (fee)
966 $this->assertEquals('105.00', $trxnAmount);
967 $newParams = array(
4ab7d517 968
6a488035 969 'id' => $contribution['id'],
21dfd5f5 970 'total_amount' => '125',
5896d037 971 );
694769da 972 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
4ab7d517 973
6c6e6187 974 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
6a488035 975
5896d037
TO
976 'entity_id' => $contribution['id'],
977 'entity_table' => 'civicrm_contribution',
978 'sequential' => 1,
979 'return' => 'line_total',
6a488035
TO
980 ));
981
982 $this->assertEquals('125.00', $lineItems);
983 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
28de42d1
EM
984
985 // Financial trxn SUM = 125 + 5 (fee).
6a488035 986 $this->assertEquals('130.00', $trxnAmount);
28de42d1 987 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
6a488035
TO
988 }
989
a1a2a83d 990 /**
1e52837d 991 * Function tests that line items, financial records are updated when pay later contribution is received.
6a488035 992 */
00be9182 993 public function testCreateUpdateContributionPayLater() {
6a488035
TO
994 $contribParams = array(
995 'contact_id' => $this->_individualId,
996 'receive_date' => '2012-01-01',
997 'total_amount' => 100.00,
4ab7d517 998 'financial_type_id' => $this->_financialTypeId,
6a488035 999 'payment_instrument_id' => 1,
8f39a111 1000 'contribution_status_id' => 2,
1001 'is_pay_later' => 1,
4ab7d517 1002
6a488035 1003 );
4ab7d517 1004 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1005
1006 $newParams = array_merge($contribParams, array(
5896d037
TO
1007 'id' => $contribution['id'],
1008 'contribution_status_id' => 1,
1009 )
c71ae314 1010 );
694769da 1011 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035 1012 $contribution = $contribution['values'][$contribution['id']];
6c6e6187 1013 $this->assertEquals($contribution['contribution_status_id'], '1');
6a488035
TO
1014 $this->_checkFinancialItem($contribution['id'], 'paylater');
1015 $this->_checkFinancialTrxn($contribution, 'payLater');
1016 }
1017
a1a2a83d 1018 /**
eceb18cc 1019 * Function tests that financial records are updated when Payment Instrument is changed.
6a488035 1020 */
00be9182 1021 public function testCreateUpdateContributionPaymentInstrument() {
6a488035
TO
1022 $instrumentId = $this->_addPaymentInstrument();
1023 $contribParams = array(
1024 'contact_id' => $this->_individualId,
1025 'total_amount' => 100.00,
4ab7d517 1026 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1027 'payment_instrument_id' => 4,
1028 'contribution_status_id' => 1,
4ab7d517 1029
6a488035 1030 );
4ab7d517 1031 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1032
1033 $newParams = array_merge($contribParams, array(
5896d037
TO
1034 'id' => $contribution['id'],
1035 'payment_instrument_id' => $instrumentId,
1036 )
6a488035 1037 );
694769da 1038 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
fc928539 1039 $this->assertAPISuccess($contribution);
4ecc6d4b 1040 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
6a488035
TO
1041 }
1042
a1a2a83d 1043 /**
eceb18cc 1044 * Function tests that financial records are added when Contribution is Refunded.
6a488035 1045 */
00be9182 1046 public function testCreateUpdateContributionRefund() {
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' => 4,
1053 'contribution_status_id' => 1,
4ab7d517 1054
6a488035 1055 );
4ab7d517 1056 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1057 $newParams = array_merge($contribParams, array(
5896d037 1058 'id' => $contribution['id'],
b7990bb6 1059 'contribution_status_id' => 'Refunded',
1060 'cancel_date' => '2015-01-01 09:00',
6a488035
TO
1061 )
1062 );
1063
694769da 1064 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1065 $this->_checkFinancialTrxn($contribution, 'refund');
1066 $this->_checkFinancialItem($contribution['id'], 'refund');
8f39a111 1067 }
c71ae314 1068
a1a2a83d 1069 /**
eceb18cc 1070 * Function tests invalid contribution status change.
c71ae314 1071 */
00be9182 1072 public function testCreateUpdateContributionInValidStatusChange() {
c71ae314
PN
1073 $contribParams = array(
1074 'contact_id' => 1,
1075 'receive_date' => '2012-01-01',
1076 'total_amount' => 100.00,
1077 'financial_type_id' => 1,
1078 'payment_instrument_id' => 1,
1079 'contribution_status_id' => 1,
c71ae314 1080 );
4ab7d517 1081 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
c71ae314 1082 $newParams = array_merge($contribParams, array(
5896d037
TO
1083 'id' => $contribution['id'],
1084 'contribution_status_id' => 2,
c71ae314
PN
1085 )
1086 );
6c6e6187 1087 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1088
6a488035
TO
1089 }
1090
a1a2a83d 1091 /**
eceb18cc 1092 * Function tests that financial records are added when Pending Contribution is Canceled.
6a488035 1093 */
00be9182 1094 public function testCreateUpdateContributionCancelPending() {
6a488035
TO
1095 $contribParams = array(
1096 'contact_id' => $this->_individualId,
1097 'receive_date' => '2012-01-01',
1098 'total_amount' => 100.00,
4ab7d517 1099 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1100 'payment_instrument_id' => 1,
1101 'contribution_status_id' => 2,
c71ae314 1102 'is_pay_later' => 1,
4ab7d517 1103
6a488035 1104 );
4ab7d517 1105 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1106 $newParams = array_merge($contribParams, array(
5896d037
TO
1107 'id' => $contribution['id'],
1108 'contribution_status_id' => 3,
6a488035
TO
1109 )
1110 );
694769da 1111 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1112 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1113 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1114 }
1115
a1a2a83d 1116 /**
eceb18cc 1117 * Function tests that financial records are added when Financial Type is Changed.
6a488035 1118 */
00be9182 1119 public function testCreateUpdateContributionChangeFinancialType() {
6a488035
TO
1120 $contribParams = array(
1121 'contact_id' => $this->_individualId,
1122 'receive_date' => '2012-01-01',
1123 'total_amount' => 100.00,
1124 'financial_type_id' => 1,
1125 'payment_instrument_id' => 1,
1126 'contribution_status_id' => 1,
4ab7d517 1127
6a488035 1128 );
4ab7d517 1129 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1130 $newParams = array_merge($contribParams, array(
5896d037
TO
1131 'id' => $contribution['id'],
1132 'financial_type_id' => 3,
6a488035
TO
1133 )
1134 );
694769da 1135 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1136 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1137 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1138 }
1139
694769da 1140 /**
1e52837d 1141 * Test that update does not change status id CRM-15105.
694769da 1142 */
00be9182 1143 public function testCreateUpdateWithoutChangingPendingStatus() {
694769da
VU
1144 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1145 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
5896d037 1146 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1147 'id' => $contribution['id'],
1148 'api.contribution.delete' => 1,
1149 ));
694769da
VU
1150 $this->assertEquals(2, $contribution['contribution_status_id']);
1151 }
a1a2a83d
TO
1152
1153 /**
28de42d1
EM
1154 * Test Updating a Contribution.
1155 *
a1a2a83d
TO
1156 * CHANGE: we require the API to do an incremental update
1157 */
00be9182 1158 public function testCreateUpdateContribution() {
6a488035 1159
78ab0ca4 1160 $contributionID = $this->contributionCreate(array(
1161 'contact_id' => $this->_individualId,
1162 'trxn_id' => 212355,
1163 'financial_type_id' => $this->_financialTypeId,
1164 'invoice_id' => 'old_invoice',
1165 ));
6a488035
TO
1166 $old_params = array(
1167 'contribution_id' => $contributionID,
6a488035 1168 );
4ab7d517 1169 $original = $this->callAPISuccess('contribution', 'get', $old_params);
2bfae985 1170 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1171 //set up list of old params, verify
1172
1173 //This should not be required on update:
1174 $old_contact_id = $original['values'][$contributionID]['contact_id'];
7d543448 1175 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
6a488035
TO
1176 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1177 $old_source = $original['values'][$contributionID]['contribution_source'];
1178
6a488035
TO
1179 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1180 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1181
1182 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1183 $this->assertEquals($old_contact_id, $this->_individualId);
1184 $this->assertEquals($old_fee_amount, 5.00);
1185 $this->assertEquals($old_source, 'SSF');
1186 $this->assertEquals($old_trxn_id, 212355);
78ab0ca4 1187 $this->assertEquals($old_invoice_id, 'old_invoice');
6a488035
TO
1188 $params = array(
1189 'id' => $contributionID,
1190 'contact_id' => $this->_individualId,
1191 'total_amount' => 110.00,
4ab7d517 1192 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1193 'non_deductible_amount' => 10.00,
1194 'net_amount' => 100.00,
1195 'contribution_status_id' => 1,
1196 'note' => 'Donating for Nobel Cause',
4ab7d517 1197
6a488035
TO
1198 );
1199
4ab7d517 1200 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1201
1202 $new_params = array(
1203 'contribution_id' => $contribution['id'],
4ab7d517 1204
6a488035 1205 );
4ab7d517 1206 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
6a488035 1207
2bfae985
EM
1208 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1209 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
ff977830 1210 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
7d543448 1211 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
2bfae985
EM
1212 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1213 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1214 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1215 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1216 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1217 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1218 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
6a488035
TO
1219 $params = array(
1220 'contribution_id' => $contributionID,
4ab7d517 1221
6a488035 1222 );
4ab7d517 1223 $result = $this->callAPISuccess('contribution', 'delete', $params);
22f80e87 1224 $this->assertAPISuccess($result);
6a488035
TO
1225 }
1226
1227 ///////////////// civicrm_contribution_delete methods
a1a2a83d
TO
1228
1229 /**
1230 * Attempt (but fail) to delete a contribution without parameters.
1231 */
00be9182 1232 public function testDeleteEmptyParamsContribution() {
4ab7d517 1233 $params = array();
858d0bf8 1234 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1235 }
1236
00be9182 1237 public function testDeleteParamsNotArrayContribution() {
6a488035 1238 $params = 'contribution_id= 1';
d0e1eff2 1239 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1240 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1241 }
1242
00be9182 1243 public function testDeleteWrongParamContribution() {
6a488035
TO
1244 $params = array(
1245 'contribution_source' => 'SSF',
4ab7d517 1246
6a488035 1247 );
858d0bf8 1248 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1249 }
1250
00be9182 1251 public function testDeleteContribution() {
78ab0ca4 1252 $contributionID = $this->contributionCreate(array(
1253 'contact_id' => $this->_individualId,
1254 'financial_type_id' => $this->_financialTypeId,
1255 ));
6a488035
TO
1256 $params = array(
1257 'id' => $contributionID,
6a488035 1258 );
4ab7d517 1259 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1260 }
1261
1262 /**
d177a2a6 1263 * Test civicrm_contribution_search with empty params.
1e52837d 1264 *
d177a2a6 1265 * All available contributions expected.
6a488035 1266 */
00be9182 1267 public function testSearchEmptyParams() {
4ab7d517 1268 $params = array();
6a488035
TO
1269
1270 $p = array(
1271 'contact_id' => $this->_individualId,
1272 'receive_date' => date('Ymd'),
1273 'total_amount' => 100.00,
4ab7d517 1274 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1275 'non_deductible_amount' => 10.00,
1276 'fee_amount' => 5.00,
1277 'net_amount' => 95.00,
1278 'trxn_id' => 23456,
1279 'invoice_id' => 78910,
1280 'source' => 'SSF',
1281 'contribution_status_id' => 1,
4ab7d517 1282
6a488035 1283 );
4ab7d517 1284 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1285
4ab7d517 1286 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1287 // We're taking the first element.
1288 $res = $result['values'][$contribution['id']];
1289
2bfae985
EM
1290 $this->assertEquals($p['contact_id'], $res['contact_id']);
1291 $this->assertEquals($p['total_amount'], $res['total_amount']);
5896d037 1292 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1293 $this->assertEquals($p['net_amount'], $res['net_amount']);
1294 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1295 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1296 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1297 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1298 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1299 // contribution_status_id = 1 => Completed
2bfae985 1300 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1301
1302 $this->contributionDelete($contribution['id']);
1303 }
1304
1305 /**
d177a2a6 1306 * Test civicrm_contribution_search. Success expected.
6a488035 1307 */
00be9182 1308 public function testSearch() {
6a488035
TO
1309 $p1 = array(
1310 'contact_id' => $this->_individualId,
1311 'receive_date' => date('Ymd'),
1312 'total_amount' => 100.00,
4ab7d517 1313 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1314 'non_deductible_amount' => 10.00,
1315 'contribution_status_id' => 1,
4ab7d517 1316
6a488035 1317 );
4ab7d517 1318 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1319
1320 $p2 = array(
1321 'contact_id' => $this->_individualId,
1322 'receive_date' => date('Ymd'),
1323 'total_amount' => 200.00,
4ab7d517 1324 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1325 'non_deductible_amount' => 20.00,
1326 'trxn_id' => 5454565,
1327 'invoice_id' => 1212124,
1328 'fee_amount' => 50.00,
1329 'net_amount' => 60.00,
1330 'contribution_status_id' => 2,
4ab7d517 1331
6a488035 1332 );
2f45e1c2 1333 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1334
1335 $params = array(
1336 'contribution_id' => $contribution2['id'],
4ab7d517 1337
6a488035 1338 );
2f45e1c2 1339 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1340 $res = $result['values'][$contribution2['id']];
1341
2bfae985
EM
1342 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1343 $this->assertEquals($p2['total_amount'], $res['total_amount']);
5896d037 1344 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1345 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1346 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1347 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1348 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1349 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
6a488035 1350 // contribution_status_id = 2 => Pending
2bfae985 1351 $this->assertEquals('Pending', $res['contribution_status']);
6a488035
TO
1352
1353 $this->contributionDelete($contribution1['id']);
1354 $this->contributionDelete($contribution2['id']);
1355 }
2f45e1c2 1356
0efa8efe 1357 /**
eceb18cc 1358 * Test completing a transaction via the API.
0efa8efe 1359 *
1360 * Note that we are creating a logged in user because email goes out from
1361 * that person
1362 */
00be9182 1363 public function testCompleteTransaction() {
5896d037 1364 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 1365 $this->createLoggedInUser();
6c6e6187
TO
1366 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1367 $contribution = $this->callAPISuccess('contribution', 'create', $params);
66d3f9be 1368 $this->callAPISuccess('contribution', 'completetransaction', array(
0efa8efe 1369 'id' => $contribution['id'],
66d3f9be 1370 ));
6c6e6187 1371 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
0efa8efe 1372 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1373 $mut->checkMailLog(array(
1374 'Receipt - Contribution',
1375 'Please print this confirmation for your records.',
1376 ));
46fa5206
EM
1377 $mut->stop();
1378 }
1379
080a561b 1380 /**
1381 * Test completing a transaction via the API.
1382 *
1383 * Note that we are creating a logged in user because email goes out from
1384 * that person
1385 */
1386 public function testCompleteTransactionFeeAmount() {
1387 $this->createLoggedInUser();
1388 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1389 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1390 $this->callAPISuccess('contribution', 'completetransaction', array(
1391 'id' => $contribution['id'],
1392 'fee_amount' => '.56',
1393 'trxn_id' => '7778888',
1394 ));
1395 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1396 $this->assertEquals('Completed', $contribution['contribution_status']);
1397 $this->assertEquals('7778888', $contribution['trxn_id']);
1398 $this->assertEquals('.56', $contribution['fee_amount']);
1399 $this->assertEquals('99.44', $contribution['net_amount']);
1400 }
1401
d97c96dc
EM
1402 /**
1403 * Test repeat contribution successfully creates line items.
1404 */
1e52837d 1405 public function testRepeatTransaction() {
d97c96dc
EM
1406 $paymentProcessorID = $this->paymentProcessorCreate();
1407 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1408 'contact_id' => $this->_individualId,
1409 'installments' => '12',
1410 'frequency_interval' => '1',
1411 'amount' => '500',
1412 'contribution_status_id' => 1,
1413 'start_date' => '2012-01-01 00:00:00',
1414 'currency' => 'USD',
1415 'frequency_unit' => 'month',
1416 'payment_processor_id' => $paymentProcessorID,
1417 ));
1418 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1419 $this->_params,
1420 array('contribution_recur_id' => $contributionRecur['id']))
1421 );
1422
1423 $this->callAPISuccess('contribution', 'repeattransaction', array(
1424 'original_contribution_id' => $originalContribution['id'],
1425 'contribution_status_id' => 'Completed',
1426 'trxn_id' => uniqid(),
1427 ));
1428 $lineItemParams = array(
1429 'entity_id' => $originalContribution['id'],
1430 'sequential' => 1,
1431 'return' => array(
1432 'entity_table',
1433 'qty',
1434 'unit_price',
1435 'line_total',
1436 'label',
1437 'financial_type_id',
1438 'deductible_amount',
1439 'price_field_value_id',
1440 'price_field_id',
1e52837d 1441 ),
d97c96dc
EM
1442 );
1443 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1444 'entity_id' => $originalContribution['id'],
1445 )));
1446 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1447 'entity_id' => $originalContribution['id'] + 1,
1448 )));
1449 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1450 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1451 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1452
1453 $this->quickCleanUpFinancialEntities();
1454 }
1455
1eade77d 1456 /**
1457 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1458 */
1459 public function testRepeatTransactionAcceptRecurID() {
1460 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1461 'contact_id' => $this->_individualId,
1462 'installments' => '12',
1463 'frequency_interval' => '1',
1464 'amount' => '100',
1465 'contribution_status_id' => 1,
1466 'start_date' => '2012-01-01 00:00:00',
1467 'currency' => 'USD',
1468 'frequency_unit' => 'month',
1469 'payment_processor_id' => $this->paymentProcessorID,
1470 ));
1471 $this->callAPISuccess('contribution', 'create', array_merge(
1472 $this->_params,
1473 array('contribution_recur_id' => $contributionRecur['id']))
1474 );
1475
1476 $this->callAPISuccess('contribution', 'repeattransaction', array(
1477 'contribution_recur_id' => $contributionRecur['id'],
1478 'contribution_status_id' => 'Completed',
1479 'trxn_id' => uniqid(),
1480 ));
1481
1482 $this->quickCleanUpFinancialEntities();
1483 }
1484
c03f1689
EM
1485 /**
1486 * CRM-16397 test appropriate action if total amount has changed for single line items.
1487 */
1488 public function testRepeatTransactionAlteredAmount() {
1489 $paymentProcessorID = $this->paymentProcessorCreate();
c03f1689
EM
1490 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1491 'contact_id' => $this->_individualId,
1492 'installments' => '12',
1493 'frequency_interval' => '1',
1494 'amount' => '500',
1495 'contribution_status_id' => 1,
1496 'start_date' => '2012-01-01 00:00:00',
1497 'currency' => 'USD',
1498 'frequency_unit' => 'month',
1499 'payment_processor_id' => $paymentProcessorID,
1500 ));
1501 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1502 $this->_params,
1503 array(
1504 'contribution_recur_id' => $contributionRecur['id'],
c03f1689
EM
1505 ))
1506 );
1507
1508 $this->callAPISuccess('contribution', 'repeattransaction', array(
1509 'original_contribution_id' => $originalContribution['id'],
1510 'contribution_status_id' => 'Completed',
1511 'trxn_id' => uniqid(),
1512 'total_amount' => '400',
1513 'fee_amount' => 50,
c03f1689
EM
1514 ));
1515 $lineItemParams = array(
1516 'entity_id' => $originalContribution['id'],
1517 'sequential' => 1,
1518 'return' => array(
1519 'entity_table',
1520 'qty',
1521 'unit_price',
1522 'line_total',
1523 'label',
1524 'financial_type_id',
1525 'deductible_amount',
1526 'price_field_value_id',
1527 'price_field_id',
1528 ),
1529 );
1530 $this->callAPISuccessGetSingle('contribution', array(
1531 'total_amount' => 400,
c03f1689
EM
1532 'fee_amount' => 50,
1533 'net_amount' => 350,
1534 ));
1535 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1536 'entity_id' => $originalContribution['id'],
1537 )));
1538 $expectedLineItem = array_merge(
1539 $lineItem1['values'][0], array(
1540 'line_total' => '400.00',
1541 'unit_price' => '400.00',
542d9e2c
EM
1542 )
1543 );
c03f1689
EM
1544
1545 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1546 'entity_id' => $originalContribution['id'] + 1,
1547 )));
1548 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1549 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1550 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
c02c17df 1551 }
c03f1689 1552
c02c17df 1553 /**
1eade77d 1554 * CRM-16397 test appropriate action if campaign has been passed in.
c02c17df 1555 */
1556 public function testRepeatTransactionPassedInCampaign() {
1557 $paymentProcessorID = $this->paymentProcessorCreate();
1558 $campaignID = $this->campaignCreate();
1559 $campaignID2 = $this->campaignCreate();
1560 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1561 'contact_id' => $this->_individualId,
1562 'installments' => '12',
1563 'frequency_interval' => '1',
1564 'amount' => '100',
1565 'contribution_status_id' => 1,
1566 'start_date' => '2012-01-01 00:00:00',
1567 'currency' => 'USD',
1568 'frequency_unit' => 'month',
1569 'payment_processor_id' => $paymentProcessorID,
1570 ));
1571 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1572 $this->_params,
1573 array(
1574 'contribution_recur_id' => $contributionRecur['id'],
1575 'campaign_id' => $campaignID,
1576 ))
1577 );
1578
1579 $this->callAPISuccess('contribution', 'repeattransaction', array(
1580 'original_contribution_id' => $originalContribution['id'],
1581 'contribution_status_id' => 'Completed',
1582 'trxn_id' => uniqid(),
1583 'campaign_id' => $campaignID2,
1584 ));
1585
1586 $this->callAPISuccessGetSingle('contribution', array(
1587 'total_amount' => 100,
1588 'campaign_id' => $campaignID2,
1589 ));
1590 }
1591
1592 /**
1593 * CRM-17718 campaign stored on contribution recur gets priority.
1594 *
1595 * This reflects the fact we permit people to update them.
1596 */
1597 public function testRepeatTransactionUpdatedCampaign() {
1598 $paymentProcessorID = $this->paymentProcessorCreate();
1599 $campaignID = $this->campaignCreate();
1600 $campaignID2 = $this->campaignCreate();
1601 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1602 'contact_id' => $this->_individualId,
1603 'installments' => '12',
1604 'frequency_interval' => '1',
1605 'amount' => '100',
1606 'contribution_status_id' => 1,
1607 'start_date' => '2012-01-01 00:00:00',
1608 'currency' => 'USD',
1609 'frequency_unit' => 'month',
1610 'payment_processor_id' => $paymentProcessorID,
1611 'campaign_id' => $campaignID,
1612 ));
1613 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1614 $this->_params,
1615 array(
1616 'contribution_recur_id' => $contributionRecur['id'],
1617 'campaign_id' => $campaignID2,
1618 ))
1619 );
1620
1621 $this->callAPISuccess('contribution', 'repeattransaction', array(
1622 'original_contribution_id' => $originalContribution['id'],
1623 'contribution_status_id' => 'Completed',
1624 'trxn_id' => uniqid(),
1625 ));
1626
1627 $this->callAPISuccessGetSingle('contribution', array(
1628 'total_amount' => 100,
1629 'campaign_id' => $campaignID,
1630 ));
c03f1689
EM
1631 }
1632
2936c3b5
EM
1633 /**
1634 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1635 */
1636 public function testCompleteTransactionNetAmountOK() {
1637 $this->createLoggedInUser();
1638 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1639 unset($params['net_amount']);
1640 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1641 $this->callAPISuccess('contribution', 'completetransaction', array(
1642 'id' => $contribution['id'],
1643 ));
1644 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1645 $this->assertEquals('Completed', $contribution['contribution_status']);
1646 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1647 }
1648
46fa5206 1649 /**
1e52837d 1650 * CRM-14151 - Test completing a transaction via the API.
46fa5206 1651 */
00be9182 1652 public function testCompleteTransactionWithReceiptDateSet() {
5896d037 1653 $mut = new CiviMailUtils($this, TRUE);
46fa5206 1654 $this->createLoggedInUser();
6c6e6187
TO
1655 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1656 $contribution = $this->callAPISuccess('contribution', 'create', $params);
7104593e 1657 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
6c6e6187 1658 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
46fa5206 1659 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
7104593e 1660 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
46fa5206
EM
1661 $mut->checkMailLog(array(
1662 'Receipt - Contribution',
1663 'Please print this confirmation for your records.',
1664 ));
0efa8efe 1665 $mut->stop();
1666 }
1667
91259407 1668 /**
1669 * Test completing first transaction in a recurring series.
1670 *
1671 * The status should be set to 'in progress' and the next scheduled payment date calculated.
1672 */
1673 public function testCompleteTransactionSetStatusToInProgress() {
1674 $paymentProcessorID = $this->paymentProcessorCreate();
1675 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1676 'contact_id' => $this->_individualId,
1677 'installments' => '12',
1678 'frequency_interval' => '1',
1679 'amount' => '500',
1680 'contribution_status_id' => 'Pending',
1681 'start_date' => '2012-01-01 00:00:00',
1682 'currency' => 'USD',
1683 'frequency_unit' => 'month',
1684 'payment_processor_id' => $paymentProcessorID,
1685 ));
1686 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
1687 $this->_params,
1688 array(
1689 'contribution_recur_id' => $contributionRecur['id'],
1690 'contribution_status_id' => 'Pending',
1691 ))
1692 );
1693 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
1694 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
1695 'id' => $contributionRecur['id'],
1696 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
1697 ));
1698 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
1699 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
1700 }
1701
0efa8efe 1702 /**
eceb18cc 1703 * Test completing a transaction with an event via the API.
0efa8efe 1704 *
1705 * Note that we are creating a logged in user because email goes out from
1706 * that person
1707 */
00be9182 1708 public function testCompleteTransactionWithParticipantRecord() {
5896d037 1709 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 1710 $mut->clearMessages();
1711 $this->createLoggedInUser();
1712 $contributionID = $this->createPendingParticipantContribution();
66d3f9be 1713 $this->callAPISuccess('contribution', 'completetransaction', array(
5896d037
TO
1714 'id' => $contributionID,
1715 )
0efa8efe 1716 );
5896d037 1717 $participantStatus = $this->callAPISuccessGetValue('participant', array(
92915c55
TO
1718 'id' => $this->_ids['participant'],
1719 'return' => 'participant_status_id',
1720 ));
0efa8efe 1721 $this->assertEquals(1, $participantStatus);
1722 $mut->checkMailLog(array(
1723 'Annual CiviCRM meet',
1724 'Event',
afc59fef 1725 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 1726 ));
66d3f9be
EM
1727 $mut->stop();
1728 }
1729
1730 /**
eceb18cc 1731 * Test membership is renewed when transaction completed.
66d3f9be 1732 */
00be9182 1733 public function testCompleteTransactionMembershipPriceSet() {
66d3f9be 1734 $this->createPriceSetWithPage('membership');
4ff927bc 1735 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
1736 'name' => 'Grace',
1737 'return' => 'id')
1738 );
66d3f9be 1739 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
4ff927bc 1740 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1741 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1742 'membership_id' => $this->_ids['membership'],
1743 ));
1744 $this->assertEquals(1, $logs['count']);
1745 $this->assertEquals($stateOfGrace, $membership['status_id']);
6c6e6187 1746 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
1747 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1748 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
4ff927bc 1749 $this->callAPISuccessGetSingle('LineItem', array(
1750 'entity_id' => $this->_ids['membership'],
1751 'entity_table' => 'civicrm_membership',
1752 ));
1753 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1754 'membership_id' => $this->_ids['membership'],
1755 ));
1756 $this->assertEquals(2, $logs['count']);
1757 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
66d3f9be
EM
1758 $this->cleanUpAfterPriceSets();
1759 }
1760
1761 /**
eceb18cc 1762 * Test membership is renewed when transaction completed.
66d3f9be 1763 */
00be9182 1764 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
66d3f9be
EM
1765 $this->createPriceSetWithPage('membership');
1766 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
6c6e6187 1767 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
1768 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1769 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1770 $this->cleanUpAfterPriceSets();
1771 }
1772
00be9182 1773 public function cleanUpAfterPriceSets() {
1cf3c2b1 1774 $this->quickCleanUpFinancialEntities();
66d3f9be 1775 $this->contactDelete($this->_ids['contact']);
66d3f9be
EM
1776 }
1777
1778
1779 /**
1e52837d
EM
1780 * Create price set with contribution test for test setup.
1781 *
100fef9d 1782 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
28de42d1 1783 * on parent class at some point (fn is not in 4.4).
1e52837d 1784 *
66d3f9be
EM
1785 * @param $entity
1786 * @param array $params
1787 */
00be9182 1788 public function createPriceSetWithPage($entity, $params = array()) {
66d3f9be
EM
1789 $membershipTypeID = $this->membershipTypeCreate();
1790 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
1791 'title' => "Test Contribution Page",
1792 'financial_type_id' => 1,
1793 'currency' => 'NZD',
1794 'goal_amount' => 50,
1795 'is_pay_later' => 1,
1796 'is_monetary' => TRUE,
1797 'is_email_receipt' => FALSE,
1798 ));
1799 $priceSet = $this->callAPISuccess('price_set', 'create', array(
1800 'is_quick_config' => 0,
1801 'extends' => 'CiviMember',
1802 'financial_type_id' => 1,
21dfd5f5 1803 'title' => 'my Page',
66d3f9be
EM
1804 ));
1805 $priceSetID = $priceSet['id'];
1806
5896d037 1807 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
66d3f9be 1808 $priceField = $this->callAPISuccess('price_field', 'create', array(
5896d037 1809 'price_set_id' => $priceSetID,
66d3f9be
EM
1810 'label' => 'Goat Breed',
1811 'html_type' => 'Radio',
1812 ));
1813 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
5896d037 1814 'price_set_id' => $priceSetID,
66d3f9be
EM
1815 'price_field_id' => $priceField['id'],
1816 'label' => 'Long Haired Goat',
1817 'amount' => 20,
43dbd988 1818 'financial_type_id' => 'Donation',
66d3f9be
EM
1819 'membership_type_id' => $membershipTypeID,
1820 'membership_num_terms' => 1,
1821 )
1822 );
1823 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
1824 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
5896d037 1825 'price_set_id' => $priceSetID,
66d3f9be
EM
1826 'price_field_id' => $priceField['id'],
1827 'label' => 'Shoe-eating Goat',
1828 'amount' => 10,
43dbd988 1829 'financial_type_id' => 'Donation',
66d3f9be
EM
1830 'membership_type_id' => $membershipTypeID,
1831 'membership_num_terms' => 2,
1832 )
1833 );
1834 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
1835 $this->_ids['price_set'] = $priceSetID;
1836 $this->_ids['contribution_page'] = $contributionPageResult['id'];
1837 $this->_ids['price_field'] = array($priceField['id']);
0efa8efe 1838
66d3f9be
EM
1839 $this->_ids['membership_type'] = $membershipTypeID;
1840 }
1841
1842 /**
eceb18cc 1843 * Set up a pending transaction with a specific price field id.
1e52837d 1844 *
100fef9d 1845 * @param int $priceFieldValueID
66d3f9be 1846 */
5896d037 1847 public function setUpPendingContribution($priceFieldValueID) {
66d3f9be
EM
1848 $contactID = $this->individualCreate();
1849 $membership = $this->callAPISuccess('membership', 'create', array(
1850 'contact_id' => $contactID,
1851 'membership_type_id' => $this->_ids['membership_type'],
1852 'start_date' => 'yesterday - 1 year',
1853 'end_date' => 'yesterday',
4ff927bc 1854 'join_date' => 'yesterday - 1 year',
66d3f9be
EM
1855 ));
1856 $contribution = $this->callAPISuccess('contribution', 'create', array(
1857 'domain_id' => 1,
1858 'contact_id' => $contactID,
1859 'receive_date' => date('Ymd'),
1860 'total_amount' => 100.00,
1861 'financial_type_id' => 1,
1862 'payment_instrument_id' => 'Credit Card',
1863 'non_deductible_amount' => 10.00,
1864 'trxn_id' => 'jdhfi88',
1865 'invoice_id' => 'djfhiewuyr',
1866 'source' => 'SSF',
1867 'contribution_status_id' => 2,
1868 'contribution_page_id' => $this->_ids['contribution_page'],
1869 'api.membership_payment.create' => array('membership_id' => $membership['id']),
1870 ));
1871
1872 $this->callAPISuccess('line_item', 'create', array(
1873 'entity_id' => $contribution['id'],
1874 'entity_table' => 'civicrm_contribution',
1274acef 1875 'contribution_id' => $contribution['id'],
66d3f9be
EM
1876 'price_field_id' => $this->_ids['price_field'][0],
1877 'qty' => 1,
1878 'unit_price' => 20,
1879 'line_total' => 20,
1880 'financial_type_id' => 1,
1881 'price_field_value_id' => $priceFieldValueID,
1882 ));
1883 $this->_ids['contact'] = $contactID;
1884 $this->_ids['contribution'] = $contribution['id'];
1885 $this->_ids['membership'] = $membership['id'];
0efa8efe 1886 }
1887
2f45e1c2 1888 /**
eceb18cc 1889 * Test sending a mail via the API.
6a488035 1890 */
00be9182 1891 public function testSendMail() {
5896d037 1892 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 1893 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
858d0bf8 1894 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
1895 'id' => $contribution['id'],
1896 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
1897 )
1898 );
6a488035
TO
1899 $mut->checkMailLog(array(
1900 '$ 100.00',
1901 'Contribution Information',
1902 'Please print this confirmation for your records',
1903 ), array(
21dfd5f5 1904 'Event',
6a488035
TO
1905 )
1906 );
1907 $mut->stop();
1908 }
1909
2f45e1c2 1910 /**
eceb18cc 1911 * Test sending a mail via the API.
6a488035 1912 */
00be9182 1913 public function testSendMailEvent() {
5896d037 1914 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 1915 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2f45e1c2 1916 $event = $this->eventCreate(array(
6a488035
TO
1917 'is_email_confirm' => 1,
1918 'confirm_from_email' => 'test@civicrm.org',
1919 ));
1920 $this->_eventID = $event['id'];
1921 $participantParams = array(
1922 'contact_id' => $this->_individualId,
1923 'event_id' => $this->_eventID,
1924 'status_id' => 1,
1925 'role_id' => 1,
1926 // to ensure it matches later on
1927 'register_date' => '2007-07-21 00:00:00',
1928 'source' => 'Online Event Registration: API Testing',
4ab7d517 1929
6a488035 1930 );
2f45e1c2 1931 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
1932 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
1933 'participant_id' => $participant['id'],
1934 'contribution_id' => $contribution['id'],
2f45e1c2 1935 ));
66d3f9be 1936 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
1937 'id' => $contribution['id'],
1938 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
1939 )
1940 );
1941
6a488035
TO
1942 $mut->checkMailLog(array(
1943 'Annual CiviCRM meet',
1944 'Event',
1945 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
6c6e6187 1946 ), array()
6a488035
TO
1947 );
1948 $mut->stop();
1949 }
1950
4302618d 1951 /**
1e52837d
EM
1952 * This function does a GET & compares the result against the $params.
1953 *
1954 * Use as a double check on Creates.
1955 *
1956 * @param array $params
1957 * @param int $id
67f947ac 1958 * @param bool $delete
6c6e6187 1959 */
1e52837d 1960 public function contributionGetnCheck($params, $id, $delete = TRUE) {
6a488035 1961
4ab7d517 1962 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 1963 'id' => $id,
4ab7d517 1964
5896d037 1965 ));
6a488035
TO
1966
1967 if ($delete) {
4ab7d517 1968 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 1969 }
2bfae985 1970 $this->assertAPISuccess($contribution, 0);
6a488035
TO
1971 $values = $contribution['values'][$contribution['id']];
1972 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
1973 // this is not returned in id format
1974 unset($params['payment_instrument_id']);
1975 $params['contribution_source'] = $params['source'];
1976 unset($params['source']);
1977 foreach ($params as $key => $value) {
22f80e87 1978 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
6a488035
TO
1979 }
1980 }
1981
0efa8efe 1982 /**
1e52837d 1983 * Create a pending contribution & linked pending participant record (along with an event).
0efa8efe 1984 */
5896d037 1985 public function createPendingParticipantContribution() {
6c6e6187 1986 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
0efa8efe 1987 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
5896d037 1988 $this->_ids['participant'] = $participantID;
0efa8efe 1989 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
6c6e6187 1990 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 1991 $this->callAPISuccess('participant_payment', 'create', array(
92915c55
TO
1992 'contribution_id' => $contribution['id'],
1993 'participant_id' => $participantID,
1994 ));
858d0bf8 1995 $this->callAPISuccess('line_item', 'get', array(
0efa8efe 1996 'entity_id' => $contribution['id'],
1997 'entity_table' => 'civicrm_contribution',
1998 'api.line_item.create' => array(
1999 'entity_id' => $participantID,
2000 'entity_table' => 'civicrm_participant',
2001 ),
2002 ));
2003 return $contribution['id'];
2004 }
2005
4cbe18b8 2006 /**
1e52837d
EM
2007 * Get financial transaction amount.
2008 *
100fef9d 2009 * @param int $contId
4cbe18b8
EM
2010 *
2011 * @return null|string
f4d89200 2012 */
2da40d21 2013 public function _getFinancialTrxnAmount($contId) {
6c6e6187 2014 $query = "SELECT
6a488035
TO
2015 SUM( ft.total_amount ) AS total
2016 FROM civicrm_financial_trxn AS ft
2017 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2018 WHERE ceft.entity_table = 'civicrm_contribution'
2019 AND ceft.entity_id = {$contId}";
2020
6c6e6187
TO
2021 $result = CRM_Core_DAO::singleValueQuery($query);
2022 return $result;
2023 }
6a488035 2024
4cbe18b8 2025 /**
100fef9d 2026 * @param int $contId
4cbe18b8
EM
2027 *
2028 * @return null|string
f4d89200 2029 */
2da40d21 2030 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
2031 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2032 $query = "SELECT
6a488035
TO
2033 SUM(amount)
2034 FROM civicrm_financial_item
2035 WHERE entity_table = 'civicrm_line_item'
2036 AND entity_id = {$lineItem}";
6c6e6187
TO
2037 $result = CRM_Core_DAO::singleValueQuery($query);
2038 return $result;
2039 }
6a488035 2040
4cbe18b8 2041 /**
100fef9d 2042 * @param int $contId
4cbe18b8
EM
2043 * @param $context
2044 */
00be9182 2045 public function _checkFinancialItem($contId, $context) {
6c6e6187
TO
2046 if ($context != 'paylater') {
2047 $params = array(
5896d037
TO
2048 'entity_id' => $contId,
2049 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2050 );
2051 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2052 $entityParams = array(
6a488035
TO
2053 'financial_trxn_id' => $trxn['financial_trxn_id'],
2054 'entity_table' => 'civicrm_financial_item',
6c6e6187
TO
2055 );
2056 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2057 $params = array(
6a488035 2058 'id' => $entityTrxn['entity_id'],
6c6e6187
TO
2059 );
2060 }
2061 if ($context == 'paylater') {
2062 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2063 foreach ($lineItems as $key => $item) {
2064 $params = array(
5896d037
TO
2065 'entity_id' => $key,
2066 'entity_table' => 'civicrm_line_item',
6c6e6187
TO
2067 );
2068 $compareParams = array('status_id' => 1);
2069 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2070 }
2071 }
2072 elseif ($context == 'refund') {
2073 $compareParams = array(
5896d037
TO
2074 'status_id' => 1,
2075 'financial_account_id' => 1,
2076 'amount' => -100,
6c6e6187
TO
2077 );
2078 }
2079 elseif ($context == 'cancelPending') {
2080 $compareParams = array(
5896d037
TO
2081 'status_id' => 3,
2082 'financial_account_id' => 1,
2083 'amount' => -100,
6c6e6187
TO
2084 );
2085 }
2086 elseif ($context == 'changeFinancial') {
2087 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2088 $params = array(
5896d037
TO
2089 'entity_id' => $lineKey,
2090 'amount' => -100,
6c6e6187
TO
2091 );
2092 $compareParams = array(
5896d037 2093 'financial_account_id' => 1,
6c6e6187
TO
2094 );
2095 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2096 $params = array(
5896d037
TO
2097 'financial_account_id' => 3,
2098 'entity_id' => $lineKey,
6c6e6187
TO
2099 );
2100 $compareParams = array(
5896d037 2101 'amount' => 100,
6c6e6187
TO
2102 );
2103 }
2104 if ($context != 'paylater') {
2105 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2106 }
2107 }
6a488035 2108
4cbe18b8 2109 /**
4ff927bc 2110 * @param array $contribution
2111 * @param string $context
100fef9d 2112 * @param int $instrumentId
4cbe18b8 2113 */
00be9182 2114 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL) {
6c6e6187 2115 $trxnParams = array(
5896d037
TO
2116 'entity_id' => $contribution['id'],
2117 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2118 );
2119 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2120 $params = array(
5896d037 2121 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
2122 );
2123 if ($context == 'payLater') {
2124 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2125 $compareParams = array(
5896d037
TO
2126 'status_id' => 1,
2127 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
6c6e6187
TO
2128 );
2129 }
2130 elseif ($context == 'refund') {
2131 $compareParams = array(
5896d037
TO
2132 'to_financial_account_id' => 6,
2133 'total_amount' => -100,
2134 'status_id' => 7,
b7990bb6 2135 'trxn_date' => '2015-01-01 09:00:00',
6c6e6187
TO
2136 );
2137 }
2138 elseif ($context == 'cancelPending') {
2139 $compareParams = array(
ed4d0aea 2140 'to_financial_account_id' => 7,
5896d037
TO
2141 'total_amount' => -100,
2142 'status_id' => 3,
6c6e6187
TO
2143 );
2144 }
2145 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
2146 $entityParams = array(
5896d037
TO
2147 'entity_id' => $contribution['id'],
2148 'entity_table' => 'civicrm_contribution',
2149 'amount' => -100,
6c6e6187
TO
2150 );
2151 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2152 $trxnParams1 = array(
6a488035 2153 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
2154 );
2155 $compareParams = array(
5896d037
TO
2156 'total_amount' => -100,
2157 'status_id' => 1,
6c6e6187
TO
2158 );
2159 if ($context == 'paymentInstrument') {
2160 $compareParams += array(
5896d037
TO
2161 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
2162 'payment_instrument_id' => 4,
6c6e6187
TO
2163 );
2164 }
2165 else {
2166 $compareParams['to_financial_account_id'] = 12;
2167 }
2168 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, $compareParams);
2169 $compareParams['total_amount'] = 100;
2170 if ($context == 'paymentInstrument') {
2171 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
2172 $compareParams['payment_instrument_id'] = $instrumentId;
2173 }
2174 else {
2175 $compareParams['to_financial_account_id'] = 12;
2176 }
2177 }
2178
2179 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, $compareParams);
2180 }
6a488035 2181
4cbe18b8
EM
2182 /**
2183 * @return mixed
2184 */
5896d037 2185 public function _addPaymentInstrument() {
6c6e6187
TO
2186 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2187 $optionParams = array(
5896d037
TO
2188 'option_group_id' => $gId,
2189 'label' => 'Test Card',
2190 'name' => 'Test Card',
2191 'value' => '6',
2192 'weight' => '6',
2193 'is_active' => 1,
6c6e6187
TO
2194 );
2195 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2196 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2197 $financialParams = array(
5896d037
TO
2198 'entity_table' => 'civicrm_option_value',
2199 'entity_id' => $optionValue['id'],
2200 'account_relationship' => $relationTypeId,
2201 'financial_account_id' => 7,
6c6e6187
TO
2202 );
2203 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
2204 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2205 return $optionValue['values'][$optionValue['id']]['value'];
2206 }
6a488035 2207
4cbe18b8 2208 /**
c490a46a 2209 * @param array $params
4cbe18b8
EM
2210 * @param $context
2211 */
6c6e6187
TO
2212 public function _checkFinancialRecords($params, $context) {
2213 $entityParams = array(
5896d037
TO
2214 'entity_id' => $params['id'],
2215 'entity_table' => 'civicrm_contribution',
6c6e6187 2216 );
e0e3c51b
EM
2217 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2218 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
6c6e6187
TO
2219 if ($context == 'pending') {
2220 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
2221 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2222 return;
2223 }
2224 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2225 $trxnParams = array(
5896d037 2226 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
2227 );
2228 if ($context != 'online' && $context != 'payLater') {
2229 $compareParams = array(
5896d037
TO
2230 'to_financial_account_id' => 6,
2231 'total_amount' => 100,
2232 'status_id' => 1,
6c6e6187
TO
2233 );
2234 }
2235 if ($context == 'feeAmount') {
2236 $compareParams['fee_amount'] = 50;
2237 }
2238 elseif ($context == 'online') {
2239 $compareParams = array(
5896d037
TO
2240 'to_financial_account_id' => 12,
2241 'total_amount' => 100,
2242 'status_id' => 1,
6c6e6187
TO
2243 );
2244 }
2245 elseif ($context == 'payLater') {
2246 $compareParams = array(
5896d037
TO
2247 'to_financial_account_id' => 7,
2248 'total_amount' => 100,
2249 'status_id' => 2,
6c6e6187
TO
2250 );
2251 }
2252 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2253 $entityParams = array(
5896d037
TO
2254 'financial_trxn_id' => $trxn['financial_trxn_id'],
2255 'entity_table' => 'civicrm_financial_item',
6c6e6187
TO
2256 );
2257 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2258 $fitemParams = array(
5896d037 2259 'id' => $entityTrxn['entity_id'],
6c6e6187
TO
2260 );
2261 $compareParams = array(
5896d037
TO
2262 'amount' => 100,
2263 'status_id' => 1,
2264 'financial_account_id' => 1,
6c6e6187
TO
2265 );
2266 if ($context == 'payLater') {
2267 $compareParams = array(
5896d037
TO
2268 'amount' => 100,
2269 'status_id' => 3,
2270 'financial_account_id' => 1,
6c6e6187
TO
2271 );
2272 }
2273 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2274 if ($context == 'feeAmount') {
2275 $maxParams = array(
5896d037
TO
2276 'entity_id' => $params['id'],
2277 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2278 );
2279 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
2280 $trxnParams = array(
5896d037 2281 'id' => $maxTrxn['financial_trxn_id'],
6c6e6187
TO
2282 );
2283 $compareParams = array(
5896d037
TO
2284 'to_financial_account_id' => 5,
2285 'from_financial_account_id' => 6,
2286 'total_amount' => 50,
2287 'status_id' => 1,
6c6e6187
TO
2288 );
2289 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
2290 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2291 $fitemParams = array(
5896d037
TO
2292 'entity_id' => $trxnId['financialTrxnId'],
2293 'entity_table' => 'civicrm_financial_trxn',
6c6e6187
TO
2294 );
2295 $compareParams = array(
5896d037
TO
2296 'amount' => 50,
2297 'status_id' => 1,
2298 'financial_account_id' => 5,
6c6e6187
TO
2299 );
2300 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2301 }
2302 }
96025800 2303
6a488035 2304}