3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Class CRM_Financial_BAO_FinancialTypeAccountTest
16 class CRM_Financial_BAO_FinancialTypeAccountTest
extends CiviUnitTestCase
{
18 public function setUp(): void
{
20 $this->organizationCreate();
26 public function testAdd() {
27 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
31 $result = $this->assertDBNotNull(
32 'CRM_Financial_DAO_EntityFinancialAccount',
33 $financialAccount->id
,
35 'financial_account_id',
36 'Database check on added financial type record.'
38 $this->assertEquals($result, $financialType->id
, 'Verify Account Type');
44 public function testDel() {
45 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
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.');
57 * Check method retrieve()
59 public function testRetrieve() {
60 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
64 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
66 'entity_table' => 'civicrm_financial_type',
67 'entity_id' => $financialType->id
,
68 'account_relationship' => $relationTypeId,
69 'financial_account_id' => $financialAccount->id
,
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.');
79 * Check method getInstrumentFinancialAccount()
81 public function testGetInstrumentFinancialAccount() {
82 $paymentInstrumentValue = 1;
83 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
87 'name' => 'Credit Card',
88 'value' => $paymentInstrumentValue,
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' "));
93 'entity_table' => 'civicrm_option_value',
94 'entity_id' => $optionValue->id
,
95 'account_relationship' => $relationTypeId,
96 'financial_account_id' => $financialAccount->id
,
99 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams);
100 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($paymentInstrumentValue);
102 $this->assertEquals($financialAccountId, $financialAccount->id
, 'Verify Payment Instrument');
106 * Test validate account relationship with financial account type.
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);
119 CRM_Financial_BAO_FinancialTypeAccount
::validateRelationship($financialAccountType);
120 $this->fail("Missed expected exception");
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.");
129 * Function to create Financial Account.
131 * @param string $financialAccountType
133 * @param string $relationType
136 * obj CRM_Financial_DAO_FinancialAccount, obj CRM_Financial_DAO_FinancialType, obj CRM_Financial_DAO_EntityFinancialAccount
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);
143 'name' => 'TestFinancialAccount_' . rand(),
145 'is_deductible' => 0,
148 'financial_account_type_id' => array_search($financialAccountType, $financialAccountTypes),
150 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params);
151 $financialType = $financialAccountType = NULL;
153 $params['name'] = 'test_financialType1';
154 $financialType = CRM_Financial_BAO_FinancialType
::add($params);
156 'entity_table' => 'civicrm_financial_type',
157 'entity_id' => $financialType->id
,
158 'account_relationship' => array_search($relationType, $relationTypes),
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);
167 $financialParams['id'] = $dao->id
;
169 $financialParams['financial_account_id'] = $financialAccount->id
;
170 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams);
172 return [$financialAccount, $financialType, $financialAccountType];