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';
42 public $streamFactory;
49 const YAML_LOC = __DIR__ .
'/../../../ui/api/documentation/openapi.yaml';
104 "options" => [
"A",
"F",
"g",
"l",
"o"],
105 "logsDate"=>
"2021-08-19",
106 "goldDate"=>
"2022-07-16"
110 "A"=>
"Run all maintenance operations.",
111 "F"=>
"Validate folder contents.",
112 "g"=>
"Remove orphaned gold files.",
113 "o"=>
"Remove older gold files from repository.",
114 "l"=>
"Remove older log files from repository."
116 $container = M::mock(
'ContainerBuilder');
117 $this->dbHelper = M::mock(DbHelper::class);
118 $this->restHelper = M::mock(RestHelper::class);
119 $this->userDao = M::mock(UserDao::class);
120 $this->auth = M::mock(Auth::class);
122 $this->maintagentPlugin = M::mock(
'maintagent');
124 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
125 $this->restHelper->shouldReceive(
'getUserDao')
126 ->andReturn($this->userDao);
128 $this->restHelper->shouldReceive(
'getPlugin')
129 ->withArgs(array(
'maintagent'))
130 ->andReturn($this->maintagentPlugin);
132 $this->auth->shouldReceive(
'isAdmin')->andReturn(
true);
134 $container->shouldReceive(
'get')->withArgs(array(
135 'helper.restHelper'))->andReturn($this->restHelper);
137 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
138 $this->
dbManager = M::mock(DbManager::class);
139 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
140 $this->streamFactory =
new StreamFactory();
152 $response->getBody()->seek(0);
153 return json_decode($response->getBody()->getContents(),
true);
163 $_SESSION[
'UserLevel'] = 10;
166 "options" => [
"A",
"F",
"g",
"l",
"o"],
167 "logsDate"=>
"2021-08-19",
168 "goldDate"=>
"2022-07-16"
172 "A"=>
"Run all maintenance operations.",
173 "F"=>
"Validate folder contents.",
174 "g"=>
"Remove orphaned gold files.",
175 "o"=>
"Remove older gold files from repository.",
176 "l"=>
"Remove older log files from repository."
179 $alteredOptions = array();
180 foreach ($rq[
'options'] as $key) {
181 $alteredOptions[$key] = $key;
184 $body[
'options'] = $alteredOptions;
186 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($OPTIONS);
188 $mess = _(
"The maintenance job has been queued");
190 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
192 $info =
new Info(201, $mess, InfoType::INFO);
195 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(), $info->getCode());
197 $reqBody = $this->streamFactory->createStream(json_encode(
201 $requestHeaders =
new Headers();
202 $requestHeaders->setHeader(
'Content-Type',
'application/json');
203 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
204 $requestHeaders, [], [], $reqBody);
207 $actualResponse = $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
209 $this->assertEquals($expectedResponse->getStatusCode(),
210 $actualResponse->getStatusCode());
222 $_SESSION[
'UserLevel'] = 0;
224 $alteredOptions = array();
225 foreach ($this->rq[
'options'] as $key) {
226 $alteredOptions[$key] = $key;
229 $body[
'options'] = $alteredOptions;
231 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
233 $mess = _(
"The maintenance job has been queued");
235 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
236 $reqBody = $this->streamFactory->createStream(json_encode(
240 $requestHeaders =
new Headers();
241 $requestHeaders->setHeader(
'Content-Type',
'application/json');
242 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
243 $requestHeaders, [], [], $reqBody);
245 $this->expectException(HttpForbiddenException::class);
246 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
256 $_SESSION[
'UserLevel'] = 10;
258 $this->rq[
"options"] = [];
260 $reqBody = $this->streamFactory->createStream(json_encode(
263 $requestHeaders =
new Headers();
264 $requestHeaders->setHeader(
'Content-Type',
'application/json');
265 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
266 $requestHeaders, [], [], $reqBody);
268 $this->expectException(HttpBadRequestException::class);
269 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
280 $_SESSION[
'UserLevel'] = 10;
282 array_push($this->rq[
"options"],
"M");
283 $alteredOptions = array();
284 foreach ($this->rq[
'options'] as $key) {
285 $alteredOptions[$key] = $key;
288 $body[
'options'] = $alteredOptions;
290 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
292 $mess = _(
"The maintenance job has been queued");
294 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
295 $reqBody = $this->streamFactory->createStream(json_encode(
299 $requestHeaders =
new Headers();
300 $requestHeaders->setHeader(
'Content-Type',
'application/json');
301 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
302 $requestHeaders, [], [], $reqBody);
304 $this->expectException(HttpNotFoundException::class);
305 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
315 $_SESSION[
'UserLevel'] = 10;
318 $req[
"goldDate"] =
"";
321 $alteredOptions = array();
322 foreach ($req[
'options'] as $key) {
323 $alteredOptions[$key] = $key;
326 $body[
'options'] = $alteredOptions;
328 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
330 $mess = _(
"The maintenance job has been queued");
332 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
333 $reqBody = $this->streamFactory->createStream(json_encode(
337 $requestHeaders =
new Headers();
338 $requestHeaders->setHeader(
'Content-Type',
'application/json');
339 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
340 $requestHeaders, [], [], $reqBody);
342 $this->expectException(HttpBadRequestException::class);
343 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
354 $_SESSION[
'UserLevel'] = 10;
357 $req[
"logsDate"] =
"";
358 $alteredOptions = array();
359 foreach ($req[
'options'] as $key) {
360 $alteredOptions[$key] = $key;
363 $body[
'options'] = $alteredOptions;
365 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
367 $mess = _(
"The maintenance job has been queued");
369 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
370 $reqBody = $this->streamFactory->createStream(json_encode(
374 $requestHeaders =
new Headers();
375 $requestHeaders->setHeader(
'Content-Type',
'application/json');
376 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
377 $requestHeaders, [], [], $reqBody);
379 $this->expectException(HttpBadRequestException::class);
380 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
390 $_SESSION[
'UserLevel'] = 10;
392 $this->maintagentPlugin->shouldReceive(
'getLastMaintenanceRunTime')
393 ->andReturn(
"2022-07-16 10:15:30");
395 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
396 new Headers(), [], [], $this->streamFactory->createStream());
398 $actualResponse = $this->maintenanceController->getMaintenanceInfo($request,
new ResponseHelper(),
null);
400 $this->assertEquals(200, $actualResponse->getStatusCode());
402 [
'lastRun' => date(DATE_ATOM, strtotime(
"2022-07-16 10:15:30"))],
413 $_SESSION[
'UserLevel'] = 10;
415 $this->maintagentPlugin->shouldReceive(
'getLastMaintenanceRunTime')
418 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
419 new Headers(), [], [], $this->streamFactory->createStream());
421 $actualResponse = $this->maintenanceController->getMaintenanceInfo($request,
new ResponseHelper(),
null);
423 $this->assertEquals(200, $actualResponse->getStatusCode());
424 $this->assertEquals([
'lastRun' =>
null], $this->
getResponseJson($actualResponse));
434 $_SESSION[
'UserLevel'] = 0;
436 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
437 new Headers(), [], [], $this->streamFactory->createStream());
439 $this->expectException(HttpForbiddenException::class);
440 $this->maintenanceController->getMaintenanceInfo($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.
testGetMaintenanceInfoNoRuns()
testGetMaintenanceInfoUserNotAdmin()
testCreateMaintenanceWithBadRequest()
testCreateMaintenanceInvalidGoldDate()
setUp()
Setup test objects.
testCreateMaintenanceUserNotAdmin()
testCreateMaintenanceInvalidLogsDate()
getResponseJson($response)
testCreateMaintenanceOptionKeyNotFound()
fo_dbManager * dbManager
fo_dbManager object