1<?php 2 3/** 4 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures 5 * all the essential functionalities required for any enterprise. 6 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com 7 * 8 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of 9 * the GNU General Public License as published by the Free Software Foundation; either 10 * version 2 of the License, or (at your option) any later version. 11 * 12 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 14 * See the GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License along with this program; 17 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA 19 */ 20 21/** 22 * Description of ScreenPermissionServiceTest 23 * @group Core 24 */ 25class ScreenPermissionServiceTest extends PHPUnit_Framework_TestCase { 26 27 /** @property ScreenPermissionService $service */ 28 private $service; 29 30 /** 31 * Set up method 32 */ 33 protected function setUp() { 34 $this->service = new ScreenPermissionService(); 35 } 36 37 /** 38 * Test case for when no permissions are defined for given user role(s). 39 * Behavior is to allow access if the screen is not defined, unless prohibited through a rule in the database. 40 * This allows to progressively update the rules in code. 41 */ 42 public function testGetScreenPermissionsNoneWithNoScreen() { 43 $module = 'xim'; 44 $action = 'doThis'; 45 $roles = ''; 46 47 $permissionDao = $this->getMockBuilder('ScreenPermissionDao') 48 ->setMethods( array('getScreenPermissions')) 49 ->getMock(); 50 $emptyDoctrineCollection = new Doctrine_Collection('ScreenPermission'); 51 52 $permissionDao->expects($this->once()) 53 ->method('getScreenPermissions') 54 ->with($module, $action, $roles) 55 ->will($this->returnValue($emptyDoctrineCollection)); 56 57 $this->service->setScreenPermissionDao($permissionDao); 58 59 $screenDao = $this->getMockBuilder('ScreenDao') 60 ->setMethods( array('getScreen')) 61 ->getMock(); 62 $screenDao->expects($this->once()) 63 ->method('getScreen') 64 ->with($module, $action) 65 ->will($this->returnValue(false)); 66 67 $this->service->setScreenDao($screenDao); 68 69 $permissions = $this->service->getScreenPermissions($module, $action, $roles); 70 71 $this->assertTrue($permissions instanceof ResourcePermission); 72 $this->verifyPermissions($permissions, true, true, true, true); 73 74 } 75 76 public function testGetScreenPermissionsNoneWithScreenDefined() { 77 $module = 'xim'; 78 $action = 'doThis'; 79 $roles = ''; 80 81 $mockDao = $this->getMockBuilder('ScreenPermissionDao') 82 ->setMethods( array('getScreenPermissions')) 83 ->getMock(); 84 $emptyDoctrineCollection = new Doctrine_Collection('ScreenPermission'); 85 86 $mockDao->expects($this->once()) 87 ->method('getScreenPermissions') 88 ->with($module, $action, $roles) 89 ->will($this->returnValue($emptyDoctrineCollection)); 90 91 $this->service->setScreenPermissionDao($mockDao); 92 93 $screen = new Screen(); 94 $screen->setName('abc'); 95 96 $screenDao = $this->getMockBuilder('ScreenDao') 97 ->setMethods( array('getScreen')) 98 ->getMock(); 99 $screenDao->expects($this->once()) 100 ->method('getScreen') 101 ->with($module, $action) 102 ->will($this->returnValue($screen)); 103 104 $this->service->setScreenDao($screenDao); 105 106 $permissions = $this->service->getScreenPermissions($module, $action, $roles); 107 108 $this->assertTrue($permissions instanceof ResourcePermission); 109 $this->verifyPermissions($permissions, false, false, false, false); 110 111 } 112 113 public function testGetScreenPermissionsOne() { 114 $module = 'xim'; 115 $action = 'doThis'; 116 $roles = array('Admin'); 117 118 119 $doctrineCollection = new Doctrine_Collection('ScreenPermission'); 120 $screenPermission1 = new ScreenPermission(); 121 $screenPermission1->fromArray(array('id' => 1, 'user_role_id' => 1, 'screen_id' => 1, 122 'can_read' => 1, 'can_create' => 0, 123 'can_update'=> 0, 'can_delete'=> 1)); 124 $screenPermission2 = new ScreenPermission(); 125 $screenPermission2->fromArray(array('id' => 1, 'user_role_id' => 1, 'screen_id' => 1, 126 'can_read' => 0, 'can_create' => 1, 127 'can_update'=> 0, 'can_delete'=> 1)); 128 129 $screenPermissions = array($screenPermission1, $screenPermission2); 130 $doctrineCollection->setData($screenPermissions); 131 132 $mockDao = $this->getMockBuilder('ScreenPermissionDao') 133 ->setMethods( array('getScreenPermissions')) 134 ->getMock(); 135 $mockDao->expects($this->once()) 136 ->method('getScreenPermissions') 137 ->with($module, $action, $roles) 138 ->will($this->returnValue($doctrineCollection)); 139 140 $this->service->setScreenPermissionDao($mockDao); 141 142 $permissions = $this->service->getScreenPermissions($module, $action, $roles); 143 144 $this->assertTrue($permissions instanceof ResourcePermission); 145 $this->verifyPermissions($permissions, true, true, false, true); 146 } 147 148 public function testGetScreenPermissionsTwo() { 149 $module = 'xim'; 150 $action = 'doThis'; 151 $roles = array('Admin', 'ESS'); 152 153 154 $doctrineCollection = new Doctrine_Collection('ScreenPermission'); 155 $screenPermission1 = new ScreenPermission(); 156 $screenPermission1->fromArray(array('id' => 1, 'user_role_id' => 1, 'screen_id' => 1, 157 'can_read' => 1, 'can_create' => 0, 158 'can_update'=> 0, 'can_delete'=> 1)); 159 $screenPermission2 = new ScreenPermission(); 160 $screenPermission2->fromArray(array('id' => 1, 'user_role_id' => 1, 'screen_id' => 1, 161 'can_read' => 0, 'can_create' => 1, 162 'can_update'=> 0, 'can_delete'=> 1)); 163 164 $screenPermissions = array($screenPermission1, $screenPermission2); 165 $doctrineCollection->setData($screenPermissions); 166 167 $mockDao = $this->getMockBuilder('ScreenPermissionDao') 168 ->setMethods( array('getScreenPermissions')) 169 ->getMock(); 170 $mockDao->expects($this->once()) 171 ->method('getScreenPermissions') 172 ->with($module, $action, $roles) 173 ->will($this->returnValue($doctrineCollection)); 174 175 $this->service->setScreenPermissionDao($mockDao); 176 177 $permissions = $this->service->getScreenPermissions($module, $action, $roles); 178 179 $this->assertTrue($permissions instanceof ResourcePermission); 180 $this->verifyPermissions($permissions, true, true, false, true); 181 } 182 183 public function testGetScreenPermissionsMany() { 184 $module = 'xim'; 185 $action = 'doThis'; 186 $roles = array('Admin', 'ESS', 'Supervisor'); 187 188 189 $doctrineCollection = new Doctrine_Collection('ScreenPermission'); 190 $screenPermission1 = new ScreenPermission(); 191 $screenPermission1->fromArray(array('id' => 1, 'user_role_id' => 1, 'screen_id' => 1, 192 'can_read' => 0, 'can_create' => 0, 193 'can_update'=> 0, 'can_delete'=> 0)); 194 $screenPermission2 = new ScreenPermission(); 195 $screenPermission2->fromArray(array('id' => 1, 'user_role_id' => 1, 'screen_id' => 1, 196 'can_read' => 0, 'can_create' => 1, 197 'can_update'=> 0, 'can_delete'=> 0)); 198 199 $screenPermission3 = new ScreenPermission(); 200 $screenPermission3->fromArray(array('id' => 1, 'user_role_id' => 1, 'screen_id' => 1, 201 'can_read' => 0, 'can_create' => 1, 202 'can_update'=> 0, 'can_delete'=> 1)); 203 204 $screenPermissions = array($screenPermission1, $screenPermission2, $screenPermission3); 205 $doctrineCollection->setData($screenPermissions); 206 207 $mockDao = $this->getMockBuilder('ScreenPermissionDao') 208 ->setMethods( array('getScreenPermissions')) 209 ->getMock(); 210 $mockDao->expects($this->once()) 211 ->method('getScreenPermissions') 212 ->with($module, $action, $roles) 213 ->will($this->returnValue($doctrineCollection)); 214 215 $this->service->setScreenPermissionDao($mockDao); 216 217 $permissions = $this->service->getScreenPermissions($module, $action, $roles); 218 219 $this->assertTrue($permissions instanceof ResourcePermission); 220 $this->verifyPermissions($permissions, false, true, false, true); 221 } 222 223 public function testGetScreen() { 224 225 $module = 'xim'; 226 $action = 'doThis'; 227 $expected = new Screen(); 228 $expected->setId(2); 229 $expected->setName('test'); 230 $expected->setModuleId(33); 231 $expected->setActionUrl($action); 232 233 234 $screenDao = $this->getMockBuilder('ScreenDao') 235 ->setMethods( array('getScreen')) 236 ->getMock(); 237 $screenDao->expects($this->once()) 238 ->method('getScreen') 239 ->with($module, $action) 240 ->will($this->returnValue($expected)); 241 242 $this->service->setScreenDao($screenDao); 243 244 $result = $this->service->getScreen($module, $action); 245 $this->assertEquals($expected, $result); 246 } 247 248 protected function verifyPermissions(ResourcePermission $permission, $read, $create, $update, $delete) { 249 $this->assertEquals($read, $permission->canRead()); 250 $this->assertEquals($create, $permission->canCreate()); 251 $this->assertEquals($update, $permission->canUpdate()); 252 $this->assertEquals($delete, $permission->canDelete()); 253 } 254} 255 256