Merge pull request #9717 from eileenmcnaughton/comments
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 27
6a488035
TO
28/**
29 * Test APIv3 civicrm_contribute_* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
acb109b7 33 * @group headless
6a488035 34 */
6a488035
TO
35class api_v3_ContributionTest extends CiviUnitTestCase {
36
37 /**
fe482240 38 * Assume empty database with just civicrm_data.
6a488035
TO
39 */
40 protected $_individualId;
41 protected $_contribution;
4ab7d517 42 protected $_financialTypeId = 1;
6a488035
TO
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
45 public $debug = 0;
46 protected $_params;
858d0bf8
EM
47 protected $_ids = array();
48 protected $_pageParams = array();
1eade77d 49 /**
50 * Payment processor ID (dummy processor).
51 *
52 * @var int
53 */
54 protected $paymentProcessorID;
b7c9bc4c 55
1e52837d
EM
56 /**
57 * Parameters to create payment processor.
58 *
59 * @var array
60 */
61 protected $_processorParams = array();
62
63 /**
64 * ID of created event.
65 *
66 * @var int
67 */
68 protected $_eventID;
69
ec7e3954
E
70 /**
71 * @var CiviMailUtils
72 */
73 protected $mut;
74
22f80e87
EM
75 /**
76 * Setup function.
77 */
00be9182 78 public function setUp() {
6a488035
TO
79 parent::setUp();
80
81 $this->_apiversion = 3;
82 $this->_individualId = $this->individualCreate();
6a488035
TO
83 $this->_params = array(
84 'contact_id' => $this->_individualId,
85 'receive_date' => '20120511',
86 'total_amount' => 100.00,
5896d037 87 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
88 'non_deductible_amount' => 10.00,
89 'fee_amount' => 5.00,
90 'net_amount' => 95.00,
91 'source' => 'SSF',
92 'contribution_status_id' => 1,
6a488035
TO
93 );
94 $this->_processorParams = array(
95 'domain_id' => 1,
96 'name' => 'Dummy',
97502bac 97 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
6a488035
TO
98 'financial_account_id' => 12,
99 'is_active' => 1,
100 'user_name' => '',
101 'url_site' => 'http://dummy.com',
102 'url_recur' => 'http://dummy.com',
103 'billing_mode' => 1,
104 );
1eade77d 105 $this->paymentProcessorID = $this->processorCreate();
6a488035 106 $this->_pageParams = array(
6a488035
TO
107 'title' => 'Test Contribution Page',
108 'financial_type_id' => 1,
109 'currency' => 'USD',
110 'financial_account_id' => 1,
1eade77d 111 'payment_processor' => $this->paymentProcessorID,
6a488035
TO
112 'is_active' => 1,
113 'is_allow_other_amount' => 1,
114 'min_amount' => 10,
115 'max_amount' => 1000,
5896d037 116 );
6a488035
TO
117 }
118
22f80e87
EM
119 /**
120 * Clean up after each test.
121 */
00be9182 122 public function tearDown() {
39d632fd 123 $this->quickCleanUpFinancialEntities();
225d474b 124 $this->quickCleanup(array('civicrm_uf_match'));
6a488035
TO
125 }
126
22f80e87
EM
127 /**
128 * Test Get.
129 */
00be9182 130 public function testGetContribution() {
6a488035
TO
131 $p = array(
132 'contact_id' => $this->_individualId,
133 'receive_date' => '2010-01-20',
134 'total_amount' => 100.00,
4ab7d517 135 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
136 'non_deductible_amount' => 10.00,
137 'fee_amount' => 5.00,
138 'net_amount' => 95.00,
139 'trxn_id' => 23456,
140 'invoice_id' => 78910,
141 'source' => 'SSF',
142 'contribution_status_id' => 1,
6a488035 143 );
2f45e1c2 144 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
145
146 $params = array(
147 'contribution_id' => $this->_contribution['id'],
6a488035 148 );
e0e3c51b 149
2f45e1c2 150 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 151 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 152 $default = NULL;
858d0bf8 153 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 154
e0e3c51b 155 $this->assertEquals(1, $contribution['count']);
2bfae985 156 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
22f80e87
EM
157 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
158 // Passing back a string rather than an id seems like an error/cruft.
159 // If it is to be introduced we should discuss.
6a488035 160 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
161 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
162 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
163 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
164 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
165 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
166 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
167 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
168 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
22f80e87 169 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
170 $p['trxn_id'] = '3847';
171 $p['invoice_id'] = '3847';
172
2f45e1c2 173 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
174
22f80e87 175 // Now we have 2 - test getcount.
4ab7d517 176 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035 177 $this->assertEquals(2, $contribution);
22f80e87 178 // Test id only format.
4ab7d517 179 $contribution = $this->callAPISuccess('contribution', 'get', array(
180 'id' => $this->_contribution['id'],
181 'format.only_id' => 1,
182 ));
22f80e87
EM
183 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
184 // Test id only format.
2f45e1c2 185 $contribution = $this->callAPISuccess('contribution', 'get', array(
4ab7d517 186 'id' => $contribution2['id'],
187 'format.only_id' => 1,
188 ));
189 $this->assertEquals($contribution2['id'], $contribution);
22f80e87 190 // Test id as field.
4ab7d517 191 $contribution = $this->callAPISuccess('contribution', 'get', array(
192 'id' => $this->_contribution['id'],
193 ));
2bfae985 194 $this->assertEquals(1, $contribution['count']);
4ab7d517 195
22f80e87 196 // Test get by contact id works.
4ab7d517 197 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 198
2bfae985 199 $this->assertEquals(2, $contribution['count']);
2f45e1c2 200 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 201 'id' => $this->_contribution['id'],
4ab7d517 202 ));
2f45e1c2 203 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 204 'id' => $contribution2['id'],
4ab7d517 205 ));
6a488035
TO
206 }
207
71d5a412 208 /**
209 * Test that test contributions can be retrieved.
210 */
211 public function testGetTestContribution() {
212 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1)));
213 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
214 }
215
59f9e5a6 216 /**
217 * Test the 'return' param works for all fields.
218 */
219 public function testGetContributionReturnFunctionality() {
220 $params = $this->_params;
221 $params['check_number'] = 'bouncer';
222 $params['payment_instrument_id'] = 'Check';
223 $params['cancel_date'] = 'yesterday';
224 $params['receipt_date'] = 'yesterday';
225 $params['thankyou_date'] = 'yesterday';
226 $params['revenue_recognition_date'] = 'yesterday';
227 $params['amount_level'] = 'Unreasonable';
228 $params['cancel_reason'] = 'You lose sucker';
229 $params['creditnote_id'] = 'sudo rm -rf';
230 $params['tax_amount'] = '1';
231 $address = $this->callAPISuccess('Address', 'create', array(
232 'street_address' => 'Knockturn Alley',
233 'contact_id' => $this->_individualId,
234 'location_type_id' => 'Home',
235 ));
236 $params['address_id'] = $address['id'];
237 $contributionPage = $this->contributionPageCreate();
238 $params['contribution_page_id'] = $contributionPage['id'];
239 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array(
240 'contact_id' => $this->_individualId,
241 'frequency_interval' => 1,
242 'amount' => 5,
243 ));
244 $params['contribution_recur_id'] = $contributionRecur['id'];
245
246 $params['campaign_id'] = $this->campaignCreate();
247
248 $contributionID = $this->contributionCreate($params);
249 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
250 $this->assertEquals('bouncer', $contribution['check_number']);
251 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
252
253 $fields = CRM_Contribute_BAO_Contribution::fields();
254 $fieldsLockedIn = array(
255 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
256 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
257 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
258 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
259 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
260 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
261 );
262 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
263 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
264 // covered as the test contract now guarantees them in the return array.
265 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
266 foreach ($fields as $fieldName => $fieldSpec) {
267 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
268 $returnField = $fieldName;
269 if ($returnField == 'contribution_contact_id') {
270 $returnField = 'contact_id';
271 }
272 $this->assertTrue((!empty($contribution[$returnField]) || $contribution[$returnField] === "0"), $returnField);
273 }
274 }
275
6c6e6187 276 /**
22f80e87 277 * We need to ensure previous tested behaviour still works as part of the api contract.
6c6e6187 278 */
00be9182 279 public function testGetContributionLegacyBehaviour() {
6a488035
TO
280 $p = array(
281 'contact_id' => $this->_individualId,
282 'receive_date' => '2010-01-20',
283 'total_amount' => 100.00,
4ab7d517 284 'contribution_type_id' => $this->_financialTypeId,
6a488035
TO
285 'non_deductible_amount' => 10.00,
286 'fee_amount' => 5.00,
287 'net_amount' => 95.00,
288 'trxn_id' => 23456,
289 'invoice_id' => 78910,
290 'source' => 'SSF',
291 'contribution_status_id' => 1,
6a488035 292 );
71d5a412 293 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
6a488035
TO
294
295 $params = array(
296 'contribution_id' => $this->_contribution['id'],
6a488035 297 );
2f45e1c2 298 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 299 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 300 $default = NULL;
858d0bf8 301 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 302
6c6e6187 303 $this->assertEquals(1, $contribution['count']);
2bfae985 304 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
4ab7d517 305 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
306 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
2bfae985
EM
307 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
308 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
309 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
310 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
311 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
312 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
313 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
314 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
22f80e87
EM
315
316 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
317 $p['trxn_id'] = '3847';
318 $p['invoice_id'] = '3847';
319
2f45e1c2 320 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
6a488035 321
6a488035 322 // now we have 2 - test getcount
4ab7d517 323 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
324 $this->assertEquals(2, $contribution);
325 //test id only format
4ab7d517 326 $contribution = $this->callAPISuccess('contribution', 'get', array(
327 'id' => $this->_contribution['id'],
328 'format.only_id' => 1,
329 ));
22f80e87 330 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
6a488035 331 //test id only format
4ab7d517 332 $contribution = $this->callAPISuccess('contribution', 'get', array(
333 'id' => $contribution2['id'],
334 'format.only_id' => 1,
335 ));
6a488035 336 $this->assertEquals($contribution2['id'], $contribution);
2f45e1c2 337 $contribution = $this->callAPISuccess('contribution', 'get', array(
6a488035
TO
338 'id' => $this->_contribution['id'],
339 ));
340 //test id as field
2bfae985 341 $this->assertEquals(1, $contribution['count']);
6a488035
TO
342 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
343 //test get by contact id works
4ab7d517 344 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 345
2bfae985 346 $this->assertEquals(2, $contribution['count']);
2f45e1c2 347 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 348 'id' => $this->_contribution['id'],
6a488035 349 ));
2f45e1c2 350 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 351 'id' => $contribution2['id'],
6a488035
TO
352 ));
353 }
5896d037 354
a1a2a83d
TO
355 /**
356 * Create an contribution_id=FALSE and financial_type_id=Donation.
357 */
00be9182 358 public function testCreateEmptyContributionIDUseDonation() {
6a488035
TO
359 $params = array(
360 'contribution_id' => FALSE,
361 'contact_id' => 1,
362 'total_amount' => 1,
6c6e6187 363 'check_permissions' => FALSE,
6a488035
TO
364 'financial_type_id' => 'Donation',
365 );
858d0bf8 366 $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 367 }
6a488035 368
6a488035 369 /**
1e52837d
EM
370 * Check with complete array + custom field.
371 *
6a488035
TO
372 * Note that the test is written on purpose without any
373 * variables specific to participant so it can be replicated into other entities
374 * and / or moved to the automated test suite
375 */
00be9182 376 public function testCreateWithCustom() {
6a488035
TO
377 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
378
379 $params = $this->_params;
380 $params['custom_' . $ids['custom_field_id']] = "custom string";
381
6c6e6187 382 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 383 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
2f45e1c2 384 $check = $this->callAPISuccess($this->_entity, 'get', array(
4ab7d517 385 'return.custom_' . $ids['custom_field_id'] => 1,
386 'id' => $result['id'],
387 ));
6a488035
TO
388 $this->customFieldDelete($ids['custom_field_id']);
389 $this->customGroupDelete($ids['custom_group_id']);
22f80e87 390 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
391 }
392
393 /**
fd786d03
EM
394 * Check with complete array + custom field.
395 *
6a488035
TO
396 * Note that the test is written on purpose without any
397 * variables specific to participant so it can be replicated into other entities
398 * and / or moved to the automated test suite
399 */
00be9182 400 public function testCreateGetFieldsWithCustom() {
5896d037 401 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
6a488035 402 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
5896d037 403 $result = $this->callAPISuccess('Contribution', 'getfields', array());
6a488035
TO
404 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
405 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
406 $this->customFieldDelete($ids['custom_field_id']);
407 $this->customGroupDelete($ids['custom_group_id']);
408 $this->customFieldDelete($idsContact['custom_field_id']);
409 $this->customGroupDelete($idsContact['custom_group_id']);
410 }
411
00be9182 412 public function testCreateContributionNoLineItems() {
6a488035
TO
413
414 $params = array(
415 'contact_id' => $this->_individualId,
416 'receive_date' => '20120511',
417 'total_amount' => 100.00,
5896d037 418 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
419 'payment_instrument_id' => 1,
420 'non_deductible_amount' => 10.00,
421 'fee_amount' => 50.00,
422 'net_amount' => 90.00,
423 'trxn_id' => 12345,
424 'invoice_id' => 67890,
425 'source' => 'SSF',
426 'contribution_status_id' => 1,
6a488035
TO
427 'skipLineItem' => 1,
428 );
429
2f45e1c2 430 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 431 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035
TO
432 'entity_id' => $contribution['id'],
433 'entity_table' => 'civicrm_contribution',
434 'sequential' => 1,
435 ));
436 $this->assertEquals(0, $lineItems['count']);
437 }
5896d037 438
a1a2a83d 439 /**
eceb18cc 440 * Test checks that passing in line items suppresses the create mechanism.
6a488035 441 */
00be9182 442 public function testCreateContributionChainedLineItems() {
6a488035
TO
443 $params = array(
444 'contact_id' => $this->_individualId,
445 'receive_date' => '20120511',
446 'total_amount' => 100.00,
4ab7d517 447 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
448 'payment_instrument_id' => 1,
449 'non_deductible_amount' => 10.00,
450 'fee_amount' => 50.00,
451 'net_amount' => 90.00,
452 'trxn_id' => 12345,
453 'invoice_id' => 67890,
454 'source' => 'SSF',
455 'contribution_status_id' => 1,
6a488035
TO
456 'skipLineItem' => 1,
457 'api.line_item.create' => array(
458 array(
459 'price_field_id' => 1,
460 'qty' => 2,
461 'line_total' => '20',
462 'unit_price' => '10',
463 ),
464 array(
465 'price_field_id' => 1,
466 'qty' => 1,
467 'line_total' => '80',
468 'unit_price' => '80',
469 ),
470 ),
471 );
472
5c49fee0 473 $description = "Create Contribution with Nested Line Items.";
6a488035 474 $subfile = "CreateWithNestedLineItems";
6c6e6187 475 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
2f45e1c2 476
6c6e6187 477 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035 478 'entity_id' => $contribution['id'],
4ede4532 479 'contribution_id' => $contribution['id'],
6a488035
TO
480 'entity_table' => 'civicrm_contribution',
481 'sequential' => 1,
482 ));
483 $this->assertEquals(2, $lineItems['count']);
484 }
485
00be9182 486 public function testCreateContributionOffline() {
6a488035
TO
487 $params = array(
488 'contact_id' => $this->_individualId,
489 'receive_date' => '20120511',
490 'total_amount' => 100.00,
491 'financial_type_id' => 1,
492 'trxn_id' => 12345,
493 'invoice_id' => 67890,
494 'source' => 'SSF',
495 'contribution_status_id' => 1,
6a488035
TO
496 );
497
4ab7d517 498 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
499 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
500 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 501 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
502 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
503 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
504 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
505 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
6c6e6187 506 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035 507 'entity_id' => $contribution['id'],
4ede4532 508 'contribution_id' => $contribution['id'],
6a488035
TO
509 'entity_table' => 'civicrm_contribution',
510 'sequential' => 1,
5896d037 511 ));
6a488035
TO
512 $this->assertEquals(1, $lineItems['count']);
513 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 514 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6a488035
TO
515 $this->_checkFinancialRecords($contribution, 'offline');
516 $this->contributionGetnCheck($params, $contribution['id']);
517 }
5896d037 518
f70a6752 519 /**
28de42d1 520 * Test create with valid payment instrument.
6a488035 521 */
00be9182 522 public function testCreateContributionWithPaymentInstrument() {
6a488035 523 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 524 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 525 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 526 'sequential' => 1,
21dfd5f5 527 'id' => $contribution['id'],
53191813 528 ));
6a488035 529 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
530 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
531
5896d037 532 $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
533 'id' => $contribution['id'],
534 'payment_instrument' => 'Credit Card',
535 ));
6c6e6187 536 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 537 'sequential' => 1,
21dfd5f5 538 'id' => $contribution['id'],
53191813
CW
539 ));
540 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
541 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
6a488035
TO
542 }
543
00be9182 544 public function testGetContributionByPaymentInstrument() {
6a488035 545 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 546 $params2 = $this->_params + array('payment_instrument' => 'Cash');
6c6e6187
TO
547 $this->callAPISuccess('contribution', 'create', $params);
548 $this->callAPISuccess('contribution', 'create', $params2);
5896d037 549 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55 550 'sequential' => 1,
7d543448 551 'contribution_payment_instrument' => 'Cash',
92915c55 552 ));
6a488035 553 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
ff977830
EM
554 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
555 $this->assertEquals(1, $contribution['count']);
868e247d 556 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
6a488035 557 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
868e247d 558 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
6a488035 559 $this->assertEquals(1, $contribution['count']);
5896d037 560 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
561 'sequential' => 1,
562 'payment_instrument_id' => 5,
563 ));
6a488035 564 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
565 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
566 $this->assertEquals(1, $contribution['count']);
5896d037 567 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
568 'sequential' => 1,
569 'payment_instrument' => 'EFT',
570 ));
6a488035 571 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
572 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
573 $this->assertEquals(1, $contribution['count']);
5896d037 574 $contribution = $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
575 'id' => $contribution['id'],
576 'payment_instrument' => 'Credit Card',
577 ));
5896d037 578 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
6a488035 579 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
580 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
581 $this->assertEquals(1, $contribution['count']);
6a488035
TO
582 }
583
b5a37491
EM
584 /**
585 * CRM-16227 introduces invoice_id as a parameter.
586 */
587 public function testGetContributionByInvoice() {
588 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
589 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
590 $this->callAPISuccessGetCount('Contribution', array(), 2);
591 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
592 // The following don't work. They are the format we are trying to introduce but although the form uses this format
593 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
594 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
595 // come out of convertFormValues
596 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
597 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
598 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
599 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
600 // 2);
601 }
602
2b3e31ac 603 /**
604 * Check the credit note retrieval is case insensitive.
605 */
606 public function testGetCreditNoteCaseInsensitive() {
607 $this->contributionCreate(array('contact_id' => $this->_individualId));
608 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId, 'invoice_id' => rand(), 'trxn_id' => rand()));
609 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
610 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
611 }
612
e58a3abb 613 /**
614 * Test retrieval by total_amount works.
615 *
616 * @throws Exception
617 */
618 public function testGetContributionByTotalAmount() {
619 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '5')));
620 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '10')));
621 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
622 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
623 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
624 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
625 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
626 $this->callAPISuccessGetCount('Contribution', array(), 2);
627 }
628
c490a46a 629 /**
eceb18cc 630 * Create test with unique field name on source.
c490a46a 631 */
00be9182 632 public function testCreateContributionSource() {
6a488035
TO
633
634 $params = array(
635 'contact_id' => $this->_individualId,
636 'receive_date' => date('Ymd'),
637 'total_amount' => 100.00,
4ab7d517 638 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
639 'payment_instrument_id' => 1,
640 'non_deductible_amount' => 10.00,
641 'fee_amount' => 50.00,
642 'net_amount' => 90.00,
643 'trxn_id' => 12345,
644 'invoice_id' => 67890,
645 'contribution_source' => 'SSF',
646 'contribution_status_id' => 1,
6a488035
TO
647 );
648
4ab7d517 649 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
650 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
651 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
6a488035 652 }
d5d148ab 653
d424ffde 654 /**
eceb18cc 655 * Create test with unique field name on source.
d424ffde 656 */
00be9182 657 public function testCreateDefaultNow() {
d5d148ab
EM
658
659 $params = $this->_params;
660 unset($params['receive_date']);
661
662 $contribution = $this->callAPISuccess('contribution', 'create', $params);
663 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
664 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
665 }
666
c490a46a 667 /**
55d2c6f1 668 * Create test with unique field name on source.
c490a46a 669 */
55d2c6f1 670 public function testCreateContributionSourceInvalidContact() {
6a488035
TO
671
672 $params = array(
673 'contact_id' => 999,
674 'receive_date' => date('Ymd'),
675 'total_amount' => 100.00,
4ab7d517 676 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
677 'payment_instrument_id' => 1,
678 'non_deductible_amount' => 10.00,
679 'fee_amount' => 50.00,
680 'net_amount' => 90.00,
681 'trxn_id' => 12345,
682 'invoice_id' => 67890,
683 'contribution_source' => 'SSF',
684 'contribution_status_id' => 1,
6a488035
TO
685 );
686
858d0bf8 687 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
688 }
689
00be9182 690 public function testCreateContributionSourceInvalidContContact() {
6a488035
TO
691
692 $params = array(
693 'contribution_contact_id' => 999,
694 'receive_date' => date('Ymd'),
695 'total_amount' => 100.00,
4ab7d517 696 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
697 'payment_instrument_id' => 1,
698 'non_deductible_amount' => 10.00,
699 'fee_amount' => 50.00,
700 'net_amount' => 90.00,
701 'trxn_id' => 12345,
702 'invoice_id' => 67890,
703 'contribution_source' => 'SSF',
704 'contribution_status_id' => 1,
6a488035
TO
705 );
706
33139905 707 $this->callAPIFailure('contribution', 'create', $params);
6a488035
TO
708 }
709
858d0bf8 710 /**
442cf836 711 * Test note created correctly.
858d0bf8 712 */
00be9182 713 public function testCreateContributionWithNote() {
5c49fee0 714 $description = "Demonstrates creating contribution with Note Entity.";
5896d037
TO
715 $subfile = "ContributionCreateWithNote";
716 $params = array(
6a488035
TO
717 'contact_id' => $this->_individualId,
718 'receive_date' => '2012-01-01',
719 'total_amount' => 100.00,
4ab7d517 720 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
721 'payment_instrument_id' => 1,
722 'non_deductible_amount' => 10.00,
723 'fee_amount' => 50.00,
724 'net_amount' => 90.00,
725 'trxn_id' => 12345,
726 'invoice_id' => 67890,
727 'source' => 'SSF',
728 'contribution_status_id' => 1,
6a488035
TO
729 'note' => 'my contribution note',
730 );
731
4ab7d517 732 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
5896d037 733 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
734 'entity_table' => 'civicrm_contribution',
735 'entity_id' => $contribution['id'],
736 'sequential' => 1,
737 ));
6a488035 738 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 739 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
740 }
741
00be9182 742 public function testCreateContributionWithNoteUniqueNameAliases() {
5896d037 743 $params = array(
6a488035
TO
744 'contact_id' => $this->_individualId,
745 'receive_date' => '2012-01-01',
746 'total_amount' => 100.00,
4ab7d517 747 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
748 'payment_instrument_id' => 1,
749 'non_deductible_amount' => 10.00,
750 'fee_amount' => 50.00,
751 'net_amount' => 90.00,
752 'trxn_id' => 12345,
753 'invoice_id' => 67890,
754 'source' => 'SSF',
755 'contribution_status_id' => 1,
6a488035
TO
756 'contribution_note' => 'my contribution note',
757 );
758
4ab7d517 759 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 760 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
761 'entity_table' => 'civicrm_contribution',
762 'entity_id' => $contribution['id'],
763 'sequential' => 1,
764 ));
6a488035 765 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 766 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035 767 }
c490a46a
CW
768
769 /**
55d2c6f1 770 * This is the test for creating soft credits.
c490a46a 771 */
55d2c6f1 772 public function testCreateContributionWithSoftCredit() {
5c49fee0 773 $description = "Demonstrates creating contribution with SoftCredit.";
5896d037
TO
774 $subfile = "ContributionCreateWithSoftCredit";
775 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
776 'display_name' => 'superman',
777 'contact_type' => 'Individual',
778 ));
55d2c6f1 779 $softParams = array(
bcc03b98 780 'contact_id' => $contact2['id'],
781 'amount' => 50,
21dfd5f5 782 'soft_credit_type_id' => 3,
6a488035
TO
783 );
784
55d2c6f1 785 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
4ab7d517 786 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 787 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
a1c68fd2 788
55d2c6f1
EM
789 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
790 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
791 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a516bd6
DG
792
793 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
794 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
795 }
796
00be9182 797 public function testCreateContributionWithSoftCreditDefaults() {
5c49fee0 798 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
5896d037
TO
799 $subfile = "ContributionCreateWithSoftCreditDefaults";
800 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
801 'display_name' => 'superman',
802 'contact_type' => 'Individual',
803 ));
6a516bd6 804 $params = $this->_params + array(
442cf836
EM
805 'soft_credit_to' => $contact2['id'],
806 );
6a516bd6 807 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 808 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
809
810 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
811 // Default soft credit amount = contribution.total_amount
812 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
813 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
814
815 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
816 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
817 }
818
00be9182 819 public function testCreateContributionWithHonoreeContact() {
5c49fee0 820 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
5896d037
TO
821 $subfile = "ContributionCreateWithHonoreeContact";
822 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
823 'display_name' => 'superman',
824 'contact_type' => 'Individual',
825 ));
6a516bd6 826 $params = $this->_params + array(
442cf836
EM
827 'honor_contact_id' => $contact2['id'],
828 );
6a516bd6 829 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 830 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
831
832 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
833 // Default soft credit amount = contribution.total_amount
834 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
835 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
836 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a488035 837
4ab7d517 838 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
839 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
6a488035
TO
840 }
841
842 /**
92c99a4a 843 * Test using example code.
6a488035 844 */
00be9182 845 public function testContributionCreateExample() {
6a488035 846 //make sure at least on page exists since there is a truncate in tear down
8be629ac 847 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
3ec6e38d 848 require_once 'api/v3/examples/Contribution/Create.php';
5896d037 849 $result = contribution_create_example();
006d6361 850 $id = $result['id'];
6a488035 851 $expectedResult = contribution_create_expectedresult();
8e342a79 852 $this->checkArrayEquals($expectedResult, $result);
006d6361 853 $this->contributionDelete($id);
6a488035
TO
854 }
855
a1a2a83d 856 /**
f55c5fa8 857 * Function tests that additional financial records are created when fee amount is recorded.
6a488035 858 */
00be9182 859 public function testCreateContributionWithFee() {
6a488035
TO
860 $params = array(
861 'contact_id' => $this->_individualId,
862 'receive_date' => '20120511',
863 'total_amount' => 100.00,
864 'fee_amount' => 50,
865 'financial_type_id' => 1,
866 'trxn_id' => 12345,
867 'invoice_id' => 67890,
868 'source' => 'SSF',
869 'contribution_status_id' => 1,
6a488035
TO
870 );
871
4ab7d517 872 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
873 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
874 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
e0e3c51b
EM
875 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
876 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
5896d037 877 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
878 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
879 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
880 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
881 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
e0e3c51b 882
6c6e6187 883 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 884
6a488035
TO
885 'entity_id' => $contribution['id'],
886 'entity_table' => 'civicrm_contribution',
887 'sequential' => 1,
5896d037 888 ));
6a488035
TO
889 $this->assertEquals(1, $lineItems['count']);
890 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 891 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6c6e6187 892 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 893
5896d037
TO
894 'entity_id' => $contribution['id'],
895 'contribution_id' => $contribution['id'],
896 'entity_table' => 'civicrm_contribution',
897 'sequential' => 1,
6a488035
TO
898 ));
899 $this->assertEquals(1, $lineItems['count']);
900 $this->_checkFinancialRecords($contribution, 'feeAmount');
901 }
902
903
f70a6752 904 /**
442cf836 905 * Function tests that additional financial records are created when online contribution is created.
6a488035 906 */
00be9182 907 public function testCreateContributionOnline() {
858d0bf8 908 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 909 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 910 $this->assertAPISuccess($contributionPage);
6a488035
TO
911 $params = array(
912 'contact_id' => $this->_individualId,
913 'receive_date' => '20120511',
914 'total_amount' => 100.00,
915 'financial_type_id' => 1,
916 'contribution_page_id' => $contributionPage['id'],
16f3bd02 917 'payment_processor' => $this->paymentProcessorID,
6a488035
TO
918 'trxn_id' => 12345,
919 'invoice_id' => 67890,
920 'source' => 'SSF',
921 'contribution_status_id' => 1,
4ab7d517 922
6a488035
TO
923 );
924
4ab7d517 925 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
926 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
927 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 928 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
929 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
930 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
931 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
932 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
16f3bd02 933 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
934 'id' => $this->paymentProcessorID,
935 'return' => 'payment_instrument_id',
936 ));
6a488035
TO
937 $this->_checkFinancialRecords($contribution, 'online');
938 }
939
f70a6752 940 /**
442cf836
EM
941 * Check handling of financial type.
942 *
100fef9d 943 * In the interests of removing financial type / contribution type checks from
f70a6752 944 * legacy format function lets test that the api is doing this for us
945 */
00be9182 946 public function testCreateInvalidFinancialType() {
f70a6752 947 $params = $this->_params;
948 $params['financial_type_id'] = 99999;
858d0bf8 949 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
f70a6752 950 }
951
4302618d 952 /**
442cf836
EM
953 * Check handling of financial type.
954 *
100fef9d 955 * In the interests of removing financial type / contribution type checks from
4302618d 956 * legacy format function lets test that the api is doing this for us
957 */
00be9182 958 public function testValidNamedFinancialType() {
4302618d 959 $params = $this->_params;
960 $params['financial_type_id'] = 'Donation';
858d0bf8 961 $this->callAPISuccess($this->_entity, 'create', $params);
4302618d 962 }
963
f70a6752 964 /**
92c99a4a
EM
965 * Tests that additional financial records are created.
966 *
967 * Checks when online contribution with pay later option is created
6a488035 968 */
00be9182 969 public function testCreateContributionPayLaterOnline() {
858d0bf8 970 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
6a488035 971 $this->_pageParams['is_pay_later'] = 1;
5896d037 972 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 973 $this->assertAPISuccess($contributionPage);
6a488035
TO
974 $params = array(
975 'contact_id' => $this->_individualId,
976 'receive_date' => '20120511',
977 'total_amount' => 100.00,
978 'financial_type_id' => 1,
979 'contribution_page_id' => $contributionPage['id'],
980 'trxn_id' => 12345,
981 'is_pay_later' => 1,
982 'invoice_id' => 67890,
983 'source' => 'SSF',
984 'contribution_status_id' => 2,
4ab7d517 985
6a488035
TO
986 );
987
4ab7d517 988 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
989 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
990 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 991 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
992 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
993 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
994 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
995 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
996 $this->_checkFinancialRecords($contribution, 'payLater');
997 }
998
a1a2a83d 999 /**
1e52837d 1000 * Function tests that additional financial records are created for online contribution with pending option.
6a488035 1001 */
00be9182 1002 public function testCreateContributionPendingOnline() {
6a488035 1003 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 1004 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 1005 $this->assertAPISuccess($contributionPage);
6a488035
TO
1006 $params = array(
1007 'contact_id' => $this->_individualId,
1008 'receive_date' => '20120511',
1009 'total_amount' => 100.00,
1010 'financial_type_id' => 1,
1011 'contribution_page_id' => $contributionPage['id'],
1012 'trxn_id' => 12345,
1013 'invoice_id' => 67890,
1014 'source' => 'SSF',
1015 'contribution_status_id' => 2,
6a488035
TO
1016 );
1017
4ab7d517 1018 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
1019 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1020 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 1021 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
1022 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1023 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1024 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1025 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
1026 $this->_checkFinancialRecords($contribution, 'pending');
1027 }
1028
e748bf60 1029 /**
92c99a4a 1030 * Test that BAO defaults work.
e748bf60 1031 */
00be9182 1032 public function testCreateBAODefaults() {
e748bf60
EM
1033 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
1034 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
5896d037 1035 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1036 'id' => $contribution['id'],
1037 'api.contribution.delete' => 1,
1038 ));
e748bf60 1039 $this->assertEquals(1, $contribution['contribution_status_id']);
12879069 1040 $this->assertEquals('Check', $contribution['payment_instrument']);
e748bf60
EM
1041 }
1042
a1a2a83d 1043 /**
1e52837d 1044 * Function tests that line items, financial records are updated when contribution amount is changed.
6a488035 1045 */
00be9182 1046 public function testCreateUpdateContributionChangeTotal() {
4ab7d517 1047 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
6c6e6187 1048 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
4ab7d517 1049
6a488035
TO
1050 'entity_id' => $contribution['id'],
1051 'entity_table' => 'civicrm_contribution',
1052 'sequential' => 1,
1053 'return' => 'line_total',
1054 ));
1055 $this->assertEquals('100.00', $lineItems);
1056 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1057 // Financial trxn SUM = 100 + 5 (fee)
1058 $this->assertEquals('105.00', $trxnAmount);
1059 $newParams = array(
4ab7d517 1060
6a488035 1061 'id' => $contribution['id'],
21dfd5f5 1062 'total_amount' => '125',
5896d037 1063 );
694769da 1064 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
4ab7d517 1065
6c6e6187 1066 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
6a488035 1067
5896d037
TO
1068 'entity_id' => $contribution['id'],
1069 'entity_table' => 'civicrm_contribution',
1070 'sequential' => 1,
1071 'return' => 'line_total',
6a488035
TO
1072 ));
1073
1074 $this->assertEquals('125.00', $lineItems);
1075 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
28de42d1
EM
1076
1077 // Financial trxn SUM = 125 + 5 (fee).
6a488035 1078 $this->assertEquals('130.00', $trxnAmount);
28de42d1 1079 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
6a488035
TO
1080 }
1081
a1a2a83d 1082 /**
1e52837d 1083 * Function tests that line items, financial records are updated when pay later contribution is received.
6a488035 1084 */
00be9182 1085 public function testCreateUpdateContributionPayLater() {
6a488035
TO
1086 $contribParams = array(
1087 'contact_id' => $this->_individualId,
1088 'receive_date' => '2012-01-01',
1089 'total_amount' => 100.00,
4ab7d517 1090 'financial_type_id' => $this->_financialTypeId,
6a488035 1091 'payment_instrument_id' => 1,
8f39a111 1092 'contribution_status_id' => 2,
1093 'is_pay_later' => 1,
4ab7d517 1094
6a488035 1095 );
4ab7d517 1096 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1097
1098 $newParams = array_merge($contribParams, array(
5896d037
TO
1099 'id' => $contribution['id'],
1100 'contribution_status_id' => 1,
1101 )
c71ae314 1102 );
694769da 1103 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035 1104 $contribution = $contribution['values'][$contribution['id']];
6c6e6187 1105 $this->assertEquals($contribution['contribution_status_id'], '1');
6a488035
TO
1106 $this->_checkFinancialItem($contribution['id'], 'paylater');
1107 $this->_checkFinancialTrxn($contribution, 'payLater');
1108 }
1109
a1a2a83d 1110 /**
eceb18cc 1111 * Function tests that financial records are updated when Payment Instrument is changed.
6a488035 1112 */
00be9182 1113 public function testCreateUpdateContributionPaymentInstrument() {
6a488035
TO
1114 $instrumentId = $this->_addPaymentInstrument();
1115 $contribParams = array(
1116 'contact_id' => $this->_individualId,
1117 'total_amount' => 100.00,
4ab7d517 1118 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1119 'payment_instrument_id' => 4,
1120 'contribution_status_id' => 1,
4ab7d517 1121
6a488035 1122 );
4ab7d517 1123 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1124
1125 $newParams = array_merge($contribParams, array(
5896d037
TO
1126 'id' => $contribution['id'],
1127 'payment_instrument_id' => $instrumentId,
1128 )
6a488035 1129 );
694769da 1130 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
fc928539 1131 $this->assertAPISuccess($contribution);
4ecc6d4b 1132 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
6a488035
TO
1133 }
1134
122250ec
SL
1135 /**
1136 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1137 */
1138 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1139 $instrumentId = $this->_addPaymentInstrument();
1140 $contribParams = array(
1141 'contact_id' => $this->_individualId,
1142 'total_amount' => -100.00,
1143 'financial_type_id' => $this->_financialTypeId,
1144 'payment_instrument_id' => 4,
1145 'contribution_status_id' => 1,
1146
1147 );
1148 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1149
1150 $newParams = array_merge($contribParams, array(
1151 'id' => $contribution['id'],
1152 'payment_instrument_id' => $instrumentId,
1153 )
1154 );
1155 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1156 $this->assertAPISuccess($contribution);
1157 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId, array('total_amount' => '-100.00'));
1158 }
1159
a1a2a83d 1160 /**
eceb18cc 1161 * Function tests that financial records are added when Contribution is Refunded.
6a488035 1162 */
00be9182 1163 public function testCreateUpdateContributionRefund() {
797d4c52 1164 $contributionParams = array(
6a488035
TO
1165 'contact_id' => $this->_individualId,
1166 'receive_date' => '2012-01-01',
1167 'total_amount' => 100.00,
4ab7d517 1168 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1169 'payment_instrument_id' => 4,
1170 'contribution_status_id' => 1,
797d4c52 1171 'trxn_id' => 'original_payment',
1172 );
1173 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1174 $newParams = array_merge($contributionParams, array(
1175 'id' => $contribution['id'],
1176 'contribution_status_id' => 'Refunded',
1177 'cancel_date' => '2015-01-01 09:00',
1178 'refund_trxn_id' => 'the refund',
1179 )
1180 );
1181
1182 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1183 $this->_checkFinancialTrxn($contribution, 'refund');
1184 $this->_checkFinancialItem($contribution['id'], 'refund');
1185 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1186 'id' => $contribution['id'],
1187 'return' => 'trxn_id',
1188 )));
1189 }
4ab7d517 1190
52da5b1e 1191 /**
1192 * Refund a contribution for a financial type with a contra account.
1193 *
1194 * CRM-17951 the contra account is a financial account with a relationship to a
1195 * financial type. It is not always configured but should be reflected
1196 * in the financial_trxn & financial_item table if it is.
1197 */
1198 public function testCreateUpdateChargebackContributionDefaultAccount() {
1199 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1200 $this->callAPISuccess('Contribution', 'create', array(
1201 'id' => $contribution['id'],
1202 'contribution_status_id' => 'Chargeback',
1203 ));
1204 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1205
1206 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1207 'contribution_id' => $contribution['id'],
1208 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1209 ));
1210 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1211 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1212 'total_amount' => -100,
1213 'status_id' => 'Chargeback',
1214 'to_financial_account_id' => 6,
1215 ));
1216 }
1217
1218 /**
1219 * Refund a contribution for a financial type with a contra account.
1220 *
1221 * CRM-17951 the contra account is a financial account with a relationship to a
1222 * financial type. It is not always configured but should be reflected
1223 * in the financial_trxn & financial_item table if it is.
1224 */
1225 public function testCreateUpdateChargebackContributionCustomAccount() {
1226 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1227 'name' => 'Chargeback Account',
1228 'is_active' => TRUE,
1229 ));
1230
1231 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1232 'entity_id' => $this->_financialTypeId,
1233 'entity_table' => 'civicrm_financial_type',
1234 'account_relationship' => 'Chargeback Account is',
1235 'financial_account_id' => 'Chargeback Account',
1236 ));
1237
1238 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1239 $this->callAPISuccess('Contribution', 'create', array(
1240 'id' => $contribution['id'],
1241 'contribution_status_id' => 'Chargeback',
1242 ));
1243 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1244
1245 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1246 'contribution_id' => $contribution['id'],
1247 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1248 ));
1249 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1250
1251 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1252 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1253 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1254 }
1255
bf2cf926 1256 /**
1257 * Refund a contribution for a financial type with a contra account.
1258 *
1259 * CRM-17951 the contra account is a financial account with a relationship to a
1260 * financial type. It is not always configured but should be reflected
1261 * in the financial_trxn & financial_item table if it is.
1262 */
1263 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1264 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1265 'name' => 'Refund Account',
1266 'is_active' => TRUE,
1267 ));
1268
1269 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1270 'entity_id' => $this->_financialTypeId,
1271 'entity_table' => 'civicrm_financial_type',
1272 'account_relationship' => 'Credit/Contra Revenue Account is',
1273 'financial_account_id' => 'Refund Account',
1274 ));
1275
1276 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1277 $this->callAPISuccess('Contribution', 'create', array(
1278 'id' => $contribution['id'],
1279 'contribution_status_id' => 'Refunded',
1280 ));
1281
52da5b1e 1282 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
bf2cf926 1283 'contribution_id' => $contribution['id'],
1284 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1285 ));
1286 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1287
1288 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1289 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1290 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
bf2cf926 1291 }
1292
797d4c52 1293 /**
1294 * Function tests that trxn_id is set when passed in.
1295 *
1296 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1297 * when trxn_id is passed in.
1298 */
1299 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1300 $contributionParams = array(
1301 'contact_id' => $this->_individualId,
1302 'receive_date' => '2012-01-01',
1303 'total_amount' => 100.00,
1304 'financial_type_id' => $this->_financialTypeId,
1305 'payment_instrument_id' => 4,
1306 'contribution_status_id' => 1,
1307 'trxn_id' => 'original_payment',
6a488035 1308 );
797d4c52 1309 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1310 $newParams = array_merge($contributionParams, array(
1311 'id' => $contribution['id'],
1312 'contribution_status_id' => 'Refunded',
1313 'cancel_date' => '2015-01-01 09:00',
1314 'trxn_id' => 'the refund',
1315 )
1316 );
1317
1318 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1319 $this->_checkFinancialTrxn($contribution, 'refund');
1320 $this->_checkFinancialItem($contribution['id'], 'refund');
1321 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1322 'id' => $contribution['id'],
1323 'return' => 'trxn_id',
1324 )));
1325 }
1326
1327 /**
1328 * Function tests that trxn_id is set when passed in.
1329 *
1330 * Here we ensure that the civicrm_contribution.trxn_id is set
1331 * when trxn_id is passed in but if refund_trxn_id is different then that
1332 * is kept for the refund transaction.
1333 */
1334 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1335 $contributionParams = array(
1336 'contact_id' => $this->_individualId,
1337 'receive_date' => '2012-01-01',
1338 'total_amount' => 100.00,
1339 'financial_type_id' => $this->_financialTypeId,
1340 'payment_instrument_id' => 4,
1341 'contribution_status_id' => 1,
1342 'trxn_id' => 'original_payment',
1343 );
1344 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1345 $newParams = array_merge($contributionParams, array(
5896d037 1346 'id' => $contribution['id'],
b7990bb6 1347 'contribution_status_id' => 'Refunded',
1348 'cancel_date' => '2015-01-01 09:00',
797d4c52 1349 'trxn_id' => 'cont id',
1350 'refund_trxn_id' => 'the refund',
6a488035
TO
1351 )
1352 );
1353
694769da 1354 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1355 $this->_checkFinancialTrxn($contribution, 'refund');
1356 $this->_checkFinancialItem($contribution['id'], 'refund');
797d4c52 1357 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1358 'id' => $contribution['id'],
1359 'return' => 'trxn_id',
1360 )));
1361 }
1362
1363 /**
1364 * Function tests that refund_trxn_id is set when passed in empty.
1365 *
1366 * Here we ensure that the civicrm_contribution.trxn_id is set
1367 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1368 * is kept for the refund transaction.
1369 */
1370 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1371 $contributionParams = array(
1372 'contact_id' => $this->_individualId,
1373 'receive_date' => '2012-01-01',
1374 'total_amount' => 100.00,
1375 'financial_type_id' => $this->_financialTypeId,
1376 'payment_instrument_id' => 4,
1377 'contribution_status_id' => 1,
1378 'trxn_id' => 'original_payment',
1379 );
1380 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1381 $newParams = array_merge($contributionParams, array(
1382 'id' => $contribution['id'],
1383 'contribution_status_id' => 'Refunded',
1384 'cancel_date' => '2015-01-01 09:00',
1385 'trxn_id' => 'cont id',
1386 'refund_trxn_id' => '',
1387 )
1388 );
1389
1390 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1391 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1392 $this->_checkFinancialItem($contribution['id'], 'refund');
1393 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1394 'id' => $contribution['id'],
1395 'return' => 'trxn_id',
1396 )));
8f39a111 1397 }
c71ae314 1398
a1a2a83d 1399 /**
eceb18cc 1400 * Function tests invalid contribution status change.
c71ae314 1401 */
00be9182 1402 public function testCreateUpdateContributionInValidStatusChange() {
c71ae314
PN
1403 $contribParams = array(
1404 'contact_id' => 1,
1405 'receive_date' => '2012-01-01',
1406 'total_amount' => 100.00,
1407 'financial_type_id' => 1,
1408 'payment_instrument_id' => 1,
1409 'contribution_status_id' => 1,
c71ae314 1410 );
4ab7d517 1411 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
c71ae314 1412 $newParams = array_merge($contribParams, array(
5896d037
TO
1413 'id' => $contribution['id'],
1414 'contribution_status_id' => 2,
c71ae314
PN
1415 )
1416 );
6c6e6187 1417 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1418
6a488035
TO
1419 }
1420
a1a2a83d 1421 /**
eceb18cc 1422 * Function tests that financial records are added when Pending Contribution is Canceled.
6a488035 1423 */
00be9182 1424 public function testCreateUpdateContributionCancelPending() {
6a488035
TO
1425 $contribParams = array(
1426 'contact_id' => $this->_individualId,
1427 'receive_date' => '2012-01-01',
1428 'total_amount' => 100.00,
4ab7d517 1429 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1430 'payment_instrument_id' => 1,
1431 'contribution_status_id' => 2,
c71ae314 1432 'is_pay_later' => 1,
4ab7d517 1433
6a488035 1434 );
4ab7d517 1435 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1436 $newParams = array_merge($contribParams, array(
5896d037
TO
1437 'id' => $contribution['id'],
1438 'contribution_status_id' => 3,
0a8160e8 1439 'cancel_date' => '2012-02-02 09:00',
6a488035
TO
1440 )
1441 );
0a8160e8 1442 //Check if trxn_date is same as cancel_date.
1443 $checkTrxnDate = array(
1444 'trxn_date' => '2012-02-02 09:00:00',
1445 );
694769da 1446 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
0a8160e8 1447 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
6a488035
TO
1448 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1449 }
1450
a1a2a83d 1451 /**
eceb18cc 1452 * Function tests that financial records are added when Financial Type is Changed.
6a488035 1453 */
00be9182 1454 public function testCreateUpdateContributionChangeFinancialType() {
6a488035
TO
1455 $contribParams = array(
1456 'contact_id' => $this->_individualId,
1457 'receive_date' => '2012-01-01',
1458 'total_amount' => 100.00,
1459 'financial_type_id' => 1,
1460 'payment_instrument_id' => 1,
1461 'contribution_status_id' => 1,
4ab7d517 1462
6a488035 1463 );
4ab7d517 1464 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1465 $newParams = array_merge($contribParams, array(
5896d037
TO
1466 'id' => $contribution['id'],
1467 'financial_type_id' => 3,
6a488035
TO
1468 )
1469 );
694769da 1470 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1471 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1472 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1473 }
1474
694769da 1475 /**
1e52837d 1476 * Test that update does not change status id CRM-15105.
694769da 1477 */
00be9182 1478 public function testCreateUpdateWithoutChangingPendingStatus() {
694769da
VU
1479 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1480 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
5896d037 1481 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1482 'id' => $contribution['id'],
1483 'api.contribution.delete' => 1,
1484 ));
694769da
VU
1485 $this->assertEquals(2, $contribution['contribution_status_id']);
1486 }
a1a2a83d
TO
1487
1488 /**
28de42d1
EM
1489 * Test Updating a Contribution.
1490 *
a1a2a83d
TO
1491 * CHANGE: we require the API to do an incremental update
1492 */
00be9182 1493 public function testCreateUpdateContribution() {
6a488035 1494
78ab0ca4 1495 $contributionID = $this->contributionCreate(array(
1496 'contact_id' => $this->_individualId,
1497 'trxn_id' => 212355,
1498 'financial_type_id' => $this->_financialTypeId,
1499 'invoice_id' => 'old_invoice',
1500 ));
6a488035
TO
1501 $old_params = array(
1502 'contribution_id' => $contributionID,
6a488035 1503 );
4ab7d517 1504 $original = $this->callAPISuccess('contribution', 'get', $old_params);
2bfae985 1505 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1506 //set up list of old params, verify
1507
1508 //This should not be required on update:
1509 $old_contact_id = $original['values'][$contributionID]['contact_id'];
7d543448 1510 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
6a488035
TO
1511 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1512 $old_source = $original['values'][$contributionID]['contribution_source'];
1513
6a488035
TO
1514 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1515 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1516
1517 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1518 $this->assertEquals($old_contact_id, $this->_individualId);
1519 $this->assertEquals($old_fee_amount, 5.00);
1520 $this->assertEquals($old_source, 'SSF');
1521 $this->assertEquals($old_trxn_id, 212355);
78ab0ca4 1522 $this->assertEquals($old_invoice_id, 'old_invoice');
6a488035
TO
1523 $params = array(
1524 'id' => $contributionID,
1525 'contact_id' => $this->_individualId,
1526 'total_amount' => 110.00,
4ab7d517 1527 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1528 'non_deductible_amount' => 10.00,
1529 'net_amount' => 100.00,
1530 'contribution_status_id' => 1,
ef32adff 1531 'note' => 'Donating for Noble Cause',
4ab7d517 1532
6a488035
TO
1533 );
1534
4ab7d517 1535 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1536
1537 $new_params = array(
1538 'contribution_id' => $contribution['id'],
4ab7d517 1539
6a488035 1540 );
ef32adff 1541 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1542
1543 $this->assertEquals($contribution['contact_id'], $this->_individualId);
1544 $this->assertEquals($contribution['total_amount'], 110.00);
1545 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
1546 $this->assertEquals($contribution['financial_type'], 'Donation');
1547 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1548 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
1549 $this->assertEquals($contribution['fee_amount'], $old_fee_amount);
1550 $this->assertEquals($contribution['net_amount'], 100.00);
1551 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1552 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1553 $this->assertEquals($contribution['contribution_source'], $old_source);
1554 $this->assertEquals($contribution['contribution_status'], 'Completed');
6a488035
TO
1555 $params = array(
1556 'contribution_id' => $contributionID,
4ab7d517 1557
6a488035 1558 );
4ab7d517 1559 $result = $this->callAPISuccess('contribution', 'delete', $params);
22f80e87 1560 $this->assertAPISuccess($result);
6a488035
TO
1561 }
1562
a1a2a83d
TO
1563 /**
1564 * Attempt (but fail) to delete a contribution without parameters.
1565 */
00be9182 1566 public function testDeleteEmptyParamsContribution() {
4ab7d517 1567 $params = array();
858d0bf8 1568 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1569 }
1570
00be9182 1571 public function testDeleteParamsNotArrayContribution() {
6a488035 1572 $params = 'contribution_id= 1';
d0e1eff2 1573 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1574 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1575 }
1576
00be9182 1577 public function testDeleteWrongParamContribution() {
6a488035
TO
1578 $params = array(
1579 'contribution_source' => 'SSF',
4ab7d517 1580
6a488035 1581 );
858d0bf8 1582 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1583 }
1584
00be9182 1585 public function testDeleteContribution() {
78ab0ca4 1586 $contributionID = $this->contributionCreate(array(
1587 'contact_id' => $this->_individualId,
1588 'financial_type_id' => $this->_financialTypeId,
1589 ));
6a488035
TO
1590 $params = array(
1591 'id' => $contributionID,
6a488035 1592 );
4ab7d517 1593 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1594 }
1595
1596 /**
d177a2a6 1597 * Test civicrm_contribution_search with empty params.
1e52837d 1598 *
d177a2a6 1599 * All available contributions expected.
6a488035 1600 */
00be9182 1601 public function testSearchEmptyParams() {
4ab7d517 1602 $params = array();
6a488035
TO
1603
1604 $p = array(
1605 'contact_id' => $this->_individualId,
1606 'receive_date' => date('Ymd'),
1607 'total_amount' => 100.00,
4ab7d517 1608 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1609 'non_deductible_amount' => 10.00,
1610 'fee_amount' => 5.00,
1611 'net_amount' => 95.00,
1612 'trxn_id' => 23456,
1613 'invoice_id' => 78910,
1614 'source' => 'SSF',
1615 'contribution_status_id' => 1,
4ab7d517 1616
6a488035 1617 );
4ab7d517 1618 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1619
4ab7d517 1620 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1621 // We're taking the first element.
1622 $res = $result['values'][$contribution['id']];
1623
2bfae985
EM
1624 $this->assertEquals($p['contact_id'], $res['contact_id']);
1625 $this->assertEquals($p['total_amount'], $res['total_amount']);
5896d037 1626 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1627 $this->assertEquals($p['net_amount'], $res['net_amount']);
1628 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1629 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1630 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1631 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1632 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1633 // contribution_status_id = 1 => Completed
2bfae985 1634 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1635
1636 $this->contributionDelete($contribution['id']);
1637 }
1638
1639 /**
d177a2a6 1640 * Test civicrm_contribution_search. Success expected.
6a488035 1641 */
00be9182 1642 public function testSearch() {
6a488035
TO
1643 $p1 = array(
1644 'contact_id' => $this->_individualId,
1645 'receive_date' => date('Ymd'),
1646 'total_amount' => 100.00,
4ab7d517 1647 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1648 'non_deductible_amount' => 10.00,
1649 'contribution_status_id' => 1,
4ab7d517 1650
6a488035 1651 );
4ab7d517 1652 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1653
1654 $p2 = array(
1655 'contact_id' => $this->_individualId,
1656 'receive_date' => date('Ymd'),
1657 'total_amount' => 200.00,
4ab7d517 1658 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1659 'non_deductible_amount' => 20.00,
1660 'trxn_id' => 5454565,
1661 'invoice_id' => 1212124,
1662 'fee_amount' => 50.00,
1663 'net_amount' => 60.00,
1664 'contribution_status_id' => 2,
4ab7d517 1665
6a488035 1666 );
2f45e1c2 1667 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1668
1669 $params = array(
1670 'contribution_id' => $contribution2['id'],
4ab7d517 1671
6a488035 1672 );
2f45e1c2 1673 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1674 $res = $result['values'][$contribution2['id']];
1675
2bfae985
EM
1676 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1677 $this->assertEquals($p2['total_amount'], $res['total_amount']);
5896d037 1678 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1679 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1680 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1681 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1682 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1683 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
6a488035 1684 // contribution_status_id = 2 => Pending
2bfae985 1685 $this->assertEquals('Pending', $res['contribution_status']);
6a488035
TO
1686
1687 $this->contributionDelete($contribution1['id']);
1688 $this->contributionDelete($contribution2['id']);
1689 }
2f45e1c2 1690
0efa8efe 1691 /**
eceb18cc 1692 * Test completing a transaction via the API.
0efa8efe 1693 *
1694 * Note that we are creating a logged in user because email goes out from
1695 * that person
1696 */
00be9182 1697 public function testCompleteTransaction() {
5896d037 1698 $mut = new CiviMailUtils($this, TRUE);
ec7e3954 1699 $this->swapMessageTemplateForTestTemplate();
0efa8efe 1700 $this->createLoggedInUser();
6c6e6187
TO
1701 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1702 $contribution = $this->callAPISuccess('contribution', 'create', $params);
66d3f9be 1703 $this->callAPISuccess('contribution', 'completetransaction', array(
0efa8efe 1704 'id' => $contribution['id'],
66d3f9be 1705 ));
cc7b912f 1706 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
d5580ed4 1707 $this->assertEquals('SSF', $contribution['contribution_source']);
cc7b912f 1708 $this->assertEquals('Completed', $contribution['contribution_status']);
1709 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
0efa8efe 1710 $mut->checkMailLog(array(
ec7e3954
E
1711 'email:::anthony_anderson@civicrm.org',
1712 'is_monetary:::1',
1713 'amount:::100.00',
1714 'currency:::USD',
1715 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
76e8d9c4 1716 "receipt_date:::\n",
ec7e3954
E
1717 'contributeMode:::notify',
1718 'title:::Contribution',
1719 'displayName:::Mr. Anthony Anderson II',
0efa8efe 1720 ));
46fa5206 1721 $mut->stop();
ec7e3954 1722 $this->revertTemplateToReservedTemplate();
46fa5206
EM
1723 }
1724
e05d2e11 1725 /**
1726 * Test to ensure mail is sent on chosing pay later
1727 */
1728 public function testpayLater() {
1729 $mut = new CiviMailUtils($this, TRUE);
1730 $this->swapMessageTemplateForTestTemplate();
1731 $this->createLoggedInUser();
1732
1733 // create contribution page first
1734 $contributionPageParams = array(
1735 'title' => 'Help Support CiviCRM!',
1736 'financial_type_id' => 1,
1737 'is_monetary' => TRUE,
1738 'is_pay_later' => 1,
1739 'is_quick_config' => TRUE,
1740 'pay_later_text' => 'I will send payment by check',
1741 'pay_later_receipt' => 'This is a pay later reciept',
1742 'is_allow_other_amount' => 1,
1743 'min_amount' => 10.00,
1744 'max_amount' => 10000.00,
1745 'goal_amount' => 100000.00,
1746 'is_email_receipt' => 1,
1747 'is_active' => 1,
1748 'amount_block_is_active' => 1,
1749 'currency' => 'USD',
1750 'is_billing_required' => 0,
1751 );
1752 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1753
1754 // submit form values
1755 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1756 $params = array(
1757 'id' => $contributionPageResult['id'],
1758 'contact_id' => $this->_individualId,
1759 'email-5' => 'anthony_anderson@civicrm.org',
1760 'payment_processor_id' => 0,
1761 'amount' => 100.00,
1762 'tax_amount' => '',
1763 'currencyID' => 'USD',
1764 'is_pay_later' => 1,
1765 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1766 'is_quick_config' => 1,
1767 'description' => 'Online Contribution: Help Support CiviCRM!',
1768 'price_set_id' => $priceSet['id'],
1769 );
1770 $this->callAPISuccess('contribution_page', 'submit', $params);
1771
1772 $mut->checkMailLog(array(
1773 'is_pay_later:::1',
1774 'email:::anthony_anderson@civicrm.org',
1775 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1776 'displayName:::Mr. Anthony Anderson II',
1777 'contributionPageId:::' . $contributionPageResult['id'],
1778 'title:::' . $contributionPageParams['title'],
8beee0e8 1779 'amount:::' . $params['amount'],
e05d2e11 1780 ));
1781 $mut->stop();
1782 $this->revertTemplateToReservedTemplate();
1783 }
1784
2a0df9d9 1785 /**
1786 * Test to check whether contact billing address is used when no contribution address
1787 */
1788 public function testBillingAddress() {
1789 $mut = new CiviMailUtils($this, TRUE);
1790 $this->swapMessageTemplateForTestTemplate();
1791 $this->createLoggedInUser();
1792
1793 //Scenario 1: When Contact don't have any address
1794 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1795 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1796 $this->callAPISuccess('contribution', 'completetransaction', array(
1797 'id' => $contribution['id'],
1798 ));
1799 $mut->checkMailLog(array(
1800 'address:::',
1801 ));
1802
1803 // Scenario 2: Contribution using address
1804 $address = $this->callAPISuccess('address', 'create', array(
1805 'street_address' => 'contribution billing st',
1806 'location_type_id' => 2,
1807 'contact_id' => $this->_params['contact_id'],
1808 ));
76e8d9c4
E
1809 $params = array_merge($this->_params, array(
1810 'contribution_status_id' => 2,
2a0df9d9 1811 'address_id' => $address['id'],
1812 )
1813 );
1814 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1815 $this->callAPISuccess('contribution', 'completetransaction', array(
1816 'id' => $contribution['id'],
1817 ));
1818 $mut->checkMailLog(array(
1819 'address:::contribution billing st',
1820 ));
1821
1822 // Scenario 3: Contribution wtth no address but contact has a billing address
1823 $this->callAPISuccess('address', 'create', array(
1824 'id' => $address['id'],
1825 'street_address' => 'is billing st',
1826 'contact_id' => $this->_params['contact_id'],
1827 ));
1828 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1829 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1830 $this->callAPISuccess('contribution', 'completetransaction', array(
1831 'id' => $contribution['id'],
1832 ));
1833 $mut->checkMailLog(array(
1834 'address:::is billing st',
1835 ));
1836
1837 $mut->stop();
1838 $this->revertTemplateToReservedTemplate();
1839 }
1840
080a561b 1841 /**
1842 * Test completing a transaction via the API.
1843 *
1844 * Note that we are creating a logged in user because email goes out from
1845 * that person
1846 */
1847 public function testCompleteTransactionFeeAmount() {
1848 $this->createLoggedInUser();
1849 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1850 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1851 $this->callAPISuccess('contribution', 'completetransaction', array(
1852 'id' => $contribution['id'],
1853 'fee_amount' => '.56',
1854 'trxn_id' => '7778888',
1855 ));
1856 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1857 $this->assertEquals('Completed', $contribution['contribution_status']);
1858 $this->assertEquals('7778888', $contribution['trxn_id']);
1859 $this->assertEquals('.56', $contribution['fee_amount']);
1860 $this->assertEquals('99.44', $contribution['net_amount']);
1861 }
1862
effb4d85
SL
1863 /**
1864 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1865 */
1866 public function testCheckTaxAmount() {
1867 $contact = $this->createLoggedInUser();
1868 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1869 'name' => 'Test taxable financial Type',
1870 'is_reserved' => 0,
1871 'is_active' => 1,
1872 ));
1873 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1874 'name' => 'Test Tax financial account ',
1875 'contact_id' => $contact,
1876 'financial_account_type_id' => 2,
1877 'is_tax' => 1,
1878 'tax_rate' => 5.00,
1879 'is_reserved' => 0,
1880 'is_active' => 1,
1881 'is_default' => 0,
1882 ));
1883 $financialTypeId = $financialType['id'];
1884 $financialAccountId = $financialAccount['id'];
1885 $financialAccountParams = array(
1886 'entity_table' => 'civicrm_financial_type',
1887 'entity_id' => $financialTypeId,
1888 'account_relationship' => 10,
1889 'financial_account_id' => $financialAccountId,
1890 );
a76b8bd8 1891 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
effb4d85
SL
1892 $taxRates = CRM_Core_PseudoConstant::getTaxRates();
1893 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1894 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1895 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1896 $this->callAPISuccess('contribution', 'completetransaction', array(
1897 'id' => $contribution['id'],
1898 'trxn_id' => '777788888',
99a4cd32 1899 'fee_amount' => '6.00',
effb4d85 1900 ));
99a4cd32 1901 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
effb4d85 1902 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
99a4cd32 1903 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
f836984d 1904 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
effb4d85
SL
1905 }
1906
d97c96dc 1907 /**
0e6ccb2e 1908 * Test repeat contribution successfully creates line item.
d97c96dc 1909 */
1e52837d 1910 public function testRepeatTransaction() {
0e6ccb2e 1911 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
d97c96dc
EM
1912 $this->callAPISuccess('contribution', 'repeattransaction', array(
1913 'original_contribution_id' => $originalContribution['id'],
1914 'contribution_status_id' => 'Completed',
1915 'trxn_id' => uniqid(),
1916 ));
1917 $lineItemParams = array(
1918 'entity_id' => $originalContribution['id'],
1919 'sequential' => 1,
1920 'return' => array(
1921 'entity_table',
1922 'qty',
1923 'unit_price',
1924 'line_total',
1925 'label',
1926 'financial_type_id',
1927 'deductible_amount',
1928 'price_field_value_id',
1929 'price_field_id',
1e52837d 1930 ),
d97c96dc
EM
1931 );
1932 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1933 'entity_id' => $originalContribution['id'],
1934 )));
1935 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1936 'entity_id' => $originalContribution['id'] + 1,
1937 )));
1938 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1939 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1940 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
f69a9ac3 1941 $this->_checkFinancialRecords(array(
1942 'id' => $originalContribution['id'] + 1,
1943 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
1944 'id' => $originalContribution['payment_processor_id'],
1945 'return' => 'payment_instrument_id',
1946 )),
1947 ), 'online');
d97c96dc
EM
1948 $this->quickCleanUpFinancialEntities();
1949 }
1950
893a550c 1951 /**
0e6ccb2e
K
1952 * Test repeat contribution successfully creates line items (plural).
1953 */
1954 public function testRepeatTransactionLineItems() {
7150b1c8 1955 // CRM-19309
0e6ccb2e
K
1956 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
1957 $this->callAPISuccess('contribution', 'repeattransaction', array(
1958 'original_contribution_id' => $originalContribution['id'],
1959 'contribution_status_id' => 'Completed',
1960 'trxn_id' => uniqid(),
1961 ));
1962
1963 $lineItemParams = array(
1964 'entity_id' => $originalContribution['id'],
1965 'sequential' => 1,
1966 'return' => array(
1967 'entity_table',
1968 'qty',
1969 'unit_price',
1970 'line_total',
1971 'label',
1972 'financial_type_id',
1973 'deductible_amount',
1974 'price_field_value_id',
1975 'price_field_id',
1976 ),
1977 );
1978 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1979 'entity_id' => $originalContribution['id'],
1980 )));
1981 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1982 'entity_id' => $originalContribution['id'] + 1,
1983 )));
1984
1985 // unset id and entity_id for all of them to be able to compare the lineItems:
1986 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1987 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1988 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1989
1990 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
1991 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
1992 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
1993
7150b1c8
K
1994 // CRM-19309 so in future we also want to:
1995 // check that financial_line_items have been created for entity_id 3 and 4;
0e6ccb2e
K
1996
1997 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
1998 $this->quickCleanUpFinancialEntities();
1999 }
2000
2001 /**
2002 * Test repeat contribution successfully creates is_test transaction.
893a550c 2003 */
2004 public function testRepeatTransactionIsTest() {
2005 $this->_params['is_test'] = 1;
0e6ccb2e 2006 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
893a550c 2007
2008 $this->callAPISuccess('contribution', 'repeattransaction', array(
2009 'original_contribution_id' => $originalContribution['id'],
2010 'contribution_status_id' => 'Completed',
2011 'trxn_id' => uniqid(),
2012 ));
2013 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2014 }
2015
d5580ed4 2016 /**
2017 * Test repeat contribution passed in status.
2018 */
2019 public function testRepeatTransactionPassedInStatus() {
0e6ccb2e 2020 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
d5580ed4 2021
2022 $this->callAPISuccess('contribution', 'repeattransaction', array(
2023 'original_contribution_id' => $originalContribution['id'],
2024 'contribution_status_id' => 'Pending',
2025 'trxn_id' => uniqid(),
2026 ));
2027 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2028 }
2029
1eade77d 2030 /**
2031 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2032 */
2033 public function testRepeatTransactionAcceptRecurID() {
2034 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2035 'contact_id' => $this->_individualId,
2036 'installments' => '12',
2037 'frequency_interval' => '1',
2038 'amount' => '100',
2039 'contribution_status_id' => 1,
2040 'start_date' => '2012-01-01 00:00:00',
2041 'currency' => 'USD',
2042 'frequency_unit' => 'month',
2043 'payment_processor_id' => $this->paymentProcessorID,
2044 ));
2045 $this->callAPISuccess('contribution', 'create', array_merge(
2046 $this->_params,
2047 array('contribution_recur_id' => $contributionRecur['id']))
2048 );
2049
2050 $this->callAPISuccess('contribution', 'repeattransaction', array(
2051 'contribution_recur_id' => $contributionRecur['id'],
2052 'contribution_status_id' => 'Completed',
2053 'trxn_id' => uniqid(),
2054 ));
2055
2056 $this->quickCleanUpFinancialEntities();
2057 }
2058
c03f1689
EM
2059 /**
2060 * CRM-16397 test appropriate action if total amount has changed for single line items.
2061 */
2062 public function testRepeatTransactionAlteredAmount() {
2063 $paymentProcessorID = $this->paymentProcessorCreate();
c03f1689
EM
2064 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2065 'contact_id' => $this->_individualId,
2066 'installments' => '12',
2067 'frequency_interval' => '1',
2068 'amount' => '500',
2069 'contribution_status_id' => 1,
2070 'start_date' => '2012-01-01 00:00:00',
2071 'currency' => 'USD',
2072 'frequency_unit' => 'month',
2073 'payment_processor_id' => $paymentProcessorID,
2074 ));
2075 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2076 $this->_params,
2077 array(
2078 'contribution_recur_id' => $contributionRecur['id'],
c03f1689
EM
2079 ))
2080 );
2081
2082 $this->callAPISuccess('contribution', 'repeattransaction', array(
2083 'original_contribution_id' => $originalContribution['id'],
2084 'contribution_status_id' => 'Completed',
2085 'trxn_id' => uniqid(),
2086 'total_amount' => '400',
2087 'fee_amount' => 50,
c03f1689 2088 ));
0e6ccb2e 2089
c03f1689
EM
2090 $lineItemParams = array(
2091 'entity_id' => $originalContribution['id'],
2092 'sequential' => 1,
2093 'return' => array(
2094 'entity_table',
2095 'qty',
2096 'unit_price',
2097 'line_total',
2098 'label',
2099 'financial_type_id',
2100 'deductible_amount',
2101 'price_field_value_id',
2102 'price_field_id',
2103 ),
2104 );
2105 $this->callAPISuccessGetSingle('contribution', array(
2106 'total_amount' => 400,
c03f1689
EM
2107 'fee_amount' => 50,
2108 'net_amount' => 350,
2109 ));
2110 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2111 'entity_id' => $originalContribution['id'],
2112 )));
2113 $expectedLineItem = array_merge(
2114 $lineItem1['values'][0], array(
2115 'line_total' => '400.00',
2116 'unit_price' => '400.00',
542d9e2c
EM
2117 )
2118 );
c03f1689
EM
2119
2120 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2121 'entity_id' => $originalContribution['id'] + 1,
2122 )));
0e6ccb2e 2123
c03f1689
EM
2124 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2125 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2126 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
c02c17df 2127 }
c03f1689 2128
3c49d90c 2129 /**
2130 * CRM-17718 test appropriate action if financial type has changed for single line items.
2131 */
2132 public function testRepeatTransactionPassedInFinancialType() {
2133 $originalContribution = $this->setUpRecurringContribution();
2134
2135 $this->callAPISuccess('contribution', 'repeattransaction', array(
2136 'original_contribution_id' => $originalContribution['id'],
2137 'contribution_status_id' => 'Completed',
2138 'trxn_id' => uniqid(),
2139 'financial_type_id' => 2,
2140 ));
2141 $lineItemParams = array(
2142 'entity_id' => $originalContribution['id'],
2143 'sequential' => 1,
2144 'return' => array(
2145 'entity_table',
2146 'qty',
2147 'unit_price',
2148 'line_total',
2149 'label',
2150 'financial_type_id',
2151 'deductible_amount',
2152 'price_field_value_id',
2153 'price_field_id',
2154 ),
2155 );
2156
2157 $this->callAPISuccessGetSingle('contribution', array(
2158 'total_amount' => 100,
2159 'financial_type_id' => 2,
2160 ));
2161 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2162 'entity_id' => $originalContribution['id'],
2163 )));
2164 $expectedLineItem = array_merge(
2165 $lineItem1['values'][0], array(
2166 'line_total' => '100.00',
2167 'unit_price' => '100.00',
2168 'financial_type_id' => 2,
257ab382 2169 'contribution_type_id' => 2,
3c49d90c 2170 )
2171 );
3c49d90c 2172 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2173 'entity_id' => $originalContribution['id'] + 1,
2174 )));
2175 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2176 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2177 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2178 }
2179
7f4ef731 2180 /**
2181 * CRM-17718 test appropriate action if financial type has changed for single line items.
2182 */
2183 public function testRepeatTransactionUpdatedFinancialType() {
2184 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2185
2186 $this->callAPISuccess('contribution', 'repeattransaction', array(
2187 'contribution_recur_id' => $originalContribution['id'],
2188 'contribution_status_id' => 'Completed',
2189 'trxn_id' => uniqid(),
2190 ));
2191 $lineItemParams = array(
2192 'entity_id' => $originalContribution['id'],
2193 'sequential' => 1,
2194 'return' => array(
2195 'entity_table',
2196 'qty',
2197 'unit_price',
2198 'line_total',
2199 'label',
2200 'financial_type_id',
2201 'deductible_amount',
2202 'price_field_value_id',
2203 'price_field_id',
2204 ),
2205 );
2206
2207 $this->callAPISuccessGetSingle('contribution', array(
2208 'total_amount' => 100,
2209 'financial_type_id' => 2,
2210 ));
2211 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2212 'entity_id' => $originalContribution['id'],
2213 )));
2214 $expectedLineItem = array_merge(
2215 $lineItem1['values'][0], array(
2216 'line_total' => '100.00',
2217 'unit_price' => '100.00',
2218 'financial_type_id' => 2,
257ab382 2219 'contribution_type_id' => 2,
7f4ef731 2220 )
2221 );
2222
2223 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2224 'entity_id' => $originalContribution['id'] + 1,
2225 )));
2226 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2227 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2228 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2229 }
2230
c02c17df 2231 /**
1eade77d 2232 * CRM-16397 test appropriate action if campaign has been passed in.
c02c17df 2233 */
2234 public function testRepeatTransactionPassedInCampaign() {
2235 $paymentProcessorID = $this->paymentProcessorCreate();
2236 $campaignID = $this->campaignCreate();
2237 $campaignID2 = $this->campaignCreate();
2238 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2239 'contact_id' => $this->_individualId,
2240 'installments' => '12',
2241 'frequency_interval' => '1',
2242 'amount' => '100',
2243 'contribution_status_id' => 1,
2244 'start_date' => '2012-01-01 00:00:00',
2245 'currency' => 'USD',
2246 'frequency_unit' => 'month',
2247 'payment_processor_id' => $paymentProcessorID,
2248 ));
2249 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2250 $this->_params,
2251 array(
2252 'contribution_recur_id' => $contributionRecur['id'],
2253 'campaign_id' => $campaignID,
2254 ))
2255 );
2256
2257 $this->callAPISuccess('contribution', 'repeattransaction', array(
2258 'original_contribution_id' => $originalContribution['id'],
2259 'contribution_status_id' => 'Completed',
2260 'trxn_id' => uniqid(),
2261 'campaign_id' => $campaignID2,
2262 ));
2263
2264 $this->callAPISuccessGetSingle('contribution', array(
2265 'total_amount' => 100,
2266 'campaign_id' => $campaignID2,
2267 ));
2268 }
2269
2270 /**
2271 * CRM-17718 campaign stored on contribution recur gets priority.
2272 *
2273 * This reflects the fact we permit people to update them.
2274 */
2275 public function testRepeatTransactionUpdatedCampaign() {
2276 $paymentProcessorID = $this->paymentProcessorCreate();
2277 $campaignID = $this->campaignCreate();
2278 $campaignID2 = $this->campaignCreate();
2279 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2280 'contact_id' => $this->_individualId,
2281 'installments' => '12',
2282 'frequency_interval' => '1',
2283 'amount' => '100',
2284 'contribution_status_id' => 1,
2285 'start_date' => '2012-01-01 00:00:00',
2286 'currency' => 'USD',
2287 'frequency_unit' => 'month',
2288 'payment_processor_id' => $paymentProcessorID,
2289 'campaign_id' => $campaignID,
2290 ));
2291 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2292 $this->_params,
2293 array(
2294 'contribution_recur_id' => $contributionRecur['id'],
2295 'campaign_id' => $campaignID2,
2296 ))
2297 );
2298
2299 $this->callAPISuccess('contribution', 'repeattransaction', array(
2300 'original_contribution_id' => $originalContribution['id'],
2301 'contribution_status_id' => 'Completed',
2302 'trxn_id' => uniqid(),
2303 ));
2304
2305 $this->callAPISuccessGetSingle('contribution', array(
2306 'total_amount' => 100,
2307 'campaign_id' => $campaignID,
2308 ));
c03f1689
EM
2309 }
2310
2936c3b5
EM
2311 /**
2312 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2313 */
2314 public function testCompleteTransactionNetAmountOK() {
2315 $this->createLoggedInUser();
2316 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2317 unset($params['net_amount']);
2318 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2319 $this->callAPISuccess('contribution', 'completetransaction', array(
2320 'id' => $contribution['id'],
2321 ));
2322 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2323 $this->assertEquals('Completed', $contribution['contribution_status']);
2324 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2325 }
2326
46fa5206 2327 /**
1e52837d 2328 * CRM-14151 - Test completing a transaction via the API.
46fa5206 2329 */
00be9182 2330 public function testCompleteTransactionWithReceiptDateSet() {
76e8d9c4 2331 $this->swapMessageTemplateForTestTemplate();
5896d037 2332 $mut = new CiviMailUtils($this, TRUE);
46fa5206 2333 $this->createLoggedInUser();
6c6e6187
TO
2334 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2335 $contribution = $this->callAPISuccess('contribution', 'create', $params);
7104593e 2336 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
6c6e6187 2337 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
46fa5206 2338 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
7104593e 2339 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
46fa5206
EM
2340 $mut->checkMailLog(array(
2341 'Receipt - Contribution',
76e8d9c4 2342 'receipt_date:::' . date('Ymd'),
46fa5206 2343 ));
0efa8efe 2344 $mut->stop();
76e8d9c4 2345 $this->revertTemplateToReservedTemplate();
0efa8efe 2346 }
2347
e2ca457d
KG
2348 /**
2349 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2350 */
2351 public function testCompleteTransactionWithEmailReceiptInput() {
2352 // Create a Contribution Page with is_email_receipt = TRUE
2353 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2354 'receipt_from_name' => 'Mickey Mouse',
2355 'receipt_from_email' => 'mickey@mouse.com',
2356 'title' => "Test Contribution Page",
2357 'financial_type_id' => 1,
2358 'currency' => 'CAD',
2359 'is_monetary' => TRUE,
2360 'is_email_receipt' => TRUE,
2361 ));
2362 $this->_params['contribution_page_id'] = $contributionPage['id'];
2363 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2364 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2365 // Complete the transaction overriding is_email_receipt to = FALSE
2366 $this->callAPISuccess('contribution', 'completetransaction', array(
2367 'id' => $contribution['id'],
2368 'trxn_date' => date('2011-04-09'),
2369 'trxn_id' => 'kazam',
2370 'is_email_receipt' => 0,
2371 ));
2372 // Check if a receipt was issued
2373 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2374 $this->assertEquals('', $receipt_date);
2375 }
3b8c739e 2376
b80f2ad1
E
2377 /**
2378 * Complete the transaction using the template with all the possible.
2379 */
2380 public function testCompleteTransactionWithTestTemplate() {
2381 $this->swapMessageTemplateForTestTemplate();
ec7e3954 2382 $contribution = $this->setUpForCompleteTransaction();
b80f2ad1
E
2383 $this->callAPISuccess('contribution', 'completetransaction', array(
2384 'id' => $contribution['id'],
2385 'trxn_date' => date('2011-04-09'),
2386 'trxn_id' => 'kazam',
2387 ));
2388 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
ec7e3954 2389 $this->mut->checkMailLog(array(
b80f2ad1
E
2390 'email:::anthony_anderson@civicrm.org',
2391 'is_monetary:::1',
2392 'amount:::100.00',
2393 'currency:::USD',
2394 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2395 'receipt_date:::' . date('Ymd'),
2396 'contributeMode:::notify',
2397 'title:::Contribution',
2398 'displayName:::Mr. Anthony Anderson II',
2399 'trxn_id:::kazam',
ec7e3954 2400 'contactID:::' . $this->_params['contact_id'],
b80f2ad1
E
2401 'contributionID:::' . $contribution['id'],
2402 'financialTypeId:::1',
2403 'financialTypeName:::Donation',
2404 ));
ec7e3954 2405 $this->mut->stop();
b80f2ad1
E
2406 $this->revertTemplateToReservedTemplate();
2407 }
2408
ec7e3954
E
2409 /**
2410 * Complete the transaction using the template with all the possible.
2411 */
2412 public function testCompleteTransactionContributionPageFromAddress() {
2413 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2414 'receipt_from_name' => 'Mickey Mouse',
2415 'receipt_from_email' => 'mickey@mouse.com',
2416 'title' => "Test Contribution Page",
2417 'financial_type_id' => 1,
2418 'currency' => 'NZD',
2419 'goal_amount' => 50,
2420 'is_pay_later' => 1,
2421 'is_monetary' => TRUE,
2422 'is_email_receipt' => TRUE,
2423 ));
2424 $this->_params['contribution_page_id'] = $contributionPage['id'];
2425 $contribution = $this->setUpForCompleteTransaction();
2426 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2427 $this->mut->checkMailLog(array(
2428 'mickey@mouse.com',
2429 'Mickey Mouse <',
2430 ));
2431 $this->mut->stop();
2432 }
2433
91259407 2434 /**
2435 * Test completing first transaction in a recurring series.
2436 *
2437 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2438 */
2439 public function testCompleteTransactionSetStatusToInProgress() {
2440 $paymentProcessorID = $this->paymentProcessorCreate();
2441 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2442 'contact_id' => $this->_individualId,
2443 'installments' => '12',
2444 'frequency_interval' => '1',
2445 'amount' => '500',
2446 'contribution_status_id' => 'Pending',
2447 'start_date' => '2012-01-01 00:00:00',
2448 'currency' => 'USD',
2449 'frequency_unit' => 'month',
2450 'payment_processor_id' => $paymentProcessorID,
2451 ));
2452 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2453 $this->_params,
2454 array(
2455 'contribution_recur_id' => $contributionRecur['id'],
2456 'contribution_status_id' => 'Pending',
2457 ))
2458 );
2459 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2460 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2461 'id' => $contributionRecur['id'],
2462 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2463 ));
2464 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2465 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2466 }
2467
294cc627 2468 /**
2469 * Test completing a pledge with the completeTransaction api..
2470 *
2471 * Note that we are creating a logged in user because email goes out from
2472 * that person.
2473 */
2474 public function testCompleteTransactionUpdatePledgePayment() {
9f9fa558 2475 $this->swapMessageTemplateForTestTemplate();
294cc627 2476 $mut = new CiviMailUtils($this, TRUE);
2477 $mut->clearMessages();
2478 $this->createLoggedInUser();
2479 $contributionID = $this->createPendingPledgeContribution();
2480 $this->callAPISuccess('contribution', 'completetransaction', array(
2481 'id' => $contributionID,
2482 'trxn_date' => '1 Feb 2013',
2483 ));
2484 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2485 'id' => $this->_ids['pledge'],
2486 ));
2487 $this->assertEquals('Completed', $pledge['pledge_status']);
2488
2489 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2490 'pledge_id' => $this->_ids['pledge'],
2491 'return' => 'status_id',
2492 ));
2493 $this->assertEquals(1, $status);
2494 $mut->checkMailLog(array(
9f9fa558
EM
2495 'amount:::500.00',
2496 'receive_date:::20130201000000',
76e8d9c4 2497 "receipt_date:::\n",
294cc627 2498 ));
2499 $mut->stop();
9f9fa558 2500 $this->revertTemplateToReservedTemplate();
294cc627 2501 }
2502
0efa8efe 2503 /**
eceb18cc 2504 * Test completing a transaction with an event via the API.
0efa8efe 2505 *
2506 * Note that we are creating a logged in user because email goes out from
2507 * that person
2508 */
00be9182 2509 public function testCompleteTransactionWithParticipantRecord() {
5896d037 2510 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 2511 $mut->clearMessages();
2512 $this->createLoggedInUser();
2513 $contributionID = $this->createPendingParticipantContribution();
66d3f9be 2514 $this->callAPISuccess('contribution', 'completetransaction', array(
5896d037
TO
2515 'id' => $contributionID,
2516 )
0efa8efe 2517 );
5896d037 2518 $participantStatus = $this->callAPISuccessGetValue('participant', array(
92915c55
TO
2519 'id' => $this->_ids['participant'],
2520 'return' => 'participant_status_id',
2521 ));
0efa8efe 2522 $this->assertEquals(1, $participantStatus);
2523 $mut->checkMailLog(array(
2524 'Annual CiviCRM meet',
2525 'Event',
afc59fef 2526 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 2527 ));
66d3f9be
EM
2528 $mut->stop();
2529 }
2530
2531 /**
eceb18cc 2532 * Test membership is renewed when transaction completed.
66d3f9be 2533 */
00be9182 2534 public function testCompleteTransactionMembershipPriceSet() {
66d3f9be 2535 $this->createPriceSetWithPage('membership');
4ff927bc 2536 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2537 'name' => 'Grace',
2538 'return' => 'id')
2539 );
66d3f9be 2540 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
4ff927bc 2541 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2542 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2543 'membership_id' => $this->_ids['membership'],
2544 ));
2545 $this->assertEquals(1, $logs['count']);
2546 $this->assertEquals($stateOfGrace, $membership['status_id']);
0445e085 2547 $contribution = $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
2548 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2549 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
4ff927bc 2550 $this->callAPISuccessGetSingle('LineItem', array(
2551 'entity_id' => $this->_ids['membership'],
2552 'entity_table' => 'civicrm_membership',
2553 ));
2554 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2555 'membership_id' => $this->_ids['membership'],
2556 ));
0445e085 2557 //CRM-19600: Ensure that 'Membership Renewal' activity is created after successful membership regsitration
2558 $activity = $this->callAPISuccess('Activity', 'get', array(
2559 'activity_type_id' => 'Membership Renewal',
2560 'source_record_id' => $contribution['id'],
2561 ));
2562 $this->assertEquals(1, $activity['count']);
4ff927bc 2563 $this->assertEquals(2, $logs['count']);
2564 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
66d3f9be
EM
2565 $this->cleanUpAfterPriceSets();
2566 }
2567
0f07bb06 2568 /**
2569 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2570 */
2571 public function testPendingToCompleteContribution() {
2572 $contributionPage = $this->createPriceSetWithPage('membership');
2573 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2574 'name' => 'Grace',
2575 'return' => 'id')
2576 );
2577 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2578 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2579
2580 // change pending contribution to completed
2581 $form = new CRM_Contribute_Form_Contribution();
2582 $error = FALSE;
2583 $form->_params = array(
2584 'id' => $this->_ids['contribution'],
2585 'total_amount' => 20,
2586 'net_amount' => 20,
2587 'fee_amount' => 0,
2588 'financial_type_id' => 1,
2589 'receive_date' => '04/21/2015',
2590 'receive_date_time' => '11:27PM',
2591 'contact_id' => $this->_individualId,
2592 'contribution_status_id' => 1,
2593 'billing_middle_name' => '',
2594 'billing_last_name' => 'Adams',
2595 'billing_street_address-5' => '790L Lincoln St S',
2596 'billing_city-5' => 'Maryknoll',
2597 'billing_state_province_id-5' => 1031,
2598 'billing_postal_code-5' => 10545,
2599 'billing_country_id-5' => 1228,
2600 'frequency_interval' => 1,
2601 'frequency_unit' => 'month',
2602 'installments' => '',
2603 'hidden_AdditionalDetail' => 1,
2604 'hidden_Premium' => 1,
2605 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2606 'receipt_date' => '',
2607 'receipt_date_time' => '',
2608 'payment_processor_id' => $this->paymentProcessorID,
2609 'currency' => 'USD',
2610 'contribution_page_id' => $this->_ids['contribution_page'],
2611 'contribution_mode' => 'membership',
2612 'source' => 'Membership Signup and Renewal',
2613 );
2614 try {
2615 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
2616 }
2617 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
2618 $error = TRUE;
2619 }
2620 $activity = $this->callAPISuccess('Activity', 'get', array(
2621 'activity_type_id' => 'Membership Renewal',
2622 'source_record_id' => $this->_ids['contribution'],
2623 ));
2624 $this->assertEquals(1, $activity['count']);
2625 }
2626
66d3f9be 2627 /**
eceb18cc 2628 * Test membership is renewed when transaction completed.
66d3f9be 2629 */
00be9182 2630 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
66d3f9be
EM
2631 $this->createPriceSetWithPage('membership');
2632 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
6c6e6187 2633 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
2634 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2635 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2636 $this->cleanUpAfterPriceSets();
2637 }
2638
00be9182 2639 public function cleanUpAfterPriceSets() {
1cf3c2b1 2640 $this->quickCleanUpFinancialEntities();
66d3f9be 2641 $this->contactDelete($this->_ids['contact']);
66d3f9be
EM
2642 }
2643
66d3f9be 2644 /**
eceb18cc 2645 * Set up a pending transaction with a specific price field id.
1e52837d 2646 *
100fef9d 2647 * @param int $priceFieldValueID
66d3f9be 2648 */
5896d037 2649 public function setUpPendingContribution($priceFieldValueID) {
66d3f9be
EM
2650 $contactID = $this->individualCreate();
2651 $membership = $this->callAPISuccess('membership', 'create', array(
2652 'contact_id' => $contactID,
2653 'membership_type_id' => $this->_ids['membership_type'],
2654 'start_date' => 'yesterday - 1 year',
2655 'end_date' => 'yesterday',
4ff927bc 2656 'join_date' => 'yesterday - 1 year',
66d3f9be
EM
2657 ));
2658 $contribution = $this->callAPISuccess('contribution', 'create', array(
2659 'domain_id' => 1,
2660 'contact_id' => $contactID,
2661 'receive_date' => date('Ymd'),
0f07bb06 2662 'total_amount' => 20.00,
66d3f9be
EM
2663 'financial_type_id' => 1,
2664 'payment_instrument_id' => 'Credit Card',
2665 'non_deductible_amount' => 10.00,
2666 'trxn_id' => 'jdhfi88',
2667 'invoice_id' => 'djfhiewuyr',
2668 'source' => 'SSF',
2669 'contribution_status_id' => 2,
2670 'contribution_page_id' => $this->_ids['contribution_page'],
2671 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2672 ));
2673
2674 $this->callAPISuccess('line_item', 'create', array(
2675 'entity_id' => $contribution['id'],
2676 'entity_table' => 'civicrm_contribution',
1274acef 2677 'contribution_id' => $contribution['id'],
66d3f9be
EM
2678 'price_field_id' => $this->_ids['price_field'][0],
2679 'qty' => 1,
2680 'unit_price' => 20,
2681 'line_total' => 20,
2682 'financial_type_id' => 1,
2683 'price_field_value_id' => $priceFieldValueID,
2684 ));
2685 $this->_ids['contact'] = $contactID;
2686 $this->_ids['contribution'] = $contribution['id'];
2687 $this->_ids['membership'] = $membership['id'];
0efa8efe 2688 }
2689
2f45e1c2 2690 /**
eceb18cc 2691 * Test sending a mail via the API.
6a488035 2692 */
00be9182 2693 public function testSendMail() {
5896d037 2694 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 2695 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
858d0bf8 2696 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
2697 'id' => $contribution['id'],
2698 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
2699 )
2700 );
6a488035
TO
2701 $mut->checkMailLog(array(
2702 '$ 100.00',
2703 'Contribution Information',
2704 'Please print this confirmation for your records',
2705 ), array(
21dfd5f5 2706 'Event',
6a488035
TO
2707 )
2708 );
128d44a1 2709
2710 $this->checkCreditCardDetails($mut, $contribution['id']);
6a488035
TO
2711 $mut->stop();
2712 }
2713
128d44a1 2714 /**
2715 * Check credit card details in sent mail via API
2716 *
2717 * @param $mut obj CiviMailUtils instance
2718 * @param int $contributionID Contribution ID
2719 *
2720 */
2721 public function checkCreditCardDetails($mut, $contributionID) {
2722 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2723 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2724 'id' => $contributionID,
2725 'receipt_from_email' => 'api@civicrm.org',
2726 'payment_processor_id' => $this->paymentProcessorID,
2727 )
2728 );
2729 $mut->checkMailLog(array(
2730 'Credit Card Information', // credit card header
2731 'Billing Name and Address', // billing header
2732 'anthony_anderson@civicrm.org', // billing name
2733 ), array(
2734 'Event',
2735 )
2736 );
2737 }
2738
2f45e1c2 2739 /**
eceb18cc 2740 * Test sending a mail via the API.
6a488035 2741 */
00be9182 2742 public function testSendMailEvent() {
5896d037 2743 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 2744 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2f45e1c2 2745 $event = $this->eventCreate(array(
6a488035
TO
2746 'is_email_confirm' => 1,
2747 'confirm_from_email' => 'test@civicrm.org',
2748 ));
2749 $this->_eventID = $event['id'];
2750 $participantParams = array(
2751 'contact_id' => $this->_individualId,
2752 'event_id' => $this->_eventID,
2753 'status_id' => 1,
2754 'role_id' => 1,
2755 // to ensure it matches later on
2756 'register_date' => '2007-07-21 00:00:00',
2757 'source' => 'Online Event Registration: API Testing',
4ab7d517 2758
6a488035 2759 );
2f45e1c2 2760 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2761 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
2762 'participant_id' => $participant['id'],
2763 'contribution_id' => $contribution['id'],
2f45e1c2 2764 ));
66d3f9be 2765 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
2766 'id' => $contribution['id'],
2767 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
2768 )
2769 );
2770
6a488035
TO
2771 $mut->checkMailLog(array(
2772 'Annual CiviCRM meet',
2773 'Event',
2774 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
6c6e6187 2775 ), array()
6a488035
TO
2776 );
2777 $mut->stop();
2778 }
2779
4302618d 2780 /**
1e52837d
EM
2781 * This function does a GET & compares the result against the $params.
2782 *
2783 * Use as a double check on Creates.
2784 *
2785 * @param array $params
2786 * @param int $id
67f947ac 2787 * @param bool $delete
6c6e6187 2788 */
1e52837d 2789 public function contributionGetnCheck($params, $id, $delete = TRUE) {
6a488035 2790
4ab7d517 2791 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 2792 'id' => $id,
4ab7d517 2793
5896d037 2794 ));
6a488035
TO
2795
2796 if ($delete) {
4ab7d517 2797 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 2798 }
2bfae985 2799 $this->assertAPISuccess($contribution, 0);
6a488035
TO
2800 $values = $contribution['values'][$contribution['id']];
2801 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2802 // this is not returned in id format
2803 unset($params['payment_instrument_id']);
2804 $params['contribution_source'] = $params['source'];
2805 unset($params['source']);
2806 foreach ($params as $key => $value) {
22f80e87 2807 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
6a488035
TO
2808 }
2809 }
2810
294cc627 2811 /**
2812 * Create a pending contribution & linked pending pledge record.
2813 */
2814 public function createPendingPledgeContribution() {
2815
2816 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
2817 $this->_ids['pledge'] = $pledgeID;
2818 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
2819 'contribution_status_id' => 'Pending',
2820 'total_amount' => 500,
2821 ))
2822 );
2823 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
2824 'options' => array('limit' => 1),
2825 'return' => 'id',
2826 ));
2827 $this->callAPISuccess('PledgePayment', 'create', array(
2828 'id' => $paymentID,
2829 'contribution_id' =>
2830 $contribution['id'],
2831 'status_id' => 'Pending',
2832 'scheduled_amount' => 500,
2833 ));
2834
2835 return $contribution['id'];
2836 }
2837
0efa8efe 2838 /**
1e52837d 2839 * Create a pending contribution & linked pending participant record (along with an event).
0efa8efe 2840 */
5896d037 2841 public function createPendingParticipantContribution() {
6c6e6187 2842 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
0efa8efe 2843 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
5896d037 2844 $this->_ids['participant'] = $participantID;
0efa8efe 2845 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
6c6e6187 2846 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 2847 $this->callAPISuccess('participant_payment', 'create', array(
92915c55
TO
2848 'contribution_id' => $contribution['id'],
2849 'participant_id' => $participantID,
2850 ));
858d0bf8 2851 $this->callAPISuccess('line_item', 'get', array(
0efa8efe 2852 'entity_id' => $contribution['id'],
2853 'entity_table' => 'civicrm_contribution',
2854 'api.line_item.create' => array(
2855 'entity_id' => $participantID,
2856 'entity_table' => 'civicrm_participant',
2857 ),
2858 ));
2859 return $contribution['id'];
2860 }
2861
4cbe18b8 2862 /**
1e52837d
EM
2863 * Get financial transaction amount.
2864 *
100fef9d 2865 * @param int $contId
4cbe18b8
EM
2866 *
2867 * @return null|string
f4d89200 2868 */
2da40d21 2869 public function _getFinancialTrxnAmount($contId) {
6c6e6187 2870 $query = "SELECT
6a488035
TO
2871 SUM( ft.total_amount ) AS total
2872 FROM civicrm_financial_trxn AS ft
2873 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2874 WHERE ceft.entity_table = 'civicrm_contribution'
2875 AND ceft.entity_id = {$contId}";
2876
6c6e6187
TO
2877 $result = CRM_Core_DAO::singleValueQuery($query);
2878 return $result;
2879 }
6a488035 2880
4cbe18b8 2881 /**
100fef9d 2882 * @param int $contId
4cbe18b8
EM
2883 *
2884 * @return null|string
f4d89200 2885 */
2da40d21 2886 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
2887 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2888 $query = "SELECT
6a488035
TO
2889 SUM(amount)
2890 FROM civicrm_financial_item
2891 WHERE entity_table = 'civicrm_line_item'
2892 AND entity_id = {$lineItem}";
6c6e6187
TO
2893 $result = CRM_Core_DAO::singleValueQuery($query);
2894 return $result;
2895 }
6a488035 2896
4cbe18b8 2897 /**
100fef9d 2898 * @param int $contId
4cbe18b8
EM
2899 * @param $context
2900 */
00be9182 2901 public function _checkFinancialItem($contId, $context) {
6c6e6187
TO
2902 if ($context != 'paylater') {
2903 $params = array(
5896d037
TO
2904 'entity_id' => $contId,
2905 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2906 );
2907 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2908 $entityParams = array(
6a488035
TO
2909 'financial_trxn_id' => $trxn['financial_trxn_id'],
2910 'entity_table' => 'civicrm_financial_item',
6c6e6187
TO
2911 );
2912 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2913 $params = array(
6a488035 2914 'id' => $entityTrxn['entity_id'],
6c6e6187
TO
2915 );
2916 }
2917 if ($context == 'paylater') {
2918 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2919 foreach ($lineItems as $key => $item) {
2920 $params = array(
5896d037
TO
2921 'entity_id' => $key,
2922 'entity_table' => 'civicrm_line_item',
6c6e6187
TO
2923 );
2924 $compareParams = array('status_id' => 1);
2925 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2926 }
2927 }
2928 elseif ($context == 'refund') {
2929 $compareParams = array(
5896d037
TO
2930 'status_id' => 1,
2931 'financial_account_id' => 1,
2932 'amount' => -100,
6c6e6187
TO
2933 );
2934 }
2935 elseif ($context == 'cancelPending') {
2936 $compareParams = array(
5896d037
TO
2937 'status_id' => 3,
2938 'financial_account_id' => 1,
2939 'amount' => -100,
6c6e6187
TO
2940 );
2941 }
2942 elseif ($context == 'changeFinancial') {
2943 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2944 $params = array(
5896d037
TO
2945 'entity_id' => $lineKey,
2946 'amount' => -100,
6c6e6187
TO
2947 );
2948 $compareParams = array(
5896d037 2949 'financial_account_id' => 1,
6c6e6187
TO
2950 );
2951 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2952 $params = array(
5896d037
TO
2953 'financial_account_id' => 3,
2954 'entity_id' => $lineKey,
6c6e6187
TO
2955 );
2956 $compareParams = array(
5896d037 2957 'amount' => 100,
6c6e6187
TO
2958 );
2959 }
2960 if ($context != 'paylater') {
2961 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2962 }
2963 }
6a488035 2964
4cbe18b8 2965 /**
52da5b1e 2966 * Check financial transaction.
2967 *
2968 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
2969 *
4ff927bc 2970 * @param array $contribution
2971 * @param string $context
100fef9d 2972 * @param int $instrumentId
52da5b1e 2973 * @param array $extraParams
4cbe18b8 2974 */
797d4c52 2975 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
6c6e6187 2976 $trxnParams = array(
5896d037
TO
2977 'entity_id' => $contribution['id'],
2978 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
2979 );
2980 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2981 $params = array(
5896d037 2982 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
2983 );
2984 if ($context == 'payLater') {
2985 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2986 $compareParams = array(
5896d037
TO
2987 'status_id' => 1,
2988 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
6c6e6187
TO
2989 );
2990 }
2991 elseif ($context == 'refund') {
2992 $compareParams = array(
5896d037
TO
2993 'to_financial_account_id' => 6,
2994 'total_amount' => -100,
2995 'status_id' => 7,
b7990bb6 2996 'trxn_date' => '2015-01-01 09:00:00',
797d4c52 2997 'trxn_id' => 'the refund',
6c6e6187
TO
2998 );
2999 }
3000 elseif ($context == 'cancelPending') {
3001 $compareParams = array(
ed4d0aea 3002 'to_financial_account_id' => 7,
5896d037
TO
3003 'total_amount' => -100,
3004 'status_id' => 3,
6c6e6187
TO
3005 );
3006 }
3007 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
3008 $entityParams = array(
5896d037
TO
3009 'entity_id' => $contribution['id'],
3010 'entity_table' => 'civicrm_contribution',
3011 'amount' => -100,
6c6e6187
TO
3012 );
3013 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3014 $trxnParams1 = array(
6a488035 3015 'id' => $trxn['financial_trxn_id'],
6c6e6187 3016 );
122250ec
SL
3017 if (empty($extraParams)) {
3018 $compareParams = array(
3019 'total_amount' => -100,
3020 'status_id' => 1,
3021 );
3022 }
3023 else {
3024 $compareParams = array(
3025 'total_amount' => 100,
3026 'status_id' => 1,
3027 );
3028 }
6c6e6187
TO
3029 if ($context == 'paymentInstrument') {
3030 $compareParams += array(
5896d037
TO
3031 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
3032 'payment_instrument_id' => 4,
6c6e6187
TO
3033 );
3034 }
3035 else {
3036 $compareParams['to_financial_account_id'] = 12;
3037 }
797d4c52 3038 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
6c6e6187
TO
3039 $compareParams['total_amount'] = 100;
3040 if ($context == 'paymentInstrument') {
3041 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3042 $compareParams['payment_instrument_id'] = $instrumentId;
3043 }
3044 else {
3045 $compareParams['to_financial_account_id'] = 12;
3046 }
3047 }
3048
797d4c52 3049 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
6c6e6187 3050 }
6a488035 3051
4cbe18b8
EM
3052 /**
3053 * @return mixed
3054 */
5896d037 3055 public function _addPaymentInstrument() {
6c6e6187
TO
3056 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3057 $optionParams = array(
5896d037
TO
3058 'option_group_id' => $gId,
3059 'label' => 'Test Card',
3060 'name' => 'Test Card',
3061 'value' => '6',
3062 'weight' => '6',
3063 'is_active' => 1,
6c6e6187
TO
3064 );
3065 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3066 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3067 $financialParams = array(
5896d037
TO
3068 'entity_table' => 'civicrm_option_value',
3069 'entity_id' => $optionValue['id'],
3070 'account_relationship' => $relationTypeId,
3071 'financial_account_id' => 7,
6c6e6187
TO
3072 );
3073 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
3074 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3075 return $optionValue['values'][$optionValue['id']]['value'];
3076 }
6a488035 3077
3c49d90c 3078 /**
3079 * Set up the basic recurring contribution for tests.
3080 *
3081 * @param array $generalParams
3082 * Parameters that can be merged into the recurring AND the contribution.
7f4ef731 3083 *
3084 * @param array $recurParams
3085 * Parameters to merge into the recur only.
3c49d90c 3086 *
3087 * @return array|int
3088 */
7f4ef731 3089 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3c49d90c 3090 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3091 'contact_id' => $this->_individualId,
3092 'installments' => '12',
3093 'frequency_interval' => '1',
3094 'amount' => '100',
3095 'contribution_status_id' => 1,
3096 'start_date' => '2012-01-01 00:00:00',
3097 'currency' => 'USD',
3098 'frequency_unit' => 'month',
3099 'payment_processor_id' => $this->paymentProcessorID,
7f4ef731 3100 ), $generalParams, $recurParams));
3c49d90c 3101 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3102 $this->_params,
3103 array(
3104 'contribution_recur_id' => $contributionRecur['id'],
3105 ), $generalParams)
3106 );
3107 return $originalContribution;
3108 }
3109
893a550c 3110 /**
3111 * Set up a repeat transaction.
3112 *
3113 * @param array $recurParams
3114 *
3115 * @return array
3116 */
0e6ccb2e 3117 protected function setUpRepeatTransaction($recurParams = array(), $flag) {
893a550c 3118 $paymentProcessorID = $this->paymentProcessorCreate();
3119 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3120 'contact_id' => $this->_individualId,
3121 'installments' => '12',
3122 'frequency_interval' => '1',
3123 'amount' => '500',
3124 'contribution_status_id' => 1,
3125 'start_date' => '2012-01-01 00:00:00',
3126 'currency' => 'USD',
3127 'frequency_unit' => 'month',
3128 'payment_processor_id' => $paymentProcessorID,
3129 ), $recurParams));
0e6ccb2e 3130
7150b1c8 3131 $originalContribution = '';
0e6ccb2e 3132 if ($flag == 'multiple') {
7150b1c8 3133 // CRM-19309 create a contribution + also add in line_items (plural):
0e6ccb2e
K
3134 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3135 $this->_params,
3136 array(
3137 'contribution_recur_id' => $contributionRecur['id'],
3138 'skipLineItem' => 1,
3139 'api.line_item.create' => array(
3140 array(
3141 'price_field_id' => 1,
3142 'qty' => 2,
3143 'line_total' => '20',
3144 'unit_price' => '10',
3145 'financial_type_id' => 1,
3146 ),
3147 array(
3148 'price_field_id' => 1,
3149 'qty' => 1,
3150 'line_total' => '80',
3151 'unit_price' => '80',
3152 'financial_type_id' => 2,
3153 ),
3154 ),
3155 )
3156 )
3157 );
3158 }
3159 elseif ($flag == 'single') {
3160 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3161 $this->_params,
3162 array('contribution_recur_id' => $contributionRecur['id']))
3163 );
3164 }
f69a9ac3 3165 $originalContribution['payment_processor_id'] = $paymentProcessorID;
893a550c 3166 return $originalContribution;
3167 }
3168
ec7e3954
E
3169 /**
3170 * Common set up routine.
3171 *
3172 * @return array
3173 */
3174 protected function setUpForCompleteTransaction() {
3175 $this->mut = new CiviMailUtils($this, TRUE);
3176 $this->createLoggedInUser();
3177 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3178 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3179 return $contribution;
3180 }
3181
6a488035 3182}