CRM-20260, Added Unit test
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialItemTest.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_FinancialItemTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class CRM_Financial_BAO_FinancialItemTest extends CiviUnitTestCase {
33
00be9182 34 public function setUp() {
6a488035
TO
35 parent::setUp();
36 }
37
ffa4c203
PN
38 /**
39 * Clean up after each test.
40 */
41 public function tearDown() {
42 $this->quickCleanUpFinancialEntities();
43 parent::tearDown();
44 }
45
6a488035 46 /**
100fef9d 47 * Check method add()
6a488035 48 */
00be9182 49 public function testAdd() {
6a488035 50 $firstName = 'Shane';
92915c55
TO
51 $lastName = 'Whatson';
52 $params = array(
6a488035
TO
53 'first_name' => $firstName,
54 'last_name' => $lastName,
55 'contact_type' => 'Individual',
56 );
57
58 $contact = CRM_Contact_BAO_Contact::add($params);
59
60 $price = 100;
61 $cParams = array(
62 'contact_id' => $contact->id,
63 'total_amount' => $price,
64 'financial_type_id' => 1,
92915c55
TO
65 'is_active' => 1,
66 'skipLineItem' => 1,
6a488035
TO
67 );
68
69 $defaults = array();
70 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
71 $lParams = array(
72 'entity_id' => $contribution->id,
73 'entity_table' => 'civicrm_contribution',
74 'price_field_id' => 1,
75 'qty' => 1,
76 'label' => 'Contribution Amount',
77 'unit_price' => $price,
78 'line_total' => $price,
79 'price_field_value_id' => 1,
80 'financial_type_id' => 1,
81 );
82
83 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
84 CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
85 $result = $this->assertDBNotNull(
86 'CRM_Financial_DAO_FinancialItem',
6c6e6187 87 $lineItem->id,
6a488035
TO
88 'amount',
89 'entity_id',
90 'Database check on added financial item record.'
91 );
481a74f4 92 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
6a488035
TO
93 }
94
95 /**
100fef9d 96 * Check method retrive()
6a488035 97 */
00be9182 98 public function testRetrieve() {
6a488035 99 $firstName = 'Shane';
92915c55
TO
100 $lastName = 'Whatson';
101 $params = array(
6a488035
TO
102 'first_name' => $firstName,
103 'last_name' => $lastName,
104 'contact_type' => 'Individual',
105 );
106
107 $contact = CRM_Contact_BAO_Contact::add($params);
108 $price = 100.00;
109 $cParams = array(
110 'contact_id' => $contact->id,
111 'total_amount' => $price,
112 'financial_type_id' => 1,
92915c55
TO
113 'is_active' => 1,
114 'skipLineItem' => 1,
6a488035
TO
115 );
116
117 $defaults = array();
118 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
119 $lParams = array(
120 'entity_id' => $contribution->id,
121 'entity_table' => 'civicrm_contribution',
122 'price_field_id' => 1,
123 'qty' => 1,
124 'label' => 'Contribution Amount',
125 'unit_price' => $price,
126 'line_total' => $price,
127 'price_field_value_id' => 1,
128 'financial_type_id' => 1,
129 );
130
131 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
132 CRM_Financial_BAO_FinancialItem::add($lineItem, $contribution);
133 $values = array();
134 $fParams = array(
135 'entity_id' => $lineItem->id,
136 'entity_table' => 'civicrm_line_item',
137 );
138 $financialItem = CRM_Financial_BAO_FinancialItem::retrieve($fParams, $values);
481a74f4 139 $this->assertEquals($financialItem->amount, $price, 'Verify financial item amount.');
6a488035
TO
140 }
141
142 /**
100fef9d 143 * Check method create()
6a488035 144 */
00be9182 145 public function testCreate() {
6a488035 146 $firstName = 'Shane';
92915c55
TO
147 $lastName = 'Whatson';
148 $params = array(
6a488035
TO
149 'first_name' => $firstName,
150 'last_name' => $lastName,
151 'contact_type' => 'Individual',
152 );
153
154 $contact = CRM_Contact_BAO_Contact::add($params);
155 $price = 100.00;
156 $cParams = array(
157 'contact_id' => $contact->id,
158 'total_amount' => $price,
159 'financial_type_id' => 1,
92915c55
TO
160 'is_active' => 1,
161 'skipLineItem' => 1,
6a488035
TO
162 );
163
164 $defaults = array();
165 $contribution = CRM_Contribute_BAO_Contribution::add($cParams, $defaults);
166 $lParams = array(
167 'entity_id' => $contribution->id,
168 'entity_table' => 'civicrm_contribution',
169 'price_field_id' => 1,
170 'qty' => 1,
171 'label' => 'Contribution Amount',
172 'unit_price' => $price,
173 'line_total' => $price,
174 'price_field_value_id' => 1,
175 'financial_type_id' => 1,
176 );
177
178 $lineItem = CRM_Price_BAO_LineItem::create($lParams);
179 $fParams = array(
180 'contact_id' => $contact->id,
181 'description' => 'Contribution Amount',
182 'amount' => $price,
183 'financial_account_id' => 1,
184 'status_id' => 1,
185 'transaction_date' => date('YmdHis'),
186 'entity_id' => $lineItem->id,
187 'entity_table' => 'civicrm_line_item',
188 );
189
190 CRM_Financial_BAO_FinancialItem::create($fParams);
191 $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
192 $entityTrxn->entity_table = 'civicrm_contribution';
193 $entityTrxn->entity_id = $contribution->id;
194 $entityTrxn->amount = $price;
195 if ($entityTrxn->find(TRUE)) {
196 $entityId = $entityTrxn->entity_id;
197 }
198
199 $result = $this->assertDBNotNull(
200 'CRM_Financial_DAO_FinancialItem',
6c6e6187 201 $lineItem->id,
6a488035
TO
202 'amount',
203 'entity_id',
204 'Database check on added financial item record.'
205 );
206
481a74f4 207 $this->assertEquals($result, $price, 'Verify Amount for Financial Item');
6a488035
TO
208 $entityResult = $this->assertDBNotNull(
209 'CRM_Financial_DAO_EntityFinancialTrxn',
6c6e6187 210 $entityId,
6a488035
TO
211 'amount',
212 'entity_id',
213 'Database check on added entity financial trxn record.'
214 );
481a74f4 215 $this->assertEquals($entityResult, $price, 'Verify Amount for Financial Item');
6a488035
TO
216 }
217
218 /**
100fef9d 219 * Check method del()
6a488035 220 */
00be9182 221 public function testCreateEntityTrxn() {
6a488035 222 $fParams = array(
92fcb95f 223 'name' => 'Donations' . substr(sha1(rand()), 0, 7),
6a488035
TO
224 'is_deductible' => 0,
225 'is_active' => 1,
226 );
227
228 $amount = 200;
229 $ids = array();
230 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($fParams, $ids);
231 $financialTrxn = new CRM_Financial_DAO_FinancialTrxn();
232 $financialTrxn->to_financial_account_id = $financialAccount->id;
233 $financialTrxn->total_amount = $amount;
234 $financialTrxn->save();
235 $params = array(
236 'entity_table' => 'civicrm_contribution',
237 'entity_id' => 1,
238 'financial_trxn_id' => $financialTrxn->id,
239 'amount' => $amount,
240 );
241
242 $entityTrxn = CRM_Financial_BAO_FinancialItem::createEntityTrxn($params);
243 $entityResult = $this->assertDBNotNull(
244 'CRM_Financial_DAO_EntityFinancialTrxn',
6c6e6187 245 $financialTrxn->id,
6a488035
TO
246 'amount',
247 'financial_trxn_id',
248 'Database check on added entity financial trxn record.'
249 );
481a74f4 250 $this->assertEquals($entityResult, $amount, 'Verify Amount for Financial Item');
6a488035
TO
251 return $entityTrxn;
252 }
253
254 /**
100fef9d 255 * Check method retrieveEntityFinancialTrxn()
6a488035 256 */
00be9182 257 public function testRetrieveEntityFinancialTrxn() {
6a488035
TO
258 $entityTrxn = self::testCreateEntityTrxn();
259 $params = array(
260 'entity_table' => 'civicrm_contribution',
261 'entity_id' => 1,
262 'financial_trxn_id' => $entityTrxn->financial_trxn_id,
263 'amount' => $entityTrxn->amount,
264 );
265
266 CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params);
267 $entityResult = $this->assertDBNotNull(
268 'CRM_Financial_DAO_EntityFinancialTrxn',
269 $entityTrxn->financial_trxn_id,
270 'amount',
271 'financial_trxn_id',
272 'Database check on added entity financial trxn record.'
273 );
481a74f4 274 $this->assertEquals($entityResult, $entityTrxn->amount, 'Verify Amount for Financial Item');
6a488035 275 }
96025800 276
00b4d571
PN
277 /**
278 * Check method getPreviousFinancialItem().
279 */
280 public function testGetPreviousFinancialItem() {
281 $contactId = $this->individualCreate();
282
283 $params = array(
284 'contact_id' => $contactId,
285 'currency' => 'USD',
286 'financial_type_id' => 1,
287 'contribution_status_id' => 1,
288 'payment_instrument_id' => 1,
289 'source' => 'STUDENT',
290 'receive_date' => '20160522000000',
291 'receipt_date' => '20160522000000',
292 'non_deductible_amount' => 0.00,
293 'total_amount' => 100.00,
294 'trxn_id' => '22ereerwww444444',
295 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
296 );
297
298 $contribution = CRM_Contribute_BAO_Contribution::create($params);
299
300 $params = array(
301 'id' => $contribution->id,
302 'total_amount' => 300.00,
303 );
304
305 $contribution = CRM_Contribute_BAO_Contribution::create($params);
306 $financialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution->id);
cf28d075 307 $params = array('id' => $financialItem['id']);
00b4d571 308 $financialItem = $this->callAPISuccess('FinancialItem', 'get', $params);
cf28d075 309 $this->assertEquals(200.00, $financialItem['values'][$financialItem['id']]['amount'], "The amounts do not match.");
00b4d571
PN
310 }
311
ffa4c203
PN
312 /**
313 * Check method getPreviousFinancialItem() with tax entry.
314 */
315 public function testGetPreviousFinancialItemHavingTax() {
316 $contactId = $this->individualCreate();
317 $this->enableTaxAndInvoicing();
318 $this->relationForFinancialTypeWithFinancialAccount(1);
319 $form = new CRM_Contribute_Form_Contribution();
320 $form->testSubmit(array(
321 'total_amount' => 100,
322 'financial_type_id' => 1,
323 'receive_date' => '04/21/2015',
324 'receive_date_time' => '11:27PM',
325 'contact_id' => $contactId,
326 'contribution_status_id' => 1,
327 'price_set_id' => 0,
328 ),
329 CRM_Core_Action::ADD
330 );
331 $contribution = $this->callAPISuccessGetSingle('Contribution',
332 array(
333 'contact_id' => $contactId,
334 'return' => array('id'),
335 )
336 );
337 $financialItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
338 $params = array(
339 'id' => $financialItem['id'],
340 'return' => array(
341 'description',
342 'status_id',
343 'amount',
344 'financial_account_id',
345 ),
346 );
347 $checkAgainst = array(
348 'id' => $financialItem['id'],
349 'description' => 'Contribution Amount',
350 'status_id' => '1',
351 'amount' => '100.00',
352 'financial_account_id' => '1',
353 );
354 $this->callAPISuccessGetSingle('FinancialItem', $params, $checkAgainst);
355 }
356
232624b1 357}