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 
64  use Mockery as M;
65  use Slim\Psr7\Factory\StreamFactory;
66  use Slim\Psr7\Headers;
67  use Slim\Psr7\Request;
68  use Slim\Psr7\Uri;
69 
74  class FolderControllerTest extends \PHPUnit\Framework\TestCase
75  {
76 
81  private $dbHelper;
86  private $folderDao;
91  private $restHelper;
96  private $folderController;
101  private $userId;
106  private $folderPlugin;
111  private $deletePlugin;
116  private $folderPropertiesPlugin;
121  private $folderContentPlugin;
122 
127  private $assertCountBefore;
128 
133  private $streamFactory;
134 
139  protected function setUp() : void
140  {
141  global $container;
142  $this->userId = 2;
143  $container = M::mock('ContainerBuilder');
144  $this->dbHelper = M::mock(DbHelper::class);
145  $this->restHelper = M::mock(RestHelper::class);
146  $this->folderDao = M::mock(FolderDao::class);
147  $this->folderPlugin = M::mock('folder_create');
148  $this->deletePlugin = M::mock('admin_folder_delete');
149  $this->folderPropertiesPlugin = M::mock('folder_properties');
150  $this->folderContentPlugin = M::mock('content_move');
151 
152  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
153  $this->restHelper->shouldReceive('getFolderDao')->andReturn($this->folderDao);
154  $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId);
155  $this->restHelper->shouldReceive('getPlugin')
156  ->withArgs(array('folder_create'))->andReturn($this->folderPlugin);
157  $this->restHelper->shouldReceive('getPlugin')
158  ->withArgs(array('admin_folder_delete'))->andReturn($this->deletePlugin);
159  $this->restHelper->shouldReceive('getPlugin')
160  ->withArgs(array('folder_properties'))
161  ->andReturn($this->folderPropertiesPlugin);
162  $this->restHelper->shouldReceive('getPlugin')
163  ->withArgs(array('content_move'))
164  ->andReturn($this->folderContentPlugin);
165 
166  $container->shouldReceive('get')->withArgs(array(
167  'helper.restHelper'))->andReturn($this->restHelper);
168  $this->folderController = new FolderController($container);
169  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
170  $this->streamFactory = new StreamFactory();
171  }
172 
177  protected function tearDown() : void
178  {
179  $this->addToAssertionCount(
180  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
181  M::close();
182  }
183 
190  public function getFolder($id)
191  {
192  $name = "";
193  switch ($id) {
194  case 2: $name = "root";break;
195  case 3: $name = "root-child1";break;
196  case 4: $name = "root-child2";break;
197  case -1: return null;
198  default: $name = "singlefolder";
199  }
200  return new Folder($id, $name, "", 1);
201  }
202 
209  public function getFolderParent($id)
210  {
211  $pid = null;
212  if ($id == 3 || $id == 4) {
213  $pid = 2;
214  } elseif ($id > 4) {
215  $pid = $id - 1;
216  }
217  return $pid;
218  }
219 
226  private function getResponseJson($response)
227  {
228  $response->getBody()->seek(0);
229  return json_decode($response->getBody()->getContents(), true);
230  }
231 
238  public function testGetAllFolders()
239  {
240  $rootFolder = new Folder(2, "root", "", 2);
241  $this->folderDao->shouldReceive('getRootFolder')->withArgs(array(2))
242  ->andReturn($rootFolder);
243  $this->folderDao->shouldReceive('getFolder')
244  ->andReturnUsing([$this, 'getFolder']);
245  $this->folderDao->shouldReceive('getFolderParentId')
246  ->andReturnUsing([$this, 'getFolderParent']);
247  $expectedFoldersList = [];
248  for ($i = 2; $i <= 4; $i ++) {
249  $folder = $this->getFolder($i);
250  $parentId = $this->getFolderParent($i);
251  $folderModel = new \Fossology\UI\Api\Models\Folder($folder->getId(),
252  $folder->getName(), $folder->getDescription(), $parentId);
253  $expectedFoldersList[] = $folderModel->getArray();
254  }
255  $actualResponse = $this->folderController->getFolders(null,
256  new ResponseHelper(), []);
257  $this->assertEquals(200, $actualResponse->getStatusCode());
258  $this->assertEquals($expectedFoldersList,
259  $this->getResponseJson($actualResponse));
260  }
261 
267  public function testGetSpecificFolders()
268  {
269  $folderId = 3;
270  $this->folderDao->shouldReceive('isFolderAccessible')
271  ->withArgs(array($folderId))->andReturn(true);
272  $this->folderDao->shouldReceive('getFolder')
273  ->andReturnUsing([$this, 'getFolder']);
274  $this->folderDao->shouldReceive('getFolderParentId')
275  ->andReturnUsing([$this, 'getFolderParent']);
276  $folder = $this->getFolder($folderId);
277  $parentId = $this->getFolderParent($folderId);
278  $folderModel = new \Fossology\UI\Api\Models\Folder($folder->getId(),
279  $folder->getName(), $folder->getDescription(), $parentId);
280  $expectedFoldersList = $folderModel->getArray();
281  $actualResponse = $this->folderController->getFolders(null,
282  new ResponseHelper(), ['id' => $folderId]);
283  $this->assertEquals(200, $actualResponse->getStatusCode());
284  $this->assertEquals($expectedFoldersList,
285  $this->getResponseJson($actualResponse));
286  }
287 
294  public function testGetInvalidFolder()
295  {
296  $folderId = -1;
297  $this->folderDao->shouldReceive('isFolderAccessible')
298  ->withArgs(array($folderId))->andReturn(false);
299  $this->folderDao->shouldReceive('getFolder')
300  ->andReturnUsing([$this, 'getFolder']);
301  $this->expectException(HttpForbiddenException::class);
302 
303  $this->folderController->getFolders(null, new ResponseHelper(),
304  ['id' => $folderId]);
305  }
306 
313  public function testGetInAccessibleFolder()
314  {
315  $folderId = 3;
316  $this->folderDao->shouldReceive('isFolderAccessible')
317  ->withArgs(array($folderId))->andReturn(false);
318  $this->folderDao->shouldReceive('getFolder')
319  ->andReturnUsing([$this, 'getFolder']);
320  $this->expectException(HttpForbiddenException::class);
321 
322  $this->folderController->getFolders(null, new ResponseHelper(),
323  ['id' => $folderId]);
324  }
325 
331  public function testCreateFolder()
332  {
333  $parentFolder = 2;
334  $folderName = "root-child1";
335  $folderDescription = "Description";
336  $folderId = 5;
337  $this->folderDao->shouldReceive('isFolderAccessible')
338  ->withArgs(array($parentFolder, $this->userId))->andReturn(true);
339  $this->folderPlugin->shouldReceive('create')
340  ->withArgs(array($parentFolder, $folderName, $folderDescription))
341  ->andReturn(1);
342  $this->folderDao->shouldReceive('getFolderId')
343  ->withArgs(array($folderName, $parentFolder))->andReturn($folderId);
344  $requestHeaders = new Headers();
345  $requestHeaders->setHeader('parentFolder', $parentFolder);
346  $requestHeaders->setHeader('folderName', $folderName);
347  $requestHeaders->setHeader('folderDescription', $folderDescription);
348  $body = $this->streamFactory->createStream();
349  $request = new Request("POST", new Uri("HTTP", "localhost"),
350  $requestHeaders, [], [], $body);
351  $response = new ResponseHelper();
352  $actualResponse = $this->folderController->createFolder($request,
353  $response, []);
354  $expectedResponse = new Info(201, $folderId, InfoType::INFO);
355  $this->assertEquals($expectedResponse->getCode(),
356  $actualResponse->getStatusCode());
357  $this->assertEquals($expectedResponse->getArray(),
358  $this->getResponseJson($actualResponse));
359  }
360 
366  public function testCreateFolderParentNotAccessible()
367  {
368  $parentFolder = 2;
369  $folderName = "root-child1";
370  $folderDescription = "Description";
371  $this->folderDao->shouldReceive('isFolderAccessible')
372  ->withArgs(array($parentFolder, $this->userId))->andReturn(false);
373  $requestHeaders = new Headers();
374  $requestHeaders->setHeader('parentFolder', $parentFolder);
375  $requestHeaders->setHeader('folderName', $folderName);
376  $requestHeaders->setHeader('folderDescription', $folderDescription);
377  $body = $this->streamFactory->createStream();
378  $request = new Request("POST", new Uri("HTTP", "localhost"),
379  $requestHeaders, [], [], $body);
380  $response = new ResponseHelper();
381 
382  $this->expectException(HttpForbiddenException::class);
383 
384  $this->folderController->createFolder($request, $response, []);
385  }
386 
393  public function testCreateFolderDuplicateNames()
394  {
395  $parentFolder = 2;
396  $folderName = "root-child1";
397  $folderDescription = "Description";
398  $this->folderDao->shouldReceive('isFolderAccessible')
399  ->withArgs(array($parentFolder, $this->userId))->andReturn(true);
400  $this->folderPlugin->shouldReceive('create')
401  ->withArgs(array($parentFolder, $folderName, $folderDescription))
402  ->andReturn(4);
403  $requestHeaders = new Headers();
404  $requestHeaders->setHeader('parentFolder', $parentFolder);
405  $requestHeaders->setHeader('folderName', $folderName);
406  $requestHeaders->setHeader('folderDescription', $folderDescription);
407  $body = $this->streamFactory->createStream();
408  $request = new Request("POST", new Uri("HTTP", "localhost"),
409  $requestHeaders, [], [], $body);
410  $response = new ResponseHelper();
411  $actualResponse = $this->folderController->createFolder($request,
412  $response, []);
413  $expectedResponse = new Info(200, "Folder $folderName already exists!",
414  InfoType::INFO);
415  $this->assertEquals($expectedResponse->getCode(),
416  $actualResponse->getStatusCode());
417  $this->assertEquals($expectedResponse->getArray(),
418  $this->getResponseJson($actualResponse));
419  }
420 
426  public function testDeleteFolder()
427  {
428  $folderId = 3;
429  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
430  $this->folderDao->shouldReceive('getFolder')
431  ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
432  $this->deletePlugin->shouldReceive('Delete')
433  ->withArgs(array("2 $folderId", $this->userId))->andReturnNull();
434  $actualResponse = $this->folderController->deleteFolder(null,
435  new ResponseHelper(), ["id" => $folderId]);
436  $expectedResponse = new Info(202, "Folder, \"$folderName\" deleted.",
437  InfoType::INFO);
438  $this->assertEquals($expectedResponse->getCode(),
439  $actualResponse->getStatusCode());
440  $this->assertEquals($expectedResponse->getArray(),
441  $this->getResponseJson($actualResponse));
442  }
443 
449  public function testDeleteFolderInvalidFolder()
450  {
451  $folderId = 0;
452  $this->folderDao->shouldReceive('getFolder')
453  ->withArgs(array($folderId))->andReturnNull();
454  $this->expectException(HttpNotFoundException::class);
455 
456  $this->folderController->deleteFolder(null, new ResponseHelper(),
457  ["id" => $folderId]);
458  }
459 
465  public function testDeleteFolderNoAccess()
466  {
467  $folderId = 3;
468  $errorText = "No access to delete this folder";
469  $this->folderDao->shouldReceive('getFolder')
470  ->withArgs(array($folderId))->andReturn($this->getFolder($folderId));
471  $this->deletePlugin->shouldReceive('Delete')
472  ->withArgs(array("2 $folderId", $this->userId))
473  ->andReturn($errorText);
474  $this->expectException(HttpForbiddenException::class);
475 
476  $this->folderController->deleteFolder(null, new ResponseHelper(),
477  ["id" => $folderId]);
478  }
479 
485  public function testEditFolder()
486  {
487  $folderId = 3;
488  $folderName = "new name";
489  $folderDescription = "new description";
490  $this->folderDao->shouldReceive('getFolder')
491  ->andReturnUsing([$this, 'getFolder']);
492  $this->folderDao->shouldReceive('isFolderAccessible')
493  ->withArgs(array($folderId, $this->userId))->andReturn(true);
494  $this->folderPropertiesPlugin->shouldReceive('Edit')
495  ->withArgs(array($folderId, $folderName, $folderDescription));
496  $requestHeaders = new Headers();
497  $requestHeaders->setHeader('name', $folderName);
498  $requestHeaders->setHeader('description', $folderDescription);
499  $body = $this->streamFactory->createStream();
500  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
501  $requestHeaders, [], [], $body);
502  $response = new ResponseHelper();
503  $actualResponse = $this->folderController->editFolder($request,
504  $response, ["id" => $folderId]);
505  $expectedResponse = new Info(200, 'Folder "' . \Fossology\UI\Api\Controllers\FolderGetName($folderId) .
506  '" updated.', InfoType::INFO);
507  $this->assertEquals($expectedResponse->getCode(),
508  $actualResponse->getStatusCode());
509  $this->assertEquals($expectedResponse->getArray(),
510  $this->getResponseJson($actualResponse));
511  }
512 
518  public function testEditFolderNotExists()
519  {
520  $folderId = 8;
521  $folderName = "new name";
522  $folderDescription = "new description";
523  $this->folderDao->shouldReceive('getFolder')->andReturnNull();
524  $requestHeaders = new Headers();
525  $requestHeaders->setHeader('name', $folderName);
526  $requestHeaders->setHeader('description', $folderDescription);
527  $body = $this->streamFactory->createStream();
528  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
529  $requestHeaders, [], [], $body);
530  $response = new ResponseHelper();
531  $this->expectException(HttpNotFoundException::class);
532 
533  $this->folderController->editFolder($request, $response,
534  ["id" => $folderId]);
535  }
536 
542  public function testEditFolderNotAccessible()
543  {
544  $folderId = 3;
545  $folderName = "new name";
546  $folderDescription = "new description";
547  $this->folderDao->shouldReceive('getFolder')
548  ->andReturnUsing([$this, 'getFolder']);
549  $this->folderDao->shouldReceive('isFolderAccessible')
550  ->withArgs(array($folderId, $this->userId))->andReturn(false);
551  $requestHeaders = new Headers();
552  $requestHeaders->setHeader('name', $folderName);
553  $requestHeaders->setHeader('description', $folderDescription);
554  $body = $this->streamFactory->createStream();
555  $request = new Request("PATCH", new Uri("HTTP", "localhost"),
556  $requestHeaders, [], [], $body);
557  $response = new ResponseHelper();
558  $this->expectException(HttpForbiddenException::class);
559 
560  $this->folderController->editFolder($request, $response,
561  ["id" => $folderId]);
562  }
563 
569  public function testCopyFolder()
570  {
571  $folderId = 3;
572  $parentId = 2;
573  $folderContentPk = 5;
574  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
575  $parentFolderName = \Fossology\UI\Api\Controllers\FolderGetName($parentId);
576 
577  $this->folderDao->shouldReceive('getFolder')
578  ->andReturnUsing([$this, 'getFolder']);
579  $this->folderDao->shouldReceive('isFolderAccessible')
580  ->withArgs(array(M::anyOf($folderId, "$parentId"),
581  $this->userId))->andReturn(true);
582  $this->folderDao->shouldReceive('getFolderContentsId')
583  ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
584  $this->folderContentPlugin->shouldReceive('copyContent')
585  ->withArgs(array([$folderContentPk], $parentId, true))->andReturn("");
586  $requestHeaders = new Headers();
587  $requestHeaders->setHeader('parent', $parentId);
588  $requestHeaders->setHeader('action', "copy");
589  $body = $this->streamFactory->createStream();
590  $request = new Request("PUT", new Uri("HTTP", "localhost"),
591  $requestHeaders, [], [], $body);
592  $response = new ResponseHelper();
593 
594  $actualResponse = $this->folderController->copyFolder($request,
595  $response, ["id" => $folderId]);
596  $expectedResponse = new Info(202,
597  "Folder \"$folderName\" copy(ed) under \"$parentFolderName\".",
598  InfoType::INFO);
599  $this->assertEquals($expectedResponse->getCode(),
600  $actualResponse->getStatusCode());
601  $this->assertEquals($expectedResponse->getArray(),
602  $this->getResponseJson($actualResponse));
603  }
604 
610  public function testMoveFolder()
611  {
612  $folderId = 3;
613  $parentId = 2;
614  $folderContentPk = 5;
615  $folderName = \Fossology\UI\Api\Controllers\FolderGetName($folderId);
616  $parentFolderName = \Fossology\UI\Api\Controllers\FolderGetName($parentId);
617 
618  $this->folderDao->shouldReceive('getFolder')
619  ->andReturnUsing([$this, 'getFolder']);
620  $this->folderDao->shouldReceive('isFolderAccessible')
621  ->withArgs(array(M::anyOf($folderId, "$parentId"),
622  $this->userId))->andReturn(true);
623  $this->folderDao->shouldReceive('getFolderContentsId')
624  ->withArgs(array($folderId, 1))->andReturn($folderContentPk);
625  $this->folderContentPlugin->shouldReceive('copyContent')
626  ->withArgs(array([$folderContentPk], $parentId, false))->andReturn("");
627  $requestHeaders = new Headers();
628  $requestHeaders->setHeader('parent', $parentId);
629  $requestHeaders->setHeader('action', "move");
630  $body = $this->streamFactory->createStream();
631  $request = new Request("PUT", new Uri("HTTP", "localhost"),
632  $requestHeaders, [], [], $body);
633  $response = new ResponseHelper();
634 
635  $actualResponse = $this->folderController->copyFolder($request,
636  $response, ["id" => $folderId]);
637  $expectedResponse = new Info(202,
638  "Folder \"$folderName\" move(ed) under \"$parentFolderName\".",
639  InfoType::INFO);
640  $this->assertEquals($expectedResponse->getCode(),
641  $actualResponse->getStatusCode());
642  $this->assertEquals($expectedResponse->getArray(),
643  $this->getResponseJson($actualResponse));
644  }
645 
651  public function testCopyFolderNotFound()
652  {
653  $folderId = 3;
654  $parentId = 2;
655 
656  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))
657  ->andReturnNull();
658  $requestHeaders = new Headers();
659  $requestHeaders->setHeader('parent', $parentId);
660  $requestHeaders->setHeader('action', "copy");
661  $body = $this->streamFactory->createStream();
662  $request = new Request("PUT", new Uri("HTTP", "localhost"),
663  $requestHeaders, [], [], $body);
664  $response = new ResponseHelper();
665  $this->expectException(HttpNotFoundException::class);
666 
667  $this->folderController->copyFolder($request, $response,
668  ["id" => $folderId]);
669  }
670 
676  public function testCopyParentFolderNotFound()
677  {
678  $folderId = 3;
679  $parentId = 2;
680 
681  $this->folderDao->shouldReceive('getFolder')->withArgs(array($folderId))
682  ->andReturn($this->getFolder($folderId));
683  $this->folderDao->shouldReceive('getFolder')->withArgs(array($parentId))
684  ->andReturnNull();
685  $requestHeaders = new Headers();
686  $requestHeaders->setHeader('parent', $parentId);
687  $requestHeaders->setHeader('action', "copy");
688  $body = $this->streamFactory->createStream();
689  $request = new Request("PUT", new Uri("HTTP", "localhost"),
690  $requestHeaders, [], [], $body);
691  $response = new ResponseHelper();
692  $this->expectException(HttpNotFoundException::class);
693 
694  $this->folderController->copyFolder($request, $response,
695  ["id" => $folderId]);
696  }
697 
703  public function testCopyFolderNotAccessible()
704  {
705  $folderId = 3;
706  $parentId = 2;
707 
708  $this->folderDao->shouldReceive('getFolder')
709  ->andReturnUsing([$this, 'getFolder']);
710  $this->folderDao->shouldReceive('isFolderAccessible')
711  ->withArgs(array($folderId, $this->userId))->andReturn(false);
712  $requestHeaders = new Headers();
713  $requestHeaders->setHeader('parent', $parentId);
714  $requestHeaders->setHeader('action', "copy");
715  $body = $this->streamFactory->createStream();
716  $request = new Request("PUT", new Uri("HTTP", "localhost"),
717  $requestHeaders, [], [], $body);
718  $response = new ResponseHelper();
719  $this->expectException(HttpForbiddenException::class);
720 
721  $this->folderController->copyFolder($request, $response,
722  ["id" => $folderId]);
723  }
724 
730  public function testCopyParentFolderNotAccessible()
731  {
732  $folderId = 3;
733  $parentId = 2;
734 
735  $this->folderDao->shouldReceive('getFolder')
736  ->andReturnUsing([$this, 'getFolder']);
737  $this->folderDao->shouldReceive('isFolderAccessible')
738  ->withArgs(array($folderId, $this->userId))->andReturn(true);
739  $this->folderDao->shouldReceive('isFolderAccessible')
740  ->withArgs(array("$parentId", $this->userId))->andReturn(false);
741  $requestHeaders = new Headers();
742  $requestHeaders->setHeader('parent', $parentId);
743  $requestHeaders->setHeader('action', "copy");
744  $body = $this->streamFactory->createStream();
745  $request = new Request("PUT", new Uri("HTTP", "localhost"),
746  $requestHeaders, [], [], $body);
747  $response = new ResponseHelper();
748  $this->expectException(HttpForbiddenException::class);
749 
750  $this->folderController->copyFolder($request, $response,
751  ["id" => $folderId]);
752  }
753 
759  public function testCopyFolderInvalidAction()
760  {
761  $folderId = 3;
762  $parentId = 2;
763 
764  $this->folderDao->shouldReceive('getFolder')
765  ->andReturnUsing([$this, 'getFolder']);
766  $this->folderDao->shouldReceive('isFolderAccessible')
767  ->withArgs(array(M::anyOf($folderId, "$parentId"),
768  $this->userId))->andReturn(true);
769  $requestHeaders = new Headers();
770  $requestHeaders->setHeader('parent', $parentId);
771  $requestHeaders->setHeader('action', "somethingrandom");
772  $body = $this->streamFactory->createStream();
773  $request = new Request("PUT", new Uri("HTTP", "localhost"),
774  $requestHeaders, [], [], $body);
775  $response = new ResponseHelper();
776  $this->expectException(HttpBadRequestException::class);
777 
778  $this->folderController->copyFolder($request, $response,
779  ["id" => $folderId]);
780  }
781  }
782 }
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.