Merge pull request #13337 from GinkgoFJG/crmPageTitle
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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_FinancialTypeTest
30 * @group headless
31 */
32 class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 $this->_orgContactID = $this->organizationCreate();
37 }
38
39 public function teardown() {
40 $this->financialAccountDelete('Donations');
41 }
42
43 /**
44 * Check method add().
45 */
46 public function testAdd() {
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',
57 $financialType->id,
58 'name',
59 'id',
60 'Database check on added financial type record.'
61 );
62 $this->assertEquals($result, 'Donations', 'Verify Name for Financial Type');
63 }
64
65 /**
66 * Check method retrieve().
67 */
68 public function testRetrieve() {
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);
81 $this->assertEquals($result->name, 'Donations', 'Verify Name for Financial Type');
82 }
83
84 /**
85 * Check method setIsActive()
86 */
87 public function testSetIsActive() {
88 $params = array(
89 'name' => 'Donations',
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);
96 $this->assertEquals($result, TRUE, 'Verify financial type record updation for is_active.');
97 $isActive = $this->assertDBNotNull(
98 'CRM_Financial_DAO_FinancialType',
99 $financialType->id,
100 'is_active',
101 'id',
102 'Database check on updated for financial type is_active.'
103 );
104 $this->assertEquals($isActive, 0, 'Verify financial types is_active.');
105 }
106
107 /**
108 * Check method del()
109 */
110 public function testDel() {
111 $params = array(
112 'name' => 'Donations',
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);
120 $params = array('id' => $financialType->id);
121 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
122 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
123 }
124
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 /**
133 * Check method testGetAvailableFinancialTypes()
134 */
135 public function testGetAvailableFinancialTypes() {
136 $this->setACL();
137 $this->setPermissions(array(
138 'view contributions of type Donation',
139 'view contributions of type Member Dues',
140 ));
141 $types = array();
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');
148
149 $this->setPermissions(array(
150 'view contributions of type Donation',
151 ));
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();
183
184 $this->setPermissions(array(
185 'view contributions of type Donation',
186 'view contributions of type Member Dues',
187 ));
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');
194 $this->setPermissions(array(
195 'view contributions of type Donation',
196 ));
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 }
201
202 /**
203 * Check method testPermissionedFinancialTypes()
204 */
205 public function testPermissionedFinancialTypes() {
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 }
227
228 /**
229 * Check method testcheckPermissionedLineItems()
230 */
231 public function testcheckPermissionedLineItems() {
232 $contactId = $this->individualCreate();
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,
271 'skipCleanMoney' => TRUE,
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();
290
291 $this->setPermissions(array(
292 'view contributions of type Member Dues',
293 ));
294
295 try {
296 CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
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 }
302
303 $this->setPermissions(array(
304 'view contributions of type Donation',
305 ));
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
321 /**
322 * Check method testisACLFinancialTypeStatus()
323 */
324 public function testBuildPermissionedClause() {
325 $this->setACL();
326 $this->setPermissions(array(
327 'view contributions of type Donation',
328 'view contributions of type Member Dues',
329 ));
330 CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution');
331 $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,2)');
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 ));
337 $whereClause = NULL;
338
339 CRM_Financial_BAO_FinancialType::buildPermissionedClause($whereClause, 'contribution');
340 $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,4,2)');
341 }
342
343 }