1<?php 2 3/* 4 * To change this template, choose Tools | Templates 5 * and open the template in the editor. 6 */ 7 8/** 9 * Description of AccessFlowStateMachineServiceTest 10 * 11 * @group Core 12 */ 13class AccessFlowStateMachineServiceTest extends PHPUnit_Framework_TestCase { 14 15 private $accessFlowStateMachineService; 16 17 protected function setUp() { 18 19 $this->accessFlowStateMachineService = new AccessFlowStateMachineService(); 20 21 $this->fixture = sfConfig::get('sf_plugins_dir') . '/orangehrmCorePlugin/test/fixtures/AccessFlowStateMachineService.yml'; 22 23 TestDataService::populate($this->fixture); 24 } 25 26 public function testGetAccessFlowStateMachineDao(){ 27 28 $accessFlowStateMachineDao= $this->accessFlowStateMachineService->getAccessFlowStateMachineDao(); 29 30 $this->assertTrue($accessFlowStateMachineDao instanceof AccessFlowStateMachineDao); 31 32 33 } 34 35 public function testSetAccessFlowStateMachineDao(){ 36 37 $accessFlowStateMachineDao= new AccessFlowStateMachineDao(); 38 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($accessFlowStateMachineDao); 39 40 $this->assertTrue($this->accessFlowStateMachineService->getAccessFlowStateMachineDao() instanceof AccessFlowStateMachineDao); 41 42 43 } 44 public function testGetAllowedActions() { 45 $flow = "Time"; 46 $state = "SUBMITTED"; 47 $role = "ESS USER"; 48 $fetchedRecord1 = TestDataService::fetchObject('WorkflowStateMachine', 10); 49 $fetchedRecord2 = TestDataService::fetchObject('WorkflowStateMachine', 12); 50 $recordsArray = array($fetchedRecord1, $fetchedRecord2); 51 52 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 53 ->setMethods( array('getAllowedActions')) 54 ->getMock(); 55 $acessFlowStateMachineDaoMock->expects($this->once()) 56 ->method('getAllowedActions') 57 ->with($flow, $state, $role) 58 ->will($this->returnValue($recordsArray)); 59 60 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 61 $retrievedActionsArray = $this->accessFlowStateMachineService->getAllowedActions($flow, $state, $role); 62 63 $this->assertEquals($retrievedActionsArray[0], $recordsArray[0]->getAction()); 64 $this->assertEquals($retrievedActionsArray[1], $recordsArray[1]->getAction()); 65 66 $flow = "Attendance"; 67 $state = "INITIAL"; 68 $role = "ADMIN"; 69 $recordsArray = null; 70 71 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 72 ->setMethods( array('getAllowedActions')) 73 ->getMock(); 74 $acessFlowStateMachineDaoMock->expects($this->once()) 75 ->method('getAllowedActions') 76 ->with($flow, $state, $role) 77 ->will($this->returnValue($recordsArray)); 78 79 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 80 $retrievedActionsArray = $this->accessFlowStateMachineService->getAllowedActions($flow, $state, $role); 81 82 $this->assertNull($retrievedActionsArray); 83 } 84 85 public function testGetAllowedWorkflowItems() { 86 $flow = "Time"; 87 $state = "SUBMITTED"; 88 $role = "ESS USER"; 89 $fetchedRecord1 = TestDataService::fetchObject('WorkflowStateMachine', 10); 90 $fetchedRecord2 = TestDataService::fetchObject('WorkflowStateMachine', 12); 91 $expected = Doctrine_Collection::create('WorkflowStateMachine'); 92 $expected->add($fetchedRecord1); 93 $expected->add($fetchedRecord2); 94 95 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 96 ->setMethods( array('getAllowedWorkflowItems')) 97 ->getMock(); 98 $acessFlowStateMachineDaoMock->expects($this->once()) 99 ->method('getAllowedWorkflowItems') 100 ->with($flow, $state, $role) 101 ->will($this->returnValue($expected)); 102 103 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 104 $results = $this->accessFlowStateMachineService->getAllowedWorkflowItems($flow, $state, $role); 105 $this->assertEquals($expected, $results); 106 } 107 108 public function testGetNextState() { 109 110 $flow = "Time"; 111 $state = "SUBMITTED"; 112 $role = "ADMIN"; 113 $action = "APPROVE"; 114 115 $fetchedRecord1 = TestDataService::fetchObject('WorkflowStateMachine', 1); 116 117 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 118 ->setMethods( array('getNextState')) 119 ->getMock(); 120 $acessFlowStateMachineDaoMock->expects($this->once()) 121 ->method('getNextState') 122 ->with($flow, $state, $role, $action) 123 ->will($this->returnValue($fetchedRecord1)); 124 125 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 126 $retrievedState = $this->accessFlowStateMachineService->getNextState($flow, $state, $role, $action); 127 128 $this->assertEquals($retrievedState, $fetchedRecord1->getResultingState()); 129 130 //checking the null case 131 132 $flow = "Attendace"; 133 $state = "SUBMITTED"; 134 $role = "ADMIN"; 135 $action = "APPROVE"; 136 137 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 138 ->setMethods( array('getNextState')) 139 ->getMock(); 140 $acessFlowStateMachineDaoMock->expects($this->once()) 141 ->method('getNextState') 142 ->with($flow, $state, $role, $action) 143 ->will($this->returnValue(null)); 144 145 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 146 $retrievedState = $this->accessFlowStateMachineService->getNextState($flow, $state, $role, $action); 147 148 $this->assertNull($retrievedState); 149 } 150 151 public function testGetActionableStates() { 152 153 $actions = array("APPROVE", "REJECT"); 154 $workFlow = "Time"; 155 $userRole = "ADMIN"; 156 157 $fetchedRecord1 = TestDataService::fetchObject('WorkflowStateMachine', 1); 158 $fetchedRecord2 = TestDataService::fetchObject('WorkflowStateMachine', 5); 159 $tempArray = array($fetchedRecord1, $fetchedRecord2); 160 161 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 162 ->setMethods( array('getActionableStates')) 163 ->getMock(); 164 $acessFlowStateMachineDaoMock->expects($this->once()) 165 ->method('getActionableStates') 166 ->with($workFlow, $userRole, $actions) 167 ->will($this->returnValue($tempArray)); 168 169 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 170 $record = $this->accessFlowStateMachineService->getActionableStates($workFlow, $userRole, $actions); 171 172 $this->assertEquals(2, count($record)); 173 $this->assertEquals($fetchedRecord1->getState(), $record[0]); 174 $this->assertEquals($fetchedRecord2->getState(), $record[1]); 175 } 176 177 public function testSaveWorkflowStateMachineRecord() { 178 179 180 $workflowStateMachineRecords = TestDataService::loadObjectList('WorkflowStateMachine', $this->fixture, 'WorkflowStateMachine'); 181 182 $workflowStateMachineRecord = $workflowStateMachineRecords[0]; 183 184 $accessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 185 ->setMethods( array('saveWorkflowStateMachineRecord')) 186 ->getMock(); 187 188 $accessFlowStateMachineDaoMock->expects($this->once()) 189 ->method('saveWorkflowStateMachineRecord') 190 ->with($workflowStateMachineRecord) 191 ->will($this->returnValue($workflowStateMachineRecord)); 192 193 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($accessFlowStateMachineDaoMock); 194 195 $this->assertTrue($this->accessFlowStateMachineService->saveWorkflowStateMachineRecord($workflowStateMachineRecord) instanceof WorkflowStateMachine); 196 } 197 198 public function testDeleteWorkflowStateMachineRecord() { 199 $flow = "Time"; 200 $state = "SUPERVISOR APPROVED"; 201 $role = "ADMIN"; 202 $action = "VIEW TIMESHEET"; 203 $resultingState = "SUPERVISOR APPROVED"; 204 $isSuccess = true; 205 206 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 207 ->setMethods( array('deleteWorkflowStateMachineRecord')) 208 ->getMock(); 209 $acessFlowStateMachineDaoMock->expects($this->once()) 210 ->method('deleteWorkflowStateMachineRecord') 211 ->with($flow, $state, $role, $action, $resultingState) 212 ->will($this->returnValue($isSuccess)); 213 214 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 215 $retunedValue = $this->accessFlowStateMachineService->deleteWorkflowStateMachineRecord($flow, $state, $role, $action, $resultingState); 216 217 $this->assertEquals($isSuccess, $retunedValue); 218 219 $flow = "Time"; 220 $state = "SUPERVISOR APPROVED"; 221 $role = "ADMIN"; 222 $action = "VIEW TIMESHEET"; 223 $resultingState = "SUPERVISOR APPROVED"; 224 $isSuccess = false; 225 226 $acessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 227 ->setMethods( array('deleteWorkflowStateMachineRecord')) 228 ->getMock(); 229 $acessFlowStateMachineDaoMock->expects($this->once()) 230 ->method('deleteWorkflowStateMachineRecord') 231 ->with($flow, $state, $role, $action, $resultingState) 232 ->will($this->returnValue($isSuccess)); 233 234 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($acessFlowStateMachineDaoMock); 235 $retunedValue = $this->accessFlowStateMachineService->deleteWorkflowStateMachineRecord($flow, $state, $role, $action, $resultingState); 236 237 $this->assertEquals($isSuccess, $retunedValue); 238 } 239 240 public function testGetWorkFlowStateMachineRecordsService() { 241 $workflowStateMachineRecords = TestDataService::loadObjectList('WorkflowStateMachine', $this->fixture, 'WorkflowStateMachine'); 242 243 $workflowStateMachineRecord = $workflowStateMachineRecords[12]; 244 245 $accessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao')->getMock(); 246 247 $accessFlowStateMachineDaoMock->expects($this->once()) 248 ->method('getWorkFlowStateMachineRecords') 249 ->with(PluginWorkflowStateMachine::FLOW_EMPLOYEE) 250 ->will($this->returnValue($workflowStateMachineRecord)); 251 252 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($accessFlowStateMachineDaoMock); 253 254 $this->assertTrue($this->accessFlowStateMachineService->getWorkFlowStateMachineRecords(PluginWorkflowStateMachine::FLOW_EMPLOYEE, null) instanceof WorkflowStateMachine); 255 } 256 257 public function testIsActionAllowedService() { 258 $accessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao')->getMock(); 259 260 $accessFlowStateMachineDaoMock->expects($this->once()) 261 ->method('isActionAllowed') 262 ->with(PluginWorkflowStateMachine::FLOW_EMPLOYEE, 'NOT EXIST', 'ADMIN', 263 PluginWorkflowStateMachine::EMPLOYEE_ACTION_ADD) 264 ->will($this->returnValue(TRUE)); 265 266 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($accessFlowStateMachineDaoMock); 267 268 $isAllowed = $this->accessFlowStateMachineService->isActionAllowed(PluginWorkflowStateMachine::FLOW_EMPLOYEE, 269 'NOT EXIST', 'ADMIN', PluginWorkflowStateMachine::EMPLOYEE_ACTION_ADD); 270 $this->assertTrue($isAllowed); 271 } 272 273 public function testGetWorkflowItemsByStateActionAndRole() { 274 $item = new WorkflowStateMachine(); 275 $item->fromArray(array('id' => 9, 'workflow' => 'Time', 'state' => 'APPROVED', 'role' => 'SUPERVISOR', 276 'action' => 'VIEW TIMESHEET','resulting_state' => 'APPROVED')); 277 $accessFlowStateMachineDaoMock = $this->getMockBuilder('AccessFlowStateMachineDao') 278 ->setMethods( array('getWorkflowItemByStateActionAndRole')) 279 ->getMock(); 280 281 $accessFlowStateMachineDaoMock->expects($this->once()) 282 ->method('getWorkflowItemByStateActionAndRole') 283 ->with('Time', 'NOT SUBMITTED', 'SAVE', 'XYZ') 284 ->will($this->returnValue($item)); 285 286 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($accessFlowStateMachineDaoMock); 287 288 $result = $this->accessFlowStateMachineService->getWorkflowItemByStateActionAndRole('Time', 'NOT SUBMITTED', 'SAVE', 'XYZ'); 289 $this->assertEquals($item, $result); 290 } 291 292 public function testDeleteWorkflowRecordsForUserRole() { 293 $flow = "Time"; 294 $role = "ADMIN"; 295 296 $mockDao = $this->getMockBuilder('AccessFlowStateMachineDao') 297 ->setMethods( array('deleteWorkflowRecordsForUserRole')) 298 ->getMock(); 299 $mockDao->expects($this->once()) 300 ->method('deleteWorkflowRecordsForUserRole') 301 ->with($flow, $role) 302 ->will($this->returnValue(true)); 303 304 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($mockDao); 305 $returnedValue = $this->accessFlowStateMachineService->deleteWorkflowRecordsForUserRole($flow, $role); 306 307 $this->assertTrue($returnedValue); 308 } 309 310 public function testHandleUserRoleRename() { 311 $oldName = "ADMIN"; 312 $newName = "MANAGER"; 313 314 $mockDao = $this->getMockBuilder('AccessFlowStateMachineDao') 315 ->setMethods( array('handleUserRoleRename')) 316 ->getMock(); 317 $mockDao->expects($this->once()) 318 ->method('handleUserRoleRename') 319 ->with($oldName, $newName) 320 ->will($this->returnValue(true)); 321 322 $this->accessFlowStateMachineService->setAccessFlowStateMachineDao($mockDao); 323 $returnedValue = $this->accessFlowStateMachineService->handleUserRoleRename($oldName, $newName); 324 325 $this->assertTrue($returnedValue); 326 } 327 328} 329 330