27 use Slim\Psr7\Factory\StreamFactory;
28 use Slim\Psr7\Headers;
29 use Slim\Psr7\Request;
32 require_once dirname(__DIR__, 4) .
"/lib/php/Plugin/FO_Plugin.php";
98 protected function setUp() : void
101 $container = M::mock(
'ContainerBuilder');
102 $this->dbHelper = M::mock(DbHelper::class);
103 $this->restHelper = M::mock(RestHelper::class);
104 $this->jobDao = M::mock(JobDao::class);
105 $this->showJobsDao = M::mock(ShowJobsDao::class);
106 $this->uploadDao = M::mock(UploadDao::class);
109 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
110 $this->restHelper->shouldReceive(
'getJobDao')->andReturn($this->jobDao);
111 $this->restHelper->shouldReceive(
'getShowJobDao')->andReturn($this->showJobsDao);
112 $this->restHelper->shouldReceive(
'getUploadDao')->andReturn($this->uploadDao);
113 $this->restHelper->shouldReceive(
'getGroupId')->andReturn($this->groupId);
115 $container->shouldReceive(
'get')->withArgs(array(
116 'helper.restHelper'))->andReturn($this->restHelper);
118 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
119 $this->streamFactory =
new StreamFactory();
128 $this->addToAssertionCount(
129 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
142 $response->getBody()->seek(0);
143 return json_decode($response->getBody()->getContents(),
true);
153 $userArray = array();
154 foreach ($userIds as $userId) {
157 } elseif ($userId > 2 && $userId <= 4) {
159 } elseif ($userId == 5) {
164 $user =
new User($userId,
"user$userId",
"User $userId",
165 "user$userId@example.com", $accessLevel, 2, 4,
"");
166 $userArray[] = $user->getArray();
178 $job =
new Job(11,
"job_name",
"01-01-2020", 4, 2, 2, 0,
"Completed");
179 $jobQueue =
new JobQueue(44,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
180 'Completed', 0,
null, [], 0,
true,
false,
true,
181 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
182 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(11))
183 ->andReturn([
'44' => 0]);
184 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
185 ->withArgs(array(11,
'', 0, 4))->andReturn(
"0");
186 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
187 ->withArgs(array(44))->andReturn([
"jq_endtext"=>
'Completed']);
189 $requestHeaders =
new Headers();
190 $body = $this->streamFactory->createStream();
191 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
192 $requestHeaders, [], [], $body);
196 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
197 $this->dbHelper->shouldReceive(
'getUserJobs')->withArgs(array(2, NULL,
'ASC', 0, 1))
198 ->andReturn([[$job], 1]);
199 $actualResponse = $this->jobController->getJobs($request, $response, []);
200 $expectedResponse = $job->getArray(ApiVersion::V1);
201 $this->assertEquals(200, $actualResponse->getStatusCode());
202 $this->assertEquals($expectedResponse,
204 $this->assertEquals(
'1',
205 $actualResponse->getHeaderLine(
'X-Total-Pages'));
215 $jobTwo =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
216 $jobTwoQueue =
new JobQueue(45,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
217 'Completed', 0,
null, [], 0,
true,
false,
true,
218 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
219 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(11))
220 ->andReturn([
'44' => 0]);
221 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(12))
222 ->andReturn([
'45' => 0]);
223 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
224 ->withArgs(array(M::anyOf(11, 12),
'', 0, M::anyOf(4, 5)))->andReturn(
"0");
225 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
226 ->withArgs([M::anyOf(44, 45)])->andReturn([
"jq_endtext"=>
'Completed']);
228 $requestHeaders =
new Headers();
229 $requestHeaders->setHeader(
'limit',
'1');
230 $requestHeaders->setHeader(
'page',
'2');
231 $body = $this->streamFactory->createStream();
232 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
233 $requestHeaders, [], [], $body);
237 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
238 $this->dbHelper->shouldReceive(
'getUserJobs')->withArgs(array(2,
null,
"ASC", 1, 2))
239 ->andReturn([[$jobTwo], 2]);
240 $actualResponse = $this->jobController->getJobs($request, $response, []);
241 $expectedResponse = $jobTwo->getArray(ApiVersion::V1);
242 $this->assertEquals(200, $actualResponse->getStatusCode());
243 $this->assertEquals($expectedResponse,
245 $this->assertEquals(
'2',
246 $actualResponse->getHeaderLine(
'X-Total-Pages'));
256 $this->dbHelper->shouldReceive(
'doesIdExist')
257 ->withArgs([
"job",
"job_pk", 2])->andReturn(
false);
259 $requestHeaders =
new Headers();
260 $requestHeaders->setHeader(
'limit',
'1');
261 $requestHeaders->setHeader(
'page',
'2');
262 $body = $this->streamFactory->createStream();
263 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
264 $requestHeaders, [], [], $body);
267 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
268 $this->expectException(HttpNotFoundException::class);
270 $this->jobController->getJobs($request, $response, [
"id" => 2]);
280 $job =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
281 $jobTwoQueue =
new JobQueue(45,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
282 'Completed', 0,
null, [], 0,
true,
false,
true,
283 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
284 $this->dbHelper->shouldReceive(
'doesIdExist')
285 ->withArgs([
"job",
"job_pk", 12])->andReturn(
true);
286 $this->dbHelper->shouldReceive(
'doesIdExist')
287 ->withArgs([
"upload",
"upload_pk", 5])->andReturn(
true);
288 $this->uploadDao->shouldReceive(
'isAccessible')
289 ->withArgs([5, $this->groupId])->andReturn(
true);
290 $this->dbHelper->shouldReceive(
'getJobs')->withArgs(array(12, NULL,
'ASC', 0, 1, NULL))
291 ->andReturn([[$job], 1]);
292 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(12))
293 ->andReturn([
'45' => 0]);
294 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
295 ->withArgs(array(12,
'', 0, 5))->andReturn(
"0");
296 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
297 ->withArgs([45])->andReturn([
"jq_endtext"=>
'Completed']);
299 $requestHeaders =
new Headers();
300 $body = $this->streamFactory->createStream();
301 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
302 $requestHeaders, [], [], $body);
306 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
307 $actualResponse = $this->jobController->getJobs($request, $response, [
309 $expectedResponse = $job->getArray(ApiVersion::V1);
310 $this->assertEquals(200, $actualResponse->getStatusCode());
311 $this->assertEquals($expectedResponse,
313 $this->assertEquals(
'1',
314 $actualResponse->getHeaderLine(
'X-Total-Pages'));
326 $job =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
327 $this->dbHelper->shouldReceive(
'doesIdExist')
328 ->withArgs([
"job",
"job_pk", 12])->andReturn(
true);
329 $this->dbHelper->shouldReceive(
'doesIdExist')
330 ->withArgs([
"upload",
"upload_pk", 5])->andReturn(
true);
331 $this->uploadDao->shouldReceive(
'isAccessible')
332 ->withArgs([5, $this->groupId])->andReturn(
false);
333 $this->dbHelper->shouldReceive(
'getJobs')->withArgs(array(12, NULL,
'ASC', 0, 1, NULL))
334 ->andReturn([[$job], 1]);
336 $requestHeaders =
new Headers();
337 $body = $this->streamFactory->createStream();
338 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
339 $requestHeaders, [], [], $body);
342 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
343 $this->expectException(HttpForbiddenException::class);
349 $this->jobController->getJobs($request, $response, [
"id" => 12]);
359 $job =
new Job(12,
"job_two",
"01-01-2020", 5, 2, 2, 0,
"Completed");
360 $jobTwoQueue =
new JobQueue(45,
'readmeoss',
'2020-01-01 20:41:49',
'2020-01-01 20:41:50',
361 'Completed', 0,
null, [], 0,
true,
false,
true,
362 [
'text' =>
'ReadMeOss',
'link' =>
'http://localhost/repo/api/v1/report/16']);
363 $this->dbHelper->shouldReceive(
'doesIdExist')
364 ->withArgs([
"upload",
"upload_pk", 5])->andReturn(
true);
365 $this->uploadDao->shouldReceive(
'isAccessible')
366 ->withArgs([5, $this->groupId])->andReturn(
true);
367 $this->dbHelper->shouldReceive(
'doesIdExist')
368 ->withArgs([
'job',
'job_pk', 12])->andReturn(
true);
369 $this->dbHelper->shouldReceive(
'getJobs')->withArgs(array(
null,
null,
"ASC", 0, 1, 5))
370 ->andReturn([[$job], 1]);
371 $this->jobDao->shouldReceive(
'getChlidJobStatus')->withArgs(array(12))
372 ->andReturn([
'45' => 0]);
373 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
374 ->withArgs(array(12,
'', 0, 5))->andReturn(
"0");
375 $this->showJobsDao->shouldReceive(
'getDataForASingleJob')
376 ->withArgs([45])->andReturn([
"jq_endtext"=>
'Completed']);
378 $requestHeaders =
new Headers();
379 $body = $this->streamFactory->createStream();
380 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
381 $requestHeaders, [], [], $body);
386 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
387 $actualResponse = $this->jobController->getJobs($request, $response, []);
388 $expectedResponse = $job->getArray(ApiVersion::V1);
389 $this->assertEquals(200, $actualResponse->getStatusCode());
390 $this->assertEquals($expectedResponse,
392 $this->assertEquals(
'1',
393 $actualResponse->getHeaderLine(
'X-Total-Pages'));
405 $this->dbHelper->shouldReceive(
'doesIdExist')
406 ->withArgs([
"upload",
"upload_pk", 5])->andReturn(
true);
407 $this->uploadDao->shouldReceive(
'isAccessible')
408 ->withArgs([5, $this->groupId])->andReturn(
false);
409 $this->dbHelper->shouldNotReceive(
'getJobs');
411 $requestHeaders =
new Headers();
412 $body = $this->streamFactory->createStream();
413 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
414 $requestHeaders, [], [], $body);
418 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
419 $this->expectException(HttpForbiddenException::class);
421 $this->jobController->getJobs($request, $response, []);
433 $this->dbHelper->shouldReceive(
'doesIdExist')
434 ->withArgs([
"upload",
"upload_pk", 99])->andReturn(
false);
435 $this->dbHelper->shouldNotReceive(
'getJobs');
437 $requestHeaders =
new Headers();
438 $body = $this->streamFactory->createStream();
439 $request =
new Request(
"GET",
new Uri(
"HTTP",
"localhost"),
440 $requestHeaders, [], [], $body);
444 $this->restHelper->shouldReceive(
'getUserId')->andReturn($userId);
445 $this->expectException(HttpNotFoundException::class);
447 $this->jobController->getJobs($request, $response, []);
461 $completedUpload = 3;
462 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
463 ->withArgs([$jobId,
'', 0, $uploadId])
464 ->andReturn(
"3:10:23");
465 $this->showJobsDao->shouldReceive(
'getEstimatedTime')
466 ->withArgs([$completedJob,
'', 0, $completedUpload])
468 $reflection = new \ReflectionClass(get_class($this->jobController));
469 $method = $reflection->getMethod(
'getUploadEtaInSeconds');
470 $method->setAccessible(
true);
472 $result = $method->invokeArgs($this->jobController, [$jobId, $uploadId]);
473 $this->assertEquals((3 * 3600) + (10 * 60) + 23, $result);
475 $result = $method->invokeArgs($this->jobController,
476 [$completedJob, $completedUpload]);
477 $this->assertEquals(0, $result);
Controller for Job model.
Override Slim response for withJson function.
Model class to hold JobQueue info.
Model to hold user information.
getResponseJson($response)
testGetJobsFromUploadNotAccessible()
testGetUploadEtaInSeconds()
tearDown()
Remove test objects.
setUp()
Setup test objects.
testGetJobFromIdNotAccessible()
testGetJobsFromUploadNotFound()
#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.