3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class CRM_Financial_BAO_FinancialTypeTest
32 class CRM_Financial_BAO_FinancialTypeTest
extends CiviUnitTestCase
{
34 public function setUp() {
36 $this->_orgContactID
= $this->organizationCreate();
39 public function teardown() {
40 $this->financialAccountDelete('Donations');
46 public function testAdd() {
48 'name' => 'Donations',
54 $financialType = CRM_Financial_BAO_FinancialType
::add($params, $ids);
55 $result = $this->assertDBNotNull(
56 'CRM_Financial_DAO_FinancialType',
60 'Database check on added financial type record.'
62 $this->assertEquals($result, 'Donations', 'Verify Name for Financial Type');
66 * Check method retrieve().
68 public function testRetrieve() {
70 'name' => 'Donations',
77 CRM_Financial_BAO_FinancialType
::add($params, $ids);
80 $result = CRM_Financial_BAO_FinancialType
::retrieve($params, $defaults);
81 $this->assertEquals($result->name
, 'Donations', 'Verify Name for Financial Type');
85 * Check method setIsActive()
87 public function testSetIsActive() {
89 'name' => 'Donations',
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',
102 'Database check on updated for financial type is_active.'
104 $this->assertEquals($isActive, 0, 'Verify financial types is_active.');
110 public function testDel() {
112 'name' => 'Donations',
113 'is_deductible' => 0,
117 $financialType = CRM_Financial_BAO_FinancialType
::add($params, $ids);
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.');
126 * Set ACLs for Financial Types()
128 public function setACL() {
129 CRM_Core_BAO_Setting
::setItem(array('acl_financial_type' => 1), NULL, 'contribution_invoice_settings');
133 * Check method testGetAvailableFinancialTypes()
135 public function testGetAvailableFinancialTypes() {
137 $this->setPermissions(array(
138 'view contributions of type Donation',
139 'view contributions of type Member Dues',
142 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($types);
143 $expectedResult = array(
147 $this->assertEquals($expectedResult, $types, 'Verify that only certain financial types can be retrieved');
149 $this->setPermissions(array(
150 'view contributions of type Donation',
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');
158 * Check method testgetAvailableMembershipTypes()
160 public function testgetAvailableMembershipTypes() {
161 // Create Membership types
164 'name' => 'Type One',
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',
176 $membershipType = CRM_Member_BAO_MembershipType
::add($params, $ids);
178 $params['name'] = 'Type Two';
179 $params['financial_type_id'] = 2;
180 $membershipType = CRM_Member_BAO_MembershipType
::add($params, $ids);
184 $this->setPermissions(array(
185 'view contributions of type Donation',
186 'view contributions of type Member Dues',
188 CRM_Financial_BAO_FinancialType
::getAvailableMembershipTypes($types);
189 $expectedResult = array(
193 $this->assertEquals($expectedResult, $types, 'Verify that only certain membership types can be retrieved');
194 $this->setPermissions(array(
195 'view contributions of type Donation',
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');
203 * Check method testPermissionedFinancialTypes()
205 public function testPermissionedFinancialTypes() {
206 // First get all core permissions
207 $permissions = $checkPerms = CRM_Core_Permission
::getCorePermissions();
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,
221 $checkPerms['administer CiviCRM Financial Types'] = array(
222 $prefix . ts('administer CiviCRM Financial Types'),
223 ts('Administer access to Financial Types'),
225 $this->assertEquals($permissions, $checkPerms, 'Verify that permissions for each financial type have been added');
229 * Check method testcheckPermissionedLineItems()
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;
239 $priceset = CRM_Price_BAO_PriceSet
::create($paramsSet);
240 $priceSetId = $priceset->id
;
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'
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,
257 'options_per_line' => 1,
258 'is_active' => array('1' => 1, '2' => 1),
259 'price_set_id' => $priceset->id
,
261 'financial_type_id' => 1,
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,
268 'contact_id' => $contactId,
269 'financial_type_id' => 1,
270 'contribution_status_id' => 1,
273 foreach ($priceFields['values'] as $key => $priceField) {
274 $lineItems[1][$key] = array(
275 'price_field_id' => $priceField['price_field_id'],
276 'price_field_value_id' => $priceField['id'],
277 'label' => $priceField['label'],
278 'field_title' => $priceField['label'],
280 'unit_price' => $priceField['amount'],
281 'line_total' => $priceField['amount'],
282 'financial_type_id' => $priceField['financial_type_id'],
285 $contributionParams['line_item'] = $lineItems;
286 $contributions = CRM_Contribute_BAO_Contribution
::create($contributionParams);
287 CRM_Financial_BAO_FinancialType
::$_statusACLFt = array();
290 $this->setPermissions(array(
291 'view contributions of type Member Dues',
295 CRM_Financial_BAO_FinancialType
::checkPermissionedLineItems($contributions->id
, 'view');
296 $this->fail("Missed expected exception");
298 catch (Exception
$e) {
299 $this->assertEquals("A fatal error was triggered: You do not have permission to access this page.", $e->getMessage());
302 $this->setPermissions(array(
303 'view contributions of type Donation',
305 $perm = CRM_Financial_BAO_FinancialType
::checkPermissionedLineItems($contributions->id
, 'view');
306 $this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.');
310 * Check method testisACLFinancialTypeStatus()
312 public function testisACLFinancialTypeStatus() {
313 $isACL = CRM_Core_BAO_Setting
::getItem(NULL, 'contribution_invoice_settings');
314 $this->assertEquals(array_search('acl_financial_type', $isACL), NULL);
316 $isACL = CRM_Core_BAO_Setting
::getItem(NULL, 'contribution_invoice_settings');
317 $this->assertEquals($isACL, array('acl_financial_type' => 1));
321 * Check method testisACLFinancialTypeStatus()
323 public function testBuildPermissionedClause() {
325 $this->setPermissions(array(
326 'view contributions of type Donation',
327 'view contributions of type Member Dues',
329 CRM_Financial_BAO_FinancialType
::buildPermissionedClause($whereClause, 'contribution');
330 $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,2)');
331 $this->setPermissions(array(
332 'view contributions of type Donation',
333 'view contributions of type Member Dues',
334 'view contributions of type Event Fee',
338 CRM_Financial_BAO_FinancialType
::buildPermissionedClause($whereClause, 'contribution');
339 $this->assertEquals($whereClause, ' civicrm_contribution.financial_type_id IN (1,4,2)');