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)];
66 use Psr\Container\ContainerInterface;
67 use Slim\Psr7\Factory\StreamFactory;
68 use Slim\Psr7\Headers;
69 use Slim\Psr7\Request;
76 class FolderControllerTest
extends \PHPUnit\Framework\TestCase
98 private $folderController;
108 private $folderPlugin;
113 private $deletePlugin;
118 private $folderPropertiesPlugin;
123 private $folderContentPlugin;
129 private $assertCountBefore;
136 private $streamFactory;
140 private $folderContents;
147 protected function setUp() : void
151 $container = M::mock(
'ContainerBuilder');
152 $this->dbHelper = M::mock(DbHelper::class);
153 $this->restHelper = M::mock(RestHelper::class);
154 $this->folderDao = M::mock(FolderDao::class);
155 $this->folderPlugin = M::mock(
'folder_create');
156 $this->deletePlugin = M::mock(
'admin_folder_delete');
157 $this->folderPropertiesPlugin = M::mock(
'folder_properties');
158 $this->folderContentPlugin = M::mock(
'content_move');
159 $this->folderContents = M::mock(
'foldercontents');
160 $this->container = M::mock(ContainerInterface::class);
162 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
163 $this->restHelper->shouldReceive(
'getFolderDao')->andReturn($this->folderDao);
164 $this->restHelper->shouldReceive(
'getUserId')->andReturn($this->userId);
165 $this->restHelper->shouldReceive(
'getPlugin')
166 ->withArgs(array(
'folder_create'))->andReturn($this->folderPlugin);
167 $this->restHelper->shouldReceive(
'getPlugin')
168 ->withArgs(array(
'admin_folder_delete'))->andReturn($this->deletePlugin);
169 $this->restHelper->shouldReceive(
'getPlugin')
170 ->withArgs(array(
'folder_properties'))
171 ->andReturn($this->folderPropertiesPlugin);
172 $this->restHelper->shouldReceive(
"getPlugin")
173 ->withArgs(array(
"foldercontents"))
174 ->andReturn($this->folderContentPlugin);
175 $this->restHelper->shouldReceive(
'getPlugin')
176 ->withArgs(array(
'content_move'))
177 ->andReturn($this->folderContentPlugin);
179 $container->shouldReceive(
'get')->withArgs(array(
180 'helper.restHelper'))->andReturn($this->restHelper);
181 $this->folderController =
new FolderController($container);
182 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
183 $this->streamFactory =
new StreamFactory();
190 protected function tearDown() : void
192 $this->addToAssertionCount(
193 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
203 public function getFolder($id)
207 case 2: $name =
"root";
break;
208 case 3: $name =
"root-child1";
break;
209 case 4: $name =
"root-child2";
break;
210 case -1:
return null;
211 default: $name =
"singlefolder";
213 return new Folder($id, $name,
"", 1);
222 public function getFolderParent($id)
225 if ($id == 3 || $id == 4) {
239 private function getResponseJson($response)
241 $response->getBody()->seek(0);
242 return json_decode($response->getBody()->getContents(),
true);
251 public function testGetAllFolders()
253 $rootFolder =
new Folder(2,
"root",
"", 2);
254 $this->folderDao->shouldReceive(
'getRootFolder')->withArgs(array(2))
255 ->andReturn($rootFolder);
256 $this->folderDao->shouldReceive(
'getFolder')
257 ->andReturnUsing([$this,
'getFolder']);
258 $this->folderDao->shouldReceive(
'getFolderParentId')
259 ->andReturnUsing([$this,
'getFolderParent']);
260 $expectedFoldersList = [];
261 for ($i = 2; $i <= 4; $i ++) {
262 $folder = $this->getFolder($i);
263 $parentId = $this->getFolderParent($i);
264 $folderModel = new \Fossology\UI\Api\Models\Folder($folder->getId(),
265 $folder->getName(), $folder->getDescription(), $parentId);
266 $expectedFoldersList[] = $folderModel->getArray();
268 $actualResponse = $this->folderController->getFolders(
null,
269 new ResponseHelper(), []);
270 $this->assertEquals(200, $actualResponse->getStatusCode());
271 $this->assertEquals($expectedFoldersList,
272 $this->getResponseJson($actualResponse));
280 public function testGetAllFoldersNotFound()
283 $rootFolder =
new Folder(2,
"root",
"", 2);
284 $this->folderDao->shouldReceive(
'getRootFolder')->withArgs(array(2))
285 ->andReturn($rootFolder);
286 $this->folderDao->shouldReceive(
'getFolder')
287 ->andReturnUsing([$this,
'getFolder']);
288 $this->folderDao->shouldReceive(
'isFolderAccessible')->withArgs([$folderId])->andReturn(
false);
289 $this->folderDao->shouldReceive(
'getFolderParentId')
290 ->andReturnUsing([$this,
'getFolderParent']);
291 $this->expectException(HttpForbiddenException::class);
292 $this->folderController->getFolders(
null,
293 new ResponseHelper(), [
"id"=>1]);
303 public function testGetSpecificFolders()
306 $this->folderDao->shouldReceive(
'isFolderAccessible')
307 ->withArgs(array($folderId))->andReturn(
true);
308 $this->folderDao->shouldReceive(
'getFolder')
309 ->andReturnUsing([$this,
'getFolder']);
310 $this->folderDao->shouldReceive(
'getFolderParentId')
311 ->andReturnUsing([$this,
'getFolderParent']);
312 $folder = $this->getFolder($folderId);
313 $parentId = $this->getFolderParent($folderId);
314 $folderModel = new \Fossology\UI\Api\Models\Folder($folder->getId(),
315 $folder->getName(), $folder->getDescription(), $parentId);
316 $expectedFoldersList = $folderModel->getArray();
317 $actualResponse = $this->folderController->getFolders(
null,
318 new ResponseHelper(), [
'id' => $folderId]);
319 $this->assertEquals(200, $actualResponse->getStatusCode());
320 $this->assertEquals($expectedFoldersList,
321 $this->getResponseJson($actualResponse));
331 public function testGetInvalidFolder()
334 $this->folderDao->shouldReceive(
'isFolderAccessible')
335 ->withArgs(array($folderId))->andReturn(
false);
336 $this->folderDao->shouldReceive(
'getFolder')
337 ->andReturnUsing([$this,
'getFolder']);
338 $this->expectException(HttpForbiddenException::class);
340 $this->folderController->getFolders(
null,
new ResponseHelper(),
341 [
'id' => $folderId]);
350 public function testGetInAccessibleFolder()
353 $this->folderDao->shouldReceive(
'isFolderAccessible')
354 ->withArgs(array($folderId))->andReturn(
false);
355 $this->folderDao->shouldReceive(
'getFolder')
356 ->andReturnUsing([$this,
'getFolder']);
357 $this->expectException(HttpForbiddenException::class);
359 $this->folderController->getFolders(
null,
new ResponseHelper(),
360 [
'id' => $folderId]);
368 public function testCreateFolderV1()
370 $this->testCreateFolder(ApiVersion::V1);
377 public function testCreateFolderV2()
379 $this->testCreateFolder();
386 private function testCreateFolder($version = ApiVersion::V2)
389 $folderName =
"root-child1";
390 $folderDescription =
"Description";
392 $this->folderDao->shouldReceive(
'isFolderAccessible')
393 ->withArgs(array($parentFolder, $this->userId))->andReturn(
true);
394 $this->folderPlugin->shouldReceive(
'create')
395 ->withArgs(array($parentFolder, $folderName, $folderDescription))
397 $this->folderDao->shouldReceive(
'getFolderId')
398 ->withArgs(array($folderName, $parentFolder))->andReturn($folderId);
399 $requestHeaders =
new Headers();
400 $body = $this->streamFactory->createStream();
401 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
402 $requestHeaders, [], [], $body);
403 if ($version == ApiVersion::V2) {
404 $request = $request->withQueryParams([
'parentFolder'=>$parentFolder,
'folderName'=>$folderName,
'folderDescription'=>$folderDescription]);
406 $request = $request->withHeader(
'parentFolder', $parentFolder)
407 ->withHeader(
'folderName', $folderName)
408 ->withHeader(
'folderDescription', $folderDescription);
410 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
411 $response =
new ResponseHelper();
412 $actualResponse = $this->folderController->createFolder($request,
414 $expectedResponse =
new Info(201, $folderId, InfoType::INFO);
415 $this->assertEquals($expectedResponse->getCode(),
416 $actualResponse->getStatusCode());
417 $this->assertEquals($expectedResponse->getArray(),
418 $this->getResponseJson($actualResponse));
419 $this->assertEquals($expectedResponse->getArray(),$this->getResponseJson($actualResponse));
427 public function testCreateFolderInvalidBody()
429 $requestHeaders =
new Headers();
430 $body = $this->streamFactory->createStream(
"invalid_json");
431 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
432 $requestHeaders, [], [], $body);
433 $response =
new ResponseHelper();
435 $this->expectException(HttpBadRequestException::class);
437 $this->folderController->createFolder($request, $response, []);
445 public function testCreateFolderParentNotAccessibleV1()
447 $this->testCreateFolderParentNotAccessible(ApiVersion::V1);
454 public function testCreateFolderParentNotAccessibleV2()
456 $this->testCreateFolderParentNotAccessible();
463 private function testCreateFolderParentNotAccessible($version = ApiVersion::V2)
466 $folderName =
"root-child1";
467 $folderDescription =
"Description";
468 $this->folderDao->shouldReceive(
'isFolderAccessible')
469 ->withArgs(array($parentFolder, $this->userId))->andReturn(
false);
470 $requestHeaders =
new Headers();
471 $body = $this->streamFactory->createStream();
472 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
473 $requestHeaders, [], [], $body);
474 if ($version == ApiVersion::V2) {
475 $request = $request->withQueryParams([
'parentFolder'=>$parentFolder,
'folderName'=>$folderName,
'folderDescription'=>$folderDescription]);
477 $request = $request->withHeader(
'parentFolder', $parentFolder)
478 ->withHeader(
'folderName', $folderName)
479 ->withHeader(
'folderDescription', $folderDescription);
481 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
482 $response =
new ResponseHelper();
483 $this->expectException(HttpForbiddenException::class);
484 $this->folderController->createFolder($request, $response, []);
493 public function testCreateFolderDuplicateNamesV1()
495 $this->testCreateFolderDuplicateNames(ApiVersion::V1);
503 public function testCreateFolderDuplicateNamesV2()
505 $this->testCreateFolderDuplicateNames();
511 private function testCreateFolderDuplicateNames($version = ApiVersion::V2)
514 $folderName =
"root-child1";
515 $folderDescription =
"Description";
516 $this->folderDao->shouldReceive(
'isFolderAccessible')
517 ->withArgs(array($parentFolder, $this->userId))->andReturn(
true);
518 $this->folderPlugin->shouldReceive(
'create')
519 ->withArgs(array($parentFolder, $folderName, $folderDescription))
521 $requestHeaders =
new Headers();
522 $requestHeaders->setHeader(
'parentFolder', $parentFolder);
523 $requestHeaders->setHeader(
'folderName', $folderName);
524 $requestHeaders->setHeader(
'folderDescription', $folderDescription);
525 $body = $this->streamFactory->createStream();
526 $request =
new Request(
"POST",
new Uri(
"HTTP",
"localhost"),
527 $requestHeaders, [], [], $body);
528 if ($version == ApiVersion::V2) {
529 $request = $request->withQueryParams([
'parentFolder'=>$parentFolder,
'folderName'=>$folderName,
'folderDescription'=>$folderDescription]);
531 $request = $request->withHeader(
'parentFolder', $parentFolder)
532 ->withHeader(
'folderName', $folderName)
533 ->withHeader(
'folderDescription', $folderDescription);
535 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
536 $response =
new ResponseHelper();
537 $actualResponse = $this->folderController->createFolder($request,
539 $expectedResponse =
new Info(200,
"Folder $folderName already exists!",
541 $this->assertEquals($expectedResponse->getCode(),
542 $actualResponse->getStatusCode());
543 $this->assertEquals($expectedResponse->getArray(),
544 $this->getResponseJson($actualResponse));
552 public function testDeleteFolder()
556 $this->folderDao->shouldReceive(
'getFolder')
557 ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
558 $this->deletePlugin->shouldReceive(
'Delete')
559 ->withArgs(array(
"2 $folderId", $this->userId))->andReturnNull();
560 $actualResponse = $this->folderController->deleteFolder(
null,
561 new ResponseHelper(), [
"id" => $folderId]);
562 $expectedResponse =
new Info(202,
"Folder, \"$folderName\" deleted.",
564 $this->assertEquals($expectedResponse->getCode(),
565 $actualResponse->getStatusCode());
566 $this->assertEquals($expectedResponse->getArray(),
567 $this->getResponseJson($actualResponse));
575 public function testDeleteFolderInvalidFolder()
578 $this->folderDao->shouldReceive(
'getFolder')
579 ->withArgs(array($folderId))->andReturnNull();
580 $this->expectException(HttpNotFoundException::class);
582 $this->folderController->deleteFolder(
null,
new ResponseHelper(),
583 [
"id" => $folderId]);
591 public function testDeleteFolderNoAccess()
595 $errorText =
"No access to delete this folder";
596 $this->folderDao->shouldReceive(
'getFolder')
597 ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
598 $this->deletePlugin->shouldReceive(
'Delete')
599 ->withArgs(array(
"2 $folderId", $this->userId))
600 ->andReturn($errorText);
601 $this->expectException(HttpForbiddenException::class);
603 $this->folderController->deleteFolder(
null,
new ResponseHelper(),
604 [
"id" => $folderId]);
612 public function testEditFolderV1()
614 $this->testEditFolder(APiVersion::V1);
621 public function testEditFolderV2()
623 $this->testEditFolder();
630 private function testEditFolder($version = ApiVersion::V2)
633 $folderName =
"new name";
634 $folderDescription =
"new description";
635 $this->folderDao->shouldReceive(
'getFolder')
636 ->andReturnUsing([$this,
'getFolder']);
637 $this->folderDao->shouldReceive(
'isFolderAccessible')
638 ->withArgs(array($folderId, $this->userId))->andReturn(
true);
639 $this->folderPropertiesPlugin->shouldReceive(
'Edit')
640 ->withArgs(array($folderId, $folderName, $folderDescription));
641 $requestHeaders =
new Headers();
642 $body = $this->streamFactory->createStream();
643 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost"),
644 $requestHeaders, [], [], $body);
645 if ($version == ApiVersion::V2) {
646 $request = $request->withQueryParams([
'name'=> $folderName,
'description'=>$folderDescription]);
648 $request = $request->withHeader(
'name', $folderName)
649 ->withHeader(
'description', $folderDescription);
651 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
652 $response =
new ResponseHelper();
653 $actualResponse = $this->folderController->editFolder($request,
654 $response, [
"id" => $folderId]);
655 $expectedResponse =
new Info(200,
'Folder "' . \Fossology\UI\Api\Controllers\
FolderGetName($folderId) .
656 '" updated.', InfoType::INFO);
657 $this->assertEquals($expectedResponse->getCode(),
658 $actualResponse->getStatusCode());
659 $this->assertEquals($expectedResponse->getArray(),
660 $this->getResponseJson($actualResponse));
668 public function testEditFolderNotExistsV1()
670 $this->testEditFolderNotExists(ApiVersion::V1);
677 public function testEditFolderNotExistsV2()
679 $this->testEditFolderNotExists();
681 private function testEditFolderNotExists($version = ApiVersion::V2)
684 $folderName =
"new name";
685 $folderDescription =
"new description";
686 $this->folderDao->shouldReceive(
'getFolder')->andReturnNull();
687 $requestHeaders =
new Headers();
688 $requestHeaders->setHeader(
'name', $folderName);
689 $requestHeaders->setHeader(
'description', $folderDescription);
690 $body = $this->streamFactory->createStream();
691 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost"),
692 $requestHeaders, [], [], $body);
693 if ($version == ApiVersion::V2) {
694 $request = $request->withQueryParams([
'name'=> $folderName,
'description'=>$folderDescription]);
696 $request = $request->withHeader(
'name', $folderName)
697 ->withHeader(
'description', $folderDescription);
699 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
700 $response =
new ResponseHelper();
701 $this->expectException(HttpNotFoundException::class);
703 $this->folderController->editFolder($request, $response,
704 [
"id" => $folderId]);
711 public function testEditFolderNotAccessibleV1()
713 $this->testEditFolderNotAccessible(APIVersion::V2);
720 public function testEditFolderNotAccessibleV2()
722 $this->testEditFolderNotAccessible();
728 private function testEditFolderNotAccessible($version = ApiVersion::V2)
731 $folderName =
"new name";
732 $folderDescription =
"new description";
733 $this->folderDao->shouldReceive(
'getFolder')
734 ->andReturnUsing([$this,
'getFolder']);
735 $this->folderDao->shouldReceive(
'isFolderAccessible')
736 ->withArgs(array($folderId, $this->userId))->andReturn(
false);
737 $requestHeaders =
new Headers();
738 $requestHeaders->setHeader(
'name', $folderName);
739 $requestHeaders->setHeader(
'description', $folderDescription);
740 $body = $this->streamFactory->createStream();
741 $request =
new Request(
"PATCH",
new Uri(
"HTTP",
"localhost"),
742 $requestHeaders, [], [], $body);
743 if ($version == ApiVersion::V2) {
744 $request = $request->withQueryParams([
'name'=> $folderName,
'description'=>$folderDescription]);
746 $request = $request->withHeader(
'name', $folderName)
747 ->withHeader(
'description', $folderDescription);
749 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
750 $response =
new ResponseHelper();
751 $this->expectException(HttpForbiddenException::class);
753 $this->folderController->editFolder($request, $response,
754 [
"id" => $folderId]);
762 public function testCopyFolderV1()
764 $this->testCopyFolder(APiVersion::V1);
771 public function testCopyFolderV2()
773 $this->testCopyFolder();
779 private function testCopyFolder($version = ApiVersion::V2)
783 $folderContentPk = 5;
787 $this->folderDao->shouldReceive(
'getFolder')
788 ->andReturnUsing([$this,
'getFolder']);
789 $this->folderDao->shouldReceive(
'isFolderAccessible')
790 ->withArgs(array(M::anyOf($folderId,
"$parentId"),
791 $this->userId))->andReturn(
true);
792 $this->folderDao->shouldReceive(
'isFolderAccessible')
793 ->withArgs([$parentId,
794 $this->userId])->andReturn(
true);
795 $this->folderDao->shouldReceive(
'getFolderContentsId')
796 ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
797 $this->folderContentPlugin->shouldReceive(
'copyContent')
798 ->withArgs(array([$folderContentPk], $parentId,
true))->andReturn(
"");
800 $requestHeaders =
new Headers();
801 $body = $this->streamFactory->createStream();
802 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
803 $requestHeaders, [], [], $body);
804 if ($version == ApiVersion::V2) {
805 $request = $request->withQueryParams([
'parent'=> $parentId,
'action'=>
"copy"]);
807 $request = $request->withHeader(
'parent', $parentId)
808 ->withHeader(
'action',
"copy");
810 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
811 $response =
new ResponseHelper();
813 $actualResponse = $this->folderController->copyFolder($request,
814 $response, [
"id" => $folderId]);
815 $expectedResponse =
new Info(202,
816 "Folder \"$folderName\" copy(ed) under \"$parentFolderName\".",
818 $this->assertEquals($expectedResponse->getCode(),
819 $actualResponse->getStatusCode());
820 $this->assertEquals($expectedResponse->getArray(),
821 $this->getResponseJson($actualResponse));
829 public function testMoveFolderV1()
831 $this->testMoveFolder(ApiVersion::V1);
838 public function testMoveFolderV2()
840 $this->testMoveFolder();
847 private function testMoveFolder($version = ApiVersion::V2)
851 $folderContentPk = 5;
855 $this->folderDao->shouldReceive(
'getFolder')
856 ->andReturnUsing([$this,
'getFolder']);
857 $this->folderDao->shouldReceive(
'isFolderAccessible')
858 ->withArgs(array(M::anyOf($folderId,
"$parentId"),
859 $this->userId))->andReturn(
true);
860 $this->folderDao->shouldReceive(
'isFolderAccessible')
861 ->withArgs([$parentId, $this->userId])->andReturn(
true);
862 $this->folderDao->shouldReceive(
'getFolderContentsId')
863 ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
864 $this->folderContentPlugin->shouldReceive(
'copyContent')
865 ->withArgs(array([$folderContentPk], $parentId,
false))->andReturn(
"");
866 $requestHeaders =
new Headers();
867 $body = $this->streamFactory->createStream();
868 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
869 $requestHeaders, [], [], $body);
870 $response =
new ResponseHelper();
871 if ($version == ApiVersion::V2) {
872 $request = $request->withQueryParams([
'parent'=>$parentId,
'action'=>
'move']);
874 $request = $request->withHeader(
'parent', $parentId)
875 ->withHeader(
'action',
"move");
877 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
878 $actualResponse = $this->folderController->copyFolder($request,
879 $response, [
"id" => $folderId]);
880 $expectedResponse =
new Info(202,
881 "Folder \"$folderName\" move(ed) under \"$parentFolderName\".",
883 $this->assertEquals($expectedResponse->getCode(),
884 $actualResponse->getStatusCode());
885 $this->assertEquals($expectedResponse->getArray(),
886 $this->getResponseJson($actualResponse));
894 public function testCopyFolderNotFoundV1()
896 $this->testCopyFolderNotFound(APiVersion::V2);
903 public function testCopyFolderNotFoundV2()
905 $this->testCopyFolderNotFound();
907 private function testCopyFolderNotFound($version = ApiVersion::V2)
912 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($folderId))
914 $requestHeaders =
new Headers();
915 $body = $this->streamFactory->createStream();
916 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
917 $requestHeaders, [], [], $body);
918 if ($version == ApiVersion::V2) {
919 $request = $request->withQueryParams([
'parent'=>$parentId,
'action'=>
'copy']);
921 $request = $request->withHeader(
'parent', $parentId)
922 ->withHeader(
'action',
"copy");
924 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
925 $response =
new ResponseHelper();
926 $this->expectException(HttpNotFoundException::class);
928 $this->folderController->copyFolder($request, $response,
929 [
"id" => $folderId]);
937 public function testCopyParentFolderNotFoundV1()
939 $this->testCopyParentFolderNotFound(APiVersion::V1);
946 public function testCopyParentFolderNotFoundV2()
948 $this->testCopyParentFolderNotFound();
950 private function testCopyParentFolderNotFound($version = ApiVersion::V2)
955 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($folderId))
956 ->andReturn($this->getFolder($folderId));
957 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($parentId))
959 $requestHeaders =
new Headers();
960 $requestHeaders->setHeader(
'parent', $parentId);
961 $requestHeaders->setHeader(
'action',
"copy");
962 $body = $this->streamFactory->createStream();
963 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
964 $requestHeaders, [], [], $body);
965 if ($version == ApiVersion::V2) {
966 $request = $request->withQueryParams([
'parent'=>$parentId,
'action'=>
'copy']);
968 $request = $request->withHeader(
'parent', $parentId)
969 ->withHeader(
'action',
"copy");
971 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
972 $response =
new ResponseHelper();
973 $this->expectException(HttpNotFoundException::class);
975 $this->folderController->copyFolder($request, $response,
976 [
"id" => $folderId]);
984 public function testCopyFolderNotAccessibleV1()
986 $this->testCopyFolderNotAccessible(ApiVersion::V1);
993 public function testCopyFolderNotAccessibleV2()
995 $this->testCopyFolderNotAccessible();
1002 private function testCopyFolderNotAccessible($version = ApiVersion::V2)
1007 $this->folderDao->shouldReceive(
'getFolder')
1008 ->andReturnUsing([$this,
'getFolder']);
1009 $this->folderDao->shouldReceive(
'isFolderAccessible')
1010 ->withArgs(array($folderId, $this->userId))->andReturn(
false);
1011 $requestHeaders =
new Headers();
1012 $body = $this->streamFactory->createStream();
1013 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
1014 $requestHeaders, [], [], $body);
1015 if ($version == ApiVersion::V2) {
1016 $request = $request->withQueryParams([
'parent'=>$parentId,
'action'=>
'copy']);
1018 $request = $request->withHeader(
'parent', $parentId)
1019 ->withHeader(
'action',
"copy");
1021 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1022 $response =
new ResponseHelper();
1023 $this->expectException(HttpForbiddenException::class);
1025 $this->folderController->copyFolder($request, $response,
1026 [
"id" => $folderId]);
1034 public function testCopyParentFolderNotAccessibleV1()
1036 $this->testCopyParentFolderNotAccessible(ApiVersion::V1);
1043 public function testCopyParentFolderNotAccessibleV2()
1045 $this->testCopyParentFolderNotAccessible();
1047 private function testCopyParentFolderNotAccessible($version = ApiVersion::V2)
1052 $this->folderDao->shouldReceive(
'getFolder')
1053 ->andReturnUsing([$this,
'getFolder']);
1054 $this->folderDao->shouldReceive(
'isFolderAccessible')
1055 ->withArgs(array($folderId, $this->userId))->andReturn(
true);
1056 $this->folderDao->shouldReceive(
'isFolderAccessible')
1057 ->withArgs(array(
"$parentId", $this->userId))->andReturn(
false);
1058 $requestHeaders =
new Headers();
1059 $body = $this->streamFactory->createStream();
1060 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
1061 $requestHeaders, [], [], $body);
1062 if ($version == ApiVersion::V2) {
1063 $request = $request->withQueryParams([
'parent'=>$parentId,
'action'=>
'copy']);
1065 $request = $request->withHeader(
'parent', $parentId)
1066 ->withHeader(
'action',
"copy");
1068 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1069 $response =
new ResponseHelper();
1070 $this->expectException(HttpForbiddenException::class);
1072 $this->folderController->copyFolder($request, $response,
1073 [
"id" => $folderId]);
1081 public function testCopyFolderInvalidActionV1()
1083 $this->testCopyFolderInvalidAction(ApiVersion::V1);
1090 public function testCopyFolderInvalidActionV2()
1092 $this->testCopyFolderInvalidAction();
1100 private function testCopyFolderInvalidAction($version = ApiVersion::V2)
1105 $this->folderDao->shouldReceive(
'removeContent')->withArgs(array($contentId))->andReturn(
false);
1107 $this->folderDao->shouldReceive(
'getFolder')
1108 ->andReturnUsing([$this,
'getFolder']);
1109 $this->folderDao->shouldReceive(
'isFolderAccessible')
1110 ->withArgs(array(M::anyOf($folderId,
"$parentId"),
1111 $this->userId))->andReturn(
true);
1112 $this->folderDao->shouldReceive(
'isFolderAccessible')
1113 ->withArgs([$parentId,$this->userId])->andReturn(
true);
1114 $requestHeaders =
new Headers();
1115 $body = $this->streamFactory->createStream();
1116 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),
1117 $requestHeaders, [], [], $body);
1118 if ($version == ApiVersion::V2) {
1119 $request = $request->withQueryParams([
'parent'=>$parentId,
'action'=>
'somethingrandom']);
1121 $request = $request->withHeader(
'parent', $parentId)
1122 ->withHeader(
'action',
"somethingrandom");
1124 $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1125 $response =
new ResponseHelper();
1126 $this->expectException(HttpBadRequestException::class);
1128 $this->folderController->copyFolder($request, $response,
1129 [
"id" => $folderId]);
1138 public function testUnlinkFolderFolderNotFound()
1141 $this->dbHelper->shouldReceive(
"doesIdExist")->withArgs(array(
"foldercontents",
"foldercontents_pk", $contentId))->andReturn(
false);
1143 $requestHeaders =
new Headers();
1144 $body = $this->streamFactory->createStream();
1145 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),$requestHeaders, [],[],$body);
1146 $this->expectException(HttpNotFoundException::class);
1147 $this->folderController->unlinkFolder($request,
new ResponseHelper(),[
'contentId' => $contentId]);
1156 public function testUnlinkUnExistingFolder()
1159 $this->dbHelper->shouldReceive(
"doesIdExist")->withArgs(array(
"foldercontents",
"foldercontents_pk", $contentId))->andReturn(
false);
1161 $requestHeaders =
new Headers();
1162 $body = $this->streamFactory->createStream();
1163 $request =
new Request(
"PUT",
new Uri(
"HTTP",
"localhost"),$requestHeaders, [],[],$body);
1164 $this->expectException(HttpNotFoundException::class);
1165 $this->folderController->unlinkFolder(
null,
new ResponseHelper(),[
'contentId' => $contentId]);
1174 public function testGetAllFolderContents()
1177 $folder =
new Folder($folderId,
"Data",
"Folding for testing",1);
1178 $this->restHelper->shouldReceive(
'getUserId')->andReturn($this->userId);
1179 $this->folderDao->shouldReceive(
'isFolderAccessible')->andReturn(
true);
1180 $this->folderDao->shouldReceive(
'getFolder')->andReturn($folder);
1181 $this->folderContentPlugin->shouldReceive(
'handle')->andReturn([]);
1182 $this->folderDao->shouldReceive(
'getRemovableContents')->withArgs(array($folderId))->andReturn([]);
1184 $actualResponse = $this->folderController->getAllFolderContents(
null,
new ResponseHelper(),[
'id' => $folderId]);
1185 $this->assertEquals(200, $actualResponse->getStatusCode());
1193 public function testGetAllFolderContentsNotFound()
1196 $this->folderDao->shouldReceive(
'getFolder')->andReturn(
null);
1198 $this->expectException(HttpNotFoundException::class);
1199 $this->folderController->getAllFolderContents(
null,
new ResponseHelper(),[
'id' => $folderId]);
1207 public function testGetAllInAccessibleFolderContents()
1211 $folder =
new Folder($folderId,
"Data",
"Folding for testing",1);
1212 $this->folderDao->shouldReceive(
'isFolderAccessible')->andReturn(
false);
1213 $this->folderDao->shouldReceive(
'getFolder')->andReturn($folder);
1215 $this->expectException(HttpForbiddenException::class);
1216 $this->folderController->getAllFolderContents(
null,
new ResponseHelper(),[
'id' => $folderId]);
1224 public function testGetUnlinkableFolderContents()
1229 $folder =
new Folder($folderId,
"Data",
"Folding for testing",1);
1230 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($folderId))->andReturn($folder);
1231 $this->restHelper->shouldReceive(
"getUserId")->andReturn($userId);
1232 $this->folderDao->shouldReceive(
'isFolderAccessible')->withArgs(array($folderId, $this->restHelper->getUserId()))->andReturn(
true);
1233 $this->restHelper->shouldReceive(
'getFolderDao')->andReturn($this->folderDao);
1234 $this->folderContentPlugin->shouldReceive(
'handle');
1236 $actualResponse = $this->folderController->getUnlinkableFolderContents(
null,
new ResponseHelper(),[
"id" => $id]);
1237 var_dump($actualResponse);
1238 $this->assertEquals(200,$actualResponse->getStatusCode());
1246 public function testGetUnlinkableInaccessibleFolderContents()
1250 $folder =
new Folder($folderId,
"Data",
"Folding for testing",1);
1251 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($folderId))->andReturn($folder);
1252 $this->folderDao->shouldReceive(
'isFolderAccessible')->withArgs(array($folderId, $this->restHelper->getUserId()))->andReturn(
false);
1253 $this->restHelper->shouldReceive(
'getFolderDao')->andReturn($this->folderDao);
1254 $this->folderContentPlugin->shouldReceive(
'handle');
1256 $this->expectException(HttpForbiddenException::class);
1257 $this->folderController->getUnlinkableFolderContents(
null,
new ResponseHelper(),[
"id" => $id]);
1265 public function testGetUnlinkableFolderContentsBadRequest()
1269 $this->folderDao->shouldReceive(
'getFolder')->withArgs(array($folderId))->andReturn(
null);
1270 $this->folderDao->shouldReceive(
'isFolderAccessible')->withArgs(array($folderId, $this->restHelper->getUserId()))->andReturn(
false);
1271 $this->restHelper->shouldReceive(
'getFolderDao')->andReturn($this->folderDao);
1273 $this->expectException(HttpNotFoundException::class);
1274 $this->folderController->getUnlinkableFolderContents(
null,
new ResponseHelper(),[
"id" => $id]);
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.