FOSSology  4.7.1
Open Source License Compliance by Open Source Software
LicenseDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Steffen Weber
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
19 
20 class LicenseDaoTest extends \PHPUnit\Framework\TestCase
21 {
22  public $assertCountBefore;
23 
25  private $testDb;
27  private $dbManager;
29  private $licenseDao;
30 
31  protected function setUp() : void
32  {
33  $this->testDb = new TestPgDb();
34  $this->testDb->createPlainTables(array('obligation_ref','obligation_map','obligation_candidate_map'));
35  $this->dbManager = $this->testDb->getDbManager();
36  $this->licenseDao = new LicenseDao($this->dbManager);
37  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
38  }
39 
40  protected function tearDown() : void
41  {
42  $this->testDb = null;
43  $this->dbManager = null;
44  }
45 
49  private function setUpLicenseRefTable()
50  {
51  $this->testDb->createPlainTables(array('license_ref'));
52  $this->testDb->createSequences(array('license_ref_rf_pk_seq'));
53  $this->testDb->alterTables(array('license_ref'));
54  }
55 
56  public function testGetFileLicenseMatches()
57  {
58  $this->testDb->createPlainTables(array('uploadtree','license_file','agent'));
59  $this->setUpLicenseRefTable();
60  $this->testDb->insertData_license_ref();
61 
62  $lic0 = $this->dbManager->getSingleRow("Select * from license_ref limit 1");
63  $licenseRefNumber = $lic0['rf_pk'];
64  $licenseFileId= 1;
65  $pfileId= 42;
66  $agentId = 23;
67  $matchPercent = 50;
68  $uploadtreeId= 512;
69  $uploadID =123;
70  $left=2009;
71  $right=2014;
72  $agentName="fake";
73  $agentRev=1;
74  $mydate = "'2014-06-04 14:01:30.551093+02'";
75  $this->testDb->createViews(array('license_file_ref'));
76  $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk, rf_fk, agent_fk, rf_match_pct, rf_timestamp, pfile_fk)
77  VALUES ($licenseFileId, $licenseRefNumber, $agentId, $matchPercent, $mydate, $pfileId)");
78  $this->dbManager->queryOnce("INSERT INTO uploadtree (uploadtree_pk, upload_fk, pfile_fk, lft, rgt)
79  VALUES ($uploadtreeId, $uploadID, $pfileId, $left, $right)");
80  $stmt = __METHOD__.'.insert.agent';
81  $this->dbManager->prepare($stmt,"INSERT INTO agent (agent_pk, agent_name, agent_rev, agent_enabled) VALUES ($1,$2,$3,$4)");
82  $this->dbManager->execute($stmt,array($agentId, $agentName, $agentRev, 'true'));
83 
84  $licDao = new LicenseDao($this->dbManager);
85  $itemTreeBounds = new ItemTreeBounds($uploadtreeId,"uploadtree",$uploadID,$left,$right);
86  $matches = $licDao->getAgentFileLicenseMatches($itemTreeBounds);
87 
88  $licenseRef = new LicenseRef($licenseRefNumber, $lic0['rf_shortname'], $lic0['rf_fullname'], $lic0['rf_spdx_id']);
89  $agentRef = new AgentRef($agentId, $agentName, $agentRev);
90  $expected = array( new LicenseMatch($pfileId, $licenseRef, $agentRef, $licenseFileId, $matchPercent) );
91 
92  assertThat($matches, equalTo($expected));
93  assertThat($matches[0], is(anInstanceOf(LicenseMatch::class)) );
94  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
95  }
96 
97  public function testGetLicenseByShortName()
98  {
99  $this->setUpLicenseRefTable();
100  $this->testDb->insertData_license_ref($limit=3);
101  $licDao = new LicenseDao($this->dbManager);
102  $lic0 = $this->dbManager->getSingleRow("Select rf_shortname from license_ref limit 1");
103  $sname = $lic0['rf_shortname'];
104  $lic = $licDao->getLicenseByShortName($sname);
105  $this->assertInstanceOf('Fossology\Lib\Data\License', $lic);
106  $this->assertEquals($sname, $lic->getShortName());
107 
108  $sname = "Self-destructing license";
109  $lic = $licDao->getLicenseByShortName($sname);
110  $this->assertNull($lic);
111  }
112 
113  public function testGetLicenseId()
114  {
115  $this->setUpLicenseRefTable();
116  $this->testDb->insertData_license_ref($limit=3);
117  $licDao = new LicenseDao($this->dbManager);
118  $lic0 = $this->dbManager->getSingleRow("Select rf_pk from license_ref limit 1");
119  $id = $lic0['rf_pk'];
120  $lic = $licDao->getLicenseById($id);
121  $this->assertInstanceOf('Fossology\Lib\Data\License', $lic);
122  $this->assertEquals($id, $lic->getId());
123 
124  $invalidId = -1;
125  $lic = $licDao->getLicenseById($invalidId);
126  $this->assertNull($lic);
127  }
128 
129  public function testGetLicenseRefs()
130  {
131  $this->setUpLicenseRefTable();
132  $this->testDb->insertData_license_ref();
133  $licDao = new LicenseDao($this->dbManager);
134  $licAll = $licDao->getLicenseRefs();
135  $cntA = $this->dbManager->getSingleRow("Select count(*) cnt from license_ref limit 1");
136  $this->assertEquals($cntA['cnt'], count($licAll));
137  $this->assertInstanceOf('Fossology\Lib\Data\LicenseRef', $licAll[0]);
138  }
139 
140  public function testGetLicenseShortnamesContained()
141  {
142  $this->testDb->createPlainTables(array('license_file','uploadtree'));
143  $this->setUpLicenseRefTable();
144  $this->dbManager->queryOnce("CREATE TABLE \"uploadtree_a\" AS SELECT * FROM uploadtree");
145  $this->testDb->createViews(array('license_file_ref'));
146  $this->testDb->insertData(array('license_file','uploadtree_a'));
147  $this->testDb->insertData_license_ref($limit=3);
148  $stmt = __METHOD__.'.select.license_ref';
149  $this->dbManager->prepare($stmt,"SELECT rf_pk,rf_shortname FROM license_ref");
150  $licRes = $this->dbManager->execute($stmt);
151  $licAll = array();
152  while ($erg=$this->dbManager->fetchArray($licRes)) {
153  $licAll[$erg['rf_pk']] = $erg['rf_shortname'];
154  }
155  $this->dbManager->freeResult($licRes);
156  $pfileId= 42;
157  $agentId = 23;
158  $matchPercent = 50;
159  $uploadtreeId= 512;
160  $uploadId =123;
161  $left=2009;
162  $right=2014;
163  $mydate = "'2014-06-04 14:01:30.551093+02'";
164  foreach ($licAll as $licenseRefNumber=>$shortname) {
165  $this->dbManager->queryOnce("INSERT INTO license_file (rf_fk, agent_fk, rf_match_pct, rf_timestamp, pfile_fk)
166  VALUES ($licenseRefNumber, $agentId, $matchPercent, $mydate, $pfileId)");
167  }
168  $this->dbManager->queryOnce("INSERT INTO uploadtree (uploadtree_pk, upload_fk, pfile_fk, lft, rgt)
169  VALUES ($uploadtreeId, $uploadId, $pfileId, $left, $right)");
170 
171  $licDao = new LicenseDao($this->dbManager);
172  $itemTreeBounds = new ItemTreeBounds($uploadtreeId,"uploadtree",$uploadId,$left,$right);
173  $licenses = $licDao->getLicenseShortnamesContained($itemTreeBounds);
174 
175  assertThat($licenses, is(arrayContainingInAnyOrder(array_values($licAll))));
176 
177  $licensesForBadAgent = $licDao->getLicenseShortnamesContained($itemTreeBounds,array(2*$agentId));
178  assertThat($licensesForBadAgent, is(emptyArray()));
179 
180  $licensesForNoAgent = $licDao->getLicenseShortnamesContained($itemTreeBounds,array());
181  assertThat($licensesForNoAgent, is(emptyArray()));
182 
183  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
184  }
185 
186 
187  public function testGetLicenseIdPerPfileForAgentId()
188  {
189  $this->testDb->createPlainTables(array('license_file','uploadtree','agent'));
190  $this->setUpLicenseRefTable();
191  $this->testDb->insertData(array('agent'));
192  $this->testDb->createViews(array('license_file_ref'));
193  $this->testDb->insertData_license_ref($limit=3);
194  $licAll = $this->dbManager->createMap('license_ref', 'rf_pk','rf_shortname');
195  $rf_pk_all = array_keys($licAll);
196  $rf_pk = $rf_pk_all[0];
197  $uploadtreetable_name = 'uploadtree';
198  $this->dbManager->insertInto('license_file',
199  'fl_pk, rf_fk, agent_fk, rf_match_pct, rf_timestamp, pfile_fk, server_fk',
200  array(1, $rf_pk, $agentId = 5, $matchPercent = 50, $mydate = "'2014-06-04 14:01:30.551093+02'", $pfileId=42, 1) );
201  $uploadtreeId= 512;
202  $uploadId =123;
203  $left=2009;
204  $containerMode = 1<<29;
205  $nonArtifactChildId = $uploadtreeId+2;
206  $this->dbManager->insertTableRow('uploadtree',
207  array('uploadtree_pk'=>$uploadtreeId, 'upload_fk'=>$uploadId, 'pfile_fk'=>0,
208  'lft'=>$left, 'rgt'=>$left+5, 'parent'=>NULL, 'ufile_mode'=>$containerMode));
209  $this->dbManager->insertTableRow('uploadtree',
210  array('uploadtree_pk'=>$uploadtreeId+1, 'upload_fk'=>$uploadId, 'pfile_fk'=>0,
211  'lft'=>$left+1, 'rgt'=>$left+4, 'parent'=>$uploadtreeId, 'ufile_mode'=>$containerMode));
212  $this->dbManager->insertTableRow('uploadtree',
213  array('uploadtree_pk'=>$uploadtreeId+2, 'upload_fk'=>$uploadId, 'pfile_fk'=>$pfileId,
214  'lft'=>$left+2, 'rgt'=>$left+3, 'parent'=>$uploadtreeId+1, 'ufile_mode'=>0));
215 
216  $licDao = new LicenseDao($this->dbManager);
217  $itemTreeBounds = new ItemTreeBounds($uploadtreeId,$uploadtreetable_name,$uploadId,$left,$left+5);
218 
219  $row = array('pfile_id'=>$pfileId,'license_id'=>$rf_pk,'match_percentage'=>$matchPercent,'agent_id'=>$agentId,'uploadtree_pk'=>$nonArtifactChildId);
220  $expected = array($pfileId=>array($rf_pk=>$row));
221  $itemRestriction = array($nonArtifactChildId, $nonArtifactChildId+7);
222 
223  $licensesForGoodAgent = $licDao->getLicenseIdPerPfileForAgentId($itemTreeBounds, $selectedAgentId = $agentId, $itemRestriction);
224  assertThat($licensesForGoodAgent, is(equalTo($expected)));
225 
226  $licensesForBadAgent = $licDao->getLicenseIdPerPfileForAgentId($itemTreeBounds, $selectedAgentId = 1+$agentId, $itemRestriction);
227  assertThat($licensesForBadAgent, is(equalTo(array())));
228 
229  $licensesOutside = $licDao->getLicenseIdPerPfileForAgentId($itemTreeBounds, $selectedAgentId = $agentId, array());
230  assertThat($licensesOutside, is(equalTo(array())));
231 
232  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
233  }
234 
235  public function testGetLicensesPerFileNameForAgentId()
236  {
237  $this->testDb->createPlainTables(array('license_file','uploadtree','agent'));
238  $this->setUpLicenseRefTable();
239  $this->testDb->insertData(array('agent'));
240  $this->testDb->createViews(array('license_file_ref'));
241  $this->testDb->insertData_license_ref($limit=3);
242  $licAll = $this->dbManager->createMap('license_ref', 'rf_pk','rf_shortname');
243  $rf_pk_all = array_keys($licAll);
244 
245  $uploadtreetable_name = 'uploadtree';
246  // uploadtree_pk | parent | realparent | upload_fk | pfile_fk | ufile_mode | lft | rgt | ufile_name
247  // ---------------+--------+------------+-----------+----------+------------+-----+-----+------------------
248  // 80895 | | | 16 | 70585 | 536904704 | 1 | 36 | project.tar.gz
249  // 80896 | 80895 | 80895 | 16 | 0 | 805323776 | 2 | 35 | artifact.dir
250  // 80897 | 80896 | 80895 | 16 | 70586 | 536903680 | 3 | 34 | project.tar
251  // 80898 | 80897 | 80897 | 16 | 0 | 805323776 | 4 | 33 | artifact.dir
252  // 80899 | 80898 | 80897 | 16 | 0 | 536888320 | 5 | 32 | project
253  // 80900 | 80899 | 80899 | 16 | 0 | 536888320 | 6 | 7 | folderA
254  // 80905 | 80899 | 80899 | 16 | 0 | 536888320 | 8 | 23 | folderB
255  // 80907 | 80905 | 80905 | 16 | 0 | 536888320 | 9 | 10 | subBfolderA
256  // 80908 | 80905 | 80905 | 16 | 0 | 536888320 | 11 | 20 | subBfolderB
257  // 80909 | 80908 | 80908 | 16 | 0 | 536888320 | 12 | 19 | subBBsubBfolderA
258  // 80912 | 80909 | 80909 | 16 | 70592 | 33152 | 13 | 14 | BBBfileA
259  // 80911 | 80909 | 80909 | 16 | 70591 | 33152 | 15 | 16 | BBBfileB
260  // 80910 | 80909 | 80909 | 16 | 70590 | 33152 | 17 | 18 | BBBfileC
261  // 80906 | 80905 | 80905 | 16 | 0 | 536888320 | 21 | 22 | subBfolderC
262  // 80901 | 80899 | 80899 | 16 | 0 | 536888320 | 24 | 31 | folderC
263  // 80903 | 80901 | 80901 | 16 | 70588 | 33152 | 25 | 26 | CfileA
264  // 80904 | 80901 | 80901 | 16 | 70589 | 33152 | 27 | 28 | CfileB
265  // 80902 | 80901 | 80901 | 16 | 70587 | 33152 | 29 | 30 | CfileC
266  $mainUploadtreeId = 80895;
267  $uploadtreeId = $mainUploadtreeId;
268  $uploadId = 16;
269  /* 80895 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'pfile_fk'=>70585, 'lft'=>1, 'rgt'=>36, 'ufile_mode'=>536904704, 'ufile_name'=>'project.tar.gz'));
270  /* 80896 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80895, 'realparent'=>80895, 'pfile_fk'=>0, 'lft'=>2, 'rgt'=>35, 'ufile_mode'=>805323776, 'ufile_name'=>'artifact.dir'));
271  /* 80897 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80896, 'realparent'=>80895, 'pfile_fk'=>70586, 'lft'=>3, 'rgt'=>34, 'ufile_mode'=>536903680, 'ufile_name'=>'project.tar'));
272  /* 80898 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80897, 'realparent'=>80897, 'pfile_fk'=>0, 'lft'=>4, 'rgt'=>33, 'ufile_mode'=>805323776, 'ufile_name'=>'artifact.dir'));
273  /* 80899 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80898, 'realparent'=>80897, 'pfile_fk'=>0, 'lft'=>5, 'rgt'=>32, 'ufile_mode'=>536888320, 'ufile_name'=>'project'));
274  /* 80900 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80899, 'realparent'=>80899, 'pfile_fk'=>0, 'lft'=>6, 'rgt'=>7, 'ufile_mode'=>536888320, 'ufile_name'=>'folderA'));
275  /* 80901 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80899, 'realparent'=>80899, 'pfile_fk'=>0, 'lft'=>24, 'rgt'=>31, 'ufile_mode'=>536888320, 'ufile_name'=>'folderC'));
276  /* 80902 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80901, 'realparent'=>80901, 'pfile_fk'=>70587, 'lft'=>29, 'rgt'=>30, 'ufile_mode'=>33152, 'ufile_name'=>'CfileC'));
277  /* 80903 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80901, 'realparent'=>80901, 'pfile_fk'=>70588, 'lft'=>25, 'rgt'=>26, 'ufile_mode'=>33152, 'ufile_name'=>'CfileA'));
278  /* 80904 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80901, 'realparent'=>80901, 'pfile_fk'=>70589, 'lft'=>27, 'rgt'=>28, 'ufile_mode'=>33152, 'ufile_name'=>'CfileB'));
279  /* 80905 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80899, 'realparent'=>80899, 'pfile_fk'=>0, 'lft'=>8, 'rgt'=>23, 'ufile_mode'=>536888320, 'ufile_name'=>'folderB'));
280  /* 80906 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80905, 'realparent'=>80905, 'pfile_fk'=>0, 'lft'=>21, 'rgt'=>22, 'ufile_mode'=>536888320, 'ufile_name'=>'subBfolderC'));
281  /* 80907 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80905, 'realparent'=>80905, 'pfile_fk'=>0, 'lft'=>9, 'rgt'=>10, 'ufile_mode'=>536888320, 'ufile_name'=>'subBfolderA'));
282  /* 80908 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80905, 'realparent'=>80905, 'pfile_fk'=>0, 'lft'=>11, 'rgt'=>20, 'ufile_mode'=>536888320, 'ufile_name'=>'subBfolderB'));
283  /* 80909 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80908, 'realparent'=>80908, 'pfile_fk'=>0, 'lft'=>12, 'rgt'=>19, 'ufile_mode'=>536888320, 'ufile_name'=>'subBBsubBfolderA'));
284  /* 80910 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80909, 'realparent'=>80909, 'pfile_fk'=>70590, 'lft'=>17, 'rgt'=>18, 'ufile_mode'=>33152, 'ufile_name'=>'BBBfileC'));
285  /* 80911 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80909, 'realparent'=>80909, 'pfile_fk'=>70591, 'lft'=>15, 'rgt'=>16, 'ufile_mode'=>33152, 'ufile_name'=>'BBBfileB'));
286  /* 80912 */ $this->dbManager->insertTableRow($uploadtreetable_name, array('uploadtree_pk'=>$uploadtreeId++, 'upload_fk'=>$uploadId, 'parent'=>80909, 'realparent'=>80909, 'pfile_fk'=>70592, 'lft'=>13, 'rgt'=>14, 'ufile_mode'=>33152, 'ufile_name'=>'BBBfileA'));
287 
288  $agentId = 5;
289  // fl_pk | rf_fk | agent_fk | rf_match_pct | rf_timestamp | pfile_fk | server_fk | fl_ref_start_byte | fl_ref_end_byte | fl_start_byte | fl_end_byte
290  // --------+---------------+--------------+--------------+-------------------------------+----------+-----------+-------------------+-----------------+---------------+-------------
291  // 1 | $rf_pk_all[0] | $agentId | | 2016-02-08 16:08:59.333096+00 | 70592 | 1 | | | |
292  // 2 | $rf_pk_all[1] | $agentId + 1 | | 2016-02-08 16:08:59.333096+00 | 70591 | 1 | | | |
293  // 3 | $rf_pk_all[0] | $agentId | | 2016-02-08 16:08:59.333096+00 | 70590 | 1 | | | |
294  // 4 | $rf_pk_all[1] | $agentId | | 2016-02-08 16:08:59.333096+00 | 70590 | 1 | | | |
295  $someDate = "'2016-02-08 16:08:59.333096+00'";
296  /* 1 */ $this->dbManager->insertInto('license_file', 'fl_pk, rf_fk, agent_fk, rf_timestamp, pfile_fk, server_fk', array(1, $rf_pk_all[0], $agentId, $someDate, 70592, 1) );
297  /* 2 */ $this->dbManager->insertInto('license_file', 'fl_pk, rf_fk, agent_fk, rf_timestamp, pfile_fk, server_fk', array(2, $rf_pk_all[1], $agentId+1, $someDate, 70591, 1) );
298  /* 3 */ $this->dbManager->insertInto('license_file', 'fl_pk, rf_fk, agent_fk, rf_timestamp, pfile_fk, server_fk', array(3, $rf_pk_all[0], $agentId, $someDate, 70590, 1) );
299  /* 4 */ $this->dbManager->insertInto('license_file', 'fl_pk, rf_fk, agent_fk, rf_timestamp, pfile_fk, server_fk', array(4, $rf_pk_all[1], $agentId, $someDate, 70590, 1) );
300 
301  $licDao = new LicenseDao($this->dbManager);
302  $itemTreeBounds = new ItemTreeBounds($mainUploadtreeId,$uploadtreetable_name,$uploadId,1,36);
303 
304  //**************************************************************************
305  // Test with minimal input
306  $result = $licDao->getLicensesPerFileNameForAgentId($itemTreeBounds);
307 
308  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileA";
309  $this->assertArrayHasKey($key, $result);
310  $expected = $licAll[$rf_pk_all[0]];
311  assertThat($result[$key]['scanResults'][0], is(equalTo($expected)));
312 
313  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileB";
314  $this->assertArrayHasKey($key, $result);
315 
316  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileC";
317  $this->assertArrayHasKey($key, $result);
318  $this->assertContains($licAll[$rf_pk_all[0]],$result[$key]['scanResults']);
319  $this->assertContains($licAll[$rf_pk_all[1]],$result[$key]['scanResults']);
320 
321  $key = "project.tar.gz";
322  $this->assertArrayHasKey($key, $result);
323 
324  //**************************************************************************
325  // Test with empty agent list
326  $result = $licDao->getLicensesPerFileNameForAgentId($itemTreeBounds, array(),true,'',true);
327 
328  $expected = array();
329  assertThat($result, is(equalTo($expected)));
330 
331  //**************************************************************************
332  // Test with only one agent
333  $result = $licDao->getLicensesPerFileNameForAgentId($itemTreeBounds, array($agentId));
334 
335  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileA";
336  $this->assertArrayHasKey($key, $result);
337 
338  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileB";
339  $this->assertArrayNotHasKey($key, $result);
340 
341  //**************************************************************************
342  // Test with excluding
343  $result = $licDao->getLicensesPerFileNameForAgentId($itemTreeBounds, array($agentId),true,"fileC");
344 
345  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileA";
346  $this->assertArrayHasKey($key, $result);
347 
348  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileB";
349  $this->assertArrayNotHasKey($key, $result);
350 
351  $key = "project.tar.gz/project.tar/project/folderB/subBfolderB/subBBsubBfolderA/BBBfileC";
352  $this->assertArrayNotHasKey($key, $result);
353 
354  //**************************************************************************
355  // Test with container
356  $result = $licDao->getLicensesPerFileNameForAgentId($itemTreeBounds, array($agentId));
357 
358  $key = "project.tar.gz";
359  $this->assertArrayHasKey($key, $result);
360 
361  $key = "project.tar.gz/project.tar";
362  $this->assertArrayHasKey($key, $result);
363 
364  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
365  }
366 
367  public function testIsNewLicense()
368  {
369  $groupId = 401;
370  $this->setUpLicenseRefTable();
371  $this->testDb->insertData_license_ref();
372  $this->dbManager->queryOnce("CREATE TABLE license_candidate AS SELECT *,$groupId group_fk FROM license_ref LIMIT 1");
373  $licCandi = $this->dbManager->getSingleRow("SELECT * FROM license_candidate",array(),__METHOD__.'.candi');
374  $this->dbManager->queryOnce("DELETE FROM license_ref WHERE rf_pk=$licCandi[rf_pk]");
375  $licRef = $this->dbManager->getSingleRow("SELECT * FROM license_ref LIMIT 1",array(),__METHOD__.'.ref');
376  $licDao = new LicenseDao($this->dbManager);
377  /* test the test but do not count assert */
378  assertThat($this->dbManager->getSingleRow(
379  "SELECT count(*) cnt FROM license_ref WHERE rf_shortname=$1",array($licCandi['rf_shortname']),__METHOD__.'.check'),
380  is(equalTo(array('cnt'=>0))));
381  $this->assertCountBefore++;
382  /* test the DAO */
383  assertThat($licDao->isNewLicense($licRef['rf_shortname'],$groupId), equalTo(FALSE));
384  assertThat($licDao->isNewLicense($licRef['rf_shortname'],0), equalTo(FALSE));
385 
386  assertThat($licDao->isNewLicense($licCandi['rf_shortname'],$groupId), equalTo(FALSE));
387  assertThat($licDao->isNewLicense($licCandi['rf_shortname'],$groupId+1), equalTo(TRUE));
388  assertThat($licDao->isNewLicense($licCandi['rf_shortname'],0), equalTo(TRUE));
389 
390  assertThat($licDao->isNewLicense('(a new shortname)',$groupId), equalTo(TRUE));
391  assertThat($licDao->isNewLicense('(a new shortname)',0), equalTo(TRUE));
392 
393  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
394  }
395 
404  {
405  $ownerGroupId = 401;
406  $otherGroupId = 402;
407  $this->setUpLicenseRefTable();
408  $this->testDb->insertData_license_ref();
409 
410  $this->dbManager->queryOnce("CREATE TABLE license_candidate AS SELECT *,$ownerGroupId group_fk FROM license_ref LIMIT 1");
411  $licCandi = $this->dbManager->getSingleRow("SELECT * FROM license_candidate", array(), __METHOD__.'.candi');
412  $this->dbManager->queryOnce("DELETE FROM license_ref WHERE rf_pk=$licCandi[rf_pk]");
413  $this->dbManager->queryOnce("UPDATE license_candidate SET rf_shortname='CANDIDATE-SCOPE-TEST' WHERE rf_pk=$licCandi[rf_pk]");
414 
415  $licRef = $this->dbManager->getSingleRow("SELECT * FROM license_ref LIMIT 1", array(), __METHOD__.'.ref');
416 
417  $licDao = new LicenseDao($this->dbManager);
418 
419  $ownerLicenses = $licDao->getActiveLicensesForGroup($ownerGroupId);
420  $otherLicenses = $licDao->getActiveLicensesForGroup($otherGroupId);
421 
422  assertThat($ownerLicenses, hasValue('*CANDIDATE-SCOPE-TEST'));
423  assertThat($otherLicenses, not(hasValue('*CANDIDATE-SCOPE-TEST')));
424  assertThat($otherLicenses, not(hasValue('CANDIDATE-SCOPE-TEST')));
425 
426  assertThat($ownerLicenses, hasValue($licRef['rf_shortname']));
427  assertThat($otherLicenses, hasValue($licRef['rf_shortname']));
428 
429  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
430  }
431 
432  public function testGetAgentFileLicenseMatchesWithLicenseMapping()
433  {
434  $this->testDb->createPlainTables(array('uploadtree','license_file','agent','license_map'));
435  $this->setUpLicenseRefTable();
436  $this->testDb->insertData_license_ref();
437 
438  $lic0 = $this->dbManager->getSingleRow("Select * from license_ref limit 1",array(),__METHOD__.'.anyLicense');
439  $licRefId = $lic0['rf_pk'];
440  $licenseFileId= 1;
441  $pfileId= 42;
442  $agentId = 23;
443  $matchPercent = 50;
444  $uploadtreeId= 512;
445  $uploadID =123;
446  $left=2009;
447  $right=2014;
448  $agentName="fake";
449  $agentRev=1;
450  $lic1 = $this->dbManager->getSingleRow("SELECT * FROM license_ref WHERE rf_pk!=$1 LIMIT 1",array($licRefId),__METHOD__.'.anyOtherLicense');
451  $licVarId = $lic1['rf_pk'];
452  $mydate = "'2014-06-04 14:01:30.551093+02'";
453  $this->dbManager->insertTableRow('license_map', array('license_map_pk'=>0,'rf_fk'=>$licVarId,'rf_parent'=>$licRefId,'usage'=>LicenseMap::CONCLUSION));
454  $this->dbManager->queryOnce("INSERT INTO license_file (fl_pk, rf_fk, agent_fk, rf_match_pct, rf_timestamp, pfile_fk)
455  VALUES ($licenseFileId, $licVarId, $agentId, $matchPercent, $mydate, $pfileId)");
456  $this->dbManager->queryOnce("INSERT INTO uploadtree (uploadtree_pk, upload_fk, pfile_fk, lft, rgt)
457  VALUES ($uploadtreeId, $uploadID, $pfileId, $left, $right)");
458  $stmt = __METHOD__.'.insert.agent';
459  $this->dbManager->prepare($stmt,"INSERT INTO agent (agent_pk, agent_name, agent_rev, agent_enabled) VALUES ($1,$2,$3,$4)");
460  $this->dbManager->execute($stmt,array($agentId, $agentName, $agentRev, 'true'));
461 
462  $licDao = new LicenseDao($this->dbManager);
463  $itemTreeBounds = new ItemTreeBounds($uploadtreeId,"uploadtree",$uploadID,$left,$right);
464  $matches = $licDao->getAgentFileLicenseMatches($itemTreeBounds,LicenseMap::CONCLUSION);
465 
466  $licenseRef = new LicenseRef($licRefId, $lic0['rf_shortname'], $lic0['rf_fullname'], $lic0['rf_spdx_id']);
467  $agentRef = new AgentRef($agentId, $agentName, $agentRev);
468  $expected = array( new LicenseMatch($pfileId, $licenseRef, $agentRef, $licenseFileId, $matchPercent) );
469 
470  assertThat($matches, equalTo($expected));
471  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
472  }
473 }
Wrapper class for license map.
Definition: LicenseMap.php:19
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Bounds of an item within an uploadtree table.
Definition: ReuserTypes.hpp:14