FOSSology  4.4.0
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])->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))->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))->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))->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->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.",
563  InfoType::INFO);
564  $this->assertEquals($expectedResponse->getCode(),
565  $actualResponse->getStatusCode());
566  $this->assertEquals($expectedResponse->getArray(),
567  $this->getResponseJson($actualResponse));
568  }
569 
575  public function testDeleteFolderInvalidFolder()
576  {
577  $folderId = 0;
578  $this->folderDao->shouldReceive('getFolder')
579  ->withArgs(array($folderId))->andReturnNull();
580  $this->expectException(HttpNotFoundException::class);
581 
582  $this->folderController->deleteFolder(null, new ResponseHelper(),
583  ["id" => $folderId]);
584  }
585 
591  public function testDeleteFolderNoAccess()
592  {
593 
594  $folderId = 3;
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);
602 
603  $this->folderController->deleteFolder(null, new ResponseHelper(),
604  ["id" => $folderId]);
605  }
606 
612  public function testEditFolderV1()
613  {
614  $this->testEditFolder(APiVersion::V1);
615  }
621  public function testEditFolderV2()
622  {
623  $this->testEditFolder();
624  }
625 
630  private function testEditFolder($version = ApiVersion::V2)
631  {
632  $folderId = 3;
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]);
647  } else {
648  $request = $request->withHeader('name', $folderName)
649  ->withHeader('description', $folderDescription);
650  }
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));
661  }
662 
668  public function testEditFolderNotExistsV1()
669  {
670  $this->testEditFolderNotExists(ApiVersion::V1);
671  }
677  public function testEditFolderNotExistsV2()
678  {
679  $this->testEditFolderNotExists();
680  }
681  private function testEditFolderNotExists($version = ApiVersion::V2)
682  {
683  $folderId = 8;
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]);
695  } else {
696  $request = $request->withHeader('name', $folderName)
697  ->withHeader('description', $folderDescription);
698  }
699  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
700  $response = new ResponseHelper();
701  $this->expectException(HttpNotFoundException::class);
702 
703  $this->folderController->editFolder($request, $response,
704  ["id" => $folderId]);
705  }
711  public function testEditFolderNotAccessibleV1()
712  {
713  $this->testEditFolderNotAccessible(APIVersion::V2);
714  }
720  public function testEditFolderNotAccessibleV2()
721  {
722  $this->testEditFolderNotAccessible();
723  }
728  private function testEditFolderNotAccessible($version = ApiVersion::V2)
729  {
730  $folderId = 3;
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]);
745  } else {
746  $request = $request->withHeader('name', $folderName)
747  ->withHeader('description', $folderDescription);
748  }
749  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
750  $response = new ResponseHelper();
751  $this->expectException(HttpForbiddenException::class);
752 
753  $this->folderController->editFolder($request, $response,
754  ["id" => $folderId]);
755  }
756 
762  public function testCopyFolderV1()
763  {
764  $this->testCopyFolder(APiVersion::V1);
765  }
771  public function testCopyFolderV2()
772  {
773  $this->testCopyFolder();
774  }
779  private function testCopyFolder($version = ApiVersion::V2)
780  {
781  $folderId = 3;
782  $parentId = 2;
783  $folderContentPk = 5;
784  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
785  $parentFolderName = \Fossology\UI\Api\Controllers\FolderGetName($parentId);
786 
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("");
799 
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"]);
806  } else {
807  $request = $request->withHeader('parent', $parentId)
808  ->withHeader('action', "copy");
809  }
810  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
811  $response = new ResponseHelper();
812 
813  $actualResponse = $this->folderController->copyFolder($request,
814  $response, ["id" => $folderId]);
815  $expectedResponse = new Info(202,
816  "Folder \"$folderName\" copy(ed) under \"$parentFolderName\".",
817  InfoType::INFO);
818  $this->assertEquals($expectedResponse->getCode(),
819  $actualResponse->getStatusCode());
820  $this->assertEquals($expectedResponse->getArray(),
821  $this->getResponseJson($actualResponse));
822  }
823 
829  public function testMoveFolderV1()
830  {
831  $this->testMoveFolder(ApiVersion::V1);
832  }
838  public function testMoveFolderV2()
839  {
840  $this->testMoveFolder();
841  }
842 
847  private function testMoveFolder($version = ApiVersion::V2)
848  {
849  $folderId = 3;
850  $parentId = 2;
851  $folderContentPk = 5;
852  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
853  $parentFolderName = \Fossology\UI\Api\Controllers\FolderGetName($parentId);
854 
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']);
873  } else {
874  $request = $request->withHeader('parent', $parentId)
875  ->withHeader('action', "move");
876  }
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\".",
882  InfoType::INFO);
883  $this->assertEquals($expectedResponse->getCode(),
884  $actualResponse->getStatusCode());
885  $this->assertEquals($expectedResponse->getArray(),
886  $this->getResponseJson($actualResponse));
887  }
888 
894  public function testCopyFolderNotFoundV1()
895  {
896  $this->testCopyFolderNotFound(APiVersion::V2);
897  }
903  public function testCopyFolderNotFoundV2()
904  {
905  $this->testCopyFolderNotFound();
906  }
907  private function testCopyFolderNotFound($version = ApiVersion::V2)
908  {
909  $folderId = 3;
910  $parentId = 2;
911 
912  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))
913  ->andReturnNull();
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']);
920  } else {
921  $request = $request->withHeader('parent', $parentId)
922  ->withHeader('action', "copy");
923  }
924  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
925  $response = new ResponseHelper();
926  $this->expectException(HttpNotFoundException::class);
927 
928  $this->folderController->copyFolder($request, $response,
929  ["id" => $folderId]);
930  }
931 
937  public function testCopyParentFolderNotFoundV1()
938  {
939  $this->testCopyParentFolderNotFound(APiVersion::V1);
940  }
946  public function testCopyParentFolderNotFoundV2()
947  {
948  $this->testCopyParentFolderNotFound();
949  }
950  private function testCopyParentFolderNotFound($version = ApiVersion::V2)
951  {
952  $folderId = 3;
953  $parentId = 2;
954 
955  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))
956  ->andReturn($this->getFolder($folderId));
957  $this->folderDao->shouldReceive('getFolder')->withArgs(array($parentId))
958  ->andReturnNull();
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']);
967  } else {
968  $request = $request->withHeader('parent', $parentId)
969  ->withHeader('action', "copy");
970  }
971  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
972  $response = new ResponseHelper();
973  $this->expectException(HttpNotFoundException::class);
974 
975  $this->folderController->copyFolder($request, $response,
976  ["id" => $folderId]);
977  }
978 
984  public function testCopyFolderNotAccessibleV1()
985  {
986  $this->testCopyFolderNotAccessible(ApiVersion::V1);
987  }
993  public function testCopyFolderNotAccessibleV2()
994  {
995  $this->testCopyFolderNotAccessible();
996  }
997 
1002  private function testCopyFolderNotAccessible($version = ApiVersion::V2)
1003  {
1004  $folderId = 3;
1005  $parentId = 2;
1006 
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']);
1017  } else {
1018  $request = $request->withHeader('parent', $parentId)
1019  ->withHeader('action', "copy");
1020  }
1021  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1022  $response = new ResponseHelper();
1023  $this->expectException(HttpForbiddenException::class);
1024 
1025  $this->folderController->copyFolder($request, $response,
1026  ["id" => $folderId]);
1027  }
1028 
1034  public function testCopyParentFolderNotAccessibleV1()
1035  {
1036  $this->testCopyParentFolderNotAccessible(ApiVersion::V1);
1037  }
1043  public function testCopyParentFolderNotAccessibleV2()
1044  {
1045  $this->testCopyParentFolderNotAccessible();
1046  }
1047  private function testCopyParentFolderNotAccessible($version = ApiVersion::V2)
1048  {
1049  $folderId = 3;
1050  $parentId = 2;
1051 
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']);
1064  } else {
1065  $request = $request->withHeader('parent', $parentId)
1066  ->withHeader('action', "copy");
1067  }
1068  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1069  $response = new ResponseHelper();
1070  $this->expectException(HttpForbiddenException::class);
1071 
1072  $this->folderController->copyFolder($request, $response,
1073  ["id" => $folderId]);
1074  }
1075 
1081  public function testCopyFolderInvalidActionV1()
1082  {
1083  $this->testCopyFolderInvalidAction(ApiVersion::V1);
1084  }
1090  public function testCopyFolderInvalidActionV2()
1091  {
1092  $this->testCopyFolderInvalidAction();
1093  }
1094 
1095 
1100  private function testCopyFolderInvalidAction($version = ApiVersion::V2)
1101  {
1102  $folderId = 3;
1103  $parentId = 2;
1104  $contentId = 1;
1105  $this->folderDao->shouldReceive('removeContent')->withArgs(array($contentId))->andReturn(false);
1106 
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']);
1120  } else {
1121  $request = $request->withHeader('parent', $parentId)
1122  ->withHeader('action', "somethingrandom");
1123  }
1124  $request = $request->withAttribute(ApiVersion::ATTRIBUTE_NAME,$version);
1125  $response = new ResponseHelper();
1126  $this->expectException(HttpBadRequestException::class);
1127 
1128  $this->folderController->copyFolder($request, $response,
1129  ["id" => $folderId]);
1130  }
1131 
1138  public function testUnlinkFolderFolderNotFound()
1139  {
1140  $contentId = 4;
1141  $this->dbHelper->shouldReceive("doesIdExist")->withArgs(array("foldercontents","foldercontents_pk", $contentId))->andReturn(false);
1142 
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]);
1148 
1149  }
1150 
1156  public function testUnlinkUnExistingFolder()
1157  {
1158  $contentId = 4;
1159  $this->dbHelper->shouldReceive("doesIdExist")->withArgs(array("foldercontents","foldercontents_pk", $contentId))->andReturn(false);
1160 
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]);
1166 
1167  }
1168 
1174  public function testGetAllFolderContents()
1175  {
1176  $folderId = 2;
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([]);
1183 
1184  $actualResponse = $this->folderController->getAllFolderContents(null,new ResponseHelper(),['id' => $folderId]);
1185  $this->assertEquals(200, $actualResponse->getStatusCode());
1186  }
1187 
1193  public function testGetAllFolderContentsNotFound()
1194  {
1195  $folderId = 2;
1196  $this->folderDao->shouldReceive('getFolder')->andReturn(null);
1197 
1198  $this->expectException(HttpNotFoundException::class);
1199  $this->folderController->getAllFolderContents(null,new ResponseHelper(),['id' => $folderId]);
1200  }
1201 
1207  public function testGetAllInAccessibleFolderContents()
1208  {
1209 
1210  $folderId = 2;
1211  $folder = new Folder($folderId,"Data","Folding for testing",1);
1212  $this->folderDao->shouldReceive('isFolderAccessible')->andReturn(false);
1213  $this->folderDao->shouldReceive('getFolder')->andReturn($folder);
1214 
1215  $this->expectException(HttpForbiddenException::class);
1216  $this->folderController->getAllFolderContents(null,new ResponseHelper(),['id' => $folderId]);
1217  }
1218 
1224  public function testGetUnlinkableFolderContents()
1225  {
1226  $folderId = 3;
1227  $id = 3;
1228  $userId = 2;
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');
1235 
1236  $actualResponse = $this->folderController->getUnlinkableFolderContents(null,new ResponseHelper(),["id" => $id]);
1237  var_dump($actualResponse);
1238  $this->assertEquals(200,$actualResponse->getStatusCode());
1239  }
1240 
1246  public function testGetUnlinkableInaccessibleFolderContents()
1247  {
1248  $folderId = 3;
1249  $id = 3;
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');
1255 
1256  $this->expectException(HttpForbiddenException::class);
1257  $this->folderController->getUnlinkableFolderContents(null,new ResponseHelper(),["id" => $id]);
1258  }
1259 
1265  public function testGetUnlinkableFolderContentsBadRequest()
1266  {
1267  $folderId = 3;
1268  $id = 3;
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);
1272 
1273  $this->expectException(HttpNotFoundException::class);
1274  $this->folderController->getUnlinkableFolderContents(null,new ResponseHelper(),["id" => $id]);
1275 
1276  }
1277  }
1278 }
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.