Update thank-you letter code path
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
4cbe18b8
EM
28/**
29 * Class CRM_Contribute_BAO_ContributionTest
acb109b7 30 * @group headless
4cbe18b8 31 */
6a488035 32class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {
6a488035 33
6b60d32c 34 use CRMTraits_Financial_FinancialACLTrait;
53666099 35 use CRMTraits_Financial_PriceSetTrait;
6b60d32c 36
6a488035 37 /**
2449fe69 38 * Clean up after tests.
39 */
40 public function tearDown() {
41 $this->quickCleanUpFinancialEntities();
2449fe69 42 parent::tearDown();
43 }
44
45 /**
46 * Test create method (create and update modes).
6a488035 47 */
00be9182 48 public function testCreate() {
2449fe69 49 $contactId = $this->individualCreate();
6a488035
TO
50
51 $params = array(
52 'contact_id' => $contactId,
53 'currency' => 'USD',
92915c55 54 'financial_type_id' => 1,
6a488035
TO
55 'contribution_status_id' => 1,
56 'payment_instrument_id' => 1,
57 'source' => 'STUDENT',
58 'receive_date' => '20080522000000',
59 'receipt_date' => '20080522000000',
60 'non_deductible_amount' => 0.00,
61 'total_amount' => 200.00,
62 'fee_amount' => 5,
63 'net_amount' => 195,
64 'trxn_id' => '22ereerwww444444',
65 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
66 'thankyou_date' => '20080522',
d0c97775 67 'sequential' => TRUE,
6a488035
TO
68 );
69
d0c97775 70 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
6a488035 71
d0c97775 72 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transaction id creation.');
73 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035
TO
74
75 //update contribution amount
d0c97775 76 $params['id'] = $contribution['id'];
6a488035
TO
77 $params['fee_amount'] = 10;
78 $params['net_amount'] = 190;
79
d0c97775 80 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
6a488035 81
d0c97775 82 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .');
83 $this->assertEquals($params['net_amount'], $contribution['net_amount'], 'Check for Amount updation.');
6a488035
TO
84 }
85
86 /**
f43ac8d8 87 * Create() method with custom data.
6a488035 88 */
00be9182 89 public function testCreateWithCustomData() {
2449fe69 90 $contactId = $this->individualCreate();
6a488035
TO
91
92 //create custom data
2449fe69 93 $customGroup = $this->customGroupCreate(array('extends' => 'Contribution'));
94 $customGroupID = $customGroup['id'];
95 $customGroup = $customGroup['values'][$customGroupID];
96
6a488035
TO
97 $fields = array(
98 'label' => 'testFld',
99 'data_type' => 'String',
100 'html_type' => 'Text',
101 'is_active' => 1,
2449fe69 102 'custom_group_id' => $customGroupID,
6a488035
TO
103 );
104 $customField = CRM_Core_BAO_CustomField::create($fields);
105
106 $params = array(
107 'contact_id' => $contactId,
108 'currency' => 'USD',
92915c55 109 'financial_type_id' => 1,
6a488035
TO
110 'contribution_status_id' => 1,
111 'payment_instrument_id' => 1,
112 'source' => 'STUDENT',
113 'receive_date' => '20080522000000',
114 'receipt_date' => '20080522000000',
115 'id' => NULL,
116 'non_deductible_amount' => 0.00,
117 'total_amount' => 200.00,
118 'fee_amount' => 5,
119 'net_amount' => 195,
120 'trxn_id' => '22ereerwww322323',
121 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
122 'thankyou_date' => '20080522',
d0c97775 123 'skipCleanMoney' => TRUE,
6a488035
TO
124 );
125
6a488035
TO
126 $params['custom'] = array(
127 $customField->id => array(
128 -1 => array(
129 'value' => 'Test custom value',
130 'type' => 'String',
131 'custom_field_id' => $customField->id,
2449fe69 132 'custom_group_id' => $customGroupID,
133 'table_name' => $customGroup['table_name'],
6a488035
TO
134 'column_name' => $customField->column_name,
135 'file_id' => NULL,
136 ),
137 ),
138 );
139
2449fe69 140 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035
TO
141
142 // Check that the custom field value is saved
143 $customValueParams = array(
144 'entityID' => $contribution->id,
145 'custom_' . $customField->id => 1,
146 );
147 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
148 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
149
150 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
151 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.');
6a488035
TO
152 }
153
d73f286e
SL
154 /**
155 * CRM-21026 Test ContributionCount after contribution created with disabled FT
156 */
157 public function testContributionCountDisabledFinancialType() {
158 $contactId = $this->individualCreate();
159 $financialType = array(
160 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7),
161 'is_reserved' => 0,
162 'is_active' => 0,
163 );
164 $finType = $this->callAPISuccess('financial_type', 'create', $financialType);
165 $params = array(
166 'contact_id' => $contactId,
167 'currency' => 'USD',
168 'financial_type_id' => $finType['id'],
169 'contribution_status_id' => 1,
170 'payment_instrument_id' => 1,
171 'source' => 'STUDENT',
172 'receive_date' => '20080522000000',
173 'receipt_date' => '20080522000000',
174 'id' => NULL,
175 'non_deductible_amount' => 0.00,
176 'total_amount' => 200.00,
177 'fee_amount' => 5,
178 'net_amount' => 195,
179 'trxn_id' => '22ereerwww322323',
180 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
181 'thankyou_date' => '20080522',
182 );
d0c97775 183 $this->callAPISuccess('Contribution', 'create', $params);
184 $this->callAPISuccess('financial_type', 'create', array('is_active' => 0, 'id' => $finType['id']));
d73f286e
SL
185 $contributionCount = CRM_Contribute_BAO_Contribution::contributionCount($contactId);
186 $this->assertEquals(1, $contributionCount);
187 }
188
6a488035 189 /**
100fef9d 190 * DeleteContribution() method
6a488035 191 */
00be9182 192 public function testDeleteContribution() {
2449fe69 193 $contactId = $this->individualCreate();
6a488035
TO
194
195 $params = array(
196 'contact_id' => $contactId,
197 'currency' => 'USD',
92915c55 198 'financial_type_id' => 1,
6a488035
TO
199 'contribution_status_id' => 1,
200 'payment_instrument_id' => 1,
201 'source' => 'STUDENT',
202 'receive_date' => '20080522000000',
203 'receipt_date' => '20080522000000',
204 'id' => NULL,
205 'non_deductible_amount' => 0.00,
206 'total_amount' => 200.00,
207 'fee_amount' => 5,
208 'net_amount' => 195,
209 'trxn_id' => '33ereerwww322323',
210 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
211 'thankyou_date' => '20080522',
55ee9063 212 'sequential' => TRUE,
6a488035
TO
213 );
214
55ee9063 215 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
6a488035 216
55ee9063 217 CRM_Contribute_BAO_Contribution::deleteContribution($contribution['id']);
6a488035 218
55ee9063 219 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution['trxn_id'],
6a488035
TO
220 'id', 'trxn_id', 'Database check for deleted Contribution.'
221 );
6a488035
TO
222 }
223
224 /**
2449fe69 225 * Create honor-contact method.
6a488035 226 */
2449fe69 227 public function testCreateAndGetHonorContact() {
6a488035 228 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
92915c55
TO
229 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
230 $email = "{$firstName}.{$lastName}@example.com";
6a488035 231
8381af80 232 //Get profile id of name honoree_individual used to create profileContact
233 $honoreeProfileId = NULL;
234 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
235 $ufGroupDAO->name = 'honoree_individual';
236 if ($ufGroupDAO->find(TRUE)) {
237 $honoreeProfileId = $ufGroupDAO->id;
238 }
239
6a488035 240 $params = array(
8381af80 241 'prefix_id' => 3,
242 'first_name' => $firstName,
243 'last_name' => $lastName,
244 'email-1' => $email,
6a488035 245 );
8381af80 246 $softParam = array('soft_credit_type_id' => 1);
247
7b4b0b68
SL
248 $null = [];
249 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, $null,
92915c55
TO
250 NULL, NULL, $honoreeProfileId
251 );
6a488035 252
8381af80 253 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
254 'Database check for created honor contact record.'
255 );
256 //create contribution on behalf of honary.
257
2449fe69 258 $contactId = $this->individualCreate(array('first_name' => 'John', 'last_name' => 'Doe'));
6a488035 259
6a488035
TO
260 $param = array(
261 'contact_id' => $contactId,
262 'currency' => 'USD',
92915c55 263 'financial_type_id' => 4,
6a488035
TO
264 'contribution_status_id' => 1,
265 'receive_date' => date('Ymd'),
266 'total_amount' => 66,
d0c97775 267 'sequential' => 1,
6a488035
TO
268 );
269
d0c97775 270 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
271 $id = $contribution['id'];
2449fe69 272 $softParam['contact_id'] = $honoreeContactId;
8381af80 273 $softParam['contribution_id'] = $id;
d0c97775 274 $softParam['currency'] = $contribution['currency'];
275 $softParam['amount'] = $contribution['total_amount'];
8381af80 276
277 //Create Soft Contribution for honoree contact
278 CRM_Contribute_BAO_ContributionSoft::add($softParam);
279
280 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
281 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
6a488035 282 );
e4f46be0 283 //get honorary information
8381af80 284 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
4ff25fbb 285 $this->assertEquals(array(
286 $id => array(
287 'honor_type' => 'In Honor of',
2449fe69 288 'honorId' => $contactId,
289 'display_name' => 'Mr. John Doe II',
4ff25fbb 290 'type' => 'Event Fee',
291 'type_id' => '4',
292 'amount' => '$ 66.00',
293 'source' => NULL,
755ca72c 294 'receive_date' => date('Y-m-d 00:00:00'),
4ff25fbb 295 'contribution_status' => 'Completed',
296 ),
297 ), $getHonorContact);
6a488035 298
8381af80 299 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
300 'Database check for created honor contact record.'
301 );
302
303 //get annual contribution information
304 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
305
d0c97775 306 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', CRM_Core_Config::singleton()->defaultCurrency, 'symbol', 'name');
6a488035
TO
307 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
308 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
309 );
6a488035
TO
310 }
311
6b60d32c 312 /**
313 * Test that financial type data is not added to the annual query if acls not enabled.
314 */
315 public function testAnnualQueryWithFinancialACLsEnabled() {
316 $this->enableFinancialACLs();
317 $this->createLoggedInUserWithFinancialACL();
318 $permittedFinancialType = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation');
319 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
320 $this->assertContains('SUM(total_amount) as amount,', $sql);
321 $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql);
322 $this->assertContains('b.financial_type_id IN (' . $permittedFinancialType . ')', $sql);
323
324 // Run it to make sure it's not bad sql.
325 CRM_Core_DAO::executeQuery($sql);
326 $this->disableFinancialACLs();
327 }
328
53666099 329 /**
330 * Test the annual query returns a correct result when multiple line items are present.
331 */
332 public function testAnnualWithMultipleLineItems() {
333 $contactID = $this->createLoggedInUserWithFinancialACL();
334 $this->createContributionWithTwoLineItemsAgainstPriceSet([
39b959db
SL
335 'contact_id' => $contactID,
336 ]
53666099 337 );
338 $this->enableFinancialACLs();
339 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([$contactID]);
340 $result = CRM_Core_DAO::executeQuery($sql);
341 $result->fetch();
342 $this->assertEquals(300, $result->amount);
343 $this->assertEquals(1, $result->count);
344 $this->disableFinancialACLs();
345 }
346
6b60d32c 347 /**
348 * Test that financial type data is not added to the annual query if acls not enabled.
349 */
350 public function testAnnualQueryWithFinancialACLsDisabled() {
351 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
352 $this->assertContains('SUM(total_amount) as amount,', $sql);
353 $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql);
354 $this->assertNotContains('b.financial_type_id', $sql);
355 //$this->assertNotContains('line_item', $sql);
356 // Run it to make sure it's not bad sql.
357 CRM_Core_DAO::executeQuery($sql);
358 }
359
c77f8667 360 /**
361 * Test that financial type data is not added to the annual query if acls not enabled.
362 */
363 public function testAnnualQueryWithFinancialHook() {
364 $this->hookClass->setHook('civicrm_selectWhereClause', array($this, 'aclIdNoZero'));
365 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
366 $this->assertContains('SUM(total_amount) as amount,', $sql);
367 $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql);
368 $this->assertContains('b.id NOT IN (0)', $sql);
369 $this->assertNotContains('b.financial_type_id', $sql);
370 CRM_Core_DAO::executeQuery($sql);
371 }
372
373 /**
374 * Add ACL denying values LIKE '0'.
375 *
376 * @param string $entity
377 * @param string $clauses
378 */
379 public function aclIdNoZero($entity, &$clauses) {
380 if ($entity != 'Contribution') {
381 return;
382 }
383 $clauses['id'] = "NOT IN (0)";
384 }
385
6a488035 386 /**
eceb18cc 387 * Display sort name during.
b581842f 388 * Update multiple contributions
6a488035
TO
389 * sortName();
390 */
00be9182 391 public function testsortName() {
6a488035
TO
392 $params = array(
393 'first_name' => 'Shane',
394 'last_name' => 'Whatson',
395 'contact_type' => 'Individual',
396 );
397
398 $contact = CRM_Contact_BAO_Contact::add($params);
399
400 //Now check $contact is object of contact DAO..
401 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
402
403 $contactId = $contact->id;
6a488035
TO
404 $param = array(
405 'contact_id' => $contactId,
406 'currency' => 'USD',
92915c55 407 'financial_type_id' => 1,
6a488035
TO
408 'contribution_status_id' => 1,
409 'payment_instrument_id' => 1,
410 'source' => 'STUDENT',
411 'receive_date' => '20080522000000',
412 'receipt_date' => '20080522000000',
413 'id' => NULL,
414 'non_deductible_amount' => 0.00,
415 'total_amount' => 300.00,
416 'fee_amount' => 5,
417 'net_amount' => 295,
418 'trxn_id' => '22ereerwww323',
419 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
420 'thankyou_date' => '20080522',
d0c97775 421 'sequential' => TRUE,
6a488035
TO
422 );
423
d0c97775 424 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
6a488035 425
d0c97775 426 $this->assertEquals($param['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
427 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035 428
b581842f 429 //display sort name during Update multiple contributions
d0c97775 430 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution['id']);
6a488035
TO
431
432 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
6a488035
TO
433 }
434
435 /**
eceb18cc 436 * Add premium during online Contribution.
6a488035
TO
437 *
438 * AddPremium();
439 */
00be9182 440 public function testAddPremium() {
2449fe69 441 $contactId = $this->individualCreate();
6a488035 442
6a488035
TO
443 $params = array(
444 'name' => 'TEST Premium',
445 'sku' => 111,
446 'imageOption' => 'noImage',
447 'MAX_FILE_SIZE' => 2097152,
448 'price' => 100.00,
449 'cost' => 90.00,
450 'min_contribution' => 100,
451 'is_active' => 1,
452 );
32f27499 453 $premium = CRM_Contribute_BAO_Product::create($params);
6a488035
TO
454
455 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
456
a275d4b6 457 $contributionParams = array(
6a488035
TO
458 'contact_id' => $contactId,
459 'currency' => 'USD',
92915c55 460 'financial_type_id' => 1,
6a488035
TO
461 'contribution_status_id' => 1,
462 'payment_instrument_id' => 1,
463 'source' => 'STUDENT',
464 'receive_date' => '20080522000000',
465 'receipt_date' => '20080522000000',
466 'id' => NULL,
467 'non_deductible_amount' => 0.00,
468 'total_amount' => 300.00,
469 'fee_amount' => 5,
470 'net_amount' => 295,
471 'trxn_id' => '33erdfrwvw434',
472 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
473 'thankyou_date' => '20080522',
d0c97775 474 'sequential' => TRUE,
6a488035 475 );
a275d4b6 476 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0];
6a488035 477
a275d4b6 478 $this->assertEquals($contributionParams['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
d0c97775 479 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035
TO
480
481 //parameter for adding premium to contribution
482 $data = array(
483 'product_id' => $premium->id,
d0c97775 484 'contribution_id' => $contribution['id'],
6a488035
TO
485 'product_option' => NULL,
486 'quantity' => 1,
487 );
488 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
489 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
490
491 //Delete Product
37828d4f 492 CRM_Contribute_BAO_Product::del($premium->id);
6a488035
TO
493 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name,
494 'id', 'name', 'Database check for deleted Product.'
495 );
6a488035
TO
496 }
497
498 /**
fe482240 499 * Check duplicate contribution id.
6a488035
TO
500 * during the contribution import
501 * checkDuplicateIds();
502 */
00be9182 503 public function testcheckDuplicateIds() {
2449fe69 504 $contactId = $this->individualCreate();
6a488035
TO
505
506 $param = array(
507 'contact_id' => $contactId,
508 'currency' => 'USD',
92915c55 509 'financial_type_id' => 1,
6a488035
TO
510 'contribution_status_id' => 1,
511 'payment_instrument_id' => 1,
512 'source' => 'STUDENT',
513 'receive_date' => '20080522000000',
514 'receipt_date' => '20080522000000',
515 'id' => NULL,
516 'non_deductible_amount' => 0.00,
517 'total_amount' => 300.00,
518 'fee_amount' => 5,
519 'net_amount' => 295,
520 'trxn_id' => '76ereeswww835',
521 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
522 'thankyou_date' => '20080522',
d0c97775 523 'sequential' => TRUE,
6a488035
TO
524 );
525
d0c97775 526 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
6a488035 527
d0c97775 528 $this->assertEquals($param['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
529 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035 530 $data = array(
d0c97775 531 'id' => $contribution['id'],
532 'trxn_id' => $contribution['trxn_id'],
533 'invoice_id' => $contribution['invoice_id'],
6a488035
TO
534 );
535 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
d0c97775 536 $this->assertEquals($contributionID, $contribution['id'], 'Check for duplicate transcation id .');
6a488035 537 }
96025800 538
8022372f
GC
539 /**
540 * Check credit note id creation
541 * when a contribution is cancelled or refunded
542 * createCreditNoteId();
543 */
544 public function testCreateCreditNoteId() {
2449fe69 545 $contactId = $this->individualCreate();
8022372f
GC
546
547 $param = array(
548 'contact_id' => $contactId,
549 'currency' => 'USD',
550 'financial_type_id' => 1,
551 'contribution_status_id' => 3,
552 'payment_instrument_id' => 1,
553 'source' => 'STUDENT',
554 'receive_date' => '20080522000000',
555 'receipt_date' => '20080522000000',
556 'id' => NULL,
557 'non_deductible_amount' => 0.00,
558 'total_amount' => 300.00,
559 'fee_amount' => 5,
560 'net_amount' => 295,
561 'trxn_id' => '76ereeswww835',
562 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
563 'thankyou_date' => '20080522',
d0c97775 564 'sequential' => TRUE,
8022372f
GC
565 );
566
567 $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
d0c97775 568 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
569 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
570 $this->assertEquals($creditNoteId, $contribution['creditnote_id'], 'Check if credit note id is created correctly.');
8022372f
GC
571 }
572
b04e4b11
PN
573 /**
574 * Create() method (create and update modes).
575 */
576 public function testIsPaymentFlag() {
2449fe69 577 $contactId = $this->individualCreate();
b04e4b11 578
d0c97775 579 $params = [
b04e4b11
PN
580 'contact_id' => $contactId,
581 'currency' => 'USD',
582 'financial_type_id' => 1,
583 'contribution_status_id' => 1,
584 'payment_instrument_id' => 1,
585 'source' => 'STUDENT',
586 'receive_date' => '20080522000000',
587 'receipt_date' => '20080522000000',
588 'non_deductible_amount' => 0.00,
589 'total_amount' => 200.00,
590 'fee_amount' => 5,
591 'net_amount' => 195,
592 'trxn_id' => '22ereerwww4444xx',
593 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
594 'thankyou_date' => '20080522',
d0c97775 595 'sequential' => TRUE,
596 ];
597 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
b04e4b11 598
d0c97775 599 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
600 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
b04e4b11
PN
601
602 $trxnArray = array(
603 'trxn_id' => $params['trxn_id'],
604 'is_payment' => 1,
605 );
606 $defaults = array();
607 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 608 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11 609 //update contribution amount
d0c97775 610 $params['id'] = $contribution['id'];
b04e4b11 611 $params['total_amount'] = 150;
d0c97775 612 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
b04e4b11 613
d0c97775 614 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .');
615 $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for Amount updation.');
b04e4b11
PN
616 $trxnArray = array(
617 'trxn_id' => $params['trxn_id'],
618 'is_payment' => 1,
619 );
620 $defaults = array();
621 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 622 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
623 $trxnArray['is_payment'] = 0;
624 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 625 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
626 }
627
a387acc9
PN
628 /**
629 * Create() method (create and update modes).
630 */
631 public function testIsPaymentFlagForPending() {
2449fe69 632 $contactId = $this->individualCreate();
a387acc9
PN
633
634 $params = array(
635 'contact_id' => $contactId,
636 'currency' => 'USD',
637 'financial_type_id' => 1,
638 'contribution_status_id' => 2,
639 'payment_instrument_id' => 1,
640 'source' => 'STUDENT',
641 'is_pay_later' => 1,
642 'receive_date' => '20080522000000',
643 'receipt_date' => '20080522000000',
644 'non_deductible_amount' => 0.00,
645 'total_amount' => 200.00,
646 'fee_amount' => 5,
647 'net_amount' => 195,
648 'trxn_id' => '22ereerwww4444yy',
649 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
650 'thankyou_date' => '20080522',
d0c97775 651 'sequential' => TRUE,
a387acc9
PN
652 );
653
d0c97775 654 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
a387acc9 655
d0c97775 656 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transaction id creation.');
657 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
a387acc9
PN
658
659 $trxnArray = array(
660 'trxn_id' => $params['trxn_id'],
661 'is_payment' => 0,
662 );
663 $defaults = array();
664 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
665 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
666 $trxnArray['is_payment'] = 1;
667 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
668 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
669 //update contribution amount
d0c97775 670 $params['id'] = $contribution['id'];
a387acc9
PN
671 $params['contribution_status_id'] = 1;
672
d0c97775 673 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
a387acc9 674
d0c97775 675 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .');
676 $this->assertEquals($params['contribution_status_id'], $contribution['contribution_status_id'], 'Check for status updation.');
a387acc9
PN
677 $trxnArray = array(
678 'trxn_id' => $params['trxn_id'],
679 'is_payment' => 1,
680 );
681 $defaults = array();
682 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
683 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
684 $trxnArray['is_payment'] = 0;
685 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
686 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
a387acc9
PN
687 }
688
eec619df 689 /**
0a5651eb 690 * addPayments() method (add and edit modes of participant)
eec619df
PN
691 */
692 public function testAddPayments() {
693 list($lineItems, $contribution) = $this->addParticipantWithContribution();
d0c97775 694 CRM_Contribute_BAO_Contribution::addPayments([$contribution]);
0a5651eb
PN
695 $this->checkItemValues($contribution);
696 }
697
698 /**
699 * checks db values for financial item
700 */
624195c8 701 public function checkItemValues($contribution) {
876b8ab0 702 $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(4, 'Accounts Receivable Account is');
2449fe69 703 $query = "SELECT eft1.entity_id, ft.total_amount, eft1.amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
eec619df
PN
704INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
705WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
706
707 $queryParams[1] = array($contribution->id, 'Integer');
708 $queryParams[2] = array($toFinancialAccount, 'Integer');
709
710 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
19084b68 711 $amounts = array(100.00, 50.00);
eec619df
PN
712 while ($dao->fetch()) {
713 $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.');
19084b68 714 $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
eec619df 715 }
eec619df
PN
716 }
717
0a5651eb
PN
718 /**
719 * assignProportionalLineItems() method (add and edit modes of participant)
720 */
721 public function testAssignProportionalLineItems() {
722 list($lineItems, $contribution) = $this->addParticipantWithContribution();
0a5651eb
PN
723 $params = array(
724 'contribution_id' => $contribution->id,
725 'total_amount' => 150.00,
726 );
727 $trxn = new CRM_Financial_DAO_FinancialTrxn();
728 $trxn->orderBy('id DESC');
729 $trxn->find(TRUE);
8de1ade9 730 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution->total_amount);
0a5651eb
PN
731 $this->checkItemValues($contribution);
732 }
733
eec619df
PN
734 /**
735 * Add participant with contribution
736 *
737 * @return array
738 */
78c99516 739 public function addParticipantWithContribution() {
eec619df 740 // creating price set, price field
2449fe69 741 $this->_contactId = $this->individualCreate();
742 $event = $this->eventCreate();
743 $this->_eventId = $event['id'];
624195c8 744 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
19084b68 745 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
eec619df
PN
746 $paramsSet['is_active'] = TRUE;
747 $paramsSet['financial_type_id'] = 4;
748 $paramsSet['extends'] = 1;
749
750 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
751 $priceSetId = $priceset->id;
752
753 //Checking for priceset added in the table.
754 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
755 'id', $paramsSet['title'], 'Check DB for created priceset'
756 );
757 $paramsField = array(
758 'label' => 'Price Field',
759 'name' => CRM_Utils_String::titleToVar('Price Field'),
760 'html_type' => 'CheckBox',
761 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
762 'option_value' => array('1' => 100, '2' => 200),
763 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
764 'option_weight' => array('1' => 1, '2' => 2),
765 'option_amount' => array('1' => 100, '2' => 200),
766 'is_display_amounts' => 1,
767 'weight' => 1,
768 'options_per_line' => 1,
769 'is_active' => array('1' => 1, '2' => 1),
770 'price_set_id' => $priceset->id,
771 'is_enter_qty' => 1,
772 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
773 );
774 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
775 $eventParams = array(
776 'id' => $this->_eventId,
777 'financial_type_id' => 4,
778 'is_monetary' => 1,
779 );
780 CRM_Event_BAO_Event::create($eventParams);
781 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
e4ba8498 782
eec619df
PN
783 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
784 $participantParams = array(
785 'financial_type_id' => 4,
786 'event_id' => $this->_eventId,
787 'role_id' => 1,
788 'status_id' => 14,
789 'fee_currency' => 'USD',
790 'contact_id' => $this->_contactId,
791 );
792 $participant = CRM_Event_BAO_Participant::add($participantParams);
793 $contributionParams = array(
794 'total_amount' => 150,
795 'currency' => 'USD',
796 'contact_id' => $this->_contactId,
797 'financial_type_id' => 4,
798 'contribution_status_id' => 1,
799 'partial_payment_total' => 300.00,
f49cdeab 800 'partial_amount_to_pay' => 150,
eec619df
PN
801 'contribution_mode' => 'participant',
802 'participant_id' => $participant->id,
d0c97775 803 'sequential' => TRUE,
eec619df
PN
804 );
805
806 foreach ($priceFields['values'] as $key => $priceField) {
807 $lineItems[1][$key] = array(
808 'price_field_id' => $priceField['price_field_id'],
809 'price_field_value_id' => $priceField['id'],
810 'label' => $priceField['label'],
811 'field_title' => $priceField['label'],
812 'qty' => 1,
813 'unit_price' => $priceField['amount'],
814 'line_total' => $priceField['amount'],
815 'financial_type_id' => $priceField['financial_type_id'],
816 );
817 }
818 $contributionParams['line_item'] = $lineItems;
d0c97775 819 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0];
eec619df
PN
820
821 $paymentParticipant = array(
822 'participant_id' => $participant->id,
d0c97775 823 'contribution_id' => $contribution['id'],
eec619df 824 );
a5750507 825 CRM_Event_BAO_ParticipantPayment::create($paymentParticipant);
eec619df 826
d0c97775 827 $contributionObject = new CRM_Contribute_BAO_Contribution();
828 $contributionObject->id = $contribution['id'];
829 $contributionObject->find(TRUE);
830
831 return array($lineItems, $contributionObject);
eec619df
PN
832 }
833
5c8b902b 834 /**
a4a31ffe 835 * checkLineItems() check if total amount matches the sum of line total
5c8b902b
PN
836 */
837 public function testcheckLineItems() {
838 $params = array(
839 'contact_id' => 202,
840 'receive_date' => '2010-01-20',
841 'total_amount' => 100,
842 'financial_type_id' => 3,
843 'line_items' => array(
844 array(
845 'line_item' => array(
846 array(
847 'entity_table' => 'civicrm_contribution',
848 'price_field_id' => 8,
849 'price_field_value_id' => 16,
850 'label' => 'test 1',
851 'qty' => 1,
852 'unit_price' => 100,
853 'line_total' => 100,
854 ),
855 array(
856 'entity_table' => 'civicrm_contribution',
857 'price_field_id' => 8,
858 'price_field_value_id' => 17,
859 'label' => 'Test 2',
860 'qty' => 1,
861 'unit_price' => 200,
862 'line_total' => 200,
863 'financial_type_id' => 1,
864 ),
865 ),
a4a31ffe 866 'params' => array(),
5c8b902b 867 ),
a4a31ffe 868 ),
5c8b902b 869 );
c16c6ad8 870
af004c04 871 try {
2449fe69 872 CRM_Contribute_BAO_Contribution::checkLineItems($params);
af004c04
PN
873 $this->fail("Missed expected exception");
874 }
c16c6ad8
CR
875 catch (CRM_Contribute_Exception_CheckLineItemsException $e) {
876 $this->assertEquals(
877 CRM_Contribute_Exception_CheckLineItemsException::LINE_ITEM_DIFFERRING_TOTAL_EXCEPTON_MSG,
878 $e->getMessage()
879 );
af004c04 880 }
c16c6ad8 881
5c8b902b
PN
882 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
883 $params['total_amount'] = 300;
c16c6ad8 884
af004c04 885 CRM_Contribute_BAO_Contribution::checkLineItems($params);
5c8b902b
PN
886 }
887
c16c6ad8
CR
888 /**
889 * Tests CRM_Contribute_BAO_Contribution::checkLineItems() method works with
890 * floating point values.
891 */
892 public function testCheckLineItemsWithFloatingPointValues() {
893 $params = array(
894 'contact_id' => 202,
895 'receive_date' => date('Y-m-d'),
896 'total_amount' => 16.67,
897 'financial_type_id' => 3,
898 'line_items' => array(
899 array(
900 'line_item' => array(
901 array(
902 'entity_table' => 'civicrm_contribution',
903 'price_field_id' => 8,
904 'price_field_value_id' => 16,
905 'label' => 'test 1',
906 'qty' => 1,
907 'unit_price' => 14.85,
908 'line_total' => 14.85,
909 ),
910 array(
911 'entity_table' => 'civicrm_contribution',
912 'price_field_id' => 8,
913 'price_field_value_id' => 17,
914 'label' => 'Test 2',
915 'qty' => 1,
916 'unit_price' => 1.66,
917 'line_total' => 1.66,
918 'financial_type_id' => 1,
919 ),
920 array(
921 'entity_table' => 'civicrm_contribution',
922 'price_field_id' => 8,
923 'price_field_value_id' => 17,
924 'label' => 'Test 2',
925 'qty' => 1,
926 'unit_price' => 0.16,
927 'line_total' => 0.16,
928 'financial_type_id' => 1,
929 ),
930 ),
931 'params' => array(),
932 ),
933 ),
934 );
935
936 $foundException = FALSE;
937
938 try {
939 CRM_Contribute_BAO_Contribution::checkLineItems($params);
940 }
941 catch (CRM_Contribute_Exception_CheckLineItemsException $e) {
942 $foundException = TRUE;
943 }
944
945 $this->assertFalse($foundException);
946 }
947
88f7a518
E
948 /**
949 * Test activity amount updation.
950 */
951 public function testActivityCreate() {
952 $contactId = $this->individualCreate();
953 $defaults = array();
954
955 $params = array(
956 'contact_id' => $contactId,
957 'currency' => 'USD',
958 'financial_type_id' => 1,
959 'contribution_status_id' => 1,
960 'payment_instrument_id' => 1,
961 'source' => 'STUDENT',
962 'receive_date' => '20080522000000',
963 'receipt_date' => '20080522000000',
964 'non_deductible_amount' => 0.00,
965 'total_amount' => 100.00,
966 'trxn_id' => '22ereerwww444444',
967 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
968 'thankyou_date' => '20160519',
d0c97775 969 'sequential' => 1,
88f7a518
E
970 );
971
d0c97775 972 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
88f7a518 973
d0c97775 974 $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.');
975 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
88f7a518
E
976
977 // Check amount in activity.
978 $activityParams = array(
d0c97775 979 'source_record_id' => $contribution['id'],
d66c61b6 980 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contribution'),
88f7a518 981 );
d66c61b6 982 // @todo use api instead.
88f7a518
E
983 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
984
d0c97775 985 $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.');
88f7a518
E
986 $this->assertEquals("$ 100.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
987
d0c97775 988 $params['id'] = $contribution['id'];
88f7a518
E
989 $params['total_amount'] = 200;
990
d0c97775 991 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
88f7a518 992
d0c97775 993 $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.');
994 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
88f7a518
E
995
996 // Retrieve activity again.
997 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
998
d0c97775 999 $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.');
88f7a518
E
1000 $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
1001 }
1002
5c3d600f
PN
1003 /**
1004 * Test allowUpdateRevenueRecognitionDate.
1005 */
1006 public function testAllowUpdateRevenueRecognitionDate() {
1007 $contactId = $this->individualCreate();
1008 $params = array(
1009 'contact_id' => $contactId,
1010 'receive_date' => '2010-01-20',
1011 'total_amount' => 100,
4303ea89 1012 'financial_type_id' => 4,
5c3d600f
PN
1013 );
1014 $order = $this->callAPISuccess('order', 'create', $params);
1015 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
1016 $this->assertTrue($allowUpdate);
1017
1018 $event = $this->eventCreate();
1019 $params = array(
1020 'contact_id' => $contactId,
1021 'receive_date' => '2010-01-20',
1022 'total_amount' => 300,
8484a5f0 1023 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
1024 'contribution_status_id' => 'Completed',
5c3d600f
PN
1025 );
1026 $priceFields = $this->createPriceSet('event', $event['id']);
1027 foreach ($priceFields['values'] as $key => $priceField) {
1028 $lineItems[$key] = array(
1029 'price_field_id' => $priceField['price_field_id'],
1030 'price_field_value_id' => $priceField['id'],
1031 'label' => $priceField['label'],
1032 'field_title' => $priceField['label'],
1033 'qty' => 1,
1034 'unit_price' => $priceField['amount'],
1035 'line_total' => $priceField['amount'],
1036 'financial_type_id' => $priceField['financial_type_id'],
1037 'entity_table' => 'civicrm_participant',
1038 );
1039 }
1040 $params['line_items'][] = array(
1041 'line_item' => $lineItems,
1042 'params' => array(
1043 'contact_id' => $contactId,
1044 'event_id' => $event['id'],
1045 'status_id' => 1,
1046 'role_id' => 1,
1047 'register_date' => '2007-07-21 00:00:00',
1048 'source' => 'Online Event Registration: API Testing',
1049 ),
1050 );
1051 $order = $this->callAPISuccess('order', 'create', $params);
1052 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
1053 $this->assertFalse($allowUpdate);
1054
1055 $params = array(
1056 'contact_id' => $contactId,
1057 'receive_date' => '2010-01-20',
1058 'total_amount' => 200,
8484a5f0 1059 'financial_type_id' => $this->getFinancialTypeId('Member Dues'),
1060 'contribution_status_id' => 'Completed',
5c3d600f
PN
1061 );
1062 $membershipType = $this->membershipTypeCreate();
1063 $priceFields = $this->createPriceSet();
1064 $lineItems = array();
1065 foreach ($priceFields['values'] as $key => $priceField) {
1066 $lineItems[$key] = array(
1067 'price_field_id' => $priceField['price_field_id'],
1068 'price_field_value_id' => $priceField['id'],
1069 'label' => $priceField['label'],
1070 'field_title' => $priceField['label'],
1071 'qty' => 1,
1072 'unit_price' => $priceField['amount'],
1073 'line_total' => $priceField['amount'],
1074 'financial_type_id' => $priceField['financial_type_id'],
1075 'entity_table' => 'civicrm_membership',
1076 'membership_type_id' => $membershipType,
1077 );
1078 }
1079 $params['line_items'][] = array(
1080 'line_item' => array(array_pop($lineItems)),
1081 'params' => array(
1082 'contact_id' => $contactId,
1083 'membership_type_id' => $membershipType,
1084 'join_date' => '2006-01-21',
1085 'start_date' => '2006-01-21',
1086 'end_date' => '2006-12-21',
1087 'source' => 'Payment',
1088 'is_override' => 1,
1089 'status_id' => 1,
1090 ),
1091 );
1092 $order = $this->callAPISuccess('order', 'create', $params);
1093 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
1094 $this->assertFalse($allowUpdate);
1095 }
1096
d9553c2e
PN
1097 /**
1098 * Test calculateFinancialItemAmount().
1099 */
1100 public function testcalculateFinancialItemAmount() {
1101 $testParams = array(
1102 array(
1103 'params' => array(),
1104 'amountParams' => array(
1105 'line_total' => 100,
1106 'previous_line_total' => 300,
1107 'diff' => 1,
1108 ),
1109 'context' => 'changedAmount',
1110 'expectedItemAmount' => -200,
1111 ),
1112 array(
1113 'params' => array(),
1114 'amountParams' => array(
1115 'line_total' => 100,
1116 'previous_line_total' => 100,
1117 'diff' => -1,
1118 ),
efb16c63 1119 // Most contexts are ignored. Removing refs to change payment instrument so placeholder.
1120 'context' => 'not null',
d9553c2e
PN
1121 'expectedItemAmount' => -100,
1122 ),
1123 array(
1124 'params' => array(
1125 'is_quick_config' => TRUE,
1126 'total_amount' => 110,
1127 'tax_amount' => 10,
1128 ),
1129 'amountParams' => array(
1130 'item_amount' => 100,
1131 ),
1132 'context' => 'changedAmount',
1133 'expectedItemAmount' => 100,
1134 ),
1135 array(
1136 'params' => array(
1137 'is_quick_config' => TRUE,
1138 'total_amount' => 110,
1139 'tax_amount' => 10,
1140 ),
1141 'amountParams' => array(
1142 'item_amount' => NULL,
1143 ),
1144 'context' => 'changedAmount',
1145 'expectedItemAmount' => 110,
1146 ),
1147 array(
1148 'params' => array(
1149 'is_quick_config' => TRUE,
1150 'total_amount' => 110,
1151 'tax_amount' => 10,
1152 ),
1153 'amountParams' => array(
1154 'item_amount' => NULL,
1155 ),
1156 'context' => NULL,
1157 'expectedItemAmount' => 100,
1158 ),
1159 );
1160 foreach ($testParams as $params) {
1161 $itemAmount = CRM_Contribute_BAO_Contribution::calculateFinancialItemAmount($params['params'], $params['amountParams'], $params['context']);
1162 $this->assertEquals($itemAmount, $params['expectedItemAmount'], 'Invalid Financial Item amount.');
1163 }
1164 }
1165
d934a732
PN
1166 /**
1167 * Test recording of amount with comma separator.
1168 */
1169 public function testCommaSeparatorAmount() {
1170 $contactId = $this->individualCreate();
1171
1172 $params = array(
1173 'contact_id' => $contactId,
1174 'currency' => 'USD',
1175 'financial_type_id' => 1,
1176 'contribution_status_id' => 8,
1177 'payment_instrument_id' => 1,
1178 'receive_date' => '20080522000000',
1179 'receipt_date' => '20080522000000',
1180 'total_amount' => '20000.00',
1181 'partial_payment_total' => '20,000.00',
f49cdeab 1182 'partial_amount_to_pay' => '8,000.00',
d934a732
PN
1183 );
1184
d0c97775 1185 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
1186 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
d934a732
PN
1187 $financialTrxn = $this->callAPISuccessGetSingle(
1188 'FinancialTrxn',
1189 array(
1190 'id' => $lastFinancialTrxnId['financialTrxnId'],
1191 'return' => array('total_amount'),
1192 )
1193 );
1194 $this->assertEquals($financialTrxn['total_amount'], 8000, 'Invalid Tax amount.');
1195 }
1196
adbc354b
PN
1197 /**
1198 * Test for function getSalesTaxFinancialAccounts().
1199 */
1200 public function testgetSalesTaxFinancialAccounts() {
1201 $this->enableTaxAndInvoicing();
1202 $financialType = $this->createFinancialType();
1203 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1204 $expectedResult = array($financialAccount->financial_account_id => $financialAccount->financial_account_id);
1205 $financialType = $this->createFinancialType();
1206 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1207 $expectedResult[$financialAccount->financial_account_id] = $financialAccount->financial_account_id;
1208 $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution::getSalesTaxFinancialAccounts();
1209 $this->assertTrue(($salesTaxFinancialAccount == $expectedResult), 'Function returned wrong values.');
1210 }
1211
0409b821
PN
1212 /**
1213 * Test for function createProportionalEntry().
83644f47 1214 *
1215 * @param string $thousandSeparator
1216 * punctuation used to refer to thousands.
1217 *
1218 * @dataProvider getThousandSeparators
0409b821 1219 */
83644f47 1220 public function testCreateProportionalEntry($thousandSeparator) {
1221 $this->setCurrencySeparators($thousandSeparator);
2a4a2f00 1222 list($contribution, $financialAccount) = $this->createContributionWithTax();
0409b821
PN
1223 $params = array(
1224 'total_amount' => 55,
1225 'to_financial_account_id' => $financialAccount->financial_account_id,
1226 'payment_instrument_id' => 1,
1227 'trxn_date' => date('Ymd'),
1228 'status_id' => 1,
0409b821 1229 'entity_id' => $contribution['id'],
0409b821 1230 );
3137781d 1231 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
0409b821
PN
1232 $entityParams = array(
1233 'contribution_total_amount' => $contribution['total_amount'],
1234 'trxn_total_amount' => 55,
1235 'line_item_amount' => 100,
1236 );
1237 $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
1238 $eftParams = array(
1239 'entity_table' => 'civicrm_financial_item',
cf28d075 1240 'entity_id' => $previousLineItem['id'],
3137781d 1241 'financial_trxn_id' => (string) $financialTrxn['id'],
0409b821
PN
1242 );
1243 CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams);
1244 $trxnTestArray = array_merge($eftParams, array(
3137781d 1245 'amount' => '50.00',
0409b821 1246 ));
cf28d075 1247 $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
0409b821
PN
1248 }
1249
c364c544
MW
1250 /**
1251 * Test for function createProportionalEntry with zero amount().
1252 *
1253 * @param string $thousandSeparator
1254 * punctuation used to refer to thousands.
1255 *
1256 * @dataProvider getThousandSeparators
1257 */
1258 public function testCreateProportionalEntryZeroAmount($thousandSeparator) {
1259 $this->setCurrencySeparators($thousandSeparator);
1260 list($contribution, $financialAccount) = $this->createContributionWithTax(array('total_amount' => 0));
1261 $params = array(
1262 'total_amount' => 0,
1263 'to_financial_account_id' => $financialAccount->financial_account_id,
1264 'payment_instrument_id' => 1,
1265 'trxn_date' => date('Ymd'),
1266 'status_id' => 1,
1267 'entity_id' => $contribution['id'],
1268 );
1269 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1270 $entityParams = array(
1271 'contribution_total_amount' => $contribution['total_amount'],
1272 'trxn_total_amount' => 0,
1273 'line_item_amount' => 0,
1274 );
1275 $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
1276 $eftParams = array(
1277 'entity_table' => 'civicrm_financial_item',
1278 'entity_id' => $previousLineItem['id'],
1279 'financial_trxn_id' => (string) $financialTrxn['id'],
1280 );
1281 CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams);
1282 $trxnTestArray = array_merge($eftParams, array(
1283 'amount' => '0.00',
1284 ));
1285 $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
1286 }
1287
646bc565
PN
1288 /**
1289 * Test for function getLastFinancialItemIds().
1290 */
1291 public function testgetLastFinancialItemIds() {
2a4a2f00 1292 list($contribution, $financialAccount) = $this->createContributionWithTax();
646bc565
PN
1293 list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
1294 $this->assertEquals(count($ftIds), 1, 'Invalid count.');
1295 $this->assertEquals(count($taxItems), 1, 'Invalid count.');
1296 foreach ($taxItems as $value) {
1297 $this->assertEquals($value['amount'], 10, 'Invalid tax amount.');
1298 }
1299 }
1300
2a4a2f00 1301 /**
97c818fd 1302 * Test to ensure proportional entries are creating when adding a payment..
1303 *
1304 * In this test we create a pending contribution for $110 consisting of $100 contribution and $10 tax.
1305 *
1306 * We pay $50, resulting in it being allocated as $45.45 paymnt & $4.55 tax. This is in equivalent proportions
1307 * to the original payment - ie. .0909 of the $110 is 10 & that * 50 is $4.54 (note the rounding seems wrong as it should be
1308 * saved un-rounded).
2a4a2f00 1309 */
97c818fd 1310 public function testCreateProportionalFinancialEntriesViaPaymentCreate() {
1311 list($contribution, $financialAccount) = $this->createContributionWithTax([], FALSE);
1312 $params = [
3137781d 1313 'total_amount' => 50,
2a4a2f00
PN
1314 'to_financial_account_id' => $financialAccount->financial_account_id,
1315 'payment_instrument_id' => 1,
1316 'trxn_date' => date('Ymd'),
1317 'status_id' => 1,
2a4a2f00 1318 'entity_id' => $contribution['id'],
97c818fd 1319 'contribution_id' => $contribution['id'],
1320 ];
1321 $financialTrxn = $this->callAPISuccess('Payment', 'create', $params);
1322 $eftParams = [
2a4a2f00
PN
1323 'entity_table' => 'civicrm_financial_item',
1324 'financial_trxn_id' => $financialTrxn['id'],
97c818fd 1325 ];
2a4a2f00
PN
1326 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1327 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
97c818fd 1328 $testAmount = [4.55, 45.45];
2a4a2f00 1329 foreach ($entityFinancialTrxn['values'] as $value) {
97c818fd 1330 $this->assertEquals(array_pop($testAmount), $value['amount'], 'Invalid amount stored in civicrm_entity_financial_trxn.');
77641de4
PN
1331 }
1332 }
1333
1334 /**
1335 * Test to check if amount is proportionally asigned for PI change.
1336 */
1337 public function testProportionallyAssignedForPIChange() {
1338 list($contribution, $financialAccount) = $this->createContributionWithTax();
1339 $params = array(
1340 'id' => $contribution['id'],
1341 'payment_instrument_id' => 3,
1342 );
1343 $this->callAPISuccess('Contribution', 'create', $params);
1344 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1345 $eftParams = array(
1346 'entity_table' => 'civicrm_financial_item',
1347 'financial_trxn_id' => $lastFinancialTrxnId['financialTrxnId'],
1348 );
1349 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1350 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1351 $testAmount = array(10, 100);
1352 foreach ($entityFinancialTrxn['values'] as $value) {
1353 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
2a4a2f00
PN
1354 }
1355 }
1356
646bc565
PN
1357 /**
1358 * Function to create contribution with tax.
1359 */
97c818fd 1360 public function createContributionWithTax($params = array(), $isCompleted = TRUE) {
c364c544
MW
1361 if (!isset($params['total_amount'])) {
1362 $params['total_amount'] = 100;
1363 }
646bc565
PN
1364 $contactId = $this->individualCreate();
1365 $this->enableTaxAndInvoicing();
1366 $financialType = $this->createFinancialType();
1367 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1368 $form = new CRM_Contribute_Form_Contribution();
1369
1370 $form->testSubmit(array(
39b959db
SL
1371 'total_amount' => $params['total_amount'],
1372 'financial_type_id' => $financialType['id'],
1373 'contact_id' => $contactId,
97c818fd 1374 'contribution_status_id' => $isCompleted ? 1 : 2,
39b959db
SL
1375 'price_set_id' => 0,
1376 ), CRM_Core_Action::ADD);
646bc565
PN
1377 $contribution = $this->callAPISuccessGetSingle('Contribution',
1378 array(
1379 'contact_id' => $contactId,
1380 'return' => array('tax_amount', 'total_amount'),
1381 )
1382 );
2a4a2f00 1383 return array($contribution, $financialAccount);
646bc565
PN
1384 }
1385
75c07fde
JP
1386 /**
1387 * Test processOnBehalfOrganization() function.
1388 */
1389 public function testProcessOnBehalfOrganization() {
1390 $orgInfo = array(
1391 'phone' => '11111111',
1392 'email' => 'testorg@gmail.com',
1393 'street_address' => 'test Street',
1394 'city' => 'test City',
1395 'state_province' => 'AA',
1396 'postal_code' => '222222',
1397 'country' => 'United States',
1398 );
1399 $contactID = $this->individualCreate();
1400 $orgId = $this->organizationCreate(array('organization_name' => 'testorg1'));
1401 $orgCount = $this->callAPISuccessGetCount('Contact', array(
1402 'contact_type' => "Organization",
1403 'organization_name' => "testorg1",
1404 ));
1405 $this->assertEquals($orgCount, 1);
1406
1407 $values = $params = array();
1408 $behalfOrganization = array(
1409 'organization_name' => 'testorg1',
1410 'phone' => array(
1411 1 => array(
1412 'phone' => $orgInfo['phone'],
1413 'is_primary' => 1,
1414 ),
1415 ),
1416 'email' => array(
1417 1 => array(
1418 'email' => $orgInfo['email'],
1419 'is_primary' => 1,
1420 ),
1421 ),
1422 'address' => array(
1423 3 => array(
1424 'street_address' => $orgInfo['street_address'],
1425 'city' => $orgInfo['city'],
1426 'location_type_id' => 3,
1427 'postal_code' => $orgInfo['postal_code'],
1428 'country' => 'US',
1429 'state_province' => 'AA',
1430 'is_primary' => 1,
1431 ),
1432 ),
1433 );
1434 $fields = array(
1435 'organization_name' => 1,
1436 'phone-3-1' => 1,
1437 'email-3' => 1,
1438 'street_address-3' => 1,
1439 'city-3' => 1,
1440 'postal_code-3' => 1,
1441 'country-3' => 1,
1442 'state_province-3' => 1,
1443 );
1444 CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
1445
1446 //Check whether new organisation is not created.
1447 $result = $this->callAPISuccess('Contact', 'get', array(
1448 'contact_type' => "Organization",
1449 'organization_name' => "testorg1",
1450 ));
1451 $this->assertEquals($result['count'], 1);
1452
1453 //Assert all org values are updated.
1454 foreach ($orgInfo as $key => $val) {
1455 $this->assertEquals($result['values'][$orgId][$key], $val);
1456 }
1457
1458 //Check if alert is assigned to params if more than 1 dupe exists.
1459 $orgId = $this->organizationCreate(array('organization_name' => 'testorg1', 'email' => 'testorg@gmail.com'));
1460 CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
1461 $this->assertEquals($params['onbehalf_dupe_alert'], 1);
1462 }
1463
7e2ec997
E
1464 /**
1465 * Test for replaceContributionTokens.
1466 * This function tests whether the contribution tokens are replaced with values from contribution.
1467 */
1468 public function testReplaceContributionTokens() {
875e076b
JG
1469 $customGroup = $this->customGroupCreate(['extends' => 'Contribution']);
1470 $customField = $this->customFieldOptionValueCreate($customGroup, 'myCustomField');
7e2ec997
E
1471 $contactId1 = $this->individualCreate();
1472 $params = array(
1473 'contact_id' => $contactId1,
1474 'receive_date' => '20120511',
1475 'total_amount' => 100.00,
1476 'financial_type_id' => 1,
1477 'trxn_id' => 12345,
1478 'invoice_id' => 67890,
1479 'source' => 'SSF',
1480 'contribution_status_id' => 2,
875e076b 1481 "custom_{$customField['id']}" => 'value1',
7e2ec997
E
1482 );
1483 $contribution1 = $this->contributionCreate($params);
1484 $contactId2 = $this->individualCreate();
1485 $params = array(
1486 'contact_id' => $contactId2,
1487 'receive_date' => '20150511',
1488 'total_amount' => 200.00,
1489 'financial_type_id' => 1,
1490 'trxn_id' => 6789,
1491 'invoice_id' => 12345,
1492 'source' => 'ABC',
1493 'contribution_status_id' => 1,
875e076b 1494 "custom_{$customField['id']}" => 'value2',
7e2ec997
E
1495 );
1496 $contribution2 = $this->contributionCreate($params);
1497 $ids = array($contribution1, $contribution2);
1498
1499 $subject = "This is a test for contribution ID: {contribution.contribution_id}";
1500 $text = "Contribution Amount: {contribution.total_amount}";
1501 $html = "<p>Contribution Source: {contribution.contribution_source}</p></br>
1502 <p>Contribution Invoice ID: {contribution.invoice_id}</p></br>
875e076b
JG
1503 <p>Contribution Receive Date: {contribution.receive_date}</p></br>
1504 <p>Contribution Custom Field 1: {contribution.custom_1}</p></br>";
7e2ec997
E
1505
1506 $subjectToken = CRM_Utils_Token::getTokens($subject);
1507 $messageToken = CRM_Utils_Token::getTokens($text);
1508 $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html));
1509
1510 $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens(
1511 $ids,
1512 $subject,
1513 $subjectToken,
1514 $text,
1515 $html,
1516 $messageToken,
1517 TRUE
1518 );
1519
1520 $this->assertEquals("Contribution Amount: $ 100.00", $contributionDetails[$contactId1]['text'], "The text does not match");
1521 $this->assertEquals("<p>Contribution Source: ABC</p></br>
1522 <p>Contribution Invoice ID: 12345</p></br>
875e076b
JG
1523 <p>Contribution Receive Date: May 11th, 2015</p></br>
1524 <p>Contribution Custom Field 1: Label2</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
7e2ec997
E
1525 }
1526
7b5169d0
PN
1527 /**
1528 * Test for contribution with deferred revenue.
1529 */
1530 public function testContributionWithDeferredRevenue() {
1531 $contactId = $this->individualCreate();
1532 Civi::settings()->set('deferred_revenue_enabled', TRUE);
1533 $params = array(
1534 'contact_id' => $contactId,
1535 'receive_date' => '20120511',
1536 'total_amount' => 100.00,
1537 'financial_type_id' => 'Event Fee',
1538 'trxn_id' => 12345,
1539 'invoice_id' => 67890,
1540 'source' => 'SSF',
1541 'contribution_status_id' => 'Completed',
1542 'revenue_recognition_date' => date('Ymd', strtotime("+3 month")),
1543 );
1544 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1545
1546 $this->callAPISuccessGetCount('EntityFinancialTrxn', array(
1547 'entity_table' => "civicrm_contribution",
1548 'entity_id' => $contribution['id'],
1549 ), 2);
1550
1551 $checkAgainst = array(
1552 'financial_trxn_id.to_financial_account_id.name' => 'Deferred Revenue - Event Fee',
1553 'financial_trxn_id.from_financial_account_id.name' => 'Event Fee',
f818aed5 1554 'financial_trxn_id' => '2',
7b5169d0
PN
1555 );
1556 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
1557 'return' => array(
1558 "financial_trxn_id.from_financial_account_id.name",
1559 "financial_trxn_id.to_financial_account_id.name",
1560 "financial_trxn_id",
1561 ),
1562 'entity_table' => "civicrm_contribution",
1563 'entity_id' => $contribution['id'],
1564 'financial_trxn_id.is_payment' => 0,
1565 ), $checkAgainst);
1566
1567 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
1568 'entity_table' => "civicrm_financial_item",
1569 'financial_trxn_id' => $result['financial_trxn_id'],
1570 'return' => array('entity_id'),
1571 ));
1572
1573 $checkAgainst = array(
1574 'financial_account_id.name' => 'Deferred Revenue - Event Fee',
1575 'id' => $result['entity_id'],
1576 );
1577 $result = $this->callAPISuccessGetSingle('FinancialItem', array(
1578 'id' => $result['entity_id'],
1579 'return' => array("financial_account_id.name"),
1580 ), $checkAgainst);
1581 }
1582
a495d83a
PN
1583 /**
1584 * https://lab.civicrm.org/dev/financial/issues/56
1585 * Changing financial type on a contribution records correct financial items
1586 */
1587 public function testChangingFinancialTypeWithoutTax() {
1588 $ids = $values = [];
1589 $contactId = $this->individualCreate();
1590 $params = array(
1591 'contact_id' => $contactId,
1592 'receive_date' => date('YmdHis'),
1593 'total_amount' => 100.00,
1594 'financial_type_id' => 'Donation',
1595 'contribution_status_id' => 'Completed',
1596 );
1597 /* first test the scenario when sending an email */
1598 $contributionId = $this->callAPISuccess(
1599 'contribution',
1600 'create',
1601 $params
1602 )['id'];
1603
1604 // Update Financial Type.
1605 $this->callAPISuccess('contribution', 'create', [
1606 'id' => $contributionId,
1607 'financial_type_id' => 'Event Fee',
1608 ]);
1609
1610 // Get line item
1611 $lineItem = $this->callAPISuccessGetSingle('LineItem', [
1612 'contribution_id' => $contributionId,
1613 'return' => ["financial_type_id.name", "line_total"],
1614 ]);
1615
1616 $this->assertEquals(
1617 $lineItem['line_total'],
1618 100.00,
1619 'Invalid line amount.'
1620 );
1621
1622 $this->assertEquals(
1623 $lineItem['financial_type_id.name'],
1624 'Event Fee',
1625 'Invalid Financial Type stored.'
1626 );
1627
1628 // Get Financial Items.
1629 $financialItems = $this->callAPISuccess('FinancialItem', 'get', [
1630 'entity_id' => $lineItem['id'],
1631 'sequential' => 1,
1632 'entity_table' => 'civicrm_line_item',
1633 'options' => ['sort' => "id"],
1634 'return' => ["financial_account_id.name", "amount", "description"],
1635 ]);
1636
1637 $this->assertEquals($financialItems['count'], 3, 'Count mismatch.');
1638
1639 $toCheck = [
1640 ['Donation', 100.00],
1641 ['Donation', -100.00],
1642 ['Event Fee', 100.00],
1643 ];
1644
1645 foreach ($financialItems['values'] as $key => $values) {
1646 $this->assertEquals(
1647 $values['financial_account_id.name'],
1648 $toCheck[$key][0],
1649 'Invalid Financial Account stored.'
1650 );
1651 $this->assertEquals(
1652 $values['amount'],
1653 $toCheck[$key][1],
1654 'Amount mismatch.'
1655 );
1656 $this->assertEquals(
1657 $values['description'],
1658 'Contribution Amount',
1659 'Description mismatch.'
1660 );
1661 }
1662
1663 // Check transactions.
1664 $financialTransactions = $this->callAPISuccess('EntityFinancialTrxn', 'get', [
1665 'return' => ["financial_trxn_id"],
1666 'entity_table' => "civicrm_contribution",
1667 'entity_id' => $contributionId,
1668 'sequential' => 1,
1669 ]);
1670 $this->assertEquals($financialTransactions['count'], 3, 'Count mismatch.');
1671
1672 foreach ($financialTransactions['values'] as $key => $values) {
1673 $this->callAPISuccessGetCount('EntityFinancialTrxn', [
1674 'financial_trxn_id' => $values['financial_trxn_id'],
1675 'amount' => $toCheck[$key][1],
1676 'financial_trxn_id.total_amount' => $toCheck[$key][1],
1677 ], 2);
1678 }
1679 }
1680
1db3ddea
KE
1681 /**
1682 * CRM-21424 Check if the receipt update is set after composing the receipt message
1683 */
1684 public function testSendMailUpdateReceiptDate() {
1685 $ids = $values = array();
1686 $contactId = $this->individualCreate();
1687 $params = array(
1688 'contact_id' => $contactId,
1689 'receive_date' => '20120511',
1690 'total_amount' => 100.00,
1691 'financial_type_id' => 'Donation',
1692 'source' => 'SSF',
1693 'contribution_status_id' => 'Completed',
1694 );
1695 /* first test the scenario when sending an email */
1696 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1697 $contributionId = $contribution['id'];
1698 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null');
1699 $input = array('receipt_update' => 0);
1700 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values);
1701 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null');
1702 $input = array('receipt_update' => 1);
1703 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values);
1704 $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set');
1705
1706 /* repeat the same scenario for downloading a pdf */
1707 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1708 $contributionId = $contribution['id'];
1709 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null');
1710 $input = array('receipt_update' => 0);
1711 /* setting the lasast parameter (returnmessagetext) to TRUE is done by the download of the pdf */
1712 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE);
1713 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null');
1714 $input = array('receipt_update' => 1);
1715 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE);
1716 $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set');
1717 }
1718
6a488035 1719}