15 require_once dirname(__DIR__, 4) .
'/lib/php/Plugin/FO_Plugin.php';
28 use Slim\Psr7\Request;
59 protected function setUp() : void
62 $container = M::mock(
'ContainerBuilder');
63 $this->dbHelper = M::mock(DbHelper::class);
64 $this->restHelper = M::mock(RestHelper::class);
65 $this->userDao = M::mock(UserDao::class);
67 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
68 $this->restHelper->shouldReceive(
'getUserDao')
69 ->andReturn($this->userDao);
71 $container->shouldReceive(
'get')->withArgs(array(
72 'helper.restHelper'))->andReturn($this->restHelper);
74 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
83 $this->addToAssertionCount(
84 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
96 $response->getBody()->seek(0);
97 return json_decode($response->getBody()->getContents(),
true);
107 $userArray = array();
108 foreach ($userIds as $userId) {
111 } elseif ($userId > 2 && $userId <= 4) {
113 } elseif ($userId == 5) {
118 $user =
new User($userId,
"user$userId",
"User $userId",
119 "user$userId@example.com", $accessLevel, 2, 4,
"");
120 $userArray[] = $user;
134 $userArray = [
'user_pk' => $userId];
136 $request = M::mock(Request::class);
137 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')
138 ->withArgs([$userName])->andReturn($userArray);
139 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
140 $this->dbHelper->shouldReceive(
'doesIdExist')
141 ->withArgs([
"users",
"user_pk", $userId])->andReturn(
true);
142 $this->dbHelper->shouldReceive(
'getUsers')->withArgs([$userId])
144 $expectedResponse = (
new ResponseHelper())->withJson($user[0]->getArray(), 200);
145 $actualResponse = $this->userController->getUsers($request,
new ResponseHelper(),
146 [
'pathParam' => $userId]);
147 $this->assertEquals($expectedResponse->getStatusCode(),
148 $actualResponse->getStatusCode());
161 $request = M::mock(Request::class);
162 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
163 $this->dbHelper->shouldReceive(
'doesIdExist')
164 ->withArgs([
"users",
"user_pk", $userId])->andReturn(
false);
165 $this->expectException(HttpNotFoundException::class);
168 [
'pathParam' => $userId]);
178 $users = $this->
getUsers([2, 3, 4]);
179 $request = M::mock(Request::class);
180 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
181 $this->dbHelper->shouldReceive(
'getUsers')->withArgs([
null])
185 foreach ($users as $user) {
186 $allUsers[] = $user->getArray();
189 $expectedResponse = (
new ResponseHelper())->withJson($allUsers, 200);
190 $actualResponse = $this->userController->getUsers($request,
new ResponseHelper(), []);
191 $this->assertEquals($expectedResponse->getStatusCode(),
192 $actualResponse->getStatusCode());
206 $userArray = [
'user_pk' => $userId];
207 $request = M::mock(Request::class);
208 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
209 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')
210 ->withArgs([$userName])->andReturn($userArray);
211 $this->dbHelper->shouldReceive(
'doesIdExist')
212 ->withArgs([
"users",
"user_pk", $userId])->andReturn(
true);
213 $this->dbHelper->shouldReceive(
'deleteUser')->withArgs([$userId]);
214 $info =
new Info(202,
"User will be deleted", InfoType::INFO);
215 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(),
217 $actualResponse = $this->userController->deleteUser($request,
new ResponseHelper(),
218 [
'pathParam' => $userId]);
219 $this->assertEquals($expectedResponse->getStatusCode(),
220 $actualResponse->getStatusCode());
234 $userArray = [
'user_pk' => $userId];
235 $request = M::mock(Request::class);
236 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
237 $this->restHelper->getUserDao()->shouldReceive(
'getUserByName')
238 ->withArgs([$userName])->andReturn($userArray);
239 $this->dbHelper->shouldReceive(
'doesIdExist')
240 ->withArgs([
"users",
"user_pk", $userId])->andReturn(
false);
241 $this->expectException(HttpNotFoundException::class);
244 [
'pathParam' => $userId]);
256 $request = M::mock(Request::class);
257 $request->shouldReceive(
'getAttribute')->andReturn(ApiVersion::V1);
258 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
259 $this->dbHelper->shouldReceive(
'getUsers')->withArgs([$userId])
261 $this->userDao->shouldReceive(
'getUserAndDefaultGroupByUserName')->withArgs([$user[0]->getArray()[
"name"]])
262 ->andReturn([
"group_name" =>
"fossy"]);
264 $expectedUser = $user[0]->getArray();
265 $expectedUser[
"default_group"] =
"fossy";
267 $expectedResponse = (
new ResponseHelper())->withJson($expectedUser, 200);
269 $actualResponse = $this->userController->getCurrentUser($request,
272 $this->assertEquals($expectedResponse->getStatusCode(),
273 $actualResponse->getStatusCode());
Controller for User 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.
Test cases for UserController.
testGetSpecificUserNotFound()
tearDown()
Remove test objects.
testDeleteUserDoesNotExists()
getResponseJson($response)
setUp()
Setup test objects.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.