Merge pull request #7555 from colemanw/breadcrumb
[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() {
797d4c52 1047 $contributionParams = array(
6a488035
TO
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,
797d4c52 1054 'trxn_id' => 'original_payment',
1055 );
1056 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1057 $newParams = array_merge($contributionParams, array(
1058 'id' => $contribution['id'],
1059 'contribution_status_id' => 'Refunded',
1060 'cancel_date' => '2015-01-01 09:00',
1061 'refund_trxn_id' => 'the refund',
1062 )
1063 );
1064
1065 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1066 $this->_checkFinancialTrxn($contribution, 'refund');
1067 $this->_checkFinancialItem($contribution['id'], 'refund');
1068 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1069 'id' => $contribution['id'],
1070 'return' => 'trxn_id',
1071 )));
1072 }
4ab7d517 1073
797d4c52 1074 /**
1075 * Function tests that trxn_id is set when passed in.
1076 *
1077 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1078 * when trxn_id is passed in.
1079 */
1080 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1081 $contributionParams = array(
1082 'contact_id' => $this->_individualId,
1083 'receive_date' => '2012-01-01',
1084 'total_amount' => 100.00,
1085 'financial_type_id' => $this->_financialTypeId,
1086 'payment_instrument_id' => 4,
1087 'contribution_status_id' => 1,
1088 'trxn_id' => 'original_payment',
6a488035 1089 );
797d4c52 1090 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1091 $newParams = array_merge($contributionParams, array(
1092 'id' => $contribution['id'],
1093 'contribution_status_id' => 'Refunded',
1094 'cancel_date' => '2015-01-01 09:00',
1095 'trxn_id' => 'the refund',
1096 )
1097 );
1098
1099 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1100 $this->_checkFinancialTrxn($contribution, 'refund');
1101 $this->_checkFinancialItem($contribution['id'], 'refund');
1102 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1103 'id' => $contribution['id'],
1104 'return' => 'trxn_id',
1105 )));
1106 }
1107
1108 /**
1109 * Function tests that trxn_id is set when passed in.
1110 *
1111 * Here we ensure that the civicrm_contribution.trxn_id is set
1112 * when trxn_id is passed in but if refund_trxn_id is different then that
1113 * is kept for the refund transaction.
1114 */
1115 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1116 $contributionParams = array(
1117 'contact_id' => $this->_individualId,
1118 'receive_date' => '2012-01-01',
1119 'total_amount' => 100.00,
1120 'financial_type_id' => $this->_financialTypeId,
1121 'payment_instrument_id' => 4,
1122 'contribution_status_id' => 1,
1123 'trxn_id' => 'original_payment',
1124 );
1125 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1126 $newParams = array_merge($contributionParams, array(
5896d037 1127 'id' => $contribution['id'],
b7990bb6 1128 'contribution_status_id' => 'Refunded',
1129 'cancel_date' => '2015-01-01 09:00',
797d4c52 1130 'trxn_id' => 'cont id',
1131 'refund_trxn_id' => 'the refund',
6a488035
TO
1132 )
1133 );
1134
694769da 1135 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1136 $this->_checkFinancialTrxn($contribution, 'refund');
1137 $this->_checkFinancialItem($contribution['id'], 'refund');
797d4c52 1138 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1139 'id' => $contribution['id'],
1140 'return' => 'trxn_id',
1141 )));
1142 }
1143
1144 /**
1145 * Function tests that refund_trxn_id is set when passed in empty.
1146 *
1147 * Here we ensure that the civicrm_contribution.trxn_id is set
1148 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1149 * is kept for the refund transaction.
1150 */
1151 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1152 $contributionParams = array(
1153 'contact_id' => $this->_individualId,
1154 'receive_date' => '2012-01-01',
1155 'total_amount' => 100.00,
1156 'financial_type_id' => $this->_financialTypeId,
1157 'payment_instrument_id' => 4,
1158 'contribution_status_id' => 1,
1159 'trxn_id' => 'original_payment',
1160 );
1161 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1162 $newParams = array_merge($contributionParams, array(
1163 'id' => $contribution['id'],
1164 'contribution_status_id' => 'Refunded',
1165 'cancel_date' => '2015-01-01 09:00',
1166 'trxn_id' => 'cont id',
1167 'refund_trxn_id' => '',
1168 )
1169 );
1170
1171 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1172 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1173 $this->_checkFinancialItem($contribution['id'], 'refund');
1174 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1175 'id' => $contribution['id'],
1176 'return' => 'trxn_id',
1177 )));
8f39a111 1178 }
c71ae314 1179
a1a2a83d 1180 /**
eceb18cc 1181 * Function tests invalid contribution status change.
c71ae314 1182 */
00be9182 1183 public function testCreateUpdateContributionInValidStatusChange() {
c71ae314
PN
1184 $contribParams = array(
1185 'contact_id' => 1,
1186 'receive_date' => '2012-01-01',
1187 'total_amount' => 100.00,
1188 'financial_type_id' => 1,
1189 'payment_instrument_id' => 1,
1190 'contribution_status_id' => 1,
c71ae314 1191 );
4ab7d517 1192 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
c71ae314 1193 $newParams = array_merge($contribParams, array(
5896d037
TO
1194 'id' => $contribution['id'],
1195 'contribution_status_id' => 2,
c71ae314
PN
1196 )
1197 );
6c6e6187 1198 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1199
6a488035
TO
1200 }
1201
a1a2a83d 1202 /**
eceb18cc 1203 * Function tests that financial records are added when Pending Contribution is Canceled.
6a488035 1204 */
00be9182 1205 public function testCreateUpdateContributionCancelPending() {
6a488035
TO
1206 $contribParams = array(
1207 'contact_id' => $this->_individualId,
1208 'receive_date' => '2012-01-01',
1209 'total_amount' => 100.00,
4ab7d517 1210 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1211 'payment_instrument_id' => 1,
1212 'contribution_status_id' => 2,
c71ae314 1213 'is_pay_later' => 1,
4ab7d517 1214
6a488035 1215 );
4ab7d517 1216 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1217 $newParams = array_merge($contribParams, array(
5896d037
TO
1218 'id' => $contribution['id'],
1219 'contribution_status_id' => 3,
6a488035
TO
1220 )
1221 );
694769da 1222 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1223 $this->_checkFinancialTrxn($contribution, 'cancelPending');
1224 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1225 }
1226
a1a2a83d 1227 /**
eceb18cc 1228 * Function tests that financial records are added when Financial Type is Changed.
6a488035 1229 */
00be9182 1230 public function testCreateUpdateContributionChangeFinancialType() {
6a488035
TO
1231 $contribParams = array(
1232 'contact_id' => $this->_individualId,
1233 'receive_date' => '2012-01-01',
1234 'total_amount' => 100.00,
1235 'financial_type_id' => 1,
1236 'payment_instrument_id' => 1,
1237 'contribution_status_id' => 1,
4ab7d517 1238
6a488035 1239 );
4ab7d517 1240 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1241 $newParams = array_merge($contribParams, array(
5896d037
TO
1242 'id' => $contribution['id'],
1243 'financial_type_id' => 3,
6a488035
TO
1244 )
1245 );
694769da 1246 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1247 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1248 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1249 }
1250
694769da 1251 /**
1e52837d 1252 * Test that update does not change status id CRM-15105.
694769da 1253 */
00be9182 1254 public function testCreateUpdateWithoutChangingPendingStatus() {
694769da
VU
1255 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1256 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
5896d037 1257 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1258 'id' => $contribution['id'],
1259 'api.contribution.delete' => 1,
1260 ));
694769da
VU
1261 $this->assertEquals(2, $contribution['contribution_status_id']);
1262 }
a1a2a83d
TO
1263
1264 /**
28de42d1
EM
1265 * Test Updating a Contribution.
1266 *
a1a2a83d
TO
1267 * CHANGE: we require the API to do an incremental update
1268 */
00be9182 1269 public function testCreateUpdateContribution() {
6a488035 1270
78ab0ca4 1271 $contributionID = $this->contributionCreate(array(
1272 'contact_id' => $this->_individualId,
1273 'trxn_id' => 212355,
1274 'financial_type_id' => $this->_financialTypeId,
1275 'invoice_id' => 'old_invoice',
1276 ));
6a488035
TO
1277 $old_params = array(
1278 'contribution_id' => $contributionID,
6a488035 1279 );
4ab7d517 1280 $original = $this->callAPISuccess('contribution', 'get', $old_params);
2bfae985 1281 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1282 //set up list of old params, verify
1283
1284 //This should not be required on update:
1285 $old_contact_id = $original['values'][$contributionID]['contact_id'];
7d543448 1286 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
6a488035
TO
1287 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1288 $old_source = $original['values'][$contributionID]['contribution_source'];
1289
6a488035
TO
1290 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1291 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1292
1293 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1294 $this->assertEquals($old_contact_id, $this->_individualId);
1295 $this->assertEquals($old_fee_amount, 5.00);
1296 $this->assertEquals($old_source, 'SSF');
1297 $this->assertEquals($old_trxn_id, 212355);
78ab0ca4 1298 $this->assertEquals($old_invoice_id, 'old_invoice');
6a488035
TO
1299 $params = array(
1300 'id' => $contributionID,
1301 'contact_id' => $this->_individualId,
1302 'total_amount' => 110.00,
4ab7d517 1303 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1304 'non_deductible_amount' => 10.00,
1305 'net_amount' => 100.00,
1306 'contribution_status_id' => 1,
1307 'note' => 'Donating for Nobel Cause',
4ab7d517 1308
6a488035
TO
1309 );
1310
4ab7d517 1311 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1312
1313 $new_params = array(
1314 'contribution_id' => $contribution['id'],
4ab7d517 1315
6a488035 1316 );
4ab7d517 1317 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
6a488035 1318
2bfae985
EM
1319 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1320 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
ff977830 1321 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
7d543448 1322 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
2bfae985
EM
1323 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1324 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1325 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1326 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1327 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1328 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1329 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
6a488035
TO
1330 $params = array(
1331 'contribution_id' => $contributionID,
4ab7d517 1332
6a488035 1333 );
4ab7d517 1334 $result = $this->callAPISuccess('contribution', 'delete', $params);
22f80e87 1335 $this->assertAPISuccess($result);
6a488035
TO
1336 }
1337
1338 ///////////////// civicrm_contribution_delete methods
a1a2a83d
TO
1339
1340 /**
1341 * Attempt (but fail) to delete a contribution without parameters.
1342 */
00be9182 1343 public function testDeleteEmptyParamsContribution() {
4ab7d517 1344 $params = array();
858d0bf8 1345 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1346 }
1347
00be9182 1348 public function testDeleteParamsNotArrayContribution() {
6a488035 1349 $params = 'contribution_id= 1';
d0e1eff2 1350 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1351 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1352 }
1353
00be9182 1354 public function testDeleteWrongParamContribution() {
6a488035
TO
1355 $params = array(
1356 'contribution_source' => 'SSF',
4ab7d517 1357
6a488035 1358 );
858d0bf8 1359 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1360 }
1361
00be9182 1362 public function testDeleteContribution() {
78ab0ca4 1363 $contributionID = $this->contributionCreate(array(
1364 'contact_id' => $this->_individualId,
1365 'financial_type_id' => $this->_financialTypeId,
1366 ));
6a488035
TO
1367 $params = array(
1368 'id' => $contributionID,
6a488035 1369 );
4ab7d517 1370 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1371 }
1372
1373 /**
d177a2a6 1374 * Test civicrm_contribution_search with empty params.
1e52837d 1375 *
d177a2a6 1376 * All available contributions expected.
6a488035 1377 */
00be9182 1378 public function testSearchEmptyParams() {
4ab7d517 1379 $params = array();
6a488035
TO
1380
1381 $p = array(
1382 'contact_id' => $this->_individualId,
1383 'receive_date' => date('Ymd'),
1384 'total_amount' => 100.00,
4ab7d517 1385 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1386 'non_deductible_amount' => 10.00,
1387 'fee_amount' => 5.00,
1388 'net_amount' => 95.00,
1389 'trxn_id' => 23456,
1390 'invoice_id' => 78910,
1391 'source' => 'SSF',
1392 'contribution_status_id' => 1,
4ab7d517 1393
6a488035 1394 );
4ab7d517 1395 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1396
4ab7d517 1397 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1398 // We're taking the first element.
1399 $res = $result['values'][$contribution['id']];
1400
2bfae985
EM
1401 $this->assertEquals($p['contact_id'], $res['contact_id']);
1402 $this->assertEquals($p['total_amount'], $res['total_amount']);
5896d037 1403 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1404 $this->assertEquals($p['net_amount'], $res['net_amount']);
1405 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1406 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1407 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1408 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1409 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1410 // contribution_status_id = 1 => Completed
2bfae985 1411 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1412
1413 $this->contributionDelete($contribution['id']);
1414 }
1415
1416 /**
d177a2a6 1417 * Test civicrm_contribution_search. Success expected.
6a488035 1418 */
00be9182 1419 public function testSearch() {
6a488035
TO
1420 $p1 = array(
1421 'contact_id' => $this->_individualId,
1422 'receive_date' => date('Ymd'),
1423 'total_amount' => 100.00,
4ab7d517 1424 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1425 'non_deductible_amount' => 10.00,
1426 'contribution_status_id' => 1,
4ab7d517 1427
6a488035 1428 );
4ab7d517 1429 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1430
1431 $p2 = array(
1432 'contact_id' => $this->_individualId,
1433 'receive_date' => date('Ymd'),
1434 'total_amount' => 200.00,
4ab7d517 1435 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1436 'non_deductible_amount' => 20.00,
1437 'trxn_id' => 5454565,
1438 'invoice_id' => 1212124,
1439 'fee_amount' => 50.00,
1440 'net_amount' => 60.00,
1441 'contribution_status_id' => 2,
4ab7d517 1442
6a488035 1443 );
2f45e1c2 1444 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1445
1446 $params = array(
1447 'contribution_id' => $contribution2['id'],
4ab7d517 1448
6a488035 1449 );
2f45e1c2 1450 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1451 $res = $result['values'][$contribution2['id']];
1452
2bfae985
EM
1453 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1454 $this->assertEquals($p2['total_amount'], $res['total_amount']);
5896d037 1455 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1456 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1457 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1458 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1459 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1460 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
6a488035 1461 // contribution_status_id = 2 => Pending
2bfae985 1462 $this->assertEquals('Pending', $res['contribution_status']);
6a488035
TO
1463
1464 $this->contributionDelete($contribution1['id']);
1465 $this->contributionDelete($contribution2['id']);
1466 }
2f45e1c2 1467
0efa8efe 1468 /**
eceb18cc 1469 * Test completing a transaction via the API.
0efa8efe 1470 *
1471 * Note that we are creating a logged in user because email goes out from
1472 * that person
1473 */
00be9182 1474 public function testCompleteTransaction() {
5896d037 1475 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 1476 $this->createLoggedInUser();
6c6e6187
TO
1477 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1478 $contribution = $this->callAPISuccess('contribution', 'create', $params);
66d3f9be 1479 $this->callAPISuccess('contribution', 'completetransaction', array(
0efa8efe 1480 'id' => $contribution['id'],
66d3f9be 1481 ));
6c6e6187 1482 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
0efa8efe 1483 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
1484 $mut->checkMailLog(array(
1485 'Receipt - Contribution',
1486 'Please print this confirmation for your records.',
1487 ));
46fa5206
EM
1488 $mut->stop();
1489 }
1490
080a561b 1491 /**
1492 * Test completing a transaction via the API.
1493 *
1494 * Note that we are creating a logged in user because email goes out from
1495 * that person
1496 */
1497 public function testCompleteTransactionFeeAmount() {
1498 $this->createLoggedInUser();
1499 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1500 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1501 $this->callAPISuccess('contribution', 'completetransaction', array(
1502 'id' => $contribution['id'],
1503 'fee_amount' => '.56',
1504 'trxn_id' => '7778888',
1505 ));
1506 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1507 $this->assertEquals('Completed', $contribution['contribution_status']);
1508 $this->assertEquals('7778888', $contribution['trxn_id']);
1509 $this->assertEquals('.56', $contribution['fee_amount']);
1510 $this->assertEquals('99.44', $contribution['net_amount']);
1511 }
1512
d97c96dc
EM
1513 /**
1514 * Test repeat contribution successfully creates line items.
1515 */
1e52837d 1516 public function testRepeatTransaction() {
d97c96dc
EM
1517 $paymentProcessorID = $this->paymentProcessorCreate();
1518 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1519 'contact_id' => $this->_individualId,
1520 'installments' => '12',
1521 'frequency_interval' => '1',
1522 'amount' => '500',
1523 'contribution_status_id' => 1,
1524 'start_date' => '2012-01-01 00:00:00',
1525 'currency' => 'USD',
1526 'frequency_unit' => 'month',
1527 'payment_processor_id' => $paymentProcessorID,
1528 ));
1529 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1530 $this->_params,
1531 array('contribution_recur_id' => $contributionRecur['id']))
1532 );
1533
1534 $this->callAPISuccess('contribution', 'repeattransaction', array(
1535 'original_contribution_id' => $originalContribution['id'],
1536 'contribution_status_id' => 'Completed',
1537 'trxn_id' => uniqid(),
1538 ));
1539 $lineItemParams = array(
1540 'entity_id' => $originalContribution['id'],
1541 'sequential' => 1,
1542 'return' => array(
1543 'entity_table',
1544 'qty',
1545 'unit_price',
1546 'line_total',
1547 'label',
1548 'financial_type_id',
1549 'deductible_amount',
1550 'price_field_value_id',
1551 'price_field_id',
1e52837d 1552 ),
d97c96dc
EM
1553 );
1554 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1555 'entity_id' => $originalContribution['id'],
1556 )));
1557 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1558 'entity_id' => $originalContribution['id'] + 1,
1559 )));
1560 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1561 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1562 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1563
1564 $this->quickCleanUpFinancialEntities();
1565 }
1566
1eade77d 1567 /**
1568 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1569 */
1570 public function testRepeatTransactionAcceptRecurID() {
1571 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1572 'contact_id' => $this->_individualId,
1573 'installments' => '12',
1574 'frequency_interval' => '1',
1575 'amount' => '100',
1576 'contribution_status_id' => 1,
1577 'start_date' => '2012-01-01 00:00:00',
1578 'currency' => 'USD',
1579 'frequency_unit' => 'month',
1580 'payment_processor_id' => $this->paymentProcessorID,
1581 ));
1582 $this->callAPISuccess('contribution', 'create', array_merge(
1583 $this->_params,
1584 array('contribution_recur_id' => $contributionRecur['id']))
1585 );
1586
1587 $this->callAPISuccess('contribution', 'repeattransaction', array(
1588 'contribution_recur_id' => $contributionRecur['id'],
1589 'contribution_status_id' => 'Completed',
1590 'trxn_id' => uniqid(),
1591 ));
1592
1593 $this->quickCleanUpFinancialEntities();
1594 }
1595
c03f1689
EM
1596 /**
1597 * CRM-16397 test appropriate action if total amount has changed for single line items.
1598 */
1599 public function testRepeatTransactionAlteredAmount() {
1600 $paymentProcessorID = $this->paymentProcessorCreate();
c03f1689
EM
1601 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1602 'contact_id' => $this->_individualId,
1603 'installments' => '12',
1604 'frequency_interval' => '1',
1605 'amount' => '500',
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 ));
1612 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1613 $this->_params,
1614 array(
1615 'contribution_recur_id' => $contributionRecur['id'],
c03f1689
EM
1616 ))
1617 );
1618
1619 $this->callAPISuccess('contribution', 'repeattransaction', array(
1620 'original_contribution_id' => $originalContribution['id'],
1621 'contribution_status_id' => 'Completed',
1622 'trxn_id' => uniqid(),
1623 'total_amount' => '400',
1624 'fee_amount' => 50,
c03f1689
EM
1625 ));
1626 $lineItemParams = array(
1627 'entity_id' => $originalContribution['id'],
1628 'sequential' => 1,
1629 'return' => array(
1630 'entity_table',
1631 'qty',
1632 'unit_price',
1633 'line_total',
1634 'label',
1635 'financial_type_id',
1636 'deductible_amount',
1637 'price_field_value_id',
1638 'price_field_id',
1639 ),
1640 );
1641 $this->callAPISuccessGetSingle('contribution', array(
1642 'total_amount' => 400,
c03f1689
EM
1643 'fee_amount' => 50,
1644 'net_amount' => 350,
1645 ));
1646 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1647 'entity_id' => $originalContribution['id'],
1648 )));
1649 $expectedLineItem = array_merge(
1650 $lineItem1['values'][0], array(
1651 'line_total' => '400.00',
1652 'unit_price' => '400.00',
542d9e2c
EM
1653 )
1654 );
c03f1689
EM
1655
1656 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1657 'entity_id' => $originalContribution['id'] + 1,
1658 )));
1659 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1660 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1661 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
c02c17df 1662 }
c03f1689 1663
3c49d90c 1664 /**
1665 * CRM-17718 test appropriate action if financial type has changed for single line items.
1666 */
1667 public function testRepeatTransactionPassedInFinancialType() {
1668 $originalContribution = $this->setUpRecurringContribution();
1669
1670 $this->callAPISuccess('contribution', 'repeattransaction', array(
1671 'original_contribution_id' => $originalContribution['id'],
1672 'contribution_status_id' => 'Completed',
1673 'trxn_id' => uniqid(),
1674 'financial_type_id' => 2,
1675 ));
1676 $lineItemParams = array(
1677 'entity_id' => $originalContribution['id'],
1678 'sequential' => 1,
1679 'return' => array(
1680 'entity_table',
1681 'qty',
1682 'unit_price',
1683 'line_total',
1684 'label',
1685 'financial_type_id',
1686 'deductible_amount',
1687 'price_field_value_id',
1688 'price_field_id',
1689 ),
1690 );
1691
1692 $this->callAPISuccessGetSingle('contribution', array(
1693 'total_amount' => 100,
1694 'financial_type_id' => 2,
1695 ));
1696 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1697 'entity_id' => $originalContribution['id'],
1698 )));
1699 $expectedLineItem = array_merge(
1700 $lineItem1['values'][0], array(
1701 'line_total' => '100.00',
1702 'unit_price' => '100.00',
1703 'financial_type_id' => 2,
1704 )
1705 );
1706
1707 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1708 'entity_id' => $originalContribution['id'] + 1,
1709 )));
1710 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1711 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1712 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1713 }
1714
7f4ef731 1715 /**
1716 * CRM-17718 test appropriate action if financial type has changed for single line items.
1717 */
1718 public function testRepeatTransactionUpdatedFinancialType() {
1719 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
1720
1721 $this->callAPISuccess('contribution', 'repeattransaction', array(
1722 'contribution_recur_id' => $originalContribution['id'],
1723 'contribution_status_id' => 'Completed',
1724 'trxn_id' => uniqid(),
1725 ));
1726 $lineItemParams = array(
1727 'entity_id' => $originalContribution['id'],
1728 'sequential' => 1,
1729 'return' => array(
1730 'entity_table',
1731 'qty',
1732 'unit_price',
1733 'line_total',
1734 'label',
1735 'financial_type_id',
1736 'deductible_amount',
1737 'price_field_value_id',
1738 'price_field_id',
1739 ),
1740 );
1741
1742 $this->callAPISuccessGetSingle('contribution', array(
1743 'total_amount' => 100,
1744 'financial_type_id' => 2,
1745 ));
1746 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1747 'entity_id' => $originalContribution['id'],
1748 )));
1749 $expectedLineItem = array_merge(
1750 $lineItem1['values'][0], array(
1751 'line_total' => '100.00',
1752 'unit_price' => '100.00',
1753 'financial_type_id' => 2,
1754 )
1755 );
1756
1757 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1758 'entity_id' => $originalContribution['id'] + 1,
1759 )));
1760 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
1761 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1762 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
1763 }
1764
c02c17df 1765 /**
1eade77d 1766 * CRM-16397 test appropriate action if campaign has been passed in.
c02c17df 1767 */
1768 public function testRepeatTransactionPassedInCampaign() {
1769 $paymentProcessorID = $this->paymentProcessorCreate();
1770 $campaignID = $this->campaignCreate();
1771 $campaignID2 = $this->campaignCreate();
1772 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1773 'contact_id' => $this->_individualId,
1774 'installments' => '12',
1775 'frequency_interval' => '1',
1776 'amount' => '100',
1777 'contribution_status_id' => 1,
1778 'start_date' => '2012-01-01 00:00:00',
1779 'currency' => 'USD',
1780 'frequency_unit' => 'month',
1781 'payment_processor_id' => $paymentProcessorID,
1782 ));
1783 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1784 $this->_params,
1785 array(
1786 'contribution_recur_id' => $contributionRecur['id'],
1787 'campaign_id' => $campaignID,
1788 ))
1789 );
1790
1791 $this->callAPISuccess('contribution', 'repeattransaction', array(
1792 'original_contribution_id' => $originalContribution['id'],
1793 'contribution_status_id' => 'Completed',
1794 'trxn_id' => uniqid(),
1795 'campaign_id' => $campaignID2,
1796 ));
1797
1798 $this->callAPISuccessGetSingle('contribution', array(
1799 'total_amount' => 100,
1800 'campaign_id' => $campaignID2,
1801 ));
1802 }
1803
1804 /**
1805 * CRM-17718 campaign stored on contribution recur gets priority.
1806 *
1807 * This reflects the fact we permit people to update them.
1808 */
1809 public function testRepeatTransactionUpdatedCampaign() {
1810 $paymentProcessorID = $this->paymentProcessorCreate();
1811 $campaignID = $this->campaignCreate();
1812 $campaignID2 = $this->campaignCreate();
1813 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1814 'contact_id' => $this->_individualId,
1815 'installments' => '12',
1816 'frequency_interval' => '1',
1817 'amount' => '100',
1818 'contribution_status_id' => 1,
1819 'start_date' => '2012-01-01 00:00:00',
1820 'currency' => 'USD',
1821 'frequency_unit' => 'month',
1822 'payment_processor_id' => $paymentProcessorID,
1823 'campaign_id' => $campaignID,
1824 ));
1825 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1826 $this->_params,
1827 array(
1828 'contribution_recur_id' => $contributionRecur['id'],
1829 'campaign_id' => $campaignID2,
1830 ))
1831 );
1832
1833 $this->callAPISuccess('contribution', 'repeattransaction', array(
1834 'original_contribution_id' => $originalContribution['id'],
1835 'contribution_status_id' => 'Completed',
1836 'trxn_id' => uniqid(),
1837 ));
1838
1839 $this->callAPISuccessGetSingle('contribution', array(
1840 'total_amount' => 100,
1841 'campaign_id' => $campaignID,
1842 ));
c03f1689
EM
1843 }
1844
2936c3b5
EM
1845 /**
1846 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
1847 */
1848 public function testCompleteTransactionNetAmountOK() {
1849 $this->createLoggedInUser();
1850 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1851 unset($params['net_amount']);
1852 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1853 $this->callAPISuccess('contribution', 'completetransaction', array(
1854 'id' => $contribution['id'],
1855 ));
1856 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1857 $this->assertEquals('Completed', $contribution['contribution_status']);
1858 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
1859 }
1860
46fa5206 1861 /**
1e52837d 1862 * CRM-14151 - Test completing a transaction via the API.
46fa5206 1863 */
00be9182 1864 public function testCompleteTransactionWithReceiptDateSet() {
5896d037 1865 $mut = new CiviMailUtils($this, TRUE);
46fa5206 1866 $this->createLoggedInUser();
6c6e6187
TO
1867 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
1868 $contribution = $this->callAPISuccess('contribution', 'create', $params);
7104593e 1869 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
6c6e6187 1870 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
46fa5206 1871 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
7104593e 1872 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
46fa5206
EM
1873 $mut->checkMailLog(array(
1874 'Receipt - Contribution',
1875 'Please print this confirmation for your records.',
1876 ));
0efa8efe 1877 $mut->stop();
1878 }
1879
91259407 1880 /**
1881 * Test completing first transaction in a recurring series.
1882 *
1883 * The status should be set to 'in progress' and the next scheduled payment date calculated.
1884 */
1885 public function testCompleteTransactionSetStatusToInProgress() {
1886 $paymentProcessorID = $this->paymentProcessorCreate();
1887 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1888 'contact_id' => $this->_individualId,
1889 'installments' => '12',
1890 'frequency_interval' => '1',
1891 'amount' => '500',
1892 'contribution_status_id' => 'Pending',
1893 'start_date' => '2012-01-01 00:00:00',
1894 'currency' => 'USD',
1895 'frequency_unit' => 'month',
1896 'payment_processor_id' => $paymentProcessorID,
1897 ));
1898 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
1899 $this->_params,
1900 array(
1901 'contribution_recur_id' => $contributionRecur['id'],
1902 'contribution_status_id' => 'Pending',
1903 ))
1904 );
1905 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
1906 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
1907 'id' => $contributionRecur['id'],
1908 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
1909 ));
1910 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
1911 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
1912 }
1913
0efa8efe 1914 /**
eceb18cc 1915 * Test completing a transaction with an event via the API.
0efa8efe 1916 *
1917 * Note that we are creating a logged in user because email goes out from
1918 * that person
1919 */
00be9182 1920 public function testCompleteTransactionWithParticipantRecord() {
5896d037 1921 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 1922 $mut->clearMessages();
1923 $this->createLoggedInUser();
1924 $contributionID = $this->createPendingParticipantContribution();
66d3f9be 1925 $this->callAPISuccess('contribution', 'completetransaction', array(
5896d037
TO
1926 'id' => $contributionID,
1927 )
0efa8efe 1928 );
5896d037 1929 $participantStatus = $this->callAPISuccessGetValue('participant', array(
92915c55
TO
1930 'id' => $this->_ids['participant'],
1931 'return' => 'participant_status_id',
1932 ));
0efa8efe 1933 $this->assertEquals(1, $participantStatus);
1934 $mut->checkMailLog(array(
1935 'Annual CiviCRM meet',
1936 'Event',
afc59fef 1937 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 1938 ));
66d3f9be
EM
1939 $mut->stop();
1940 }
1941
1942 /**
eceb18cc 1943 * Test membership is renewed when transaction completed.
66d3f9be 1944 */
00be9182 1945 public function testCompleteTransactionMembershipPriceSet() {
66d3f9be 1946 $this->createPriceSetWithPage('membership');
4ff927bc 1947 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
1948 'name' => 'Grace',
1949 'return' => 'id')
1950 );
66d3f9be 1951 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
4ff927bc 1952 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1953 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1954 'membership_id' => $this->_ids['membership'],
1955 ));
1956 $this->assertEquals(1, $logs['count']);
1957 $this->assertEquals($stateOfGrace, $membership['status_id']);
6c6e6187 1958 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
1959 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1960 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
4ff927bc 1961 $this->callAPISuccessGetSingle('LineItem', array(
1962 'entity_id' => $this->_ids['membership'],
1963 'entity_table' => 'civicrm_membership',
1964 ));
1965 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
1966 'membership_id' => $this->_ids['membership'],
1967 ));
1968 $this->assertEquals(2, $logs['count']);
1969 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
66d3f9be
EM
1970 $this->cleanUpAfterPriceSets();
1971 }
1972
1973 /**
eceb18cc 1974 * Test membership is renewed when transaction completed.
66d3f9be 1975 */
00be9182 1976 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
66d3f9be
EM
1977 $this->createPriceSetWithPage('membership');
1978 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
6c6e6187 1979 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
1980 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
1981 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
1982 $this->cleanUpAfterPriceSets();
1983 }
1984
00be9182 1985 public function cleanUpAfterPriceSets() {
1cf3c2b1 1986 $this->quickCleanUpFinancialEntities();
66d3f9be 1987 $this->contactDelete($this->_ids['contact']);
66d3f9be
EM
1988 }
1989
1990
1991 /**
1e52837d
EM
1992 * Create price set with contribution test for test setup.
1993 *
100fef9d 1994 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
28de42d1 1995 * on parent class at some point (fn is not in 4.4).
1e52837d 1996 *
66d3f9be
EM
1997 * @param $entity
1998 * @param array $params
1999 */
00be9182 2000 public function createPriceSetWithPage($entity, $params = array()) {
66d3f9be
EM
2001 $membershipTypeID = $this->membershipTypeCreate();
2002 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2003 'title' => "Test Contribution Page",
2004 'financial_type_id' => 1,
2005 'currency' => 'NZD',
2006 'goal_amount' => 50,
2007 'is_pay_later' => 1,
2008 'is_monetary' => TRUE,
2009 'is_email_receipt' => FALSE,
2010 ));
2011 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2012 'is_quick_config' => 0,
2013 'extends' => 'CiviMember',
2014 'financial_type_id' => 1,
21dfd5f5 2015 'title' => 'my Page',
66d3f9be
EM
2016 ));
2017 $priceSetID = $priceSet['id'];
2018
5896d037 2019 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
66d3f9be 2020 $priceField = $this->callAPISuccess('price_field', 'create', array(
5896d037 2021 'price_set_id' => $priceSetID,
66d3f9be
EM
2022 'label' => 'Goat Breed',
2023 'html_type' => 'Radio',
2024 ));
2025 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
5896d037 2026 'price_set_id' => $priceSetID,
66d3f9be
EM
2027 'price_field_id' => $priceField['id'],
2028 'label' => 'Long Haired Goat',
2029 'amount' => 20,
43dbd988 2030 'financial_type_id' => 'Donation',
66d3f9be
EM
2031 'membership_type_id' => $membershipTypeID,
2032 'membership_num_terms' => 1,
2033 )
2034 );
2035 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
2036 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
5896d037 2037 'price_set_id' => $priceSetID,
66d3f9be
EM
2038 'price_field_id' => $priceField['id'],
2039 'label' => 'Shoe-eating Goat',
2040 'amount' => 10,
43dbd988 2041 'financial_type_id' => 'Donation',
66d3f9be
EM
2042 'membership_type_id' => $membershipTypeID,
2043 'membership_num_terms' => 2,
2044 )
2045 );
2046 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
2047 $this->_ids['price_set'] = $priceSetID;
2048 $this->_ids['contribution_page'] = $contributionPageResult['id'];
2049 $this->_ids['price_field'] = array($priceField['id']);
0efa8efe 2050
66d3f9be
EM
2051 $this->_ids['membership_type'] = $membershipTypeID;
2052 }
2053
2054 /**
eceb18cc 2055 * Set up a pending transaction with a specific price field id.
1e52837d 2056 *
100fef9d 2057 * @param int $priceFieldValueID
66d3f9be 2058 */
5896d037 2059 public function setUpPendingContribution($priceFieldValueID) {
66d3f9be
EM
2060 $contactID = $this->individualCreate();
2061 $membership = $this->callAPISuccess('membership', 'create', array(
2062 'contact_id' => $contactID,
2063 'membership_type_id' => $this->_ids['membership_type'],
2064 'start_date' => 'yesterday - 1 year',
2065 'end_date' => 'yesterday',
4ff927bc 2066 'join_date' => 'yesterday - 1 year',
66d3f9be
EM
2067 ));
2068 $contribution = $this->callAPISuccess('contribution', 'create', array(
2069 'domain_id' => 1,
2070 'contact_id' => $contactID,
2071 'receive_date' => date('Ymd'),
2072 'total_amount' => 100.00,
2073 'financial_type_id' => 1,
2074 'payment_instrument_id' => 'Credit Card',
2075 'non_deductible_amount' => 10.00,
2076 'trxn_id' => 'jdhfi88',
2077 'invoice_id' => 'djfhiewuyr',
2078 'source' => 'SSF',
2079 'contribution_status_id' => 2,
2080 'contribution_page_id' => $this->_ids['contribution_page'],
2081 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2082 ));
2083
2084 $this->callAPISuccess('line_item', 'create', array(
2085 'entity_id' => $contribution['id'],
2086 'entity_table' => 'civicrm_contribution',
1274acef 2087 'contribution_id' => $contribution['id'],
66d3f9be
EM
2088 'price_field_id' => $this->_ids['price_field'][0],
2089 'qty' => 1,
2090 'unit_price' => 20,
2091 'line_total' => 20,
2092 'financial_type_id' => 1,
2093 'price_field_value_id' => $priceFieldValueID,
2094 ));
2095 $this->_ids['contact'] = $contactID;
2096 $this->_ids['contribution'] = $contribution['id'];
2097 $this->_ids['membership'] = $membership['id'];
0efa8efe 2098 }
2099
2f45e1c2 2100 /**
eceb18cc 2101 * Test sending a mail via the API.
6a488035 2102 */
00be9182 2103 public function testSendMail() {
5896d037 2104 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 2105 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
858d0bf8 2106 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
2107 'id' => $contribution['id'],
2108 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
2109 )
2110 );
6a488035
TO
2111 $mut->checkMailLog(array(
2112 '$ 100.00',
2113 'Contribution Information',
2114 'Please print this confirmation for your records',
2115 ), array(
21dfd5f5 2116 'Event',
6a488035
TO
2117 )
2118 );
2119 $mut->stop();
2120 }
2121
2f45e1c2 2122 /**
eceb18cc 2123 * Test sending a mail via the API.
6a488035 2124 */
00be9182 2125 public function testSendMailEvent() {
5896d037 2126 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 2127 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2f45e1c2 2128 $event = $this->eventCreate(array(
6a488035
TO
2129 'is_email_confirm' => 1,
2130 'confirm_from_email' => 'test@civicrm.org',
2131 ));
2132 $this->_eventID = $event['id'];
2133 $participantParams = array(
2134 'contact_id' => $this->_individualId,
2135 'event_id' => $this->_eventID,
2136 'status_id' => 1,
2137 'role_id' => 1,
2138 // to ensure it matches later on
2139 'register_date' => '2007-07-21 00:00:00',
2140 'source' => 'Online Event Registration: API Testing',
4ab7d517 2141
6a488035 2142 );
2f45e1c2 2143 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2144 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
2145 'participant_id' => $participant['id'],
2146 'contribution_id' => $contribution['id'],
2f45e1c2 2147 ));
66d3f9be 2148 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
2149 'id' => $contribution['id'],
2150 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
2151 )
2152 );
2153
6a488035
TO
2154 $mut->checkMailLog(array(
2155 'Annual CiviCRM meet',
2156 'Event',
2157 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
6c6e6187 2158 ), array()
6a488035
TO
2159 );
2160 $mut->stop();
2161 }
2162
4302618d 2163 /**
1e52837d
EM
2164 * This function does a GET & compares the result against the $params.
2165 *
2166 * Use as a double check on Creates.
2167 *
2168 * @param array $params
2169 * @param int $id
67f947ac 2170 * @param bool $delete
6c6e6187 2171 */
1e52837d 2172 public function contributionGetnCheck($params, $id, $delete = TRUE) {
6a488035 2173
4ab7d517 2174 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 2175 'id' => $id,
4ab7d517 2176
5896d037 2177 ));
6a488035
TO
2178
2179 if ($delete) {
4ab7d517 2180 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 2181 }
2bfae985 2182 $this->assertAPISuccess($contribution, 0);
6a488035
TO
2183 $values = $contribution['values'][$contribution['id']];
2184 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2185 // this is not returned in id format
2186 unset($params['payment_instrument_id']);
2187 $params['contribution_source'] = $params['source'];
2188 unset($params['source']);
2189 foreach ($params as $key => $value) {
22f80e87 2190 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
6a488035
TO
2191 }
2192 }
2193
0efa8efe 2194 /**
1e52837d 2195 * Create a pending contribution & linked pending participant record (along with an event).
0efa8efe 2196 */
5896d037 2197 public function createPendingParticipantContribution() {
6c6e6187 2198 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
0efa8efe 2199 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
5896d037 2200 $this->_ids['participant'] = $participantID;
0efa8efe 2201 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
6c6e6187 2202 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 2203 $this->callAPISuccess('participant_payment', 'create', array(
92915c55
TO
2204 'contribution_id' => $contribution['id'],
2205 'participant_id' => $participantID,
2206 ));
858d0bf8 2207 $this->callAPISuccess('line_item', 'get', array(
0efa8efe 2208 'entity_id' => $contribution['id'],
2209 'entity_table' => 'civicrm_contribution',
2210 'api.line_item.create' => array(
2211 'entity_id' => $participantID,
2212 'entity_table' => 'civicrm_participant',
2213 ),
2214 ));
2215 return $contribution['id'];
2216 }
2217
4cbe18b8 2218 /**
1e52837d
EM
2219 * Get financial transaction amount.
2220 *
100fef9d 2221 * @param int $contId
4cbe18b8
EM
2222 *
2223 * @return null|string
f4d89200 2224 */
2da40d21 2225 public function _getFinancialTrxnAmount($contId) {
6c6e6187 2226 $query = "SELECT
6a488035
TO
2227 SUM( ft.total_amount ) AS total
2228 FROM civicrm_financial_trxn AS ft
2229 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2230 WHERE ceft.entity_table = 'civicrm_contribution'
2231 AND ceft.entity_id = {$contId}";
2232
6c6e6187
TO
2233 $result = CRM_Core_DAO::singleValueQuery($query);
2234 return $result;
2235 }
6a488035 2236
4cbe18b8 2237 /**
100fef9d 2238 * @param int $contId
4cbe18b8
EM
2239 *
2240 * @return null|string
f4d89200 2241 */
2da40d21 2242 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
2243 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2244 $query = "SELECT
6a488035
TO
2245 SUM(amount)
2246 FROM civicrm_financial_item
2247 WHERE entity_table = 'civicrm_line_item'
2248 AND entity_id = {$lineItem}";
6c6e6187
TO
2249 $result = CRM_Core_DAO::singleValueQuery($query);
2250 return $result;
2251 }
6a488035 2252
4cbe18b8 2253 /**
100fef9d 2254 * @param int $contId
4cbe18b8
EM
2255 * @param $context
2256 */
00be9182 2257 public function _checkFinancialItem($contId, $context) {
6c6e6187
TO
2258 if ($context != 'paylater') {
2259 $params = array(
5896d037
TO
2260 'entity_id' => $contId,
2261 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2262 );
2263 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2264 $entityParams = array(
6a488035
TO
2265 'financial_trxn_id' => $trxn['financial_trxn_id'],
2266 'entity_table' => 'civicrm_financial_item',
6c6e6187
TO
2267 );
2268 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2269 $params = array(
6a488035 2270 'id' => $entityTrxn['entity_id'],
6c6e6187
TO
2271 );
2272 }
2273 if ($context == 'paylater') {
2274 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2275 foreach ($lineItems as $key => $item) {
2276 $params = array(
5896d037
TO
2277 'entity_id' => $key,
2278 'entity_table' => 'civicrm_line_item',
6c6e6187
TO
2279 );
2280 $compareParams = array('status_id' => 1);
2281 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2282 }
2283 }
2284 elseif ($context == 'refund') {
2285 $compareParams = array(
5896d037
TO
2286 'status_id' => 1,
2287 'financial_account_id' => 1,
2288 'amount' => -100,
6c6e6187
TO
2289 );
2290 }
2291 elseif ($context == 'cancelPending') {
2292 $compareParams = array(
5896d037
TO
2293 'status_id' => 3,
2294 'financial_account_id' => 1,
2295 'amount' => -100,
6c6e6187
TO
2296 );
2297 }
2298 elseif ($context == 'changeFinancial') {
2299 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2300 $params = array(
5896d037
TO
2301 'entity_id' => $lineKey,
2302 'amount' => -100,
6c6e6187
TO
2303 );
2304 $compareParams = array(
5896d037 2305 'financial_account_id' => 1,
6c6e6187
TO
2306 );
2307 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2308 $params = array(
5896d037
TO
2309 'financial_account_id' => 3,
2310 'entity_id' => $lineKey,
6c6e6187
TO
2311 );
2312 $compareParams = array(
5896d037 2313 'amount' => 100,
6c6e6187
TO
2314 );
2315 }
2316 if ($context != 'paylater') {
2317 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2318 }
2319 }
6a488035 2320
4cbe18b8 2321 /**
4ff927bc 2322 * @param array $contribution
2323 * @param string $context
100fef9d 2324 * @param int $instrumentId
4cbe18b8 2325 */
797d4c52 2326 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
6c6e6187 2327 $trxnParams = array(
5896d037
TO
2328 'entity_id' => $contribution['id'],
2329 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2330 );
2331 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2332 $params = array(
5896d037 2333 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
2334 );
2335 if ($context == 'payLater') {
2336 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2337 $compareParams = array(
5896d037
TO
2338 'status_id' => 1,
2339 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
6c6e6187
TO
2340 );
2341 }
2342 elseif ($context == 'refund') {
2343 $compareParams = array(
5896d037
TO
2344 'to_financial_account_id' => 6,
2345 'total_amount' => -100,
2346 'status_id' => 7,
b7990bb6 2347 'trxn_date' => '2015-01-01 09:00:00',
797d4c52 2348 'trxn_id' => 'the refund',
6c6e6187
TO
2349 );
2350 }
2351 elseif ($context == 'cancelPending') {
2352 $compareParams = array(
ed4d0aea 2353 'to_financial_account_id' => 7,
5896d037
TO
2354 'total_amount' => -100,
2355 'status_id' => 3,
6c6e6187
TO
2356 );
2357 }
2358 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
2359 $entityParams = array(
5896d037
TO
2360 'entity_id' => $contribution['id'],
2361 'entity_table' => 'civicrm_contribution',
2362 'amount' => -100,
6c6e6187
TO
2363 );
2364 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2365 $trxnParams1 = array(
6a488035 2366 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
2367 );
2368 $compareParams = array(
5896d037
TO
2369 'total_amount' => -100,
2370 'status_id' => 1,
6c6e6187
TO
2371 );
2372 if ($context == 'paymentInstrument') {
2373 $compareParams += array(
5896d037
TO
2374 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
2375 'payment_instrument_id' => 4,
6c6e6187
TO
2376 );
2377 }
2378 else {
2379 $compareParams['to_financial_account_id'] = 12;
2380 }
797d4c52 2381 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
6c6e6187
TO
2382 $compareParams['total_amount'] = 100;
2383 if ($context == 'paymentInstrument') {
2384 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
2385 $compareParams['payment_instrument_id'] = $instrumentId;
2386 }
2387 else {
2388 $compareParams['to_financial_account_id'] = 12;
2389 }
2390 }
2391
797d4c52 2392 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
6c6e6187 2393 }
6a488035 2394
4cbe18b8
EM
2395 /**
2396 * @return mixed
2397 */
5896d037 2398 public function _addPaymentInstrument() {
6c6e6187
TO
2399 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2400 $optionParams = array(
5896d037
TO
2401 'option_group_id' => $gId,
2402 'label' => 'Test Card',
2403 'name' => 'Test Card',
2404 'value' => '6',
2405 'weight' => '6',
2406 'is_active' => 1,
6c6e6187
TO
2407 );
2408 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2409 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2410 $financialParams = array(
5896d037
TO
2411 'entity_table' => 'civicrm_option_value',
2412 'entity_id' => $optionValue['id'],
2413 'account_relationship' => $relationTypeId,
2414 'financial_account_id' => 7,
6c6e6187
TO
2415 );
2416 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
2417 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2418 return $optionValue['values'][$optionValue['id']]['value'];
2419 }
6a488035 2420
4cbe18b8 2421 /**
c490a46a 2422 * @param array $params
4cbe18b8
EM
2423 * @param $context
2424 */
6c6e6187
TO
2425 public function _checkFinancialRecords($params, $context) {
2426 $entityParams = array(
5896d037
TO
2427 'entity_id' => $params['id'],
2428 'entity_table' => 'civicrm_contribution',
6c6e6187 2429 );
e0e3c51b
EM
2430 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id']));
2431 $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']);
6c6e6187
TO
2432 if ($context == 'pending') {
2433 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
2434 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
2435 return;
2436 }
2437 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2438 $trxnParams = array(
5896d037 2439 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
2440 );
2441 if ($context != 'online' && $context != 'payLater') {
2442 $compareParams = array(
5896d037
TO
2443 'to_financial_account_id' => 6,
2444 'total_amount' => 100,
2445 'status_id' => 1,
6c6e6187
TO
2446 );
2447 }
2448 if ($context == 'feeAmount') {
2449 $compareParams['fee_amount'] = 50;
2450 }
2451 elseif ($context == 'online') {
2452 $compareParams = array(
5896d037
TO
2453 'to_financial_account_id' => 12,
2454 'total_amount' => 100,
2455 'status_id' => 1,
6c6e6187
TO
2456 );
2457 }
2458 elseif ($context == 'payLater') {
2459 $compareParams = array(
5896d037
TO
2460 'to_financial_account_id' => 7,
2461 'total_amount' => 100,
2462 'status_id' => 2,
6c6e6187
TO
2463 );
2464 }
2465 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2466 $entityParams = array(
5896d037
TO
2467 'financial_trxn_id' => $trxn['financial_trxn_id'],
2468 'entity_table' => 'civicrm_financial_item',
6c6e6187
TO
2469 );
2470 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2471 $fitemParams = array(
5896d037 2472 'id' => $entityTrxn['entity_id'],
6c6e6187
TO
2473 );
2474 $compareParams = array(
5896d037
TO
2475 'amount' => 100,
2476 'status_id' => 1,
2477 'financial_account_id' => 1,
6c6e6187
TO
2478 );
2479 if ($context == 'payLater') {
2480 $compareParams = array(
5896d037
TO
2481 'amount' => 100,
2482 'status_id' => 3,
2483 'financial_account_id' => 1,
6c6e6187
TO
2484 );
2485 }
2486 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2487 if ($context == 'feeAmount') {
2488 $maxParams = array(
5896d037
TO
2489 'entity_id' => $params['id'],
2490 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2491 );
2492 $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE));
2493 $trxnParams = array(
5896d037 2494 'id' => $maxTrxn['financial_trxn_id'],
6c6e6187
TO
2495 );
2496 $compareParams = array(
5896d037
TO
2497 'to_financial_account_id' => 5,
2498 'from_financial_account_id' => 6,
2499 'total_amount' => 50,
2500 'status_id' => 1,
6c6e6187
TO
2501 );
2502 $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC');
2503 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
2504 $fitemParams = array(
5896d037
TO
2505 'entity_id' => $trxnId['financialTrxnId'],
2506 'entity_table' => 'civicrm_financial_trxn',
6c6e6187
TO
2507 );
2508 $compareParams = array(
5896d037
TO
2509 'amount' => 50,
2510 'status_id' => 1,
2511 'financial_account_id' => 5,
6c6e6187
TO
2512 );
2513 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
2514 }
2515 }
96025800 2516
3c49d90c 2517 /**
2518 * Set up the basic recurring contribution for tests.
2519 *
2520 * @param array $generalParams
2521 * Parameters that can be merged into the recurring AND the contribution.
7f4ef731 2522 *
2523 * @param array $recurParams
2524 * Parameters to merge into the recur only.
3c49d90c 2525 *
2526 * @return array|int
2527 */
7f4ef731 2528 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3c49d90c 2529 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2530 'contact_id' => $this->_individualId,
2531 'installments' => '12',
2532 'frequency_interval' => '1',
2533 'amount' => '100',
2534 'contribution_status_id' => 1,
2535 'start_date' => '2012-01-01 00:00:00',
2536 'currency' => 'USD',
2537 'frequency_unit' => 'month',
2538 'payment_processor_id' => $this->paymentProcessorID,
7f4ef731 2539 ), $generalParams, $recurParams));
3c49d90c 2540 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2541 $this->_params,
2542 array(
2543 'contribution_recur_id' => $contributionRecur['id'],
2544 ), $generalParams)
2545 );
2546 return $originalContribution;
2547 }
2548
6a488035 2549}