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