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