13 namespace Fossology\UI\Api\Controllers
23 $b[3] =
"root-child1";
24 $b[4] =
"root-child2";
26 $b[$a] =
"singlefolder";
35 return "$folderId-folder";
43 $folderList = array();
44 $folderList[] = [
"folder_pk" => 2,
"folder_name" =>
FolderGetName(2)];
45 $folderList[] = [
"folder_pk" => 3,
"folder_name" =>
FolderGetName(3)];
65 use Slim\Psr7\Factory\StreamFactory;
66 use Slim\Psr7\Headers;
67 use Slim\Psr7\Request;
74 class FolderControllerTest
extends \PHPUnit\Framework\TestCase
96 private $folderController;
106 private $folderPlugin;
111 private $deletePlugin;
116 private $folderPropertiesPlugin;
121 private $folderContentPlugin;
127 private $assertCountBefore;
133 private $streamFactory;
139 protected function setUp() : void
143 $container = M::mock(
'ContainerBuilder');
144 $this->dbHelper = M::mock(DbHelper::class);
145 $this->restHelper = M::mock(RestHelper::class);
146 $this->folderDao = M::mock(FolderDao::class);
147 $this->folderPlugin = M::mock(
'folder_create');
148 $this->deletePlugin = M::mock(
'admin_folder_delete');
149 $this->folderPropertiesPlugin = M::mock(
'folder_properties');
150 $this->folderContentPlugin = M::mock(
'content_move');
152 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
153 $this->restHelper->shouldReceive(
'getFolderDao')->andReturn($this->folderDao);
154 $this->restHelper->shouldReceive(
'getUserId')->andReturn($this->userId);
155 $this->restHelper->shouldReceive(
'getPlugin')
156 ->withArgs(array(
'folder_create'))->andReturn($this->folderPlugin);
157 $this->restHelper->shouldReceive(
'getPlugin')
158 ->withArgs(array(
'admin_folder_delete'))->andReturn($this->deletePlugin);
159 $this->restHelper->shouldReceive(
'getPlugin')
160 ->withArgs(array(
'folder_properties'))
161 ->andReturn($this->folderPropertiesPlugin);
162 $this->restHelper->shouldReceive(
'getPlugin')
163 ->withArgs(array(
'content_move'))
164 ->andReturn($this->folderContentPlugin);
166 $container->shouldReceive(
'get')->withArgs(array(
167 'helper.restHelper'))->andReturn($this->restHelper);
168 $this->folderController =
new FolderController($container);
169 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
170 $this->streamFactory =
new StreamFactory();
177 protected function tearDown() : void
179 $this->addToAssertionCount(
180 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
190 public function getFolder($id)
194 case 2: $name =
"root";
break;
195 case 3: $name =
"root-child1";
break;
196 case 4: $name =
"root-child2";
break;
197 case -1:
return null;
198 default: $name =
"singlefolder";
200 return new Folder($id, $name,
"", 1);
209 public function getFolderParent($id)
212 if ($id == 3 || $id == 4) {
226 private function getResponseJson($response)
228 $response->getBody()->seek(0);
229 return json_decode($response->getBody()->getContents(),
true);
238 public function testGetAllFolders()
240 $rootFolder =
new Folder(2,
"root",
"", 2);
241 $this->folderDao->shouldReceive(
'getRootFolder')->withArgs(array(2))
242 ->andReturn($rootFolder);
243 $this->folderDao->shouldReceive(
'getFolder')
244 ->andReturnUsing([$this,
'getFolder']);
245 $this->folderDao->shouldReceive(
'getFolderParentId')
246 ->andReturnUsing([$this,
'getFolderParent']);
247 $expectedFoldersList = [];
248 for ($i = 2; $i <= 4; $i ++) {
249 $folder = $this->getFolder($i);
250 $parentId = $this->getFolderParent($i);
251 $folderModel = new \Fossology\UI\Api\Models\Folder($folder->getId(),
252 $folder->getName(), $folder->getDescription(), $parentId);
253 $expectedFoldersList[] = $folderModel->getArray();
255 $actualResponse = $this->folderController->getFolders(
null,
256 new ResponseHelper(), []);
257 $this->assertEquals(200, $actualResponse->getStatusCode());
258 $this->assertEquals($expectedFoldersList,
259 $this->getResponseJson($actualResponse));
267 public function testGetSpecificFolders()
270 $this->folderDao->shouldReceive(
'isFolderAccessible')
271 ->withArgs(array($folderId))->andReturn(
true);
272 $this->folderDao->shouldReceive(
'getFolder')
273 ->andReturnUsing([$this,
'getFolder']);
274 $this->folderDao->shouldReceive(
'getFolderParentId')
275 ->andReturnUsing([$this,
'getFolderParent']);
276 $folder = $this->getFolder($folderId);
277 $parentId = $this->getFolderParent($folderId);
278 $folderModel = new \Fossology\UI\Api\Models\Folder($folder->getId(),
279 $folder->getName(), $folder->getDescription(), $parentId);
280 $expectedFoldersList = $folderModel->getArray();
281 $actualResponse = $this->folderController->getFolders(
null,
282 new ResponseHelper(), [
'id' => $folderId]);
283 $this->assertEquals(200, $actualResponse->getStatusCode());
284 $this->assertEquals($expectedFoldersList,
285 $this->getResponseJson($actualResponse));
294 public function testGetInvalidFolder()
297 $this->folderDao->shouldReceive(
'isFolderAccessible')
298 ->withArgs(array($folderId))->andReturn(
false);
299 $this->folderDao->shouldReceive(
'getFolder')
300 ->andReturnUsing([$this,
'getFolder']);
301 $this->expectException(HttpForbiddenException::class);
303 $this->folderController->getFolders(
null,
new ResponseHelper(),
304 [
'id' => $folderId]);
313 public function testGetInAccessibleFolder()
316 $this->folderDao->shouldReceive(
'isFolderAccessible')
317 ->withArgs(array($folderId))->andReturn(
false);
318 $this->folderDao->shouldReceive(
'getFolder')
319 ->andReturnUsing([$this,
'getFolder']);
320 $this->expectException(HttpForbiddenException::class);
322 $this->folderController->getFolders(
null,
new ResponseHelper(),
323 [
'id' => $folderId]);
331 public function testCreateFolder()
334 $folderName =
"root-child1";
335 $folderDescription =
"Description";
337 $this->folderDao->shouldReceive(
'isFolderAccessible')
338 ->withArgs(array($parentFolder, $this->userId))->andReturn(
true);
339 $this->folderPlugin->shouldReceive(
'create')
340 ->withArgs(array($parentFolder, $folderName, $folderDescription))
342 $this->folderDao->shouldReceive(
'getFolderId')
343 ->withArgs(array($folderName, $parentFolder))->andReturn($folderId);
344 $requestHeaders =
new Headers();
345 $requestHeaders->setHeader(
'parentFolder', $parentFolder);
346 $requestHeaders->setHeader(
'folderName', $folderName);
347 $requestHeaders->setHeader(
'folderDescription', $folderDescription);
348 $body = $this->streamFactory->createStream();
349 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
350 $requestHeaders, [], [], $body);
351 $response =
new ResponseHelper();
352 $actualResponse = $this->folderController->createFolder($request,
354 $expectedResponse =
new Info(201, $folderId, InfoType::INFO);
355 $this->assertEquals($expectedResponse->getCode(),
356 $actualResponse->getStatusCode());
357 $this->assertEquals($expectedResponse->getArray(),
358 $this->getResponseJson($actualResponse));
366 public function testCreateFolderParentNotAccessible()
369 $folderName =
"root-child1";
370 $folderDescription =
"Description";
371 $this->folderDao->shouldReceive(
'isFolderAccessible')
372 ->withArgs(array($parentFolder, $this->userId))->andReturn(
false);
373 $requestHeaders =
new Headers();
374 $requestHeaders->setHeader(
'parentFolder', $parentFolder);
375 $requestHeaders->setHeader(
'folderName', $folderName);
376 $requestHeaders->setHeader(
'folderDescription', $folderDescription);
377 $body = $this->streamFactory->createStream();
378 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
379 $requestHeaders, [], [], $body);
380 $response =
new ResponseHelper();
382 $this->expectException(HttpForbiddenException::class);
384 $this->folderController->createFolder($request, $response, []);
393 public function testCreateFolderDuplicateNames()
396 $folderName =
"root-child1";
397 $folderDescription =
"Description";
398 $this->folderDao->shouldReceive(
'isFolderAccessible')
399 ->withArgs(array($parentFolder, $this->userId))->andReturn(
true);
400 $this->folderPlugin->shouldReceive(
'create')
401 ->withArgs(array($parentFolder, $folderName, $folderDescription))
403 $requestHeaders =
new Headers();
404 $requestHeaders->setHeader(
'parentFolder', $parentFolder);
405 $requestHeaders->setHeader(
'folderName', $folderName);
406 $requestHeaders->setHeader(
'folderDescription', $folderDescription);
407 $body = $this->streamFactory->createStream();
408 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
409 $requestHeaders, [], [], $body);
410 $response =
new ResponseHelper();
411 $actualResponse = $this->folderController->createFolder($request,
413 $expectedResponse =
new Info(200,
"Folder $folderName already exists!",
415 $this->assertEquals($expectedResponse->getCode(),
416 $actualResponse->getStatusCode());
417 $this->assertEquals($expectedResponse->getArray(),
418 $this->getResponseJson($actualResponse));
426 public function testDeleteFolder()
430 $this->folderDao->shouldReceive(
'getFolder')
431 ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
432 $this->deletePlugin->shouldReceive(
'Delete')
433 ->withArgs(array(
"2 $folderId", $this->userId))->andReturnNull();
434 $actualResponse = $this->folderController->deleteFolder(
null,
435 new ResponseHelper(), [
"id" => $folderId]);
436 $expectedResponse =
new Info(202,
"Folder, \"$folderName\" deleted.",
438 $this->assertEquals($expectedResponse->getCode(),
439 $actualResponse->getStatusCode());
440 $this->assertEquals($expectedResponse->getArray(),
441 $this->getResponseJson($actualResponse));
449 public function testDeleteFolderInvalidFolder()
452 $this->folderDao->shouldReceive(
'getFolder')
453 ->withArgs(array($folderId))->andReturnNull();
454 $this->expectException(HttpNotFoundException::class);
456 $this->folderController->deleteFolder(
null,
new ResponseHelper(),
457 [
"id" => $folderId]);
465 public function testDeleteFolderNoAccess()
468 $errorText =
"No access to delete this folder";
469 $this->folderDao->shouldReceive(
'getFolder')
470 ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
471 $this->deletePlugin->shouldReceive(
'Delete')
472 ->withArgs(array(
"2 $folderId", $this->userId))
473 ->andReturn($errorText);
474 $this->expectException(HttpForbiddenException::class);
476 $this->folderController->deleteFolder(
null,
new ResponseHelper(),
477 [
"id" => $folderId]);
485 public function testEditFolder()
488 $folderName =
"new name";
489 $folderDescription =
"new description";
490 $this->folderDao->shouldReceive(
'getFolder')
491 ->andReturnUsing([$this,
'getFolder']);
492 $this->folderDao->shouldReceive(
'isFolderAccessible')
493 ->withArgs(array($folderId, $this->userId))->andReturn(
true);
494 $this->folderPropertiesPlugin->shouldReceive(
'Edit')
495 ->withArgs(array($folderId, $folderName, $folderDescription));
496 $requestHeaders =
new Headers();
497 $requestHeaders->setHeader(
'name', $folderName);
498 $requestHeaders->setHeader(
'description', $folderDescription);
499 $body = $this->streamFactory->createStream();
500 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost"),
501 $requestHeaders, [], [], $body);
502 $response =
new ResponseHelper();
503 $actualResponse = $this->folderController->editFolder($request,
504 $response, [
"id" => $folderId]);
505 $expectedResponse =
new Info(200,
'Folder "' . \Fossology\UI\Api\Controllers\
FolderGetName($folderId) .
506 '" updated.', InfoType::INFO);
507 $this->assertEquals($expectedResponse->getCode(),
508 $actualResponse->getStatusCode());
509 $this->assertEquals($expectedResponse->getArray(),
510 $this->getResponseJson($actualResponse));
518 public function testEditFolderNotExists()
521 $folderName =
"new name";
522 $folderDescription =
"new description";
523 $this->folderDao->shouldReceive(
'getFolder')->andReturnNull();
524 $requestHeaders =
new Headers();
525 $requestHeaders->setHeader(
'name', $folderName);
526 $requestHeaders->setHeader(
'description', $folderDescription);
527 $body = $this->streamFactory->createStream();
528 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost"),
529 $requestHeaders, [], [], $body);
530 $response =
new ResponseHelper();
531 $this->expectException(HttpNotFoundException::class);
533 $this->folderController->editFolder($request, $response,
534 [
"id" => $folderId]);
542 public function testEditFolderNotAccessible()
545 $folderName =
"new name";
546 $folderDescription =
"new description";
547 $this->folderDao->shouldReceive(
'getFolder')
548 ->andReturnUsing([$this,
'getFolder']);
549 $this->folderDao->shouldReceive(
'isFolderAccessible')
550 ->withArgs(array($folderId, $this->userId))->andReturn(
false);
551 $requestHeaders =
new Headers();
552 $requestHeaders->setHeader(
'name', $folderName);
553 $requestHeaders->setHeader(
'description', $folderDescription);
554 $body = $this->streamFactory->createStream();
555 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost"),
556 $requestHeaders, [], [], $body);
557 $response =
new ResponseHelper();
558 $this->expectException(HttpForbiddenException::class);
560 $this->folderController->editFolder($request, $response,
561 [
"id" => $folderId]);
569 public function testCopyFolder()
573 $folderContentPk = 5;
577 $this->folderDao->shouldReceive(
'getFolder')
578 ->andReturnUsing([$this,
'getFolder']);
579 $this->folderDao->shouldReceive(
'isFolderAccessible')
580 ->withArgs(array(M::anyOf($folderId,
"$parentId"),
581 $this->userId))->andReturn(
true);
582 $this->folderDao->shouldReceive(
'getFolderContentsId')
583 ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
584 $this->folderContentPlugin->shouldReceive(
'copyContent')
585 ->withArgs(array([$folderContentPk], $parentId,
true))->andReturn(
"");
586 $requestHeaders =
new Headers();
587 $requestHeaders->setHeader(
'parent', $parentId);
588 $requestHeaders->setHeader(
'action',
"copy");
589 $body = $this->streamFactory->createStream();
590 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
591 $requestHeaders, [], [], $body);
592 $response =
new ResponseHelper();
594 $actualResponse = $this->folderController->copyFolder($request,
595 $response, [
"id" => $folderId]);
596 $expectedResponse =
new Info(202,
597 "Folder \"$folderName\" copy(ed) under \"$parentFolderName\".",
599 $this->assertEquals($expectedResponse->getCode(),
600 $actualResponse->getStatusCode());
601 $this->assertEquals($expectedResponse->getArray(),
602 $this->getResponseJson($actualResponse));
610 public function testMoveFolder()
614 $folderContentPk = 5;
618 $this->folderDao->shouldReceive(
'getFolder')
619 ->andReturnUsing([$this,
'getFolder']);
620 $this->folderDao->shouldReceive(
'isFolderAccessible')
621 ->withArgs(array(M::anyOf($folderId,
"$parentId"),
622 $this->userId))->andReturn(
true);
623 $this->folderDao->shouldReceive(
'getFolderContentsId')
624 ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
625 $this->folderContentPlugin->shouldReceive(
'copyContent')
626 ->withArgs(array([$folderContentPk], $parentId,
false))->andReturn(
"");
627 $requestHeaders =
new Headers();
628 $requestHeaders->setHeader(
'parent', $parentId);
629 $requestHeaders->setHeader(
'action',
"move");
630 $body = $this->streamFactory->createStream();
631 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
632 $requestHeaders, [], [], $body);
633 $response =
new ResponseHelper();
635 $actualResponse = $this->folderController->copyFolder($request,
636 $response, [
"id" => $folderId]);
637 $expectedResponse =
new Info(202,
638 "Folder \"$folderName\" move(ed) under \"$parentFolderName\".",
640 $this->assertEquals($expectedResponse->getCode(),
641 $actualResponse->getStatusCode());
642 $this->assertEquals($expectedResponse->getArray(),
643 $this->getResponseJson($actualResponse));
651 public function testCopyFolderNotFound()
656 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($folderId))
658 $requestHeaders =
new Headers();
659 $requestHeaders->setHeader(
'parent', $parentId);
660 $requestHeaders->setHeader(
'action',
"copy");
661 $body = $this->streamFactory->createStream();
662 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
663 $requestHeaders, [], [], $body);
664 $response =
new ResponseHelper();
665 $this->expectException(HttpNotFoundException::class);
667 $this->folderController->copyFolder($request, $response,
668 [
"id" => $folderId]);
676 public function testCopyParentFolderNotFound()
681 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($folderId))
682 ->andReturn($this->getFolder($folderId));
683 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($parentId))
685 $requestHeaders =
new Headers();
686 $requestHeaders->setHeader(
'parent', $parentId);
687 $requestHeaders->setHeader(
'action',
"copy");
688 $body = $this->streamFactory->createStream();
689 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
690 $requestHeaders, [], [], $body);
691 $response =
new ResponseHelper();
692 $this->expectException(HttpNotFoundException::class);
694 $this->folderController->copyFolder($request, $response,
695 [
"id" => $folderId]);
703 public function testCopyFolderNotAccessible()
708 $this->folderDao->shouldReceive(
'getFolder')
709 ->andReturnUsing([$this,
'getFolder']);
710 $this->folderDao->shouldReceive(
'isFolderAccessible')
711 ->withArgs(array($folderId, $this->userId))->andReturn(
false);
712 $requestHeaders =
new Headers();
713 $requestHeaders->setHeader(
'parent', $parentId);
714 $requestHeaders->setHeader(
'action',
"copy");
715 $body = $this->streamFactory->createStream();
716 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
717 $requestHeaders, [], [], $body);
718 $response =
new ResponseHelper();
719 $this->expectException(HttpForbiddenException::class);
721 $this->folderController->copyFolder($request, $response,
722 [
"id" => $folderId]);
730 public function testCopyParentFolderNotAccessible()
735 $this->folderDao->shouldReceive(
'getFolder')
736 ->andReturnUsing([$this,
'getFolder']);
737 $this->folderDao->shouldReceive(
'isFolderAccessible')
738 ->withArgs(array($folderId, $this->userId))->andReturn(
true);
739 $this->folderDao->shouldReceive(
'isFolderAccessible')
740 ->withArgs(array(
"$parentId", $this->userId))->andReturn(
false);
741 $requestHeaders =
new Headers();
742 $requestHeaders->setHeader(
'parent', $parentId);
743 $requestHeaders->setHeader(
'action',
"copy");
744 $body = $this->streamFactory->createStream();
745 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
746 $requestHeaders, [], [], $body);
747 $response =
new ResponseHelper();
748 $this->expectException(HttpForbiddenException::class);
750 $this->folderController->copyFolder($request, $response,
751 [
"id" => $folderId]);
759 public function testCopyFolderInvalidAction()
764 $this->folderDao->shouldReceive(
'getFolder')
765 ->andReturnUsing([$this,
'getFolder']);
766 $this->folderDao->shouldReceive(
'isFolderAccessible')
767 ->withArgs(array(M::anyOf($folderId,
"$parentId"),
768 $this->userId))->andReturn(
true);
769 $requestHeaders =
new Headers();
770 $requestHeaders->setHeader(
'parent', $parentId);
771 $requestHeaders->setHeader(
'action',
"somethingrandom");
772 $body = $this->streamFactory->createStream();
773 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
774 $requestHeaders, [], [], $body);
775 $response =
new ResponseHelper();
776 $this->expectException(HttpBadRequestException::class);
778 $this->folderController->copyFolder($request, $response,
779 [
"id" => $folderId]);
Controller for Folder 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.
FolderGetName($FolderPk, $Top=-1)
Given a folder_pk, return the full path to this folder.
GetFolderArray($RootFolder, &$FolderArray)
Get an array of all the folders from a $RootFolder on down.
Folder2Path($folder_pk)
Return an array of folder_pk, folder_name from the users.root_folder_fk to $folder_pk.