3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Contribute_BAO_ContributionTest
32 class CRM_Contribute_BAO_ContributionTest
extends CiviUnitTestCase
{
34 use CRMTraits_Financial_FinancialACLTrait
;
35 use CRMTraits_Financial_PriceSetTrait
;
38 * Clean up after tests.
40 public function tearDown() {
41 $this->quickCleanUpFinancialEntities();
46 * Test create method (create and update modes).
48 public function testCreate() {
49 $contactId = $this->individualCreate();
52 'contact_id' => $contactId,
54 'financial_type_id' => 1,
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,
64 'trxn_id' => '22ereerwww444444',
65 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
66 'thankyou_date' => '20080522',
70 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
75 //update contribution amount
76 $params['id'] = $contribution['id'];
77 $params['fee_amount'] = 10;
78 $params['net_amount'] = 190;
80 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
87 * Create() method with custom data.
89 public function testCreateWithCustomData() {
90 $contactId = $this->individualCreate();
93 $customGroup = $this->customGroupCreate(['extends' => 'Contribution']);
94 $customGroupID = $customGroup['id'];
95 $customGroup = $customGroup['values'][$customGroupID];
99 'data_type' => 'String',
100 'html_type' => 'Text',
102 'custom_group_id' => $customGroupID,
104 $customField = CRM_Core_BAO_CustomField
::create($fields);
107 'contact_id' => $contactId,
109 'financial_type_id' => 1,
110 'contribution_status_id' => 1,
111 'payment_instrument_id' => 1,
112 'source' => 'STUDENT',
113 'receive_date' => '20080522000000',
114 'receipt_date' => '20080522000000',
116 'non_deductible_amount' => 0.00,
117 'total_amount' => 200.00,
120 'trxn_id' => '22ereerwww322323',
121 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
122 'thankyou_date' => '20080522',
123 'skipCleanMoney' => TRUE,
126 $params['custom'] = [
127 $customField->id
=> [
129 'value' => 'Test custom value',
131 'custom_field_id' => $customField->id
,
132 'custom_group_id' => $customGroupID,
133 'table_name' => $customGroup['table_name'],
134 'column_name' => $customField->column_name
,
140 $contribution = CRM_Contribute_BAO_Contribution
::create($params);
142 // Check that the custom field value is saved
143 $customValueParams = [
144 'entityID' => $contribution->id
,
145 'custom_' . $customField->id
=> 1,
147 $values = CRM_Core_BAO_CustomValueTable
::getValues($customValueParams);
148 $this->assertEquals('Test custom value', $values['custom_' . $customField->id
], 'Check the custom field value');
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.');
155 * CRM-21026 Test ContributionCount after contribution created with disabled FT
157 public function testContributionCountDisabledFinancialType() {
158 $contactId = $this->individualCreate();
160 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7),
164 $finType = $this->callAPISuccess('financial_type', 'create', $financialType);
166 'contact_id' => $contactId,
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',
175 'non_deductible_amount' => 0.00,
176 'total_amount' => 200.00,
179 'trxn_id' => '22ereerwww322323',
180 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
181 'thankyou_date' => '20080522',
183 $this->callAPISuccess('Contribution', 'create', $params);
184 $this->callAPISuccess('financial_type', 'create', ['is_active' => 0, 'id' => $finType['id']]);
185 $contributionCount = CRM_Contribute_BAO_Contribution
::contributionCount($contactId);
186 $this->assertEquals(1, $contributionCount);
190 * DeleteContribution() method
192 public function testDeleteContribution() {
193 $contactId = $this->individualCreate();
196 'contact_id' => $contactId,
198 'financial_type_id' => 1,
199 'contribution_status_id' => 1,
200 'payment_instrument_id' => 1,
201 'source' => 'STUDENT',
202 'receive_date' => '20080522000000',
203 'receipt_date' => '20080522000000',
205 'non_deductible_amount' => 0.00,
206 'total_amount' => 200.00,
209 'trxn_id' => '33ereerwww322323',
210 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
211 'thankyou_date' => '20080522',
212 'sequential' => TRUE,
215 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
217 CRM_Contribute_BAO_Contribution
::deleteContribution($contribution['id']);
219 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution['trxn_id'],
220 'id', 'trxn_id', 'Database check for deleted Contribution.'
225 * Create honor-contact method.
227 public function testCreateAndGetHonorContact() {
228 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
229 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
230 $email = "{$firstName}.{$lastName}@example.com";
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
;
242 'first_name' => $firstName,
243 'last_name' => $lastName,
246 $softParam = ['soft_credit_type_id' => 1];
249 $honoreeContactId = CRM_Contact_BAO_Contact
::createProfileContact($params, $null,
250 NULL, NULL, $honoreeProfileId
253 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
254 'Database check for created honor contact record.'
256 //create contribution on behalf of honary.
258 $contactId = $this->individualCreate(['first_name' => 'John', 'last_name' => 'Doe']);
261 'contact_id' => $contactId,
263 'financial_type_id' => 4,
264 'contribution_status_id' => 1,
265 'receive_date' => date('Ymd'),
266 'total_amount' => 66,
270 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
271 $id = $contribution['id'];
272 $softParam['contact_id'] = $honoreeContactId;
273 $softParam['contribution_id'] = $id;
274 $softParam['currency'] = $contribution['currency'];
275 $softParam['amount'] = $contribution['total_amount'];
277 //Create Soft Contribution for honoree contact
278 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
280 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
281 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
283 //get honorary information
284 $getHonorContact = CRM_Contribute_BAO_Contribution
::getHonorContacts($honoreeContactId);
285 $this->assertEquals([
287 'honor_type' => 'In Honor of',
288 'honorId' => $contactId,
289 'display_name' => 'Mr. John Doe II',
290 'type' => 'Event Fee',
292 'amount' => '$ 66.00',
294 'receive_date' => date('Y-m-d 00:00:00'),
295 'contribution_status' => 'Completed',
297 ], $getHonorContact);
299 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
300 'Database check for created honor contact record.'
303 //get annual contribution information
304 $annual = CRM_Contribute_BAO_Contribution
::annual($contactId);
306 $currencySymbol = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency', CRM_Core_Config
::singleton()->defaultCurrency
, 'symbol', 'name');
307 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
308 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
313 * Test that financial type data is not added to the annual query if acls not enabled.
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);
324 // Run it to make sure it's not bad sql.
325 CRM_Core_DAO
::executeQuery($sql);
326 $this->disableFinancialACLs();
330 * Test the annual query returns a correct result when multiple line items are present.
332 public function testAnnualWithMultipleLineItems() {
333 $contactID = $this->createLoggedInUserWithFinancialACL();
334 $this->createContributionWithTwoLineItemsAgainstPriceSet([
335 'contact_id' => $contactID,
338 $this->enableFinancialACLs();
339 $sql = CRM_Contribute_BAO_Contribution
::getAnnualQuery([$contactID]);
340 $result = CRM_Core_DAO
::executeQuery($sql);
342 $this->assertEquals(300, $result->amount
);
343 $this->assertEquals(1, $result->count
);
344 $this->disableFinancialACLs();
348 * Test that financial type data is not added to the annual query if acls not enabled.
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);
361 * Test that financial type data is not added to the annual query if acls not enabled.
363 public function testAnnualQueryWithFinancialHook() {
364 $this->hookClass
->setHook('civicrm_selectWhereClause', [$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);
374 * Add ACL denying values LIKE '0'.
376 * @param string $entity
377 * @param string $clauses
379 public function aclIdNoZero($entity, &$clauses) {
380 if ($entity != 'Contribution') {
383 $clauses['id'] = "NOT IN (0)";
387 * Display sort name during.
388 * Update multiple contributions
391 public function testsortName() {
393 'first_name' => 'Shane',
394 'last_name' => 'Whatson',
395 'contact_type' => 'Individual',
398 $contact = CRM_Contact_BAO_Contact
::add($params);
400 //Now check $contact is object of contact DAO..
401 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
403 $contactId = $contact->id
;
405 'contact_id' => $contactId,
407 'financial_type_id' => 1,
408 'contribution_status_id' => 1,
409 'payment_instrument_id' => 1,
410 'source' => 'STUDENT',
411 'receive_date' => '20080522000000',
412 'receipt_date' => '20080522000000',
414 'non_deductible_amount' => 0.00,
415 'total_amount' => 300.00,
418 'trxn_id' => '22ereerwww323',
419 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
420 'thankyou_date' => '20080522',
421 'sequential' => TRUE,
424 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
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.');
429 //display sort name during Update multiple contributions
430 $sortName = CRM_Contribute_BAO_Contribution
::sortName($contribution['id']);
432 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
436 * Add premium during online Contribution.
440 public function testAddPremium() {
441 $contactId = $this->individualCreate();
444 'name' => 'TEST Premium',
446 'imageOption' => 'noImage',
447 'MAX_FILE_SIZE' => 2097152,
450 'min_contribution' => 100,
453 $premium = CRM_Contribute_BAO_Product
::create($params);
455 $this->assertEquals('TEST Premium', $premium->name
, 'Check for premium name.');
457 $contributionParams = [
458 'contact_id' => $contactId,
460 'financial_type_id' => 1,
461 'contribution_status_id' => 1,
462 'payment_instrument_id' => 1,
463 'source' => 'STUDENT',
464 'receive_date' => '20080522000000',
465 'receipt_date' => '20080522000000',
467 'non_deductible_amount' => 0.00,
468 'total_amount' => 300.00,
471 'trxn_id' => '33erdfrwvw434',
472 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
473 'thankyou_date' => '20080522',
474 'sequential' => TRUE,
476 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0];
478 $this->assertEquals($contributionParams['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
479 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
481 //parameter for adding premium to contribution
483 'product_id' => $premium->id
,
484 'contribution_id' => $contribution['id'],
485 'product_option' => NULL,
488 $contributionProduct = CRM_Contribute_BAO_Contribution
::addPremium($data);
489 $this->assertEquals($contributionProduct->product_id
, $premium->id
, 'Check for Product id .');
492 CRM_Contribute_BAO_Product
::del($premium->id
);
493 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name
,
494 'id', 'name', 'Database check for deleted Product.'
499 * Check duplicate contribution id.
500 * during the contribution import
501 * checkDuplicateIds();
503 public function testcheckDuplicateIds() {
504 $contactId = $this->individualCreate();
507 'contact_id' => $contactId,
509 'financial_type_id' => 1,
510 'contribution_status_id' => 1,
511 'payment_instrument_id' => 1,
512 'source' => 'STUDENT',
513 'receive_date' => '20080522000000',
514 'receipt_date' => '20080522000000',
516 'non_deductible_amount' => 0.00,
517 'total_amount' => 300.00,
520 'trxn_id' => '76ereeswww835',
521 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
522 'thankyou_date' => '20080522',
523 'sequential' => TRUE,
526 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
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.');
531 'id' => $contribution['id'],
532 'trxn_id' => $contribution['trxn_id'],
533 'invoice_id' => $contribution['invoice_id'],
535 $contributionID = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($data);
536 $this->assertEquals($contributionID, $contribution['id'], 'Check for duplicate transcation id .');
540 * Check credit note id creation
541 * when a contribution is cancelled or refunded
542 * createCreditNoteId();
544 public function testCreateCreditNoteId() {
545 $contactId = $this->individualCreate();
548 'contact_id' => $contactId,
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',
557 'non_deductible_amount' => 0.00,
558 'total_amount' => 300.00,
561 'trxn_id' => '76ereeswww835',
562 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
563 'thankyou_date' => '20080522',
564 'sequential' => TRUE,
567 $creditNoteId = CRM_Contribute_BAO_Contribution
::createCreditNoteId();
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.');
574 * Create() method (create and update modes).
576 public function testIsPaymentFlag() {
577 $contactId = $this->individualCreate();
580 'contact_id' => $contactId,
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,
592 'trxn_id' => '22ereerwww4444xx',
593 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
594 'thankyou_date' => '20080522',
595 'sequential' => TRUE,
597 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
603 'trxn_id' => $params['trxn_id'],
607 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
608 $this->assertEquals(1, $financialTrxn->N
, 'Mismatch count for is payment flag.');
609 //update contribution amount
610 $params['id'] = $contribution['id'];
611 $params['total_amount'] = 150;
612 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
617 'trxn_id' => $params['trxn_id'],
621 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
622 $this->assertEquals(2, $financialTrxn->N
, 'Mismatch count for is payment flag.');
623 $trxnArray['is_payment'] = 0;
624 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
625 $this->assertEquals(1, $financialTrxn->N
, 'Mismatch count for is payment flag.');
629 * Create() method (create and update modes).
631 public function testIsPaymentFlagForPending() {
632 $contactId = $this->individualCreate();
635 'contact_id' => $contactId,
637 'financial_type_id' => 1,
638 'contribution_status_id' => 2,
639 'payment_instrument_id' => 1,
640 'source' => 'STUDENT',
642 'receive_date' => '20080522000000',
643 'receipt_date' => '20080522000000',
644 'non_deductible_amount' => 0.00,
645 'total_amount' => 200.00,
648 'trxn_id' => '22ereerwww4444yy',
649 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
650 'thankyou_date' => '20080522',
651 'sequential' => TRUE,
654 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
660 'trxn_id' => $params['trxn_id'],
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
670 $params['id'] = $contribution['id'];
671 $params['contribution_status_id'] = 1;
673 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
678 'trxn_id' => $params['trxn_id'],
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.');
690 * addPayments() method (add and edit modes of participant)
692 public function testAddPayments() {
693 list($lineItems, $contribution) = $this->addParticipantWithContribution();
694 CRM_Contribute_BAO_Contribution
::addPayments([$contribution]);
695 $this->checkItemValues($contribution);
699 * checks db values for financial item
701 public function checkItemValues($contribution) {
702 $toFinancialAccount = CRM_Contribute_PseudoConstant
::getRelationalFinancialAccount(4, 'Accounts Receivable Account is');
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')
704 INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
705 WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
707 $queryParams[1] = [$contribution->id
, 'Integer'];
708 $queryParams[2] = [$toFinancialAccount, 'Integer'];
710 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
711 $amounts = [100.00, 50.00];
712 while ($dao->fetch()) {
713 $this->assertEquals(150.00, $dao->total_amount
, 'Mismatch of total amount paid.');
714 $this->assertEquals($dao->amount
, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
719 * assignProportionalLineItems() method (add and edit modes of participant)
721 public function testAssignProportionalLineItems() {
722 list($lineItems, $contribution) = $this->addParticipantWithContribution();
724 'contribution_id' => $contribution->id
,
725 'total_amount' => 150.00,
727 $trxn = new CRM_Financial_DAO_FinancialTrxn();
728 $trxn->orderBy('id DESC');
730 CRM_Contribute_BAO_Contribution
::assignProportionalLineItems($params, $trxn->id
, $contribution->total_amount
);
731 $this->checkItemValues($contribution);
735 * Add participant with contribution
739 public function addParticipantWithContribution() {
740 // creating price set, price field
741 $this->_contactId
= $this->individualCreate();
742 $event = $this->eventCreate();
743 $this->_eventId
= $event['id'];
744 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
745 $paramsSet['name'] = CRM_Utils_String
::titleToVar($paramsSet['title']);
746 $paramsSet['is_active'] = TRUE;
747 $paramsSet['financial_type_id'] = 4;
748 $paramsSet['extends'] = 1;
750 $priceset = CRM_Price_BAO_PriceSet
::create($paramsSet);
751 $priceSetId = $priceset->id
;
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'
758 'label' => 'Price Field',
759 'name' => CRM_Utils_String
::titleToVar('Price Field'),
760 'html_type' => 'CheckBox',
761 'option_label' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
762 'option_value' => ['1' => 100, '2' => 200],
763 'option_name' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
764 'option_weight' => ['1' => 1, '2' => 2],
765 'option_amount' => ['1' => 100, '2' => 200],
766 'is_display_amounts' => 1,
768 'options_per_line' => 1,
769 'is_active' => ['1' => 1, '2' => 1],
770 'price_set_id' => $priceset->id
,
772 'financial_type_id' => CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
774 $priceField = CRM_Price_BAO_PriceField
::create($paramsField);
776 'id' => $this->_eventId
,
777 'financial_type_id' => 4,
780 CRM_Event_BAO_Event
::create($eventParams);
781 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_eventId
, $priceSetId);
783 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id
]);
784 $participantParams = [
785 'financial_type_id' => 4,
786 'event_id' => $this->_eventId
,
789 'fee_currency' => 'USD',
790 'contact_id' => $this->_contactId
,
792 $participant = CRM_Event_BAO_Participant
::add($participantParams);
793 $contributionParams = [
794 'total_amount' => 150,
796 'contact_id' => $this->_contactId
,
797 'financial_type_id' => 4,
798 'contribution_status_id' => 1,
799 'partial_payment_total' => 300.00,
800 'partial_amount_to_pay' => 150,
801 'contribution_mode' => 'participant',
802 'participant_id' => $participant->id
,
803 'sequential' => TRUE,
806 foreach ($priceFields['values'] as $key => $priceField) {
807 $lineItems[1][$key] = [
808 'price_field_id' => $priceField['price_field_id'],
809 'price_field_value_id' => $priceField['id'],
810 'label' => $priceField['label'],
811 'field_title' => $priceField['label'],
813 'unit_price' => $priceField['amount'],
814 'line_total' => $priceField['amount'],
815 'financial_type_id' => $priceField['financial_type_id'],
818 $contributionParams['line_item'] = $lineItems;
819 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0];
821 $paymentParticipant = [
822 'participant_id' => $participant->id
,
823 'contribution_id' => $contribution['id'],
825 CRM_Event_BAO_ParticipantPayment
::create($paymentParticipant);
827 $contributionObject = new CRM_Contribute_BAO_Contribution();
828 $contributionObject->id
= $contribution['id'];
829 $contributionObject->find(TRUE);
831 return [$lineItems, $contributionObject];
835 * checkLineItems() check if total amount matches the sum of line total
837 public function testcheckLineItems() {
840 'receive_date' => '2010-01-20',
841 'total_amount' => 100,
842 'financial_type_id' => 3,
847 'entity_table' => 'civicrm_contribution',
848 'price_field_id' => 8,
849 'price_field_value_id' => 16,
856 'entity_table' => 'civicrm_contribution',
857 'price_field_id' => 8,
858 'price_field_value_id' => 17,
863 'financial_type_id' => 1,
872 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
873 $this->fail("Missed expected exception");
875 catch (CRM_Contribute_Exception_CheckLineItemsException
$e) {
877 CRM_Contribute_Exception_CheckLineItemsException
::LINE_ITEM_DIFFERRING_TOTAL_EXCEPTON_MSG
,
882 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
883 $params['total_amount'] = 300;
885 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
889 * Tests CRM_Contribute_BAO_Contribution::checkLineItems() method works with
890 * floating point values.
892 public function testCheckLineItemsWithFloatingPointValues() {
895 'receive_date' => date('Y-m-d'),
896 'total_amount' => 16.67,
897 'financial_type_id' => 3,
902 'entity_table' => 'civicrm_contribution',
903 'price_field_id' => 8,
904 'price_field_value_id' => 16,
907 'unit_price' => 14.85,
908 'line_total' => 14.85,
911 'entity_table' => 'civicrm_contribution',
912 'price_field_id' => 8,
913 'price_field_value_id' => 17,
916 'unit_price' => 1.66,
917 'line_total' => 1.66,
918 'financial_type_id' => 1,
921 'entity_table' => 'civicrm_contribution',
922 'price_field_id' => 8,
923 'price_field_value_id' => 17,
926 'unit_price' => 0.16,
927 'line_total' => 0.16,
928 'financial_type_id' => 1,
936 $foundException = FALSE;
939 CRM_Contribute_BAO_Contribution
::checkLineItems($params);
941 catch (CRM_Contribute_Exception_CheckLineItemsException
$e) {
942 $foundException = TRUE;
945 $this->assertFalse($foundException);
949 * Test activity amount updation.
951 public function testActivityCreate() {
952 $contactId = $this->individualCreate();
956 'contact_id' => $contactId,
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',
972 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
977 // Check amount in activity.
979 'source_record_id' => $contribution['id'],
980 'activity_type_id' => CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contribution'),
982 // @todo use api instead.
983 $activity = CRM_Activity_BAO_Activity
::retrieve($activityParams, $defaults);
985 $this->assertEquals($contribution['id'], $activity->source_record_id
, 'Check for activity associated with contribution.');
986 $this->assertEquals("$ 100.00 - STUDENT", $activity->subject
, 'Check for total amount in activity.');
988 $params['id'] = $contribution['id'];
989 $params['total_amount'] = 200;
991 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
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.');
996 // Retrieve activity again.
997 $activity = CRM_Activity_BAO_Activity
::retrieve($activityParams, $defaults);
999 $this->assertEquals($contribution['id'], $activity->source_record_id
, 'Check for activity associated with contribution.');
1000 $this->assertEquals("$ 200.00 - STUDENT", $activity->subject
, 'Check for total amount in activity.');
1004 * Test allowUpdateRevenueRecognitionDate.
1006 public function testAllowUpdateRevenueRecognitionDate() {
1007 $contactId = $this->individualCreate();
1009 'contact_id' => $contactId,
1010 'receive_date' => '2010-01-20',
1011 'total_amount' => 100,
1012 'financial_type_id' => 4,
1014 $order = $this->callAPISuccess('order', 'create', $params);
1015 $allowUpdate = CRM_Contribute_BAO_Contribution
::allowUpdateRevenueRecognitionDate($order['id']);
1016 $this->assertTrue($allowUpdate);
1018 $event = $this->eventCreate();
1020 'contact_id' => $contactId,
1021 'receive_date' => '2010-01-20',
1022 'total_amount' => 300,
1023 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
1024 'contribution_status_id' => 'Completed',
1026 $priceFields = $this->createPriceSet('event', $event['id']);
1027 foreach ($priceFields['values'] as $key => $priceField) {
1028 $lineItems[$key] = [
1029 'price_field_id' => $priceField['price_field_id'],
1030 'price_field_value_id' => $priceField['id'],
1031 'label' => $priceField['label'],
1032 'field_title' => $priceField['label'],
1034 'unit_price' => $priceField['amount'],
1035 'line_total' => $priceField['amount'],
1036 'financial_type_id' => $priceField['financial_type_id'],
1037 'entity_table' => 'civicrm_participant',
1040 $params['line_items'][] = [
1041 'line_item' => $lineItems,
1043 'contact_id' => $contactId,
1044 'event_id' => $event['id'],
1047 'register_date' => '2007-07-21 00:00:00',
1048 'source' => 'Online Event Registration: API Testing',
1051 $order = $this->callAPISuccess('order', 'create', $params);
1052 $allowUpdate = CRM_Contribute_BAO_Contribution
::allowUpdateRevenueRecognitionDate($order['id']);
1053 $this->assertFalse($allowUpdate);
1056 'contact_id' => $contactId,
1057 'receive_date' => '2010-01-20',
1058 'total_amount' => 200,
1059 'financial_type_id' => $this->getFinancialTypeId('Member Dues'),
1060 'contribution_status_id' => 'Completed',
1062 $membershipType = $this->membershipTypeCreate();
1063 $priceFields = $this->createPriceSet();
1065 foreach ($priceFields['values'] as $key => $priceField) {
1066 $lineItems[$key] = [
1067 'price_field_id' => $priceField['price_field_id'],
1068 'price_field_value_id' => $priceField['id'],
1069 'label' => $priceField['label'],
1070 'field_title' => $priceField['label'],
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,
1079 $params['line_items'][] = [
1080 'line_item' => [array_pop($lineItems)],
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',
1092 $order = $this->callAPISuccess('order', 'create', $params);
1093 $allowUpdate = CRM_Contribute_BAO_Contribution
::allowUpdateRevenueRecognitionDate($order['id']);
1094 $this->assertFalse($allowUpdate);
1098 * Test calculateFinancialItemAmount().
1100 public function testcalculateFinancialItemAmount() {
1105 'line_total' => 100,
1106 'previous_line_total' => 300,
1109 'context' => 'changedAmount',
1110 'expectedItemAmount' => -200,
1115 'line_total' => 100,
1116 'previous_line_total' => 100,
1119 // Most contexts are ignored. Removing refs to change payment instrument so placeholder.
1120 'context' => 'not null',
1121 'expectedItemAmount' => -100,
1125 'is_quick_config' => TRUE,
1126 'total_amount' => 110,
1130 'item_amount' => 100,
1132 'context' => 'changedAmount',
1133 'expectedItemAmount' => 100,
1137 'is_quick_config' => TRUE,
1138 'total_amount' => 110,
1142 'item_amount' => NULL,
1144 'context' => 'changedAmount',
1145 'expectedItemAmount' => 110,
1149 'is_quick_config' => TRUE,
1150 'total_amount' => 110,
1154 'item_amount' => NULL,
1157 'expectedItemAmount' => 100,
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.');
1167 * Test recording of amount with comma separator.
1169 public function testCommaSeparatorAmount() {
1170 $contactId = $this->individualCreate();
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',
1182 'partial_amount_to_pay' => '8,000.00',
1185 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
1186 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
1187 $financialTrxn = $this->callAPISuccessGetSingle(
1190 'id' => $lastFinancialTrxnId['financialTrxnId'],
1191 'return' => ['total_amount'],
1194 $this->assertEquals($financialTrxn['total_amount'], 8000, 'Invalid Tax amount.');
1198 * Test for function getSalesTaxFinancialAccounts().
1200 public function testgetSalesTaxFinancialAccounts() {
1201 $this->enableTaxAndInvoicing();
1202 $financialType = $this->createFinancialType();
1203 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1204 $expectedResult = [$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.');
1213 * Test for function createProportionalEntry().
1215 * @param string $thousandSeparator
1216 * punctuation used to refer to thousands.
1218 * @dataProvider getThousandSeparators
1220 public function testCreateProportionalEntry($thousandSeparator) {
1221 $this->setCurrencySeparators($thousandSeparator);
1222 list($contribution, $financialAccount) = $this->createContributionWithTax();
1224 'total_amount' => 55,
1225 'to_financial_account_id' => $financialAccount->financial_account_id
,
1226 'payment_instrument_id' => 1,
1227 'trxn_date' => date('Ymd'),
1229 'entity_id' => $contribution['id'],
1231 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1233 'contribution_total_amount' => $contribution['total_amount'],
1234 'trxn_total_amount' => 55,
1235 'line_item_amount' => 100,
1237 $previousLineItem = CRM_Financial_BAO_FinancialItem
::getPreviousFinancialItem($contribution['id']);
1239 'entity_table' => 'civicrm_financial_item',
1240 'entity_id' => $previousLineItem['id'],
1241 'financial_trxn_id' => (string) $financialTrxn['id'],
1243 CRM_Contribute_BAO_Contribution
::createProportionalEntry($entityParams, $eftParams);
1244 $trxnTestArray = array_merge($eftParams, [
1245 'amount' => '50.00',
1247 $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
1251 * Test for function createProportionalEntry with zero amount().
1253 * @param string $thousandSeparator
1254 * punctuation used to refer to thousands.
1256 * @dataProvider getThousandSeparators
1258 public function testCreateProportionalEntryZeroAmount($thousandSeparator) {
1259 $this->setCurrencySeparators($thousandSeparator);
1260 list($contribution, $financialAccount) = $this->createContributionWithTax(['total_amount' => 0]);
1262 'total_amount' => 0,
1263 'to_financial_account_id' => $financialAccount->financial_account_id
,
1264 'payment_instrument_id' => 1,
1265 'trxn_date' => date('Ymd'),
1267 'entity_id' => $contribution['id'],
1269 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1271 'contribution_total_amount' => $contribution['total_amount'],
1272 'trxn_total_amount' => 0,
1273 'line_item_amount' => 0,
1275 $previousLineItem = CRM_Financial_BAO_FinancialItem
::getPreviousFinancialItem($contribution['id']);
1277 'entity_table' => 'civicrm_financial_item',
1278 'entity_id' => $previousLineItem['id'],
1279 'financial_trxn_id' => (string) $financialTrxn['id'],
1281 CRM_Contribute_BAO_Contribution
::createProportionalEntry($entityParams, $eftParams);
1282 $trxnTestArray = array_merge($eftParams, [
1285 $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
1289 * Test for function getLastFinancialItemIds().
1291 public function testgetLastFinancialItemIds() {
1292 list($contribution, $financialAccount) = $this->createContributionWithTax();
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.');
1302 * Test to ensure proportional entries are creating when adding a payment..
1304 * In this test we create a pending contribution for $110 consisting of $100 contribution and $10 tax.
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).
1310 public function testCreateProportionalFinancialEntriesViaPaymentCreate() {
1311 list($contribution, $financialAccount) = $this->createContributionWithTax([], FALSE);
1313 'total_amount' => 50,
1314 'to_financial_account_id' => $financialAccount->financial_account_id
,
1315 'payment_instrument_id' => 1,
1316 'trxn_date' => date('Ymd'),
1318 'entity_id' => $contribution['id'],
1319 'contribution_id' => $contribution['id'],
1321 $financialTrxn = $this->callAPISuccess('Payment', 'create', $params);
1323 'entity_table' => 'civicrm_financial_item',
1324 'financial_trxn_id' => $financialTrxn['id'],
1326 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1327 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1328 $testAmount = [4.55, 45.45];
1329 foreach ($entityFinancialTrxn['values'] as $value) {
1330 $this->assertEquals(array_pop($testAmount), $value['amount'], 'Invalid amount stored in civicrm_entity_financial_trxn.');
1335 * Test to check if amount is proportionally asigned for PI change.
1337 public function testProportionallyAssignedForPIChange() {
1338 list($contribution, $financialAccount) = $this->createContributionWithTax();
1340 'id' => $contribution['id'],
1341 'payment_instrument_id' => 3,
1343 $this->callAPISuccess('Contribution', 'create', $params);
1344 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
1346 'entity_table' => 'civicrm_financial_item',
1347 'financial_trxn_id' => $lastFinancialTrxnId['financialTrxnId'],
1349 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1350 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1351 $testAmount = [10, 100];
1352 foreach ($entityFinancialTrxn['values'] as $value) {
1353 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
1358 * Function to create contribution with tax.
1360 public function createContributionWithTax($params = [], $isCompleted = TRUE) {
1361 if (!isset($params['total_amount'])) {
1362 $params['total_amount'] = 100;
1364 $contactId = $this->individualCreate();
1365 $this->enableTaxAndInvoicing();
1366 $financialType = $this->createFinancialType();
1367 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1368 $form = new CRM_Contribute_Form_Contribution();
1371 'total_amount' => $params['total_amount'],
1372 'financial_type_id' => $financialType['id'],
1373 'contact_id' => $contactId,
1374 'contribution_status_id' => $isCompleted ?
1 : 2,
1375 'price_set_id' => 0,
1376 ], CRM_Core_Action
::ADD
);
1377 $contribution = $this->callAPISuccessGetSingle('Contribution',
1379 'contact_id' => $contactId,
1380 'return' => ['tax_amount', 'total_amount'],
1383 return [$contribution, $financialAccount];
1387 * Test processOnBehalfOrganization() function.
1389 public function testProcessOnBehalfOrganization() {
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',
1399 $contactID = $this->individualCreate();
1400 $orgId = $this->organizationCreate(['organization_name' => 'testorg1']);
1401 $orgCount = $this->callAPISuccessGetCount('Contact', [
1402 'contact_type' => "Organization",
1403 'organization_name' => "testorg1",
1405 $this->assertEquals($orgCount, 1);
1407 $values = $params = [];
1408 $behalfOrganization = [
1409 'organization_name' => 'testorg1',
1412 'phone' => $orgInfo['phone'],
1418 'email' => $orgInfo['email'],
1424 'street_address' => $orgInfo['street_address'],
1425 'city' => $orgInfo['city'],
1426 'location_type_id' => 3,
1427 'postal_code' => $orgInfo['postal_code'],
1429 'state_province' => 'AA',
1435 'organization_name' => 1,
1438 'street_address-3' => 1,
1440 'postal_code-3' => 1,
1442 'state_province-3' => 1,
1444 CRM_Contribute_Form_Contribution_Confirm
::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
1446 //Check whether new organisation is not created.
1447 $result = $this->callAPISuccess('Contact', 'get', [
1448 'contact_type' => "Organization",
1449 'organization_name' => "testorg1",
1451 $this->assertEquals($result['count'], 1);
1453 //Assert all org values are updated.
1454 foreach ($orgInfo as $key => $val) {
1455 $this->assertEquals($result['values'][$orgId][$key], $val);
1458 //Check if alert is assigned to params if more than 1 dupe exists.
1459 $orgId = $this->organizationCreate(['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);
1465 * Test for replaceContributionTokens.
1466 * This function tests whether the contribution tokens are replaced with values from contribution.
1468 public function testReplaceContributionTokens() {
1469 $customGroup = $this->customGroupCreate(['extends' => 'Contribution', 'title' => 'contribution stuff']);
1470 $customField = $this->customFieldOptionValueCreate($customGroup, 'myCustomField');
1471 $contactId1 = $this->individualCreate();
1473 'contact_id' => $contactId1,
1474 'receive_date' => '20120511',
1475 'total_amount' => 100.00,
1476 'financial_type_id' => 1,
1478 'invoice_id' => 67890,
1480 'contribution_status_id' => 2,
1481 "custom_{$customField['id']}" => 'value1',
1483 $contribution1 = $this->contributionCreate($params);
1484 $contactId2 = $this->individualCreate();
1486 'contact_id' => $contactId2,
1487 'receive_date' => '20150511',
1488 'total_amount' => 200.00,
1489 'financial_type_id' => 1,
1491 'invoice_id' => 12345,
1493 'contribution_status_id' => 1,
1494 "custom_{$customField['id']}" => 'value2',
1496 $contribution2 = $this->contributionCreate($params);
1497 $ids = [$contribution1, $contribution2];
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>
1503 <p>Contribution Receive Date: {contribution.receive_date}</p></br>
1504 <p>Contribution Custom Field: {contribution.custom_{$customField['id']}}</p></br>";
1506 $subjectToken = CRM_Utils_Token
::getTokens($subject);
1507 $messageToken = CRM_Utils_Token
::getTokens($text);
1508 $messageToken = array_merge($messageToken, CRM_Utils_Token
::getTokens($html));
1510 $contributionDetails = CRM_Contribute_BAO_Contribution
::replaceContributionTokens(
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>
1523 <p>Contribution Receive Date: May 11th, 2015</p></br>
1524 <p>Contribution Custom Field: Label2</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
1528 * Test for contribution with deferred revenue.
1530 public function testContributionWithDeferredRevenue() {
1531 $contactId = $this->individualCreate();
1532 Civi
::settings()->set('deferred_revenue_enabled', TRUE);
1534 'contact_id' => $contactId,
1535 'receive_date' => '20120511',
1536 'total_amount' => 100.00,
1537 'financial_type_id' => 'Event Fee',
1539 'invoice_id' => 67890,
1541 'contribution_status_id' => 'Completed',
1542 'revenue_recognition_date' => date('Ymd', strtotime("+3 month")),
1544 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1546 $this->callAPISuccessGetCount('EntityFinancialTrxn', [
1547 'entity_table' => "civicrm_contribution",
1548 'entity_id' => $contribution['id'],
1552 'financial_trxn_id.to_financial_account_id.name' => 'Deferred Revenue - Event Fee',
1553 'financial_trxn_id.from_financial_account_id.name' => 'Event Fee',
1554 'financial_trxn_id' => '2',
1556 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', [
1558 "financial_trxn_id.from_financial_account_id.name",
1559 "financial_trxn_id.to_financial_account_id.name",
1560 "financial_trxn_id",
1562 'entity_table' => "civicrm_contribution",
1563 'entity_id' => $contribution['id'],
1564 'financial_trxn_id.is_payment' => 0,
1567 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', [
1568 'entity_table' => "civicrm_financial_item",
1569 'financial_trxn_id' => $result['financial_trxn_id'],
1570 'return' => ['entity_id'],
1574 'financial_account_id.name' => 'Deferred Revenue - Event Fee',
1575 'id' => $result['entity_id'],
1577 $result = $this->callAPISuccessGetSingle('FinancialItem', [
1578 'id' => $result['entity_id'],
1579 'return' => ["financial_account_id.name"],
1584 * https://lab.civicrm.org/dev/financial/issues/56
1585 * Changing financial type on a contribution records correct financial items
1587 public function testChangingFinancialTypeWithoutTax() {
1588 $ids = $values = [];
1589 $contactId = $this->individualCreate();
1591 'contact_id' => $contactId,
1592 'receive_date' => date('YmdHis'),
1593 'total_amount' => 100.00,
1594 'financial_type_id' => 'Donation',
1595 'contribution_status_id' => 'Completed',
1597 /* first test the scenario when sending an email */
1598 $contributionId = $this->callAPISuccess(
1604 // Update Financial Type.
1605 $this->callAPISuccess('contribution', 'create', [
1606 'id' => $contributionId,
1607 'financial_type_id' => 'Event Fee',
1611 $lineItem = $this->callAPISuccessGetSingle('LineItem', [
1612 'contribution_id' => $contributionId,
1613 'return' => ["financial_type_id.name", "line_total"],
1616 $this->assertEquals(
1617 $lineItem['line_total'],
1619 'Invalid line amount.'
1622 $this->assertEquals(
1623 $lineItem['financial_type_id.name'],
1625 'Invalid Financial Type stored.'
1628 // Get Financial Items.
1629 $financialItems = $this->callAPISuccess('FinancialItem', 'get', [
1630 'entity_id' => $lineItem['id'],
1632 'entity_table' => 'civicrm_line_item',
1633 'options' => ['sort' => "id"],
1634 'return' => ["financial_account_id.name", "amount", "description"],
1637 $this->assertEquals($financialItems['count'], 3, 'Count mismatch.');
1640 ['Donation', 100.00],
1641 ['Donation', -100.00],
1642 ['Event Fee', 100.00],
1645 foreach ($financialItems['values'] as $key => $values) {
1646 $this->assertEquals(
1647 $values['financial_account_id.name'],
1649 'Invalid Financial Account stored.'
1651 $this->assertEquals(
1656 $this->assertEquals(
1657 $values['description'],
1658 'Contribution Amount',
1659 'Description mismatch.'
1663 // Check transactions.
1664 $financialTransactions = $this->callAPISuccess('EntityFinancialTrxn', 'get', [
1665 'return' => ["financial_trxn_id"],
1666 'entity_table' => "civicrm_contribution",
1667 'entity_id' => $contributionId,
1670 $this->assertEquals($financialTransactions['count'], 3, 'Count mismatch.');
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],
1682 * CRM-21424 Check if the receipt update is set after composing the receipt message
1684 public function testSendMailUpdateReceiptDate() {
1685 $ids = $values = [];
1686 $contactId = $this->individualCreate();
1688 'contact_id' => $contactId,
1689 'receive_date' => '20120511',
1690 'total_amount' => 100.00,
1691 'financial_type_id' => 'Donation',
1693 'contribution_status_id' => 'Completed',
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 = ['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 = ['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');
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 = ['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 = ['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');