Import from SVN (r45945, r596)
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialItemTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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*/
27require_once 'CiviTest/CiviUnitTestCase.php';
28require_once 'CRM/Financial/DAO/FinancialAccount.php';
29require_once 'CRM/Financial/BAO/FinancialAccount.php';
30
31class CRM_Financial_BAO_FinancialItemTest extends CiviUnitTestCase {
32
33 function get_info() {
34 return array(
35 'name' => 'FinancialItem BAOs',
36 'description' => 'Test all Contribute_BAO_Contribution methods.',
37 'group' => 'CiviCRM BAO Tests',
38 );
39 }
40
41 function setUp() {
42 parent::setUp();
43 }
44
45 /**
46 * check method add()
47 */
48 function testAdd() {
49 $firstName = 'Shane';
50 $lastName = 'Whatson';
51 $params = array(
52 'first_name' => $firstName,
53 'last_name' => $lastName,
54 'contact_type' => 'Individual',
55 );
56
57 $contact = CRM_Contact_BAO_Contact::add($params);
58
59 $price = 100;
60 $cParams = array(
61 'contact_id' => $contact->id,
62 'total_amount' => $price,
63 'financial_type_id' => 1,
64 'is_active' => 1,
65 'skipLineItem' => 1,
66 );
67
68 $defaults = array();
69 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
70 $lParams = array(
71 'entity_id' => $contribution->id,
72 'entity_table' => 'civicrm_contribution',
73 'price_field_id' => 1,
74 'qty' => 1,
75 'label' => 'Contribution Amount',
76 'unit_price' => $price,
77 'line_total' => $price,
78 'price_field_value_id' => 1,
79 'financial_type_id' => 1,
80 );
81
82 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
83 CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
84 $result = $this->assertDBNotNull(
85 'CRM_Financial_DAO_FinancialItem',
86 $lineItem->id ,
87 'amount',
88 'entity_id',
89 'Database check on added financial item record.'
90 );
91 $this->assertEquals( $result, $price, 'Verify Amount for Financial Item');
92 }
93
94 /**
95 * check method retrive()
96 */
97 function testRetrieve() {
98 $firstName = 'Shane';
99 $lastName = 'Whatson';
100 $params = array(
101 'first_name' => $firstName,
102 'last_name' => $lastName,
103 'contact_type' => 'Individual',
104 );
105
106 $contact = CRM_Contact_BAO_Contact::add($params);
107 $price = 100.00;
108 $cParams = array(
109 'contact_id' => $contact->id,
110 'total_amount' => $price,
111 'financial_type_id' => 1,
112 'is_active' => 1,
113 'skipLineItem' => 1,
114 );
115
116 $defaults = array();
117 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
118 $lParams = array(
119 'entity_id' => $contribution->id,
120 'entity_table' => 'civicrm_contribution',
121 'price_field_id' => 1,
122 'qty' => 1,
123 'label' => 'Contribution Amount',
124 'unit_price' => $price,
125 'line_total' => $price,
126 'price_field_value_id' => 1,
127 'financial_type_id' => 1,
128 );
129
130 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
131 CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
132 $values = array();
133 $fParams = array(
134 'entity_id' => $lineItem->id,
135 'entity_table' => 'civicrm_line_item',
136 );
137 $financialItem = CRM_Financial_BAO_FinancialItem::retrieve($fParams, $values);
138 $this->assertEquals( $financialItem->amount, $price, 'Verify financial item amount.');
139 }
140
141 /**
142 * check method create()
143 */
144 function testCreate() {
145 $firstName = 'Shane';
146 $lastName = 'Whatson';
147 $params = array(
148 'first_name' => $firstName,
149 'last_name' => $lastName,
150 'contact_type' => 'Individual',
151 );
152
153 $contact = CRM_Contact_BAO_Contact::add($params);
154 $price = 100.00;
155 $cParams = array(
156 'contact_id' => $contact->id,
157 'total_amount' => $price,
158 'financial_type_id' => 1,
159 'is_active' => 1,
160 'skipLineItem' => 1,
161 );
162
163 $defaults = array();
164 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
165 $lParams = array(
166 'entity_id' => $contribution->id,
167 'entity_table' => 'civicrm_contribution',
168 'price_field_id' => 1,
169 'qty' => 1,
170 'label' => 'Contribution Amount',
171 'unit_price' => $price,
172 'line_total' => $price,
173 'price_field_value_id' => 1,
174 'financial_type_id' => 1,
175 );
176
177 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
178 $fParams = array(
179 'contact_id' => $contact->id,
180 'description' => 'Contribution Amount',
181 'amount' => $price,
182 'financial_account_id' => 1,
183 'status_id' => 1,
184 'transaction_date' => date('YmdHis'),
185 'entity_id' => $lineItem->id,
186 'entity_table' => 'civicrm_line_item',
187 );
188
189 CRM_Financial_BAO_FinancialItem::create($fParams);
190 $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
191 $entityTrxn->entity_table = 'civicrm_contribution';
192 $entityTrxn->entity_id = $contribution->id;
193 $entityTrxn->amount = $price;
194 if ($entityTrxn->find(TRUE)) {
195 $entityId = $entityTrxn->entity_id;
196 }
197
198 $result = $this->assertDBNotNull(
199 'CRM_Financial_DAO_FinancialItem',
200 $lineItem->id ,
201 'amount',
202 'entity_id',
203 'Database check on added financial item record.'
204 );
205
206 $this->assertEquals( $result, $price, 'Verify Amount for Financial Item');
207 $entityResult = $this->assertDBNotNull(
208 'CRM_Financial_DAO_EntityFinancialTrxn',
209 $entityId ,
210 'amount',
211 'entity_id',
212 'Database check on added entity financial trxn record.'
213 );
214 $this->assertEquals( $entityResult, $price, 'Verify Amount for Financial Item');
215 }
216
217 /**
218 * check method del()
219 */
220 function testCreateEntityTrxn() {
221 $fParams = array(
222 'name' => 'Donations'.substr(sha1(rand()), 0, 7),
223 'is_deductible' => 0,
224 'is_active' => 1,
225 );
226
227 $amount = 200;
228 $ids = array();
229 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($fParams, $ids);
230 $financialTrxn = new CRM_Financial_DAO_FinancialTrxn();
231 $financialTrxn->to_financial_account_id = $financialAccount->id;
232 $financialTrxn->total_amount = $amount;
233 $financialTrxn->save();
234 $params = array(
235 'entity_table' => 'civicrm_contribution',
236 'entity_id' => 1,
237 'financial_trxn_id' => $financialTrxn->id,
238 'amount' => $amount,
239 );
240
241 $entityTrxn = CRM_Financial_BAO_FinancialItem::createEntityTrxn($params);
242 $entityResult = $this->assertDBNotNull(
243 'CRM_Financial_DAO_EntityFinancialTrxn',
244 $financialTrxn->id ,
245 'amount',
246 'financial_trxn_id',
247 'Database check on added entity financial trxn record.'
248 );
249 $this->assertEquals( $entityResult, $amount, 'Verify Amount for Financial Item');
250 return $entityTrxn;
251 }
252
253 /**
254 * check method retrieveEntityFinancialTrxn()
255 */
256 function testRetrieveEntityFinancialTrxn() {
257 $entityTrxn = self::testCreateEntityTrxn();
258 $params = array(
259 'entity_table' => 'civicrm_contribution',
260 'entity_id' => 1,
261 'financial_trxn_id' => $entityTrxn->financial_trxn_id,
262 'amount' => $entityTrxn->amount,
263 );
264
265 CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params);
266 $entityResult = $this->assertDBNotNull(
267 'CRM_Financial_DAO_EntityFinancialTrxn',
268 $entityTrxn->financial_trxn_id,
269 'amount',
270 'financial_trxn_id',
271 'Database check on added entity financial trxn record.'
272 );
273 $this->assertEquals( $entityResult, $entityTrxn->amount, 'Verify Amount for Financial Item');
274 }
275}