FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadTreeControllerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Samuel Dushimimana <dushsam100@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 namespace {
14  if (!function_exists('RepPathItem')) {
15  function RepPathItem($Item, $Repo = "files")
16  {
17  return dirname(__DIR__) . "/tests.xml";
18  }
19  }
20 }
21 
23 
24  use AjaxClearingView;
26  use ClearingView;
60  use Mockery as M;
61  use Psr\Http\Message\ServerRequestInterface;
62  use Slim\Psr7\Factory\StreamFactory;
63  use Slim\Psr7\Headers;
64  use Slim\Psr7\Request;
65  use Slim\Psr7\Response;
66  use Slim\Psr7\Uri;
67  use Symfony\Component\HttpFoundation\JsonResponse;
68 
73  class UploadTreeControllerTest extends \PHPUnit\Framework\TestCase
74  {
79  private $dbHelper;
80 
85  private $dbManager;
86 
91  private $restHelper;
92 
98 
103  private $uploadDao;
104 
109  private $licenseDao;
110 
116 
121  private $highlightDao;
122 
128 
133  private $streamFactory;
134 
140 
146 
152 
158 
163  private $decisionTypes;
164 
170 
175 
185  private $groupId;
186 
190  private $uploadId;
194  private $agentId;
195 
199  private $tagId;
200 
201  protected function setUp(): void
202  {
203  global $container;
204  $this->userId = 2;
205  $this->groupId = 2;
206  $this->uploadId = 3;
207  $this->agentId = 5;
208  $this->tagId = 7;
209  $container = M::mock('ContainerBuilder');
210  $this->dbHelper = M::mock(DbHelper::class);
211  $this->dbManager = M::mock(DbManager::class);
212  $this->restHelper = M::mock(RestHelper::class);
213  $this->uploadDao = M::mock(UploadDao::class);
214  $this->decisionTypes = M::mock(DecisionTypes::class);
215  $this->licenseDao = M::mock(LicenseDao::class);
216  $this->clearingDecisionEventProcessor = M::mock(ClearingDecisionProcessor::class);
217  $this->viewFilePlugin = M::mock('ui_view');
218  $this->viewLicensePlugin = M::mock(ClearingView::class);
219  $this->clearingDao = M::mock(ClearingDao::class);
220  $this->decisionScopes = M::mock(DecisionScopes::class);
221  $this->highlightDao = M::mock(HighlightDao::class);
222  $this->clearingEventTypes = M::mock(ClearingEventTypes::class);
223  $this->itemTreeBoundsMock = M::mock(ItemTreeBounds::class);
224  $this->concludeLicensePlugin = M::mock(AjaxClearingView::class);
225  $this->licenseDao = M::mock(LicenseDao::class);
226  $this->changeLicenseBulk = M::mock(ChangeLicenseBulk::class);
227  $this->licenseDao = M::mock(LicenseDao::class);
228 
229  $this->restHelper->shouldReceive('getPlugin')
230  ->withArgs(array('view'))->andReturn($this->viewFilePlugin);
231  $this->restHelper->shouldReceive('getPlugin')
232  ->withArgs(array('view-license'))->andReturn($this->viewLicensePlugin);
233  $this->restHelper->shouldReceive('getPlugin')
234  ->withArgs(array('conclude-license'))->andReturn($this->concludeLicensePlugin);
235  $this->restHelper->shouldReceive('getPlugin')
236  ->withArgs(array('change-license-bulk'))->andReturn($this->changeLicenseBulk);
237 
238  $this->dbManager->shouldReceive('getSingleRow')
239  ->withArgs([M::any(), [$this->groupId, UploadStatus::OPEN,
240  Auth::PERM_READ]]);
241  $this->dbHelper->shouldReceive('getDbManager')->andReturn($this->dbManager);
242  $this->restHelper->shouldReceive('getDbHelper')->andReturn($this->dbHelper);
243  $this->restHelper->shouldReceive('getGroupId')->andReturn($this->groupId);
244  $this->restHelper->shouldReceive('getUserId')->andReturn($this->userId);
245  $this->restHelper->shouldReceive('getUploadDao')
246  ->andReturn($this->uploadDao);
247  $container->shouldReceive('get')->withArgs(array(
248  'helper.restHelper'))->andReturn($this->restHelper);
249  $container->shouldReceive('get')->withArgs(['decision.types'])->andReturn($this->decisionTypes);
250  $container->shouldReceive('get')->withArgs(['dao.clearing'])->andReturn($this->clearingDao);
251  $container->shouldReceive('get')->withArgs(['dao.license'])->andReturn($this->licenseDao);
252 
253  $container->shouldReceive('get')->withArgs(['businessrules.clearing_decision_processor'])->andReturn($this->clearingDecisionEventProcessor);
254  $container->shouldReceive('get')->withArgs(['dao.highlight'])->andReturn($this->highlightDao);
255  $container->shouldReceive('get')->withArgs(['dao.license'])->andReturn($this->licenseDao);
256  $this->uploadTreeController = new UploadTreeController($container);
257  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
258  $this->streamFactory = new StreamFactory();
259  }
260 
267  private function getResponseJson($response)
268  {
269  $response->getBody()->seek(0);
270  return json_decode($response->getBody()->getContents(), true);
271  }
272 
278  public function testViewLicenseFile()
279  {
280  $upload_pk = 1;
281  $item_pk = 200;
282  $expectedContent = file_get_contents(dirname(__DIR__) . "/tests.xml");
283 
284  $this->uploadDao->shouldReceive('isAccessible')
285  ->withArgs([$upload_pk, $this->groupId])->andReturn(true);
286  $this->dbHelper->shouldReceive('doesIdExist')
287  ->withArgs(["upload", "upload_pk", $upload_pk])->andReturn(true);
288  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$upload_pk])->andReturn("uploadtree");
289  $this->dbHelper->shouldReceive('doesIdExist')
290  ->withArgs(["uploadtree", "uploadtree_pk", $item_pk])->andReturn(true);
291 
292  $this->viewFilePlugin->shouldReceive('getText')->withArgs([M::any(), 0, 0, -1, null, false, true])->andReturn($expectedContent);
293 
294  $expectedResponse = new ResponseHelper();
295  $expectedResponse->getBody()->write($expectedContent);
296  $actualResponse = $this->uploadTreeController->viewLicenseFile(null, new ResponseHelper(), ['id' => $upload_pk, 'itemId' => $item_pk]);
297  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
298  $this->assertEquals($expectedResponse->getBody()->getContents(), $actualResponse->getBody()->getContents());
299  }
300 
301 
308  {
309  $upload_pk = 1;
310  $item_pk = 200;
311  $rq = [
312  "decisionType" => 3,
313  "globalDecision" => false,
314  ];
315  $dummyDecisionTypes = array_map(function ($i) {
316  return $i;
317  }, range(1, 7));
318 
319  $this->decisionTypes->shouldReceive('getMap')
320  ->andReturn($dummyDecisionTypes);
321  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$upload_pk])->andReturn("uploadtree");
322  $this->uploadDao->shouldReceive('isAccessible')
323  ->withArgs([$upload_pk, $this->groupId])->andReturn(true);
324  $this->dbHelper->shouldReceive('doesIdExist')
325  ->withArgs(["upload", "upload_pk", $upload_pk])->andReturn(true);
326  $this->dbHelper->shouldReceive('doesIdExist')
327  ->withArgs(["uploadtree", "uploadtree_pk", $item_pk])->andReturn(true);
328 
329 
330  $this->viewLicensePlugin->shouldReceive('updateLastItem')->withArgs([2, 2, $item_pk, $item_pk]);
331 
332  $info = new Info(200, "Successfully set decision", InfoType::INFO);
333 
334  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
335  $reqBody = $this->streamFactory->createStream(json_encode(
336  $rq
337  ));
338  $requestHeaders = new Headers();
339  $requestHeaders->setHeader('Content-Type', 'application/json');
340  $request = new Request("PUT", new Uri("HTTP", "localhost"),
341  $requestHeaders, [], [], $reqBody);
342  $actualResponse = $this->uploadTreeController->setClearingDecision($request, new ResponseHelper(), ['id' => $upload_pk, 'itemId' => $item_pk]);
343 
344  $this->assertEquals($expectedResponse->getStatusCode(),
345  $actualResponse->getStatusCode());
346  $this->assertEquals($this->getResponseJson($expectedResponse),
347  $this->getResponseJson($actualResponse));
348  }
349 
356  {
357  $upload_pk = 1;
358  $item_pk = 200;
359  $rq = [
360  "decisionType" => 40,
361  "globalDecision" => false,
362  ];
363  $dummyDecisionTypes = array_map(function ($i) {
364  return $i;
365  }, range(1, 7));
366 
367  $this->decisionTypes->shouldReceive('getMap')
368  ->andReturn($dummyDecisionTypes);
369  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$upload_pk])->andReturn("uploadtree");
370  $this->uploadDao->shouldReceive('isAccessible')
371  ->withArgs([$upload_pk, $this->groupId])->andReturn(true);
372  $this->dbHelper->shouldReceive('doesIdExist')
373  ->withArgs(["upload", "upload_pk", $upload_pk])->andReturn(true);
374  $this->dbHelper->shouldReceive('doesIdExist')
375  ->withArgs(["uploadtree", "uploadtree_pk", $item_pk])->andReturn(true);
376 
377  $this->viewLicensePlugin->shouldReceive('updateLastItem')->withArgs([2, 2, $item_pk, $item_pk]);
378 
379  $reqBody = $this->streamFactory->createStream(json_encode(
380  $rq
381  ));
382  $requestHeaders = new Headers();
383  $requestHeaders->setHeader('Content-Type', 'application/json');
384  $request = new Request("PUT", new Uri("HTTP", "localhost"),
385  $requestHeaders, [], [], $reqBody);
386  $this->expectException(HttpBadRequestException::class);
387 
388  $this->uploadTreeController->setClearingDecision($request,
389  new ResponseHelper(), ['id' => $upload_pk, 'itemId' => $item_pk]);
390  }
396  public function testGetNextPreviousItem()
397  {
398  $itemId = 200;
399  $uploadId = 1;
400  $nextItemId = 915;
401  $prevItemId = 109;
402  $itemTreeBounds1 = new ItemTreeBounds($nextItemId, 'uploadtree_a', $uploadId, 1, 2);
403  $itemTreeBounds2 = new ItemTreeBounds($prevItemId, 'uploadtree_a', $uploadId, 1, 2);
404 
405  $item1 = new Item($itemTreeBounds1, 1, 1, 1, "fileName");
406  $item2 = new Item($itemTreeBounds2, 1, 1, 1, "fileName");
407 
408  $result = array(
409  "prevItemId" => $prevItemId,
410  "nextItemId" => $nextItemId
411  );
412  $options = array('skipThese' => "", 'groupId' => $this->groupId);
413 
414  $this->uploadDao->shouldReceive('isAccessible')
415  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
416  $this->dbHelper->shouldReceive('doesIdExist')
417  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
418  $this->uploadDao->shouldReceive('getUploadtreeTableName')->withArgs([$uploadId])->andReturn("uploadtree");
419  $this->dbHelper->shouldReceive('doesIdExist')
420  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
421 
422  $this->uploadDao->shouldReceive('getNextItem')->withArgs([$uploadId, $itemId, $options])->andReturn($item1);
423  $this->uploadDao->shouldReceive('getPreviousItem')->withArgs([$uploadId, $itemId, $options])->andReturn($item2);
424  $expectedResponse = (new ResponseHelper())->withJson($result, 200);
425  $queryParams = ['selection' => null];
426  $request = $this->getMockBuilder(ServerRequestInterface::class)
427  ->getMock();
428  $request->expects($this->any())
429  ->method('getQueryParams')
430  ->willReturn($queryParams);
431 
432  $actualResponse = $this->uploadTreeController->getNextPreviousItem($request, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
433  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
434  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
435  }
436 
443  {
444  $itemId = 200;
445  $uploadId = 1;
446  $nextItemId = 915;
447  $prevItemId = 109;
448  $itemTreeBounds1 = new ItemTreeBounds($nextItemId, 'uploadtree_a', $uploadId, 1, 2);
449  $itemTreeBounds2 = new ItemTreeBounds($prevItemId, 'uploadtree_a', $uploadId, 1, 2);
450 
451  $item1 = new Item($itemTreeBounds1, 1, 1, 1, "fileName");
452  $item2 = new Item($itemTreeBounds2, 1, 1, 1, "fileName");
453 
454  $options = array('skipThese' => "", 'groupId' => $this->groupId);
455 
456  $this->uploadDao->shouldReceive('isAccessible')
457  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
458  $this->dbHelper->shouldReceive('doesIdExist')
459  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
460  $this->uploadDao->shouldReceive('getUploadtreeTableName')->withArgs([$uploadId])->andReturn("uploadtree");
461  $this->dbHelper->shouldReceive('doesIdExist')
462  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
463 
464  $this->uploadDao->shouldReceive('getNextItem')->withArgs([$uploadId, $itemId, $options])->andReturn($item1);
465  $this->uploadDao->shouldReceive('getPreviousItem')->withArgs([$uploadId, $itemId, $options])->andReturn($item2);
466 
467  $queryParams = ['selection' => "invalidSelection"];
468  $request = $this->getMockBuilder(ServerRequestInterface::class)
469  ->getMock();
470  $request->expects($this->any())
471  ->method('getQueryParams')
472  ->willReturn($queryParams);
473  $this->expectException(HttpBadRequestException::class);
474 
475  $this->uploadTreeController->getNextPreviousItem($request,
476  new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
477  }
478 
484  public function testGetBulkHistory()
485  {
486  $itemId = 200;
487  $uploadId = 1;
488  $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree', $uploadId, 1, 2);
489 
490  $res[] = array(
491  "bulkId" => 1,
492  "id" => 1,
493  "text" => "test",
494  "matched" => true,
495  "tried" => true,
496  "addedLicenses" => [],
497  "removedLicenses" => [],
498  );
499 
500  $obj = new BulkHistory(1, 1, "test", true, true, [], []);
501  $updatedRes[] = $obj->getArray();
502 
503  $this->uploadDao->shouldReceive('isAccessible')
504  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
505  $this->dbHelper->shouldReceive('doesIdExist')
506  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
507 
508  $this->uploadDao->shouldReceive('getUploadtreeTableName')->withArgs([$uploadId])->andReturn("uploadtree");
509  $this->dbHelper->shouldReceive('doesIdExist')
510  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
511 
512  $this->uploadDao->shouldReceive("getItemTreeBounds")
513  ->withArgs([$itemId, "uploadtree"])->andReturn($itemTreeBounds);
514 
515  $this->clearingDao->shouldReceive("getBulkHistory")
516  ->withArgs([$itemTreeBounds, $this->groupId])->andReturn($res);
517  $expectedResponse = (new ResponseHelper())->withJson($updatedRes, 200);
518  $actualResponse = $this->uploadTreeController->getBulkHistory(null, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
519  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
520  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
521  }
522 
528  public function testGetClearingHistory()
529  {
530  $itemId = 200;
531  $uploadId = 1;
532  $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree_a', $uploadId, 1, 2);
533  $fileClearings[] = new ClearingDecision(1, 1, $itemId, 1, 1, 1, 3, 1, 1, [], 1, 1, 1);
534  $obj = new ClearingHistory(
535  date('Y-m-d', 1),
536  1,
537  "global",
538  "TO_BE_DISCUSSED",
539  [],
540  [],
541  );
542  $result[] = $obj->getArray();
543 
544  $this->uploadDao->shouldReceive('isAccessible')
545  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
546  $this->dbHelper->shouldReceive('doesIdExist')
547  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
548 
549  $this->uploadDao->shouldReceive('getUploadtreeTableName')->withArgs([$uploadId])->andReturn("uploadtree");
550  $this->dbHelper->shouldReceive('doesIdExist')
551  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
552 
553  $this->uploadDao->shouldReceive("getItemTreeBoundsFromUploadId")
554  ->withArgs([$itemId, $uploadId])->andReturn($itemTreeBounds);
555  $this->clearingDao->shouldReceive("getFileClearings")
556  ->withArgs([$itemTreeBounds, $this->groupId, false, true])->andReturn($fileClearings);
557  $this->decisionTypes->shouldReceive("getTypeName")
558  ->withArgs([$fileClearings[0]->getType()])->andReturn("test");
559  $this->decisionScopes->shouldReceive("getTypeName")->withArgs([$fileClearings[0]->getScope()])->andReturn("test");
560  $this->decisionTypes->shouldReceive("getConstantNameFromKey")
561  ->withArgs([$fileClearings[0]->getType()])->andReturn("TO_BE_DISCUSSED");
562  $expectedResponse = (new ResponseHelper())->withJson($result, 200);
563  $actualResponse = $this->uploadTreeController->getClearingHistory(null, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
564  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
565  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
566  }
567 
573  public function testGetHighlightEntries()
574  {
575  $uploadId = 1;
576  $itemId = 200;
577  $res [] = array(
578  "start" => 70,
579  "end" => 70,
580  "type" => "MD",
581  "licenseId" => null,
582  "refStart" => 0,
583  "refEnd" => 53,
584  "infoText" => "MIT: 'MIT License\n\nCopyright (c) <year> <copyright holders>'",
585  "htmlElement" => null
586  );
587 
588  $highlight = new Highlight($res[0]["start"], $res[0]["end"], $res[0]["type"], $res[0]["refStart"], $res[0]["refEnd"], $res[0]["infoText"]);
589  $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree', $uploadId, 1, 2);
590 
591  $this->uploadDao->shouldReceive('isAccessible')
592  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
593  $this->dbHelper->shouldReceive('doesIdExist')
594  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
595  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
596  $this->dbHelper->shouldReceive('doesIdExist')
597  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
598  $this->uploadDao->shouldReceive("getItemTreeBounds")->withArgs([$itemId, "uploadtree"])->andReturn($itemTreeBounds);
599  $this->viewLicensePlugin->shouldReceive('getSelectedHighlighting')->withArgs([$itemTreeBounds, null, null, null, null, $uploadId])->andReturn([$highlight]);
600 
601  $expectedResponse = (new ResponseHelper())->withJson($res, 200);
602  $queryParams = ['clearingId' => null, 'agentId' => null, 'highlightId' => null, 'licenseId' => null];
603  $request = $this->getMockBuilder(ServerRequestInterface::class)
604  ->getMock();
605  $request->expects($this->any())
606  ->method('getQueryParams')
607  ->willReturn($queryParams);
608 
609  $actualResponse = $this->uploadTreeController->getHighlightEntries($request, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
610  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
611  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
612  }
613 
620  public function testGetTreeViewUploadNotFound(){
621  $sort = "sortDir";
622 
623  $this->dbHelper->shouldReceive('doesIdExist')
624  ->withArgs(["upload", "upload_pk", $this->uploadId])->andReturn(false);
625 
626 
627  $reqBody = $this->streamFactory->createStream();
628  $requestHeaders = new Headers();
629 
630  $queryParams = [
631  "agentId" => $this->agentId,
632  'sort' => $sort,
633  "showQuick" => false
634  ];
635  $uri = (new Uri('HTTP', 'localhost'))
636  ->withQuery(http_build_query($queryParams));
637 
638  $requestHeaders->setHeader('Content-Type', 'application/json');
639  $request = new Request("POST", $uri,
640  $requestHeaders, [], [], $reqBody);
641 
642  $this->expectException(HttpNotFoundException::class);
643  $this->uploadTreeController->getTreeView($request, new ResponseHelper(), ["id" => $this->uploadId, "itemId"=> 10]);
644 
645  }
646 
654  public function testGetTreeViewAgentNotFound(){
655  $sort = "sortDir";
656 
657  $this->dbHelper->shouldReceive('doesIdExist')
658  ->withArgs(["upload", "upload_pk", $this->uploadId])->andReturn(false);
659  $this->dbHelper->shouldReceive('doesIdExist')
660  ->withArgs(["agent", "agent_pk", $this->agentId])->andReturn(false);
661  $this->uploadDao->shouldReceive('isAccessible')
662  ->withArgs([$this->uploadId, $this->groupId])->andReturn(true);
663 
664  $reqBody = $this->streamFactory->createStream();
665  $requestHeaders = new Headers();
666 
667  $queryParams = [
668  "agentId" => $this->agentId,
669  'sort' => $sort,
670  "showQuick" => false
671  ];
672  $uri = (new Uri('HTTP', 'localhost'))
673  ->withQuery(http_build_query($queryParams));
674 
675  $requestHeaders->setHeader('Content-Type', 'application/json');
676  $request = new Request("POST", $uri,
677  $requestHeaders, [], [], $reqBody);
678 
679  $this->expectException(HttpNotFoundException::class);
680  $this->uploadTreeController->getTreeView($request, new ResponseHelper(), ["id" => $this->uploadId, "itemId"=> 10]);
681 
682  }
683 
691  public function testGetTreeViewTagNotFound(){
692  $sort = "sortDir";
693 
694  $this->dbHelper->shouldReceive('doesIdExist')
695  ->withArgs(["upload", "upload_pk", $this->uploadId])->andReturn(false);
696  $this->dbHelper->shouldReceive('doesIdExist')
697  ->withArgs(["tag", "tag_pk", $this->tagId])->andReturn(false);
698  $this->uploadDao->shouldReceive('isAccessible')
699  ->withArgs([$this->uploadId, $this->groupId])->andReturn(true);
700 
701  $reqBody = $this->streamFactory->createStream();
702  $requestHeaders = new Headers();
703 
704  $queryParams = [
705  "agentId" => $this->agentId,
706  'sort' => $sort,
707  "showQuick" => false
708  ];
709  $uri = (new Uri('HTTP', 'localhost'))
710  ->withQuery(http_build_query($queryParams));
711 
712  $requestHeaders->setHeader('Content-Type', 'application/json');
713  $request = new Request("POST", $uri,
714  $requestHeaders, [], [], $reqBody);
715 
716  $this->expectException(HttpNotFoundException::class);
717  $this->uploadTreeController->getTreeView($request, new ResponseHelper(), ["id" => $this->uploadId, "itemId"=> 10]);
718 
719  }
720 
729  $sort = "sortDir";
730 
731  $this->dbHelper->shouldReceive('doesIdExist')
732  ->withArgs(["upload", "upload_pk", $this->uploadId])->andReturn(true);
733  $this->uploadDao->shouldReceive('isAccessible')
734  ->withArgs([$this->uploadId, $this->groupId])->andReturn(false);
735 
736  $reqBody = $this->streamFactory->createStream();
737  $requestHeaders = new Headers();
738 
739  $queryParams = [
740  "agentId" => $this->agentId,
741  'sort' => $sort,
742  "showQuick" => false
743  ];
744  $uri = (new Uri('HTTP', 'localhost'))
745  ->withQuery(http_build_query($queryParams));
746  $requestHeaders->setHeader('Content-Type', 'application/json');
747  $request = new Request("POST", $uri,
748  $requestHeaders, [], [], $reqBody);
749  $this->expectException(HttpForbiddenException::class);
750  $this->uploadTreeController->getTreeView($request, new ResponseHelper(), ["id" => $this->uploadId, "itemId"=> 10]);
751  }
752 
753 
754 
761  public function testGetLicenseDecisions()
762  {
763  $itemId = 200;
764  $uploadId = 1;
765  $licenseId = 123;
766  $licenses = array();
767 
768  $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree_a', $uploadId, 0, 1);
769  $addedClearingResults = [];
770 
771  $result = array(
772  'name' => 'Imported decision',
773  'clearingId' => null,
774  'agentId' => null,
775  'highlightId' => null,
776  'page' => 0,
777  'percentage' => null
778  );
779 
780  $license = new License($licenseId, "MIT", "MIT License", "text", "url", [],
781  null, false);
782  $licenseDecision = new LicenseDecision($licenseId, "MIT", "MIT License", 'text', "url", array($result),
783  '', '', false, [], null, false);
784 
785  $licenseRef = new LicenseRef($licenseId, "MIT", "MIT LICENSE", "spx");
786  $clearingEvent = new ClearingEvent(1, $itemId, 12, $this->userId, $this->groupId, 4, new ClearingLicense($licenseRef, false, "", "", "", ""));
787  $licenseDecisionResult = new ClearingResult($clearingEvent, []);
788  $addedClearingResults[$licenseId] = $licenseDecisionResult;
789 
790  $this->uploadDao->shouldReceive('isAccessible')
791  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
792  $this->dbHelper->shouldReceive('doesIdExist')
793  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
794 
795  $this->uploadDao->shouldReceive('getUploadtreeTableName')->withArgs([$uploadId])->andReturn("uploadtree");
796  $this->dbHelper->shouldReceive('doesIdExist')
797  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
798 
799  $this->uploadDao->shouldReceive("getItemTreeBoundsFromUploadId")->withArgs([$itemId, $uploadId])->andReturn($itemTreeBounds);
800  $this->itemTreeBoundsMock->shouldReceive("containsFiles")->andReturn(false);
801  $this->clearingDao->shouldReceive('getMainLicenseIds')->andReturn([]);
802  $this->clearingEventTypes->shouldReceive('getTypeName')->withArgs([$clearingEvent->getEventType()])->andReturn($result['name']);
803  $this->highlightDao->shouldReceive('getHighlightRegion')->withArgs([1])->andReturn([""]);
804  $this->clearingDecisionEventProcessor->shouldReceive("getCurrentClearings")->withArgs([$itemTreeBounds, $this->groupId, LicenseMap::CONCLUSION])->andReturn([$addedClearingResults, []]);
805  $this->licenseDao->shouldReceive('getLicenseObligations')->withArgs([[$licenseId], false])->andReturn([]);
806  $this->licenseDao->shouldReceive('getLicenseObligations')->withArgs([[$licenseId], true])->andReturn([]);
807  $this->licenseDao->shouldReceive('getLicenseById')->withArgs([$licenseId])->andReturn($license);
808  $licenses[] = $licenseDecision->getArray();
809 
810  $expectedResponse = (new ResponseHelper())->withJson($licenses, 200);
811  $actualResponse = $this->uploadTreeController->getLicenseDecisions(null, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
812  $this->assertEquals($expectedResponse->getStatusCode(), $actualResponse->getStatusCode());
813  $this->assertEquals($this->getResponseJson($expectedResponse), $this->getResponseJson($actualResponse));
814  }
815 
822  {
823  $uploadId = 1;
824  $itemId = 200;
825  $shortName = "MIT";
826  $licenseId = 23;
827  $license = new License($licenseId, "MIT", "MIT License", "risk", "texts", [],
828  'type', false);
829  $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree_a', $uploadId, 1, 2);
830  $rq = [
831  array(
832  "shortName" => "MIT",
833  "add" => true
834  )
835  ];
836 
837  $existingLicenses = array(['DT_RowId' => "$itemId,400", 'DT_RowClass' => 'removed']);
838 
839  $this->uploadDao->shouldReceive('isAccessible')
840  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
841  $this->dbHelper->shouldReceive('doesIdExist')
842  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
843  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
844  $this->dbHelper->shouldReceive('doesIdExist')
845  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
846  $this->uploadDao->shouldReceive('getItemTreeBoundsFromUploadId')->withArgs([$itemId, $uploadId])->andReturn($itemTreeBounds);
847  $this->concludeLicensePlugin->shouldReceive('getCurrentSelectedLicensesTableData')->withArgs([$itemTreeBounds, $this->groupId, true])->andReturn($existingLicenses);
848  $this->licenseDao->shouldReceive('getLicenseByShortName')
849  ->withArgs([$shortName, $this->groupId])->andReturn($license);
850  $this->clearingDao->shouldReceive('insertClearingEvent')
851  ->withArgs([$itemId, $this->userId, $this->groupId, $licenseId, false])->andReturn(null);
852 
853  $info = new Info(200, 'Successfully added MIT as a new license decision.', InfoType::INFO);
854  $res = [
855  'success' => [$info->getArray()],
856  'errors' => []
857  ];
858  $expectedResponse = (new ResponseHelper())->withJson($res, 200);
859  $reqBody = $this->streamFactory->createStream(json_encode(
860  $rq
861  ));
862  $requestHeaders = new Headers();
863  $requestHeaders->setHeader('Content-Type', 'application/json');
864  $request = new Request("PUT", new Uri("HTTP", "localhost"),
865  $requestHeaders, [], [], $reqBody);
866  $actualResponse = $this->uploadTreeController->handleAddEditAndDeleteLicenseDecision($request, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
867 
868  $this->assertEquals($expectedResponse->getStatusCode(),
869  $actualResponse->getStatusCode());
870  $this->assertEquals($this->getResponseJson($expectedResponse),
871  $this->getResponseJson($actualResponse));
872  }
873 
880  {
881  $uploadId = 1;
882  $itemId = 200;
883  $shortName = "MIT";
884  $licenseId = 23;
885  $license = new License($licenseId, "MIT", "MIT License", "risk", "texts", [],
886  'type', false);
887  $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree_a', $uploadId, 1, 2);
888  $rq = [
889  array(
890  "shortName" => "MIT",
891  "add" => true,
892  "text" => "Updated license text",
893  )
894  ];
895 
896  $existingLicenses = array(['DT_RowId' => "$itemId,$licenseId", 'DT_RowClass' => 'removed']);
897 
898  $this->uploadDao->shouldReceive('isAccessible')
899  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
900  $this->dbHelper->shouldReceive('doesIdExist')
901  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
902  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
903  $this->dbHelper->shouldReceive('doesIdExist')
904  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
905  $this->uploadDao->shouldReceive('getItemTreeBoundsFromUploadId')->withArgs([$itemId, $uploadId])->andReturn($itemTreeBounds);
906  $this->concludeLicensePlugin->shouldReceive('getCurrentSelectedLicensesTableData')->withArgs([$itemTreeBounds, $this->groupId, true])->andReturn($existingLicenses);
907  $this->licenseDao->shouldReceive('getLicenseByShortName')
908  ->withArgs([$shortName, $this->groupId])->andReturn($license);
909  $this->clearingDao->shouldReceive('updateClearingEvent')
910  ->withArgs([$itemId, $this->userId, $this->groupId, $licenseId, 'reportinfo', "Updated license text"])->andReturn(null);
911 
912  $info = new Info(200, "Successfully updated MIT's license reportinfo", InfoType::INFO);
913  $res = [
914  'success' => [$info->getArray()],
915  'errors' => []
916  ];
917  $expectedResponse = (new ResponseHelper())->withJson($res, 200);
918  $reqBody = $this->streamFactory->createStream(json_encode(
919  $rq
920  ));
921  $requestHeaders = new Headers();
922  $requestHeaders->setHeader('Content-Type', 'application/json');
923  $request = new Request("PUT", new Uri("HTTP", "localhost"),
924  $requestHeaders, [], [], $reqBody);
925  $actualResponse = $this->uploadTreeController->handleAddEditAndDeleteLicenseDecision($request, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
926 
927  $this->assertEquals($expectedResponse->getStatusCode(),
928  $actualResponse->getStatusCode());
929  $this->assertEquals($this->getResponseJson($expectedResponse),
930  $this->getResponseJson($actualResponse));
931  }
932 
939  {
940  $uploadId = 1;
941  $itemId = 200;
942  $shortName = "MIT";
943  $licenseId = 23;
944  $license = new License($licenseId, "MIT", "MIT License", "risk", "texts", [],
945  'type', false);
946  $itemTreeBounds = new ItemTreeBounds($itemId, 'uploadtree_a', $uploadId, 1, 2);
947  $rq = [
948  array(
949  "shortName" => "MIT",
950  "add" => false,
951  "text" => "Updated license text",
952  )
953  ];
954 
955  $existingLicenses = array(['DT_RowId' => "$itemId,$licenseId", 'DT_RowClass' => 'removed']);
956 
957  $this->uploadDao->shouldReceive('isAccessible')
958  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
959  $this->dbHelper->shouldReceive('doesIdExist')
960  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
961  $this->uploadDao->shouldReceive("getUploadtreeTableName")->withArgs([$uploadId])->andReturn("uploadtree");
962  $this->dbHelper->shouldReceive('doesIdExist')
963  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
964  $this->uploadDao->shouldReceive('getItemTreeBoundsFromUploadId')->withArgs([$itemId, $uploadId])->andReturn($itemTreeBounds);
965  $this->concludeLicensePlugin->shouldReceive('getCurrentSelectedLicensesTableData')->withArgs([$itemTreeBounds, $this->groupId, true])->andReturn($existingLicenses);
966  $this->licenseDao->shouldReceive('getLicenseByShortName')
967  ->withArgs([$shortName, $this->groupId])->andReturn($license);
968  $this->clearingDao->shouldReceive('insertClearingEvent')
969  ->withArgs([$itemId, $this->userId, $this->groupId, $licenseId, true])->andReturn(null);
970 
971  $info = new Info(200, 'Successfully deleted MIT from license decision list.', InfoType::INFO);
972  $res = [
973  'success' => [$info->getArray()],
974  'errors' => []
975  ];
976  $expectedResponse = (new ResponseHelper())->withJson($res, 200);
977  $reqBody = $this->streamFactory->createStream(json_encode(
978  $rq
979  ));
980  $requestHeaders = new Headers();
981  $requestHeaders->setHeader('Content-Type', 'application/json');
982  $request = new Request("PUT", new Uri("HTTP", "localhost"),
983  $requestHeaders, [], [], $reqBody);
984  $actualResponse = $this->uploadTreeController->handleAddEditAndDeleteLicenseDecision($request, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
985 
986  $this->assertEquals($expectedResponse->getStatusCode(),
987  $actualResponse->getStatusCode());
988  $this->assertEquals($this->getResponseJson($expectedResponse),
989  $this->getResponseJson($actualResponse));
990  }
991 
997  public function testScheduleBulkScan()
998  {
999  $licenseId = 357;
1000  $licenseShortName = "MIT";
1001  $existingLicense = new License( $licenseId, $licenseShortName, "FULL NAME", "text", "url", [], "1");
1002  $body = [
1003  'bulkActions' => [
1004  [
1005  'licenseShortName' => $licenseShortName,
1006  'licenseText' => '',
1007  'acknowledgement' => '',
1008  'comment' => '',
1009  'licenseAction' => 'ADD'
1010  ]
1011  ],
1012  'refText' => 'Copyright (c) 2011-2023 The Bootstrap Authors',
1013  'bulkScope' => 'folder',
1014  'forceDecision' => 0,
1015  'ignoreIrre' => 0,
1016  'delimiters' => 'DEFAULT',
1017  'scanOnlyFindings' => 0
1018  ];
1019  $jobId = 122;
1020  $res = new JsonResponse(array("jqid" => $jobId));
1021 
1022  $itemId = 1;
1023  $uploadId = 1;
1024 
1025  $this->uploadDao->shouldReceive('isAccessible')
1026  ->withArgs([$uploadId, $this->groupId])->andReturn(true);
1027  $this->dbHelper->shouldReceive('doesIdExist')
1028  ->withArgs(["upload", "upload_pk", $uploadId])->andReturn(true);
1029  $this->uploadDao->shouldReceive('getUploadtreeTableName')->withArgs([$uploadId])->andReturn("uploadtree");
1030  $this->dbHelper->shouldReceive('doesIdExist')
1031  ->withArgs(["uploadtree", "uploadtree_pk", $itemId])->andReturn(true);
1032  $this->dbHelper->shouldReceive('doesIdExist')
1033  ->withArgs(["license_ref", "rf_pk", $licenseId])->andReturn(true);
1034  $this->licenseDao->shouldReceive('getLicenseByShortName')
1035  ->withArgs([$licenseShortName, $this->groupId])
1036  ->andReturn($existingLicense);
1037  $this->changeLicenseBulk->shouldReceive('handle')
1038  ->withArgs([M::any()])
1039  ->andReturn($res);
1040 
1041  $info = new Info(201, json_decode($res->getContent(), true)["jqid"], InfoType::INFO);
1042 
1043  $reqBody = $this->streamFactory->createStream(json_encode(
1044  $body
1045  ));
1046 
1047  $requestHeaders = new Headers();
1048  $requestHeaders->setHeader('Content-Type', 'application/json');
1049  $request = new Request("POST", new Uri("HTTP", "localhost"),
1050  $requestHeaders, [], [], $reqBody);
1051 
1052  $expectedResponse = (new ResponseHelper())->withJson($info->getArray(), $info->getCode());
1053  $actualResponse = $this->uploadTreeController->scheduleBulkScan($request, new ResponseHelper(), ['id' => $uploadId, 'itemId' => $itemId]);
1054 
1055  $this->assertEquals($expectedResponse->getStatusCode(),
1056  $actualResponse->getStatusCode());
1057  $this->assertEquals($this->getResponseJson($expectedResponse),
1058  $this->getResponseJson($actualResponse));
1059  }
1060  }
1061 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Utility functions to process ClearingDecision.
Wrapper class for license map.
Definition: LicenseMap.php:19
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
RepPathItem($Item, $Repo="files")
Given an uploadtree_pk, retrieve the pfile path.
Definition: common-repo.php:91
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16