9 namespace Fossology\Lib\Dao;
23 public $assertCountBefore;
32 protected function setUp() :
void
35 $this->
dbManager = &$this->testDb->getDbManager();
37 $this->testDb->createPlainTables(array(
'upload',
'uploadtree',
38 'report_info',
'upload_events',
'users'));
40 $this->
dbManager->prepare($stmt =
'insert.upload',
41 "INSERT INTO upload (upload_pk, uploadtree_tablename) VALUES ($1, $2)");
42 $uploadArray = array(array(1,
'uploadtree'), array(2,
'uploadtree_a'));
43 foreach ($uploadArray as $uploadEntry) {
46 $logger = M::mock(
'Monolog\Logger');
47 $logger->shouldReceive(
'debug');
48 $uploadPermissionDao = M::mock(
'Fossology\Lib\Dao\UploadPermissionDao');
51 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
54 protected function tearDown() :
void
56 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
61 public function testGetFileTreeBounds()
66 $this->
dbManager->queryOnce(
"INSERT INTO uploadtree (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
67 .
" VALUES ($uploadTreeId, NULL, $uploadId, 1, 33792, $left, 2, 'WXwindows.txt');",
68 __METHOD__ .
'.insert.data');
70 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId);
71 assertThat($itemTreeBounds, anInstanceOf(
'Fossology\Lib\Data\Tree\ItemTreeBounds'));
73 assertThat($uploadId, equalTo($itemTreeBounds->getUploadId()));
74 assertThat($left, equalTo($itemTreeBounds->getLeft()));
77 public function testGetNextItemWithEmptyArchive()
81 $nextItem = $this->uploadDao->getNextItem(1, 1);
82 assertThat($nextItem, is(nullValue()));
85 public function testGetPreviousItemWithEmptyArchive()
89 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
90 assertThat($nextItem, is(nullValue()));
93 public function testGetNextItemWithSingleFile()
98 $nextItem = $this->uploadDao->getNextItem(1, 1);
99 assertThat($nextItem->getId(), is(6));
102 public function testGetPreviousItemWithSingleFile()
107 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
108 assertThat($nextItem, is(nullValue()));
111 public function testGetNextItemWithNestedFile()
116 $nextItem = $this->uploadDao->getNextItem(1, 1);
117 assertThat($nextItem->getId(), is(8));
120 public function testGetPreviousItemWithNestedFile()
125 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
126 assertThat($nextItem, is(nullValue()));
129 public function testGetNextItemWithFileAfterEmptyDirectory()
134 $nextItem = $this->uploadDao->getNextItem(1, 1);
135 assertThat($nextItem->getId(), is(8));
138 public function testGetPreviousItemWithFileAfterEmptyDirectory()
143 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
144 assertThat($nextItem, is(nullValue()));
147 public function testGetNextItemWithMultipleFiles()
152 $nextItem = $this->uploadDao->getNextItem(1, 6);
153 assertThat($nextItem->getId(), is(7));
156 public function testGetPreviousItemWithMultipleFiles()
161 $nextItem = $this->uploadDao->getPreviousItem(1, 6);
162 assertThat($nextItem, anInstanceOf(Item::class));
163 assertThat($nextItem->getId(), is(8));
172 $this->
dbManager->prepare($stmt =
'insert.uploadtree',
173 "INSERT INTO uploadtree (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)");
174 foreach ($uploadTreeArray as $uploadTreeEntry) {
185 $right_base = 5 + count($subentries) * 2;
187 $uploadTreeArray = array_merge(
189 array(1,
null, 1, 1, 536904704, 1, $right_base + 5,
'archive.tar.gz'),
190 array(2, 1, 1, 0, 805323776, 2, $right_base + 4,
'artifact.dir'),
191 array(3, 2, 1, 2, 536903680, 3, $right_base + 3,
'archive.tar'),
192 array(4, 3, 1, 0, 805323776, 4, $right_base + 2,
'artifact.dir'),
193 array(5, 4, 1, 0, 536888320, 5, $right_base + 1,
'archive')),
203 return array(array(6, 5, 1, 3, 33188, 6, 10,
'README'));
212 array(6, 5, 1, 0, 536888320, 7, 12,
'docs'),
213 array(7, 6, 1, 0, 536888320, 8, 11,
'txt'),
214 array(8, 7, 1, 3, 33188, 9, 10,
'README')
230 array(6, 5, 1, 0, 536888320, 7, 10,
'docs'),
231 array(7, 6, 1, 0, 536888320, 8, 9,
'txt'),
232 array(8, 5, 1, 3, 33188, 11, 12,
'README')
242 array(6, 5, 1, 3, 33188, 9, 10,
'INSTALL'),
243 array(7, 5, 1, 4, 33188, 11, 12,
'README'),
244 array(8, 5, 1, 5, 33188, 7, 8,
'COPYING')
291 3653, 3668, 3683, 3685, 3671, 3665, 3676, 3675, 3681, 3677, 3673, 3658, 3660, 3686,
294 protected function getTestFileStructure()
297 $isContainer = 536888320;
299 array(3650, NULL, 32, 3286, 536904704, 1, 76,
'uploadDaoTest.tar'),
300 array(3651, 3650, 32, 0, 805323776, 2, 75,
'artifact.dir'),
301 array(3652, 3651, 32, 0, 536888320, 3, 74,
'uploadDaoTest'),
303 array(3653, 3652, 32, 3287, $isFile, 4, 5,
'A'),
304 array(3663, 3652, 32, 3287, $isContainer, 6, 7,
'B'),
305 array(3668, 3652, 32, 3294, $isFile, 8, 9,
'C'),
306 array(3682, 3652, 32, 0, $isContainer, 10, 16,
'D'),
307 array(3683, 3682, 32, 3303, $isFile, 11, 12,
'E'),
309 array(3685, 3682, 32, 3305, $isFile, 14, 15,
'G'),
310 array(3669, 3652, 32, 0, $isContainer, 16, 23,
'H'),
312 array(3671, 3669, 32, 3296, $isFile, 19, 20,
'J'),
314 array(3661, 3652, 32, 0, $isContainer, 24, 37,
'L'),
315 array(3666, 3661, 32, 0, $isContainer, 25, 28,
'L1'),
316 array(3667, 3666, 32, 0, $isContainer, 26, 27,
'L1a'),
317 array(3664, 3661, 32, 0, $isContainer, 29, 32,
'L2'),
318 array(3665, 3664, 32, 3292, $isFile, 30, 31,
'L2a'),
319 array(3662, 3661, 32, 0, $isContainer, 33, 36,
'L3'),
321 array(3654, 3652, 32, 0, $isContainer, 38, 41,
'M'),
322 array(3655, 3654, 32, 0, $isContainer, 39, 40,
'M1'),
323 array(3674, 3652, 32, 0, $isContainer, 42, 57,
'N'),
324 array(3676, 3674, 32, 3293, $isFile, 43, 44,
'N1'),
325 array(3678, 3674, 32, 0, $isContainer, 45, 48,
'N2'),
327 array(3675, 3674, 32, 3299, $isFile, 49, 50,
'N3'),
328 array(3680, 3674, 32, 0, $isContainer, 51, 54,
'N4'),
329 array(3681, 3680, 32, 3302, $isFile, 52, 53,
'N4a'),
330 array(3677, 3674, 32, 3300, $isFile, 55, 56,
'N5'),
331 array(3673, 3652, 32, 3298, $isFile, 58, 59,
'O'),
332 array(3656, 3652, 32, 0, $isContainer, 60, 69,
'P'),
334 array(3657, 3656, 32, 0, $isContainer, 63, 66,
'P2'),
335 array(3658, 3657, 32, 3288, $isFile, 64, 65,
'P2a'),
336 array(3660, 3656, 32, 3290, 33188, 67, 68,
'P3'),
337 array(3686, 3652, 32, 3306, 33188, 70, 71,
'R'),
342 public function testGetNextItemUsesRecursiveAndRegularSearchAsFallback()
347 $nextItem = $this->uploadDao->getNextItem(32, 3666);
348 assertThat($nextItem->getId(), is(3665));
351 public function testGetPrevItemUsesRecursiveAndRegularSearchAsFallback()
355 $nextItem = $this->uploadDao->getPreviousItem(32, 3666);
356 assertThat($nextItem->getId(), is(3671));
359 public function testGetNextItemUsesRecursiveOnly()
363 $nextItem = $this->uploadDao->getNextItem(32, 3674);
364 assertThat($nextItem->getId(), is(3676));
367 public function testGetPrevItemUsesRecursiveOnly()
371 $nextItem = $this->uploadDao->getPreviousItem(32, 3674);
372 assertThat($nextItem->getId(), is(3665));
375 public function testGetNextFull()
380 foreach ($this->entries as $entry) {
381 $nextItem = $this->uploadDao->getNextItem(32, $previousId);
382 assertThat($nextItem->getId(), is($entry));
383 $previousId = $entry;
386 $nextItem = $this->uploadDao->getNextItem(32, $previousId);
387 assertThat($nextItem, is(nullValue()));
390 public function testGetPreviousFull()
394 $entries = array_reverse($this->entries);
397 foreach (array_slice(
$entries, 1) as $entry) {
398 $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
399 assertThat($previousItem->getId(), is($entry));
400 $previousId = $entry;
403 $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
404 assertThat($previousItem, is(nullValue()));
408 public function testCountNonArtifactDescendants()
410 $this->
dbManager->queryOnce(
'ALTER TABLE uploadtree RENAME TO uploadtree_a');
411 $this->testDb->insertData(array(
'uploadtree_a'));
414 $artifactDescendants = $this->uploadDao->countNonArtifactDescendants($artifact);
415 assertThat($artifactDescendants, is(0));
418 $zipDescendants = $this->uploadDao->countNonArtifactDescendants($zip);
419 assertThat($zipDescendants, is(count(array(6,7,8,10,11,12)) ) );
423 public function testGetUploadParent()
426 $topId = $this->uploadDao->getUploadParent(32);
427 assertThat($topId,equalTo(3650));
430 public function testGetUploadParentFromBrokenTree()
432 $this->expectException(Exception::class);
433 $this->expectExceptionMessage(
"Missing upload tree parent for upload");
434 $this->
prepareUploadTree(array(array(4651, 3650, 33, 0, 805323776, 2, 75,
'artifact.dir')));
435 $this->uploadDao->getUploadParent(33);
438 public function testGetUploadParentFromNonExistingTree()
440 $this->expectException(Exception::class);
441 $this->expectExceptionMessage(
"Missing upload tree parent for upload");
442 $this->uploadDao->getUploadParent(34);
445 public function testGetUploadHashes()
447 $this->testDb->createPlainTables(array(
'pfile'));
448 $this->
dbManager->queryOnce(
'TRUNCATE upload');
449 $this->testDb->insertData(array(
'upload',
'pfile'));
451 $hashes = $this->uploadDao->getUploadHashes(44);
452 assertThat($hashes,equalTo(array(
'md5'=>
'E7295A5773D0EA17D53CBE6293924DD4',
'sha1'=>
'93247C8DB814F0A224B75B522C1FA4DC92DC3078',
'sha256'=>
'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1')));
455 public function testGetFatItem()
458 $isContainer = 536888320;
460 $this->
prepareUploadTree(array(array($itemM1a, 3655, 32, 0, $isContainer, 39+0, 40-0,
'M1a')));
461 $this->
dbManager->queryOnce(
'UPDATE uploadtree SET realparent=parent WHERE ufile_mode&(1<<28)=0',__METHOD__.
'.fixRealparent');
463 $fatA = $this->uploadDao->getFatItemId($itemA=3653, 32,
'uploadtree');
464 assertThat($fatA,equalTo($itemA));
465 $fatB = $this->uploadDao->getFatItemId($itemBEmpty=3663, 32,
'uploadtree');
466 assertThat($fatB, equalTo($itemBEmpty));
467 $fatD = $this->uploadDao->getFatItemId($itemDFolder=3682, 32,
'uploadtree');
468 assertThat($fatD, equalTo($itemDFolder));
469 $fatL1 = $this->uploadDao->getFatItemId($itemL1ToFolder=3666, 32,
'uploadtree');
470 assertThat($fatL1, equalTo(3667));
471 $fatL2 = $this->uploadDao->getFatItemId($itemL2ToItem=3664, 32,
'uploadtree');
472 assertThat($fatL2, equalTo(3665));
474 $fatM = $this->uploadDao->getFatItemId(3654, 32,
'uploadtree');
475 assertThat($fatM, equalTo($itemM1a));
489 $time_now =
new DateTime();
490 $time_1_day = DateTime::createFromFormat(
"U",
491 strtotime($time_now->format(DATE_ATOM) .
" +1 day"));
492 $this->
dbManager->insertTableRow(
"upload_events", [
494 "event_ts" => $time_now->format(DATE_ATOM),
497 $this->
dbManager->insertTableRow(
"upload_events", [
499 "event_ts" => $time_1_day->format(DATE_ATOM),
502 $assigneeDate = $this->uploadDao->getAssigneeDate(2);
503 assertThat(strtotime($assigneeDate), equalTo($time_now->getTimestamp()));
504 self::assertThat($this->uploadDao->getAssigneeDate(3), self::equalTo(
null));
518 $time_now =
new DateTime();
519 $time_1_day = DateTime::createFromFormat(
"U",
520 strtotime($time_now->format(DATE_ATOM) .
" +1 day"));
521 $this->
dbManager->insertTableRow(
"upload_events", [
523 "event_ts" => $time_now->format(DATE_ATOM),
526 $this->
dbManager->insertTableRow(
"upload_events", [
528 "event_ts" => $time_1_day->format(DATE_ATOM),
531 $assigneeDate = $this->uploadDao->getClosedDate(2);
532 assertThat(strtotime($assigneeDate), equalTo($time_1_day->getTimestamp()));
533 self::assertThat($this->uploadDao->getClosedDate(3), self::equalTo(
null));
542 $this->
dbManager->insertTableRow(
'users', [
'user_pk' => 7,
543 'user_name' =>
'cdx',
'cyclonedx_settings' => $userSettings]);
544 $this->
dbManager->getSingleRow(
"UPDATE upload SET user_fk = 7 WHERE upload_pk = 2",
548 public function testGetCyclonedxSettingsFallsBackToOwnerDefaults()
551 $this->
dbManager->insertTableRow(
'report_info', [
'upload_fk' => 2]);
553 $settings = $this->uploadDao->getCyclonedxSettings(2);
554 $this->assertTrue($settings->isOsselotExport());
555 $this->assertFalse($settings->isLicenseCommentChecked());
556 $this->assertTrue($settings->isIgnoreFilesWithoutInfo());
557 $this->assertSame(
'acme:', $settings->getTagNamespace());
558 $this->assertSame(
'1.5', $settings->getSpecVersion());
561 public function testGetCyclonedxSettingsPrefersStoredSettings()
564 $stored =
new CycloneDxSettings(
false,
false,
false,
'own:',
'1.6');
565 $this->
dbManager->insertTableRow(
'report_info', [
'upload_fk' => 2,
566 'ri_cyclonedx_selection' => $stored->toReportInfo()]);
568 $this->assertEquals($stored, $this->uploadDao->getCyclonedxSettings(2));
571 public function testNewReportInfoRowIsSeededFromOwnerDefaults()
575 $this->assertEquals(0, $this->uploadDao->getGlobalDecisionSettingsFromInfo(2, 0));
577 "SELECT ri_cyclonedx_selection FROM report_info WHERE upload_fk = 2", [], __METHOD__);
578 $this->assertEquals(
new CycloneDxSettings(
true,
false,
false,
'acme:',
'1.4'),
582 public function testUpdateReportInfoMergesCyclonedxSettings()
585 $this->
dbManager->insertTableRow(
'report_info', [
'upload_fk' => 2]);
587 $this->uploadDao->updateReportInfo(2,
'ri_cyclonedx_selection',
588 '{"specVersion":"1.4","osselotExport":"false"}');
590 $this->assertEquals(
new CycloneDxSettings(
true,
false,
false,
'acme:',
'1.4'),
591 $this->uploadDao->getCyclonedxSettings(2));
594 public static function provideInvalidCyclonedxUpdates()
597 'not an object' => [
'"1.4"'],
598 'unknown key' => [
'{"spec":"1.4"}'],
599 'comma in namespace' => [
'{"tagNamespace":"a,b:"}'],
600 'unsupported version' => [
'{"specVersion":"2.0"}'],
601 'not a boolean' => [
'{"licenseComment":"maybe"}'],
610 $this->
dbManager->insertTableRow(
'report_info', [
'upload_fk' => 2]);
611 $this->expectException(\UnexpectedValueException::class);
612 $this->uploadDao->updateReportInfo(2,
'ri_cyclonedx_selection', $value);
setUploadOwnerCyclonedxDefaults($userSettings)
testUpdateReportInfoRejectsInvalidCyclonedxSettings($value)
getSubentriesForMultipleFiles()
getSubentriesForNestedFile()
prepareModularTable($subentries=array())
prepareUploadTree($uploadTreeArray=array())
getSubentriesForSingleFile()
getSubentriesForFileAfterEmptyDirectory()
CycloneDX export settings of a user (defaults) or of an upload.
static fromReportInfo(?string $json)
This class contains the events for the upload_events table.
const UPLOAD_CLOSED_EVENT
fo_dbManager * dbManager
fo_dbManager object
Bounds of an item within an uploadtree table.