FOSSology  4.7.1
Open Source License Compliance by Open Source Software
CompatibilityDaoTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Harshit Gandhi <gandhiharshit716@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
11 namespace Fossology\Lib\Dao;
12 
15 use Mockery as M;
16 use Monolog\Logger;
17 use PHPUnit\Framework\TestCase;
18 
25 class CompatibilityDaoTest extends TestCase
26 {
29  private $testDb;
30 
33  private $dbManager;
34 
38 
40  private $assertCountBefore;
41 
44  private $authClass;
45 
48  private $firstLicenseId;
49 
53 
54  protected function setUp() : void
55  {
56  $this->testDb = new TestPgDb("compatibilitydao");
57  $this->dbManager = $this->testDb->getDbManager();
58  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
59 
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);
65 
66  $licenses = $this->dbManager->getRows(
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']);
70 
71  $this->authClass = M::mock('alias:Fossology\Lib\Auth\Auth');
72  $this->authClass->shouldReceive('isAdmin')->andReturn(true);
73 
74  $this->compatibilityDao = new CompatibilityDao($this->dbManager,
75  new Logger("test"), M::mock(LicenseDao::class), M::mock(AgentDao::class));
76  }
77 
78  protected function tearDown() : void
79  {
80  $this->addToAssertionCount(
81  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
82  M::close();
83  $this->testDb = null;
84  $this->dbManager = null;
85  }
86 
93  private function insertRule($comment, $result = true)
94  {
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.");
98  return $ruleId;
99  }
100 
107  public function testInsertRule()
108  {
109  $ruleId = $this->insertRule("A rule", false);
110 
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']);
118  }
119 
127  {
128  $this->assertEquals(-1, $this->compatibilityDao->insertRule(
129  $this->firstLicenseId, null, null, "Strong Copyleft", " ", true));
130  }
131 
140  public function testGetRuleById()
141  {
142  $ruleId = $this->insertRule("A rule");
143 
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']);
148 
149  $this->assertNull($this->compatibilityDao->getRuleById($ruleId + 100));
150  }
151 
159  public function testGetAllRulesPaginated()
160  {
161  $firstRule = $this->insertRule("First rule");
162  $secondRule = $this->insertRule("Second rule");
163  $this->insertRule("Third rule");
164 
165  $this->assertEquals(3, $this->compatibilityDao->getTotalRulesCount());
166 
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']));
171 
172  $rules = $this->compatibilityDao->getAllRules(2, 2);
173  $this->assertCount(1, $rules);
174  $this->assertEquals("Third rule", $rules[0]['comment']);
175  }
176 
185  {
186  $this->insertRule("Permissive with copyleft");
187  $this->insertRule("Copyleft with copyleft");
188 
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']);
194  }
195 
203  {
204  $this->insertRule("A rule");
205  $searchTerm = "%' OR comment LIKE '%";
206 
207  $this->assertEquals(0,
208  $this->compatibilityDao->getTotalRulesCount($searchTerm));
209  $this->assertCount(0,
210  $this->compatibilityDao->getAllRules(10, 0, $searchTerm));
211  }
212 
220  {
221  $ruleId = $this->insertRule("A rule", true);
222 
223  $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray(
224  [$ruleId => ["result" => false]]));
225  $this->assertEquals("f",
226  $this->compatibilityDao->getRuleById($ruleId)['compatibility']);
227 
228  $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray(
229  [$ruleId => ["result" => true]]));
230  $this->assertEquals("t",
231  $this->compatibilityDao->getRuleById($ruleId)['compatibility']);
232  }
233 
242  {
243  $ruleId = $this->insertRule("A rule", true);
244 
245  $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray(
246  [$ruleId => ["result" => "f"]]));
247  $this->assertEquals("f",
248  $this->compatibilityDao->getRuleById($ruleId)['compatibility']);
249  }
250 
257  public function testUpdateRuleFromArray()
258  {
259  $ruleId = $this->insertRule("A rule");
260 
261  $this->assertEquals(1, $this->compatibilityDao->updateRuleFromArray([
262  $ruleId => [
263  "firstLic" => null,
264  "secondLic" => $this->secondLicenseId,
265  "firstType" => "Permissive",
266  "secondType" => null,
267  "comment" => "An updated rule",
268  "result" => false
269  ]
270  ]));
271 
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']);
279  }
280 
287  public function testUpdateRuleWithUnknownId()
288  {
289  $this->expectException(\UnexpectedValueException::class);
290 
291  $this->compatibilityDao->updateRuleFromArray(
292  [999 => ["comment" => "An updated rule"]]);
293  }
294 
301  public function testDeleteRule()
302  {
303  $ruleId = $this->insertRule("A rule");
304 
305  $this->assertTrue($this->compatibilityDao->deleteRule($ruleId));
306  $this->assertNull($this->compatibilityDao->getRuleById($ruleId));
307  $this->assertEquals(0, $this->compatibilityDao->getTotalRulesCount());
308  }
309 }
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
Definition: process.c:16