FOSSology  4.7.1
Open Source License Compliance by Open Source Software
FolderControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2020 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
13 namespace Fossology\UI\Api\Controllers
14 {
15 
19  function GetFolderArray($a, &$b)
20  {
21  if ($a == 2) {
22  $b[2] = "root";
23  $b[3] = "root-child1";
24  $b[4] = "root-child2";
25  } else {
26  $b[$a] = "singlefolder";
27  }
28  }
29 
33  function FolderGetName($folderId)
34  {
35  return "$folderId-folder";
36  }
37 
41  function Folder2Path($folderId)
42  {
43  $folderList = array();
44  $folderList[] = ["folder_pk" => 2, "folder_name" => FolderGetName(2)];
45  $folderList[] = ["folder_pk" => 3, "folder_name" => FolderGetName(3)];
46  return $folderList;
47  }
48 }
49 
51 {
52 
65  use Mockery as M;
66  use Psr\Container\ContainerInterface;
67  use Slim\Psr7\Factory\StreamFactory;
68  use Slim\Psr7\Headers;
69  use Slim\Psr7\Request;
70  use Slim\Psr7\Uri;
71 
76  class FolderControllerTest extends \PHPUnit\Framework\TestCase
77  {
78 
83  private $dbHelper;
88  private $folderDao;
93  private $restHelper;
98  private $folderController;
103  private $userId;
108  private $folderPlugin;
113  private $deletePlugin;
118  private $folderPropertiesPlugin;
123  private $folderContentPlugin;
124 
129  private $assertCountBefore;
130 
136  private $streamFactory;
140  private $folderContents;
141  private $container;
142 
147  protected function setUp() : void
148  {
149  global $container;
150  $this->userId = 2;
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);
161 
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);
178 
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();
184  }
185 
190  protected function tearDown() : void
191  {
192  $this->addToAssertionCount(
193  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
194  M::close();
195  }
196 
203  public function getFolder($id)
204  {
205  $name = "";
206  switch ($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";
212  }
213  return new Folder($id, $name, "", 1);
214  }
215 
222  public function getFolderParent($id)
223  {
224  $pid = null;
225  if ($id == 3 || $id == 4) {
226  $pid = 2;
227  } elseif ($id > 4) {
228  $pid = $id - 1;
229  }
230  return $pid;
231  }
232 
239  private function getResponseJson($response)
240  {
241  $response->getBody()->seek(0);
242  return json_decode($response->getBody()->getContents(), true);
243  }
244 
251  public function testGetAllFolders()
252  {
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();
267  }
268  $actualResponse = $this->folderController->getFolders(null,
269  new ResponseHelper(), []);
270  $this->assertEquals(200, $actualResponse->getStatusCode());
271  $this->assertEquals($expectedFoldersList,
272  $this->getResponseJson($actualResponse));
273  }
274 
280  public function testGetAllFoldersNotFound()
281  {
282  $folderId = 1;
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, $this->userId])->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]);
294  }
295 
296 
297 
303  public function testGetSpecificFolders()
304  {
305  $folderId = 3;
306  $this->folderDao->shouldReceive('isFolderAccessible')
307  ->withArgs(array($folderId, $this->userId))->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));
322  }
323 
324 
331  public function testGetInvalidFolder()
332  {
333  $folderId = -1;
334  $this->folderDao->shouldReceive('isFolderAccessible')
335  ->withArgs(array($folderId, $this->userId))->andReturn(false);
336  $this->folderDao->shouldReceive('getFolder')
337  ->andReturnUsing([$this, 'getFolder']);
338  $this->expectException(HttpForbiddenException::class);
339 
340  $this->folderController->getFolders(null, new ResponseHelper(),
341  ['id' => $folderId]);
342  }
343 
350  public function testGetInAccessibleFolder()
351  {
352  $folderId = 3;
353  $this->folderDao->shouldReceive('isFolderAccessible')
354  ->withArgs(array($folderId, $this->userId))->andReturn(false);
355  $this->folderDao->shouldReceive('getFolder')
356  ->andReturnUsing([$this, 'getFolder']);
357  $this->expectException(HttpForbiddenException::class);
358 
359  $this->folderController->getFolders(null, new ResponseHelper(),
360  ['id' => $folderId]);
361  }
362 
368  public function testCreateFolderV1()
369  {
370  $this->testCreateFolder(ApiVersion::V1);
371  }
377  public function testCreateFolderV2()
378  {
379  $this->testCreateFolder();
380  }
381 
386  private function testCreateFolder($version = ApiVersion::V2)
387  {
388  $parentFolder = 2;
389  $folderName = "root-child1";
390  $folderDescription = "Description";
391  $folderId = 5;
392  $this->folderDao->shouldReceive('isFolderAccessible')
393  ->withArgs(array($parentFolder, $this->userId))->andReturn(true);
394  $this->folderPlugin->shouldReceive('create')
395  ->withArgs(array($parentFolder, $folderName, $folderDescription))
396  ->andReturn(1);
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]);
405  } else {
406  $request = $request->withHeader('parentFolder', $parentFolder)
407  ->withHeader('folderName', $folderName)
408  ->withHeader('folderDescription', $folderDescription);
409  }
410  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
411  $response = new ResponseHelper();
412  $actualResponse = $this->folderController->createFolder($request,
413  $response, []);
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));
420  }
421 
427  public function testCreateFolderInvalidBody()
428  {
429  $requestHeaders = new Headers();
430  $body = $this->streamFactory->createStream("invalid_json"); // Simulate invalid JSON
431  $request = new Request("POST", new Uri("HTTP", "localhost"),
432  $requestHeaders, [], [], $body);
433  $response = new ResponseHelper();
434 
435  $this->expectException(HttpBadRequestException::class);
436 
437  $this->folderController->createFolder($request, $response, []);
438  }
439 
445  public function testCreateFolderParentNotAccessibleV1()
446  {
447  $this->testCreateFolderParentNotAccessible(ApiVersion::V1);
448  }
454  public function testCreateFolderParentNotAccessibleV2()
455  {
456  $this->testCreateFolderParentNotAccessible();
457  }
458 
463  private function testCreateFolderParentNotAccessible($version = ApiVersion::V2)
464  {
465  $parentFolder = 2;
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]);
476  } else {
477  $request = $request->withHeader('parentFolder', $parentFolder)
478  ->withHeader('folderName', $folderName)
479  ->withHeader('folderDescription', $folderDescription);
480  }
481  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
482  $response = new ResponseHelper();
483  $this->expectException(HttpForbiddenException::class);
484  $this->folderController->createFolder($request, $response, []);
485  }
486 
493  public function testCreateFolderDuplicateNamesV1()
494  {
495  $this->testCreateFolderDuplicateNames(ApiVersion::V1);
496  }
503  public function testCreateFolderDuplicateNamesV2()
504  {
505  $this->testCreateFolderDuplicateNames();
506  }
511  private function testCreateFolderDuplicateNames($version = ApiVersion::V2)
512  {
513  $parentFolder = 2;
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))
520  ->andReturn(4);
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]);
530  } else {
531  $request = $request->withHeader('parentFolder', $parentFolder)
532  ->withHeader('folderName', $folderName)
533  ->withHeader('folderDescription', $folderDescription);
534  }
535  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
536  $response = new ResponseHelper();
537  $actualResponse = $this->folderController->createFolder($request,
538  $response, []);
539  $expectedResponse = new Info(200, "Folder $folderName already exists!",
540  InfoType::INFO);
541  $this->assertEquals($expectedResponse->getCode(),
542  $actualResponse->getStatusCode());
543  $this->assertEquals($expectedResponse->getArray(),
544  $this->getResponseJson($actualResponse));
545  }
546 
552  public function testDeleteFolder()
553  {
554  $folderId = 3;
555  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
556  $this->folderDao->shouldReceive('getFolder')
557  ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
558  $this->folderDao->shouldReceive('getRootFolder')
559  ->withArgs(array($this->userId))->andReturn($this->getFolder(2));
560  $this->deletePlugin->shouldReceive('Delete')
561  ->withArgs(array("2 $folderId", $this->userId))->andReturnNull();
562  $actualResponse = $this->folderController->deleteFolder(null,
563  new ResponseHelper(), ["id" => $folderId]);
564  $expectedResponse = new Info(202, "Folder, \"$folderName\" deleted.",
565  InfoType::INFO);
566  $this->assertEquals($expectedResponse->getCode(),
567  $actualResponse->getStatusCode());
568  $this->assertEquals($expectedResponse->getArray(),
569  $this->getResponseJson($actualResponse));
570  }
571 
577  public function testDeleteFolderInvalidFolder()
578  {
579  $folderId = 0;
580  $this->folderDao->shouldReceive('getFolder')
581  ->withArgs(array($folderId))->andReturnNull();
582  $this->expectException(HttpNotFoundException::class);
583 
584  $this->folderController->deleteFolder(null, new ResponseHelper(),
585  ["id" => $folderId]);
586  }
587 
593  public function testDeleteFolderNoAccess()
594  {
595 
596  $folderId = 3;
597  $errorText = "No access to delete this folder";
598  $this->folderDao->shouldReceive('getFolder')
599  ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
600  $this->folderDao->shouldReceive('getRootFolder')
601  ->withArgs(array($this->userId))->andReturn($this->getFolder(2));
602  $this->deletePlugin->shouldReceive('Delete')
603  ->withArgs(array("2 $folderId", $this->userId))
604  ->andReturn($errorText);
605  $this->expectException(HttpForbiddenException::class);
606 
607  $this->folderController->deleteFolder(null, new ResponseHelper(),
608  ["id" => $folderId]);
609  }
610 
616  public function testDeleteFolderRootFolder()
617  {
618  $folderId = 2;
619  $this->folderDao->shouldReceive('getFolder')
620  ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
621  $this->folderDao->shouldReceive('getRootFolder')
622  ->withArgs(array($this->userId))->andReturn($this->getFolder($folderId));
623  $this->deletePlugin->shouldNotReceive('Delete');
624  $this->expectException(HttpForbiddenException::class);
625 
626  $this->folderController->deleteFolder(null, new ResponseHelper(),
627  ["id" => $folderId]);
628  }
629 
635  public function testEditFolderV1()
636  {
637  $this->testEditFolder(APiVersion::V1);
638  }
644  public function testEditFolderV2()
645  {
646  $this->testEditFolder();
647  }
648 
653  private function testEditFolder($version = ApiVersion::V2)
654  {
655  $folderId = 3;
656  $folderName = "new name";
657  $folderDescription = "new description";
658  $this->folderDao->shouldReceive('getFolder')
659  ->andReturnUsing([$this, 'getFolder']);
660  $this->folderDao->shouldReceive('isFolderAccessible')
661  ->withArgs(array($folderId, $this->userId))->andReturn(true);
662  $this->folderPropertiesPlugin->shouldReceive('Edit')
663  ->withArgs(array($folderId, $folderName, $folderDescription));
664  $requestHeaders = new Headers();
665  $body = $this->streamFactory->createStream();
666  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
667  $requestHeaders, [], [], $body);
668  if ($version == ApiVersion::V2) {
669  $request = $request->withQueryParams(['name'=> $folderName, 'description'=>$folderDescription]);
670  } else {
671  $request = $request->withHeader('name', $folderName)
672  ->withHeader('description', $folderDescription);
673  }
674  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
675  $response = new ResponseHelper();
676  $actualResponse = $this->folderController->editFolder($request,
677  $response, ["id" => $folderId]);
678  $expectedResponse = new Info(200, 'Folder "' . \Fossology\UI\Api\Controllers\FolderGetName($folderId) .
679  '" updated.', InfoType::INFO);
680  $this->assertEquals($expectedResponse->getCode(),
681  $actualResponse->getStatusCode());
682  $this->assertEquals($expectedResponse->getArray(),
683  $this->getResponseJson($actualResponse));
684  }
685 
691  public function testEditFolderNotExistsV1()
692  {
693  $this->testEditFolderNotExists(ApiVersion::V1);
694  }
700  public function testEditFolderNotExistsV2()
701  {
702  $this->testEditFolderNotExists();
703  }
704  private function testEditFolderNotExists($version = ApiVersion::V2)
705  {
706  $folderId = 8;
707  $folderName = "new name";
708  $folderDescription = "new description";
709  $this->folderDao->shouldReceive('getFolder')->andReturnNull();
710  $requestHeaders = new Headers();
711  $requestHeaders->setHeader('name', $folderName);
712  $requestHeaders->setHeader('description', $folderDescription);
713  $body = $this->streamFactory->createStream();
714  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
715  $requestHeaders, [], [], $body);
716  if ($version == ApiVersion::V2) {
717  $request = $request->withQueryParams(['name'=> $folderName, 'description'=>$folderDescription]);
718  } else {
719  $request = $request->withHeader('name', $folderName)
720  ->withHeader('description', $folderDescription);
721  }
722  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
723  $response = new ResponseHelper();
724  $this->expectException(HttpNotFoundException::class);
725 
726  $this->folderController->editFolder($request, $response,
727  ["id" => $folderId]);
728  }
734  public function testEditFolderNotAccessibleV1()
735  {
736  $this->testEditFolderNotAccessible(APIVersion::V2);
737  }
743  public function testEditFolderNotAccessibleV2()
744  {
745  $this->testEditFolderNotAccessible();
746  }
751  private function testEditFolderNotAccessible($version = ApiVersion::V2)
752  {
753  $folderId = 3;
754  $folderName = "new name";
755  $folderDescription = "new description";
756  $this->folderDao->shouldReceive('getFolder')
757  ->andReturnUsing([$this, 'getFolder']);
758  $this->folderDao->shouldReceive('isFolderAccessible')
759  ->withArgs(array($folderId, $this->userId))->andReturn(false);
760  $requestHeaders = new Headers();
761  $requestHeaders->setHeader('name', $folderName);
762  $requestHeaders->setHeader('description', $folderDescription);
763  $body = $this->streamFactory->createStream();
764  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
765  $requestHeaders, [], [], $body);
766  if ($version == ApiVersion::V2) {
767  $request = $request->withQueryParams(['name'=> $folderName, 'description'=>$folderDescription]);
768  } else {
769  $request = $request->withHeader('name', $folderName)
770  ->withHeader('description', $folderDescription);
771  }
772  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
773  $response = new ResponseHelper();
774  $this->expectException(HttpForbiddenException::class);
775 
776  $this->folderController->editFolder($request, $response,
777  ["id" => $folderId]);
778  }
779 
785  public function testCopyFolderV1()
786  {
787  $this->testCopyFolder(APiVersion::V1);
788  }
794  public function testCopyFolderV2()
795  {
796  $this->testCopyFolder();
797  }
802  private function testCopyFolder($version = ApiVersion::V2)
803  {
804  $folderId = 3;
805  $parentId = 2;
806  $folderContentPk = 5;
807  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
808  $parentFolderName = \Fossology\UI\Api\Controllers\FolderGetName($parentId);
809 
810  $this->folderDao->shouldReceive('getFolder')
811  ->andReturnUsing([$this, 'getFolder']);
812  $this->folderDao->shouldReceive('isFolderAccessible')
813  ->withArgs(array(M::anyOf($folderId, "$parentId"),
814  $this->userId))->andReturn(true);
815  $this->folderDao->shouldReceive('isFolderAccessible')
816  ->withArgs([$parentId,
817  $this->userId])->andReturn(true);
818  $this->folderDao->shouldReceive('getFolderContentsId')
819  ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
820  $this->folderContentPlugin->shouldReceive('copyContent')
821  ->withArgs(array([$folderContentPk], $parentId, true))->andReturn("");
822 
823  $requestHeaders = new Headers();
824  $body = $this->streamFactory->createStream();
825  $request = new Request("PUT", new Uri("HTTP", "localhost"),
826  $requestHeaders, [], [], $body);
827  if ($version == ApiVersion::V2) {
828  $request = $request->withQueryParams(['parent'=> $parentId, 'action'=>"copy"]);
829  } else {
830  $request = $request->withHeader('parent', $parentId)
831  ->withHeader('action', "copy");
832  }
833  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
834  $response = new ResponseHelper();
835 
836  $actualResponse = $this->folderController->copyFolder($request,
837  $response, ["id" => $folderId]);
838  $expectedResponse = new Info(202,
839  "Folder \"$folderName\" copy(ed) under \"$parentFolderName\".",
840  InfoType::INFO);
841  $this->assertEquals($expectedResponse->getCode(),
842  $actualResponse->getStatusCode());
843  $this->assertEquals($expectedResponse->getArray(),
844  $this->getResponseJson($actualResponse));
845  }
846 
852  public function testMoveFolderV1()
853  {
854  $this->testMoveFolder(ApiVersion::V1);
855  }
861  public function testMoveFolderV2()
862  {
863  $this->testMoveFolder();
864  }
865 
870  private function testMoveFolder($version = ApiVersion::V2)
871  {
872  $folderId = 3;
873  $parentId = 2;
874  $folderContentPk = 5;
875  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
876  $parentFolderName = \Fossology\UI\Api\Controllers\FolderGetName($parentId);
877 
878  $this->folderDao->shouldReceive('getFolder')
879  ->andReturnUsing([$this, 'getFolder']);
880  $this->folderDao->shouldReceive('isFolderAccessible')
881  ->withArgs(array(M::anyOf($folderId, "$parentId"),
882  $this->userId))->andReturn(true);
883  $this->folderDao->shouldReceive('isFolderAccessible')
884  ->withArgs([$parentId, $this->userId])->andReturn(true);
885  $this->folderDao->shouldReceive('getFolderContentsId')
886  ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
887  $this->folderContentPlugin->shouldReceive('copyContent')
888  ->withArgs(array([$folderContentPk], $parentId, false))->andReturn("");
889  $requestHeaders = new Headers();
890  $body = $this->streamFactory->createStream();
891  $request = new Request("PUT", new Uri("HTTP", "localhost"),
892  $requestHeaders, [], [], $body);
893  $response = new ResponseHelper();
894  if ($version == ApiVersion::V2) {
895  $request = $request->withQueryParams(['parent'=>$parentId, 'action'=>'move']);
896  } else {
897  $request = $request->withHeader('parent', $parentId)
898  ->withHeader('action', "move");
899  }
900  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
901  $actualResponse = $this->folderController->copyFolder($request,
902  $response, ["id" => $folderId]);
903  $expectedResponse = new Info(202,
904  "Folder \"$folderName\" move(ed) under \"$parentFolderName\".",
905  InfoType::INFO);
906  $this->assertEquals($expectedResponse->getCode(),
907  $actualResponse->getStatusCode());
908  $this->assertEquals($expectedResponse->getArray(),
909  $this->getResponseJson($actualResponse));
910  }
911 
917  public function testCopyFolderNotFoundV1()
918  {
919  $this->testCopyFolderNotFound(APiVersion::V2);
920  }
926  public function testCopyFolderNotFoundV2()
927  {
928  $this->testCopyFolderNotFound();
929  }
930  private function testCopyFolderNotFound($version = ApiVersion::V2)
931  {
932  $folderId = 3;
933  $parentId = 2;
934 
935  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))
936  ->andReturnNull();
937  $requestHeaders = new Headers();
938  $body = $this->streamFactory->createStream();
939  $request = new Request("PUT", new Uri("HTTP", "localhost"),
940  $requestHeaders, [], [], $body);
941  if ($version == ApiVersion::V2) {
942  $request = $request->withQueryParams(['parent'=>$parentId, 'action'=>'copy']);
943  } else {
944  $request = $request->withHeader('parent', $parentId)
945  ->withHeader('action', "copy");
946  }
947  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
948  $response = new ResponseHelper();
949  $this->expectException(HttpNotFoundException::class);
950 
951  $this->folderController->copyFolder($request, $response,
952  ["id" => $folderId]);
953  }
954 
960  public function testCopyParentFolderNotFoundV1()
961  {
962  $this->testCopyParentFolderNotFound(APiVersion::V1);
963  }
969  public function testCopyParentFolderNotFoundV2()
970  {
971  $this->testCopyParentFolderNotFound();
972  }
973  private function testCopyParentFolderNotFound($version = ApiVersion::V2)
974  {
975  $folderId = 3;
976  $parentId = 2;
977 
978  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))
979  ->andReturn($this->getFolder($folderId));
980  $this->folderDao->shouldReceive('getFolder')->withArgs(array($parentId))
981  ->andReturnNull();
982  $requestHeaders = new Headers();
983  $requestHeaders->setHeader('parent', $parentId);
984  $requestHeaders->setHeader('action', "copy");
985  $body = $this->streamFactory->createStream();
986  $request = new Request("PUT", new Uri("HTTP", "localhost"),
987  $requestHeaders, [], [], $body);
988  if ($version == ApiVersion::V2) {
989  $request = $request->withQueryParams(['parent'=>$parentId, 'action'=>'copy']);
990  } else {
991  $request = $request->withHeader('parent', $parentId)
992  ->withHeader('action', "copy");
993  }
994  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
995  $response = new ResponseHelper();
996  $this->expectException(HttpNotFoundException::class);
997 
998  $this->folderController->copyFolder($request, $response,
999  ["id" => $folderId]);
1000  }
1001 
1007  public function testCopyFolderNotAccessibleV1()
1008  {
1009  $this->testCopyFolderNotAccessible(ApiVersion::V1);
1010  }
1016  public function testCopyFolderNotAccessibleV2()
1017  {
1018  $this->testCopyFolderNotAccessible();
1019  }
1020 
1025  private function testCopyFolderNotAccessible($version = ApiVersion::V2)
1026  {
1027  $folderId = 3;
1028  $parentId = 2;
1029 
1030  $this->folderDao->shouldReceive('getFolder')
1031  ->andReturnUsing([$this, 'getFolder']);
1032  $this->folderDao->shouldReceive('isFolderAccessible')
1033  ->withArgs(array($folderId, $this->userId))->andReturn(false);
1034  $requestHeaders = new Headers();
1035  $body = $this->streamFactory->createStream();
1036  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1037  $requestHeaders, [], [], $body);
1038  if ($version == ApiVersion::V2) {
1039  $request = $request->withQueryParams(['parent'=>$parentId, 'action'=>'copy']);
1040  } else {
1041  $request = $request->withHeader('parent', $parentId)
1042  ->withHeader('action', "copy");
1043  }
1044  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1045  $response = new ResponseHelper();
1046  $this->expectException(HttpForbiddenException::class);
1047 
1048  $this->folderController->copyFolder($request, $response,
1049  ["id" => $folderId]);
1050  }
1051 
1057  public function testCopyParentFolderNotAccessibleV1()
1058  {
1059  $this->testCopyParentFolderNotAccessible(ApiVersion::V1);
1060  }
1066  public function testCopyParentFolderNotAccessibleV2()
1067  {
1068  $this->testCopyParentFolderNotAccessible();
1069  }
1070  private function testCopyParentFolderNotAccessible($version = ApiVersion::V2)
1071  {
1072  $folderId = 3;
1073  $parentId = 2;
1074 
1075  $this->folderDao->shouldReceive('getFolder')
1076  ->andReturnUsing([$this, 'getFolder']);
1077  $this->folderDao->shouldReceive('isFolderAccessible')
1078  ->withArgs(array($folderId, $this->userId))->andReturn(true);
1079  $this->folderDao->shouldReceive('isFolderAccessible')
1080  ->withArgs(array("$parentId", $this->userId))->andReturn(false);
1081  $requestHeaders = new Headers();
1082  $body = $this->streamFactory->createStream();
1083  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1084  $requestHeaders, [], [], $body);
1085  if ($version == ApiVersion::V2) {
1086  $request = $request->withQueryParams(['parent'=>$parentId, 'action'=>'copy']);
1087  } else {
1088  $request = $request->withHeader('parent', $parentId)
1089  ->withHeader('action', "copy");
1090  }
1091  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1092  $response = new ResponseHelper();
1093  $this->expectException(HttpForbiddenException::class);
1094 
1095  $this->folderController->copyFolder($request, $response,
1096  ["id" => $folderId]);
1097  }
1098 
1104  public function testCopyFolderInvalidActionV1()
1105  {
1106  $this->testCopyFolderInvalidAction(ApiVersion::V1);
1107  }
1113  public function testCopyFolderInvalidActionV2()
1114  {
1115  $this->testCopyFolderInvalidAction();
1116  }
1117 
1118 
1123  private function testCopyFolderInvalidAction($version = ApiVersion::V2)
1124  {
1125  $folderId = 3;
1126  $parentId = 2;
1127  $contentId = 1;
1128  $this->folderDao->shouldReceive('removeContent')->withArgs(array($contentId))->andReturn(false);
1129 
1130  $this->folderDao->shouldReceive('getFolder')
1131  ->andReturnUsing([$this, 'getFolder']);
1132  $this->folderDao->shouldReceive('isFolderAccessible')
1133  ->withArgs(array(M::anyOf($folderId, "$parentId"),
1134  $this->userId))->andReturn(true);
1135  $this->folderDao->shouldReceive('isFolderAccessible')
1136  ->withArgs([$parentId,$this->userId])->andReturn(true);
1137  $requestHeaders = new Headers();
1138  $body = $this->streamFactory->createStream();
1139  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1140  $requestHeaders, [], [], $body);
1141  if ($version == ApiVersion::V2) {
1142  $request = $request->withQueryParams(['parent'=>$parentId, 'action'=>'somethingrandom']);
1143  } else {
1144  $request = $request->withHeader('parent', $parentId)
1145  ->withHeader('action', "somethingrandom");
1146  }
1147  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1148  $response = new ResponseHelper();
1149  $this->expectException(HttpBadRequestException::class);
1150 
1151  $this->folderController->copyFolder($request, $response,
1152  ["id" => $folderId]);
1153  }
1154 
1161  public function testUnlinkFolderFolderNotFound()
1162  {
1163  $contentId = 4;
1164  $this->dbHelper->shouldReceive("doesIdExist")->withArgs(array("foldercontents","foldercontents_pk", $contentId))->andReturn(false);
1165 
1166  $requestHeaders = new Headers();
1167  $body = $this->streamFactory->createStream();
1168  $request = new Request("PUT", new Uri("HTTP", "localhost"),$requestHeaders, [],[],$body);
1169  $this->expectException(HttpNotFoundException::class);
1170  $this->folderController->unlinkFolder($request, new ResponseHelper(),['contentId' => $contentId]);
1171 
1172  }
1173 
1179  public function testUnlinkUnExistingFolder()
1180  {
1181  $contentId = 4;
1182  $this->dbHelper->shouldReceive("doesIdExist")->withArgs(array("foldercontents","foldercontents_pk", $contentId))->andReturn(false);
1183 
1184  $requestHeaders = new Headers();
1185  $body = $this->streamFactory->createStream();
1186  $request = new Request("PUT", new Uri("HTTP", "localhost"),$requestHeaders, [],[],$body);
1187  $this->expectException(HttpNotFoundException::class);
1188  $this->folderController->unlinkFolder(null, new ResponseHelper(),['contentId' => $contentId]);
1189 
1190  }
1191 
1197  public function testUnlinkFolder()
1198  {
1199  $contentId = 4;
1200  $parentFolderId = 2;
1201  $this->dbHelper->shouldReceive("doesIdExist")
1202  ->withArgs(array("foldercontents", "foldercontents_pk", $contentId))
1203  ->andReturn(true);
1204  $this->folderDao->shouldReceive('getContent')
1205  ->withArgs(array($contentId))
1206  ->andReturn(["foldercontents_pk" => $contentId, "parent_fk" => $parentFolderId]);
1207  $this->folderDao->shouldReceive('isFolderAccessible')
1208  ->withArgs(array($parentFolderId, $this->userId))->andReturn(true);
1209  $this->folderDao->shouldReceive('removeContent')
1210  ->withArgs(array($contentId))->andReturn(true);
1211 
1212  $requestHeaders = new Headers();
1213  $body = $this->streamFactory->createStream();
1214  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1215  $requestHeaders, [], [], $body);
1216  $actualResponse = $this->folderController->unlinkFolder($request,
1217  new ResponseHelper(), ['contentId' => $contentId]);
1218  $expectedResponse = new Info(200, "Folder unlinked successfully.",
1219  InfoType::INFO);
1220  $this->assertEquals($expectedResponse->getCode(),
1221  $actualResponse->getStatusCode());
1222  $this->assertEquals($expectedResponse->getArray(),
1223  $this->getResponseJson($actualResponse));
1224  }
1225 
1233  public function testUnlinkFolderNotAccessible()
1234  {
1235  $contentId = 4;
1236  $parentFolderId = 3;
1237  $this->dbHelper->shouldReceive("doesIdExist")
1238  ->withArgs(array("foldercontents", "foldercontents_pk", $contentId))
1239  ->andReturn(true);
1240  $this->folderDao->shouldReceive('getContent')
1241  ->withArgs(array($contentId))
1242  ->andReturn(["foldercontents_pk" => $contentId, "parent_fk" => $parentFolderId]);
1243  $this->folderDao->shouldReceive('isFolderAccessible')
1244  ->withArgs(array($parentFolderId, $this->userId))->andReturn(false);
1245  $this->folderDao->shouldNotReceive('removeContent');
1246 
1247  $requestHeaders = new Headers();
1248  $body = $this->streamFactory->createStream();
1249  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1250  $requestHeaders, [], [], $body);
1251  $this->expectException(HttpForbiddenException::class);
1252  $this->folderController->unlinkFolder($request, new ResponseHelper(),
1253  ['contentId' => $contentId]);
1254  }
1255 
1262  public function testUnlinkFolderNotRemovable()
1263  {
1264  $contentId = 4;
1265  $parentFolderId = 2;
1266  $this->dbHelper->shouldReceive("doesIdExist")
1267  ->withArgs(array("foldercontents", "foldercontents_pk", $contentId))
1268  ->andReturn(true);
1269  $this->folderDao->shouldReceive('getContent')
1270  ->withArgs(array($contentId))
1271  ->andReturn(["foldercontents_pk" => $contentId, "parent_fk" => $parentFolderId]);
1272  $this->folderDao->shouldReceive('isFolderAccessible')
1273  ->withArgs(array($parentFolderId, $this->userId))->andReturn(true);
1274  $this->folderDao->shouldReceive('removeContent')
1275  ->withArgs(array($contentId))->andReturn(false);
1276 
1277  $requestHeaders = new Headers();
1278  $body = $this->streamFactory->createStream();
1279  $request = new Request("PUT", new Uri("HTTP", "localhost"),
1280  $requestHeaders, [], [], $body);
1281  $this->expectException(HttpBadRequestException::class);
1282  $this->folderController->unlinkFolder($request, new ResponseHelper(),
1283  ['contentId' => $contentId]);
1284  }
1285 
1291  public function testGetAllFolderContents()
1292  {
1293  $folderId = 2;
1294  $folder = new Folder($folderId,"Data","Folding for testing",1);
1295  $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId);
1296  $this->folderDao->shouldReceive('isFolderAccessible')->andReturn(true);
1297  $this->folderDao->shouldReceive('getFolder')->andReturn($folder);
1298  $this->folderContentPlugin->shouldReceive('handle')->andReturn([]);
1299  $this->folderDao->shouldReceive('getRemovableContents')->withArgs(array($folderId))->andReturn([]);
1300 
1301  $actualResponse = $this->folderController->getAllFolderContents(null,new ResponseHelper(),['id' => $folderId]);
1302  $this->assertEquals(200, $actualResponse->getStatusCode());
1303  }
1304 
1310  public function testGetAllFolderContentsNotFound()
1311  {
1312  $folderId = 2;
1313  $this->folderDao->shouldReceive('getFolder')->andReturn(null);
1314 
1315  $this->expectException(HttpNotFoundException::class);
1316  $this->folderController->getAllFolderContents(null,new ResponseHelper(),['id' => $folderId]);
1317  }
1318 
1324  public function testGetAllInAccessibleFolderContents()
1325  {
1326 
1327  $folderId = 2;
1328  $folder = new Folder($folderId,"Data","Folding for testing",1);
1329  $this->folderDao->shouldReceive('isFolderAccessible')->andReturn(false);
1330  $this->folderDao->shouldReceive('getFolder')->andReturn($folder);
1331 
1332  $this->expectException(HttpForbiddenException::class);
1333  $this->folderController->getAllFolderContents(null,new ResponseHelper(),['id' => $folderId]);
1334  }
1335 
1341  public function testGetUnlinkableFolderContents()
1342  {
1343  $folderId = 3;
1344  $id = 3;
1345  $userId = 2;
1346  $folder = new Folder($folderId,"Data","Folding for testing",1);
1347  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))->andReturn($folder);
1348  $this->restHelper->shouldReceive("getUserId")->andReturn($userId);
1349  $this->folderDao->shouldReceive('isFolderAccessible')->withArgs(array($folderId, $this->restHelper->getUserId()))->andReturn(true);
1350  $this->restHelper->shouldReceive('getFolderDao')->andReturn($this->folderDao);
1351  $this->folderContentPlugin->shouldReceive('handle');
1352 
1353  $actualResponse = $this->folderController->getUnlinkableFolderContents(null,new ResponseHelper(),["id" => $id]);
1354  var_dump($actualResponse);
1355  $this->assertEquals(200,$actualResponse->getStatusCode());
1356  }
1357 
1363  public function testGetUnlinkableInaccessibleFolderContents()
1364  {
1365  $folderId = 3;
1366  $id = 3;
1367  $folder = new Folder($folderId,"Data","Folding for testing",1);
1368  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))->andReturn($folder);
1369  $this->folderDao->shouldReceive('isFolderAccessible')->withArgs(array($folderId, $this->restHelper->getUserId()))->andReturn(false);
1370  $this->restHelper->shouldReceive('getFolderDao')->andReturn($this->folderDao);
1371  $this->folderContentPlugin->shouldReceive('handle');
1372 
1373  $this->expectException(HttpForbiddenException::class);
1374  $this->folderController->getUnlinkableFolderContents(null,new ResponseHelper(),["id" => $id]);
1375  }
1376 
1382  public function testGetUnlinkableFolderContentsBadRequest()
1383  {
1384  $folderId = 3;
1385  $id = 3;
1386  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))->andReturn(null);
1387  $this->folderDao->shouldReceive('isFolderAccessible')->withArgs(array($folderId, $this->restHelper->getUserId()))->andReturn(false);
1388  $this->restHelper->shouldReceive('getFolderDao')->andReturn($this->folderDao);
1389 
1390  $this->expectException(HttpNotFoundException::class);
1391  $this->folderController->getUnlinkableFolderContents(null,new ResponseHelper(),["id" => $id]);
1392 
1393  }
1394  }
1395 }
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
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.