20 class bulkTest extends \PHPUnit\Framework\TestCase
33 private $uploadPermDao;
35 private $highlightDao;
37 protected function setUp() :
void
39 $this->testDb =
new TestPgDb(
"monkBulk");
40 $this->
dbManager = $this->testDb->getDbManager();
43 $logger =
new Logger(
"MonkBulkTest");
44 $this->uploadPermDao = \Mockery::mock(UploadPermissionDao::class);
50 protected function tearDown() :
void
52 $this->testDb->fullDestruct();
55 $this->licenseDao =
null;
56 $this->highlightDao =
null;
57 $this->clearingDao =
null;
60 private function runBulkMonk($userId = 2, $groupId = 2, $jobId = 1, $bulkId = 3)
62 $sysConf = $this->testDb->getFossSysConf();
64 $agentName =
"monkbulk";
66 $agentDir = dirname(__DIR__,4).
'/build/src/monk';
67 $execDir = $agentDir.
'/agent';
68 system(
"install -D $agentDir/VERSION-monkbulk $sysConf/mods-enabled/$agentName/VERSION");
70 $pipeFd = popen(
"echo '0\n$bulkId\n0' | $execDir/$agentName -c $sysConf --userID=$userId --groupID=$groupId --jobId=$jobId --scheduler_start",
"r");
71 $this->assertTrue($pipeFd !==
false,
'running monk bulk failed');
74 while (($buffer = fgets($pipeFd, 4096)) !==
false) {
77 $retCode = pclose($pipeFd);
79 unlink(
"$sysConf/mods-enabled/$agentName/VERSION");
80 rmdir(
"$sysConf/mods-enabled/$agentName");
81 rmdir(
"$sysConf/mods-enabled");
83 return array($output,$retCode);
86 private function setUpRepo()
88 $sysConf = $this->testDb->getFossSysConf();
90 $confFile = $sysConf.
"/fossology.conf";
91 system(
"touch ".$confFile);
92 $config =
"[FOSSOLOGY]\ndepth = 0\npath = $sysConf/repo\n";
93 file_put_contents($confFile, $config);
95 $testRepoDir = dirname(dirname(dirname(__DIR__))).
"/lib/php/Test/";
96 system(
"cp -a $testRepoDir/repo $sysConf/");
99 private function rmRepo()
101 $sysConf = $this->testDb->getFossSysConf();
102 system(
"rm $sysConf/repo -rf");
103 unlink($sysConf.
"/fossology.conf");
106 private function setUpTables()
108 $this->testDb->createPlainTables(array(
'upload',
'uploadtree',
'license_ref',
'license_ref_bulk',
'license_set_bulk',
109 'clearing_event',
'clearing_decision',
'report_info',
'clearing_decision_event',
'license_file',
'highlight',
'highlight_bulk',
'agent',
'pfile',
'ars_master',
'users'),
false);
110 $this->testDb->createSequences(array(
'agent_agent_pk_seq',
'pfile_pfile_pk_seq',
'upload_upload_pk_seq',
'nomos_ars_ars_pk_seq',
'license_file_fl_pk_seq',
'license_ref_rf_pk_seq',
'license_ref_bulk_lrb_pk_seq',
'clearing_event_clearing_event_pk_seq'),
false);
111 $this->testDb->createViews(array(
'license_file_ref'),
false);
112 $this->testDb->createConstraints(array(
'agent_pkey',
'pfile_pkey',
'upload_pkey_idx',
'FileLicense_pkey',
'clearing_event_pkey',
'license_ref_bulk_pkey',
'license_set_bulk_fkey'),
false);
113 $this->testDb->alterTables(array(
'agent',
'pfile',
'upload',
'ars_master',
'license_ref_bulk',
'license_ref',
'license_set_bulk',
'clearing_event',
'license_file',
'highlight'),
false);
114 $this->testDb->createInheritedTables();
115 $this->testDb->insertData(array(
'pfile',
'upload',
'uploadtree_a',
'users'),
false);
116 $this->testDb->insertData_license_ref();
119 private function getHeartCount($output)
122 if (preg_match(
"/.*HEART: ([0-9]*).*/", $output, $matches)) {
123 return intval($matches[1]);
133 $this->setUpTables();
142 $refText =
"The GNU General Public License is a free, copyleft license for software and other kinds of works.";
144 $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
145 array($licenseId => array($removing,
"",
"",
"")), $refText);
147 $this->assertGreaterThan($expected=0, $bulkId);
150 list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
152 $this->assertEquals($retCode, 0,
'monk bulk failed: '.$output);
155 $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
156 $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
158 assertThat(count($relevantDecisionsItem6),is(equalTo(1)));
159 assertThat(count($relevantDecisionsItem7),is(equalTo(1)));
160 assertThat($relevantDecisionsItem6,hasKeyInArray($licenseId));
162 $refSecondText =
"Our General Public Licenses are designed to make sure that you " .
163 "have the freedom to distribute copies of free software";
164 $licenseSecondId = 215;
165 $bulkSecondId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
166 array($licenseSecondId => array($removing,
"",
"",
"")), $refSecondText);
169 list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkSecondId);
171 $this->assertEquals($retCode, 0,
'monk bulk failed: '.$output);
172 $relevantDecisionsItemPfile3 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
173 $relevantDecisionsItemPfile4 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
174 assertThat(count($relevantDecisionsItemPfile3), is(equalTo(1)));
176 assertThat(count($relevantDecisionsItemPfile4), is(equalTo(2)));
177 assertThat($relevantDecisionsItemPfile4, hasKeyInArray($licenseSecondId));
183 public function testRunMonkBulkScan()
185 $this->setUpTables();
196 $refText =
"The GNU General Public License is a free, copyleft license for software and other kinds of works.";
198 $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
199 array($licenseId1 => array($removing1,
"",
"",
""),
200 $licenseId2 => array($removing2,
"",
"",
"")), $refText);
202 $this->assertGreaterThan($expected=0, $bulkId);
205 list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
206 $this->assertEquals(6, $this->getHeartCount($output));
209 $this->assertEquals($retCode, 0,
'monk bulk failed: '.$output);
212 $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
213 $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
215 assertThat(count($relevantDecisionsItem6),is(equalTo(2)));
216 assertThat(count($relevantDecisionsItem7),is(equalTo(2)));
218 assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
220 $clearingEvent = $relevantDecisionsItem6[$rfForACE];
221 $eventId = $clearingEvent->getEventId();
222 $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
224 assertThat(count($bulkHighlights), is(1));
227 $bulkHighlight1 = $bulkHighlights[0];
228 assertThat($bulkHighlight1->getLicenseId(), is(equalTo($licenseId1)));
229 assertThat($bulkHighlight1->getType(), is(equalTo(Highlight::BULK)));
230 assertThat($bulkHighlight1->getStart(), is(3));
231 assertThat($bulkHighlight1->getEnd(), is(103));
234 assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
236 $clearingEvent = $relevantDecisionsItem6[$rfForACE];
237 $eventId = $clearingEvent->getEventId();
238 $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
240 assertThat(count($bulkHighlights), is(1));
243 $bulkHighlight2 = $bulkHighlights[0];
244 assertThat($bulkHighlight2->getLicenseId(), is(equalTo($licenseId2)));
245 assertThat($bulkHighlight2->getType(), is(equalTo(Highlight::BULK)));
246 assertThat($bulkHighlight2->getStart(), is(3));
247 assertThat($bulkHighlight2->getEnd(), is(103));
249 $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
251 assertThat(count($bulkHighlights), is(equalTo(2)));
252 assertThat($bulkHighlights, containsInAnyOrder($bulkHighlight1, $bulkHighlight2));
256 public function testRunMonkBulkScanWithMultipleLicenses()
258 $this->setUpTables();
267 $refText =
"The GNU General Public License is a free, copyleft license for software and other kinds of works.";
269 $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
270 array($licenseId => array($removing,
"",
"",
"")), $refText);
272 $this->assertGreaterThan($expected = 0, $bulkId);
275 list($output, $retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
276 $this->assertEquals(6, $this->getHeartCount($output));
279 $this->assertEquals($retCode, 0,
'monk bulk failed: ' . $output);
282 $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
283 $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
285 assertThat(count($relevantDecisionsItem6), is(equalTo(1)));
286 assertThat(count($relevantDecisionsItem7), is(equalTo(1)));
288 assertThat($relevantDecisionsItem6, hasKeyInArray($rfForACE));
290 $clearingEvent = $relevantDecisionsItem6[$rfForACE];
291 $eventId = $clearingEvent->getEventId();
292 $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
294 $this->assertEquals(1, count($bulkHighlights));
297 $bulkHighlight = $bulkHighlights[0];
298 $this->assertEquals($licenseId, $bulkHighlight->getLicenseId());
299 $this->assertEquals(Highlight::BULK, $bulkHighlight->getType());
300 $this->assertEquals(3, $bulkHighlight->getStart());
301 $this->assertEquals(103, $bulkHighlight->getEnd());
303 $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
305 $this->assertEquals(1, count($bulkHighlights));
306 $this->assertEquals($bulkHighlight, $bulkHighlights[0]);
312 $this->setUpTables();
321 $refText =
"The GNU General Public License is copyleft license for software and other kinds of works.";
325 $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
326 array($licenseId => array($removing,
"",
"",
"")), $refText);
328 $this->assertGreaterThan($expected=0, $bulkId);
330 list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
334 $this->assertEquals($retCode, 0,
"monk bulk failed: $output");
337 $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
338 $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
340 $this->assertEquals($expected=0, count($relevantDecisionsItem6));
341 $this->assertEquals($expected=0, count($relevantDecisionsItem7));
345 public function testRunMonkBulkScanWithAShortSearch()
347 $this->setUpTables();
356 $refText =
"The GNU";
358 $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
359 array($licenseId => array($removing,
"",
"",
"")), $refText);
361 $this->assertGreaterThan($expected=0, $bulkId);
364 list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
368 $this->assertEquals($retCode, 0,
'monk bulk failed: '.$output);
371 $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
372 $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
374 assertThat(count($relevantDecisionsItem6),is(equalTo(1)));
375 assertThat(count($relevantDecisionsItem7),is(equalTo(1)));
377 assertThat($relevantDecisionsItem6,hasKeyInArray($rfForACE));
379 $clearingEvent = $relevantDecisionsItem6[$rfForACE];
380 $eventId = $clearingEvent->getEventId();
381 $bulkHighlights = $this->highlightDao->getHighlightBulk(6, $eventId);
383 $this->assertEquals(1, count($bulkHighlights));
386 $bulkHighlight = $bulkHighlights[0];
387 $this->assertEquals($licenseId, $bulkHighlight->getLicenseId());
388 $this->assertEquals(Highlight::BULK, $bulkHighlight->getType());
389 $this->assertEquals(3, $bulkHighlight->getStart());
390 $this->assertEquals(10, $bulkHighlight->getEnd());
392 $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
394 $this->assertEquals(1, count($bulkHighlights));
395 $this->assertEquals($bulkHighlight, $bulkHighlights[0]);
401 $this->setUpTables();
412 $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId, $uploadTreeId,
413 array($licenseId => array($removing,
"",
"",
"")), $refText);
415 $this->assertGreaterThan($expected=0, $bulkId);
418 list($output,$retCode) = $this->runBulkMonk($userId, $groupId, $jobId, $bulkId);
422 $this->assertEquals($retCode, 0,
'monk bulk failed: '.$output);
425 $relevantDecisionsItem6 = $this->clearingDao->getRelevantClearingEvents($bounds6, $groupId);
426 $relevantDecisionsItem7 = $this->clearingDao->getRelevantClearingEvents($bounds7, $groupId);
428 assertThat(count($relevantDecisionsItem6),is(equalTo(0)));
429 assertThat(count($relevantDecisionsItem7),is(equalTo(0)));
431 $bulkHighlights = $this->highlightDao->getHighlightBulk(6);
433 assertThat(count($bulkHighlights),is(equalTo(0)));
testRunTwoIndependentMonkBulkScans()
testRunMonkBulkScanWithBadSearchForDiff()
testRunMonkBulkScanWithAnEmptySearchText()
fo_dbManager * dbManager
fo_dbManager object