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