Merge pull request #7717 from yashodha/CRM-17908
[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-2015 |
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 */
31 class CRM_Financial_BAO_FinancialTypeAccountTest extends CiviUnitTestCase {
32
33 public function setUp() {
34 parent::setUp();
35 $this->organizationCreate();
36 }
37
38 /**
39 * Check method add()
40 */
41 public function testAdd() {
42 $params = array(
43 'name' => 'TestFinancialAccount_1',
44 'accounting_code' => 4800,
45 'contact_id' => 1,
46 'is_deductible' => 0,
47 'is_active' => 1,
48 'is_reserved' => 0,
49 );
50
51 $ids = array();
52 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
53 $params['name'] = 'test_financialType1';
54 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
55 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
56 $financialParams = array(
57 'entity_table' => 'civicrm_financial_type',
58 'entity_id' => $financialType->id,
59 'account_relationship' => $relationTypeId,
60 'financial_account_id' => $financialAccount->id,
61 );
62
63 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids);
64 $result = $this->assertDBNotNull(
65 'CRM_Financial_DAO_EntityFinancialAccount',
66 $financialAccount->id,
67 'entity_id',
68 'financial_account_id',
69 'Database check on added financial type record.'
70 );
71 $this->assertEquals($result, $financialType->id, 'Verify Account Type');
72 }
73
74 /**
75 * Check method del()
76 */
77 public function testDel() {
78 $params = array(
79 'name' => 'TestFinancialAccount_2',
80 'is_deductible' => 0,
81 'is_active' => 1,
82 'is_reserved' => 0,
83 );
84
85 $ids = array();
86 $defaults = array();
87 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
88 $params['name'] = 'test_financialType2';
89 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
90 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
91 $financialParams = array(
92 'entity_table' => 'civicrm_financial_type',
93 'entity_id' => $financialType->id,
94 'account_relationship' => $relationTypeId,
95 'financial_account_id' => $financialAccount->id,
96 );
97 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids);
98 CRM_Financial_BAO_FinancialTypeAccount::del($financialAccountType->id);
99 $params = array('id' => $financialAccountType->id);
100 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
101 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
102 }
103
104 /**
105 * Check method getFinancialAccount()
106 */
107 public function testRetrieve() {
108 $params = array(
109 'name' => 'TestFinancialAccount_3',
110 'is_deductible' => 0,
111 'is_active' => 1,
112 'is_reserved' => 0,
113 );
114 $ids = array();
115 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
116 $params['name'] = 'test_financialType3';
117 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
118 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
119 $financialParams = array(
120 'entity_table' => 'civicrm_financial_type',
121 'entity_id' => $financialType->id,
122 'account_relationship' => $relationTypeId,
123 'financial_account_id' => $financialAccount->id,
124 );
125
126 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids);
127 $defaults = array();
128 $financialAccountType = CRM_Financial_BAO_FinancialTypeAccount::retrieve($financialParams, $defaults);
129 $this->assertEquals($financialAccountType['entity_id'], $financialType->id, 'Verify Entity Id.');
130 $this->assertEquals($financialAccountType['financial_account_id'], $financialAccount->id, 'Verify Financial Account Id.');
131 }
132
133 /**
134 * Check method getFinancialAccount()
135 */
136 public function testGetFinancialAccount() {
137 $params = array(
138 'name' => 'TestFinancialAccount',
139 'accounting_code' => 4800,
140 'is_deductible' => 0,
141 'is_active' => 1,
142 'is_reserved' => 0,
143 );
144 $ids = array();
145 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
146 $params = array(
147 'financial_account_id' => $financialAccount->id,
148 'payment_processor_type_id' => 1,
149 'domain_id' => 1,
150 'billing_mode' => 1,
151 'name' => 'paymentProcessor',
152 );
153 $processor = CRM_Financial_BAO_PaymentProcessor::create($params);
154
155 $account = CRM_Financial_BAO_FinancialTypeAccount::getFinancialAccount(
156 $processor->id,
157 'civicrm_payment_processor'
158 );
159 $this->assertEquals($account, 'TestFinancialAccount', 'Verify Financial Account Name');
160 }
161
162 /**
163 * Check method getInstrumentFinancialAccount()
164 */
165 public function testGetInstrumentFinancialAccount() {
166 $paymentInstrumentValue = 1;
167 $params = array(
168 'name' => 'Donations',
169 'is_deductible' => 0,
170 'is_active' => 1,
171 );
172 $ids = array();
173 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, $ids);
174 $optionParams = array(
175 'name' => 'Credit Card',
176 'value' => $paymentInstrumentValue,
177 );
178 $optionValue = CRM_Core_BAO_OptionValue::retrieve($optionParams, $defaults);
179 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
180 $financialParams = array(
181 'entity_table' => 'civicrm_option_value',
182 'entity_id' => $optionValue->id,
183 'account_relationship' => $relationTypeId,
184 'financial_account_id' => $financialAccount->id,
185 );
186
187 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, $ids);
188 $financialAccountId = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($paymentInstrumentValue);
189
190 $this->assertEquals($financialAccountId, $financialAccount->id, 'Verify Payment Instrument');
191 }
192
193 }