14 require_once dirname(__DIR__, 4) .
'/lib/php/Plugin/FO_Plugin.php';
31 use Slim\Psr7\Factory\StreamFactory;
32 use Slim\Psr7\Headers;
33 use Slim\Psr7\Request;
48 const YAML_LOC = __DIR__ .
'/../../../ui/api/documentation/openapi.yaml';
78 protected function setUp() : void
81 $container = M::mock(
'ContainerBuilder');
82 $this->dbHelper = M::mock(DbHelper::class);
83 $this->restHelper = M::mock(RestHelper::class);
84 $this->userDao = M::mock(UserDao::class);
85 $this->adminPlugin = M::mock(
'AdminGroupUsers');
87 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
88 $this->restHelper->shouldReceive(
'getUserDao')
89 ->andReturn($this->userDao);
91 $this->restHelper->shouldReceive(
'getPlugin')
92 ->withArgs(array(
'group_manage_users'))->andReturn($this->adminPlugin);
94 $container->shouldReceive(
'get')->withArgs(array(
95 'helper.restHelper'))->andReturn($this->restHelper);
97 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
98 $this->
dbManager = M::mock(DbManager::class);
99 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
100 $this->streamFactory =
new StreamFactory();
110 $this->addToAssertionCount(
111 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
123 $response->getBody()->seek(0);
124 return json_decode($response->getBody()->getContents(),
true);
134 $groupPermissions = array(
"NONE" => -1, UserDao::USER => 0,
135 UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
137 $memberList = array();
138 foreach ($userIds as $userId) {
139 $key = array_rand($groupPermissions);
141 null,
null,
null,
null,
null),$groupPermissions[$key]) ;
142 $memberList[] = $userGroupMember->getArray();
154 $groupPermissions = array(
"NONE" => -1, UserDao::USER => 0,
155 UserDao::ADMIN => 1, UserDao::ADVISOR => 2);
157 $usersWithGroup = array();
158 foreach ($userIds as $userId) {
159 $perm = array_rand($groupPermissions);
161 "user_pk" => $userId,
162 "group_perm"=> $perm,
163 "user_name" => $userId.
"username",
164 "user_desc" => $userId.
"desc",
165 "user_status"=>
'active'
167 $usersWithGroup[] = $user;
169 return $usersWithGroup;
183 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
184 $this->dbHelper->shouldReceive(
'doesIdExist')
185 ->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
187 $this->userDao->shouldReceive(
'getDeletableAdminGroupMap')->withArgs([$userId,$_SESSION[
Auth::USER_LEVEL]]);
188 $this->userDao->shouldReceive(
'deleteGroup')->withArgs([$groupId]);
190 $info =
new Info(202,
"User Group will be deleted", InfoType::INFO);
191 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(),
193 $actualResponse = $this->groupController->deleteGroup(
null,
new ResponseHelper(),
196 $this->assertEquals($expectedResponse->getStatusCode(),
197 $actualResponse->getStatusCode());
210 $groupList = array();
211 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
213 $this->userDao->shouldReceive(
'getDeletableAdminGroupMap')->withArgs([$userId,
215 $expectedResponse = (
new ResponseHelper())->withJson($groupList, 200);
216 $actualResponse = $this->groupController->getDeletableGroups(
null,
new ResponseHelper(), []);
217 $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
230 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userIds[0]);
232 $this->userDao->shouldReceive(
'getAdminGroupMap')->withArgs([$userIds[0],$_SESSION[
Auth::USER_LEVEL]])->andReturn([1]);
234 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
235 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId)])->andReturn(1);
237 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
241 $users[] =
new User($user[
"user_pk"], $user[
"user_name"], $user[
"user_desc"],
242 null,
null,
null,
null,
null);
243 $this->dbHelper->shouldReceive(
"getUsers")->withArgs([$user[
'user_pk']])->andReturn($users);
245 $expectedResponse = (
new ResponseHelper())->withJson($memberList, 200);
247 $actualResponse = $this->groupController->getGroupMembers(
null,
new ResponseHelper(), [
'id' => $groupId]);
248 $this->assertEquals($expectedResponse->getStatusCode(),$actualResponse->getStatusCode());
269 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
270 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
271 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
272 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
273 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
275 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
276 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
277 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
280 $body = $this->streamFactory->createStream(json_encode([
283 $requestHeaders =
new Headers();
284 $requestHeaders->setHeader(
'Content-Type',
'application/json');
285 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
286 $requestHeaders, [], [], $body);
288 $expectedResponse =
new Info(200,
"User will be added to group.", InfoType::INFO);
290 $actualResponse = $this->groupController->addMember($request,
new ResponseHelper(), [
'id' => $groupId,
'userId' => $newuser]);
291 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
292 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
309 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
310 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
311 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
312 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
false);
314 $body = $this->streamFactory->createStream(json_encode([
317 $requestHeaders =
new Headers();
318 $requestHeaders->setHeader(
'Content-Type',
'application/json');
319 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
320 $requestHeaders, [], [], $body);
322 $this->expectException(HttpForbiddenException::class);
325 [
'id' => $groupId,
'userId' => $newuser]);
343 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
344 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
345 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn($emptyArr);
346 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
347 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
349 $this->
dbManager->shouldReceive(
'prepare')->withArgs([M::any(),M::any()]);
350 $this->
dbManager->shouldReceive(
'execute')->withArgs([M::any(),array($groupId, $newuser,$newPerm)])->andReturn(1);
351 $this->
dbManager->shouldReceive(
'freeResult')->withArgs([1]);
353 $body = $this->streamFactory->createStream(json_encode([
356 $requestHeaders =
new Headers();
357 $requestHeaders->setHeader(
'Content-Type',
'application/json');
358 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
359 $requestHeaders, [], [], $body);
361 $expectedResponse =
new Info(200,
"User will be added to group.", InfoType::INFO);
363 $actualResponse = $this->groupController->addMember($request,
new ResponseHelper(), [
'id' => $groupId,
'userId' => $newuser]);
364 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
365 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
385 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupId])->andReturn(
true);
386 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$newuser])->andReturn(
true);
387 $this->
dbManager->shouldReceive(
'getSingleRow')->withArgs([M::any(),M::any(),M::any()])->andReturn(
true);
388 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
389 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userId, $groupId])->andReturn(
true);
391 $body = $this->streamFactory->createStream(json_encode([
394 $requestHeaders =
new Headers();
395 $requestHeaders->setHeader(
'Content-Type',
'application/json');
396 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
397 $requestHeaders, [], [], $body);
399 $this->expectException(HttpBadRequestException::class);
402 [
'id' => $groupId,
'userId' => $newuser]);
411 $groupIds = [1,2,3,4,5,6];
413 $group_user_member_pk = 1;
418 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"groups",
"group_pk", $groupIds[0]])->andReturn(
true);
419 $this->dbHelper->shouldReceive(
'doesIdExist')->withArgs([
"users",
"user_pk",$userId])->andReturn(
true);
420 $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]);
421 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userPk);
422 $this->userDao->shouldReceive(
'isAdvisorOrAdmin')->withArgs([$userPk, $groupIds[0]])->andReturn(
true);
424 $this->adminPlugin->shouldReceive(
'updateGUMPermission')->withArgs([$group_user_member_pk,$newPerm, $this->
dbManager ]);
426 $body = $this->streamFactory->createStream(json_encode([
429 $requestHeaders =
new Headers();
430 $requestHeaders->setHeader(
'Content-Type',
'application/json');
431 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
432 $requestHeaders, [], [], $body);
435 $expectedResponse =
new Info(202,
"Permission updated successfully.", InfoType::INFO);
437 $actualResponse = $this->groupController->changeUserPermission($request,
new ResponseHelper(), [
'id' => $groupIds[0],
'userId' => $userId]);
438 $this->assertEquals($expectedResponse->getCode(),$actualResponse->getStatusCode());
439 $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