Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
30 | */ | |
6a488035 TO |
31 | class CRM_Financial_BAO_FinancialItemTest extends CiviUnitTestCase { |
32 | ||
00be9182 | 33 | public function setUp() { |
6a488035 TO |
34 | parent::setUp(); |
35 | } | |
36 | ||
37 | /** | |
100fef9d | 38 | * Check method add() |
6a488035 | 39 | */ |
00be9182 | 40 | public function testAdd() { |
6a488035 | 41 | $firstName = 'Shane'; |
92915c55 TO |
42 | $lastName = 'Whatson'; |
43 | $params = array( | |
6a488035 TO |
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, | |
92915c55 TO |
56 | 'is_active' => 1, |
57 | 'skipLineItem' => 1, | |
6a488035 TO |
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', | |
6c6e6187 | 78 | $lineItem->id, |
6a488035 TO |
79 | 'amount', |
80 | 'entity_id', | |
81 | 'Database check on added financial item record.' | |
82 | ); | |
481a74f4 | 83 | $this->assertEquals($result, $price, 'Verify Amount for Financial Item'); |
6a488035 TO |
84 | } |
85 | ||
86 | /** | |
100fef9d | 87 | * Check method retrive() |
6a488035 | 88 | */ |
00be9182 | 89 | public function testRetrieve() { |
6a488035 | 90 | $firstName = 'Shane'; |
92915c55 TO |
91 | $lastName = 'Whatson'; |
92 | $params = array( | |
6a488035 TO |
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, | |
92915c55 TO |
104 | 'is_active' => 1, |
105 | 'skipLineItem' => 1, | |
6a488035 TO |
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); | |
481a74f4 | 130 | $this->assertEquals($financialItem->amount, $price, 'Verify financial item amount.'); |
6a488035 TO |
131 | } |
132 | ||
133 | /** | |
100fef9d | 134 | * Check method create() |
6a488035 | 135 | */ |
00be9182 | 136 | public function testCreate() { |
6a488035 | 137 | $firstName = 'Shane'; |
92915c55 TO |
138 | $lastName = 'Whatson'; |
139 | $params = array( | |
6a488035 TO |
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, | |
92915c55 TO |
151 | 'is_active' => 1, |
152 | 'skipLineItem' => 1, | |
6a488035 TO |
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', | |
6c6e6187 | 192 | $lineItem->id, |
6a488035 TO |
193 | 'amount', |
194 | 'entity_id', | |
195 | 'Database check on added financial item record.' | |
196 | ); | |
197 | ||
481a74f4 | 198 | $this->assertEquals($result, $price, 'Verify Amount for Financial Item'); |
6a488035 TO |
199 | $entityResult = $this->assertDBNotNull( |
200 | 'CRM_Financial_DAO_EntityFinancialTrxn', | |
6c6e6187 | 201 | $entityId, |
6a488035 TO |
202 | 'amount', |
203 | 'entity_id', | |
204 | 'Database check on added entity financial trxn record.' | |
205 | ); | |
481a74f4 | 206 | $this->assertEquals($entityResult, $price, 'Verify Amount for Financial Item'); |
6a488035 TO |
207 | } |
208 | ||
209 | /** | |
100fef9d | 210 | * Check method del() |
6a488035 | 211 | */ |
00be9182 | 212 | public function testCreateEntityTrxn() { |
6a488035 | 213 | $fParams = array( |
92fcb95f | 214 | 'name' => 'Donations' . substr(sha1(rand()), 0, 7), |
6a488035 TO |
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', | |
6c6e6187 | 236 | $financialTrxn->id, |
6a488035 TO |
237 | 'amount', |
238 | 'financial_trxn_id', | |
239 | 'Database check on added entity financial trxn record.' | |
240 | ); | |
481a74f4 | 241 | $this->assertEquals($entityResult, $amount, 'Verify Amount for Financial Item'); |
6a488035 TO |
242 | return $entityTrxn; |
243 | } | |
244 | ||
245 | /** | |
100fef9d | 246 | * Check method retrieveEntityFinancialTrxn() |
6a488035 | 247 | */ |
00be9182 | 248 | public function testRetrieveEntityFinancialTrxn() { |
6a488035 TO |
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 | ); | |
481a74f4 | 265 | $this->assertEquals($entityResult, $entityTrxn->amount, 'Verify Amount for Financial Item'); |
6a488035 | 266 | } |
96025800 | 267 | |
232624b1 | 268 | } |