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