Merge pull request #10157 from eileenmcnaughton/iida3
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeAccountTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
e9479dcf
EM
28/**
29 * Class CRM_Financial_BAO_FinancialTypeAccountTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase {
33
00be9182 34 public function setUp() {
6a488035 35 parent::setUp();
f17d75bb 36 $this->organizationCreate();
6a488035
TO
37 }
38
39 /**
100fef9d 40 * Check method add()
6a488035 41 */
00be9182 42 public function testAdd() {
b70cd45c
PN
43 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
44 'Revenue',
45 'Income Account is'
6a488035 46 );
6a488035
TO
47 $result = $this->assertDBNotNull(
48 'CRM_Financial_DAO_EntityFinancialAccount',
49 $financialAccount->id,
50 'entity_id',
51 'financial_account_id',
52 'Database check on added financial type record.'
53 );
481a74f4 54 $this->assertEquals($result, $financialType->id, 'Verify Account Type');
6a488035
TO
55 }
56
57 /**
100fef9d 58 * Check method del()
6a488035 59 */
00be9182 60 public function testDel() {
b70cd45c
PN
61 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
62 'Expenses',
63 'Expense Account is'
6a488035
TO
64 );
65
6a488035 66 CRM_Financial_BAO_FinancialTypeAccount::del($financialAccountType->id);
481a74f4 67 $params = array('id' => $financialAccountType->id);
6a488035 68 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
481a74f4 69 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
6a488035
TO
70 }
71
72 /**
74afdc40 73 * Check method retrieve()
6a488035 74 */
00be9182 75 public function testRetrieve() {
b70cd45c
PN
76 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
77 'Asset',
78 'Asset Account is'
6a488035 79 );
f743a6eb 80 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
81 $financialParams = array(
82 'entity_table' => 'civicrm_financial_type',
83 'entity_id' => $financialType->id,
84 'account_relationship' => $relationTypeId,
85 'financial_account_id' => $financialAccount->id,
86 );
87
6a488035
TO
88 $defaults = array();
89 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::retrieve($financialParams, $defaults);
481a74f4
TO
90 $this->assertEquals($financialAccountType['entity_id'], $financialType->id, 'Verify Entity Id.');
91 $this->assertEquals($financialAccountType['financial_account_id'], $financialAccount->id, 'Verify Financial Account Id.');
6a488035
TO
92 }
93
6a488035 94 /**
100fef9d 95 * Check method getInstrumentFinancialAccount()
6a488035 96 */
00be9182 97 public function testGetInstrumentFinancialAccount() {
6a488035 98 $paymentInstrumentValue = 1;
b70cd45c
PN
99 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
100 'Asset'
6a488035 101 );
6a488035
TO
102 $optionParams = array(
103 'name' => 'Credit Card',
104 'value' => $paymentInstrumentValue,
105 );
106 $optionValue = CRM_Core_BAO_OptionValue::retrieve($optionParams, $defaults);
f743a6eb 107 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
6a488035
TO
108 $financialParams = array(
109 'entity_table' => 'civicrm_option_value',
110 'entity_id' => $optionValue->id,
111 'account_relationship' => $relationTypeId,
112 'financial_account_id' => $financialAccount->id,
113 );
114
b70cd45c 115 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
6a488035
TO
116 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($paymentInstrumentValue);
117
481a74f4 118 $this->assertEquals($financialAccountId, $financialAccount->id, 'Verify Payment Instrument');
6a488035 119 }
96025800 120
8180ec69
E
121 /**
122 * Test validate account relationship with financial account type.
123 */
124 public function testValidateRelationship() {
125 $params = array('labelColumn' => 'name');
126 $financialAccount = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params);
127 $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
128 $financialType = CRM_Contribute_PseudoConstant::financialType();
129 $financialAccountType = new CRM_Financial_DAO_EntityFinancialAccount();
130 $financialAccountType->entity_table = 'civicrm_financial_type';
131 $financialAccountType->entity_id = array_search('Member Dues', $financialType);
132 $financialAccountType->account_relationship = array_search('Credit/Contra Revenue Account is', $accountRelationships);
133 $financialAccountType->financial_account_id = array_search('Liability', $financialAccount);
134 try {
135 CRM_Financial_BAO_FinancialTypeAccount::validateRelationship($financialAccountType);
136 $this->fail("Missed expected exception");
137 }
138 catch (Exception $e) {
139 $this->assertTrue(TRUE, 'Received expected exception');
140 $this->assertEquals($e->getMessage(), "This financial account cannot have 'Credit/Contra Revenue Account is' relationship.");
141 }
142 }
143
b70cd45c
PN
144 /**
145 * Function to create Financial Account.
146 *
147 * @param string $financialAccountType
148 *
149 * @param string $relationType
150 *
151 * @return array
152 * obj CRM_Financial_DAO_FinancialAccount, obj CRM_Financial_DAO_FinancialType, obj CRM_Financial_DAO_EntityFinancialAccount
153 */
154 public function createFinancialAccount($financialAccountType, $relationType = NULL) {
155 $params = array('labelColumn' => 'name');
156 $relationTypes = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship', $params);
157 $financialAccountTypes = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_FinancialAccount', 'financial_account_type_id', $params);
158 $params = array(
159 'name' => 'TestFinancialAccount_' . rand(),
160 'contact_id' => 1,
161 'is_deductible' => 0,
162 'is_active' => 1,
163 'is_reserved' => 0,
164 'financial_account_type_id' => array_search($financialAccountType, $financialAccountTypes),
165 );
166 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
167 $financialType = $financialAccountType = NULL;
168 if ($relationType) {
169 $params['name'] = 'test_financialType1';
170 $financialType = CRM_Financial_BAO_FinancialType::add($params);
171 $financialParams = array(
172 'entity_table' => 'civicrm_financial_type',
173 'entity_id' => $financialType->id,
174 'account_relationship' => array_search($relationType, $relationTypes),
b70cd45c 175 );
5b3543ce
JM
176
177 //CRM-20313: As per unique index added in civicrm_entity_financial_account table,
178 // first check if there's any record on basis of unique key (entity_table, account_relationship, entity_id)
179 $dao = new CRM_Financial_DAO_EntityFinancialAccount();
180 $dao->copyValues($financialParams);
181 $dao->find();
182 if ($dao->fetch()) {
183 $financialParams['id'] = $dao->id;
184 }
185 $financialParams['financial_account_id'] = $financialAccount->id;
b70cd45c
PN
186 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
187 }
188 return array($financialAccount, $financialType, $financialAccountType);
189 }
190
232624b1 191}