28 use Slim\Psr7\Factory\StreamFactory;
30 use Slim\Psr7\Headers;
31 use Slim\Psr7\Request;
33 require_once dirname(__DIR__, 4) .
'/lib/php/Plugin/FO_Plugin.php';
47 const YAML_LOC = __DIR__ .
'/../../../ui/api/documentation/openapi.yaml';
107 "options" => [
"A",
"F",
"g",
"l",
"o"],
108 "logsDate"=>
"2021-08-19",
109 "goldDate"=>
"2022-07-16"
113 "A"=>
"Run all maintenance operations.",
114 "F"=>
"Validate folder contents.",
115 "g"=>
"Remove orphaned gold files.",
116 "o"=>
"Remove older gold files from repository.",
117 "l"=>
"Remove older log files from repository."
119 $container = M::mock(
'ContainerBuilder');
120 $this->dbHelper = M::mock(DbHelper::class);
121 $this->restHelper = M::mock(RestHelper::class);
122 $this->userDao = M::mock(UserDao::class);
123 $this->auth = M::mock(Auth::class);
125 $this->maintagentPlugin = M::mock(
'maintagent');
127 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
128 $this->restHelper->shouldReceive(
'getUserDao')
129 ->andReturn($this->userDao);
131 $this->restHelper->shouldReceive(
'getPlugin')
132 ->withArgs(array(
'maintagent'))
133 ->andReturn($this->maintagentPlugin);
135 $this->auth->shouldReceive(
'isAdmin')->andReturn(
true);
137 $container->shouldReceive(
'get')->withArgs(array(
138 'helper.restHelper'))->andReturn($this->restHelper);
140 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
141 $this->
dbManager = M::mock(DbManager::class);
142 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
143 $this->streamFactory =
new StreamFactory();
155 $response->getBody()->seek(0);
156 return json_decode($response->getBody()->getContents(),
true);
166 $_SESSION[
'UserLevel'] = 10;
169 "options" => [
"A",
"F",
"g",
"l",
"o"],
170 "logsDate"=>
"2021-08-19",
171 "goldDate"=>
"2022-07-16"
175 "A"=>
"Run all maintenance operations.",
176 "F"=>
"Validate folder contents.",
177 "g"=>
"Remove orphaned gold files.",
178 "o"=>
"Remove older gold files from repository.",
179 "l"=>
"Remove older log files from repository."
182 $alteredOptions = array();
183 foreach ($rq[
'options'] as $key) {
184 $alteredOptions[$key] = $key;
187 $body[
'options'] = $alteredOptions;
189 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($OPTIONS);
191 $mess = _(
"The maintenance job has been queued");
193 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
195 $info =
new Info(201, $mess, InfoType::INFO);
198 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(), $info->getCode());
200 $reqBody = $this->streamFactory->createStream(json_encode(
204 $requestHeaders =
new Headers();
205 $requestHeaders->setHeader(
'Content-Type',
'application/json');
206 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
207 $requestHeaders, [], [], $reqBody);
210 $actualResponse = $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
212 $this->assertEquals($expectedResponse->getStatusCode(),
213 $actualResponse->getStatusCode());
225 $_SESSION[
'UserLevel'] = 0;
227 $alteredOptions = array();
228 foreach ($this->rq[
'options'] as $key) {
229 $alteredOptions[$key] = $key;
232 $body[
'options'] = $alteredOptions;
234 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
236 $mess = _(
"The maintenance job has been queued");
238 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
239 $reqBody = $this->streamFactory->createStream(json_encode(
243 $requestHeaders =
new Headers();
244 $requestHeaders->setHeader(
'Content-Type',
'application/json');
245 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
246 $requestHeaders, [], [], $reqBody);
248 $this->expectException(HttpForbiddenException::class);
249 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
259 $_SESSION[
'UserLevel'] = 10;
261 $this->rq[
"options"] = [];
263 $reqBody = $this->streamFactory->createStream(json_encode(
266 $requestHeaders =
new Headers();
267 $requestHeaders->setHeader(
'Content-Type',
'application/json');
268 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
269 $requestHeaders, [], [], $reqBody);
271 $this->expectException(HttpBadRequestException::class);
272 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
283 $_SESSION[
'UserLevel'] = 10;
285 array_push($this->rq[
"options"],
"M");
286 $alteredOptions = array();
287 foreach ($this->rq[
'options'] as $key) {
288 $alteredOptions[$key] = $key;
291 $body[
'options'] = $alteredOptions;
293 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
295 $mess = _(
"The maintenance job has been queued");
297 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
298 $reqBody = $this->streamFactory->createStream(json_encode(
302 $requestHeaders =
new Headers();
303 $requestHeaders->setHeader(
'Content-Type',
'application/json');
304 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
305 $requestHeaders, [], [], $reqBody);
307 $this->expectException(HttpNotFoundException::class);
308 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
318 $_SESSION[
'UserLevel'] = 10;
321 $req[
"goldDate"] =
"";
324 $alteredOptions = array();
325 foreach ($req[
'options'] as $key) {
326 $alteredOptions[$key] = $key;
329 $body[
'options'] = $alteredOptions;
331 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
333 $mess = _(
"The maintenance job has been queued");
335 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
336 $reqBody = $this->streamFactory->createStream(json_encode(
340 $requestHeaders =
new Headers();
341 $requestHeaders->setHeader(
'Content-Type',
'application/json');
342 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
343 $requestHeaders, [], [], $reqBody);
345 $this->expectException(HttpBadRequestException::class);
346 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
357 $_SESSION[
'UserLevel'] = 10;
360 $req[
"logsDate"] =
"";
361 $alteredOptions = array();
362 foreach ($req[
'options'] as $key) {
363 $alteredOptions[$key] = $key;
366 $body[
'options'] = $alteredOptions;
368 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
370 $mess = _(
"The maintenance job has been queued");
372 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
373 $reqBody = $this->streamFactory->createStream(json_encode(
377 $requestHeaders =
new Headers();
378 $requestHeaders->setHeader(
'Content-Type',
'application/json');
379 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
380 $requestHeaders, [], [], $reqBody);
382 $this->expectException(HttpBadRequestException::class);
383 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
Contains the constants and helpers for authentication of user.
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.
Tests for MaintenanceController.
testCreateMaintenanceWithBadRequest()
testCreateMaintenanceInvalidGoldDate()
setUp()
Setup test objects.
testCreateMaintenanceUserNotAdmin()
testCreateMaintenanceInvalidLogsDate()
getResponseJson($response)
testCreateMaintenanceOptionKeyNotFound()
fo_dbManager * dbManager
fo_dbManager object