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