Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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_FinancialTypeTest | |
acb109b7 | 30 | * @group headless |
e9479dcf | 31 | */ |
6a488035 TO |
32 | class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase { |
33 | ||
00be9182 | 34 | public function setUp() { |
6a488035 | 35 | parent::setUp(); |
da3709a1 | 36 | $this->_orgContactID = $this->organizationCreate(); |
6a488035 TO |
37 | } |
38 | ||
00be9182 | 39 | public function teardown() { |
f17d75bb PN |
40 | $this->financialAccountDelete('Donations'); |
41 | } | |
42 | ||
6a488035 | 43 | /** |
8d35246a | 44 | * Check method add(). |
6a488035 | 45 | */ |
00be9182 | 46 | public function testAdd() { |
6a488035 TO |
47 | $params = array( |
48 | 'name' => 'Donations', | |
49 | 'is_active' => 1, | |
50 | 'is_deductible' => 1, | |
51 | 'is_reserved' => 1, | |
52 | ); | |
53 | $ids = array(); | |
54 | $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); | |
55 | $result = $this->assertDBNotNull( | |
56 | 'CRM_Financial_DAO_FinancialType', | |
6c6e6187 | 57 | $financialType->id, |
6a488035 TO |
58 | 'name', |
59 | 'id', | |
60 | 'Database check on added financial type record.' | |
61 | ); | |
481a74f4 | 62 | $this->assertEquals($result, 'Donations', 'Verify Name for Financial Type'); |
6a488035 TO |
63 | } |
64 | ||
65 | /** | |
8d35246a | 66 | * Check method retrieve(). |
6a488035 | 67 | */ |
00be9182 | 68 | public function testRetrieve() { |
6a488035 TO |
69 | $params = array( |
70 | 'name' => 'Donations', | |
71 | 'is_active' => 1, | |
72 | 'is_deductible' => 1, | |
73 | 'is_reserved' => 1, | |
74 | ); | |
75 | ||
76 | $ids = array(); | |
77 | CRM_Financial_BAO_FinancialType::add($params, $ids); | |
78 | ||
79 | $defaults = array(); | |
80 | $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults); | |
f17d75bb | 81 | $this->assertEquals($result->name, 'Donations', 'Verify Name for Financial Type'); |
6a488035 TO |
82 | } |
83 | ||
84 | /** | |
100fef9d | 85 | * Check method setIsActive() |
6a488035 | 86 | */ |
00be9182 | 87 | public function testSetIsActive() { |
6a488035 | 88 | $params = array( |
f17d75bb | 89 | 'name' => 'Donations', |
6a488035 TO |
90 | 'is_deductible' => 0, |
91 | 'is_active' => 1, | |
92 | ); | |
93 | $ids = array(); | |
94 | $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); | |
95 | $result = CRM_Financial_BAO_FinancialType::setIsActive($financialType->id, 0); | |
6c6e6187 | 96 | $this->assertEquals($result, TRUE, 'Verify financial type record updation for is_active.'); |
6a488035 TO |
97 | $isActive = $this->assertDBNotNull( |
98 | 'CRM_Financial_DAO_FinancialType', | |
6c6e6187 | 99 | $financialType->id, |
6a488035 TO |
100 | 'is_active', |
101 | 'id', | |
102 | 'Database check on updated for financial type is_active.' | |
103 | ); | |
f17d75bb | 104 | $this->assertEquals($isActive, 0, 'Verify financial types is_active.'); |
6a488035 TO |
105 | } |
106 | ||
107 | /** | |
100fef9d | 108 | * Check method del() |
6a488035 | 109 | */ |
00be9182 | 110 | public function testDel() { |
6a488035 | 111 | $params = array( |
f17d75bb | 112 | 'name' => 'Donations', |
6a488035 TO |
113 | 'is_deductible' => 0, |
114 | 'is_active' => 1, | |
115 | ); | |
116 | $ids = array(); | |
117 | $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids); | |
118 | ||
119 | CRM_Financial_BAO_FinancialType::del($financialType->id); | |
f17d75bb | 120 | $params = array('id' => $financialType->id); |
6a488035 | 121 | $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults); |
6c6e6187 | 122 | $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.'); |
6a488035 | 123 | } |
96025800 | 124 | |
da3709a1 E |
125 | /** |
126 | * Set ACLs for Financial Types() | |
127 | */ | |
128 | public function setACL() { | |
129 | CRM_Core_BAO_Setting::setItem(array('acl_financial_type' => 1), NULL, 'contribution_invoice_settings'); | |
130 | } | |
131 | ||
132 | /** | |
8d35246a | 133 | * Check method testGetAvailableFinancialTypes() |
da3709a1 | 134 | */ |
8d35246a | 135 | public function testGetAvailableFinancialTypes() { |
da3709a1 | 136 | $this->setACL(); |
8d35246a | 137 | $this->setPermissions(array( |
da3709a1 E |
138 | 'view contributions of type Donation', |
139 | 'view contributions of type Member Dues', | |
8d35246a | 140 | )); |
4954339a | 141 | $types = array(); |
da3709a1 E |
142 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types); |
143 | $expectedResult = array( | |
144 | 1 => "Donation", | |
145 | 2 => "Member Dues", | |
146 | ); | |
147 | $this->assertEquals($expectedResult, $types, 'Verify that only certain financial types can be retrieved'); | |
8d35246a EM |
148 | |
149 | $this->setPermissions(array( | |
da3709a1 | 150 | 'view contributions of type Donation', |
8d35246a | 151 | )); |
da3709a1 E |
152 | unset($expectedResult[2]); |
153 | CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types); | |
154 | $this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available financial types'); | |
155 | } | |
156 | ||
157 | /** | |
158 | * Check method testgetAvailableMembershipTypes() | |
159 | */ | |
160 | public function testgetAvailableMembershipTypes() { | |
161 | // Create Membership types | |
162 | $ids = array(); | |
163 | $params = array( | |
164 | 'name' => 'Type One', | |
165 | 'domain_id' => 1, | |
166 | 'minimum_fee' => 10, | |
167 | 'duration_unit' => 'year', | |
168 | 'member_of_contact_id' => $this->_orgContactID, | |
169 | 'period_type' => 'fixed', | |
170 | 'duration_interval' => 1, | |
171 | 'financial_type_id' => 1, | |
172 | 'visibility' => 'Public', | |
173 | 'is_active' => 1, | |
174 | ); | |
175 | ||
176 | $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids); | |
177 | // Add another | |
178 | $params['name'] = 'Type Two'; | |
179 | $params['financial_type_id'] = 2; | |
180 | $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids); | |
181 | ||
182 | $this->setACL(); | |
8d35246a EM |
183 | |
184 | $this->setPermissions(array( | |
da3709a1 E |
185 | 'view contributions of type Donation', |
186 | 'view contributions of type Member Dues', | |
8d35246a | 187 | )); |
da3709a1 E |
188 | CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($types); |
189 | $expectedResult = array( | |
190 | 1 => "Type One", | |
191 | 2 => "Type Two", | |
192 | ); | |
193 | $this->assertEquals($expectedResult, $types, 'Verify that only certain membership types can be retrieved'); | |
8d35246a | 194 | $this->setPermissions(array( |
da3709a1 | 195 | 'view contributions of type Donation', |
8d35246a | 196 | )); |
da3709a1 E |
197 | unset($expectedResult[2]); |
198 | CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($types); | |
199 | $this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available membership types'); | |
200 | } | |
0f6eb8c9 E |
201 | |
202 | /** | |
8d35246a | 203 | * Check method testPermissionedFinancialTypes() |
0f6eb8c9 | 204 | */ |
8d35246a | 205 | public function testPermissionedFinancialTypes() { |
823973c9 E |
206 | // First get all core permissions |
207 | $permissions = $checkPerms = CRM_Core_Permission::getCorePermissions(); | |
208 | $this->setACL(); | |
209 | CRM_Financial_BAO_FinancialType::permissionedFinancialTypes($permissions, TRUE); | |
210 | $financialTypes = CRM_Contribute_PseudoConstant::financialType(); | |
211 | $prefix = ts('CiviCRM') . ': '; | |
212 | $actions = array('add', 'view', 'edit', 'delete'); | |
213 | foreach ($financialTypes as $id => $type) { | |
214 | foreach ($actions as $action) { | |
215 | $checkPerms[$action . ' contributions of type ' . $type] = array( | |
216 | $prefix . ts($action . ' contributions of type ') . $type, | |
217 | ts(ucfirst($action) . ' contributions of type ') . $type, | |
218 | ); | |
219 | } | |
220 | } | |
221 | $checkPerms['administer CiviCRM Financial Types'] = array( | |
222 | $prefix . ts('administer CiviCRM Financial Types'), | |
223 | ts('Administer access to Financial Types'), | |
224 | ); | |
225 | $this->assertEquals($permissions, $checkPerms, 'Verify that permissions for each financial type have been added'); | |
226 | } | |
da3709a1 | 227 | |
bf01d873 E |
228 | /** |
229 | * Check method testcheckPermissionedLineItems() | |
230 | */ | |
231 | public function testcheckPermissionedLineItems() { | |
6ae19242 | 232 | $contactId = $this->individualCreate(); |
bf01d873 E |
233 | $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4); |
234 | $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']); | |
235 | $paramsSet['is_active'] = TRUE; | |
236 | $paramsSet['financial_type_id'] = 1; | |
237 | $paramsSet['extends'] = 1; | |
238 | ||
239 | $priceset = CRM_Price_BAO_PriceSet::create($paramsSet); | |
240 | $priceSetId = $priceset->id; | |
241 | ||
242 | //Checking for priceset added in the table. | |
243 | $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title', | |
244 | 'id', $paramsSet['title'], 'Check DB for created priceset' | |
245 | ); | |
246 | $paramsField = array( | |
247 | 'label' => 'Price Field', | |
248 | 'name' => CRM_Utils_String::titleToVar('Price Field'), | |
249 | 'html_type' => 'CheckBox', | |
250 | 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'), | |
251 | 'option_value' => array('1' => 100, '2' => 200), | |
252 | 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'), | |
253 | 'option_weight' => array('1' => 1, '2' => 2), | |
254 | 'option_amount' => array('1' => 100, '2' => 200), | |
255 | 'is_display_amounts' => 1, | |
256 | 'weight' => 1, | |
257 | 'options_per_line' => 1, | |
258 | 'is_active' => array('1' => 1, '2' => 1), | |
259 | 'price_set_id' => $priceset->id, | |
260 | 'is_enter_qty' => 1, | |
261 | 'financial_type_id' => 1, | |
262 | ); | |
263 | $priceField = CRM_Price_BAO_PriceField::create($paramsField); | |
264 | $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id)); | |
265 | $contributionParams = array( | |
266 | 'total_amount' => 300, | |
267 | 'currency' => 'USD', | |
268 | 'contact_id' => $contactId, | |
269 | 'financial_type_id' => 1, | |
270 | 'contribution_status_id' => 1, | |
d2da7b20 | 271 | 'skipCleanMoney' => TRUE, |
bf01d873 E |
272 | ); |
273 | ||
274 | foreach ($priceFields['values'] as $key => $priceField) { | |
275 | $lineItems[1][$key] = array( | |
276 | 'price_field_id' => $priceField['price_field_id'], | |
277 | 'price_field_value_id' => $priceField['id'], | |
278 | 'label' => $priceField['label'], | |
279 | 'field_title' => $priceField['label'], | |
280 | 'qty' => 1, | |
281 | 'unit_price' => $priceField['amount'], | |
282 | 'line_total' => $priceField['amount'], | |
283 | 'financial_type_id' => $priceField['financial_type_id'], | |
284 | ); | |
285 | } | |
286 | $contributionParams['line_item'] = $lineItems; | |
287 | $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams); | |
288 | CRM_Financial_BAO_FinancialType::$_statusACLFt = array(); | |
289 | $this->setACL(); | |
8d35246a EM |
290 | |
291 | $this->setPermissions(array( | |
bf01d873 | 292 | 'view contributions of type Member Dues', |
8d35246a | 293 | )); |
bf01d873 E |
294 | |
295 | try { | |
8d35246a | 296 | CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view'); |
bf01d873 E |
297 | $this->fail("Missed expected exception"); |
298 | } | |
299 | catch (Exception $e) { | |
300 | $this->assertEquals("A fatal error was triggered: You do not have permission to access this page.", $e->getMessage()); | |
301 | } | |
8d35246a EM |
302 | |
303 | $this->setPermissions(array( | |
bf01d873 | 304 | 'view contributions of type Donation', |
8d35246a | 305 | )); |
bf01d873 E |
306 | $perm = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view'); |
307 | $this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.'); | |
308 | } | |
309 | ||
310 | /** | |
311 | * Check method testisACLFinancialTypeStatus() | |
312 | */ | |
313 | public function testisACLFinancialTypeStatus() { | |
314 | $isACL = CRM_Core_BAO_Setting::getItem(NULL, 'contribution_invoice_settings'); | |
315 | $this->assertEquals(array_search('acl_financial_type', $isACL), NULL); | |
316 | $this->setACL(); | |
317 | $isACL = CRM_Core_BAO_Setting::getItem(NULL, 'contribution_invoice_settings'); | |
318 | $this->assertEquals($isACL, array('acl_financial_type' => 1)); | |
319 | } | |
320 | ||
b391ead9 E |
321 | /** |
322 | * Check method testisACLFinancialTypeStatus() | |
323 | */ | |
8d35246a | 324 | public function testBuildPermissionedClause() { |
b391ead9 | 325 | $this->setACL(); |
8d35246a | 326 | $this->setPermissions(array( |
b391ead9 E |
327 | 'view contributions of type Donation', |
328 | 'view contributions of type Member Dues', | |
8d35246a | 329 | )); |
b391ead9 E |
330 | CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution'); |
331 | $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,2)'); | |
8d35246a EM |
332 | $this->setPermissions(array( |
333 | 'view contributions of type Donation', | |
334 | 'view contributions of type Member Dues', | |
335 | 'view contributions of type Event Fee', | |
336 | )); | |
b391ead9 | 337 | $whereClause = NULL; |
8d35246a | 338 | |
b391ead9 | 339 | CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution'); |
4eb3c100 | 340 | $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,4,2)'); |
b391ead9 E |
341 | } |
342 | ||
232624b1 | 343 | } |