14 require_once dirname(__DIR__, 4) .
'/lib/php/Plugin/FO_Plugin.php';
32 use Slim\Psr7\Factory\StreamFactory;
33 use Slim\Psr7\Headers;
34 use Slim\Psr7\Request;
49 const YAML_LOC = __DIR__ .
'/../../../ui/api/documentation/openapi.yaml';
79 protected function setUp() : void
82 $container = M::mock(
'ContainerBuilder');
83 $this->dbHelper = M::mock(DbHelper::class);
84 $this->restHelper = M::mock(RestHelper::class);
85 $this->userDao = M::mock(UserDao::class);
86 $this->adminPlugin = M::mock(
'AdminGroupUsers');
88 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
89 $this->restHelper->shouldReceive(
'getUserDao')
90 ->andReturn($this->userDao);
92 $this->restHelper->shouldReceive(
'getPlugin')
93 ->withArgs(array(
'group_manage_users'))->andReturn($this->adminPlugin);
95 $container->shouldReceive(
'get')->withArgs(array(
96 'helper.restHelper'))->andReturn($this->restHelper);
98 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
99 $this->
dbManager = M::mock(DbManager::class);
100 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
101 $this->streamFactory =
new StreamFactory();
111 $this->addToAssertionCount(
112 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
124 $response->getBody()->seek(0);
125 return json_decode($response->getBody()->getContents(),
true);
135 $groupPermissions = array(
"NONE" => -1, UserDao::USER => 0,
136 UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
138 $memberList = array();
139 foreach ($userIds as $userId) {
140 $key = array_rand($groupPermissions);
142 null,
null,
null,
null,
null),$groupPermissions[$key]) ;
143 $memberList[] = $userGroupMember->getArray();
155 $groupPermissions = array(
"NONE" => -1, UserDao::USER => 0,
156 UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
158 $usersWithGroup = array();
159 foreach ($userIds as $userId) {
160 $perm = array_rand($groupPermissions);
162 "user_pk" => $userId,
163 "group_perm"=> $perm,
164 "user_name" => $userId.
"username",
165 "user_desc" => $userId.
"desc",
166 "user_status"=>
'active'
168 $usersWithGroup[] = $user;
170 return $usersWithGroup;
182 $groupName =
'fossy';
185 $request = M::mock(Request::class);
186 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
187 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupId);
188 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
189 $this->dbHelper->shouldReceive(
'doesIdExist')
190 ->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
192 $this->userDao->shouldReceive(
'getDeletableAdminGroupMap')->withArgs([$userId,$_SESSION[
Auth::USER_LEVEL]]);
193 $this->userDao->shouldReceive(
'deleteGroup')->withArgs([$groupId]);
195 $info =
new Info(202,
"User Group will be deleted", InfoType::INFO);
196 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(),
198 $actualResponse = $this->groupController->deleteGroup($request,
new ResponseHelper(),
199 [
'pathParam' => $groupId]);
201 $this->assertEquals($expectedResponse->getStatusCode(),
202 $actualResponse->getStatusCode());
215 $groupList = array();
216 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
218 $this->userDao->shouldReceive(
'getDeletableAdminGroupMap')->withArgs([$userId,
220 $expectedResponse = (
new ResponseHelper())->withJson($groupList, 200);
221 $actualResponse = $this->groupController->getDeletableGroups(
null,
new ResponseHelper(), []);
222 $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
233 $groupName =
'fossy';
236 $request = M::mock(Request::class);
237 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
238 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupId);
239 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userIds[0]);
241 $this->userDao->shouldReceive(
'getAdminGroupMap')->withArgs([$userIds[0],$_SESSION[
Auth::USER_LEVEL]])->andReturn([1]);
243 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
244 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId)])->andReturn(1);
246 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
250 $users[] =
new User($user[
"user_pk"], $user[
"user_name"], $user[
"user_desc"],
251 null,
null,
null,
null,
null);
252 $this->dbHelper->shouldReceive(
"getUsers")->withArgs([$user[
'user_pk']])->andReturn($users);
254 $expectedResponse = (
new ResponseHelper())->withJson($memberList, 200);
256 $actualResponse = $this->groupController->getGroupMembers($request,
new ResponseHelper(), [
'pathParam' => $groupId]);
257 $this->assertEquals($expectedResponse->getStatusCode(),$actualResponse->getStatusCode());
271 $groupName =
"fossy";
275 $userArray = [
'user_pk' => $newuser];
280 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupId);
281 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userName])->andReturn($userArray);
282 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
283 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
284 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
285 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
286 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
288 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
289 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
290 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
293 $body = $this->streamFactory->createStream(json_encode([
296 $requestHeaders =
new Headers();
297 $requestHeaders->setHeader(
'Content-Type',
'application/json');
298 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
299 $requestHeaders, [], [], $body);
301 $expectedResponse =
new Info(200,
"User will be added to group.", InfoType::INFO);
303 $actualResponse = $this->groupController->addMember($request,
new ResponseHelper(), [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
304 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
305 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
316 $groupName =
"fossy";
320 $userArray = [
'user_pk' => $newuser];
325 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupId);
326 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userName])->andReturn($userArray);
327 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
328 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
329 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
330 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
false);
332 $body = $this->streamFactory->createStream(json_encode([
335 $requestHeaders =
new Headers();
336 $requestHeaders->setHeader(
'Content-Type',
'application/json');
337 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
338 $requestHeaders, [], [], $body);
340 $this->expectException(HttpForbiddenException::class);
343 [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
354 $groupName =
"fossy";
358 $userArray = [
'user_pk' => $newuser];
364 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupId);
365 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userName])->andReturn($userArray);
366 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
367 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
368 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
369 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
370 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
372 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
373 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
374 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
376 $body = $this->streamFactory->createStream(json_encode([
379 $requestHeaders =
new Headers();
380 $requestHeaders->setHeader(
'Content-Type',
'application/json');
381 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
382 $requestHeaders, [], [], $body);
384 $expectedResponse =
new Info(200,
"User will be added to group.", InfoType::INFO);
386 $actualResponse = $this->groupController->addMember($request,
new ResponseHelper(), [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
387 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
388 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
402 $groupName =
"fossy";
406 $userArray = [
'user_pk' => $newuser];
411 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupId);
412 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userName])->andReturn($userArray);
413 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
414 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
415 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn(
true);
416 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
417 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
419 $body = $this->streamFactory->createStream(json_encode([
422 $requestHeaders =
new Headers();
423 $requestHeaders->setHeader(
'Content-Type',
'application/json');
424 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
425 $requestHeaders, [], [], $body);
427 $this->expectException(HttpBadRequestException::class);
430 [
'pathParam' => $groupId,
'userPathParam' => $newuser]);
439 $groupIds = [1,2,3,4,5,6];
440 $groupName =
"fossy";
443 $group_user_member_pk = 1;
446 $userArray = [
'user_pk' => $userId];
449 $this->restHelper->getUserDao()->shouldReceive(
'getGroupIdByName')->withArgs([$groupName])->andReturn($groupIds[0]);
450 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')->withArgs([$userName])->andReturn($userArray);
451 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupIds[0]])->andReturn(
true);
452 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$userId])->andReturn(
true);
453 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn([
'group_pk'=>$groupIds[0],
'group_user_member_pk'=>$group_user_member_pk,
'permission'=>$newPerm]);
454 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userPk);
455 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userPk, $groupIds[0]])->andReturn(
true);
457 $this->adminPlugin->shouldReceive(
'updateGUMPermission')->withArgs([$group_user_member_pk,$newPerm, $this->
dbManager ]);
459 $body = $this->streamFactory->createStream(json_encode([
462 $requestHeaders =
new Headers();
463 $requestHeaders->setHeader(
'Content-Type',
'application/json');
464 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
465 $requestHeaders, [], [], $body);
468 $expectedResponse =
new Info(202,
"Permission updated successfully.", InfoType::INFO);
470 $actualResponse = $this->groupController->changeUserPermission($request,
new ResponseHelper(), [
'pathParam' => $groupIds[0],
'userPathParam' => $userId]);
471 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
472 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
Contains the constants and helpers for authentication of user.
Controller for Group model.
Provides helper methods to access database for REST api.
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Different type of infos provided by REST.
Info model to contain general error and return values.
Model to hold user information.
Tests for GroupController.
setUp()
Setup test objects.
tearDown()
Remove test objects.
testAddMemberUserNotMember()
getResponseJson($response)
testAddMemberUserGroupAdmin()
testAddMemberUserAlreadyMember()
getUsersWithGroup($userIds)
getGroupMembers($userIds)
testAddMemberUserNotAdmin()
testChangeUserPermission()
fo_dbManager * dbManager
fo_dbManager object