CRM-16526 CIVI-3 Added unit test for permissioned financial types
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeTest.php
CommitLineData
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_FinancialTypeTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035
TO
32class 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 /**
100fef9d 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 /**
100fef9d 66 * Check method retrive()
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 /**
133 * Check method testgetAvailableFinancialTypes()
134 */
135 public function testgetAvailableFinancialTypes() {
136 $this->setACL();
137 $config = &CRM_Core_Config::singleton();
138 $config->userPermissionClass->permissions = array(
139 'view contributions of type Donation',
140 'view contributions of type Member Dues',
141 );
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 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
149 $config->userPermissionClass->permissions = 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 $config = &CRM_Core_Config::singleton();
184 $config->userPermissionClass->permissions = 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 $config->userPermissionClass->permissions = 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 }
823973c9
E
201
202 public function testpermissionedFinancialTypes() {
203 // First get all core permissions
204 $permissions = $checkPerms = CRM_Core_Permission::getCorePermissions();
205 $this->setACL();
206 CRM_Financial_BAO_FinancialType::permissionedFinancialTypes($permissions, TRUE);
207 $financialTypes = CRM_Contribute_PseudoConstant::financialType();
208 $prefix = ts('CiviCRM') . ': ';
209 $actions = array('add', 'view', 'edit', 'delete');
210 foreach ($financialTypes as $id => $type) {
211 foreach ($actions as $action) {
212 $checkPerms[$action . ' contributions of type ' . $type] = array(
213 $prefix . ts($action . ' contributions of type ') . $type,
214 ts(ucfirst($action) . ' contributions of type ') . $type,
215 );
216 }
217 }
218 $checkPerms['administer CiviCRM Financial Types'] = array(
219 $prefix . ts('administer CiviCRM Financial Types'),
220 ts('Administer access to Financial Types'),
221 );
222 $this->assertEquals($permissions, $checkPerms, 'Verify that permissions for each financial type have been added');
223 }
da3709a1 224
232624b1 225}