11 namespace Fossology\Lib\Dao;
17 use PHPUnit\Framework\TestCase;
40 private $assertCountBefore;
54 protected function setUp() : void
56 $this->testDb =
new TestPgDb(
"compatibilitydao");
57 $this->
dbManager = $this->testDb->getDbManager();
58 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
60 $this->testDb->createPlainTables([
"license_ref",
"license_rules"]);
61 $this->testDb->createSequences([
"license_ref_rf_pk_seq",
62 "license_rules_lr_pk_seq"]);
63 $this->testDb->alterTables([
"license_ref",
"license_rules"]);
64 $this->testDb->insertData_license_ref(2);
67 "SELECT rf_pk FROM license_ref ORDER BY rf_pk LIMIT 2;");
68 $this->firstLicenseId = intval($licenses[0][
'rf_pk']);
69 $this->secondLicenseId = intval($licenses[1][
'rf_pk']);
71 $this->authClass = M::mock(
'alias:Fossology\Lib\Auth\Auth');
72 $this->authClass->shouldReceive(
'isAdmin')->andReturn(
true);
75 new Logger(
"test"), M::mock(LicenseDao::class), M::mock(AgentDao::class));
78 protected function tearDown() : void
80 $this->addToAssertionCount(
81 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
95 $ruleId = $this->compatibilityDao->insertRule($this->firstLicenseId,
null,
96 null,
"Strong Copyleft", $comment, $result);
97 $this->assertGreaterThan(0, $ruleId,
"Unable to insert the test rule.");
111 $rule = $this->compatibilityDao->getRuleById($ruleId);
112 $this->assertEquals($this->firstLicenseId, intval($rule[
'first_rf_fk']));
113 $this->assertNull($rule[
'second_rf_fk']);
114 $this->assertNull($rule[
'first_type']);
115 $this->assertEquals(
"Strong Copyleft", $rule[
'second_type']);
116 $this->assertEquals(
"A rule", $rule[
'comment']);
117 $this->assertEquals(
"f", $rule[
'compatibility']);
128 $this->assertEquals(-1, $this->compatibilityDao->insertRule(
129 $this->firstLicenseId,
null,
null,
"Strong Copyleft",
" ",
true));
144 $rule = $this->compatibilityDao->getRuleById($ruleId);
145 $this->assertEquals($ruleId, intval($rule[
'lr_pk']));
146 $this->assertNotEmpty($rule[
'first_rf_shortname']);
147 $this->assertNull($rule[
'second_rf_shortname']);
149 $this->assertNull($this->compatibilityDao->getRuleById($ruleId + 100));
162 $secondRule = $this->
insertRule(
"Second rule");
165 $this->assertEquals(3, $this->compatibilityDao->getTotalRulesCount());
167 $rules = $this->compatibilityDao->getAllRules(2, 0);
168 $this->assertCount(2, $rules);
169 $this->assertEquals($firstRule, intval($rules[0][
'lr_pk']));
170 $this->assertEquals($secondRule, intval($rules[1][
'lr_pk']));
172 $rules = $this->compatibilityDao->getAllRules(2, 2);
173 $this->assertCount(1, $rules);
174 $this->assertEquals(
"Third rule", $rules[0][
'comment']);
186 $this->
insertRule(
"Permissive with copyleft");
189 $this->assertEquals(1,
190 $this->compatibilityDao->getTotalRulesCount(
"%permissive%"));
191 $rules = $this->compatibilityDao->getAllRules(10, 0,
"%permissive%");
192 $this->assertCount(1, $rules);
193 $this->assertEquals(
"Permissive with copyleft", $rules[0][
'comment']);
205 $searchTerm =
"%' OR comment LIKE '%";
207 $this->assertEquals(0,
208 $this->compatibilityDao->getTotalRulesCount($searchTerm));
209 $this->assertCount(0,
210 $this->compatibilityDao->getAllRules(10, 0, $searchTerm));
223 $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray(
224 [$ruleId => [
"result" =>
false]]));
225 $this->assertEquals(
"f",
226 $this->compatibilityDao->getRuleById($ruleId)[
'compatibility']);
228 $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray(
229 [$ruleId => [
"result" =>
true]]));
230 $this->assertEquals(
"t",
231 $this->compatibilityDao->getRuleById($ruleId)[
'compatibility']);
245 $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray(
246 [$ruleId => [
"result" =>
"f"]]));
247 $this->assertEquals(
"f",
248 $this->compatibilityDao->getRuleById($ruleId)[
'compatibility']);
261 $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray([
264 "secondLic" => $this->secondLicenseId,
265 "firstType" =>
"Permissive",
266 "secondType" =>
null,
267 "comment" =>
"An updated rule",
272 $rule = $this->compatibilityDao->getRuleById($ruleId);
273 $this->assertNull($rule[
'first_rf_fk']);
274 $this->assertEquals($this->secondLicenseId, intval($rule[
'second_rf_fk']));
275 $this->assertEquals(
"Permissive", $rule[
'first_type']);
276 $this->assertNull($rule[
'second_type']);
277 $this->assertEquals(
"An updated rule", $rule[
'comment']);
278 $this->assertEquals(
"f", $rule[
'compatibility']);
289 $this->expectException(\UnexpectedValueException::class);
291 $this->compatibilityDao->updateRuleFromArray(
292 [999 => [
"comment" =>
"An updated rule"]]);
305 $this->assertTrue($this->compatibilityDao->deleteRule($ruleId));
306 $this->assertNull($this->compatibilityDao->getRuleById($ruleId));
307 $this->assertEquals(0, $this->compatibilityDao->getTotalRulesCount());
testGetRuleById()
Test for CompatibilityDao::getRuleById()
insertRule($comment, $result=true)
Insert a rule to work on.
testInsertRule()
Test for CompatibilityDao::insertRule()
testInsertRuleWithoutComment()
Test for CompatibilityDao::insertRule() with an empty description.
testUpdateRuleCompatibilityFromBoolean()
Test for CompatibilityDao::updateRuleFromArray() with a boolean.
testUpdateRuleCompatibilityFromDbBoolean()
Test for CompatibilityDao::updateRuleFromArray() with the DB representation of a boolean.
testGetAllRulesPaginated()
Test for CompatibilityDao::getAllRules() with pagination.
testDeleteRule()
Test for CompatibilityDao::deleteRule()
testUpdateRuleFromArray()
Test for CompatibilityDao::updateRuleFromArray() on every field.
testGetAllRulesWithQuoteInSearchTerm()
Test that the search term is bound as a query parameter.
testUpdateRuleWithUnknownId()
Test for CompatibilityDao::updateRuleFromArray() with an unknown id.
testGetAllRulesWithSearchTerm()
Test for CompatibilityDao::getAllRules() with a search term.
fo_dbManager * dbManager
fo_dbManager object