Merge pull request #12494 from eileenmcnaughton/strtolower
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
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'));
83644f47 125 $financialAccounts = $this->callAPISuccess('FinancialAccount', 'get', array());
126 foreach ($financialAccounts['values'] as $financialAccount) {
127 if ($financialAccount['name'] == 'Test Tax financial account ' || $financialAccount['name'] == 'Test taxable financial Type') {
128 $entityFinancialTypes = $this->callAPISuccess('EntityFinancialAccount', 'get', array(
129 'financial_account_id' => $financialAccount['id'],
130 ));
131 foreach ($entityFinancialTypes['values'] as $entityFinancialType) {
132 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialType['id']));
133 }
134 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
135 }
136 }
6a488035
TO
137 }
138
22f80e87
EM
139 /**
140 * Test Get.
141 */
00be9182 142 public function testGetContribution() {
802c1c41 143 $contributionSettings = $this->enableTaxAndInvoicing();
b07b172b 144 $invoice_prefix = CRM_Contribute_BAO_Contribution::checkContributeSettings('invoice_prefix', TRUE);
6a488035
TO
145 $p = array(
146 'contact_id' => $this->_individualId,
147 'receive_date' => '2010-01-20',
148 'total_amount' => 100.00,
4ab7d517 149 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
150 'non_deductible_amount' => 10.00,
151 'fee_amount' => 5.00,
152 'net_amount' => 95.00,
153 'trxn_id' => 23456,
154 'invoice_id' => 78910,
155 'source' => 'SSF',
156 'contribution_status_id' => 1,
6a488035 157 );
2f45e1c2 158 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035
TO
159
160 $params = array(
161 'contribution_id' => $this->_contribution['id'],
6a488035 162 );
e0e3c51b 163
3af96592 164 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
4ab7d517 165 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 166 $default = NULL;
858d0bf8 167 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 168
3af96592 169 $this->assertEquals(1, $contributions['count']);
170 $contribution = $contributions['values'][$contributions['id']];
171 $this->assertEquals($contribution['contact_id'], $this->_individualId);
22f80e87
EM
172 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
173 // Passing back a string rather than an id seems like an error/cruft.
174 // If it is to be introduced we should discuss.
3af96592 175 $this->assertEquals($contribution['financial_type_id'], 1);
176 $this->assertEquals($contribution['total_amount'], 100.00);
177 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
178 $this->assertEquals($contribution['fee_amount'], 5.00);
179 $this->assertEquals($contribution['net_amount'], 95.00);
180 $this->assertEquals($contribution['trxn_id'], 23456);
181 $this->assertEquals($contribution['invoice_id'], 78910);
802c1c41 182 $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']);
3af96592 183 $this->assertEquals($contribution['contribution_source'], 'SSF');
184 $this->assertEquals($contribution['contribution_status'], 'Completed');
22f80e87 185 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
186 $p['trxn_id'] = '3847';
187 $p['invoice_id'] = '3847';
188
2f45e1c2 189 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
190
22f80e87 191 // Now we have 2 - test getcount.
4ab7d517 192 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035 193 $this->assertEquals(2, $contribution);
22f80e87 194 // Test id only format.
4ab7d517 195 $contribution = $this->callAPISuccess('contribution', 'get', array(
196 'id' => $this->_contribution['id'],
197 'format.only_id' => 1,
198 ));
22f80e87
EM
199 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
200 // Test id only format.
2f45e1c2 201 $contribution = $this->callAPISuccess('contribution', 'get', array(
4ab7d517 202 'id' => $contribution2['id'],
203 'format.only_id' => 1,
204 ));
205 $this->assertEquals($contribution2['id'], $contribution);
22f80e87 206 // Test id as field.
4ab7d517 207 $contribution = $this->callAPISuccess('contribution', 'get', array(
208 'id' => $this->_contribution['id'],
209 ));
2bfae985 210 $this->assertEquals(1, $contribution['count']);
4ab7d517 211
22f80e87 212 // Test get by contact id works.
4ab7d517 213 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 214
2bfae985 215 $this->assertEquals(2, $contribution['count']);
2f45e1c2 216 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 217 'id' => $this->_contribution['id'],
4ab7d517 218 ));
2f45e1c2 219 $this->callAPISuccess('Contribution', 'Delete', array(
6a488035 220 'id' => $contribution2['id'],
4ab7d517 221 ));
6a488035
TO
222 }
223
71d5a412 224 /**
225 * Test that test contributions can be retrieved.
226 */
227 public function testGetTestContribution() {
228 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1)));
229 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
230 }
231
59f9e5a6 232 /**
233 * Test the 'return' param works for all fields.
234 */
235 public function testGetContributionReturnFunctionality() {
236 $params = $this->_params;
237 $params['check_number'] = 'bouncer';
238 $params['payment_instrument_id'] = 'Check';
239 $params['cancel_date'] = 'yesterday';
240 $params['receipt_date'] = 'yesterday';
241 $params['thankyou_date'] = 'yesterday';
242 $params['revenue_recognition_date'] = 'yesterday';
243 $params['amount_level'] = 'Unreasonable';
244 $params['cancel_reason'] = 'You lose sucker';
245 $params['creditnote_id'] = 'sudo rm -rf';
246 $params['tax_amount'] = '1';
247 $address = $this->callAPISuccess('Address', 'create', array(
248 'street_address' => 'Knockturn Alley',
249 'contact_id' => $this->_individualId,
250 'location_type_id' => 'Home',
251 ));
252 $params['address_id'] = $address['id'];
253 $contributionPage = $this->contributionPageCreate();
254 $params['contribution_page_id'] = $contributionPage['id'];
255 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array(
256 'contact_id' => $this->_individualId,
257 'frequency_interval' => 1,
258 'amount' => 5,
259 ));
260 $params['contribution_recur_id'] = $contributionRecur['id'];
261
262 $params['campaign_id'] = $this->campaignCreate();
263
264 $contributionID = $this->contributionCreate($params);
9cad3ff4
CW
265
266 // update contribution with invoice number
267 $params = array_merge($params, array(
268 'id' => $contributionID,
269 'invoice_number' => CRM_Utils_Array::value('invoice_prefix', Civi::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
270 ));
271 $contributionID = $this->contributionCreate($params);
272
59f9e5a6 273 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
274 $this->assertEquals('bouncer', $contribution['check_number']);
275 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
276
277 $fields = CRM_Contribute_BAO_Contribution::fields();
3123273f 278 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
279 // need to specify them.
280 $fields['address_id'] = $fields['contribution_address_id'];
281 $fields['check_number'] = $fields['contribution_check_number'];
282
59f9e5a6 283 $fieldsLockedIn = array(
284 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
285 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
286 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
287 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
288 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
289 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
290 );
291 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
292 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
293 // covered as the test contract now guarantees them in the return array.
294 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
295 foreach ($fields as $fieldName => $fieldSpec) {
296 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
297 $returnField = $fieldName;
298 if ($returnField == 'contribution_contact_id') {
299 $returnField = 'contact_id';
300 }
301 $this->assertTrue((!empty($contribution[$returnField]) || $contribution[$returnField] === "0"), $returnField);
302 }
303 }
304
6c6e6187 305 /**
22f80e87 306 * We need to ensure previous tested behaviour still works as part of the api contract.
6c6e6187 307 */
00be9182 308 public function testGetContributionLegacyBehaviour() {
6a488035
TO
309 $p = array(
310 'contact_id' => $this->_individualId,
311 'receive_date' => '2010-01-20',
312 'total_amount' => 100.00,
4ab7d517 313 'contribution_type_id' => $this->_financialTypeId,
6a488035
TO
314 'non_deductible_amount' => 10.00,
315 'fee_amount' => 5.00,
316 'net_amount' => 95.00,
317 'trxn_id' => 23456,
318 'invoice_id' => 78910,
319 'source' => 'SSF',
320 'contribution_status_id' => 1,
6a488035 321 );
71d5a412 322 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
6a488035
TO
323
324 $params = array(
325 'contribution_id' => $this->_contribution['id'],
6a488035 326 );
5be22f39 327 $contribution = $this->callAPISuccess('contribution', 'get', $params);
4ab7d517 328 $financialParams['id'] = $this->_financialTypeId;
6c6e6187 329 $default = NULL;
858d0bf8 330 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
2f45e1c2 331
6c6e6187 332 $this->assertEquals(1, $contribution['count']);
2bfae985 333 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
4ab7d517 334 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
335 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
2bfae985
EM
336 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
337 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
338 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
339 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
340 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
341 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
342 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
343 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
22f80e87
EM
344
345 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
6a488035
TO
346 $p['trxn_id'] = '3847';
347 $p['invoice_id'] = '3847';
348
2f45e1c2 349 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
6a488035 350
6a488035 351 // now we have 2 - test getcount
4ab7d517 352 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
6a488035
TO
353 $this->assertEquals(2, $contribution);
354 //test id only format
4ab7d517 355 $contribution = $this->callAPISuccess('contribution', 'get', array(
356 'id' => $this->_contribution['id'],
357 'format.only_id' => 1,
358 ));
22f80e87 359 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
6a488035 360 //test id only format
4ab7d517 361 $contribution = $this->callAPISuccess('contribution', 'get', array(
362 'id' => $contribution2['id'],
363 'format.only_id' => 1,
364 ));
6a488035 365 $this->assertEquals($contribution2['id'], $contribution);
2f45e1c2 366 $contribution = $this->callAPISuccess('contribution', 'get', array(
6a488035
TO
367 'id' => $this->_contribution['id'],
368 ));
369 //test id as field
2bfae985 370 $this->assertEquals(1, $contribution['count']);
6a488035
TO
371 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
372 //test get by contact id works
4ab7d517 373 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
6a488035 374
2bfae985 375 $this->assertEquals(2, $contribution['count']);
2f45e1c2 376 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 377 'id' => $this->_contribution['id'],
6a488035 378 ));
2f45e1c2 379 $this->callAPISuccess('Contribution', 'Delete', array(
5896d037 380 'id' => $contribution2['id'],
6a488035
TO
381 ));
382 }
5896d037 383
a1a2a83d
TO
384 /**
385 * Create an contribution_id=FALSE and financial_type_id=Donation.
386 */
00be9182 387 public function testCreateEmptyContributionIDUseDonation() {
6a488035
TO
388 $params = array(
389 'contribution_id' => FALSE,
390 'contact_id' => 1,
391 'total_amount' => 1,
6c6e6187 392 'check_permissions' => FALSE,
6a488035
TO
393 'financial_type_id' => 'Donation',
394 );
858d0bf8 395 $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 396 }
6a488035 397
6a488035 398 /**
1e52837d
EM
399 * Check with complete array + custom field.
400 *
6a488035
TO
401 * Note that the test is written on purpose without any
402 * variables specific to participant so it can be replicated into other entities
403 * and / or moved to the automated test suite
404 */
00be9182 405 public function testCreateWithCustom() {
6a488035
TO
406 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
407
408 $params = $this->_params;
409 $params['custom_' . $ids['custom_field_id']] = "custom string";
410
6c6e6187 411 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
6a488035 412 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
2f45e1c2 413 $check = $this->callAPISuccess($this->_entity, 'get', array(
4ab7d517 414 'return.custom_' . $ids['custom_field_id'] => 1,
415 'id' => $result['id'],
416 ));
6a488035
TO
417 $this->customFieldDelete($ids['custom_field_id']);
418 $this->customGroupDelete($ids['custom_group_id']);
22f80e87 419 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
420 }
421
422 /**
fd786d03
EM
423 * Check with complete array + custom field.
424 *
6a488035
TO
425 * Note that the test is written on purpose without any
426 * variables specific to participant so it can be replicated into other entities
427 * and / or moved to the automated test suite
428 */
00be9182 429 public function testCreateGetFieldsWithCustom() {
5896d037 430 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
6a488035 431 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
5896d037 432 $result = $this->callAPISuccess('Contribution', 'getfields', array());
6a488035
TO
433 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
434 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
435 $this->customFieldDelete($ids['custom_field_id']);
436 $this->customGroupDelete($ids['custom_group_id']);
437 $this->customFieldDelete($idsContact['custom_field_id']);
438 $this->customGroupDelete($idsContact['custom_group_id']);
439 }
440
00be9182 441 public function testCreateContributionNoLineItems() {
6a488035
TO
442
443 $params = array(
444 'contact_id' => $this->_individualId,
445 'receive_date' => '20120511',
446 'total_amount' => 100.00,
5896d037 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 );
458
2f45e1c2 459 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 460 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035
TO
461 'entity_id' => $contribution['id'],
462 'entity_table' => 'civicrm_contribution',
463 'sequential' => 1,
464 ));
465 $this->assertEquals(0, $lineItems['count']);
466 }
5896d037 467
a1a2a83d 468 /**
eceb18cc 469 * Test checks that passing in line items suppresses the create mechanism.
6a488035 470 */
00be9182 471 public function testCreateContributionChainedLineItems() {
6a488035
TO
472 $params = array(
473 'contact_id' => $this->_individualId,
474 'receive_date' => '20120511',
475 'total_amount' => 100.00,
4ab7d517 476 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
477 'payment_instrument_id' => 1,
478 'non_deductible_amount' => 10.00,
479 'fee_amount' => 50.00,
480 'net_amount' => 90.00,
481 'trxn_id' => 12345,
482 'invoice_id' => 67890,
483 'source' => 'SSF',
484 'contribution_status_id' => 1,
6a488035
TO
485 'skipLineItem' => 1,
486 'api.line_item.create' => array(
487 array(
488 'price_field_id' => 1,
489 'qty' => 2,
490 'line_total' => '20',
491 'unit_price' => '10',
492 ),
493 array(
494 'price_field_id' => 1,
495 'qty' => 1,
496 'line_total' => '80',
497 'unit_price' => '80',
498 ),
499 ),
500 );
501
5c49fee0 502 $description = "Create Contribution with Nested Line Items.";
6a488035 503 $subfile = "CreateWithNestedLineItems";
6c6e6187 504 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
2f45e1c2 505
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,
511 ));
512 $this->assertEquals(2, $lineItems['count']);
513 }
514
00be9182 515 public function testCreateContributionOffline() {
6a488035
TO
516 $params = array(
517 'contact_id' => $this->_individualId,
518 'receive_date' => '20120511',
519 'total_amount' => 100.00,
520 'financial_type_id' => 1,
521 'trxn_id' => 12345,
522 'invoice_id' => 67890,
523 'source' => 'SSF',
524 'contribution_status_id' => 1,
6a488035
TO
525 );
526
5be22f39 527 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
528 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
529 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 530 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
531 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
532 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
533 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
534 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
6c6e6187 535 $lineItems = $this->callAPISuccess('line_item', 'get', array(
6a488035 536 'entity_id' => $contribution['id'],
4ede4532 537 'contribution_id' => $contribution['id'],
6a488035
TO
538 'entity_table' => 'civicrm_contribution',
539 'sequential' => 1,
5896d037 540 ));
6a488035
TO
541 $this->assertEquals(1, $lineItems['count']);
542 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 543 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6a488035
TO
544 $this->_checkFinancialRecords($contribution, 'offline');
545 $this->contributionGetnCheck($params, $contribution['id']);
546 }
5896d037 547
f70a6752 548 /**
28de42d1 549 * Test create with valid payment instrument.
6a488035 550 */
00be9182 551 public function testCreateContributionWithPaymentInstrument() {
6a488035 552 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 553 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6c6e6187 554 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 555 'sequential' => 1,
21dfd5f5 556 'id' => $contribution['id'],
53191813 557 ));
6a488035 558 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
559 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
560
5896d037 561 $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
562 'id' => $contribution['id'],
563 'payment_instrument' => 'Credit Card',
564 ));
6c6e6187 565 $contribution = $this->callAPISuccess('contribution', 'get', array(
53191813 566 'sequential' => 1,
21dfd5f5 567 'id' => $contribution['id'],
53191813
CW
568 ));
569 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
570 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
6a488035
TO
571 }
572
00be9182 573 public function testGetContributionByPaymentInstrument() {
6a488035 574 $params = $this->_params + array('payment_instrument' => 'EFT');
53191813 575 $params2 = $this->_params + array('payment_instrument' => 'Cash');
6c6e6187
TO
576 $this->callAPISuccess('contribution', 'create', $params);
577 $this->callAPISuccess('contribution', 'create', $params2);
5896d037 578 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55 579 'sequential' => 1,
7d543448 580 'contribution_payment_instrument' => 'Cash',
92915c55 581 ));
6a488035 582 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
ff977830
EM
583 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
584 $this->assertEquals(1, $contribution['count']);
868e247d 585 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
6a488035 586 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
868e247d 587 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
6a488035 588 $this->assertEquals(1, $contribution['count']);
5896d037 589 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
590 'sequential' => 1,
591 'payment_instrument_id' => 5,
592 ));
6a488035 593 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
594 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
595 $this->assertEquals(1, $contribution['count']);
5896d037 596 $contribution = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
597 'sequential' => 1,
598 'payment_instrument' => 'EFT',
599 ));
6a488035 600 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
53191813
CW
601 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
602 $this->assertEquals(1, $contribution['count']);
5896d037 603 $contribution = $this->callAPISuccess('contribution', 'create', array(
92915c55
TO
604 'id' => $contribution['id'],
605 'payment_instrument' => 'Credit Card',
606 ));
5896d037 607 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
6a488035 608 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
6c6e6187
TO
609 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
610 $this->assertEquals(1, $contribution['count']);
6a488035
TO
611 }
612
b5a37491
EM
613 /**
614 * CRM-16227 introduces invoice_id as a parameter.
615 */
616 public function testGetContributionByInvoice() {
617 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
618 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
619 $this->callAPISuccessGetCount('Contribution', array(), 2);
620 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
621 // The following don't work. They are the format we are trying to introduce but although the form uses this format
622 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
623 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
624 // come out of convertFormValues
625 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
626 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
627 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
628 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
629 // 2);
630 }
631
2b3e31ac 632 /**
633 * Check the credit note retrieval is case insensitive.
634 */
635 public function testGetCreditNoteCaseInsensitive() {
636 $this->contributionCreate(array('contact_id' => $this->_individualId));
637 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId, 'invoice_id' => rand(), 'trxn_id' => rand()));
638 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
639 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
640 }
641
e58a3abb 642 /**
643 * Test retrieval by total_amount works.
644 *
645 * @throws Exception
646 */
647 public function testGetContributionByTotalAmount() {
648 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '5')));
649 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '10')));
650 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
651 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
652 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
653 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
654 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
655 $this->callAPISuccessGetCount('Contribution', array(), 2);
656 }
657
c490a46a 658 /**
eceb18cc 659 * Create test with unique field name on source.
c490a46a 660 */
00be9182 661 public function testCreateContributionSource() {
6a488035
TO
662
663 $params = array(
664 'contact_id' => $this->_individualId,
665 'receive_date' => date('Ymd'),
666 'total_amount' => 100.00,
4ab7d517 667 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
668 'payment_instrument_id' => 1,
669 'non_deductible_amount' => 10.00,
670 'fee_amount' => 50.00,
671 'net_amount' => 90.00,
672 'trxn_id' => 12345,
673 'invoice_id' => 67890,
674 'contribution_source' => 'SSF',
675 'contribution_status_id' => 1,
6a488035
TO
676 );
677
4ab7d517 678 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
679 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
680 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
6a488035 681 }
d5d148ab 682
d424ffde 683 /**
eceb18cc 684 * Create test with unique field name on source.
4750ab01 685 *
686 * @param string $thousandSeparator
687 * punctuation used to refer to thousands.
688 *
689 * @dataProvider getThousandSeparators
d424ffde 690 */
4750ab01 691 public function testCreateDefaultNow($thousandSeparator) {
692 $this->setCurrencySeparators($thousandSeparator);
d5d148ab 693 $params = $this->_params;
4750ab01 694 unset($params['receive_date'], $params['net_amount']);
695
696 $params['total_amount'] = $this->formatMoneyInput(5000.77);
697 $params['fee_amount'] = $this->formatMoneyInput(.77);
25ac4ffa 698 $params['skipCleanMoney'] = FALSE;
d5d148ab
EM
699
700 $contribution = $this->callAPISuccess('contribution', 'create', $params);
701 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
702 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
4750ab01 703 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
704 $this->assertEquals(.77, $contribution['fee_amount']);
705 $this->assertEquals(5000, $contribution['net_amount']);
d5d148ab
EM
706 }
707
325033b9 708 /**
709 * Create test with unique field name on source.
710 */
711 public function testCreateContributionNullOutThankyouDate() {
712
713 $params = $this->_params;
714 $params['thankyou_date'] = 'yesterday';
715
716 $contribution = $this->callAPISuccess('contribution', 'create', $params);
717 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
718 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
719
720 $params['thankyou_date'] = 'null';
721 $contribution = $this->callAPISuccess('contribution', 'create', $params);
722 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
723 }
724
c490a46a 725 /**
55d2c6f1 726 * Create test with unique field name on source.
c490a46a 727 */
55d2c6f1 728 public function testCreateContributionSourceInvalidContact() {
6a488035
TO
729
730 $params = array(
731 'contact_id' => 999,
732 'receive_date' => date('Ymd'),
733 'total_amount' => 100.00,
4ab7d517 734 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
735 'payment_instrument_id' => 1,
736 'non_deductible_amount' => 10.00,
737 'fee_amount' => 50.00,
738 'net_amount' => 90.00,
739 'trxn_id' => 12345,
740 'invoice_id' => 67890,
741 'contribution_source' => 'SSF',
742 'contribution_status_id' => 1,
6a488035
TO
743 );
744
858d0bf8 745 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
6a488035
TO
746 }
747
00be9182 748 public function testCreateContributionSourceInvalidContContact() {
6a488035
TO
749
750 $params = array(
751 'contribution_contact_id' => 999,
752 'receive_date' => date('Ymd'),
753 'total_amount' => 100.00,
4ab7d517 754 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
755 'payment_instrument_id' => 1,
756 'non_deductible_amount' => 10.00,
757 'fee_amount' => 50.00,
758 'net_amount' => 90.00,
759 'trxn_id' => 12345,
760 'invoice_id' => 67890,
761 'contribution_source' => 'SSF',
762 'contribution_status_id' => 1,
6a488035
TO
763 );
764
33139905 765 $this->callAPIFailure('contribution', 'create', $params);
6a488035
TO
766 }
767
858d0bf8 768 /**
442cf836 769 * Test note created correctly.
858d0bf8 770 */
00be9182 771 public function testCreateContributionWithNote() {
5c49fee0 772 $description = "Demonstrates creating contribution with Note Entity.";
5896d037
TO
773 $subfile = "ContributionCreateWithNote";
774 $params = array(
6a488035
TO
775 'contact_id' => $this->_individualId,
776 'receive_date' => '2012-01-01',
777 'total_amount' => 100.00,
4ab7d517 778 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
779 'payment_instrument_id' => 1,
780 'non_deductible_amount' => 10.00,
781 'fee_amount' => 50.00,
782 'net_amount' => 90.00,
783 'trxn_id' => 12345,
784 'invoice_id' => 67890,
785 'source' => 'SSF',
786 'contribution_status_id' => 1,
6a488035
TO
787 'note' => 'my contribution note',
788 );
789
4ab7d517 790 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
5896d037 791 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
792 'entity_table' => 'civicrm_contribution',
793 'entity_id' => $contribution['id'],
794 'sequential' => 1,
795 ));
6a488035 796 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 797 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035
TO
798 }
799
00be9182 800 public function testCreateContributionWithNoteUniqueNameAliases() {
5896d037 801 $params = array(
6a488035
TO
802 'contact_id' => $this->_individualId,
803 'receive_date' => '2012-01-01',
804 'total_amount' => 100.00,
4ab7d517 805 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
806 'payment_instrument_id' => 1,
807 'non_deductible_amount' => 10.00,
808 'fee_amount' => 50.00,
809 'net_amount' => 90.00,
810 'trxn_id' => 12345,
811 'invoice_id' => 67890,
812 'source' => 'SSF',
813 'contribution_status_id' => 1,
6a488035
TO
814 'contribution_note' => 'my contribution note',
815 );
816
4ab7d517 817 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 818 $result = $this->callAPISuccess('note', 'get', array(
92915c55
TO
819 'entity_table' => 'civicrm_contribution',
820 'entity_id' => $contribution['id'],
821 'sequential' => 1,
822 ));
6a488035 823 $this->assertEquals('my contribution note', $result['values'][0]['note']);
4ab7d517 824 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
6a488035 825 }
c490a46a
CW
826
827 /**
55d2c6f1 828 * This is the test for creating soft credits.
c490a46a 829 */
55d2c6f1 830 public function testCreateContributionWithSoftCredit() {
5c49fee0 831 $description = "Demonstrates creating contribution with SoftCredit.";
5896d037
TO
832 $subfile = "ContributionCreateWithSoftCredit";
833 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
834 'display_name' => 'superman',
835 'contact_type' => 'Individual',
836 ));
55d2c6f1 837 $softParams = array(
bcc03b98 838 'contact_id' => $contact2['id'],
839 'amount' => 50,
21dfd5f5 840 'soft_credit_type_id' => 3,
6a488035
TO
841 );
842
55d2c6f1 843 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
4ab7d517 844 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 845 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
a1c68fd2 846
55d2c6f1
EM
847 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
848 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
849 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
6a516bd6
DG
850
851 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
852 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
853 }
854
00be9182 855 public function testCreateContributionWithSoftCreditDefaults() {
5c49fee0 856 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
5896d037
TO
857 $subfile = "ContributionCreateWithSoftCreditDefaults";
858 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
859 'display_name' => 'superman',
860 'contact_type' => 'Individual',
861 ));
6a516bd6 862 $params = $this->_params + array(
442cf836
EM
863 'soft_credit_to' => $contact2['id'],
864 );
6a516bd6 865 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 866 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
867
868 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
869 // Default soft credit amount = contribution.total_amount
870 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
871 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
872
873 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
874 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
875 }
876
6c266de6 877 /**
878 * Test creating contribution with Soft Credit by passing in honor_contact_id.
879 */
00be9182 880 public function testCreateContributionWithHonoreeContact() {
5c49fee0 881 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
5896d037
TO
882 $subfile = "ContributionCreateWithHonoreeContact";
883 $contact2 = $this->callAPISuccess('Contact', 'create', array(
92915c55
TO
884 'display_name' => 'superman',
885 'contact_type' => 'Individual',
886 ));
6a516bd6 887 $params = $this->_params + array(
442cf836
EM
888 'honor_contact_id' => $contact2['id'],
889 );
6a516bd6 890 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
6c6e6187 891 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
6a516bd6
DG
892
893 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
894 // Default soft credit amount = contribution.total_amount
895 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
896 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
6c266de6 897 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
898 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
6a488035 899
4ab7d517 900 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
901 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
6a488035
TO
902 }
903
904 /**
92c99a4a 905 * Test using example code.
6a488035 906 */
00be9182 907 public function testContributionCreateExample() {
6a488035 908 //make sure at least on page exists since there is a truncate in tear down
8be629ac 909 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
3ec6e38d 910 require_once 'api/v3/examples/Contribution/Create.php';
5896d037 911 $result = contribution_create_example();
006d6361 912 $id = $result['id'];
6a488035 913 $expectedResult = contribution_create_expectedresult();
8e342a79 914 $this->checkArrayEquals($expectedResult, $result);
006d6361 915 $this->contributionDelete($id);
6a488035
TO
916 }
917
a1a2a83d 918 /**
f55c5fa8 919 * Function tests that additional financial records are created when fee amount is recorded.
6a488035 920 */
00be9182 921 public function testCreateContributionWithFee() {
6a488035
TO
922 $params = array(
923 'contact_id' => $this->_individualId,
924 'receive_date' => '20120511',
925 'total_amount' => 100.00,
926 'fee_amount' => 50,
927 'financial_type_id' => 1,
928 'trxn_id' => 12345,
929 'invoice_id' => 67890,
930 'source' => 'SSF',
931 'contribution_status_id' => 1,
6a488035
TO
932 );
933
5be22f39 934 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
935 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
936 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
e0e3c51b
EM
937 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
938 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
5896d037 939 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
940 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
941 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
942 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
943 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
e0e3c51b 944
6c6e6187 945 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 946
6a488035
TO
947 'entity_id' => $contribution['id'],
948 'entity_table' => 'civicrm_contribution',
949 'sequential' => 1,
5896d037 950 ));
6a488035
TO
951 $this->assertEquals(1, $lineItems['count']);
952 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
4ede4532 953 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
6c6e6187 954 $lineItems = $this->callAPISuccess('line_item', 'get', array(
4ab7d517 955
5896d037
TO
956 'entity_id' => $contribution['id'],
957 'contribution_id' => $contribution['id'],
958 'entity_table' => 'civicrm_contribution',
959 'sequential' => 1,
6a488035
TO
960 ));
961 $this->assertEquals(1, $lineItems['count']);
962 $this->_checkFinancialRecords($contribution, 'feeAmount');
963 }
964
965
f70a6752 966 /**
442cf836 967 * Function tests that additional financial records are created when online contribution is created.
6a488035 968 */
00be9182 969 public function testCreateContributionOnline() {
858d0bf8 970 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 971 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 972 $this->assertAPISuccess($contributionPage);
6a488035
TO
973 $params = array(
974 'contact_id' => $this->_individualId,
975 'receive_date' => '20120511',
976 'total_amount' => 100.00,
977 'financial_type_id' => 1,
978 'contribution_page_id' => $contributionPage['id'],
16f3bd02 979 'payment_processor' => $this->paymentProcessorID,
6a488035
TO
980 'trxn_id' => 12345,
981 'invoice_id' => 67890,
982 'source' => 'SSF',
983 'contribution_status_id' => 1,
4ab7d517 984
6a488035
TO
985 );
986
5be22f39 987 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
988 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
989 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 990 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
991 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
992 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
993 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
994 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
16f3bd02 995 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
996 'id' => $this->paymentProcessorID,
997 'return' => 'payment_instrument_id',
998 ));
6a488035
TO
999 $this->_checkFinancialRecords($contribution, 'online');
1000 }
1001
f70a6752 1002 /**
442cf836
EM
1003 * Check handling of financial type.
1004 *
100fef9d 1005 * In the interests of removing financial type / contribution type checks from
f70a6752 1006 * legacy format function lets test that the api is doing this for us
1007 */
00be9182 1008 public function testCreateInvalidFinancialType() {
f70a6752 1009 $params = $this->_params;
1010 $params['financial_type_id'] = 99999;
858d0bf8 1011 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
f70a6752 1012 }
1013
4302618d 1014 /**
442cf836
EM
1015 * Check handling of financial type.
1016 *
100fef9d 1017 * In the interests of removing financial type / contribution type checks from
4302618d 1018 * legacy format function lets test that the api is doing this for us
1019 */
00be9182 1020 public function testValidNamedFinancialType() {
4302618d 1021 $params = $this->_params;
1022 $params['financial_type_id'] = 'Donation';
858d0bf8 1023 $this->callAPISuccess($this->_entity, 'create', $params);
4302618d 1024 }
1025
f70a6752 1026 /**
92c99a4a
EM
1027 * Tests that additional financial records are created.
1028 *
1029 * Checks when online contribution with pay later option is created
6a488035 1030 */
00be9182 1031 public function testCreateContributionPayLaterOnline() {
858d0bf8 1032 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
6a488035 1033 $this->_pageParams['is_pay_later'] = 1;
5896d037 1034 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 1035 $this->assertAPISuccess($contributionPage);
6a488035
TO
1036 $params = array(
1037 'contact_id' => $this->_individualId,
1038 'receive_date' => '20120511',
1039 'total_amount' => 100.00,
1040 'financial_type_id' => 1,
1041 'contribution_page_id' => $contributionPage['id'],
1042 'trxn_id' => 12345,
1043 'is_pay_later' => 1,
1044 'invoice_id' => 67890,
1045 'source' => 'SSF',
1046 'contribution_status_id' => 2,
4ab7d517 1047
6a488035
TO
1048 );
1049
4ab7d517 1050 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
2bfae985
EM
1051 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1052 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 1053 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
1054 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1055 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1056 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1057 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
1058 $this->_checkFinancialRecords($contribution, 'payLater');
1059 }
1060
a1a2a83d 1061 /**
1e52837d 1062 * Function tests that additional financial records are created for online contribution with pending option.
6a488035 1063 */
00be9182 1064 public function testCreateContributionPendingOnline() {
8a40179e 1065 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
5896d037 1066 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
fc928539 1067 $this->assertAPISuccess($contributionPage);
6a488035
TO
1068 $params = array(
1069 'contact_id' => $this->_individualId,
1070 'receive_date' => '20120511',
1071 'total_amount' => 100.00,
1072 'financial_type_id' => 1,
1073 'contribution_page_id' => $contributionPage['id'],
1074 'trxn_id' => 12345,
1075 'invoice_id' => 67890,
1076 'source' => 'SSF',
1077 'contribution_status_id' => 2,
6a488035
TO
1078 );
1079
5be22f39 1080 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2bfae985
EM
1081 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1082 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
5896d037 1083 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
2bfae985
EM
1084 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1085 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1086 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1087 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
6a488035
TO
1088 $this->_checkFinancialRecords($contribution, 'pending');
1089 }
1090
e748bf60 1091 /**
92c99a4a 1092 * Test that BAO defaults work.
e748bf60 1093 */
00be9182 1094 public function testCreateBAODefaults() {
e748bf60
EM
1095 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
1096 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
5896d037 1097 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1098 'id' => $contribution['id'],
1099 'api.contribution.delete' => 1,
1100 ));
e748bf60 1101 $this->assertEquals(1, $contribution['contribution_status_id']);
12879069 1102 $this->assertEquals('Check', $contribution['payment_instrument']);
e748bf60
EM
1103 }
1104
a1a2a83d 1105 /**
1e52837d 1106 * Function tests that line items, financial records are updated when contribution amount is changed.
6a488035 1107 */
00be9182 1108 public function testCreateUpdateContributionChangeTotal() {
4ab7d517 1109 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
6c6e6187 1110 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
4ab7d517 1111
6a488035
TO
1112 'entity_id' => $contribution['id'],
1113 'entity_table' => 'civicrm_contribution',
1114 'sequential' => 1,
1115 'return' => 'line_total',
1116 ));
1117 $this->assertEquals('100.00', $lineItems);
1118 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1119 // Financial trxn SUM = 100 + 5 (fee)
1120 $this->assertEquals('105.00', $trxnAmount);
1121 $newParams = array(
4ab7d517 1122
6a488035 1123 'id' => $contribution['id'],
21dfd5f5 1124 'total_amount' => '125',
5896d037 1125 );
694769da 1126 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
4ab7d517 1127
6c6e6187 1128 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
6a488035 1129
5896d037
TO
1130 'entity_id' => $contribution['id'],
1131 'entity_table' => 'civicrm_contribution',
1132 'sequential' => 1,
1133 'return' => 'line_total',
6a488035
TO
1134 ));
1135
1136 $this->assertEquals('125.00', $lineItems);
1137 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
28de42d1
EM
1138
1139 // Financial trxn SUM = 125 + 5 (fee).
6a488035 1140 $this->assertEquals('130.00', $trxnAmount);
28de42d1 1141 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
6a488035
TO
1142 }
1143
a1a2a83d 1144 /**
1e52837d 1145 * Function tests that line items, financial records are updated when pay later contribution is received.
6a488035 1146 */
00be9182 1147 public function testCreateUpdateContributionPayLater() {
6a488035
TO
1148 $contribParams = array(
1149 'contact_id' => $this->_individualId,
1150 'receive_date' => '2012-01-01',
1151 'total_amount' => 100.00,
4ab7d517 1152 'financial_type_id' => $this->_financialTypeId,
6a488035 1153 'payment_instrument_id' => 1,
8f39a111 1154 'contribution_status_id' => 2,
1155 'is_pay_later' => 1,
4ab7d517 1156
6a488035 1157 );
4ab7d517 1158 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1159
1160 $newParams = array_merge($contribParams, array(
5896d037
TO
1161 'id' => $contribution['id'],
1162 'contribution_status_id' => 1,
1163 )
c71ae314 1164 );
694769da 1165 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035 1166 $contribution = $contribution['values'][$contribution['id']];
6c6e6187 1167 $this->assertEquals($contribution['contribution_status_id'], '1');
6a488035
TO
1168 $this->_checkFinancialItem($contribution['id'], 'paylater');
1169 $this->_checkFinancialTrxn($contribution, 'payLater');
1170 }
1171
a1a2a83d 1172 /**
eceb18cc 1173 * Function tests that financial records are updated when Payment Instrument is changed.
6a488035 1174 */
00be9182 1175 public function testCreateUpdateContributionPaymentInstrument() {
6a488035
TO
1176 $instrumentId = $this->_addPaymentInstrument();
1177 $contribParams = array(
1178 'contact_id' => $this->_individualId,
1179 'total_amount' => 100.00,
4ab7d517 1180 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1181 'payment_instrument_id' => 4,
1182 'contribution_status_id' => 1,
4ab7d517 1183
6a488035 1184 );
4ab7d517 1185 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035
TO
1186
1187 $newParams = array_merge($contribParams, array(
5896d037
TO
1188 'id' => $contribution['id'],
1189 'payment_instrument_id' => $instrumentId,
1190 )
6a488035 1191 );
694769da 1192 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
fc928539 1193 $this->assertAPISuccess($contribution);
b0e806fa 1194 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
02a9c0a4 1195
1196 // cleanup - delete created payment instrument
1197 $this->_deletedAddedPaymentInstrument();
6a488035
TO
1198 }
1199
122250ec
SL
1200 /**
1201 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1202 */
1203 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1204 $instrumentId = $this->_addPaymentInstrument();
1205 $contribParams = array(
1206 'contact_id' => $this->_individualId,
1207 'total_amount' => -100.00,
1208 'financial_type_id' => $this->_financialTypeId,
1209 'payment_instrument_id' => 4,
1210 'contribution_status_id' => 1,
1211
1212 );
1213 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1214
1215 $newParams = array_merge($contribParams, array(
1216 'id' => $contribution['id'],
1217 'payment_instrument_id' => $instrumentId,
1218 )
1219 );
1220 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1221 $this->assertAPISuccess($contribution);
b0e806fa 1222 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
02a9c0a4 1223
1224 // cleanup - delete created payment instrument
1225 $this->_deletedAddedPaymentInstrument();
122250ec
SL
1226 }
1227
a1a2a83d 1228 /**
eceb18cc 1229 * Function tests that financial records are added when Contribution is Refunded.
6a488035 1230 */
00be9182 1231 public function testCreateUpdateContributionRefund() {
797d4c52 1232 $contributionParams = array(
6a488035
TO
1233 'contact_id' => $this->_individualId,
1234 'receive_date' => '2012-01-01',
1235 'total_amount' => 100.00,
4ab7d517 1236 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1237 'payment_instrument_id' => 4,
1238 'contribution_status_id' => 1,
797d4c52 1239 'trxn_id' => 'original_payment',
1240 );
1241 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1242 $newParams = array_merge($contributionParams, array(
1243 'id' => $contribution['id'],
1244 'contribution_status_id' => 'Refunded',
1245 'cancel_date' => '2015-01-01 09:00',
1246 'refund_trxn_id' => 'the refund',
1247 )
1248 );
1249
1250 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1251 $this->_checkFinancialTrxn($contribution, 'refund');
1252 $this->_checkFinancialItem($contribution['id'], 'refund');
1253 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1254 'id' => $contribution['id'],
1255 'return' => 'trxn_id',
1256 )));
1257 }
4ab7d517 1258
52da5b1e 1259 /**
1260 * Refund a contribution for a financial type with a contra account.
1261 *
1262 * CRM-17951 the contra account is a financial account with a relationship to a
1263 * financial type. It is not always configured but should be reflected
1264 * in the financial_trxn & financial_item table if it is.
1265 */
1266 public function testCreateUpdateChargebackContributionDefaultAccount() {
1267 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1268 $this->callAPISuccess('Contribution', 'create', array(
1269 'id' => $contribution['id'],
1270 'contribution_status_id' => 'Chargeback',
1271 ));
1272 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1273
1274 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1275 'contribution_id' => $contribution['id'],
1276 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1277 ));
1278 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1279 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1280 'total_amount' => -100,
1281 'status_id' => 'Chargeback',
1282 'to_financial_account_id' => 6,
1283 ));
1284 }
1285
1286 /**
1287 * Refund a contribution for a financial type with a contra account.
1288 *
1289 * CRM-17951 the contra account is a financial account with a relationship to a
1290 * financial type. It is not always configured but should be reflected
1291 * in the financial_trxn & financial_item table if it is.
1292 */
1293 public function testCreateUpdateChargebackContributionCustomAccount() {
1294 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1295 'name' => 'Chargeback Account',
1296 'is_active' => TRUE,
1297 ));
1298
1299 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1300 'entity_id' => $this->_financialTypeId,
1301 'entity_table' => 'civicrm_financial_type',
1302 'account_relationship' => 'Chargeback Account is',
1303 'financial_account_id' => 'Chargeback Account',
1304 ));
1305
1306 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1307 $this->callAPISuccess('Contribution', 'create', array(
1308 'id' => $contribution['id'],
1309 'contribution_status_id' => 'Chargeback',
1310 ));
1311 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1312
1313 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1314 'contribution_id' => $contribution['id'],
1315 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1316 ));
1317 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1318
1319 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1320 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1321 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1322 }
1323
bf2cf926 1324 /**
1325 * Refund a contribution for a financial type with a contra account.
1326 *
1327 * CRM-17951 the contra account is a financial account with a relationship to a
1328 * financial type. It is not always configured but should be reflected
1329 * in the financial_trxn & financial_item table if it is.
1330 */
1331 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1332 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1333 'name' => 'Refund Account',
1334 'is_active' => TRUE,
1335 ));
1336
1337 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1338 'entity_id' => $this->_financialTypeId,
1339 'entity_table' => 'civicrm_financial_type',
1340 'account_relationship' => 'Credit/Contra Revenue Account is',
1341 'financial_account_id' => 'Refund Account',
1342 ));
1343
1344 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1345 $this->callAPISuccess('Contribution', 'create', array(
1346 'id' => $contribution['id'],
1347 'contribution_status_id' => 'Refunded',
1348 ));
1349
52da5b1e 1350 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
bf2cf926 1351 'contribution_id' => $contribution['id'],
1352 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1353 ));
1354 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1355
1356 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1357 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1358 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
bf2cf926 1359 }
1360
797d4c52 1361 /**
1362 * Function tests that trxn_id is set when passed in.
1363 *
1364 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1365 * when trxn_id is passed in.
1366 */
1367 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1368 $contributionParams = array(
1369 'contact_id' => $this->_individualId,
1370 'receive_date' => '2012-01-01',
1371 'total_amount' => 100.00,
1372 'financial_type_id' => $this->_financialTypeId,
1373 'payment_instrument_id' => 4,
1374 'contribution_status_id' => 1,
1375 'trxn_id' => 'original_payment',
6a488035 1376 );
797d4c52 1377 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1378 $newParams = array_merge($contributionParams, array(
1379 'id' => $contribution['id'],
1380 'contribution_status_id' => 'Refunded',
1381 'cancel_date' => '2015-01-01 09:00',
1382 'trxn_id' => 'the refund',
1383 )
1384 );
1385
1386 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1387 $this->_checkFinancialTrxn($contribution, 'refund');
1388 $this->_checkFinancialItem($contribution['id'], 'refund');
1389 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1390 'id' => $contribution['id'],
1391 'return' => 'trxn_id',
1392 )));
1393 }
1394
1395 /**
1396 * Function tests that trxn_id is set when passed in.
1397 *
1398 * Here we ensure that the civicrm_contribution.trxn_id is set
1399 * when trxn_id is passed in but if refund_trxn_id is different then that
1400 * is kept for the refund transaction.
1401 */
1402 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1403 $contributionParams = array(
1404 'contact_id' => $this->_individualId,
1405 'receive_date' => '2012-01-01',
1406 'total_amount' => 100.00,
1407 'financial_type_id' => $this->_financialTypeId,
1408 'payment_instrument_id' => 4,
1409 'contribution_status_id' => 1,
1410 'trxn_id' => 'original_payment',
1411 );
1412 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1413 $newParams = array_merge($contributionParams, array(
5896d037 1414 'id' => $contribution['id'],
b7990bb6 1415 'contribution_status_id' => 'Refunded',
1416 'cancel_date' => '2015-01-01 09:00',
797d4c52 1417 'trxn_id' => 'cont id',
1418 'refund_trxn_id' => 'the refund',
6a488035
TO
1419 )
1420 );
1421
694769da 1422 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1423 $this->_checkFinancialTrxn($contribution, 'refund');
1424 $this->_checkFinancialItem($contribution['id'], 'refund');
797d4c52 1425 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1426 'id' => $contribution['id'],
1427 'return' => 'trxn_id',
1428 )));
1429 }
1430
1431 /**
1432 * Function tests that refund_trxn_id is set when passed in empty.
1433 *
1434 * Here we ensure that the civicrm_contribution.trxn_id is set
1435 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1436 * is kept for the refund transaction.
1437 */
1438 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1439 $contributionParams = array(
1440 'contact_id' => $this->_individualId,
1441 'receive_date' => '2012-01-01',
1442 'total_amount' => 100.00,
1443 'financial_type_id' => $this->_financialTypeId,
1444 'payment_instrument_id' => 4,
1445 'contribution_status_id' => 1,
1446 'trxn_id' => 'original_payment',
1447 );
1448 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1449 $newParams = array_merge($contributionParams, array(
1450 'id' => $contribution['id'],
1451 'contribution_status_id' => 'Refunded',
1452 'cancel_date' => '2015-01-01 09:00',
1453 'trxn_id' => 'cont id',
1454 'refund_trxn_id' => '',
1455 )
1456 );
1457
1458 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1459 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1460 $this->_checkFinancialItem($contribution['id'], 'refund');
1461 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1462 'id' => $contribution['id'],
1463 'return' => 'trxn_id',
1464 )));
8f39a111 1465 }
c71ae314 1466
a1a2a83d 1467 /**
eceb18cc 1468 * Function tests invalid contribution status change.
c71ae314 1469 */
00be9182 1470 public function testCreateUpdateContributionInValidStatusChange() {
c71ae314
PN
1471 $contribParams = array(
1472 'contact_id' => 1,
1473 'receive_date' => '2012-01-01',
1474 'total_amount' => 100.00,
1475 'financial_type_id' => 1,
1476 'payment_instrument_id' => 1,
1477 'contribution_status_id' => 1,
c71ae314 1478 );
4ab7d517 1479 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
c71ae314 1480 $newParams = array_merge($contribParams, array(
5896d037
TO
1481 'id' => $contribution['id'],
1482 'contribution_status_id' => 2,
c71ae314
PN
1483 )
1484 );
6c6e6187 1485 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
c71ae314 1486
6a488035
TO
1487 }
1488
a1a2a83d 1489 /**
eceb18cc 1490 * Function tests that financial records are added when Pending Contribution is Canceled.
6a488035 1491 */
00be9182 1492 public function testCreateUpdateContributionCancelPending() {
6a488035
TO
1493 $contribParams = array(
1494 'contact_id' => $this->_individualId,
1495 'receive_date' => '2012-01-01',
1496 'total_amount' => 100.00,
4ab7d517 1497 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1498 'payment_instrument_id' => 1,
1499 'contribution_status_id' => 2,
c71ae314 1500 'is_pay_later' => 1,
4ab7d517 1501
6a488035 1502 );
4ab7d517 1503 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1504 $newParams = array_merge($contribParams, array(
5896d037
TO
1505 'id' => $contribution['id'],
1506 'contribution_status_id' => 3,
0a8160e8 1507 'cancel_date' => '2012-02-02 09:00',
6a488035
TO
1508 )
1509 );
0a8160e8 1510 //Check if trxn_date is same as cancel_date.
1511 $checkTrxnDate = array(
1512 'trxn_date' => '2012-02-02 09:00:00',
1513 );
694769da 1514 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
0a8160e8 1515 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
6a488035
TO
1516 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1517 }
1518
a1a2a83d 1519 /**
eceb18cc 1520 * Function tests that financial records are added when Financial Type is Changed.
6a488035 1521 */
00be9182 1522 public function testCreateUpdateContributionChangeFinancialType() {
6a488035
TO
1523 $contribParams = array(
1524 'contact_id' => $this->_individualId,
1525 'receive_date' => '2012-01-01',
1526 'total_amount' => 100.00,
1527 'financial_type_id' => 1,
1528 'payment_instrument_id' => 1,
1529 'contribution_status_id' => 1,
4ab7d517 1530
6a488035 1531 );
4ab7d517 1532 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
6a488035 1533 $newParams = array_merge($contribParams, array(
5896d037
TO
1534 'id' => $contribution['id'],
1535 'financial_type_id' => 3,
6a488035
TO
1536 )
1537 );
694769da 1538 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
6a488035
TO
1539 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1540 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1541 }
1542
694769da 1543 /**
1e52837d 1544 * Test that update does not change status id CRM-15105.
694769da 1545 */
00be9182 1546 public function testCreateUpdateWithoutChangingPendingStatus() {
694769da
VU
1547 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1548 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
5896d037 1549 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
1550 'id' => $contribution['id'],
1551 'api.contribution.delete' => 1,
1552 ));
694769da
VU
1553 $this->assertEquals(2, $contribution['contribution_status_id']);
1554 }
a1a2a83d
TO
1555
1556 /**
28de42d1
EM
1557 * Test Updating a Contribution.
1558 *
a1a2a83d
TO
1559 * CHANGE: we require the API to do an incremental update
1560 */
00be9182 1561 public function testCreateUpdateContribution() {
78ab0ca4 1562 $contributionID = $this->contributionCreate(array(
1563 'contact_id' => $this->_individualId,
1564 'trxn_id' => 212355,
1565 'financial_type_id' => $this->_financialTypeId,
1566 'invoice_id' => 'old_invoice',
1567 ));
6a488035
TO
1568 $old_params = array(
1569 'contribution_id' => $contributionID,
6a488035 1570 );
4ab7d517 1571 $original = $this->callAPISuccess('contribution', 'get', $old_params);
2bfae985 1572 $this->assertEquals($original['id'], $contributionID);
6a488035
TO
1573 //set up list of old params, verify
1574
1575 //This should not be required on update:
1576 $old_contact_id = $original['values'][$contributionID]['contact_id'];
7d543448 1577 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
6a488035
TO
1578 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1579 $old_source = $original['values'][$contributionID]['contribution_source'];
1580
6a488035
TO
1581 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1582 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1583
1584 //check against values in CiviUnitTestCase::createContribution()
2bfae985
EM
1585 $this->assertEquals($old_contact_id, $this->_individualId);
1586 $this->assertEquals($old_fee_amount, 5.00);
1587 $this->assertEquals($old_source, 'SSF');
1588 $this->assertEquals($old_trxn_id, 212355);
78ab0ca4 1589 $this->assertEquals($old_invoice_id, 'old_invoice');
6a488035
TO
1590 $params = array(
1591 'id' => $contributionID,
1592 'contact_id' => $this->_individualId,
09201732
K
1593 'total_amount' => 105.00,
1594 'fee_amount' => 7.00,
4ab7d517 1595 'financial_type_id' => $this->_financialTypeId,
09201732 1596 'non_deductible_amount' => 22.00,
6a488035 1597 'contribution_status_id' => 1,
ef32adff 1598 'note' => 'Donating for Noble Cause',
6a488035
TO
1599 );
1600
4ab7d517 1601 $contribution = $this->callAPISuccess('contribution', 'create', $params);
6a488035
TO
1602
1603 $new_params = array(
1604 'contribution_id' => $contribution['id'],
4ab7d517 1605
6a488035 1606 );
ef32adff 1607 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1608
1609 $this->assertEquals($contribution['contact_id'], $this->_individualId);
09201732 1610 $this->assertEquals($contribution['total_amount'], 105.00);
ef32adff 1611 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
1612 $this->assertEquals($contribution['financial_type'], 'Donation');
1613 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
09201732
K
1614 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1615 $this->assertEquals($contribution['fee_amount'], 7.00);
ef32adff 1616 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1617 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1618 $this->assertEquals($contribution['contribution_source'], $old_source);
1619 $this->assertEquals($contribution['contribution_status'], 'Completed');
09201732
K
1620
1621 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1622
6a488035
TO
1623 $params = array(
1624 'contribution_id' => $contributionID,
4ab7d517 1625
6a488035 1626 );
4ab7d517 1627 $result = $this->callAPISuccess('contribution', 'delete', $params);
22f80e87 1628 $this->assertAPISuccess($result);
6a488035
TO
1629 }
1630
a1a2a83d
TO
1631 /**
1632 * Attempt (but fail) to delete a contribution without parameters.
1633 */
00be9182 1634 public function testDeleteEmptyParamsContribution() {
4ab7d517 1635 $params = array();
858d0bf8 1636 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1637 }
1638
00be9182 1639 public function testDeleteParamsNotArrayContribution() {
6a488035 1640 $params = 'contribution_id= 1';
d0e1eff2 1641 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1642 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1643 }
1644
00be9182 1645 public function testDeleteWrongParamContribution() {
6a488035
TO
1646 $params = array(
1647 'contribution_source' => 'SSF',
4ab7d517 1648
6a488035 1649 );
858d0bf8 1650 $this->callAPIFailure('contribution', 'delete', $params);
6a488035
TO
1651 }
1652
00be9182 1653 public function testDeleteContribution() {
78ab0ca4 1654 $contributionID = $this->contributionCreate(array(
1655 'contact_id' => $this->_individualId,
1656 'financial_type_id' => $this->_financialTypeId,
1657 ));
6a488035
TO
1658 $params = array(
1659 'id' => $contributionID,
6a488035 1660 );
4ab7d517 1661 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
1662 }
1663
1664 /**
d177a2a6 1665 * Test civicrm_contribution_search with empty params.
1e52837d 1666 *
d177a2a6 1667 * All available contributions expected.
6a488035 1668 */
00be9182 1669 public function testSearchEmptyParams() {
4ab7d517 1670 $params = array();
6a488035
TO
1671
1672 $p = array(
1673 'contact_id' => $this->_individualId,
1674 'receive_date' => date('Ymd'),
1675 'total_amount' => 100.00,
4ab7d517 1676 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1677 'non_deductible_amount' => 10.00,
1678 'fee_amount' => 5.00,
1679 'net_amount' => 95.00,
1680 'trxn_id' => 23456,
1681 'invoice_id' => 78910,
1682 'source' => 'SSF',
1683 'contribution_status_id' => 1,
4ab7d517 1684
6a488035 1685 );
4ab7d517 1686 $contribution = $this->callAPISuccess('contribution', 'create', $p);
6a488035 1687
4ab7d517 1688 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1689 // We're taking the first element.
1690 $res = $result['values'][$contribution['id']];
1691
2bfae985
EM
1692 $this->assertEquals($p['contact_id'], $res['contact_id']);
1693 $this->assertEquals($p['total_amount'], $res['total_amount']);
5896d037 1694 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1695 $this->assertEquals($p['net_amount'], $res['net_amount']);
1696 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1697 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1698 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1699 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1700 $this->assertEquals($p['source'], $res['contribution_source']);
6a488035 1701 // contribution_status_id = 1 => Completed
2bfae985 1702 $this->assertEquals('Completed', $res['contribution_status']);
6a488035
TO
1703
1704 $this->contributionDelete($contribution['id']);
1705 }
1706
1707 /**
d177a2a6 1708 * Test civicrm_contribution_search. Success expected.
6a488035 1709 */
00be9182 1710 public function testSearch() {
6a488035
TO
1711 $p1 = array(
1712 'contact_id' => $this->_individualId,
1713 'receive_date' => date('Ymd'),
1714 'total_amount' => 100.00,
4ab7d517 1715 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1716 'non_deductible_amount' => 10.00,
1717 'contribution_status_id' => 1,
4ab7d517 1718
6a488035 1719 );
4ab7d517 1720 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
6a488035
TO
1721
1722 $p2 = array(
1723 'contact_id' => $this->_individualId,
1724 'receive_date' => date('Ymd'),
1725 'total_amount' => 200.00,
4ab7d517 1726 'financial_type_id' => $this->_financialTypeId,
6a488035
TO
1727 'non_deductible_amount' => 20.00,
1728 'trxn_id' => 5454565,
1729 'invoice_id' => 1212124,
1730 'fee_amount' => 50.00,
1731 'net_amount' => 60.00,
1732 'contribution_status_id' => 2,
4ab7d517 1733
6a488035 1734 );
2f45e1c2 1735 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
6a488035
TO
1736
1737 $params = array(
1738 'contribution_id' => $contribution2['id'],
4ab7d517 1739
6a488035 1740 );
2f45e1c2 1741 $result = $this->callAPISuccess('contribution', 'get', $params);
6a488035
TO
1742 $res = $result['values'][$contribution2['id']];
1743
2bfae985
EM
1744 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1745 $this->assertEquals($p2['total_amount'], $res['total_amount']);
5896d037 1746 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
2bfae985
EM
1747 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1748 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1749 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1750 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1751 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
6a488035 1752 // contribution_status_id = 2 => Pending
2bfae985 1753 $this->assertEquals('Pending', $res['contribution_status']);
6a488035
TO
1754
1755 $this->contributionDelete($contribution1['id']);
1756 $this->contributionDelete($contribution2['id']);
1757 }
2f45e1c2 1758
0efa8efe 1759 /**
eceb18cc 1760 * Test completing a transaction via the API.
0efa8efe 1761 *
1762 * Note that we are creating a logged in user because email goes out from
1763 * that person
1764 */
00be9182 1765 public function testCompleteTransaction() {
5896d037 1766 $mut = new CiviMailUtils($this, TRUE);
ec7e3954 1767 $this->swapMessageTemplateForTestTemplate();
0efa8efe 1768 $this->createLoggedInUser();
6c6e6187
TO
1769 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1770 $contribution = $this->callAPISuccess('contribution', 'create', $params);
66d3f9be 1771 $this->callAPISuccess('contribution', 'completetransaction', array(
0efa8efe 1772 'id' => $contribution['id'],
66d3f9be 1773 ));
cc7b912f 1774 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
d5580ed4 1775 $this->assertEquals('SSF', $contribution['contribution_source']);
cc7b912f 1776 $this->assertEquals('Completed', $contribution['contribution_status']);
1777 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
0efa8efe 1778 $mut->checkMailLog(array(
ec7e3954
E
1779 'email:::anthony_anderson@civicrm.org',
1780 'is_monetary:::1',
1781 'amount:::100.00',
1782 'currency:::USD',
1783 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
76e8d9c4 1784 "receipt_date:::\n",
8a40179e 1785 'contributeMode:::notify',
1786 'title:::Contribution',
1787 'displayName:::Mr. Anthony Anderson II',
1788 'contributionStatus:::Completed',
1789 ));
1790 $mut->stop();
1791 $this->revertTemplateToReservedTemplate();
1792 }
1793
1794 /**
1795 * Test completing a transaction via the API with a non-USD transaction.
1796 */
1797 public function testCompleteTransactionEuro() {
1798 $mut = new CiviMailUtils($this, TRUE);
1799 $this->swapMessageTemplateForTestTemplate();
1800 $this->createLoggedInUser();
1801 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1802 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1803
1804 $this->callAPISuccess('contribution', 'completetransaction', array(
1805 'id' => $contribution['id'],
1806 ));
1807
1808 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1809 $this->assertEquals('SSF', $contribution['contribution_source']);
1810 $this->assertEquals('Completed', $contribution['contribution_status']);
1811 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1812
1813 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1814 $entityFinancialTransaction = reset($entityFinancialTransactions);
1815 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1816 $this->assertEquals('EUR', $financialTrxn['currency']);
1817
1818 $mut->checkMailLog(array(
1819 'email:::anthony_anderson@civicrm.org',
1820 'is_monetary:::1',
1821 'amount:::100.00',
1822 'currency:::EUR',
1823 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1824 "receipt_date:::\n",
ec7e3954
E
1825 'contributeMode:::notify',
1826 'title:::Contribution',
1827 'displayName:::Mr. Anthony Anderson II',
3b28799d 1828 'contributionStatus:::Completed',
0efa8efe 1829 ));
46fa5206 1830 $mut->stop();
ec7e3954 1831 $this->revertTemplateToReservedTemplate();
46fa5206
EM
1832 }
1833
e05d2e11 1834 /**
1835 * Test to ensure mail is sent on chosing pay later
1836 */
1837 public function testpayLater() {
1838 $mut = new CiviMailUtils($this, TRUE);
1839 $this->swapMessageTemplateForTestTemplate();
1840 $this->createLoggedInUser();
1841
1842 // create contribution page first
1843 $contributionPageParams = array(
1844 'title' => 'Help Support CiviCRM!',
1845 'financial_type_id' => 1,
1846 'is_monetary' => TRUE,
1847 'is_pay_later' => 1,
1848 'is_quick_config' => TRUE,
1849 'pay_later_text' => 'I will send payment by check',
1850 'pay_later_receipt' => 'This is a pay later reciept',
1851 'is_allow_other_amount' => 1,
1852 'min_amount' => 10.00,
1853 'max_amount' => 10000.00,
1854 'goal_amount' => 100000.00,
1855 'is_email_receipt' => 1,
1856 'is_active' => 1,
1857 'amount_block_is_active' => 1,
1858 'currency' => 'USD',
1859 'is_billing_required' => 0,
1860 );
1861 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1862
1863 // submit form values
1864 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1865 $params = array(
1866 'id' => $contributionPageResult['id'],
1867 'contact_id' => $this->_individualId,
1868 'email-5' => 'anthony_anderson@civicrm.org',
1869 'payment_processor_id' => 0,
1870 'amount' => 100.00,
1871 'tax_amount' => '',
1872 'currencyID' => 'USD',
1873 'is_pay_later' => 1,
1874 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1875 'is_quick_config' => 1,
1876 'description' => 'Online Contribution: Help Support CiviCRM!',
1877 'price_set_id' => $priceSet['id'],
1878 );
1879 $this->callAPISuccess('contribution_page', 'submit', $params);
1880
1881 $mut->checkMailLog(array(
1882 'is_pay_later:::1',
1883 'email:::anthony_anderson@civicrm.org',
1884 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1885 'displayName:::Mr. Anthony Anderson II',
1886 'contributionPageId:::' . $contributionPageResult['id'],
1887 'title:::' . $contributionPageParams['title'],
8beee0e8 1888 'amount:::' . $params['amount'],
e05d2e11 1889 ));
1890 $mut->stop();
1891 $this->revertTemplateToReservedTemplate();
1892 }
1893
2a0df9d9 1894 /**
1895 * Test to check whether contact billing address is used when no contribution address
1896 */
1897 public function testBillingAddress() {
1898 $mut = new CiviMailUtils($this, TRUE);
1899 $this->swapMessageTemplateForTestTemplate();
1900 $this->createLoggedInUser();
1901
1902 //Scenario 1: When Contact don't have any address
1903 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1904 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1905 $this->callAPISuccess('contribution', 'completetransaction', array(
1906 'id' => $contribution['id'],
1907 ));
1908 $mut->checkMailLog(array(
1909 'address:::',
1910 ));
1911
1912 // Scenario 2: Contribution using address
1913 $address = $this->callAPISuccess('address', 'create', array(
1914 'street_address' => 'contribution billing st',
1915 'location_type_id' => 2,
1916 'contact_id' => $this->_params['contact_id'],
1917 ));
76e8d9c4
E
1918 $params = array_merge($this->_params, array(
1919 'contribution_status_id' => 2,
2a0df9d9 1920 'address_id' => $address['id'],
1921 )
1922 );
1923 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1924 $this->callAPISuccess('contribution', 'completetransaction', array(
1925 'id' => $contribution['id'],
1926 ));
1927 $mut->checkMailLog(array(
1928 'address:::contribution billing st',
1929 ));
1930
1931 // Scenario 3: Contribution wtth no address but contact has a billing address
1932 $this->callAPISuccess('address', 'create', array(
1933 'id' => $address['id'],
1934 'street_address' => 'is billing st',
1935 'contact_id' => $this->_params['contact_id'],
1936 ));
1937 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1938 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1939 $this->callAPISuccess('contribution', 'completetransaction', array(
1940 'id' => $contribution['id'],
1941 ));
1942 $mut->checkMailLog(array(
1943 'address:::is billing st',
1944 ));
1945
1946 $mut->stop();
1947 $this->revertTemplateToReservedTemplate();
1948 }
1949
080a561b 1950 /**
1951 * Test completing a transaction via the API.
1952 *
1953 * Note that we are creating a logged in user because email goes out from
1954 * that person
1955 */
1956 public function testCompleteTransactionFeeAmount() {
1957 $this->createLoggedInUser();
1958 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1959 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1960 $this->callAPISuccess('contribution', 'completetransaction', array(
1961 'id' => $contribution['id'],
1962 'fee_amount' => '.56',
1963 'trxn_id' => '7778888',
1964 ));
1965 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1966 $this->assertEquals('Completed', $contribution['contribution_status']);
1967 $this->assertEquals('7778888', $contribution['trxn_id']);
1968 $this->assertEquals('.56', $contribution['fee_amount']);
1969 $this->assertEquals('99.44', $contribution['net_amount']);
1970 }
1971
effb4d85 1972 /**
83644f47 1973 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
1974 *
1975 * @param string $thousandSeparator
1976 * punctuation used to refer to thousands.
1977 *
1978 * @dataProvider getThousandSeparators
effb4d85 1979 */
83644f47 1980 public function testCheckTaxAmount($thousandSeparator) {
1981 $this->setCurrencySeparators($thousandSeparator);
effb4d85
SL
1982 $contact = $this->createLoggedInUser();
1983 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1984 'name' => 'Test taxable financial Type',
1985 'is_reserved' => 0,
1986 'is_active' => 1,
1987 ));
1988 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1989 'name' => 'Test Tax financial account ',
1990 'contact_id' => $contact,
1991 'financial_account_type_id' => 2,
1992 'is_tax' => 1,
1993 'tax_rate' => 5.00,
1994 'is_reserved' => 0,
1995 'is_active' => 1,
1996 'is_default' => 0,
1997 ));
1998 $financialTypeId = $financialType['id'];
1999 $financialAccountId = $financialAccount['id'];
2000 $financialAccountParams = array(
2001 'entity_table' => 'civicrm_financial_type',
2002 'entity_id' => $financialTypeId,
2003 'account_relationship' => 10,
2004 'financial_account_id' => $financialAccountId,
2005 );
a76b8bd8 2006 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
f527e012 2007
effb4d85
SL
2008 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
2009 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2010 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
2011 $this->callAPISuccess('contribution', 'completetransaction', array(
2012 'id' => $contribution['id'],
2013 'trxn_id' => '777788888',
99a4cd32 2014 'fee_amount' => '6.00',
effb4d85 2015 ));
99a4cd32 2016 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
effb4d85 2017 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
99a4cd32 2018 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
f836984d 2019 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
effb4d85
SL
2020 }
2021
d97c96dc 2022 /**
0e6ccb2e 2023 * Test repeat contribution successfully creates line item.
d97c96dc 2024 */
1e52837d 2025 public function testRepeatTransaction() {
0e6ccb2e 2026 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
d97c96dc
EM
2027 $this->callAPISuccess('contribution', 'repeattransaction', array(
2028 'original_contribution_id' => $originalContribution['id'],
2029 'contribution_status_id' => 'Completed',
2030 'trxn_id' => uniqid(),
2031 ));
2032 $lineItemParams = array(
2033 'entity_id' => $originalContribution['id'],
2034 'sequential' => 1,
2035 'return' => array(
2036 'entity_table',
2037 'qty',
2038 'unit_price',
2039 'line_total',
2040 'label',
2041 'financial_type_id',
2042 'deductible_amount',
2043 'price_field_value_id',
2044 'price_field_id',
1e52837d 2045 ),
d97c96dc
EM
2046 );
2047 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2048 'entity_id' => $originalContribution['id'],
2049 )));
2050 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2051 'entity_id' => $originalContribution['id'] + 1,
2052 )));
2053 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2054 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2055 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
f69a9ac3 2056 $this->_checkFinancialRecords(array(
2057 'id' => $originalContribution['id'] + 1,
2058 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2059 'id' => $originalContribution['payment_processor_id'],
2060 'return' => 'payment_instrument_id',
2061 )),
2062 ), 'online');
d97c96dc
EM
2063 $this->quickCleanUpFinancialEntities();
2064 }
2065
893a550c 2066 /**
0e6ccb2e
K
2067 * Test repeat contribution successfully creates line items (plural).
2068 */
2069 public function testRepeatTransactionLineItems() {
7150b1c8 2070 // CRM-19309
0e6ccb2e
K
2071 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2072 $this->callAPISuccess('contribution', 'repeattransaction', array(
2073 'original_contribution_id' => $originalContribution['id'],
2074 'contribution_status_id' => 'Completed',
2075 'trxn_id' => uniqid(),
2076 ));
2077
2078 $lineItemParams = array(
2079 'entity_id' => $originalContribution['id'],
2080 'sequential' => 1,
2081 'return' => array(
2082 'entity_table',
2083 'qty',
2084 'unit_price',
2085 'line_total',
2086 'label',
2087 'financial_type_id',
2088 'deductible_amount',
2089 'price_field_value_id',
2090 'price_field_id',
2091 ),
2092 );
2093 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2094 'entity_id' => $originalContribution['id'],
2095 )));
2096 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2097 'entity_id' => $originalContribution['id'] + 1,
2098 )));
2099
2100 // unset id and entity_id for all of them to be able to compare the lineItems:
2101 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2102 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2103 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2104
2105 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2106 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2107 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2108
7150b1c8
K
2109 // CRM-19309 so in future we also want to:
2110 // check that financial_line_items have been created for entity_id 3 and 4;
0e6ccb2e
K
2111
2112 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2113 $this->quickCleanUpFinancialEntities();
2114 }
2115
2116 /**
2117 * Test repeat contribution successfully creates is_test transaction.
893a550c 2118 */
2119 public function testRepeatTransactionIsTest() {
2120 $this->_params['is_test'] = 1;
0e6ccb2e 2121 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
893a550c 2122
2123 $this->callAPISuccess('contribution', 'repeattransaction', array(
2124 'original_contribution_id' => $originalContribution['id'],
2125 'contribution_status_id' => 'Completed',
2126 'trxn_id' => uniqid(),
2127 ));
2128 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2129 }
2130
d5580ed4 2131 /**
2132 * Test repeat contribution passed in status.
2133 */
2134 public function testRepeatTransactionPassedInStatus() {
0e6ccb2e 2135 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
d5580ed4 2136
2137 $this->callAPISuccess('contribution', 'repeattransaction', array(
2138 'original_contribution_id' => $originalContribution['id'],
2139 'contribution_status_id' => 'Pending',
2140 'trxn_id' => uniqid(),
2141 ));
2142 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2143 }
2144
1eade77d 2145 /**
2146 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2147 */
2148 public function testRepeatTransactionAcceptRecurID() {
2149 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2150 'contact_id' => $this->_individualId,
2151 'installments' => '12',
2152 'frequency_interval' => '1',
2153 'amount' => '100',
2154 'contribution_status_id' => 1,
2155 'start_date' => '2012-01-01 00:00:00',
2156 'currency' => 'USD',
2157 'frequency_unit' => 'month',
2158 'payment_processor_id' => $this->paymentProcessorID,
2159 ));
2160 $this->callAPISuccess('contribution', 'create', array_merge(
2161 $this->_params,
2162 array('contribution_recur_id' => $contributionRecur['id']))
2163 );
2164
2165 $this->callAPISuccess('contribution', 'repeattransaction', array(
2166 'contribution_recur_id' => $contributionRecur['id'],
2167 'contribution_status_id' => 'Completed',
2168 'trxn_id' => uniqid(),
2169 ));
2170
2171 $this->quickCleanUpFinancialEntities();
2172 }
2173
28124368
PH
2174 /**
2175 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2176 */
2177 public function testRepeatTransactionTestRecurId() {
2178 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2179 'contact_id' => $this->_individualId,
2180 'frequency_interval' => '1',
2181 'amount' => '1.00',
2182 'contribution_status_id' => 1,
2183 'start_date' => '2017-01-01 00:00:00',
2184 'currency' => 'USD',
2185 'frequency_unit' => 'month',
2186 'payment_processor_id' => $this->paymentProcessorID,
2187 'is_test' => 1,
2188 ));
2189 $this->callAPISuccess('contribution', 'create', array_merge(
2190 $this->_params,
2191 array(
2192 'contribution_recur_id' => $contributionRecur['id'],
2193 'is_test' => 1,
2194 ))
2195 );
2196
2197 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2198 'contribution_recur_id' => $contributionRecur['id'],
2199 'contribution_status_id' => 'Completed',
2200 'trxn_id' => uniqid(),
2201 ));
2202
2203 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2204 $this->quickCleanUpFinancialEntities();
2205 }
37f29fcf 2206
d2334242 2207 /**
37f29fcf 2208 * CRM-19945 Tests that Contribute.repeattransaction renews a membership when contribution status=Completed
7c3f3d59 2209 *
d2334242 2210 */
37f29fcf 2211 public function testRepeatTransactionMembershipRenewCompletedContribution() {
d2334242
PH
2212 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2213
2214 $this->callAPISuccess('contribution', 'create', array(
7c3f3d59 2215 'contact_id' => $originalContribution['values'][1]['contact_id'],
2216 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2217 'total_amount' => $originalContribution['values'][1]['total_amount'],
2218 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2219 'contribution_status_id' => "Failed",
d2334242
PH
2220 ));
2221
2222 $this->callAPISuccess('membership', 'create', array(
2223 'id' => $membership['id'],
2224 'end_date' => 'yesterday',
37f29fcf 2225 'status_id' => 'Expired',
d2334242
PH
2226 ));
2227
7c3f3d59 2228 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
d2334242
PH
2229 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2230 'contribution_status_id' => 'Completed',
7c3f3d59 2231 'trxn_id' => 'bobsled',
d2334242
PH
2232 ));
2233
7c3f3d59 2234 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
d2334242
PH
2235 'id' => $membership['id'],
2236 'return' => 'status_id',
2237 ));
2238
37f29fcf
MW
2239 $membership = $this->callAPISuccess('membership', 'get', array(
2240 'id' => $membership['id'],
2241 ));
2242
2243 $this->assertEquals('New', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2244
2245 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2246 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2247 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2248 $this->quickCleanUpFinancialEntities();
2249 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2250 }
2251
2252 /**
2253 * CRM-19945 Tests that Contribute.repeattransaction DOES NOT renew a membership when contribution status=Failed
2254 *
2255 * @dataProvider contributionStatusProvider
2256 */
2257 public function testRepeatTransactionMembershipRenewContributionNotCompleted($contributionStatus) {
2258 // Completed status should renew so we don't test that here
2259 // In Progress status is only for recurring contributions so we don't test that here
2260 if (in_array($contributionStatus['name'], ['Completed', 'In Progress'])) {
2261 return;
2262 }
2263 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2264
2265 $this->callAPISuccess('contribution', 'create', array(
2266 'contact_id' => $originalContribution['values'][1]['contact_id'],
2267 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2268 'total_amount' => $originalContribution['values'][1]['total_amount'],
2269 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2270 'contribution_status_id' => "Completed",
2271 ));
2272
2273 $this->callAPISuccess('membership', 'create', array(
2274 'id' => $membership['id'],
2275 'end_date' => 'yesterday',
2276 'status_id' => 'Expired',
2277 ));
2278
2279 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2280 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2281 'contribution_status_id' => $contributionStatus['name'],
2282 'trxn_id' => 'bobsled',
2283 ));
2284
2285 $updatedMembership = $this->callAPISuccess('membership', 'getsingle', array(
2286 'id' => $membership['id'],
2287 ));
2288
2289 $dateTime = new DateTime('yesterday');
2290 $this->assertEquals($dateTime->format('Y-m-d'), $updatedMembership['end_date']);
2291 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Expired'), $updatedMembership['status_id']);
7c3f3d59 2292
2293 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2294 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2295 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
d2334242
PH
2296 $this->quickCleanUpFinancialEntities();
2297 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2298 }
28124368 2299
37f29fcf
MW
2300 /**
2301 * Dataprovider provides contribution status as [optionvalue=>contribution_status_name]
2302 * FIXME: buildOptions seems to die in CRM_Core_Config::_construct when in test mode.
2303 *
2304 * @return array
2305 * @throws \CiviCRM_API3_Exception
2306 */
2307 public function contributionStatusProvider() {
2308 $contributionStatuses = civicrm_api3('OptionValue', 'get', [
2309 'return' => ["id", "name"],
2310 'option_group_id' => "contribution_status",
2311 ]);
2312 foreach ($contributionStatuses['values'] as $statusName) {
2313 $statuses[] = [$statusName];
2314 }
2315 return $statuses;
2316 }
2317
c03f1689
EM
2318 /**
2319 * CRM-16397 test appropriate action if total amount has changed for single line items.
2320 */
2321 public function testRepeatTransactionAlteredAmount() {
2322 $paymentProcessorID = $this->paymentProcessorCreate();
c03f1689
EM
2323 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2324 'contact_id' => $this->_individualId,
2325 'installments' => '12',
2326 'frequency_interval' => '1',
2327 'amount' => '500',
2328 'contribution_status_id' => 1,
2329 'start_date' => '2012-01-01 00:00:00',
2330 'currency' => 'USD',
2331 'frequency_unit' => 'month',
2332 'payment_processor_id' => $paymentProcessorID,
2333 ));
2334 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2335 $this->_params,
2336 array(
2337 'contribution_recur_id' => $contributionRecur['id'],
c03f1689
EM
2338 ))
2339 );
2340
2341 $this->callAPISuccess('contribution', 'repeattransaction', array(
2342 'original_contribution_id' => $originalContribution['id'],
2343 'contribution_status_id' => 'Completed',
2344 'trxn_id' => uniqid(),
2345 'total_amount' => '400',
2346 'fee_amount' => 50,
c03f1689 2347 ));
0e6ccb2e 2348
c03f1689
EM
2349 $lineItemParams = array(
2350 'entity_id' => $originalContribution['id'],
2351 'sequential' => 1,
2352 'return' => array(
2353 'entity_table',
2354 'qty',
2355 'unit_price',
2356 'line_total',
2357 'label',
2358 'financial_type_id',
2359 'deductible_amount',
2360 'price_field_value_id',
2361 'price_field_id',
2362 ),
2363 );
2364 $this->callAPISuccessGetSingle('contribution', array(
2365 'total_amount' => 400,
c03f1689
EM
2366 'fee_amount' => 50,
2367 'net_amount' => 350,
2368 ));
2369 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2370 'entity_id' => $originalContribution['id'],
2371 )));
2372 $expectedLineItem = array_merge(
2373 $lineItem1['values'][0], array(
2374 'line_total' => '400.00',
2375 'unit_price' => '400.00',
542d9e2c
EM
2376 )
2377 );
c03f1689
EM
2378
2379 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2380 'entity_id' => $originalContribution['id'] + 1,
2381 )));
0e6ccb2e 2382
c03f1689
EM
2383 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2384 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2385 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
c02c17df 2386 }
c03f1689 2387
3c49d90c 2388 /**
2389 * CRM-17718 test appropriate action if financial type has changed for single line items.
2390 */
2391 public function testRepeatTransactionPassedInFinancialType() {
2392 $originalContribution = $this->setUpRecurringContribution();
2393
2394 $this->callAPISuccess('contribution', 'repeattransaction', array(
2395 'original_contribution_id' => $originalContribution['id'],
2396 'contribution_status_id' => 'Completed',
2397 'trxn_id' => uniqid(),
2398 'financial_type_id' => 2,
2399 ));
2400 $lineItemParams = array(
2401 'entity_id' => $originalContribution['id'],
2402 'sequential' => 1,
2403 'return' => array(
2404 'entity_table',
2405 'qty',
2406 'unit_price',
2407 'line_total',
2408 'label',
2409 'financial_type_id',
2410 'deductible_amount',
2411 'price_field_value_id',
2412 'price_field_id',
2413 ),
2414 );
2415
2416 $this->callAPISuccessGetSingle('contribution', array(
2417 'total_amount' => 100,
2418 'financial_type_id' => 2,
2419 ));
2420 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2421 'entity_id' => $originalContribution['id'],
2422 )));
2423 $expectedLineItem = array_merge(
2424 $lineItem1['values'][0], array(
2425 'line_total' => '100.00',
2426 'unit_price' => '100.00',
2427 'financial_type_id' => 2,
257ab382 2428 'contribution_type_id' => 2,
3c49d90c 2429 )
2430 );
3c49d90c 2431 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2432 'entity_id' => $originalContribution['id'] + 1,
2433 )));
2434 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2435 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2436 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2437 }
2438
7f4ef731 2439 /**
2440 * CRM-17718 test appropriate action if financial type has changed for single line items.
2441 */
2442 public function testRepeatTransactionUpdatedFinancialType() {
2443 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2444
2445 $this->callAPISuccess('contribution', 'repeattransaction', array(
2446 'contribution_recur_id' => $originalContribution['id'],
2447 'contribution_status_id' => 'Completed',
2448 'trxn_id' => uniqid(),
2449 ));
2450 $lineItemParams = array(
2451 'entity_id' => $originalContribution['id'],
2452 'sequential' => 1,
2453 'return' => array(
2454 'entity_table',
2455 'qty',
2456 'unit_price',
2457 'line_total',
2458 'label',
2459 'financial_type_id',
2460 'deductible_amount',
2461 'price_field_value_id',
2462 'price_field_id',
2463 ),
2464 );
2465
2466 $this->callAPISuccessGetSingle('contribution', array(
2467 'total_amount' => 100,
2468 'financial_type_id' => 2,
2469 ));
2470 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2471 'entity_id' => $originalContribution['id'],
2472 )));
2473 $expectedLineItem = array_merge(
2474 $lineItem1['values'][0], array(
2475 'line_total' => '100.00',
2476 'unit_price' => '100.00',
2477 'financial_type_id' => 2,
257ab382 2478 'contribution_type_id' => 2,
7f4ef731 2479 )
2480 );
2481
2482 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2483 'entity_id' => $originalContribution['id'] + 1,
2484 )));
2485 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2486 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2487 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2488 }
2489
c02c17df 2490 /**
1eade77d 2491 * CRM-16397 test appropriate action if campaign has been passed in.
c02c17df 2492 */
2493 public function testRepeatTransactionPassedInCampaign() {
2494 $paymentProcessorID = $this->paymentProcessorCreate();
2495 $campaignID = $this->campaignCreate();
2496 $campaignID2 = $this->campaignCreate();
2497 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2498 'contact_id' => $this->_individualId,
2499 'installments' => '12',
2500 'frequency_interval' => '1',
2501 'amount' => '100',
2502 'contribution_status_id' => 1,
2503 'start_date' => '2012-01-01 00:00:00',
2504 'currency' => 'USD',
2505 'frequency_unit' => 'month',
2506 'payment_processor_id' => $paymentProcessorID,
2507 ));
2508 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2509 $this->_params,
2510 array(
2511 'contribution_recur_id' => $contributionRecur['id'],
2512 'campaign_id' => $campaignID,
2513 ))
2514 );
2515
2516 $this->callAPISuccess('contribution', 'repeattransaction', array(
2517 'original_contribution_id' => $originalContribution['id'],
2518 'contribution_status_id' => 'Completed',
2519 'trxn_id' => uniqid(),
2520 'campaign_id' => $campaignID2,
2521 ));
2522
2523 $this->callAPISuccessGetSingle('contribution', array(
2524 'total_amount' => 100,
2525 'campaign_id' => $campaignID2,
2526 ));
2527 }
2528
2529 /**
2530 * CRM-17718 campaign stored on contribution recur gets priority.
2531 *
2532 * This reflects the fact we permit people to update them.
2533 */
2534 public function testRepeatTransactionUpdatedCampaign() {
2535 $paymentProcessorID = $this->paymentProcessorCreate();
2536 $campaignID = $this->campaignCreate();
2537 $campaignID2 = $this->campaignCreate();
2538 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2539 'contact_id' => $this->_individualId,
2540 'installments' => '12',
2541 'frequency_interval' => '1',
2542 'amount' => '100',
2543 'contribution_status_id' => 1,
2544 'start_date' => '2012-01-01 00:00:00',
2545 'currency' => 'USD',
2546 'frequency_unit' => 'month',
2547 'payment_processor_id' => $paymentProcessorID,
2548 'campaign_id' => $campaignID,
2549 ));
2550 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2551 $this->_params,
2552 array(
2553 'contribution_recur_id' => $contributionRecur['id'],
2554 'campaign_id' => $campaignID2,
2555 ))
2556 );
2557
2558 $this->callAPISuccess('contribution', 'repeattransaction', array(
2559 'original_contribution_id' => $originalContribution['id'],
2560 'contribution_status_id' => 'Completed',
2561 'trxn_id' => uniqid(),
2562 ));
2563
2564 $this->callAPISuccessGetSingle('contribution', array(
2565 'total_amount' => 100,
2566 'campaign_id' => $campaignID,
2567 ));
c03f1689
EM
2568 }
2569
2b0de476
E
2570 /**
2571 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2572 *
2573 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2574 */
2575 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
2576 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2577 // This will made the trick to get the not equals behaviour.
2578 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 4));
2579 $this->callAPISuccess('contribution', 'repeattransaction', array(
2580 'contribution_recur_id' => $originalContribution['id'],
2581 'contribution_status_id' => 'Completed',
2582 'trxn_id' => uniqid(),
2583 ));
2584 $lineItemParams = array(
2585 'entity_id' => $originalContribution['id'],
2586 'sequential' => 1,
2587 'return' => array(
2588 'entity_table',
2589 'qty',
2590 'unit_price',
2591 'line_total',
2592 'label',
2593 'financial_type_id',
2594 'deductible_amount',
2595 'price_field_value_id',
2596 'price_field_id',
2597 ),
2598 );
2599 $this->callAPISuccessGetSingle('contribution', array(
2600 'total_amount' => 100,
2601 'financial_type_id' => 2,
2602 ));
2603 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2604 'entity_id' => $originalContribution['id'],
2605 )));
2606 $expectedLineItem = array_merge(
2607 $lineItem1['values'][0], array(
2608 'line_total' => '100.00',
2609 'unit_price' => '100.00',
2610 'financial_type_id' => 4,
2611 'contribution_type_id' => 4,
2612 )
2613 );
2614
2615 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2616 'entity_id' => $originalContribution['id'] + 1,
2617 )));
2618 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 1));
2619 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2620 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2621 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2622 }
2623
2624
2936c3b5
EM
2625 /**
2626 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2627 */
2628 public function testCompleteTransactionNetAmountOK() {
2629 $this->createLoggedInUser();
2630 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2631 unset($params['net_amount']);
2632 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2633 $this->callAPISuccess('contribution', 'completetransaction', array(
2634 'id' => $contribution['id'],
2635 ));
2636 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2637 $this->assertEquals('Completed', $contribution['contribution_status']);
2638 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2639 }
2640
46fa5206 2641 /**
1e52837d 2642 * CRM-14151 - Test completing a transaction via the API.
46fa5206 2643 */
00be9182 2644 public function testCompleteTransactionWithReceiptDateSet() {
76e8d9c4 2645 $this->swapMessageTemplateForTestTemplate();
5896d037 2646 $mut = new CiviMailUtils($this, TRUE);
46fa5206 2647 $this->createLoggedInUser();
6c6e6187
TO
2648 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2649 $contribution = $this->callAPISuccess('contribution', 'create', $params);
7104593e 2650 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
6c6e6187 2651 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
46fa5206 2652 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
7104593e 2653 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
46fa5206
EM
2654 $mut->checkMailLog(array(
2655 'Receipt - Contribution',
76e8d9c4 2656 'receipt_date:::' . date('Ymd'),
46fa5206 2657 ));
0efa8efe 2658 $mut->stop();
76e8d9c4 2659 $this->revertTemplateToReservedTemplate();
0efa8efe 2660 }
2661
e2ca457d
KG
2662 /**
2663 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2664 */
2665 public function testCompleteTransactionWithEmailReceiptInput() {
d891a273 2666 $contributionPage = $this->createReceiptableContributionPage();
2667
e2ca457d
KG
2668 $this->_params['contribution_page_id'] = $contributionPage['id'];
2669 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2670 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2671 // Complete the transaction overriding is_email_receipt to = FALSE
2672 $this->callAPISuccess('contribution', 'completetransaction', array(
2673 'id' => $contribution['id'],
2674 'trxn_date' => date('2011-04-09'),
2675 'trxn_id' => 'kazam',
2676 'is_email_receipt' => 0,
2677 ));
2678 // Check if a receipt was issued
2679 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2680 $this->assertEquals('', $receipt_date);
2681 }
3b8c739e 2682
d891a273 2683 /**
2684 * Test that $is_recur is assigned to the receipt.
2685 */
2686 public function testCompleteTransactionForRecurring() {
2687
2688 $this->swapMessageTemplateForTestTemplate();
2689 $recurring = $this->setUpRecurringContribution();
2690 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2691
2692 $this->_params['contribution_page_id'] = $contributionPage['id'];
2693 $this->_params['contribution_recur_id'] = $recurring['id'];
2694
2695 $contribution = $this->setUpForCompleteTransaction();
2696
2697 $this->callAPISuccess('contribution', 'completetransaction', array(
2698 'id' => $contribution['id'],
2699 'trxn_date' => date('2011-04-09'),
2700 'trxn_id' => 'kazam',
2701 'is_email_receipt' => 1,
2702 ));
2703
2704 $this->mut->checkMailLog(array(
2705 'is_recur:::1',
0131a2ce 2706 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL,
d891a273 2707 ));
2708 $this->mut->stop();
2709 $this->revertTemplateToReservedTemplate();
2710 }
55df1211
AS
2711 /**
2712 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2713 *
2714 * If passed in it will override the default from contribution page.
2715 */
2716 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2717 $mut = new CiviMailUtils($this, TRUE);
2718 $this->createLoggedInUser();
2719 // Create a Contribution Page with is_email_receipt = FALSE
2720 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2721 'receipt_from_name' => 'Mickey Mouse',
2722 'receipt_from_email' => 'mickey@mouse.com',
2723 'title' => "Test Contribution Page",
2724 'financial_type_id' => 1,
2725 'currency' => 'CAD',
2726 'is_monetary' => TRUE,
2727 'is_email_receipt' => 0,
2728 ));
2729 $this->_params['contribution_page_id'] = $contributionPage['id'];
2730 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2731 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2732 // Complete the transaction overriding is_email_receipt to = TRUE
2733 $this->callAPISuccess('contribution', 'completetransaction', array(
2734 'id' => $contribution['id'],
2735 'is_email_receipt' => 1,
2736 ));
2737 $mut->checkMailLog(array(
2738 'Please print this receipt for your records.',
2739 ));
2740 $mut->stop();
2741 }
d891a273 2742
b80f2ad1
E
2743 /**
2744 * Complete the transaction using the template with all the possible.
2745 */
2746 public function testCompleteTransactionWithTestTemplate() {
2747 $this->swapMessageTemplateForTestTemplate();
ec7e3954 2748 $contribution = $this->setUpForCompleteTransaction();
b80f2ad1
E
2749 $this->callAPISuccess('contribution', 'completetransaction', array(
2750 'id' => $contribution['id'],
2751 'trxn_date' => date('2011-04-09'),
2752 'trxn_id' => 'kazam',
2753 ));
2754 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
ec7e3954 2755 $this->mut->checkMailLog(array(
b80f2ad1
E
2756 'email:::anthony_anderson@civicrm.org',
2757 'is_monetary:::1',
2758 'amount:::100.00',
2759 'currency:::USD',
2760 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2761 'receipt_date:::' . date('Ymd'),
2762 'contributeMode:::notify',
2763 'title:::Contribution',
2764 'displayName:::Mr. Anthony Anderson II',
2765 'trxn_id:::kazam',
ec7e3954 2766 'contactID:::' . $this->_params['contact_id'],
b80f2ad1
E
2767 'contributionID:::' . $contribution['id'],
2768 'financialTypeId:::1',
2769 'financialTypeName:::Donation',
2770 ));
ec7e3954 2771 $this->mut->stop();
b80f2ad1
E
2772 $this->revertTemplateToReservedTemplate();
2773 }
2774
ec7e3954
E
2775 /**
2776 * Complete the transaction using the template with all the possible.
2777 */
2778 public function testCompleteTransactionContributionPageFromAddress() {
2779 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2780 'receipt_from_name' => 'Mickey Mouse',
2781 'receipt_from_email' => 'mickey@mouse.com',
2782 'title' => "Test Contribution Page",
2783 'financial_type_id' => 1,
2784 'currency' => 'NZD',
2785 'goal_amount' => 50,
2786 'is_pay_later' => 1,
2787 'is_monetary' => TRUE,
2788 'is_email_receipt' => TRUE,
2789 ));
2790 $this->_params['contribution_page_id'] = $contributionPage['id'];
2791 $contribution = $this->setUpForCompleteTransaction();
2792 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2793 $this->mut->checkMailLog(array(
2794 'mickey@mouse.com',
2795 'Mickey Mouse <',
2796 ));
2797 $this->mut->stop();
2798 }
2799
91259407 2800 /**
2801 * Test completing first transaction in a recurring series.
2802 *
2803 * The status should be set to 'in progress' and the next scheduled payment date calculated.
050e11d5 2804 *
2805 * @dataProvider getScheduledDateData
2806 *
2807 * @param array $dataSet
2808 *
2809 * @throws \Exception
91259407 2810 */
050e11d5 2811 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
91259407 2812 $paymentProcessorID = $this->paymentProcessorCreate();
050e11d5 2813 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
91259407 2814 'contact_id' => $this->_individualId,
050e11d5 2815 'installments' => '2',
91259407 2816 'frequency_interval' => '1',
2817 'amount' => '500',
2818 'contribution_status_id' => 'Pending',
2819 'start_date' => '2012-01-01 00:00:00',
2820 'currency' => 'USD',
2821 'frequency_unit' => 'month',
2822 'payment_processor_id' => $paymentProcessorID,
050e11d5 2823 ), $dataSet['data']));
91259407 2824 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2825 $this->_params,
2826 array(
2827 'contribution_recur_id' => $contributionRecur['id'],
2828 'contribution_status_id' => 'Pending',
050e11d5 2829 'receive_date' => $dataSet['receive_date'],
91259407 2830 ))
2831 );
050e11d5 2832 $this->callAPISuccess('Contribution', 'completetransaction', array(
2833 'id' => $contribution,
2834 'receive_date' => $dataSet['receive_date'],
2835 ));
91259407 2836 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2837 'id' => $contributionRecur['id'],
2838 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2839 ));
2840 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
050e11d5 2841 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2842 $this->callAPISuccess('Contribution', 'create', array_merge(
2843 $this->_params,
2844 array(
2845 'contribution_recur_id' => $contributionRecur['id'],
2846 'contribution_status_id' => 'Completed',
2847 )
2848 ));
2849 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2850 'id' => $contributionRecur['id'],
2851 'return' => array('contribution_status_id'),
2852 ));
2853 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2854 }
2855
2856 /**
2857 * Get dates for testing.
2858 *
2859 * @return array
2860 */
2861 public function getScheduledDateData() {
2862 $result = array();
2863 $result[]['2016-08-31-1-month'] = array(
2864 'data' => array(
2865 'start_date' => '2016-08-31',
2866 'frequency_interval' => 1,
2867 'frequency_unit' => 'month',
2868 ),
2869 'receive_date' => '2016-08-31',
2870 'expected' => '2016-10-01 00:00:00',
2871 );
2872 $result[]['2012-01-01-1-month'] = array(
2873 'data' => array(
2874 'start_date' => '2012-01-01',
2875 'frequency_interval' => 1,
2876 'frequency_unit' => 'month',
2877 ),
2878 'receive_date' => '2012-01-01',
2879 'expected' => '2012-02-01 00:00:00',
2880 );
2881 $result[]['2012-01-01-1-month'] = array(
2882 'data' => array(
2883 'start_date' => '2012-01-01',
2884 'frequency_interval' => 1,
2885 'frequency_unit' => 'month',
2886 ),
2887 'receive_date' => '2012-02-29',
2888 'expected' => '2012-03-29 00:00:00',
2889 );
46f459f2 2890 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2891 'data' => array(
2892 'start_date' => '2012-01-01',
2893 'frequency_interval' => 1,
2894 'frequency_unit' => 'month',
2895 'next_sched_contribution_date' => '2012-02-29',
2896 ),
2897 'receive_date' => '2012-02-29 16:00:00',
2898 'expected' => '2012-03-29 00:00:00',
2899 );
050e11d5 2900 return $result;
91259407 2901 }
2902
294cc627 2903 /**
2904 * Test completing a pledge with the completeTransaction api..
2905 *
2906 * Note that we are creating a logged in user because email goes out from
2907 * that person.
2908 */
2909 public function testCompleteTransactionUpdatePledgePayment() {
9f9fa558 2910 $this->swapMessageTemplateForTestTemplate();
294cc627 2911 $mut = new CiviMailUtils($this, TRUE);
2912 $mut->clearMessages();
2913 $this->createLoggedInUser();
2914 $contributionID = $this->createPendingPledgeContribution();
2915 $this->callAPISuccess('contribution', 'completetransaction', array(
2916 'id' => $contributionID,
2917 'trxn_date' => '1 Feb 2013',
2918 ));
2919 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2920 'id' => $this->_ids['pledge'],
2921 ));
2922 $this->assertEquals('Completed', $pledge['pledge_status']);
2923
2924 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2925 'pledge_id' => $this->_ids['pledge'],
2926 'return' => 'status_id',
2927 ));
2928 $this->assertEquals(1, $status);
2929 $mut->checkMailLog(array(
9f9fa558
EM
2930 'amount:::500.00',
2931 'receive_date:::20130201000000',
76e8d9c4 2932 "receipt_date:::\n",
294cc627 2933 ));
2934 $mut->stop();
9f9fa558 2935 $this->revertTemplateToReservedTemplate();
294cc627 2936 }
2937
0efa8efe 2938 /**
eceb18cc 2939 * Test completing a transaction with an event via the API.
0efa8efe 2940 *
2941 * Note that we are creating a logged in user because email goes out from
2942 * that person
2943 */
00be9182 2944 public function testCompleteTransactionWithParticipantRecord() {
5896d037 2945 $mut = new CiviMailUtils($this, TRUE);
0efa8efe 2946 $mut->clearMessages();
71acd4bf 2947 $this->_individualId = $this->createLoggedInUser();
0efa8efe 2948 $contributionID = $this->createPendingParticipantContribution();
66d3f9be 2949 $this->callAPISuccess('contribution', 'completetransaction', array(
5896d037
TO
2950 'id' => $contributionID,
2951 )
0efa8efe 2952 );
5896d037 2953 $participantStatus = $this->callAPISuccessGetValue('participant', array(
92915c55
TO
2954 'id' => $this->_ids['participant'],
2955 'return' => 'participant_status_id',
2956 ));
0efa8efe 2957 $this->assertEquals(1, $participantStatus);
71acd4bf
JP
2958
2959 //Assert only three activities are created.
2960 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_individualId);
2961 $this->assertEquals(3, count($activities));
2962 $activityNames = array_count_values(CRM_Utils_Array::collect('activity_name', $activities));
b56b628c 2963 // record two activities before and after completing payment for Event registration
71acd4bf 2964 $this->assertEquals(2, $activityNames['Event Registration']);
b56b628c 2965 // update the original 'Contribution' activity created after completing payment
71acd4bf
JP
2966 $this->assertEquals(1, $activityNames['Contribution']);
2967
0efa8efe 2968 $mut->checkMailLog(array(
2969 'Annual CiviCRM meet',
2970 'Event',
afc59fef 2971 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
0efa8efe 2972 ));
66d3f9be
EM
2973 $mut->stop();
2974 }
2975
2976 /**
eceb18cc 2977 * Test membership is renewed when transaction completed.
66d3f9be 2978 */
00be9182 2979 public function testCompleteTransactionMembershipPriceSet() {
66d3f9be 2980 $this->createPriceSetWithPage('membership');
4ff927bc 2981 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2982 'name' => 'Grace',
2983 'return' => 'id')
2984 );
66d3f9be 2985 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
4ff927bc 2986 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2987 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2988 'membership_id' => $this->_ids['membership'],
2989 ));
2990 $this->assertEquals(1, $logs['count']);
2991 $this->assertEquals($stateOfGrace, $membership['status_id']);
d2460a89 2992 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
2993 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2994 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
4ff927bc 2995 $this->callAPISuccessGetSingle('LineItem', array(
2996 'entity_id' => $this->_ids['membership'],
2997 'entity_table' => 'civicrm_membership',
2998 ));
2999 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
3000 'membership_id' => $this->_ids['membership'],
3001 ));
3002 $this->assertEquals(2, $logs['count']);
3003 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
98f0683a
JP
3004 //Assert only three activities are created.
3005 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_ids['contact']);
3006 $this->assertEquals(3, count($activities));
3007 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
3008 $this->assertArrayHasKey('Contribution', $activityNames);
3009 $this->assertArrayHasKey('Membership Signup', $activityNames);
3010 $this->assertArrayHasKey('Change Membership Status', $activityNames);
66d3f9be
EM
3011 $this->cleanUpAfterPriceSets();
3012 }
3013
0f07bb06 3014 /**
3015 * Test if renewal activity is create after changing Pending contribution to Completed via offline
3016 */
3017 public function testPendingToCompleteContribution() {
c77986c0 3018 $this->createPriceSetWithPage('membership');
0f07bb06 3019 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
3020 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
66a1e31f 3021 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
b6d493f3
MD
3022 $activity = $this->callAPISuccess('Activity', 'get', array(
3023 'activity_type_id' => 'Membership Signup',
3024 'source_record_id' => $this->_ids['membership'],
3025 'status_id' => 'Scheduled',
3026 ));
3027 $this->assertEquals(1, $activity['count']);
0f07bb06 3028
3029 // change pending contribution to completed
3030 $form = new CRM_Contribute_Form_Contribution();
c77986c0 3031
0f07bb06 3032 $form->_params = array(
3033 'id' => $this->_ids['contribution'],
3034 'total_amount' => 20,
3035 'net_amount' => 20,
3036 'fee_amount' => 0,
3037 'financial_type_id' => 1,
0f07bb06 3038 'contact_id' => $this->_individualId,
3039 'contribution_status_id' => 1,
3040 'billing_middle_name' => '',
3041 'billing_last_name' => 'Adams',
3042 'billing_street_address-5' => '790L Lincoln St S',
3043 'billing_city-5' => 'Maryknoll',
3044 'billing_state_province_id-5' => 1031,
3045 'billing_postal_code-5' => 10545,
3046 'billing_country_id-5' => 1228,
3047 'frequency_interval' => 1,
3048 'frequency_unit' => 'month',
3049 'installments' => '',
3050 'hidden_AdditionalDetail' => 1,
3051 'hidden_Premium' => 1,
3052 'from_email_address' => '"civi45" <civi45@civicrm.com>',
3053 'receipt_date' => '',
3054 'receipt_date_time' => '',
3055 'payment_processor_id' => $this->paymentProcessorID,
3056 'currency' => 'USD',
3057 'contribution_page_id' => $this->_ids['contribution_page'],
3058 'contribution_mode' => 'membership',
3059 'source' => 'Membership Signup and Renewal',
3060 );
c77986c0 3061
3062 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
3063
66a1e31f 3064 // Case 2: After successful payment for Pending backoffice there are three activities created
b6d493f3 3065 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
767d3e2e
MD
3066 $activity = $this->callAPISuccess('Activity', 'get', array(
3067 'activity_type_id' => 'Membership Signup',
b6d493f3
MD
3068 'source_record_id' => $this->_ids['membership'],
3069 'status_id' => 'Completed',
767d3e2e
MD
3070 ));
3071 $this->assertEquals(1, $activity['count']);
b6d493f3 3072 // 2.b Contribution activity created to record successful payment
767d3e2e 3073 $activity = $this->callAPISuccess('Activity', 'get', array(
b6d493f3 3074 'activity_type_id' => 'Contribution',
767d3e2e 3075 'source_record_id' => $this->_ids['contribution'],
b6d493f3 3076 'status_id' => 'Completed',
767d3e2e 3077 ));
b6d493f3 3078 $this->assertEquals(1, $activity['count']);
fde55343 3079
b6d493f3 3080 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
d2460a89 3081 $activity = $this->callAPISuccess('Activity', 'get', array(
b6d493f3
MD
3082 'activity_type_id' => 'Change Membership Status',
3083 'source_record_id' => $this->_ids['membership'],
3084 'status_id' => 'Completed',
d2460a89
MD
3085 ));
3086 $this->assertEquals(1, $activity['count']);
b6d493f3 3087 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
fde55343
JP
3088
3089 //Create another pending contribution for renewal
3090 $contribution = $this->callAPISuccess('contribution', 'create', array(
3091 'domain_id' => 1,
3092 'contact_id' => $this->_ids['contact'],
3093 'receive_date' => date('Ymd'),
3094 'total_amount' => 20.00,
3095 'financial_type_id' => 1,
3096 'payment_instrument_id' => 'Credit Card',
3097 'non_deductible_amount' => 10.00,
3098 'trxn_id' => 'rdhfi88',
3099 'invoice_id' => 'dofhiewuyr',
3100 'source' => 'SSF',
3101 'contribution_status_id' => 2,
3102 'contribution_page_id' => $this->_ids['contribution_page'],
c77986c0 3103 // We can't rely on contribution api to link line items correctly to membership
3104 'skipLineItem' => TRUE,
fde55343
JP
3105 'api.membership_payment.create' => array('membership_id' => $this->_ids['membership']),
3106 ));
3107
3108 $this->callAPISuccess('line_item', 'create', array(
3109 'entity_id' => $contribution['id'],
3110 'entity_table' => 'civicrm_contribution',
3111 'contribution_id' => $contribution['id'],
3112 'price_field_id' => $this->_ids['price_field'][0],
3113 'qty' => 1,
3114 'unit_price' => 20,
3115 'line_total' => 20,
3116 'financial_type_id' => 1,
c77986c0 3117 'price_field_value_id' => $this->_ids['price_field_value']['cont'],
3118 ));
3119 $this->callAPISuccess('line_item', 'create', array(
3120 'entity_id' => $this->_ids['membership'],
3121 'entity_table' => 'civicrm_membership',
3122 'contribution_id' => $contribution['id'],
3123 'price_field_id' => $this->_ids['price_field'][0],
3124 'qty' => 1,
3125 'unit_price' => 20,
3126 'line_total' => 20,
3127 'financial_type_id' => 1,
fde55343 3128 'price_field_value_id' => $this->_ids['price_field_value'][0],
c77986c0 3129 'membership_type_id' => $this->_ids['membership_type'],
fde55343
JP
3130 ));
3131
3132 //Update it to Failed.
3133 $form->_params['id'] = $contribution['id'];
3134 $form->_params['contribution_status_id'] = 4;
c77986c0 3135
3136 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
fde55343
JP
3137 //Existing membership should not get updated to expired.
3138 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
3139 $this->assertNotEquals($membership['status_id'], 4);
0f07bb06 3140 }
3141
66d3f9be 3142 /**
eceb18cc 3143 * Test membership is renewed when transaction completed.
66d3f9be 3144 */
00be9182 3145 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
66d3f9be
EM
3146 $this->createPriceSetWithPage('membership');
3147 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
6c6e6187 3148 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
66d3f9be
EM
3149 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
3150 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3151 $this->cleanUpAfterPriceSets();
3152 }
3153
00be9182 3154 public function cleanUpAfterPriceSets() {
1cf3c2b1 3155 $this->quickCleanUpFinancialEntities();
66d3f9be 3156 $this->contactDelete($this->_ids['contact']);
66d3f9be
EM
3157 }
3158
66d3f9be 3159 /**
eceb18cc 3160 * Set up a pending transaction with a specific price field id.
1e52837d 3161 *
100fef9d 3162 * @param int $priceFieldValueID
66d3f9be 3163 */
767d3e2e 3164 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
66d3f9be
EM
3165 $contactID = $this->individualCreate();
3166 $membership = $this->callAPISuccess('membership', 'create', array(
3167 'contact_id' => $contactID,
3168 'membership_type_id' => $this->_ids['membership_type'],
3169 'start_date' => 'yesterday - 1 year',
3170 'end_date' => 'yesterday',
4ff927bc 3171 'join_date' => 'yesterday - 1 year',
66d3f9be 3172 ));
767d3e2e 3173 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
66d3f9be
EM
3174 'domain_id' => 1,
3175 'contact_id' => $contactID,
3176 'receive_date' => date('Ymd'),
0f07bb06 3177 'total_amount' => 20.00,
66d3f9be
EM
3178 'financial_type_id' => 1,
3179 'payment_instrument_id' => 'Credit Card',
3180 'non_deductible_amount' => 10.00,
98f0683a
JP
3181 'trxn_id' => 'jdhfi' . rand(1, 100),
3182 'invoice_id' => 'djfhiew' . rand(5, 100),
66d3f9be
EM
3183 'source' => 'SSF',
3184 'contribution_status_id' => 2,
3185 'contribution_page_id' => $this->_ids['contribution_page'],
3186 'api.membership_payment.create' => array('membership_id' => $membership['id']),
767d3e2e 3187 ), $contriParams));
66d3f9be
EM
3188
3189 $this->callAPISuccess('line_item', 'create', array(
3190 'entity_id' => $contribution['id'],
3191 'entity_table' => 'civicrm_contribution',
1274acef 3192 'contribution_id' => $contribution['id'],
66d3f9be
EM
3193 'price_field_id' => $this->_ids['price_field'][0],
3194 'qty' => 1,
3195 'unit_price' => 20,
3196 'line_total' => 20,
3197 'financial_type_id' => 1,
3198 'price_field_value_id' => $priceFieldValueID,
3199 ));
3200 $this->_ids['contact'] = $contactID;
3201 $this->_ids['contribution'] = $contribution['id'];
3202 $this->_ids['membership'] = $membership['id'];
0efa8efe 3203 }
3204
2f45e1c2 3205 /**
eceb18cc 3206 * Test sending a mail via the API.
6a488035 3207 */
00be9182 3208 public function testSendMail() {
5896d037 3209 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 3210 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
858d0bf8 3211 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
3212 'id' => $contribution['id'],
3213 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
3214 )
3215 );
6a488035
TO
3216 $mut->checkMailLog(array(
3217 '$ 100.00',
3218 'Contribution Information',
3219 'Please print this confirmation for your records',
3220 ), array(
21dfd5f5 3221 'Event',
6a488035
TO
3222 )
3223 );
128d44a1 3224
3225 $this->checkCreditCardDetails($mut, $contribution['id']);
6a488035
TO
3226 $mut->stop();
3227 }
3228
dbacb875
AS
3229 /**
3230 * Test sending a mail via the API.
3231 * This simulates webform_civicrm using pay later contribution page
3232 */
3233 public function testSendconfirmationPayLater() {
3234 $mut = new CiviMailUtils($this, TRUE);
3235
3236 // Create contribution page
3237 $pageParams = array(
3238 'title' => 'Webform Contributions',
3239 'financial_type_id' => 1,
3240 'contribution_type_id' => 1,
3241 'is_confirm_enabled' => 1,
3242 'is_pay_later' => 1,
3243 'pay_later_text' => 'I will send payment by cheque',
3244 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3245 );
3246 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3247
3248 // Create pay later contribution
3249 $contribParams = array(
3250 'contact_id' => $this->_individualId,
3251 'financial_type_id' => 1,
3252 'is_pay_later' => 1,
3253 'contribution_status_id' => 2,
3254 'contribution_page_id' => $contributionPage['id'],
3255 'total_amount' => '10.00',
3256 );
3257 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3258
3259 // Create line item
3260 $lineItemParams = array(
3261 'contribution_id' => $contribution['id'],
3262 'entity_id' => $contribution['id'],
3263 'entity_table' => 'civicrm_contribution',
3264 'label' => 'My lineitem label',
3265 'qty' => 1,
3266 'unit_price' => "10.00",
3267 'line_total' => "10.00",
3268 );
3269 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3270
3271 // Create email
3272 try {
3273 civicrm_api3('contribution', 'sendconfirmation', array(
3274 'id' => $contribution['id'],
3275 'receipt_from_email' => 'api@civicrm.org',
3276 )
3277 );
717fdb8a
AS
3278 }
3279 catch (Exception $e) {
dbacb875
AS
3280 // Need to figure out how to stop this some other day
3281 // We don't care about the Payment Processor because this is Pay Later
3282 // The point of this test is to check we get the pay_later version of the mail
3283 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3284 throw $e;
3285 }
3286 }
3287
3288 // Retrieve mail & check it has the pay_later_receipt info
3289 $mut->getMostRecentEmail('raw');
3290 $mut->checkMailLog(array(
717fdb8a 3291 (string) $contribParams['total_amount'],
dbacb875
AS
3292 $pageParams['pay_later_receipt'],
3293 ), array(
3294 'Event',
3295 )
3296 );
3297 $mut->stop();
3298 }
3299
3300
128d44a1 3301 /**
3302 * Check credit card details in sent mail via API
3303 *
3304 * @param $mut obj CiviMailUtils instance
3305 * @param int $contributionID Contribution ID
3306 *
3307 */
3308 public function checkCreditCardDetails($mut, $contributionID) {
3309 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3310 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3311 'id' => $contributionID,
3312 'receipt_from_email' => 'api@civicrm.org',
3313 'payment_processor_id' => $this->paymentProcessorID,
3314 )
3315 );
3316 $mut->checkMailLog(array(
3317 'Credit Card Information', // credit card header
3318 'Billing Name and Address', // billing header
3319 'anthony_anderson@civicrm.org', // billing name
3320 ), array(
3321 'Event',
3322 )
3323 );
3324 }
3325
2f45e1c2 3326 /**
eceb18cc 3327 * Test sending a mail via the API.
6a488035 3328 */
00be9182 3329 public function testSendMailEvent() {
5896d037 3330 $mut = new CiviMailUtils($this, TRUE);
6c6e6187 3331 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2f45e1c2 3332 $event = $this->eventCreate(array(
6a488035
TO
3333 'is_email_confirm' => 1,
3334 'confirm_from_email' => 'test@civicrm.org',
3335 ));
3336 $this->_eventID = $event['id'];
3337 $participantParams = array(
3338 'contact_id' => $this->_individualId,
3339 'event_id' => $this->_eventID,
3340 'status_id' => 1,
3341 'role_id' => 1,
3342 // to ensure it matches later on
3343 'register_date' => '2007-07-21 00:00:00',
3344 'source' => 'Online Event Registration: API Testing',
4ab7d517 3345
6a488035 3346 );
2f45e1c2 3347 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3348 $this->callAPISuccess('participant_payment', 'create', array(
6a488035
TO
3349 'participant_id' => $participant['id'],
3350 'contribution_id' => $contribution['id'],
2f45e1c2 3351 ));
66d3f9be 3352 $this->callAPISuccess('contribution', 'sendconfirmation', array(
5896d037
TO
3353 'id' => $contribution['id'],
3354 'receipt_from_email' => 'api@civicrm.org',
6a488035
TO
3355 )
3356 );
3357
6a488035
TO
3358 $mut->checkMailLog(array(
3359 'Annual CiviCRM meet',
3360 'Event',
3361 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
6c6e6187 3362 ), array()
6a488035
TO
3363 );
3364 $mut->stop();
3365 }
3366
4302618d 3367 /**
1e52837d
EM
3368 * This function does a GET & compares the result against the $params.
3369 *
3370 * Use as a double check on Creates.
3371 *
3372 * @param array $params
3373 * @param int $id
67f947ac 3374 * @param bool $delete
6c6e6187 3375 */
1e52837d 3376 public function contributionGetnCheck($params, $id, $delete = TRUE) {
6a488035 3377
4ab7d517 3378 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
6a488035 3379 'id' => $id,
4ab7d517 3380
5896d037 3381 ));
6a488035
TO
3382
3383 if ($delete) {
4ab7d517 3384 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
6a488035 3385 }
2bfae985 3386 $this->assertAPISuccess($contribution, 0);
6a488035
TO
3387 $values = $contribution['values'][$contribution['id']];
3388 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3389 // this is not returned in id format
3390 unset($params['payment_instrument_id']);
3391 $params['contribution_source'] = $params['source'];
3392 unset($params['source']);
3393 foreach ($params as $key => $value) {
22f80e87 3394 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
6a488035
TO
3395 }
3396 }
3397
294cc627 3398 /**
3399 * Create a pending contribution & linked pending pledge record.
3400 */
3401 public function createPendingPledgeContribution() {
3402
3403 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
3404 $this->_ids['pledge'] = $pledgeID;
3405 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
3406 'contribution_status_id' => 'Pending',
3407 'total_amount' => 500,
3408 ))
3409 );
3410 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3411 'options' => array('limit' => 1),
3412 'return' => 'id',
3413 ));
3414 $this->callAPISuccess('PledgePayment', 'create', array(
3415 'id' => $paymentID,
3416 'contribution_id' =>
3417 $contribution['id'],
3418 'status_id' => 'Pending',
3419 'scheduled_amount' => 500,
3420 ));
3421
3422 return $contribution['id'];
3423 }
3424
0efa8efe 3425 /**
1e52837d 3426 * Create a pending contribution & linked pending participant record (along with an event).
0efa8efe 3427 */
5896d037 3428 public function createPendingParticipantContribution() {
6c6e6187 3429 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
71acd4bf 3430 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId));
5896d037 3431 $this->_ids['participant'] = $participantID;
71acd4bf 3432 $params = array_merge($this->_params, array('contact_id' => $this->_individualId, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
6c6e6187 3433 $contribution = $this->callAPISuccess('contribution', 'create', $params);
5896d037 3434 $this->callAPISuccess('participant_payment', 'create', array(
92915c55
TO
3435 'contribution_id' => $contribution['id'],
3436 'participant_id' => $participantID,
3437 ));
858d0bf8 3438 $this->callAPISuccess('line_item', 'get', array(
0efa8efe 3439 'entity_id' => $contribution['id'],
3440 'entity_table' => 'civicrm_contribution',
3441 'api.line_item.create' => array(
3442 'entity_id' => $participantID,
3443 'entity_table' => 'civicrm_participant',
3444 ),
3445 ));
3446 return $contribution['id'];
3447 }
3448
4cbe18b8 3449 /**
1e52837d
EM
3450 * Get financial transaction amount.
3451 *
100fef9d 3452 * @param int $contId
4cbe18b8
EM
3453 *
3454 * @return null|string
f4d89200 3455 */
2da40d21 3456 public function _getFinancialTrxnAmount($contId) {
6c6e6187 3457 $query = "SELECT
6a488035
TO
3458 SUM( ft.total_amount ) AS total
3459 FROM civicrm_financial_trxn AS ft
3460 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3461 WHERE ceft.entity_table = 'civicrm_contribution'
3462 AND ceft.entity_id = {$contId}";
3463
6c6e6187
TO
3464 $result = CRM_Core_DAO::singleValueQuery($query);
3465 return $result;
3466 }
6a488035 3467
4cbe18b8 3468 /**
100fef9d 3469 * @param int $contId
4cbe18b8
EM
3470 *
3471 * @return null|string
f4d89200 3472 */
2da40d21 3473 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
3474 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3475 $query = "SELECT
6a488035
TO
3476 SUM(amount)
3477 FROM civicrm_financial_item
3478 WHERE entity_table = 'civicrm_line_item'
3479 AND entity_id = {$lineItem}";
6c6e6187
TO
3480 $result = CRM_Core_DAO::singleValueQuery($query);
3481 return $result;
3482 }
6a488035 3483
4cbe18b8 3484 /**
100fef9d 3485 * @param int $contId
4cbe18b8
EM
3486 * @param $context
3487 */
00be9182 3488 public function _checkFinancialItem($contId, $context) {
6c6e6187
TO
3489 if ($context != 'paylater') {
3490 $params = array(
5896d037
TO
3491 'entity_id' => $contId,
3492 'entity_table' => 'civicrm_contribution',
6c6e6187
TO
3493 );
3494 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
3495 $entityParams = array(
6a488035
TO
3496 'financial_trxn_id' => $trxn['financial_trxn_id'],
3497 'entity_table' => 'civicrm_financial_item',
6c6e6187
TO
3498 );
3499 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3500 $params = array(
6a488035 3501 'id' => $entityTrxn['entity_id'],
6c6e6187
TO
3502 );
3503 }
3504 if ($context == 'paylater') {
3505 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
3506 foreach ($lineItems as $key => $item) {
3507 $params = array(
5896d037
TO
3508 'entity_id' => $key,
3509 'entity_table' => 'civicrm_line_item',
6c6e6187
TO
3510 );
3511 $compareParams = array('status_id' => 1);
3512 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3513 }
3514 }
3515 elseif ($context == 'refund') {
3516 $compareParams = array(
5896d037
TO
3517 'status_id' => 1,
3518 'financial_account_id' => 1,
3519 'amount' => -100,
6c6e6187
TO
3520 );
3521 }
3522 elseif ($context == 'cancelPending') {
3523 $compareParams = array(
5896d037
TO
3524 'status_id' => 3,
3525 'financial_account_id' => 1,
3526 'amount' => -100,
6c6e6187
TO
3527 );
3528 }
3529 elseif ($context == 'changeFinancial') {
3530 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3531 $params = array(
5896d037
TO
3532 'entity_id' => $lineKey,
3533 'amount' => -100,
6c6e6187
TO
3534 );
3535 $compareParams = array(
5896d037 3536 'financial_account_id' => 1,
6c6e6187
TO
3537 );
3538 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3539 $params = array(
5896d037
TO
3540 'financial_account_id' => 3,
3541 'entity_id' => $lineKey,
6c6e6187
TO
3542 );
3543 $compareParams = array(
5896d037 3544 'amount' => 100,
6c6e6187
TO
3545 );
3546 }
3547 if ($context != 'paylater') {
3548 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3549 }
3550 }
6a488035 3551
b0e806fa 3552 /**
3553 * Check correct financial transaction entries were created for the change in payment instrument.
3554 *
3555 * @param int $contributionID
3556 * @param int $originalInstrumentID
3557 * @param int $newInstrumentID
3558 */
3559 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3560
3561 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3562
3563 $originalTrxnParams = array(
3564 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3565 'payment_instrument_id' => $originalInstrumentID,
3566 'amount' => $amount,
3567 'status_id' => 1,
3568 );
3569
3570 $reversalTrxnParams = array(
3571 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3572 'payment_instrument_id' => $originalInstrumentID,
3573 'amount' => -$amount,
3574 'status_id' => 1,
3575 );
3576
3577 $newTrxnParams = array(
3578 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($newInstrumentID),
3579 'payment_instrument_id' => $newInstrumentID,
3580 'amount' => $amount,
3581 'status_id' => 1,
3582 );
3583
3584 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3585 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3586 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3587
3588 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3589 'id' => $entityFinancialTrxn['financial_trxn_id'],
3590 ));
3591 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3592 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3593 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3594 $this->assertEquals(0, $financialTrxn['fee_amount']);
3595 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3596 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3597
3598 // Generic checks.
3599 $this->assertEquals(1, $financialTrxn['is_payment']);
3600 $this->assertEquals('USD', $financialTrxn['currency']);
3601 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3602 }
3603 }
3604
4cbe18b8 3605 /**
52da5b1e 3606 * Check financial transaction.
3607 *
3608 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3609 *
4ff927bc 3610 * @param array $contribution
3611 * @param string $context
100fef9d 3612 * @param int $instrumentId
52da5b1e 3613 * @param array $extraParams
4cbe18b8 3614 */
797d4c52 3615 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
b0e806fa 3616 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3617 $trxn = array_pop($financialTrxns);
3618
6c6e6187 3619 $params = array(
5896d037 3620 'id' => $trxn['financial_trxn_id'],
6c6e6187
TO
3621 );
3622 if ($context == 'payLater') {
6c6e6187 3623 $compareParams = array(
5896d037 3624 'status_id' => 1,
876b8ab0 3625 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
6c6e6187
TO
3626 );
3627 }
3628 elseif ($context == 'refund') {
3629 $compareParams = array(
5896d037
TO
3630 'to_financial_account_id' => 6,
3631 'total_amount' => -100,
3632 'status_id' => 7,
b7990bb6 3633 'trxn_date' => '2015-01-01 09:00:00',
797d4c52 3634 'trxn_id' => 'the refund',
6c6e6187
TO
3635 );
3636 }
3637 elseif ($context == 'cancelPending') {
3638 $compareParams = array(
ed4d0aea 3639 'to_financial_account_id' => 7,
5896d037
TO
3640 'total_amount' => -100,
3641 'status_id' => 3,
6c6e6187
TO
3642 );
3643 }
3644 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
b0e806fa 3645 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3646 // It does the same thing with greater readability.
3647 // @todo remove handling for
3648
6c6e6187 3649 $entityParams = array(
5896d037
TO
3650 'entity_id' => $contribution['id'],
3651 'entity_table' => 'civicrm_contribution',
3652 'amount' => -100,
6c6e6187
TO
3653 );
3654 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3655 $trxnParams1 = array(
6a488035 3656 'id' => $trxn['financial_trxn_id'],
6c6e6187 3657 );
122250ec
SL
3658 if (empty($extraParams)) {
3659 $compareParams = array(
3660 'total_amount' => -100,
3661 'status_id' => 1,
3662 );
3663 }
3664 else {
3665 $compareParams = array(
3666 'total_amount' => 100,
3667 'status_id' => 1,
3668 );
3669 }
6c6e6187
TO
3670 if ($context == 'paymentInstrument') {
3671 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3672 $compareParams['payment_instrument_id'] = $instrumentId;
3673 }
3674 else {
3675 $compareParams['to_financial_account_id'] = 12;
3676 }
5ca657dd 3677 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3678 $compareParams['total_amount'] = 100;
6c6e6187
TO
3679 }
3680
797d4c52 3681 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
6c6e6187 3682 }
6a488035 3683
4cbe18b8
EM
3684 /**
3685 * @return mixed
3686 */
5896d037 3687 public function _addPaymentInstrument() {
6c6e6187
TO
3688 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3689 $optionParams = array(
5896d037
TO
3690 'option_group_id' => $gId,
3691 'label' => 'Test Card',
3692 'name' => 'Test Card',
3693 'value' => '6',
3694 'weight' => '6',
3695 'is_active' => 1,
6c6e6187
TO
3696 );
3697 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3698 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3699 $financialParams = array(
5896d037
TO
3700 'entity_table' => 'civicrm_option_value',
3701 'entity_id' => $optionValue['id'],
3702 'account_relationship' => $relationTypeId,
3703 'financial_account_id' => 7,
6c6e6187
TO
3704 );
3705 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
3706 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3707 return $optionValue['values'][$optionValue['id']]['value'];
3708 }
6a488035 3709
02a9c0a4 3710 public function _deletedAddedPaymentInstrument() {
3711 $result = $this->callAPISuccess('OptionValue', 'get', array(
3712 'option_group_id' => 'payment_instrument',
3713 'name' => 'Test Card',
3714 'value' => '6',
3715 'is_active' => 1,
3716 ));
3717 if ($id = CRM_Utils_Array::value('id', $result)) {
3718 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3719 }
3720 }
3721
3c49d90c 3722 /**
3723 * Set up the basic recurring contribution for tests.
3724 *
3725 * @param array $generalParams
3726 * Parameters that can be merged into the recurring AND the contribution.
7f4ef731 3727 *
3728 * @param array $recurParams
3729 * Parameters to merge into the recur only.
3c49d90c 3730 *
3731 * @return array|int
3732 */
7f4ef731 3733 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3c49d90c 3734 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3735 'contact_id' => $this->_individualId,
3736 'installments' => '12',
3737 'frequency_interval' => '1',
3738 'amount' => '100',
3739 'contribution_status_id' => 1,
3740 'start_date' => '2012-01-01 00:00:00',
3741 'currency' => 'USD',
3742 'frequency_unit' => 'month',
3743 'payment_processor_id' => $this->paymentProcessorID,
7f4ef731 3744 ), $generalParams, $recurParams));
3c49d90c 3745 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3746 $this->_params,
3747 array(
3748 'contribution_recur_id' => $contributionRecur['id'],
3749 ), $generalParams)
3750 );
3751 return $originalContribution;
3752 }
3753
d2334242
PH
3754 /**
3755 * Set up a basic auto-renew membership for tests.
3756 *
3757 * @param array $generalParams
3758 * Parameters that can be merged into the recurring AND the contribution.
3759 *
3760 * @param array $recurParams
3761 * Parameters to merge into the recur only.
3762 *
3763 * @return array|int
3764 */
3765 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3766 $newContact = $this->callAPISuccess('Contact', 'create', array(
7c3f3d59 3767 'contact_type' => 'Individual',
3768 'sort_name' => 'McTesterson, Testy',
3769 'display_name' => 'Testy McTesterson',
3770 'preferred_language' => 'en_US',
3771 'preferred_mail_format' => 'Both',
3772 'first_name' => 'Testy',
3773 'last_name' => 'McTesterson',
3774 'contact_is_deleted' => '0',
3775 'email_id' => '4',
3776 'email' => 'tmctesterson@example.com',
3777 'on_hold' => '0',
d2334242
PH
3778 ));
3779 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3780 'domain_id' => "Default Domain Name",
3781 'member_of_contact_id' => 1,
3782 'financial_type_id' => "Member Dues",
3783 'duration_unit' => "month",
3784 'duration_interval' => 1,
5b1b8db2 3785 'period_type' => 'rolling',
d2334242
PH
3786 'name' => "Standard Member",
3787 'minimum_fee' => 100,
3788 ));
3789 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
76e80087 3790 'contact_id' => $newContact['id'],
d2334242
PH
3791 'installments' => '12',
3792 'frequency_interval' => '1',
3793 'amount' => '100',
3794 'contribution_status_id' => 1,
3795 'start_date' => '2012-01-01 00:00:00',
3796 'currency' => 'USD',
3797 'frequency_unit' => 'month',
3798 'payment_processor_id' => $this->paymentProcessorID,
3799 ), $generalParams, $recurParams));
7c3f3d59 3800
3801 $membership = $this->callAPISuccess('membership', 'create', array(
3802 'contact_id' => $newContact['id'],
3803 'contribution_recur_id' => $contributionRecur['id'],
3804 'financial_type_id' => "Member Dues",
3805 'membership_type_id' => $membershipType['id'],
3806 'num_terms' => 1,
3807 'skipLineItem' => TRUE,
3808 ));
3809
3810 CRM_Price_BAO_LineItem::getLineItemArray($this->_params, NULL, 'membership', $membershipType['id']);
d2334242
PH
3811 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3812 $this->_params,
3813 array(
3814 'contact_id' => $newContact['id'],
3815 'contribution_recur_id' => $contributionRecur['id'],
3816 'financial_type_id' => "Member Dues",
3817 'contribution_status_id' => 1,
3818 'invoice_id' => uniqid(),
3819 ), $generalParams)
3820 );
7c3f3d59 3821 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3822 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3823 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3824 $this->callAPISuccess('LineItem', 'getsingle', array());
3825 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
d2334242 3826
d2334242
PH
3827 return array($originalContribution, $membership);
3828 }
893a550c 3829 /**
3830 * Set up a repeat transaction.
3831 *
3832 * @param array $recurParams
3833 *
3834 * @return array
3835 */
19893cf2 3836 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
893a550c 3837 $paymentProcessorID = $this->paymentProcessorCreate();
3838 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3839 'contact_id' => $this->_individualId,
3840 'installments' => '12',
3841 'frequency_interval' => '1',
3842 'amount' => '500',
3843 'contribution_status_id' => 1,
3844 'start_date' => '2012-01-01 00:00:00',
3845 'currency' => 'USD',
3846 'frequency_unit' => 'month',
3847 'payment_processor_id' => $paymentProcessorID,
3848 ), $recurParams));
0e6ccb2e 3849
7150b1c8 3850 $originalContribution = '';
0e6ccb2e 3851 if ($flag == 'multiple') {
7150b1c8 3852 // CRM-19309 create a contribution + also add in line_items (plural):
19893cf2 3853 $params = array_merge($this->_params, $contributionParams);
0e6ccb2e 3854 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
19893cf2 3855 $params,
0e6ccb2e
K
3856 array(
3857 'contribution_recur_id' => $contributionRecur['id'],
3858 'skipLineItem' => 1,
3859 'api.line_item.create' => array(
3860 array(
3861 'price_field_id' => 1,
3862 'qty' => 2,
3863 'line_total' => '20',
3864 'unit_price' => '10',
3865 'financial_type_id' => 1,
3866 ),
3867 array(
3868 'price_field_id' => 1,
3869 'qty' => 1,
3870 'line_total' => '80',
3871 'unit_price' => '80',
3872 'financial_type_id' => 2,
3873 ),
3874 ),
3875 )
3876 )
3877 );
3878 }
3879 elseif ($flag == 'single') {
19893cf2
SL
3880 $params = array_merge($this->_params, array('contribution_recur_id' => $contributionRecur['id']));
3881 $params = array_merge($params, $contributionParams);
3882 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
0e6ccb2e 3883 }
f69a9ac3 3884 $originalContribution['payment_processor_id'] = $paymentProcessorID;
893a550c 3885 return $originalContribution;
3886 }
3887
ec7e3954
E
3888 /**
3889 * Common set up routine.
3890 *
3891 * @return array
3892 */
3893 protected function setUpForCompleteTransaction() {
3894 $this->mut = new CiviMailUtils($this, TRUE);
3895 $this->createLoggedInUser();
3896 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3897 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3898 return $contribution;
3899 }
3900
9c01d961
SL
3901 /**
3902 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3903 */
3904 public function testRepeatTransactionWithNonCreditCardDefault() {
3905 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3906 'contact_id' => $this->_individualId,
3907 'installments' => '12',
3908 'frequency_interval' => '1',
3909 'amount' => '100',
3910 'contribution_status_id' => 1,
3911 'start_date' => '2012-01-01 00:00:00',
3912 'currency' => 'USD',
3913 'frequency_unit' => 'month',
3914 'payment_processor_id' => $this->paymentProcessorID,
3915 ));
3916 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3917 $this->_params,
4f0fadfa 3918 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
9c01d961
SL
3919 );
3920 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
3921 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3922 'contribution_status_id' => 'Completed',
3923 'trxn_id' => uniqid(),
3924 'original_contribution_id' => $contribution1,
3925 ));
3926 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3927 $this->quickCleanUpFinancialEntities();
3928 }
3929
ee63135d 3930 /**
3931 * CRM-20008 Tests repeattransaction creates pending membership.
3932 */
37f29fcf 3933 public function testRepeatTransactionMembershipCreatePendingContribution() {
ee63135d 3934 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3935 $this->callAPISuccess('membership', 'create', array(
3936 'id' => $membership['id'],
3937 'end_date' => 'yesterday',
3938 'status_id' => 'Expired',
3939 ));
3940 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3941 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3942 'contribution_status_id' => 'Pending',
3943 'trxn_id' => uniqid(),
3944 ));
3945 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3946 'id' => $membership['id'],
3947 'return' => 'status_id',
3948 ));
3949
3950 // Let's see if the membership payments got created while we're at it.
3951 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
37f29fcf 3952 'membership_id' => $membership['id'],
ee63135d 3953 ));
3954 $this->assertEquals(2, $membershipPayments['count']);
3955
3956 $this->assertEquals('Expired', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3957 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3958 $membership = $this->callAPISuccessGetSingle('membership', array(
3959 'id' => $membership['id'],
3960 'return' => 'status_id, end_date',
3961 ));
37f29fcf 3962 $this->assertEquals('New', CRM_Core_PseudoConstant::getName('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
ee63135d 3963 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3964
3965 $this->quickCleanUpFinancialEntities();
3966 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3967 }
3968
cefed6df
SL
3969 /**
3970 * Test sending a mail via the API.
3971 */
3972 public function testSendMailWithAPISetFromDetails() {
3973 $mut = new CiviMailUtils($this, TRUE);
3974 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3975 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3976 'id' => $contribution['id'],
3977 'receipt_from_email' => 'api@civicrm.org',
3978 'receipt_from_name' => 'CiviCRM LLC',
3979 ));
3980 $mut->checkMailLog(array(
3981 'From: CiviCRM LLC <api@civicrm.org>',
3982 'Contribution Information',
3983 'Please print this confirmation for your records',
3984 ), array(
3985 'Event',
3986 )
3987 );
3988 $mut->stop();
3989 }
3990
3991 /**
3992 * Test sending a mail via the API.
3993 */
3994 public function testSendMailWithNoFromSetFallToDomain() {
3995 $this->createLoggedInUser();
3996 $mut = new CiviMailUtils($this, TRUE);
3997 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3998 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3999 'id' => $contribution['id'],
4000 ));
4001 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
4002 $mut->checkMailLog(array(
4003 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4004 'Contribution Information',
4005 'Please print this confirmation for your records',
4006 ), array(
4007 'Event',
4008 )
4009 );
4010 $mut->stop();
4011 }
4012
4013 /**
4014 * Test sending a mail via the API.
4015 */
4016 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
4017 $this->createLoggedInUser();
4018 $mut = new CiviMailUtils($this, TRUE);
4019 $contribution = $this->setUpRepeatTransaction(array(), 'single');
4020 $this->callAPISuccess('contribution', 'repeattransaction', array(
4021 'contribution_status_id' => 'Completed',
4022 'trxn_id' => uniqid(),
4023 'original_contribution_id' => $contribution,
4024 ));
4025 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
4026 $mut->checkMailLog(array(
4027 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
4028 'Contribution Information',
4029 'Please print this confirmation for your records',
4030 ), array(
4031 'Event',
4032 )
4033 );
4034 $mut->stop();
4035 }
4036
4037 /**
4038 * Test sending a mail via the API.
4039 */
4040 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
4041 $mut = new CiviMailUtils($this, TRUE);
4042 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
cefed6df
SL
4043 $paymentProcessorID = $this->paymentProcessorCreate();
4044 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
4045 'contact_id' => $this->_individualId,
4046 'installments' => '12',
4047 'frequency_interval' => '1',
4048 'amount' => '500',
4049 'contribution_status_id' => 1,
4050 'start_date' => '2012-01-01 00:00:00',
4051 'currency' => 'USD',
4052 'frequency_unit' => 'month',
4053 'payment_processor_id' => $paymentProcessorID,
4054 ));
4055 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
4056 $this->_params,
4057 array(
4058 'contribution_recur_id' => $contributionRecur['id'],
4059 'contribution_page_id' => $contributionPage['id']))
4060 );
4061 $this->callAPISuccess('contribution', 'repeattransaction', array(
4062 'contribution_status_id' => 'Completed',
4063 'trxn_id' => uniqid(),
4064 'original_contribution_id' => $originalContribution,
4065 )
4066 );
4067 $mut->checkMailLog(array(
4068 'From: CiviCRM LLC <contributionpage@civicrm.org>',
4069 'Contribution Information',
4070 'Please print this confirmation for your records',
4071 ), array(
4072 'Event',
4073 )
4074 );
4075 $mut->stop();
4076 }
4077
4fb4e64f
SL
4078 /**
4079 * Test sending a mail via the API.
4080 */
4081 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4082 $mut = new CiviMailUtils($this, TRUE);
4083 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
4084 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
4085 foreach ($fromEmail['values'] as $from) {
4086 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
4087 }
4088 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
4089 $this->callAPISuccess('contribution', 'repeattransaction', array(
4090 'contribution_status_id' => 'Completed',
4091 'trxn_id' => uniqid(),
4092 'original_contribution_id' => $originalContribution,
4093 )
4094 );
4095 $mut->checkMailLog(array(
4096 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4097 'Contribution Information',
4098 'Please print this confirmation for your records',
4099 ), array(
4100 'Event',
4101 )
4102 );
4103 $mut->stop();
4104 }
4105
d891a273 4106 /**
4107 * Create a Contribution Page with is_email_receipt = TRUE.
4108 *
4109 * @param array $params
4110 * Params to overwrite with.
4111 *
4112 * @return array|int
4113 */
4114 protected function createReceiptableContributionPage($params = array()) {
4115 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
4116 'receipt_from_name' => 'Mickey Mouse',
4117 'receipt_from_email' => 'mickey@mouse.com',
4118 'title' => "Test Contribution Page",
4119 'financial_type_id' => 1,
4120 'currency' => 'CAD',
4121 'is_monetary' => TRUE,
4122 'is_email_receipt' => TRUE,
4123 ), $params));
4124 return $contributionPage;
4125 }
4126
121c4616 4127 /**
4128 * function to test card_type and pan truncation.
4129 */
4130 public function testCardTypeAndPanTruncation() {
4131 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'));
4132 $contactId = $this->individualCreate();
4133 $params = array(
4134 'contact_id' => $contactId,
4135 'receive_date' => '2016-01-20',
4136 'total_amount' => 100,
4137 'financial_type_id' => 1,
4138 'payment_instrument' => 'Credit Card',
4139 'card_type_id' => $creditCardTypeIDs['Visa'],
4140 'pan_truncation' => 4567,
4141 );
4142 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4143 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
4144 $financialTrxn = $this->callAPISuccessGetSingle(
4145 'FinancialTrxn',
4146 array(
4147 'id' => $lastFinancialTrxnId['financialTrxnId'],
4148 'return' => array('card_type_id', 'pan_truncation'),
4149 )
4150 );
4151 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4152 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
4153 $params = array(
4154 'id' => $contribution['id'],
4155 'pan_truncation' => 2345,
4156 'card_type_id' => $creditCardTypeIDs['Amex'],
4157 );
4158 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4159 $financialTrxn = $this->callAPISuccessGetSingle(
4160 'FinancialTrxn',
4161 array(
4162 'id' => $lastFinancialTrxnId['financialTrxnId'],
4163 'return' => array('card_type_id', 'pan_truncation'),
4164 )
4165 );
4166 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4167 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345);
4168 }
4169
19893cf2
SL
4170 /**
4171 * Test repeat contribution uses non default currency
4172 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4173 */
4174 public function testRepeatTransactionWithDifferenceCurrency() {
4175 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4176 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4177 'original_contribution_id' => $originalContribution['id'],
4178 'contribution_status_id' => 'Completed',
4179 'trxn_id' => uniqid(),
4180 ));
4181 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4182 }
4183
b0e806fa 4184 /**
4185 * Get the financial items for the contribution.
4186 *
4187 * @param int $contributionID
4188 *
4189 * @return array
4190 * Array of associated financial items.
4191 */
4192 protected function getFinancialTransactionsForContribution($contributionID) {
4193 $trxnParams = array(
4194 'entity_id' => $contributionID,
4195 'entity_table' => 'civicrm_contribution',
4196 );
4197 // @todo the following function has naming errors & has a weird signature & appears to
4198 // only be called from test classes. Move into test suite & maybe just use api
4199 // from this function.
4200 return array_merge(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));
4201 }
4202
1af690c4 4203 /**
4204 * Test getunique api call for Contribution entity
4205 */
4206 public function testContributionGetUnique() {
4207 $result = $this->callAPIAndDocument($this->_entity, 'getunique', array(), __FUNCTION__, __FILE__);
4208 $this->assertEquals(2, $result['count']);
4209 $this->assertEquals(array('trxn_id'), $result['values']['UI_contrib_trxn_id']);
4210 $this->assertEquals(array('invoice_id'), $result['values']['UI_contrib_invoice_id']);
4211 }
4212
6a488035 4213}