9 namespace Fossology\Lib\Dao;
29 protected function setUp() :
void
32 $this->
dbManager = &$this->testDb->getDbManager();
34 $this->testDb->createPlainTables(array(
'upload',
'uploadtree',
35 'report_info',
'upload_events'));
37 $this->
dbManager->prepare($stmt =
'insert.upload',
38 "INSERT INTO upload (upload_pk, uploadtree_tablename) VALUES ($1, $2)");
39 $uploadArray = array(array(1,
'uploadtree'), array(2,
'uploadtree_a'));
40 foreach ($uploadArray as $uploadEntry) {
43 $logger = M::mock(
'Monolog\Logger');
44 $logger->shouldReceive(
'debug');
45 $uploadPermissionDao = M::mock(
'Fossology\Lib\Dao\UploadPermissionDao');
48 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
51 protected function tearDown() :
void
53 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
58 public function testGetFileTreeBounds()
63 $this->
dbManager->queryOnce(
"INSERT INTO uploadtree (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
64 .
" VALUES ($uploadTreeId, NULL, $uploadId, 1, 33792, $left, 2, 'WXwindows.txt');",
65 __METHOD__ .
'.insert.data');
67 $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId);
68 assertThat($itemTreeBounds, anInstanceOf(
'Fossology\Lib\Data\Tree\ItemTreeBounds'));
70 assertThat($uploadId, equalTo($itemTreeBounds->getUploadId()));
71 assertThat($left, equalTo($itemTreeBounds->getLeft()));
74 public function testGetNextItemWithEmptyArchive()
78 $nextItem = $this->uploadDao->getNextItem(1, 1);
79 assertThat($nextItem, is(nullValue()));
82 public function testGetPreviousItemWithEmptyArchive()
86 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
87 assertThat($nextItem, is(nullValue()));
90 public function testGetNextItemWithSingleFile()
95 $nextItem = $this->uploadDao->getNextItem(1, 1);
96 assertThat($nextItem->getId(), is(6));
99 public function testGetPreviousItemWithSingleFile()
104 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
105 assertThat($nextItem, is(nullValue()));
108 public function testGetNextItemWithNestedFile()
113 $nextItem = $this->uploadDao->getNextItem(1, 1);
114 assertThat($nextItem->getId(), is(8));
117 public function testGetPreviousItemWithNestedFile()
122 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
123 assertThat($nextItem, is(nullValue()));
126 public function testGetNextItemWithFileAfterEmptyDirectory()
131 $nextItem = $this->uploadDao->getNextItem(1, 1);
132 assertThat($nextItem->getId(), is(8));
135 public function testGetPreviousItemWithFileAfterEmptyDirectory()
140 $nextItem = $this->uploadDao->getPreviousItem(1, 1);
141 assertThat($nextItem, is(nullValue()));
144 public function testGetNextItemWithMultipleFiles()
149 $nextItem = $this->uploadDao->getNextItem(1, 6);
150 assertThat($nextItem->getId(), is(7));
153 public function testGetPreviousItemWithMultipleFiles()
158 $nextItem = $this->uploadDao->getPreviousItem(1, 6);
159 assertThat($nextItem, anInstanceOf(Item::class));
160 assertThat($nextItem->getId(), is(8));
169 $this->
dbManager->prepare($stmt =
'insert.uploadtree',
170 "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)");
171 foreach ($uploadTreeArray as $uploadTreeEntry) {
182 $right_base = 5 + count($subentries) * 2;
184 $uploadTreeArray = array_merge(
186 array(1,
null, 1, 1, 536904704, 1, $right_base + 5,
'archive.tar.gz'),
187 array(2, 1, 1, 0, 805323776, 2, $right_base + 4,
'artifact.dir'),
188 array(3, 2, 1, 2, 536903680, 3, $right_base + 3,
'archive.tar'),
189 array(4, 3, 1, 0, 805323776, 4, $right_base + 2,
'artifact.dir'),
190 array(5, 4, 1, 0, 536888320, 5, $right_base + 1,
'archive')),
200 return array(array(6, 5, 1, 3, 33188, 6, 10,
'README'));
209 array(6, 5, 1, 0, 536888320, 7, 12,
'docs'),
210 array(7, 6, 1, 0, 536888320, 8, 11,
'txt'),
211 array(8, 7, 1, 3, 33188, 9, 10,
'README')
227 array(6, 5, 1, 0, 536888320, 7, 10,
'docs'),
228 array(7, 6, 1, 0, 536888320, 8, 9,
'txt'),
229 array(8, 5, 1, 3, 33188, 11, 12,
'README')
239 array(6, 5, 1, 3, 33188, 9, 10,
'INSTALL'),
240 array(7, 5, 1, 4, 33188, 11, 12,
'README'),
241 array(8, 5, 1, 5, 33188, 7, 8,
'COPYING')
288 3653, 3668, 3683, 3685, 3671, 3665, 3676, 3675, 3681, 3677, 3673, 3658, 3660, 3686,
291 protected function getTestFileStructure()
294 $isContainer = 536888320;
296 array(3650, NULL, 32, 3286, 536904704, 1, 76,
'uploadDaoTest.tar'),
297 array(3651, 3650, 32, 0, 805323776, 2, 75,
'artifact.dir'),
298 array(3652, 3651, 32, 0, 536888320, 3, 74,
'uploadDaoTest'),
300 array(3653, 3652, 32, 3287, $isFile, 4, 5,
'A'),
301 array(3663, 3652, 32, 3287, $isContainer, 6, 7,
'B'),
302 array(3668, 3652, 32, 3294, $isFile, 8, 9,
'C'),
303 array(3682, 3652, 32, 0, $isContainer, 10, 16,
'D'),
304 array(3683, 3682, 32, 3303, $isFile, 11, 12,
'E'),
306 array(3685, 3682, 32, 3305, $isFile, 14, 15,
'G'),
307 array(3669, 3652, 32, 0, $isContainer, 16, 23,
'H'),
309 array(3671, 3669, 32, 3296, $isFile, 19, 20,
'J'),
311 array(3661, 3652, 32, 0, $isContainer, 24, 37,
'L'),
312 array(3666, 3661, 32, 0, $isContainer, 25, 28,
'L1'),
313 array(3667, 3666, 32, 0, $isContainer, 26, 27,
'L1a'),
314 array(3664, 3661, 32, 0, $isContainer, 29, 32,
'L2'),
315 array(3665, 3664, 32, 3292, $isFile, 30, 31,
'L2a'),
316 array(3662, 3661, 32, 0, $isContainer, 33, 36,
'L3'),
318 array(3654, 3652, 32, 0, $isContainer, 38, 41,
'M'),
319 array(3655, 3654, 32, 0, $isContainer, 39, 40,
'M1'),
320 array(3674, 3652, 32, 0, $isContainer, 42, 57,
'N'),
321 array(3676, 3674, 32, 3293, $isFile, 43, 44,
'N1'),
322 array(3678, 3674, 32, 0, $isContainer, 45, 48,
'N2'),
324 array(3675, 3674, 32, 3299, $isFile, 49, 50,
'N3'),
325 array(3680, 3674, 32, 0, $isContainer, 51, 54,
'N4'),
326 array(3681, 3680, 32, 3302, $isFile, 52, 53,
'N4a'),
327 array(3677, 3674, 32, 3300, $isFile, 55, 56,
'N5'),
328 array(3673, 3652, 32, 3298, $isFile, 58, 59,
'O'),
329 array(3656, 3652, 32, 0, $isContainer, 60, 69,
'P'),
331 array(3657, 3656, 32, 0, $isContainer, 63, 66,
'P2'),
332 array(3658, 3657, 32, 3288, $isFile, 64, 65,
'P2a'),
333 array(3660, 3656, 32, 3290, 33188, 67, 68,
'P3'),
334 array(3686, 3652, 32, 3306, 33188, 70, 71,
'R'),
339 public function testGetNextItemUsesRecursiveAndRegularSearchAsFallback()
344 $nextItem = $this->uploadDao->getNextItem(32, 3666);
345 assertThat($nextItem->getId(), is(3665));
348 public function testGetPrevItemUsesRecursiveAndRegularSearchAsFallback()
352 $nextItem = $this->uploadDao->getPreviousItem(32, 3666);
353 assertThat($nextItem->getId(), is(3671));
356 public function testGetNextItemUsesRecursiveOnly()
360 $nextItem = $this->uploadDao->getNextItem(32, 3674);
361 assertThat($nextItem->getId(), is(3676));
364 public function testGetPrevItemUsesRecursiveOnly()
368 $nextItem = $this->uploadDao->getPreviousItem(32, 3674);
369 assertThat($nextItem->getId(), is(3665));
372 public function testGetNextFull()
377 foreach ($this->entries as $entry) {
378 $nextItem = $this->uploadDao->getNextItem(32, $previousId);
379 assertThat($nextItem->getId(), is($entry));
380 $previousId = $entry;
383 $nextItem = $this->uploadDao->getNextItem(32, $previousId);
384 assertThat($nextItem, is(nullValue()));
387 public function testGetPreviousFull()
391 $entries = array_reverse($this->entries);
394 foreach (array_slice(
$entries, 1) as $entry) {
395 $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
396 assertThat($previousItem->getId(), is($entry));
397 $previousId = $entry;
400 $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
401 assertThat($previousItem, is(nullValue()));
405 public function testCountNonArtifactDescendants()
407 $this->
dbManager->queryOnce(
'ALTER TABLE uploadtree RENAME TO uploadtree_a');
408 $this->testDb->insertData(array(
'uploadtree_a'));
410 $artifact =
new ItemTreeBounds(2,
'uploadtree_a', 1, 2, 3);
411 $artifactDescendants = $this->uploadDao->countNonArtifactDescendants($artifact);
412 assertThat($artifactDescendants, is(0));
414 $zip =
new ItemTreeBounds(1,
'uploadtree_a', 1, 1, 24);
415 $zipDescendants = $this->uploadDao->countNonArtifactDescendants($zip);
416 assertThat($zipDescendants, is(count(array(6,7,8,10,11,12)) ) );
420 public function testGetUploadParent()
423 $topId = $this->uploadDao->getUploadParent(32);
424 assertThat($topId,equalTo(3650));
427 public function testGetUploadParentFromBrokenTree()
429 $this->expectException(Exception::class);
430 $this->expectExceptionMessage(
"Missing upload tree parent for upload");
431 $this->
prepareUploadTree(array(array(4651, 3650, 33, 0, 805323776, 2, 75,
'artifact.dir')));
432 $this->uploadDao->getUploadParent(33);
435 public function testGetUploadParentFromNonExistingTree()
437 $this->expectException(Exception::class);
438 $this->expectExceptionMessage(
"Missing upload tree parent for upload");
439 $this->uploadDao->getUploadParent(34);
442 public function testGetUploadHashes()
444 $this->testDb->createPlainTables(array(
'pfile'));
445 $this->
dbManager->queryOnce(
'TRUNCATE upload');
446 $this->testDb->insertData(array(
'upload',
'pfile'));
448 $hashes = $this->uploadDao->getUploadHashes(44);
449 assertThat($hashes,equalTo(array(
'md5'=>
'E7295A5773D0EA17D53CBE6293924DD4',
'sha1'=>
'93247C8DB814F0A224B75B522C1FA4DC92DC3078',
'sha256'=>
'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1')));
452 public function testGetFatItem()
455 $isContainer = 536888320;
457 $this->
prepareUploadTree(array(array($itemM1a, 3655, 32, 0, $isContainer, 39+0, 40-0,
'M1a')));
458 $this->
dbManager->queryOnce(
'UPDATE uploadtree SET realparent=parent WHERE ufile_mode&(1<<28)=0',__METHOD__.
'.fixRealparent');
460 $fatA = $this->uploadDao->getFatItemId($itemA=3653, 32,
'uploadtree');
461 assertThat($fatA,equalTo($itemA));
462 $fatB = $this->uploadDao->getFatItemId($itemBEmpty=3663, 32,
'uploadtree');
463 assertThat($fatB, equalTo($itemBEmpty));
464 $fatD = $this->uploadDao->getFatItemId($itemDFolder=3682, 32,
'uploadtree');
465 assertThat($fatD, equalTo($itemDFolder));
466 $fatL1 = $this->uploadDao->getFatItemId($itemL1ToFolder=3666, 32,
'uploadtree');
467 assertThat($fatL1, equalTo(3667));
468 $fatL2 = $this->uploadDao->getFatItemId($itemL2ToItem=3664, 32,
'uploadtree');
469 assertThat($fatL2, equalTo(3665));
471 $fatM = $this->uploadDao->getFatItemId(3654, 32,
'uploadtree');
472 assertThat($fatM, equalTo($itemM1a));
486 $time_now =
new DateTime();
487 $time_1_day = DateTime::createFromFormat(
"U",
488 strtotime($time_now->format(DATE_ATOM) .
" +1 day"));
489 $this->
dbManager->insertTableRow(
"upload_events", [
491 "event_ts" => $time_now->format(DATE_ATOM),
494 $this->
dbManager->insertTableRow(
"upload_events", [
496 "event_ts" => $time_1_day->format(DATE_ATOM),
499 $assigneeDate = $this->uploadDao->getAssigneeDate(2);
500 assertThat(strtotime($assigneeDate), equalTo($time_now->getTimestamp()));
501 self::assertThat($this->uploadDao->getAssigneeDate(3), self::equalTo(
null));
515 $time_now =
new DateTime();
516 $time_1_day = DateTime::createFromFormat(
"U",
517 strtotime($time_now->format(DATE_ATOM) .
" +1 day"));
518 $this->
dbManager->insertTableRow(
"upload_events", [
520 "event_ts" => $time_now->format(DATE_ATOM),
523 $this->
dbManager->insertTableRow(
"upload_events", [
525 "event_ts" => $time_1_day->format(DATE_ATOM),
528 $assigneeDate = $this->uploadDao->getClosedDate(2);
529 assertThat(strtotime($assigneeDate), equalTo($time_1_day->getTimestamp()));
530 self::assertThat($this->uploadDao->getClosedDate(3), self::equalTo(
null));
getSubentriesForMultipleFiles()
getSubentriesForNestedFile()
prepareModularTable($subentries=array())
prepareUploadTree($uploadTreeArray=array())
getSubentriesForSingleFile()
getSubentriesForFileAfterEmptyDirectory()
This class contains the events for the upload_events table.
const UPLOAD_CLOSED_EVENT
fo_dbManager * dbManager
fo_dbManager object