FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ClearingDecisionProcessorTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
23 use Mockery as M;
24 
25 function ReportCachePurgeAll()
26 {
27 
28 }
29 
30 class ClearingDecisionProcessorTest extends \PHPUnit\Framework\TestCase
31 {
32  const MATCH_ID = 231;
33  const PERCENTAGE = 315;
35  private $uploadTreeId;
37  private $userId;
39  private $groupId;
41  private $agentLicenseEventProcessor;
43  private $dbManager;
45  private $clearingDao;
47  private $itemTreeBounds;
49  private $clearingDecisionProcessor;
51  private $clearingEventProcessor;
53  private $timestamp;
55  private $includeSubFolders;
56 
57  protected function setUp() : void
58  {
59  $this->uploadTreeId = 432;
60  $this->pfileId = 32;
61  $this->userId = 12;
62  $this->groupId = 5;
63  $this->timestamp = time();
64  $this->includeSubFolders = false;
65 
66  $this->clearingDao = M::mock(ClearingDao::class);
67  $this->agentLicenseEventProcessor = M::mock(AgentLicenseEventProcessor::class);
68  $this->clearingEventProcessor = new ClearingEventProcessor();
69 
70  $this->itemTreeBounds = M::mock(ItemTreeBounds::class);
71  $this->itemTreeBounds->shouldReceive("getItemId")->withNoArgs()->andReturn($this->uploadTreeId);
72  $this->itemTreeBounds->shouldReceive("getPfileId")->withNoArgs()->andReturn($this->pfileId);
73 
74  $this->dbManager = M::mock(DbManager::class);
75  $this->dbManager->shouldReceive('begin')->withNoArgs();
76  $this->dbManager->shouldReceive('commit')->withNoArgs();
77 
78  $this->clearingDecisionProcessor = new ClearingDecisionProcessor(
79  $this->clearingDao, $this->agentLicenseEventProcessor, $this->clearingEventProcessor,
80  $this->dbManager);
81  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
82  }
83 
84  protected function tearDown() : void
85  {
86  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
87  M::close();
88  }
89 
90  public function testMakeDecisionFromLastEvents()
91  {
92  $isGlobal = DecisionScopes::ITEM;
93  $addedEvent = $this->createClearingEvent(123, $this->timestamp, 13, "licA", "License A");
94 
95  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
96  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
97  ->andReturn(array($addedEvent));
98  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
99  ->with($this->itemTreeBounds)
100  ->andReturn(array());
101 
102  $clearingDecision = M::mock(ClearingDecision::class);
103  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
104  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
105  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
106 
107  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
108  ->with($this->itemTreeBounds, $this->groupId)
109  ->andReturn($clearingDecision);
110 
111  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()
112  ->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal, is(arrayContainingInAnyOrder(123)));
113 
114  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal);
115  }
116 
117  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeShouldNotCreateANewDecisionWhenNoLicensesShouldBeRemoved()
118  {
119  $isGlobal = DecisionScopes::REPO;
120  $addedEvent = $this->createClearingEvent(123, $this->timestamp, 13, "licA", "License A");
121 
122  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
123  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
124  ->andReturn(array($addedEvent));
125  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
126  ->with($this->itemTreeBounds)->andReturn(array());
127 
128  $clearingDecision = M::mock(ClearingDecision::class);
129  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
130  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
131  $clearingDecision->shouldReceive("getScope")->withNoArgs()->andReturn(DecisionScopes::ITEM);
132  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
133 
134  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
135  ->with($this->itemTreeBounds, $this->groupId)
136  ->andReturn($clearingDecision);
137  $eventId = 65;
138  $this->clearingDao->shouldReceive("insertClearingEvent")->with($this->itemTreeBounds->getItemId(), $this->userId, $this->groupId, $addedEvent->getLicenseId(), true)->andReturn($eventId);
139 
140  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal, array($eventId));
141  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
142 
143  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
144  }
145 
146  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeShouldNotCreateANewDecisionWhenNoLicensesShouldBeRemovedAndTheScopeDoesNotChange()
147  {
148  $isGlobal = DecisionScopes::REPO;
149  $addedEvent = $this->createClearingEvent(123, $this->timestamp, 13, "licA", "License A");
150 
151  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
152  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
153  ->andReturn(array($addedEvent));
154  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
155  ->with($this->itemTreeBounds)->andReturn(array());
156 
157  $clearingDecision = M::mock(ClearingDecision::class);
158  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
159  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
160  $clearingDecision->shouldReceive("getScope")->withNoArgs()->andReturn($isGlobal);
161  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
162 
163  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
164  ->with($this->itemTreeBounds, $this->groupId)
165  ->andReturn($clearingDecision);
166 
167  $eventId = 65;
168  $this->clearingDao->shouldReceive("insertClearingEvent")->with($this->itemTreeBounds->getItemId(), $this->userId, $this->groupId, $addedEvent->getLicenseId(), true)->andReturn($eventId);
169  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal, array($eventId));
170  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
171 
172  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
173  }
174 
175  public function testMakeDecisionFromLastEventsWithNoLicenseKnownType()
176  {
177  $isGlobal = true;
178 
179  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
180  ->with($this->itemTreeBounds)->andReturn(array());
181 
182  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
183  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
184  ->andReturn(array());
185 
186  $clearingDecision = M::mock(ClearingDecision::class);
187  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
188  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IRRELEVANT);
189  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
190 
191  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
192  ->with($this->itemTreeBounds, $this->groupId)
193  ->andReturn($clearingDecision);
194 
195  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with( $this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::REPO, array());
196  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
197 
198  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
199  }
200 
201  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeAndAnExistingAddingUserEvent()
202  {
204  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
205  $addedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, $isRemoved = false);
206 
207  $isGlobal = true;
208 
209  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
210  ->with($this->itemTreeBounds)->andReturn(array());
211 
212  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
213  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
214  ->andReturn(array($licenseRef->getId() => $addedEvent));
215 
216  $clearingDecision = M::mock(ClearingDecision::class);
217  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
218  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IRRELEVANT);
219  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
220 
221  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
222  ->with($this->itemTreeBounds, $this->groupId)
223  ->andReturn($clearingDecision);
224 
225  $eventId = 65;
226  $this->clearingDao->shouldReceive("insertClearingEvent")->with($this->itemTreeBounds->getItemId(), $this->userId, $this->groupId, $addedEvent->getLicenseId(), true)->andReturn($eventId);
227  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with( $this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::REPO, array($eventId));
228  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
229 
230  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
231  }
232 
233  public function testMakeDecisionFromLastEventsWithNoLicenseKnownTypeAndAnExistingRemovingUserEvent()
234  {
236  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
237  $removedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, $isRemoved = true);
238 
239  $isGlobal = true;
240 
241  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
242  ->with($this->itemTreeBounds)->andReturn(array());
243 
244  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
245  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
246  ->andReturn(array($licenseRef->getId() => $removedEvent));
247 
248  $clearingDecision = M::mock(ClearingDecision::class);
249  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
250  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IRRELEVANT);
251  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
252 
253  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
254  ->with($this->itemTreeBounds, $this->groupId)
255  ->andReturn($clearingDecision);
256 
257  $this->clearingDao->shouldReceive("createDecisionFromEvents")->once()->with( $this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::REPO, array());
258  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
259 
260  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE, $isGlobal);
261  }
265  public function testMakeDecisionFromLastEventsWithDelayedScanner()
266  {
268  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
269 
270  $isGlobal = false;
271  $removedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, $isRemoved = true);
272 
273  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
274  ->with($this->itemTreeBounds, $this->groupId, $this->includeSubFolders)
275  ->andReturn(array($licenseRef->getId() => $removedEvent));
276 
277  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
278  ->with($this->itemTreeBounds)->andReturn($scannerResults);
279 
280  $clearingDecision = M::mock(ClearingDecision::class);
281  $clearingDecision->shouldReceive("getTimeStamp")->withNoArgs()->andReturn($this->timestamp-3600);
282  $clearingDecision->shouldReceive("getType")->withNoArgs()->andReturn(DecisionTypes::IDENTIFIED);
283  $clearingDecision->shouldReceive("getClearingEvents")->withNoArgs()->andReturn(array());
284 
285  $this->clearingDao->shouldReceive("getRelevantClearingDecision")
286  ->with($this->itemTreeBounds,$this->groupId)
287  ->andReturn($clearingDecision);
288 
289  $this->clearingDao->shouldReceive("insertClearingEvent")
290  ->never();
291 
292  $this->clearingDao->shouldReceive("createDecisionFromEvents")
293  ->once()
294  ->with($this->uploadTreeId, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, DecisionScopes::ITEM,
295  is(arrayContainingInAnyOrder($removedEvent->getEventId())));
296  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
297 
298  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, DecisionTypes::IDENTIFIED, $isGlobal);
299  }
300 
301  public function testMakeDecisionFromLastEventsWithInvalidType()
302  {
303  $this->clearingDao->shouldReceive("getRelevantClearingEvents")->never();
304  $this->agentLicenseEventProcessor->shouldReceive("getScannerDetectedLicenses")->never();
305  $this->clearingDao->shouldReceive("getRelevantClearingDecision")->never();
306 
307  $this->clearingDao->shouldReceive("createDecisionFromEvents")->never();
308  $this->clearingDao->shouldReceive("removeWipClearingDecision")->never();
309 
310  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($this->itemTreeBounds, $this->userId, $this->groupId, ClearingDecisionProcessor::NO_LICENSE_KNOWN_DECISION_TYPE - 1, false);
311  }
312 
313  public function testGetCurrentClearingsWithoutDecisions()
314  {
315  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
316  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
317  ->andReturn(array());
318  $this->clearingDao->shouldReceive("getRelevantClearingEvents")->with($this->itemTreeBounds, $this->groupId)->andReturn(array());
319 
320  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
321 
322  assertThat($licenseDecisions, is(emptyArray()));
323  assertThat($removedClearings, is(emptyArray()));
324  }
325 
326  public function testGetCurrentClearingsWithUserDecisionsOnly()
327  {
328  $addedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseId=13, "licA", "License A");
329 
330  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
331  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
332  ->andReturn(array());
333  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
334  ->with($this->itemTreeBounds, $this->groupId)
335  ->andReturn(array($licenseId => $addedEvent));
336 
337  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
338 
339  assertThat($licenseDecisions, is(arrayWithSize(1)));
340 
342  $result = $licenseDecisions[$addedEvent->getLicenseId()];
343  assertThat($result->getLicenseRef(), is($addedEvent->getLicenseRef()));
344  assertThat($result->getClearingEvent(), is($addedEvent));
345  assertThat($result->getAgentDecisionEvents(), is(emptyArray()));
346  assertThat($removedClearings, is(emptyArray()));
347  }
348 
349  public function testGetCurrentClearingsWithAgentDecisionsOnly()
350  {
351  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
352 
353  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
354  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
355  ->andReturn($scannerResults);
356  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
357  ->with($this->itemTreeBounds, $this->groupId)
358  ->andReturn(array());
359 
360  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
361 
362  assertThat($licenseDecisions, is(arrayWithSize(1)));
363 
365  $result = $licenseDecisions[$licenseRef->getId()];
366  assertThat($result->getLicenseRef(), is($licenseRef));
367  assertThat($result->getClearingEvent(), is(nullValue()));
368  assertThat($result->getAgentDecisionEvents(), is(arrayWithSize(1)));
369  assertThat($removedClearings, is(emptyArray()));
370  }
371 
372  public function testGetCurrentClearingsWithUserAndAgentDecision()
373  {
375  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
376 
377  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
378  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)
379  ->andReturn($scannerResults);
380 
381  $licenseId = $licenseRef->getId();
382 
383  $addedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseId, $licenseRef->getShortName(), $licenseRef->getFullName());
384  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
385  ->with($this->itemTreeBounds, $this->groupId)
386  ->andReturn(array($licenseId => $addedEvent));
387 
388  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
389 
390  assertThat($licenseDecisions, is(arrayWithSize(1)));
391 
393  $result = $licenseDecisions[$licenseRef->getId()];
394  assertThat($result->getLicenseRef(), is($licenseRef));
395  assertThat($result->getClearingEvent(), is($addedEvent));
396  assertThat($result->getAgentDecisionEvents(), is(arrayWithSize(1)));
397  assertThat($removedClearings, is(emptyArray()));
398  }
399 
400  public function testGetCurrentClearingsWithUserRemovedDecisionsOnly()
401  {
403  list($scannerResults, $licenseRef, $agentRef) = $this->createScannerDetectedLicenses();
404  $removedEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName(), ClearingEventTypes::USER, true);
405 
406  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
407  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
408 
409  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
410  ->with($this->itemTreeBounds, $this->groupId)
411  ->andReturn(array($licenseRef->getId() => $removedEvent));
412 
413  list($licenseDecisions, $removedClearings) = $this->clearingDecisionProcessor->getCurrentClearings($this->itemTreeBounds, $this->groupId);
414 
415  assertThat($licenseDecisions, is(emptyArray()));
416  assertThat($removedClearings, is(arrayWithSize(1)));
417 
419  $result = $removedClearings[$removedEvent->getLicenseId()];
420  assertThat($result->getLicenseRef(), is($removedEvent->getLicenseRef()));
421  assertThat($result->getClearingEvent(), is($removedEvent));
422  $agentClearingEvents = $result->getAgentDecisionEvents();
423  assertThat($agentClearingEvents, is(arrayWithSize(1)));
424 
425  $agentEvent = $agentClearingEvents[0];
426 
427  assertThat($agentEvent->getAgentRef(), is($agentRef));
428  assertThat($agentEvent->getLicenseRef(), is($licenseRef));
429  assertThat($agentEvent->getMatchId(), is(self::MATCH_ID));
430  assertThat($agentEvent->getPercentage(), is(self::PERCENTAGE));
431  }
432 
433  public function testGetUnhandledScannerDetectedLicenses()
434  {
436  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
437 
438  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
439  ->with($this->itemTreeBounds, $this->groupId)
440  ->andReturn(array());
441  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
442  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
443 
444  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId);
445 
446  assertThat($hasUnhandledScannerDetectedLicenses);
447  }
448 
449  public function testGetUnhandledScannerDetectedLicensesWithMatch()
450  {
452  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
453  $clearingEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId(), $licenseRef->getShortName(), $licenseRef->getFullName());
454 
455  $this->clearingDao->shouldReceive("getRelevantClearingEvents")->once()
456  ->with($this->itemTreeBounds, $this->groupId)
457  ->andReturn(array($clearingEvent->getLicenseId()=>$clearingEvent));
458  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")->once()
459  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
460 
461  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId);
462 
463  assertThat( $hasUnhandledScannerDetectedLicenses, is(False));
464  }
465 
466  public function testGetUnhandledScannerDetectedLicensesWithoutMatch()
467  {
469  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
470  $offset = 23;
471  $clearingEvent = $this->createClearingEvent(123, $this->timestamp, $licenseRef->getId()+$offset, $licenseRef->getShortName(), $licenseRef->getFullName());
472 
473  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
474  ->with($this->itemTreeBounds, $this->groupId)
475  ->andReturn(array($clearingEvent->getLicenseId()=>$clearingEvent));
476  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
477  ->with($this->itemTreeBounds,LicenseMap::TRIVIAL)->andReturn($scannerResults);
478 
479  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId);
480 
481  assertThat($hasUnhandledScannerDetectedLicenses, is(True));
482  }
483 
484  public function testGetUnhandledScannerDetectedLicensesWithMappedMatch()
485  {
487  list($scannerResults, $licenseRef) = $this->createScannerDetectedLicenses();
488  $offset = 0;
489  $clearingEvent = $this->createClearingEvent($eventId=123, $this->timestamp, $licenseRef->getId()+$offset, $licenseRef->getShortName(), $licenseRef->getFullName());
490 
491  $this->clearingDao->shouldReceive("getRelevantClearingEvents")
492  ->with($this->itemTreeBounds, $this->groupId)
493  ->andReturn(array($clearingEvent->getLicenseId()=>$clearingEvent));
494  $this->agentLicenseEventProcessor->shouldReceive("getScannerEvents")
495  ->with($this->itemTreeBounds,LicenseMap::CONCLUSION)->andReturn($scannerResults);
496 
497  $licenseMap = M::mock(LicenseMap::class);
498  $licenseMap->shouldReceive('getProjectedId')->andReturnUsing(function($id) {
499  return $id;
500  });
501  $licenseMap->shouldReceive('getUsage')->andReturn(LicenseMap::CONCLUSION);
502 
503  $hasUnhandledScannerDetectedLicenses = $this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($this->itemTreeBounds, $this->groupId, array(), $licenseMap);
504 
505  assertThat( $hasUnhandledScannerDetectedLicenses, is(False) );
506  }
507 
520  private function createClearingEvent($eventId, $timestamp, $licenseId, $licenseShortName, $licenseFullName, $eventType = ClearingEventTypes::USER, $isRemoved = false, $reportInfo = "<reportInfo>", $comment = "<comment>")
521  {
522  $licenseRef = new LicenseRef($licenseId, $licenseShortName, $licenseFullName, $licenseShortName);
523  $clearingLicense = new ClearingLicense($licenseRef, $isRemoved, $reportInfo, $comment);
524  return new ClearingEvent($eventId, $this->uploadTreeId, $timestamp, $this->userId, $this->groupId, $eventType, $clearingLicense);
525  }
526 
530  protected function createScannerDetectedLicenses($licenseId = 13, $licenseShortname = "licA", $licenseFullName = "License-A")
531  {
532  $licenseRef = new LicenseRef($licenseId, $licenseShortname, $licenseFullName, $licenseShortname);
533 
534  $agentRef = M::mock(AgentRef::class);
535 
536  $scannerEvents = array(
537  $licenseId => array(new AgentClearingEvent($licenseRef, $agentRef, self::MATCH_ID, self::PERCENTAGE))
538  );
539 
540  return array($scannerEvents, $licenseRef, $agentRef);
541  }
542 }
createClearingEvent($eventId, $timestamp, $licenseId, $licenseShortName, $licenseFullName, $eventType=ClearingEventTypes::USER, $isRemoved=false, $reportInfo="<reportInfo>", $comment="<comment>")
createScannerDetectedLicenses($licenseId=13, $licenseShortname="licA", $licenseFullName="License-A")
Utility functions to process ClearingDecision.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Contains business rules for FOSSology.