CRM-16189, added full stop and used callAPISuccess() to create entries in db
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialAccountTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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_FinancialAccountTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class CRM_Financial_BAO_FinancialAccountTest extends CiviUnitTestCase {
33
00be9182 34 public function setUp() {
bf2cf926 35 $this->useTransaction(TRUE);
6a488035 36 parent::setUp();
f17d75bb
PN
37 $this->organizationCreate();
38 }
39
6a488035 40 /**
100fef9d 41 * Check method add()
6a488035 42 */
00be9182 43 public function testAdd() {
6a488035
TO
44 $params = array(
45 'name' => 'Donations',
46 'is_deductible' => 0,
47 'is_active' => 1,
48 );
49 $ids = array();
50 $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
51
52 $result = $this->assertDBNotNull(
53 'CRM_Financial_BAO_FinancialAccount',
54 $contributionType->id,
55 'name',
56 'id',
57 'Database check on updated financial type record.'
58 );
59
60 $this->assertEquals($result, 'Donations', 'Verify financial type name.');
61 }
62
63 /**
100fef9d 64 * Check method retrive()
6a488035 65 */
00be9182 66 public function testRetrieve() {
6a488035 67 $params = array(
f17d75bb 68 'name' => 'Donations',
6a488035
TO
69 'is_deductible' => 0,
70 'is_active' => 1,
71 );
72 $ids = $defaults = array();
bf2cf926 73 CRM_Financial_BAO_FinancialAccount::add($params);
6a488035
TO
74
75 $result = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults);
76
f17d75bb 77 $this->assertEquals($result->name, 'Donations', 'Verify financial type name.');
6a488035
TO
78 }
79
80 /**
100fef9d 81 * Check method setIsActive()
6a488035 82 */
00be9182 83 public function testSetIsActive() {
6a488035 84 $params = array(
f17d75bb 85 'name' => 'Donations',
6a488035
TO
86 'is_deductible' => 0,
87 'is_active' => 1,
88 );
89 $ids = array();
90 $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
91 $result = CRM_Financial_BAO_FinancialAccount::setIsActive($contributionType->id, 0);
92 $this->assertEquals($result, TRUE, 'Verify financial type record updation for is_active.');
93
94 $isActive = $this->assertDBNotNull(
95 'CRM_Financial_BAO_FinancialAccount',
96 $contributionType->id,
97 'is_active',
98 'id',
99 'Database check on updated for financial type is_active.'
100 );
101 $this->assertEquals($isActive, 0, 'Verify financial types is_active.');
102 }
103
104 /**
100fef9d 105 * Check method del()
6a488035 106 */
00be9182 107 public function testdel() {
6a488035 108 $params = array(
f17d75bb 109 'name' => 'Donations',
6a488035
TO
110 'is_deductible' => 0,
111 'is_active' => 1,
112 );
113 $ids = array();
114 $contributionType = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
115
116 CRM_Financial_BAO_FinancialAccount::del($contributionType->id);
117 $params = array('id' => $contributionType->id);
118 $result = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults);
119 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
120 }
121
122 /**
100fef9d 123 * Check method getAccountingCode()
6a488035 124 */
00be9182 125 public function testGetAccountingCode() {
6a488035 126 $params = array(
f17d75bb 127 'name' => 'Donations',
6a488035
TO
128 'is_active' => 1,
129 'is_reserved' => 0,
130 );
131
132 $ids = array();
6a488035 133 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
f17d75bb
PN
134 $financialAccountid = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Donations', 'id', 'name');
135 CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $financialAccountid, 'accounting_code', '4800');
6a488035 136 $accountingCode = CRM_Financial_BAO_FinancialAccount::getAccountingCode($financialType->id);
481a74f4 137 $this->assertEquals($accountingCode, 4800, 'Verify accounting code.');
6a488035 138 }
96025800 139
bf2cf926 140 /**
141 * Test getting financial account for a given financial Type with a particular relationship.
142 */
143 public function testGetFinancialAccountByFinancialTypeAndRelationshipBuiltIn() {
144 $this->assertEquals(2, CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship(2, 'Income Account Is'));
145 }
146
147 /**
148 * Test getting financial account for a given financial Type with a particular relationship.
149 */
150 public function testGetFinancialAccountByFinancialTypeAndRelationshipBuiltInRefunded() {
151 $this->assertEquals(2, CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship(2, 'Credit/Contra Revenue Account Is'));
152 }
153
154 /**
155 * Test getting financial account for a given financial Type with a particular relationship.
156 */
157 public function testGetFinancialAccountByFinancialTypeAndRelationshipBuiltInChargeBack() {
158 $this->assertEquals(2, CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship(2, 'Chargeback Account Is'));
159 }
160
161 /**
162 * Test getting financial account for a given financial Type with a particular relationship.
163 */
164 public function testGetFinancialAccountByFinancialTypeAndRelationshipCustomAddedRefunded() {
165 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
166 'name' => 'Refund Account',
167 'is_active' => TRUE,
168 ));
169
170 $this->callAPISuccess('EntityFinancialAccount', 'create', array(
171 'entity_id' => 2,
172 'entity_table' => 'civicrm_financial_type',
173 'account_relationship' => 'Credit/Contra Revenue Account is',
174 'financial_account_id' => 'Refund Account',
175 ));
176 $this->assertEquals($financialAccount['id'],
177 CRM_Financial_BAO_FinancialAccount::getFinancialAccountForFinancialTypeByRelationship(2, 'Credit/Contra Revenue Account is'));
178 }
179
2d5e8eeb
E
180 /**
181 * Test getting financial account relations for a given financial type.
182 */
183 public function testGetFinancialAccountRelations() {
184 $fAccounts = $rAccounts = array();
185 $relations = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations();
186 $links = array(
187 'Expense Account is' => 'Expenses',
188 'Accounts Receivable Account is' => 'Asset',
189 'Income Account is' => 'Revenue',
190 'Asset Account is' => 'Asset',
191 'Cost of Sales Account is' => 'Cost of Sales',
192 'Premiums Inventory Account is' => 'Asset',
193 'Discounts Account is' => 'Revenue',
194 'Sales Tax Account is' => 'Liability',
195 'Deferred Revenue Account is' => 'Liability',
196 );
197 $dao = CRM_Core_DAO::executeQuery("SELECT ov.value, ov.name
198 FROM civicrm_option_value ov
199 INNER JOIN civicrm_option_group og ON og.id = ov.option_group_id
200 AND og.name = 'financial_account_type'");
201 while ($dao->fetch()) {
202 $fAccounts[$dao->value] = $dao->name;
203 }
204 $dao = CRM_Core_DAO::executeQuery("SELECT ov.value, ov.name
205 FROM civicrm_option_value ov
206 INNER JOIN civicrm_option_group og ON og.id = ov.option_group_id
207 AND og.name = 'account_relationship'");
208 while ($dao->fetch()) {
209 $rAccounts[$dao->value] = $dao->name;
210 }
211 foreach ($links as $accountRelation => $accountType) {
212 $financialAccountLinks[array_search($accountRelation, $rAccounts)] = array_search($accountType, $fAccounts);
213 }
214 $this->assertTrue(($relations == $financialAccountLinks), "The two arrays are not the same");
215 }
216
c73dd828
E
217 /*
218 * Test getting deferred financial type.
219 */
220 public function testGetDeferredFinancialType() {
221 $params = array(
222 'name' => 'TestFinancialAccount_1',
223 'accounting_code' => 4800,
224 'contact_id' => 1,
225 'is_deductible' => 0,
226 'is_active' => 1,
227 'is_reserved' => 0,
228 );
229
871de167 230 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', $params);
c73dd828 231 $params['name'] = 'test_financialType1';
871de167 232 $financialType = $this->callAPISuccess('FinancialType', 'create', $params);
c73dd828
E
233 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Deferred Revenue Account is' "));
234 $financialParams = array(
235 'entity_table' => 'civicrm_financial_type',
871de167 236 'entity_id' => $financialType['id'],
c73dd828 237 'account_relationship' => $relationTypeId,
871de167 238 'financial_account_id' => $financialAccount['id'],
c73dd828
E
239 );
240
871de167 241 $this->callAPISuccess('EntityFinancialAccount', 'create', $financialParams);
c73dd828
E
242 $result = $this->assertDBNotNull(
243 'CRM_Financial_DAO_EntityFinancialAccount',
871de167 244 $financialAccount['id'],
c73dd828
E
245 'entity_id',
246 'financial_account_id',
247 'Database check on added financial type record.'
248 );
871de167 249 $this->assertEquals($result, $financialType['id'], 'Verify Account Type');
c73dd828
E
250 $financialTypes = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType();
251 $this->assertTrue(array_key_exists($result, $financialTypes), "The financial type created does not have a deferred account relationship");
252 }
253
6a488035 254}