Merge pull request #8645 from eileenmcnaughton/group_query
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeAccountTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class CRM_Financial_BAO_FinancialTypeAccountTest
30 * @group headless
31 */
32 class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 $this->organizationCreate();
37 }
38
39 /**
40 * Check method add()
41 */
42 public function testAdd() {
43 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
44 'Revenue',
45 'Income Account is'
46 );
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 );
54 $this->assertEquals($result, $financialType->id, 'Verify Account Type');
55 }
56
57 /**
58 * Check method del()
59 */
60 public function testDel() {
61 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
62 'Expenses',
63 'Expense Account is'
64 );
65
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.');
70 }
71
72 /**
73 * Check method getFinancialAccount()
74 */
75 public function testRetrieve() {
76 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
77 'Asset',
78 'Asset Account is'
79 );
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,
86 );
87
88 $defaults = array();
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.');
92 }
93
94 /**
95 * Check method getFinancialAccount()
96 */
97 public function testGetFinancialAccount() {
98 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
99 'Asset'
100 );
101 $params = array(
102 'financial_account_id' => $financialAccount->id,
103 'payment_processor_type_id' => 1,
104 'domain_id' => 1,
105 'billing_mode' => 1,
106 'name' => 'paymentProcessor',
107 );
108 $processor = CRM_Financial_BAO_PaymentProcessor::create($params);
109
110 $account = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount(
111 $processor->id,
112 'civicrm_payment_processor'
113 );
114 $this->assertEquals($account, $financialAccount->name, 'Verify Financial Account Name');
115 }
116
117 /**
118 * Check method getInstrumentFinancialAccount()
119 */
120 public function testGetInstrumentFinancialAccount() {
121 $paymentInstrumentValue = 1;
122 list($financialAccount, $financialType, $financialAccountType) = $this->createFinancialAccount(
123 'Asset'
124 );
125 $optionParams = array(
126 'name' => 'Credit Card',
127 'value' => $paymentInstrumentValue,
128 );
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,
136 );
137
138 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
139 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($paymentInstrumentValue);
140
141 $this->assertEquals($financialAccountId, $financialAccount->id, 'Verify Payment Instrument');
142 }
143
144 /**
145 * Test validate account relationship with financial account type.
146 */
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);
157 try {
158 CRM_Financial_BAO_FinancialTypeAccount::validateRelationship($financialAccountType);
159 $this->fail("Missed expected exception");
160 }
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.");
164 }
165 }
166
167 /**
168 * Function to create Financial Account.
169 *
170 * @param string $financialAccountType
171 *
172 * @param string $relationType
173 *
174 * @return array
175 * obj CRM_Financial_DAO_FinancialAccount, obj CRM_Financial_DAO_FinancialType, obj CRM_Financial_DAO_EntityFinancialAccount
176 */
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);
181 $params = array(
182 'name' => 'TestFinancialAccount_' . rand(),
183 'contact_id' => 1,
184 'is_deductible' => 0,
185 'is_active' => 1,
186 'is_reserved' => 0,
187 'financial_account_type_id' => array_search($financialAccountType, $financialAccountTypes),
188 );
189 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params);
190 $financialType = $financialAccountType = NULL;
191 if ($relationType) {
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),
198 'financial_account_id' => $financialAccount->id,
199 );
200 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams);
201 }
202 return array($financialAccount, $financialType, $financialAccountType);
203 }
204
205 }