FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ClearingDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Author: Andreas Würl, Johannes Najjar
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 
29 use Monolog\Logger;
31 use Mockery as M;
33 
34 class ClearingDaoTest extends \PHPUnit\Framework\TestCase
35 {
37  private $testDb;
39  private $dbManager;
41  private $uploadDao;
43  private $clearingDao;
45  private $now;
47  private $items;
48  private $groupId = 601;
49 
50 
51  protected function setUp()
52  {
53  $this->uploadDao = M::mock(UploadDao::class);
54 
55  $logger = new Logger('default');
56  $logger->pushHandler(new ErrorLogHandler());
57 
58  $this->testDb = new TestPgDb();
59  $this->dbManager = &$this->testDb->getDbManager();
60 
61  $this->clearingDao = new ClearingDao($this->dbManager, $this->uploadDao);
62 
63  $this->testDb->createPlainTables(
64  array(
65  'clearing_decision',
66  'clearing_decision_event',
67  'clearing_decision_type',
68  'clearing_event',
69  'clearing_licenses',
70  'highlight_bulk',
71  'license_ref',
72  'license_ref_bulk',
73  'license_set_bulk',
74  'users',
75  'uploadtree'
76  ));
77 
78  $this->testDb->createInheritedTables();
79 
80  $userArray = array(
81  array('myself', 1),
82  array('in_same_group', 2),
83  array('in_trusted_group', 3));
84  foreach ($userArray as $ur) {
85  $this->dbManager->insertInto('users', 'user_name, root_folder_fk', $ur);
86  }
87 
88  $refArray = array(
89  array(401, 'FOO', 'foo full', 'foo text'),
90  array(402, 'BAR', 'bar full', 'bar text'),
91  array(403, 'BAZ', 'baz full', 'baz text'),
92  array(404, 'QUX', 'qux full', 'qux text')
93  );
94  foreach ($refArray as $params) {
95  $this->dbManager->insertInto('license_ref', 'rf_pk, rf_shortname, rf_fullname, rf_text', $params, $logStmt = 'insert.ref');
96  }
97 
98  $modd = 536888320;
99  $modf = 33188;
100 
101  /* (pfile,item,lft,rgt)
102  upload101: upload101/ ( 0, 299, 1, 4)
103  Afile (201, 301, 1, 2)
104  Bfile (202, 302, 3, 4)
105  upload102: upload102/ ( 0, 300, 1, 8)
106  Afile (201, 303, 1, 2)
107  A-dir/ ( 0, 304, 3, 6)
108  A-dir/Afile (201, 305, 4, 5)
109  Bfile (202, 306, 7, 8)
110  */
111  $this->items = array(
112  299=>array(101, 299, 0, $modd, 1, 4, "upload101"),
113  300=>array(102, 300, 0, $modd, 1, 8, "upload102"),
114  301=>array(101, 301, 201, $modf, 1, 2, "Afile"),
115  302=>array(101, 302, 202, $modf, 3, 4, "Bfile"),
116  303=>array(102, 303, 201, $modf, 1, 2, "Afile"),
117  304=>array(102, 304, 0, $modd, 3, 6, "A-dir"),
118  305=>array(102, 305, 201, $modf, 4, 5, "Afile"),
119  306=>array(102, 306, 202, $modf, 7, 8, "Bfile"),
120  );
121  foreach ($this->items as $ur) {
122  $this->dbManager->insertInto('uploadtree', 'upload_fk,uploadtree_pk,pfile_fk,ufile_mode,lft,rgt,ufile_name', $ur);
123  }
124  $this->now = time();
125 
126  $bulkLicArray = array(
127  array(1, 401, 'TextFOO', false, 101, 299, $this->groupId),
128  array(2, 402, 'TextBAR', false, 101, 299, $this->groupId),
129  array(3, 403, 'TextBAZ', true, 101, 301, $this->groupId),
130  array(4, 403, 'TextBAZ', false, 101, 299, $this->groupId),
131  array(5, 404, 'TextQUX', true, 101, 299, $this->groupId),
132  array(6, 401, 'TexxFOO', true, 101, 302, $this->groupId),
133  array(7, 403, 'TextBAZ', false, 102, 300, $this->groupId),
134  array(8, 403, 'TextBAZ', true, 102, 306, $this->groupId)
135  );
136  foreach ($bulkLicArray as $params) {
137  $paramsRef = array($params[0], $params[2], $params[4], $params[5], $params[6]);
138  $paramsSet = array($params[0], $params[1], $params[3]);
139  $this->dbManager->insertInto('license_ref_bulk', 'lrb_pk, rf_text, upload_fk, uploadtree_fk, group_fk', $paramsRef, 'insert.bulkref');
140  $this->dbManager->insertInto('license_set_bulk', 'lrb_fk, rf_fk, removing', $paramsSet, 'insert.bulkset');
141  }
142 
143  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
144  }
145 
146  private function insertBulkEvents()
147  {
148  $bulkFindingsArray = array(
149  array(1, 5001),
150  array(1, 5001),// a second bulk match in the same file in a different place
151  array(1, 5002),
152  array(1, 5003),
153  array(4, 5004),
154  array(7, 5005)
155  );
156  foreach ($bulkFindingsArray as $params) {
157  $this->dbManager->insertInto('highlight_bulk', 'lrb_fk, clearing_event_fk', $params, $logStmt = 'insert.bulkfinds');
158  }
159 
160  $bulkClearingEvents = array(
161  array(5001, 301),
162  array(5002, 302),
163  array(5003, 303),
164  array(5004, 301),
165  array(5005, 305)
166  );
167  foreach ($bulkClearingEvents as $params) {
168  $this->dbManager->insertInto('clearing_event', 'clearing_event_pk, uploadtree_fk', $params, $logStmt = 'insert.bulkevents');
169  }
170  }
171 
172  private function buildProposals($licProp,$i=0)
173  {
174  foreach ($licProp as $lp) {
175  list($item,$user,$group,$rf,$isRm,$t) = $lp;
176  $this->dbManager->insertInto('clearing_event',
177  'clearing_event_pk, uploadtree_fk, user_fk, group_fk, rf_fk, removed, type_fk, date_added',
178  array($i,$item,$user,$group,$rf,$isRm,1, $this->getMyDate($this->now+$t)));
179  $i++;
180  }
181  }
182 
183  private function buildDecisions($cDec,$j=0)
184  {
185  foreach ($cDec as $cd) {
186  list($item,$user,$group,$type,$t,$scope,$eventIds) = $cd;
187  $this->dbManager->insertInto('clearing_decision',
188  'clearing_decision_pk, uploadtree_fk, pfile_fk, user_fk, group_fk, decision_type, date_added, scope',
189  array($j,$item,$this->items[$item][2],$user,$group,$type, $this->getMyDate($this->now+$t),$scope));
190  foreach ($eventIds as $eId) {
191  $this->dbManager->insertTableRow('clearing_decision_event', array('clearing_decision_fk' => $j, 'clearing_event_fk' => $eId));
192  }
193  $j++;
194  }
195  }
196 
197  function tearDown()
198  {
199  $this->testDb = null;
200  $this->dbManager = null;
201  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
202  }
203 
204  private function getMyDate($ts)
205  {
206  return date('Y-m-d H:i:s T',$ts);
207  }
208 
209  public function testRelevantClearingEvents()
210  {
211  $groupId = 701;
212  $this->buildProposals(array(
213  array(301,1,$groupId,401,false,-99),
214  array(301,2,$groupId,402,true,-98),
215  array(301,2,$groupId,401,true,-97)
216  ),$firstEventId=0);
217  $this->buildDecisions(array(
218  array(301,1,$groupId,DecisionTypes::IDENTIFIED,-90,DecisionScopes::REPO,array($firstEventId,$firstEventId+1,$firstEventId+2))
219  ));
220  $itemTreeBounds = M::mock(ItemTreeBounds::class);
221  $itemTreeBounds->shouldReceive('getItemId')->andReturn(301);
222  $itemTreeBounds->shouldReceive('getUploadTreeTableName')->andReturn('uploadtree');
223  $itemTreeBounds->shouldReceive('containsFiles')->andReturn(false);
224  $itemTreeBounds->shouldReceive('getUploadId')->andReturn($this->items[301][0]);
225  $itemTreeBounds->shouldReceive('getLeft')->andReturn($this->items[301][4]);
226  $itemTreeBounds->shouldReceive('getRight')->andReturn($this->items[301][5]);
227 
228  $events1 = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
229 
230  assertThat($events1, arrayWithSize(2));
231  assertThat($events1, hasKeyInArray(401));
232  assertThat($events1, hasKeyInArray(402));
233  assertThat($events1[401], is(anInstanceOf(ClearingEvent::class)));
234  assertThat($events1[402]->getEventId(), is($firstEventId+1));
235  assertThat($events1[401]->getEventId(), is($firstEventId+2));
236  }
237 
238  function testWip()
239  {
240  $groupId = 701;
241  $this->buildProposals(array(
242  array(301,1,$groupId,401,false,-99),
243  array(301,1,$groupId,402,false,-98),
244  array(301,1,$groupId,401,true,-89),
245  ),$firstEventId=0);
246  $this->buildDecisions(array(
247  array(301,1,$groupId,DecisionTypes::IDENTIFIED,-90,DecisionScopes::REPO,array($firstEventId,$firstEventId+1))
248  ));
249  $watchThis = $this->clearingDao->isDecisionWip(301, $groupId);
250  assertThat($watchThis,is(FALSE));
251  $watchOther = $this->clearingDao->isDecisionWip(303, $groupId);
252  assertThat($watchOther,is(FALSE));
253  $this->buildProposals(array(
254  array(301,1,$groupId,403,false,-89),
255  ),$firstEventId+3);
256  $this->clearingDao->markDecisionAsWip(301, 1, $groupId);
257  $watchThisNow = $this->clearingDao->isDecisionWip(301, $groupId);
258  assertThat($watchThisNow,is(TRUE));
259  $watchOtherNow = $this->clearingDao->isDecisionWip(303, $groupId);
260  assertThat($watchOtherNow,is(FALSE));
261  }
262 
263  private function collectBulkLicenses($bulks)
264  {
265  $bulkLics = array();
266  foreach ($bulks as $bulk) {
267  if (array_key_exists('removedLicenses', $bulk)) {
268  $bulkLics = array_merge($bulkLics, $bulk['removedLicenses']);
269  }
270  if (array_key_exists('addedLicenses', $bulk)) {
271  $bulkLics = array_merge($bulkLics, $bulk['addedLicenses']);
272  }
273  }
274  return $bulkLics;
275  }
276 
277  public function testBulkHistoryWithoutMatches()
278  {
279  $treeBounds = M::mock(ItemTreeBounds::class);
280  $treeBounds->shouldReceive('getItemId')->andReturn(301);
281  $treeBounds->shouldReceive('getLeft')->andReturn(1);
282  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
283  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
284  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
285 
286  $bulkMatched = array_map(function($bulk){
287  return $bulk['matched'];
288  }, $bulks);
289  $bulkText = array_map(function($bulk){
290  return $bulk['text'];
291  }, $bulks);
292 
293  assertThat($bulkMatched, arrayContaining(false, false, false, false, false));
294  assertThat($this->collectBulkLicenses($bulks), arrayContaining('FOO', 'BAR', 'BAZ', 'BAZ', 'QUX'));
295  assertThat($bulkText, arrayContaining('TextFOO', 'TextBAR', 'TextBAZ', 'TextBAZ', 'TextQUX'));
296  }
297 
298  public function testBulkHistoryWithoutMatchesFromDifferentFolder()
299  {
300  $treeBounds = M::mock(ItemTreeBounds::class);
301  $treeBounds->shouldReceive('getItemId')->andReturn(305);
302  $treeBounds->shouldReceive('getLeft')->andReturn(4);
303  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
304  $treeBounds->shouldReceive('getUploadId')->andReturn(102);
305  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
306 
307  $bulkMatched = array_map(function($bulk){
308  return $bulk['matched'];
309  }, $bulks);
310  assertThat($bulkMatched, arrayContaining(false));
311  }
312 
313  public function testBulkHistoryWithAMatch()
314  {
315  $this->insertBulkEvents();
316 
317  $treeBounds = M::mock(ItemTreeBounds::class);
318  $treeBounds->shouldReceive('getItemId')->andReturn(301);
319  $treeBounds->shouldReceive('getLeft')->andReturn(1);
320  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
321  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
322  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId);
323 
324  $clearingEventIds = array_map(function($bulk){
325  return $bulk['id'];
326  }, $bulks);
327  $bulkMatched = array_map(function($bulk){
328  return $bulk['matched'];
329  }, $bulks);
330  $bulkLicDirs = array_map(function($bulk){
331  return count($bulk['removedLicenses'])>0;
332  }, $bulks);
333  $bulkTried = array_map(function($bulk){
334  return $bulk['tried'];
335  }, $bulks);
336 
337  assertThat($clearingEventIds, arrayContaining(5001, null, null, 5004, null));
338  assertThat($bulkMatched, arrayContaining(true, false, false, true, false));
339  assertThat($this->collectBulkLicenses($bulks), arrayContaining('FOO', 'BAR', 'BAZ', 'BAZ', 'QUX'));
340  assertThat($bulkLicDirs, arrayContaining(false, false, true, false, true));
341  assertThat($bulkTried, arrayContaining(true, true, true, true, true));
342  }
343 
344  public function testBulkHistoryWithAMatchReturningAlsoNotTried()
345  {
346  $this->insertBulkEvents();
347 
348  $treeBounds = M::mock(ItemTreeBounds::class);
349  $treeBounds->shouldReceive('getItemId')->andReturn(301);
350  $treeBounds->shouldReceive('getLeft')->andReturn(1);
351  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
352  $treeBounds->shouldReceive('getUploadId')->andReturn(101);
353  $bulks = $this->clearingDao->getBulkHistory($treeBounds, $this->groupId, false);
354 
355  $clearingEventIds = array_map(function($bulk){
356  return $bulk['id'];
357  }, $bulks);
358  $bulkMatched = array_map(function($bulk){
359  return $bulk['matched'];
360  }, $bulks);
361  $bulkLicDirs = array_map(function($bulk){
362  return count($bulk['removedLicenses'])>0;
363  }, $bulks);
364  $bulkTried = array_map(function($bulk){
365  return $bulk['tried'];
366  }, $bulks);
367 
368  assertThat($clearingEventIds, arrayContaining(5001, null, null, 5004, null, null));
369  assertThat($bulkMatched, arrayContaining(true, false, false, true, false, false));
370  assertThat($this->collectBulkLicenses($bulks), arrayContaining('FOO', 'BAR', 'BAZ', 'BAZ', 'QUX', 'FOO'));
371  assertThat($bulkLicDirs, arrayContaining(false, false, true, false, true, true));
372  assertThat($bulkTried, arrayContaining(true, true, true, true, true, false));
373  }
374 
375  public function testGetClearedLicenseMultiplicities()
376  {
377  $user = 1;
378  $groupId = 601;
379  $rf = 401;
380  $isRm = false;
381  $t = -10815;
382  $this->buildProposals(array(array(303,$user,$groupId,$rf,$isRm,$t),
383  array(305,$user,$groupId,$rf,$isRm,$t+1)),$eventId=0);
384  $type = DecisionTypes::IDENTIFIED;
385  $scope = DecisionScopes::ITEM;
386  $this->buildDecisions(array(array(303,$user,$groupId,$type,$t,$scope,array($eventId)),
387  array(305,$user,$groupId,$type,$t,$scope,array($eventId+1))));
388  $treeBounds = M::mock(ItemTreeBounds::class);
389 
390  $treeBounds->shouldReceive('getLeft')->andReturn(1);
391  $treeBounds->shouldReceive('getRight')->andReturn(8);
392  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
393  $treeBounds->shouldReceive('getUploadId')->andReturn(102);
394 
395  $map = $this->clearingDao->getClearedLicenseIdAndMultiplicities($treeBounds, $groupId);
396  assertThat($map, is(array('FOO'=>array('count'=>2,'shortname'=>'FOO','rf_pk'=>401))));
397  }
398 
399  public function testGetClearedLicenses()
400  {
401  $user = 1;
402  $groupId = 601;
403  $rf = 401;
404  $isRm = false;
405  $t = -10815;
406  $item = 303;
407  $this->buildProposals(array(array($item,$user,$groupId,$rf,$isRm,$t),
408  array($item,$user,$groupId,$rf+1,!$isRm,$t+1)),$eventId=0);
409  $type = DecisionTypes::IDENTIFIED;
410  $scope = DecisionScopes::ITEM;
411  $this->buildDecisions(array( array( $item,$user,$groupId,$type,$t,$scope,array($eventId,$eventId+1) ) ));
412  $treeBounds = M::mock(ItemTreeBounds::class);
413 
414  $treeBounds->shouldReceive('getLeft')->andReturn(1);
415  $treeBounds->shouldReceive('getRight')->andReturn(8);
416  $treeBounds->shouldReceive('getUploadTreeTableName')->andReturn("uploadtree");
417  $treeBounds->shouldReceive('getUploadId')->andReturn(102);
418 
419  $map = $this->clearingDao->getClearedLicenses($treeBounds, $groupId);
420  assertThat($map, equalTo(array(new LicenseRef($rf,'FOO','foo full'))));
421  }
422 
423 
424  public function testMainLicenseIds()
425  {
426  $this->testDb->createPlainTables(array('upload_clearing_license'));
427  $uploadId = 101;
428  $mainLicIdsInitially = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
429  assertThat($mainLicIdsInitially, is(emptyArray()));
430 
431  $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId=402);
432  $mainLicIdsAfterAddingOne = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
433  assertThat($mainLicIdsAfterAddingOne, arrayContaining(array($licenseId)));
434 
435  $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId);
436  $mainLicIdsAfterAddingOneTwice = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
437  assertThat($mainLicIdsAfterAddingOneTwice, is(arrayWithSize(1)));
438 
439  $this->clearingDao->makeMainLicense($uploadId,$this->groupId,$licenseId2=403);
440  $mainLicIdsAfterAddingOther = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
441  assertThat($mainLicIdsAfterAddingOther, arrayContainingInAnyOrder(array($licenseId,$licenseId2)));
442 
443  $this->clearingDao->removeMainLicense($uploadId,$this->groupId,$licenseId2);
444  $mainLicIdsAfterRemovingOne = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
445  assertThat($mainLicIdsAfterRemovingOne, is(arrayWithSize(1)));
446 
447  $this->clearingDao->removeMainLicense($uploadId,$this->groupId,$licenseId2);
448  $mainLicIdAfterRemovingSomethingNotInSet = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
449  assertThat($mainLicIdAfterRemovingSomethingNotInSet, is(arrayWithSize(1)));
450 
451  $this->clearingDao->removeMainLicense($uploadId,$this->groupId+1,$licenseId);
452  $mainLicIdAfterInsertToOtherGroup = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
453  assertThat($mainLicIdAfterInsertToOtherGroup, is(arrayWithSize(1)));
454 
455  $this->clearingDao->removeMainLicense($uploadId+1,$this->groupId,$licenseId);
456  $mainLicIdAfterInsertToOtherUpload = $this->clearingDao->getMainLicenseIds($uploadId, $this->groupId);
457  assertThat($mainLicIdAfterInsertToOtherUpload, is(arrayWithSize(1)));
458  }
459 
460  public function testupdateClearingEvent()
461  {
462  $this->testDb->createSequences(array('clearing_event_clearing_event_pk_seq'));
463  $this->testDb->createConstraints(array('clearing_event_pkey'));
464  $this->dbManager->queryOnce("ALTER TABLE clearing_event ALTER COLUMN clearing_event_pk SET DEFAULT nextval('clearing_event_clearing_event_pk_seq'::regclass)");
465 
466  $this->clearingDao->updateClearingEvent($uploadTreeId=301, $userId=1, $groupId=1, $licenseId=402, $what='comment', $changeCom='abc123');
467  $rowPast = $this->dbManager->getSingleRow('SELECT * FROM clearing_event WHERE uploadtree_fk=$1 AND rf_fk=$2 ORDER BY clearing_event_pk DESC LIMIT 1',array($uploadTreeId,$licenseId),__METHOD__.'beforeReportinfo');
468  assertThat($rowPast['comment'],equalTo($changeCom));
469 
470  $this->clearingDao->updateClearingEvent($uploadTreeId, $userId, $groupId, $licenseId, $what='reportinfo', $changeRep='def456');
471  $rowFuture = $this->dbManager->getSingleRow('SELECT * FROM clearing_event WHERE uploadtree_fk=$1 AND rf_fk=$2 ORDER BY clearing_event_pk DESC LIMIT 1',array($uploadTreeId,$licenseId),__METHOD__.'afterReportinfo');
472  assertThat($rowFuture['comment'],equalTo($changeCom));
473  assertThat($rowFuture['reportinfo'],equalTo($changeRep));
474  }
475 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28