30 use Slim\Psr7\Factory\StreamFactory;
31 use Slim\Psr7\Headers;
32 use Slim\Psr7\Request;
93 protected function setUp() : void
95 global $container, $SysConf;
96 $container = M::mock(
'ContainerBuilder');
97 $this->dbHelper = M::mock(DbHelper::class);
98 $this->
dbManager = M::mock(DbManager::class);
99 $this->restHelper = M::mock(RestHelper::class);
100 $this->compatibilityDao = M::mock(CompatibilityDao::class);
101 $this->licenseYamlPlugin = M::mock(
'admin_license_from_yaml');
103 $this->dbHelper->shouldReceive(
'getDbManager')->andReturn($this->
dbManager);
104 $this->restHelper->shouldReceive(
'getDbHelper')->andReturn($this->dbHelper);
105 $this->restHelper->shouldReceive(
'getPlugin')
106 ->withArgs(array(
'admin_license_from_yaml'))
107 ->andReturn($this->licenseYamlPlugin);
109 $container->shouldReceive(
'get')->withArgs(array(
110 'helper.restHelper'))->andReturn($this->restHelper);
111 $container->shouldReceive(
'get')->withArgs(array(
112 'dao.compatibility'))->andReturn($this->compatibilityDao);
114 $SysConf[
'SYSCONFIG'][
'LicenseTypes'] =
"Permissive, Strong Copyleft";
117 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
118 $this->streamFactory =
new StreamFactory();
127 $this->addToAssertionCount(
128 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
140 $response->getBody()->seek(0);
141 return json_decode($response->getBody()->getContents(),
true);
154 "first_rf_fk" => 306,
155 "second_rf_fk" =>
null,
156 "first_rf_shortname" =>
"MIT",
157 "second_rf_shortname" =>
null,
158 "first_type" =>
"Permissive",
159 "second_type" =>
"Strong Copyleft",
160 "comment" =>
"Rule $ruleId",
161 "compatibility" =>
"t"
174 $version = ApiVersion::V2)
176 $requestHeaders =
new Headers();
177 $requestHeaders->setHeader(
'Content-Type',
'application/json');
178 $request =
new Request($method,
new Uri(
"HTTP",
"localhost"),
179 $requestHeaders, [], [],
180 $this->streamFactory->createStream(json_encode($body)));
181 return $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
193 $request =
new Request(
"GET",
194 new Uri(
"HTTP",
"localhost",
null,
"/license-compatibility-rules",
196 new Headers(), [], [], $this->streamFactory->createStream());
197 return $request->withAttribute(ApiVersion::ATTRIBUTE_NAME, $version);
229 $this->compatibilityDao->shouldReceive(
'getTotalRulesCount')
230 ->withArgs([
""])->andReturn(2);
231 $this->compatibilityDao->shouldReceive(
'getAllRules')
232 ->withArgs([100, 0,
""])->andReturn($rows);
235 foreach ($rows as $row) {
237 ->getArray($version);
240 $actualResponse = $this->ruleController->getRules(
243 $this->assertEquals(200, $actualResponse->getStatusCode());
244 $this->assertEquals(1,
245 intval($actualResponse->getHeaderLine(
'X-Total-Pages')));
246 $this->assertEquals($expected, $this->
getResponseJson($actualResponse));
260 $this->compatibilityDao->shouldReceive(
'getTotalRulesCount')
261 ->withArgs([
"%copyleft%"])->andReturn(3);
262 $this->compatibilityDao->shouldReceive(
'getAllRules')
263 ->withArgs([2, 2,
"%copyleft%"])->andReturn($rows);
265 $actualResponse = $this->ruleController->getRules(
269 $this->assertEquals(200, $actualResponse->getStatusCode());
270 $this->assertEquals(2,
271 intval($actualResponse->getHeaderLine(
'X-Total-Pages')));
284 $this->expectException(HttpBadRequestException::class);
299 $this->expectException(HttpBadRequestException::class);
314 $this->expectException(HttpBadRequestException::class);
328 $this->compatibilityDao->shouldReceive(
'getTotalRulesCount')
329 ->withArgs([
""])->andReturn(2);
330 $this->expectException(HttpBadRequestException::class);
345 $this->compatibilityDao->shouldReceive(
'getTotalRulesCount')
346 ->withArgs([
""])->andReturn(2);
347 $this->expectException(HttpBadRequestException::class);
361 $this->expectException(HttpForbiddenException::class);
375 $this->dbHelper->shouldReceive(
'doesIdExist')
376 ->withArgs([
"license_ref",
"rf_pk", 306])->andReturn(
true);
377 $this->compatibilityDao->shouldReceive(
'insertRule')
378 ->withArgs([306,
null,
null,
"Strong Copyleft",
"New rule",
true])
382 "firstLicenseId" => 306,
383 "secondType" =>
"Strong Copyleft",
384 "comment" =>
"New rule",
385 "compatibility" =>
true
388 $expectedResponse =
new Info(201,
"Rule 9 added successfully.",
390 $actualResponse = $this->ruleController->createRule($request,
393 $this->assertEquals($expectedResponse->getCode(),
394 $actualResponse->getStatusCode());
395 $this->assertEquals($expectedResponse->getArray(),
396 $this->getResponseJson($actualResponse));
408 $this->compatibilityDao->shouldReceive(
'insertRule')
409 ->withArgs([
null,
null,
"Permissive",
null,
"New rule",
false])
413 "first_type" =>
"Permissive",
414 "comment" =>
"New rule",
415 "compatibility" =>
false
418 $actualResponse = $this->ruleController->createRule($request,
421 $this->assertEquals(201, $actualResponse->getStatusCode());
433 $this->expectException(HttpBadRequestException::class);
436 "firstType" =>
"Permissive",
437 "compatibility" =>
true
450 $this->expectException(HttpBadRequestException::class);
453 "firstType" =>
"Permissive",
455 "compatibility" =>
true
468 $this->expectException(HttpBadRequestException::class);
471 "comment" =>
"Default rule",
472 "compatibility" =>
true
485 $this->dbHelper->shouldReceive(
'doesIdExist')
486 ->withArgs([
"license_ref",
"rf_pk", 999])->andReturn(
false);
487 $this->expectException(HttpBadRequestException::class);
490 "firstLicenseId" => 999,
491 "comment" =>
"New rule",
492 "compatibility" =>
true
505 $this->expectException(HttpBadRequestException::class);
508 "firstType" =>
"Unknown Type",
509 "comment" =>
"New rule",
510 "compatibility" =>
true
523 $this->compatibilityDao->shouldReceive(
'insertRule')->andReturn(-2);
524 $this->expectException(HttpInternalServerErrorException::class);
527 "firstType" =>
"Permissive",
528 "comment" =>
"New rule",
529 "compatibility" =>
true
543 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([$ruleId])
545 $this->compatibilityDao->shouldReceive(
'updateRuleFromArray')
546 ->withArgs([[$ruleId => [
"comment" =>
"Updated rule",
547 "result" =>
false]]])->andReturn(1);
550 "comment" =>
"Updated rule",
551 "compatibility" =>
false
554 $expectedResponse =
new Info(200,
"Rule $ruleId updated successfully.",
556 $actualResponse = $this->ruleController->updateRule($request,
559 $this->assertEquals($expectedResponse->getCode(),
560 $actualResponse->getStatusCode());
561 $this->assertEquals($expectedResponse->getArray(),
562 $this->getResponseJson($actualResponse));
575 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([$ruleId])
577 $this->compatibilityDao->shouldReceive(
'updateRuleFromArray')
578 ->withArgs([[$ruleId => [
"firstLic" =>
null,
"firstType" =>
null]]])
582 "firstLicenseId" =>
null,
586 $actualResponse = $this->ruleController->updateRule($request,
589 $this->assertEquals(200, $actualResponse->getStatusCode());
601 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([8])
603 $this->expectException(HttpNotFoundException::class);
605 $this->ruleController->updateRule(
619 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([4])
621 $this->expectException(HttpBadRequestException::class);
636 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([4])
638 $this->expectException(HttpBadRequestException::class);
640 $this->ruleController->updateRule(
655 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([$ruleId])
657 $this->compatibilityDao->shouldReceive(
'deleteRule')->withArgs([$ruleId])
660 $expectedResponse =
new Info(200,
"Rule $ruleId deleted successfully.",
662 $actualResponse = $this->ruleController->deleteRule(
null,
665 $this->assertEquals($expectedResponse->getCode(),
666 $actualResponse->getStatusCode());
667 $this->assertEquals($expectedResponse->getArray(),
668 $this->getResponseJson($actualResponse));
680 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([8])
682 $this->expectException(HttpNotFoundException::class);
697 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([4])
699 $this->compatibilityDao->shouldReceive(
'deleteRule')->withArgs([4])
701 $this->expectException(HttpInternalServerErrorException::class);
718 $this->licenseYamlPlugin->shouldReceive(
'getFileInputName')
719 ->andReturn(
"fileInput");
720 $this->licenseYamlPlugin->shouldReceive(
'handleFileUpload')
721 ->withArgs([
null,
true])
722 ->andReturn([
true,
"Read yaml: 2 license rules", 200]);
724 $expectedResponse =
new Info(200,
"Read yaml: 2 license rules",
726 $actualResponse = $this->ruleController->importRules(
729 $this->assertEquals($expectedResponse->getCode(),
730 $actualResponse->getStatusCode());
731 $this->assertEquals($expectedResponse->getArray(),
732 $this->getResponseJson($actualResponse));
746 $this->licenseYamlPlugin->shouldReceive(
'getFileInputName')
747 ->andReturn(
"fileInput");
748 $this->licenseYamlPlugin->shouldReceive(
'handleFileUpload')
749 ->withArgs([
null,
true])->andReturn([
false,
"No file selected", 400]);
750 $this->expectException(HttpBadRequestException::class);
765 "firstname" =>
"MIT",
766 "secondname" =>
null,
768 "secondtype" =>
"Strong Copyleft",
769 "compatibility" =>
"true",
770 "comment" =>
"Rule 1"
772 $this->compatibilityDao->shouldReceive(
'getDefaultCompatibility')
774 $this->
dbManager->shouldReceive(
'getRows')->andReturn($rows);
776 $actualResponse = $this->ruleController->exportRules(
779 $this->assertEquals(200, $actualResponse->getStatusCode());
780 $this->assertEquals(
"text/x-yaml; charset=UTF-8",
781 $actualResponse->getHeaderLine(
'Content-type'));
782 $actualResponse->getBody()->seek(0);
783 $this->assertEquals(yaml_emit([
"default" =>
false,
"rules" => $rows]),
784 $actualResponse->getBody()->getContents());
796 $this->compatibilityDao->shouldReceive(
'getRuleById')->withArgs([8])
798 $this->expectException(HttpNotFoundException::class);
813 $this->expectException(HttpForbiddenException::class);
Contains the constants and helpers for authentication of user.
Controller for license compatibility rules.
Provides helper methods to access database for REST api.
Override Slim response for withJson function.
Provides various DAO helper functions for REST api.
Different type of infos provided by REST.
Info model to contain general error and return values.
Model to hold a single license compatibility rule.
Unit tests for LicenseCompatibilityRuleController.
testCreateRuleWithV1FieldNames()
createGetRequest($query="", $version=ApiVersion::V2)
testUpdateRuleResetLicense()
testUpdateRuleEmptyBody()
testGetRulesPageOutOfRange()
testGetRulesWithPaginationAndSearch()
createJsonRequest($method, $body, $version=ApiVersion::V2)
testGetRulesNonNumericLimit()
tearDown()
Remove test objects.
testGetRulesInvalidLimit()
testUpdateRuleWithoutRuleValues()
testCreateRuleWithUnknownLicenseType()
testCreateRuleWithUnknownLicense()
setUp()
Setup test objects.
testExportRulesNotAdmin()
testCreateRuleWithoutComment()
testCreateRuleWithoutLicenseAndType()
testImportRulesInvalidFile()
getResponseJson($response)
testGetRules($version=ApiVersion::V2)
testExportRulesNotFound()
testCreateRuleWithEmptyComment()
fo_dbManager * dbManager
fo_dbManager object