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