FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Steffen Weber, Johannes Najjar
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
11 use DateTime;
12 use Exception;
18 use Mockery as M;
19 
20 class UploadDaoTest extends \PHPUnit\Framework\TestCase
21 {
23  private $testDb;
25  private $dbManager;
27  private $uploadDao;
28 
29  protected function setUp() : void
30  {
31  $this->testDb = new TestPgDb();
32  $this->dbManager = &$this->testDb->getDbManager();
33 
34  $this->testDb->createPlainTables(array('upload', 'uploadtree',
35  'report_info', 'upload_events'));
36 
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) {
41  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
42  }
43  $logger = M::mock('Monolog\Logger'); // new Logger("UploadDaoTest");
44  $logger->shouldReceive('debug');
45  $uploadPermissionDao = M::mock('Fossology\Lib\Dao\UploadPermissionDao');
46  $this->uploadDao = new UploadDao($this->dbManager, $logger, $uploadPermissionDao);
47 
48  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
49  }
50 
51  protected function tearDown() : void
52  {
53  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
54  $this->testDb = null;
55  $this->dbManager = null;
56  }
57 
58  public function testGetFileTreeBounds()
59  {
60  $uploadTreeId = 103;
61  $left = 1;
62  $uploadId = 101;
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'));
69 
70  assertThat($uploadId, equalTo($itemTreeBounds->getUploadId()));
71  assertThat($left, equalTo($itemTreeBounds->getLeft()));
72  }
73 
74  public function testGetNextItemWithEmptyArchive()
75  {
76  $this->prepareModularTable();
77 
78  $nextItem = $this->uploadDao->getNextItem(1, 1);
79  assertThat($nextItem, is(nullValue()));
80  }
81 
82  public function testGetPreviousItemWithEmptyArchive()
83  {
84  $this->prepareModularTable();
85 
86  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
87  assertThat($nextItem, is(nullValue()));
88  }
89 
90  public function testGetNextItemWithSingleFile()
91  {
92  $subentries = $this->getSubentriesForSingleFile();
93  $this->prepareModularTable($subentries);
94 
95  $nextItem = $this->uploadDao->getNextItem(1, 1);
96  assertThat($nextItem->getId(), is(6));
97  }
98 
99  public function testGetPreviousItemWithSingleFile()
100  {
101  $subentries = $this->getSubentriesForSingleFile();
102  $this->prepareModularTable($subentries);
103 
104  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
105  assertThat($nextItem, is(nullValue()));
106  }
107 
108  public function testGetNextItemWithNestedFile()
109  {
110  $subentries = $this->getSubentriesForNestedFile();
111  $this->prepareModularTable($subentries);
112 
113  $nextItem = $this->uploadDao->getNextItem(1, 1);
114  assertThat($nextItem->getId(), is(8));
115  }
116 
117  public function testGetPreviousItemWithNestedFile()
118  {
119  $subentries = $this->getSubentriesForNestedFile();
120  $this->prepareModularTable($subentries);
121 
122  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
123  assertThat($nextItem, is(nullValue()));
124  }
125 
126  public function testGetNextItemWithFileAfterEmptyDirectory()
127  {
128  $subentries = $this->getSubentriesForFileAfterEmptyDirectory();
129  $this->prepareModularTable($subentries);
130 
131  $nextItem = $this->uploadDao->getNextItem(1, 1);
132  assertThat($nextItem->getId(), is(8));
133  }
134 
135  public function testGetPreviousItemWithFileAfterEmptyDirectory()
136  {
137  $subentries = $this->getSubentriesForFileAfterEmptyDirectory();
138  $this->prepareModularTable($subentries);
139 
140  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
141  assertThat($nextItem, is(nullValue()));
142  }
143 
144  public function testGetNextItemWithMultipleFiles()
145  {
146  $subentries = $this->getSubentriesForMultipleFiles();
147  $this->prepareModularTable($subentries);
148 
149  $nextItem = $this->uploadDao->getNextItem(1, 6);
150  assertThat($nextItem->getId(), is(7));
151  }
152 
153  public function testGetPreviousItemWithMultipleFiles()
154  {
155  $subentries = $this->getSubentriesForMultipleFiles();
156  $this->prepareModularTable($subentries);
157 
158  $nextItem = $this->uploadDao->getPreviousItem(1, 6);
159  assertThat($nextItem, anInstanceOf(Item::class));
160  assertThat($nextItem->getId(), is(8));
161  }
162 
167  protected function prepareUploadTree($uploadTreeArray = array())
168  {
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) {
172  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadTreeEntry));
173  }
174  }
175 
176 
180  protected function prepareModularTable($subentries = array())
181  {
182  $right_base = 5 + count($subentries) * 2;
183 
184  $uploadTreeArray = array_merge(
185  array(
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')),
191  $subentries);
192  $this->prepareUploadTree($uploadTreeArray);
193  }
194 
198  protected function getSubentriesForSingleFile()
199  {
200  return array(array(6, 5, 1, 3, 33188, 6, 10, 'README'));
201  }
202 
206  protected function getSubentriesForNestedFile()
207  {
208  return array(
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')
212  );
213  }
214 
219  {
226  return array(
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')
230  );
231  }
232 
236  protected function getSubentriesForMultipleFiles()
237  {
238  return array(
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')
242  );
243  }
244 
287  private $entries = array(
288  3653, 3668, 3683, 3685, 3671, 3665, 3676, 3675, 3681, 3677, 3673, 3658, 3660, 3686,
289  );
290 
291  protected function getTestFileStructure()
292  {
293  $isFile = 33188;
294  $isContainer = 536888320;
295  return array(
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'),
299 
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'),
305  // * D/F_NoLic 13:14
306  array(3685, 3682, 32, 3305, $isFile, 14, 15, 'G'),
307  array(3669, 3652, 32, 0, $isContainer, 16, 23, 'H'),
308  // * H/I_NoLic
309  array(3671, 3669, 32, 3296, $isFile, 19, 20, 'J'),
310  // * H/K_NoLic 21:22
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'), // * L/L1/L1a_NoLic 26:27
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'),
317  // * L/L3/L3a_NoLic 34:35
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'),
323  // * N/N2/N2a_NoLic 46:47
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'),
330  // * P/P1_NoLic 61:62
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'),
335  // * S_NoLic 72:73
336  );
337  }
338 
339  public function testGetNextItemUsesRecursiveAndRegularSearchAsFallback()
340  {
341  $this->prepareUploadTree($this->getTestFileStructure());
342 
343  // L1 -> N1
344  $nextItem = $this->uploadDao->getNextItem(32, 3666);
345  assertThat($nextItem->getId(), is(3665));
346  }
347 
348  public function testGetPrevItemUsesRecursiveAndRegularSearchAsFallback()
349  {
350  $this->prepareUploadTree($this->getTestFileStructure());
351 
352  $nextItem = $this->uploadDao->getPreviousItem(32, 3666);
353  assertThat($nextItem->getId(), is(3671));
354  }
355 
356  public function testGetNextItemUsesRecursiveOnly()
357  {
358  $this->prepareUploadTree($this->getTestFileStructure());
359 
360  $nextItem = $this->uploadDao->getNextItem(32, 3674);
361  assertThat($nextItem->getId(), is(3676));
362  }
363 
364  public function testGetPrevItemUsesRecursiveOnly()
365  {
366  $this->prepareUploadTree($this->getTestFileStructure());
367 
368  $nextItem = $this->uploadDao->getPreviousItem(32, 3674);
369  assertThat($nextItem->getId(), is(3665));
370  }
371 
372  public function testGetNextFull()
373  {
374  $this->prepareUploadTree($this->getTestFileStructure());
375 
376  $previousId = 3650;
377  foreach ($this->entries as $entry) {
378  $nextItem = $this->uploadDao->getNextItem(32, $previousId);
379  assertThat($nextItem->getId(), is($entry));
380  $previousId = $entry;
381  }
382 
383  $nextItem = $this->uploadDao->getNextItem(32, $previousId);
384  assertThat($nextItem, is(nullValue()));
385  }
386 
387  public function testGetPreviousFull()
388  {
389  $this->prepareUploadTree($this->getTestFileStructure());
390 
391  $entries = array_reverse($this->entries);
392 
393  $previousId = $entries[0];
394  foreach (array_slice($entries, 1) as $entry) {
395  $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
396  assertThat($previousItem->getId(), is($entry));
397  $previousId = $entry;
398  }
399 
400  $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
401  assertThat($previousItem, is(nullValue()));
402  }
403 
404 
405  public function testCountNonArtifactDescendants()
406  {
407  $this->dbManager->queryOnce('ALTER TABLE uploadtree RENAME TO uploadtree_a');
408  $this->testDb->insertData(array('uploadtree_a'));
409 
410  $artifact = new ItemTreeBounds(2,'uploadtree_a', 1, 2, 3);
411  $artifactDescendants = $this->uploadDao->countNonArtifactDescendants($artifact);
412  assertThat($artifactDescendants, is(0));
413 
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)) ) );
417  }
418 
419 
420  public function testGetUploadParent()
421  {
422  $this->prepareUploadTree($this->getTestFileStructure());
423  $topId = $this->uploadDao->getUploadParent(32);
424  assertThat($topId,equalTo(3650));
425  }
426 
427  public function testGetUploadParentFromBrokenTree()
428  {
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);
433  }
434 
435  public function testGetUploadParentFromNonExistingTree()
436  {
437  $this->expectException(Exception::class);
438  $this->expectExceptionMessage("Missing upload tree parent for upload");
439  $this->uploadDao->getUploadParent(34);
440  }
441 
442  public function testGetUploadHashes()
443  {
444  $this->testDb->createPlainTables(array('pfile'));
445  $this->dbManager->queryOnce('TRUNCATE upload');
446  $this->testDb->insertData(array('upload','pfile'));
447  // (pfile_pk, pfile_md5, pfile_sha1, pfile_sha256, pfile_size) := (755, 'E7295A5773D0EA17D53CBE6293924DD4', '93247C8DB814F0A224B75B522C1FA4DC92DC3078', 'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1', 10240)
448  $hashes = $this->uploadDao->getUploadHashes(44);
449  assertThat($hashes,equalTo(array('md5'=>'E7295A5773D0EA17D53CBE6293924DD4','sha1'=>'93247C8DB814F0A224B75B522C1FA4DC92DC3078','sha256'=>'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1')));
450  }
451 
452  public function testGetFatItem()
453  {
454  $this->prepareUploadTree($this->getTestFileStructure());
455  $isContainer = 536888320;
456  $itemM1a = 13655;
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');
459 
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));
470 
471  $fatM = $this->uploadDao->getFatItemId(3654, 32, 'uploadtree');
472  assertThat($fatM, equalTo($itemM1a));
473  }
474 
484  public function testGetAssigneeDate()
485  {
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", [
490  "upload_fk" => 2,
491  "event_ts" => $time_now->format(DATE_ATOM),
492  "event_type" => UploadEvents::ASSIGNEE_EVENT
493  ]);
494  $this->dbManager->insertTableRow("upload_events", [
495  "upload_fk" => 2,
496  "event_ts" => $time_1_day->format(DATE_ATOM),
497  "event_type" => UploadEvents::UPLOAD_CLOSED_EVENT
498  ]);
499  $assigneeDate = $this->uploadDao->getAssigneeDate(2);
500  assertThat(strtotime($assigneeDate), equalTo($time_now->getTimestamp()));
501  self::assertThat($this->uploadDao->getAssigneeDate(3), self::equalTo(null));
502  }
503 
513  public function testGetClosedDate()
514  {
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", [
519  "upload_fk" => 2,
520  "event_ts" => $time_now->format(DATE_ATOM),
521  "event_type" => UploadEvents::ASSIGNEE_EVENT
522  ]);
523  $this->dbManager->insertTableRow("upload_events", [
524  "upload_fk" => 2,
525  "event_ts" => $time_1_day->format(DATE_ATOM),
526  "event_type" => UploadEvents::UPLOAD_CLOSED_EVENT
527  ]);
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));
531  }
532 }
prepareModularTable($subentries=array())
prepareUploadTree($uploadTreeArray=array())
This class contains the events for the upload_events table.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16