Merge pull request #22291 from eileenmcnaughton/amount_block
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeAccountTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Financial_BAO_FinancialTypeAccountTest
14 * @group headless
15 */
16 class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase {
17
18 public function setUp(): void {
19 parent::setUp();
20 $this->organizationCreate();
21 }
22
23 /**
24 * Check method add()
25 */
26 public function testAdd() {
27 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
28 'Revenue',
29 'Income Account is'
30 );
31 $result = $this->assertDBNotNull(
32 'CRM_Financial_DAO_EntityFinancialAccount',
33 $financialAccount->id,
34 'entity_id',
35 'financial_account_id',
36 'Database check on added financial type record.'
37 );
38 $this->assertEquals($result, $financialType->id, 'Verify Account Type');
39 }
40
41 /**
42 * Check method del()
43 */
44 public function testDel() {
45 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
46 'Expenses',
47 'Expense Account is'
48 );
49
50 CRM_Financial_BAO_FinancialTypeAccount::del($financialAccountType->id);
51 $params = ['id' => $financialAccountType->id];
52 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
53 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
54 }
55
56 /**
57 * Check method retrieve()
58 */
59 public function testRetrieve() {
60 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
61 'Asset',
62 'Asset Account is'
63 );
64 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
65 $financialParams = [
66 'entity_table' => 'civicrm_financial_type',
67 'entity_id' => $financialType->id,
68 'account_relationship' => $relationTypeId,
69 'financial_account_id' => $financialAccount->id,
70 ];
71
72 $defaults = [];
73 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::retrieve($financialParams, $defaults);
74 $this->assertEquals($financialAccountType['entity_id'], $financialType->id, 'Verify Entity Id.');
75 $this->assertEquals($financialAccountType['financial_account_id'], $financialAccount->id, 'Verify Financial Account Id.');
76 }
77
78 /**
79 * Check method getInstrumentFinancialAccount()
80 */
81 public function testGetInstrumentFinancialAccount() {
82 $paymentInstrumentValue = 1;
83 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
84 'Asset'
85 );
86 $optionParams = [
87 'name' => 'Credit Card',
88 'value' => $paymentInstrumentValue,
89 ];
90 $optionValue = CRM_Core_BAO_OptionValue::retrieve($optionParams, $defaults);
91 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
92 $financialParams = [
93 'entity_table' => 'civicrm_option_value',
94 'entity_id' => $optionValue->id,
95 'account_relationship' => $relationTypeId,
96 'financial_account_id' => $financialAccount->id,
97 ];
98
99 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
100 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($paymentInstrumentValue);
101
102 $this->assertEquals($financialAccountId, $financialAccount->id, 'Verify Payment Instrument');
103 }
104
105 /**
106 * Test validate account relationship with financial account type.
107 */
108 public function testValidateRelationship() {
109 $params = ['labelColumn' => 'name'];
110 $financialAccount = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params);
111 $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
112 $financialType = CRM_Contribute_PseudoConstant::financialType();
113 $financialAccountType = new CRM_Financial_DAO_EntityFinancialAccount();
114 $financialAccountType->entity_table = 'civicrm_financial_type';
115 $financialAccountType->entity_id = array_search('Member Dues', $financialType);
116 $financialAccountType->account_relationship = array_search('Credit/Contra Revenue Account is', $accountRelationships);
117 $financialAccountType->financial_account_id = array_search('Liability', $financialAccount);
118 try {
119 CRM_Financial_BAO_FinancialTypeAccount::validateRelationship($financialAccountType);
120 $this->fail("Missed expected exception");
121 }
122 catch (Exception $e) {
123 $this->assertTrue(TRUE, 'Received expected exception');
124 $this->assertEquals($e->getMessage(), "This financial account cannot have 'Credit/Contra Revenue Account is' relationship.");
125 }
126 }
127
128 /**
129 * Function to create Financial Account.
130 *
131 * @param string $financialAccountType
132 *
133 * @param string $relationType
134 *
135 * @return array
136 * obj CRM_Financial_DAO_FinancialAccount, obj CRM_Financial_DAO_FinancialType, obj CRM_Financial_DAO_EntityFinancialAccount
137 */
138 public function createFinancialAccount($financialAccountType, $relationType = NULL) {
139 $params = ['labelColumn' => 'name'];
140 $relationTypes = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
141 $financialAccountTypes = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params);
142 $params = [
143 'name' => 'TestFinancialAccount_' . rand(),
144 'contact_id' => 1,
145 'is_deductible' => 0,
146 'is_active' => 1,
147 'is_reserved' => 0,
148 'financial_account_type_id' => array_search($financialAccountType, $financialAccountTypes),
149 ];
150 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
151 $financialType = $financialAccountType = NULL;
152 if ($relationType) {
153 $params['name'] = 'test_financialType1';
154 $financialType = CRM_Financial_BAO_FinancialType::add($params);
155 $financialParams = [
156 'entity_table' => 'civicrm_financial_type',
157 'entity_id' => $financialType->id,
158 'account_relationship' => array_search($relationType, $relationTypes),
159 ];
160
161 //CRM-20313: As per unique index added in civicrm_entity_financial_account table,
162 // first check if there's any record on basis of unique key (entity_table, account_relationship, entity_id)
163 $dao = new CRM_Financial_DAO_EntityFinancialAccount();
164 $dao->copyValues($financialParams);
165 $dao->find();
166 if ($dao->fetch()) {
167 $financialParams['id'] = $dao->id;
168 }
169 $financialParams['financial_account_id'] = $financialAccount->id;
170 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
171 }
172 return [$financialAccount, $financialType, $financialAccountType];
173 }
174
175 }