FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Author: Steffen Weber, Johannes Najjar
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\Dao;
21 
26 use Mockery as M;
27 
28 class UploadDaoTest extends \PHPUnit\Framework\TestCase
29 {
31  private $testDb;
33  private $dbManager;
35  private $uploadDao;
36 
37  protected function setUp()
38  {
39  $this->testDb = new TestPgDb();
40  $this->dbManager = &$this->testDb->getDbManager();
41 
42  $this->testDb->createPlainTables(array('upload','uploadtree'));
43 
44  $this->dbManager->prepare($stmt = 'insert.upload',
45  "INSERT INTO upload (upload_pk, uploadtree_tablename) VALUES ($1, $2)");
46  $uploadArray = array(array(1, 'uploadtree'), array(2, 'uploadtree_a'));
47  foreach ($uploadArray as $uploadEntry) {
48  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadEntry));
49  }
50  $logger = M::mock('Monolog\Logger'); // new Logger("UploadDaoTest");
51  $logger->shouldReceive('debug');
52  $uploadPermissionDao = M::mock('Fossology\Lib\Dao\UploadPermissionDao');
53  $this->uploadDao = new UploadDao($this->dbManager, $logger, $uploadPermissionDao);
54 
55  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
56  }
57 
58  protected function tearDown()
59  {
60  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
61  $this->testDb = null;
62  $this->dbManager = null;
63  }
64 
65  public function testGetFileTreeBounds()
66  {
67  $uploadTreeId = 103;
68  $left = 1;
69  $uploadId = 101;
70  $this->dbManager->queryOnce("INSERT INTO uploadtree (uploadtree_pk, parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name)"
71  . " VALUES ($uploadTreeId, NULL, $uploadId, 1, 33792, $left, 2, 'WXwindows.txt');",
72  __METHOD__ . '.insert.data');
74  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId);
75  assertThat($itemTreeBounds, anInstanceOf('Fossology\Lib\Data\Tree\ItemTreeBounds'));
76 
77  assertThat($uploadId, equalTo($itemTreeBounds->getUploadId()));
78  assertThat($left, equalTo($itemTreeBounds->getLeft()));
79  }
80 
81  public function testGetNextItemWithEmptyArchive()
82  {
83  $this->prepareModularTable();
84 
85  $nextItem = $this->uploadDao->getNextItem(1, 1);
86  assertThat($nextItem, is(nullValue()));
87  }
88 
89  public function testGetPreviousItemWithEmptyArchive()
90  {
91  $this->prepareModularTable();
92 
93  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
94  assertThat($nextItem, is(nullValue()));
95  }
96 
97  public function testGetNextItemWithSingleFile()
98  {
99  $subentries = $this->getSubentriesForSingleFile();
100  $this->prepareModularTable($subentries);
101 
102  $nextItem = $this->uploadDao->getNextItem(1, 1);
103  assertThat($nextItem->getId(), is(6));
104  }
105 
106  public function testGetPreviousItemWithSingleFile()
107  {
108  $subentries = $this->getSubentriesForSingleFile();
109  $this->prepareModularTable($subentries);
110 
111  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
112  assertThat($nextItem, is(nullValue()));
113  }
114 
115  public function testGetNextItemWithNestedFile()
116  {
117  $subentries = $this->getSubentriesForNestedFile();
118  $this->prepareModularTable($subentries);
119 
120  $nextItem = $this->uploadDao->getNextItem(1, 1);
121  assertThat($nextItem->getId(), is(8));
122  }
123 
124  public function testGetPreviousItemWithNestedFile()
125  {
126  $subentries = $this->getSubentriesForNestedFile();
127  $this->prepareModularTable($subentries);
128 
129  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
130  assertThat($nextItem, is(nullValue()));
131  }
132 
133  public function testGetNextItemWithFileAfterEmptyDirectory()
134  {
135  $subentries = $this->getSubentriesForFileAfterEmptyDirectory();
136  $this->prepareModularTable($subentries);
137 
138  $nextItem = $this->uploadDao->getNextItem(1, 1);
139  assertThat($nextItem->getId(), is(8));
140  }
141 
142  public function testGetPreviousItemWithFileAfterEmptyDirectory()
143  {
144  $subentries = $this->getSubentriesForFileAfterEmptyDirectory();
145  $this->prepareModularTable($subentries);
146 
147  $nextItem = $this->uploadDao->getPreviousItem(1, 1);
148  assertThat($nextItem, is(nullValue()));
149  }
150 
151  public function testGetNextItemWithMultipleFiles()
152  {
153  $subentries = $this->getSubentriesForMultipleFiles();
154  $this->prepareModularTable($subentries);
155 
156  $nextItem = $this->uploadDao->getNextItem(1, 6);
157  assertThat($nextItem->getId(), is(7));
158  }
159 
160  public function testGetPreviousItemWithMultipleFiles()
161  {
162  $subentries = $this->getSubentriesForMultipleFiles();
163  $this->prepareModularTable($subentries);
164 
165  $nextItem = $this->uploadDao->getPreviousItem(1, 6);
166  assertThat($nextItem, anInstanceOf(Item::class));
167  assertThat($nextItem->getId(), is(8));
168  }
169 
174  protected function prepareUploadTree($uploadTreeArray = array())
175  {
176  $this->dbManager->prepare($stmt = 'insert.uploadtree',
177  "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)");
178  foreach ($uploadTreeArray as $uploadTreeEntry) {
179  $this->dbManager->freeResult($this->dbManager->execute($stmt, $uploadTreeEntry));
180  }
181  }
182 
183 
188  protected function prepareModularTable($subentries = array())
189  {
190  $right_base = 5 + count($subentries) * 2;
191 
192  $uploadTreeArray = array_merge(
193  array(
194  array(1, null, 1, 1, 536904704, 1, $right_base + 5, 'archive.tar.gz'),
195  array(2, 1, 1, 0, 805323776, 2, $right_base + 4, 'artifact.dir'),
196  array(3, 2, 1, 2, 536903680, 3, $right_base + 3, 'archive.tar'),
197  array(4, 3, 1, 0, 805323776, 4, $right_base + 2, 'artifact.dir'),
198  array(5, 4, 1, 0, 536888320, 5, $right_base + 1, 'archive')),
199  $subentries);
200  $this->prepareUploadTree($uploadTreeArray);
201  }
202 
206  protected function getSubentriesForSingleFile()
207  {
208  return array(array(6, 5, 1, 3, 33188, 6, 10, 'README'));
209  }
210 
214  protected function getSubentriesForNestedFile()
215  {
216  return array(
217  array(6, 5, 1, 0, 536888320, 7, 12, 'docs'),
218  array(7, 6, 1, 0, 536888320, 8, 11, 'txt'),
219  array(8, 7, 1, 3, 33188, 9, 10, 'README')
220  );
221  }
222 
227  {
234  return array(
235  array(6, 5, 1, 0, 536888320, 7, 10, 'docs'),
236  array(7, 6, 1, 0, 536888320, 8, 9, 'txt'),
237  array(8, 5, 1, 3, 33188, 11, 12, 'README')
238  );
239  }
240 
244  protected function getSubentriesForMultipleFiles()
245  {
246  return array(
247  array(6, 5, 1, 3, 33188, 9, 10, 'INSTALL'),
248  array(7, 5, 1, 4, 33188, 11, 12, 'README'),
249  array(8, 5, 1, 5, 33188, 7, 8, 'COPYING')
250  );
251  }
252 
295  private $entries = array(
296  3653, 3668, 3683, 3685, 3671, 3665, 3676, 3675, 3681, 3677, 3673, 3658, 3660, 3686,
297  );
298 
299  protected function getTestFileStructure()
300  {
301  $isFile = 33188;
302  $isContainer = 536888320;
303  return array(
304  array(3650, NULL, 32, 3286, 536904704, 1, 76, 'uploadDaoTest.tar'),
305  array(3651, 3650, 32, 0, 805323776, 2, 75, 'artifact.dir'),
306  array(3652, 3651, 32, 0, 536888320, 3, 74, 'uploadDaoTest'),
307 
308  array(3653, 3652, 32, 3287, $isFile, 4, 5, 'A'),
309  array(3663, 3652, 32, 3287, $isContainer, 6, 7, 'B'),
310  array(3668, 3652, 32, 3294, $isFile, 8, 9, 'C'),
311  array(3682, 3652, 32, 0, $isContainer, 10, 16, 'D'),
312  array(3683, 3682, 32, 3303, $isFile, 11, 12, 'E'),
313  // * D/F_NoLic 13:14
314  array(3685, 3682, 32, 3305, $isFile, 14, 15, 'G'),
315  array(3669, 3652, 32, 0, $isContainer, 16, 23, 'H'),
316  // * H/I_NoLic
317  array(3671, 3669, 32, 3296, $isFile, 19, 20, 'J'),
318  // * H/K_NoLic 21:22
319  array(3661, 3652, 32, 0, $isContainer, 24, 37, 'L'),
320  array(3666, 3661, 32, 0, $isContainer, 25, 28, 'L1'),
321  array(3667, 3666, 32, 0, $isContainer, 26, 27, 'L1a'), // * L/L1/L1a_NoLic 26:27
322  array(3664, 3661, 32, 0, $isContainer, 29, 32, 'L2'),
323  array(3665, 3664, 32, 3292, $isFile, 30, 31, 'L2a'),
324  array(3662, 3661, 32, 0, $isContainer, 33, 36, 'L3'),
325  // * L/L3/L3a_NoLic 34:35
326  array(3654, 3652, 32, 0, $isContainer, 38, 41, 'M'),
327  array(3655, 3654, 32, 0, $isContainer, 39, 40, 'M1'),
328  array(3674, 3652, 32, 0, $isContainer, 42, 57, 'N'),
329  array(3676, 3674, 32, 3293, $isFile, 43, 44, 'N1'),
330  array(3678, 3674, 32, 0, $isContainer, 45, 48, 'N2'),
331  // * N/N2/N2a_NoLic 46:47
332  array(3675, 3674, 32, 3299, $isFile, 49, 50, 'N3'),
333  array(3680, 3674, 32, 0, $isContainer, 51, 54, 'N4'),
334  array(3681, 3680, 32, 3302, $isFile, 52, 53, 'N4a'),
335  array(3677, 3674, 32, 3300, $isFile, 55, 56, 'N5'),
336  array(3673, 3652, 32, 3298, $isFile, 58, 59, 'O'),
337  array(3656, 3652, 32, 0, $isContainer, 60, 69, 'P'),
338  // * P/P1_NoLic 61:62
339  array(3657, 3656, 32, 0, $isContainer, 63, 66, 'P2'),
340  array(3658, 3657, 32, 3288, $isFile, 64, 65, 'P2a'),
341  array(3660, 3656, 32, 3290, 33188, 67, 68, 'P3'),
342  array(3686, 3652, 32, 3306, 33188, 70, 71, 'R'),
343  // * S_NoLic 72:73
344  );
345  }
346 
347  public function testGetNextItemUsesRecursiveAndRegularSearchAsFallback()
348  {
349  $this->prepareUploadTree($this->getTestFileStructure());
350 
351  // L1 -> N1
352  $nextItem = $this->uploadDao->getNextItem(32, 3666);
353  assertThat($nextItem->getId(), is(3665));
354  }
355 
356  public function testGetPrevItemUsesRecursiveAndRegularSearchAsFallback()
357  {
358  $this->prepareUploadTree($this->getTestFileStructure());
359 
360  $nextItem = $this->uploadDao->getPreviousItem(32, 3666);
361  assertThat($nextItem->getId(), is(3671));
362  }
363 
364  public function testGetNextItemUsesRecursiveOnly()
365  {
366  $this->prepareUploadTree($this->getTestFileStructure());
367 
368  $nextItem = $this->uploadDao->getNextItem(32, 3674);
369  assertThat($nextItem->getId(), is(3676));
370  }
371 
372  public function testGetPrevItemUsesRecursiveOnly()
373  {
374  $this->prepareUploadTree($this->getTestFileStructure());
375 
376  $nextItem = $this->uploadDao->getPreviousItem(32, 3674);
377  assertThat($nextItem->getId(), is(3665));
378  }
379 
380  public function testGetNextFull()
381  {
382  $this->prepareUploadTree($this->getTestFileStructure());
383 
384  $previousId = 3650;
385  foreach ($this->entries as $entry) {
386  $nextItem = $this->uploadDao->getNextItem(32, $previousId);
387  assertThat($nextItem->getId(), is($entry));
388  $previousId = $entry;
389  }
390 
391  $nextItem = $this->uploadDao->getNextItem(32, $previousId);
392  assertThat($nextItem, is(nullValue()));
393  }
394 
395  public function testGetPreviousFull()
396  {
397  $this->prepareUploadTree($this->getTestFileStructure());
398 
399  $entries = array_reverse($this->entries);
400 
401  $previousId = $entries[0];
402  foreach (array_slice($entries, 1) as $entry) {
403  $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
404  assertThat($previousItem->getId(), is($entry));
405  $previousId = $entry;
406  }
407 
408  $previousItem = $this->uploadDao->getPreviousItem(32, $previousId);
409  assertThat($previousItem, is(nullValue()));
410  }
411 
412 
413  public function testCountNonArtifactDescendants()
414  {
415  $this->dbManager->queryOnce('ALTER TABLE uploadtree RENAME TO uploadtree_a');
416  $this->testDb->insertData(array('uploadtree_a'));
417 
418  $artifact = new ItemTreeBounds(2,'uploadtree_a', 1, 2, 3);
419  $artifactDescendants = $this->uploadDao->countNonArtifactDescendants($artifact);
420  assertThat($artifactDescendants, is(0));
421 
422  $zip = new ItemTreeBounds(1,'uploadtree_a', 1, 1, 24);
423  $zipDescendants = $this->uploadDao->countNonArtifactDescendants($zip);
424  assertThat($zipDescendants, is(count(array(6,7,8,10,11,12)) ) );
425  }
426 
427 
428  public function testGetUploadParent()
429  {
430  $this->prepareUploadTree($this->getTestFileStructure());
431  $topId = $this->uploadDao->getUploadParent(32);
432  assertThat($topId,equalTo(3650));
433  }
434 
439  {
440  $this->prepareUploadTree(array(array(4651, 3650, 33, 0, 805323776, 2, 75, 'artifact.dir')));
441  $this->uploadDao->getUploadParent(33);
442  }
443 
448  {
449  $this->uploadDao->getUploadParent(34);
450  }
451 
452  public function testGetUploadHashes()
453  {
454  $this->testDb->createPlainTables(array('pfile'));
455  $this->dbManager->queryOnce('TRUNCATE upload');
456  $this->testDb->insertData(array('upload','pfile'));
457  // (pfile_pk, pfile_md5, pfile_sha1, pfile_sha256, pfile_size) := (755, 'E7295A5773D0EA17D53CBE6293924DD4', '93247C8DB814F0A224B75B522C1FA4DC92DC3078', 'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1', 10240)
458  $hashes = $this->uploadDao->getUploadHashes(44);
459  assertThat($hashes,equalTo(array('md5'=>'E7295A5773D0EA17D53CBE6293924DD4','sha1'=>'93247C8DB814F0A224B75B522C1FA4DC92DC3078','sha256'=>'E29ABC32DB8B6241D598BC7C76681A7623D176D85F99E738A56C0CB684C367E1')));
460  }
461 
462  public function testGetFatItem()
463  {
464  $this->prepareUploadTree($this->getTestFileStructure());
465  $isContainer = 536888320;
466  $itemM1a = 13655;
467  $this->prepareUploadTree(array(array($itemM1a, 3655, 32, 0, $isContainer, 39+0, 40-0, 'M1a')));
468  $this->dbManager->queryOnce('UPDATE uploadtree SET realparent=parent WHERE ufile_mode&(1<<28)=0',__METHOD__.'.fixRealparent');
469 
470  $fatA = $this->uploadDao->getFatItemId($itemA=3653, 32, 'uploadtree');
471  assertThat($fatA,equalTo($itemA));
472  $fatB = $this->uploadDao->getFatItemId($itemBEmpty=3663, 32, 'uploadtree');
473  assertThat($fatB, equalTo($itemBEmpty));
474  $fatD = $this->uploadDao->getFatItemId($itemDFolder=3682, 32, 'uploadtree');
475  assertThat($fatD, equalTo($itemDFolder));
476  $fatL1 = $this->uploadDao->getFatItemId($itemL1ToFolder=3666, 32, 'uploadtree');
477  assertThat($fatL1, equalTo(3667));
478  $fatL2 = $this->uploadDao->getFatItemId($itemL2ToItem=3664, 32, 'uploadtree');
479  assertThat($fatL2, equalTo(3665));
480 
481  $fatM = $this->uploadDao->getFatItemId(3654, 32, 'uploadtree');
482  assertThat($fatM, equalTo($itemM1a));
483  }
484 }
prepareUploadTree($uploadTreeArray=array())
prepareModularTable($subentries=array())
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28