3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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_Financial_BAO_FinancialTypeAccountTest
32 class CRM_Financial_BAO_FinancialTypeAccountTest
extends CiviUnitTestCase
{
34 public function setUp() {
36 $this->organizationCreate();
42 public function testAdd() {
43 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
47 $result = $this->assertDBNotNull(
48 'CRM_Financial_DAO_EntityFinancialAccount',
49 $financialAccount->id
,
51 'financial_account_id',
52 'Database check on added financial type record.'
54 $this->assertEquals($result, $financialType->id
, 'Verify Account Type');
60 public function testDel() {
61 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
66 CRM_Financial_BAO_FinancialTypeAccount
::del($financialAccountType->id
);
67 $params = array('id' => $financialAccountType->id
);
68 $result = CRM_Financial_BAO_FinancialType
::retrieve($params, $defaults);
69 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
73 * Check method getFinancialAccount()
75 public function testRetrieve() {
76 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
80 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
81 $financialParams = array(
82 'entity_table' => 'civicrm_financial_type',
83 'entity_id' => $financialType->id
,
84 'account_relationship' => $relationTypeId,
85 'financial_account_id' => $financialAccount->id
,
89 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount
::retrieve($financialParams, $defaults);
90 $this->assertEquals($financialAccountType['entity_id'], $financialType->id
, 'Verify Entity Id.');
91 $this->assertEquals($financialAccountType['financial_account_id'], $financialAccount->id
, 'Verify Financial Account Id.');
95 * Check method getFinancialAccount()
97 public function testGetFinancialAccount() {
98 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
102 'financial_account_id' => $financialAccount->id
,
103 'payment_processor_type_id' => 1,
106 'name' => 'paymentProcessor',
108 $processor = CRM_Financial_BAO_PaymentProcessor
::create($params);
110 $account = CRM_Financial_BAO_FinancialTypeAccount
::getFinancialAccount(
112 'civicrm_payment_processor'
114 $this->assertEquals($account, $financialAccount->name
, 'Verify Financial Account Name');
118 * Check method getInstrumentFinancialAccount()
120 public function testGetInstrumentFinancialAccount() {
121 $paymentInstrumentValue = 1;
122 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
125 $optionParams = array(
126 'name' => 'Credit Card',
127 'value' => $paymentInstrumentValue,
129 $optionValue = CRM_Core_BAO_OptionValue
::retrieve($optionParams, $defaults);
130 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
131 $financialParams = array(
132 'entity_table' => 'civicrm_option_value',
133 'entity_id' => $optionValue->id
,
134 'account_relationship' => $relationTypeId,
135 'financial_account_id' => $financialAccount->id
,
138 CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams);
139 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount
::getInstrumentFinancialAccount($paymentInstrumentValue);
141 $this->assertEquals($financialAccountId, $financialAccount->id
, 'Verify Payment Instrument');
145 * Test validate account relationship with financial account type.
147 public function testValidateRelationship() {
148 $params = array('labelColumn' => 'name');
149 $financialAccount = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params);
150 $accountRelationships = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
151 $financialType = CRM_Contribute_PseudoConstant
::financialType();
152 $financialAccountType = new CRM_Financial_DAO_EntityFinancialAccount();
153 $financialAccountType->entity_table
= 'civicrm_financial_type';
154 $financialAccountType->entity_id
= array_search('Member Dues', $financialType);
155 $financialAccountType->account_relationship
= array_search('Credit/Contra Revenue Account is', $accountRelationships);
156 $financialAccountType->financial_account_id
= array_search('Liability', $financialAccount);
158 CRM_Financial_BAO_FinancialTypeAccount
::validateRelationship($financialAccountType);
159 $this->fail("Missed expected exception");
161 catch (Exception
$e) {
162 $this->assertTrue(TRUE, 'Received expected exception');
163 $this->assertEquals($e->getMessage(), "This financial account cannot have 'Credit/Contra Revenue Account is' relationship.");
168 * Function to create Financial Account.
170 * @param string $financialAccountType
172 * @param string $relationType
175 * obj CRM_Financial_DAO_FinancialAccount, obj CRM_Financial_DAO_FinancialType, obj CRM_Financial_DAO_EntityFinancialAccount
177 public function createFinancialAccount($financialAccountType, $relationType = NULL) {
178 $params = array('labelColumn' => 'name');
179 $relationTypes = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
180 $financialAccountTypes = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params);
182 'name' => 'TestFinancialAccount_' . rand(),
184 'is_deductible' => 0,
187 'financial_account_type_id' => array_search($financialAccountType, $financialAccountTypes),
189 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params);
190 $financialType = $financialAccountType = NULL;
192 $params['name'] = 'test_financialType1';
193 $financialType = CRM_Financial_BAO_FinancialType
::add($params);
194 $financialParams = array(
195 'entity_table' => 'civicrm_financial_type',
196 'entity_id' => $financialType->id
,
197 'account_relationship' => array_search($relationType, $relationTypes),
200 //CRM-20313: As per unique index added in civicrm_entity_financial_account table,
201 // first check if there's any record on basis of unique key (entity_table, account_relationship, entity_id)
202 $dao = new CRM_Financial_DAO_EntityFinancialAccount();
203 $dao->copyValues($financialParams);
206 $financialParams['id'] = $dao->id
;
208 $financialParams['financial_account_id'] = $financialAccount->id
;
209 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount
::add($financialParams);
211 return array($financialAccount, $financialType, $financialAccountType);