add inClassCache for Tinebase_Acl_Roles class
[tine20] / tests / tine20 / Tinebase / Acl / RolesTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Tinebase
6  * @subpackage  Acl
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2008-2013 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Philipp Schüle <p.schuele@metaways.de>
10  */
11
12 /**
13  * Test helper
14  */
15 require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
16
17 /**
18  * Test class for Tinebase_Acl_Roles
19  */
20 class Tinebase_Acl_RolesTest extends TestCase
21 {
22     /**
23      * @var array test objects
24      */
25     protected $objects = array();
26     
27     /**
28      * Sets up the fixture.
29      * This method is called before a test is executed.
30      *
31      * @access protected
32      */
33     protected function setUp()
34     {
35         parent::setUp();
36         
37         $this->objects['application'] = Tinebase_Application::getInstance()->getApplicationByName('Crm');
38         $this->objects['user'] = new Tinebase_Model_FullUser(array(
39             'accountLoginName'      => 'tine20phpunit',
40             'accountDisplayName'    => 'tine20phpunit',
41             'accountStatus'         => 'enabled',
42             'accountExpires'        => NULL,
43             'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getGroupByName('Users')->getId(),
44             'accountLastName'       => 'Tine 2.0',
45             'accountFirstName'      => 'PHPUnit',
46             'accountEmailAddress'   => 'phpunit@metaways.de'
47         ));
48         $this->objects['role'] = new Tinebase_Model_Role(array(
49             'id'                    => 10,
50             'name'                  => 'phpunitrole',
51             'description'           => 'test role for phpunit',
52         ));
53         $this->objects['role_2'] = new Tinebase_Model_Role(array(
54             'id'                    => 11,
55             'name'                  => 'phpunitrole 2',
56             'description'           => 'test role 2 for phpunit',
57         ));
58         
59         // add account for group / role member tests
60         $this->objects['user'] = Tinebase_User::getInstance()->addUser($this->objects['user']);
61         Tinebase_Group::getInstance()->addGroupMember($this->objects['user']->accountPrimaryGroup, $this->objects['user']);
62         
63         Tinebase_Acl_Roles::getInstance()->resetClassCache();
64     }
65
66     /**
67      * Tears down the fixture
68      * This method is called after a test is executed.
69      *
70      * @access protected
71      */
72     protected function tearDown()
73     {
74         Tinebase_User::getInstance()->deleteUser($this->objects['user']->accountId);
75         Tinebase_Acl_Roles::getInstance()->resetClassCache();
76         parent::tearDown();
77     }
78
79     /**
80      * try to add a role
81      *
82      */
83     public function testCreateRole()
84     {
85         $role = Tinebase_Acl_Roles::getInstance()->createRole($this->objects['role']);
86         
87         $this->assertEquals($role->getId(), $this->objects['role']->getId());
88     }
89     
90     /**
91      * try to add a role membership
92      *
93      */
94     public function testSetRoleMember()
95     {
96         $this->testCreateRole();
97         
98         $member = array(
99             array(
100                 "type"  => 'user',
101                 "id"    => $this->objects['user']->getId(),
102             )
103         );
104         Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role']->getId(), $member);
105         
106         $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
107         
108         $this->assertGreaterThan(0, count($members));
109     }    
110     
111     /**
112      * try to remove member from role
113      */
114     public function removeRoleMember()
115     {
116         $this->testCreateRole();
117         
118         Tinebase_Acl_Roles::getInstance()->removeRoleMember(10, $this->objects['user']);
119         
120         $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
121         
122         $this->assertEquals(0, count($members));
123     }
124     
125     /**
126      * try to add member to role
127      */
128     public function testAddRoleMember()
129     {
130         $this->testCreateRole();
131         
132         Tinebase_Acl_Roles::getInstance()->addRoleMember($this->objects['role']->getId(), array(
133             'type'     => 'user',
134             'id'    => $this->objects['user']->getId()
135         ));
136         
137         $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
138         
139         $this->assertGreaterThan(0, count($members));
140     }
141         
142     /**
143      * try to add member to multiple roles
144      */
145     public function testSetRoleMemberships()
146     {
147         $this->testCreateRole();
148         
149         // create role 2 for test
150         $role2 = Tinebase_Acl_Roles::getInstance()->createRole($this->objects['role_2']);
151         
152         // remove role members
153         Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role']->getId(), array());
154         Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role_2']->getId(), array());
155         
156         Tinebase_Acl_Roles::getInstance()->setRoleMemberships(
157             array(
158                 'type'     => 'user', 
159                 'id'     => $this->objects['user']->getId()
160             ), 
161             array(
162                 $this->objects['role']->getId(), 
163                 $this->objects['role_2']->getId()
164             )
165         );
166         
167         $members = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role']->getId());
168         $members_2 = Tinebase_Acl_Roles::getInstance()->getRoleMembers($this->objects['role_2']->getId());
169         
170         $this->assertEquals(2, (count($members) + count($members_2)));
171     }
172     
173     /**
174      * try to add a role right
175      *
176      */
177     public function testSetRoleRight()
178     {
179         $this->testCreateRole();
180         
181         $right = array(
182             array(
183                 "application_id"    => $this->objects['application']->getId(),
184                 "right"             => Tinebase_Acl_Rights::RUN,
185             )
186         );
187         Tinebase_Acl_Roles::getInstance()->setRoleRights($this->objects['role']->getId(), $right);
188         
189         $rights = Tinebase_Acl_Roles::getInstance()->getRoleRights($this->objects['role']->getId());
190         
191         $this->assertGreaterThan(0, count($rights));
192     }    
193     
194     /**
195      * try to check getting applications
196      */
197     public function testGetApplications()
198     {
199         $result = Tinebase_Acl_Roles::getInstance()->getApplications($this->objects['user']->getId());
200         
201         $this->assertGreaterThan(0, count($result->toArray()));
202     }
203
204     /**
205      * try to check getting applications
206      */
207     public function testGetApplicationRights()
208     {
209         $result = Tinebase_Acl_Roles::getInstance()->getApplicationRights(
210             $this->objects['application']->name, 
211             $this->objects['user']->getId()
212         );
213         
214         $this->assertGreaterThan(0, count($result));
215     }    
216     
217     /**
218      * try to check if user with a role has right
219      */
220     public function testHasRight()
221     {
222         $result = Tinebase_Acl_Roles::getInstance()->hasRight(
223             $this->objects['application'],
224             $this->objects['user']->getId(),
225             Tinebase_Acl_Rights::RUN
226         );
227         
228         $this->assertTrue($result, 'has no run right');
229         
230         $result = Tinebase_Acl_Roles::getInstance()->hasRight(
231             $this->objects['application'],
232             $this->objects['user']->getId(),
233             Tinebase_Acl_Rights::ADMIN
234         );
235
236         $this->assertFalse($result, 'has admin right for application ' . $this->objects['application']->name);
237         
238         // test for not installed application
239         $result = Tinebase_Acl_Roles::getInstance()->hasRight(
240             'FooBar',
241             $this->objects['user']->getId(),
242             Tinebase_Acl_Rights::ADMIN
243         );
244
245         $this->assertFalse($result, 'user has admin right for not installed application');
246         
247         // test for disabled application
248         Tinebase_Application::getInstance()->setApplicationState($this->objects['application'], Tinebase_Application::DISABLED);
249         
250         $result = Tinebase_Acl_Roles::getInstance()->hasRight(
251             $this->objects['application'],
252             $this->objects['user']->getId(),
253             Tinebase_Acl_Rights::RUN
254         );
255
256         $this->assertFalse($result, 'user has run right for disabled application');
257     }
258
259     /**
260      * try to delete a role
261      *
262      */
263     public function testDeleteRole()
264     {
265         $this->testSetRoleMemberships();
266         
267         // remove role members and rights first
268         Tinebase_Acl_Roles::getInstance()->setRoleRights($this->objects['role']->getId(), array());
269         
270         Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role']->getId(), array());
271         Tinebase_Acl_Roles::getInstance()->setRoleMembers($this->objects['role_2']->getId(), array());
272         
273         Tinebase_Acl_Roles::getInstance()->deleteRoles(array($this->objects['role']->getId(), $this->objects['role_2']->getId()));
274       
275         $this->setExpectedException('Tinebase_Exception_NotFound');
276         Tinebase_Acl_Roles::getInstance()->getRoleById($this->objects['role']->getId());
277     }
278 }