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