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';
45 const YAML_LOC = __DIR__ .
'/../../../ui/api/documentation/openapi.yaml';
96 protected function setUp() : void
100 "options" => [
"A",
"F",
"g",
"l",
"o"],
101 "logsDate"=>
"2021-08-19",
102 "goldDate"=>
"2022-07-16"
106 "A"=>
"Run all maintenance operations.",
107 "F"=>
"Validate folder contents.",
108 "g"=>
"Remove orphaned gold files.",
109 "o"=>
"Remove older gold files from repository.",
110 "l"=>
"Remove older log files from repository."
112 $container = M::mock(
'ContainerBuilder');
113 $this->dbHelper = M::mock(DbHelper::class);
114 $this->restHelper = M::mock(RestHelper::class);
115 $this->userDao = M::mock(UserDao::class);
116 $this->auth = M::mock(Auth::class);
118 $this->maintagentPlugin = M::mock(
'maintagent');
120 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
121 $this->restHelper->shouldReceive(
'getUserDao')
122 ->andReturn($this->userDao);
124 $this->restHelper->shouldReceive(
'getPlugin')
125 ->withArgs(array(
'maintagent'))
126 ->andReturn($this->maintagentPlugin);
128 $this->auth->shouldReceive(
'isAdmin')->andReturn(
true);
130 $container->shouldReceive(
'get')->withArgs(array(
131 'helper.restHelper'))->andReturn($this->restHelper);
133 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
134 $this->
dbManager = M::mock(DbManager::class);
135 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
136 $this->streamFactory =
new StreamFactory();
148 $response->getBody()->seek(0);
149 return json_decode($response->getBody()->getContents(),
true);
159 $_SESSION[
'UserLevel'] = 10;
162 "options" => [
"A",
"F",
"g",
"l",
"o"],
163 "logsDate"=>
"2021-08-19",
164 "goldDate"=>
"2022-07-16"
168 "A"=>
"Run all maintenance operations.",
169 "F"=>
"Validate folder contents.",
170 "g"=>
"Remove orphaned gold files.",
171 "o"=>
"Remove older gold files from repository.",
172 "l"=>
"Remove older log files from repository."
175 $alteredOptions = array();
176 foreach ($rq[
'options'] as $key) {
177 $alteredOptions[$key] = $key;
180 $body[
'options'] = $alteredOptions;
182 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($OPTIONS);
184 $mess = _(
"The maintenance job has been queued");
186 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
188 $info =
new Info(201, $mess, InfoType::INFO);
191 $expectedResponse = (
new ResponseHelper())->withJson($info->getArray(), $info->getCode());
193 $reqBody = $this->streamFactory->createStream(json_encode(
197 $requestHeaders =
new Headers();
198 $requestHeaders->setHeader(
'Content-Type',
'application/json');
199 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
200 $requestHeaders, [], [], $reqBody);
203 $actualResponse = $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
205 $this->assertEquals($expectedResponse->getStatusCode(),
206 $actualResponse->getStatusCode());
218 $_SESSION[
'UserLevel'] = 0;
220 $alteredOptions = array();
221 foreach ($this->rq[
'options'] as $key) {
222 $alteredOptions[$key] = $key;
225 $body[
'options'] = $alteredOptions;
227 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
229 $mess = _(
"The maintenance job has been queued");
231 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
232 $reqBody = $this->streamFactory->createStream(json_encode(
236 $requestHeaders =
new Headers();
237 $requestHeaders->setHeader(
'Content-Type',
'application/json');
238 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
239 $requestHeaders, [], [], $reqBody);
241 $this->expectException(HttpForbiddenException::class);
242 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
252 $_SESSION[
'UserLevel'] = 10;
254 $this->rq[
"options"] = [];
256 $reqBody = $this->streamFactory->createStream(json_encode(
259 $requestHeaders =
new Headers();
260 $requestHeaders->setHeader(
'Content-Type',
'application/json');
261 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
262 $requestHeaders, [], [], $reqBody);
264 $this->expectException(HttpBadRequestException::class);
265 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
276 $_SESSION[
'UserLevel'] = 10;
278 array_push($this->rq[
"options"],
"M");
279 $alteredOptions = array();
280 foreach ($this->rq[
'options'] as $key) {
281 $alteredOptions[$key] = $key;
284 $body[
'options'] = $alteredOptions;
286 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
288 $mess = _(
"The maintenance job has been queued");
290 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
291 $reqBody = $this->streamFactory->createStream(json_encode(
295 $requestHeaders =
new Headers();
296 $requestHeaders->setHeader(
'Content-Type',
'application/json');
297 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
298 $requestHeaders, [], [], $reqBody);
300 $this->expectException(HttpNotFoundException::class);
301 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
311 $_SESSION[
'UserLevel'] = 10;
314 $req[
"goldDate"] =
"";
317 $alteredOptions = array();
318 foreach ($req[
'options'] as $key) {
319 $alteredOptions[$key] = $key;
322 $body[
'options'] = $alteredOptions;
324 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
326 $mess = _(
"The maintenance job has been queued");
328 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
329 $reqBody = $this->streamFactory->createStream(json_encode(
333 $requestHeaders =
new Headers();
334 $requestHeaders->setHeader(
'Content-Type',
'application/json');
335 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
336 $requestHeaders, [], [], $reqBody);
338 $this->expectException(HttpBadRequestException::class);
339 $this->maintenanceController->createMaintenance($request,
new ResponseHelper(),
null);
350 $_SESSION[
'UserLevel'] = 10;
353 $req[
"logsDate"] =
"";
354 $alteredOptions = array();
355 foreach ($req[
'options'] as $key) {
356 $alteredOptions[$key] = $key;
359 $body[
'options'] = $alteredOptions;
361 $this->maintagentPlugin->shouldReceive(
'getOptions')->andReturn($this->OPTIONS);
363 $mess = _(
"The maintenance job has been queued");
365 $this->maintagentPlugin->shouldReceive(
'handle')->withArgs([$body])->andReturn($mess);
366 $reqBody = $this->streamFactory->createStream(json_encode(
370 $requestHeaders =
new Headers();
371 $requestHeaders->setHeader(
'Content-Type',
'application/json');
372 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
373 $requestHeaders, [], [], $reqBody);
375 $this->expectException(HttpBadRequestException::class);
376 $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