FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
FolderDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2017, Siemens AG
4 Author: Steffen Weber
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 
24 use Mockery as M;
25 
26 class FolderDaoTest extends \PHPUnit\Framework\TestCase
27 {
29  private $testDb;
31  private $dbManager;
33  private $folderDao;
34 
35  protected function setUp()
36  {
37  $this->testDb = new TestPgDb();
38  $this->dbManager = $this->testDb->getDbManager();
39  $userDao = M::mock('Fossology\Lib\Dao\UserDao');
40  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
41  $this->folderDao = new FolderDao($this->dbManager, $userDao, $uploadDao);
42 
43  $this->testDb->createPlainTables(array('folder','foldercontents'));
44  $this->testDb->createSequences(array('folder_folder_pk_seq','foldercontents_foldercontents_pk_seq'));
45  $this->testDb->createConstraints(array('folder_pkey','foldercontents_pkey'));
46  $this->testDb->alterTables(array('folder','foldercontents'));
47 
48  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
49  }
50 
51  protected function tearDown()
52  {
53  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
54  $this->testDb = null;
55  $this->dbManager = null;
56  }
57 
58  public function testGetAllFolderIds()
59  {
60  $this->testDb->insertData(array('folder'));
61  assertThat(sizeof($this->folderDao->getAllFolderIds())>0);
62  }
63 
64  public function testHasTopLevelFolder_yes()
65  {
66  $this->testDb->insertData(array('folder'));
67  $htlf = $this->folderDao->hasTopLevelFolder();
68  assertThat($htlf, is(TRUE));
69  }
70 
71  public function testHasTopLevelFolder_no()
72  {
73  $htlf = $this->folderDao->hasTopLevelFolder();
74  assertThat($htlf, is(FALSE));
75  }
76 
77 
78  public function testInsertFolder()
79  {
80  $folderId = $this->folderDao->insertFolder($folderName = 'three', $folderDescription = 'floor(PI)');
81  assertThat($folderId, equalTo(FolderDao::TOP_LEVEL));
82  $folderInfo = $this->dbManager->getSingleRow('SELECT folder_name,folder_desc FROM folder WHERE folder_pk=$1',
83  array($folderId), __METHOD__);
84  assertThat($folderInfo, is(array('folder_name' => $folderName, 'folder_desc' => $folderDescription)));
85 
86  $folderIdPlusOne = $this->folderDao->insertFolder($folderName = 'four', $folderDescription = 'ceil(PI)');
87  assertThat($folderIdPlusOne, equalTo(FolderDao::TOP_LEVEL+1));
88 
89  }
90 
91  public function testInsertFolderContents()
92  {
93  $this->folderDao->insertFolderContents($parentId = 7, $foldercontentsMode = 2, $childId = 22);
94  $contentsInfo = $this->dbManager->getSingleRow('SELECT foldercontents_mode, child_id FROM foldercontents WHERE parent_fk=$1',
95  array($parentId), __METHOD__);
96  assertThat($contentsInfo, is(equalTo(array('foldercontents_mode' => $foldercontentsMode, 'child_id' => $childId))));
97  }
98 
99 
100  public function testGetFolderPK()
101  {
102  $folderId = $this->folderDao->insertFolder($folderName = 'three', $folderDescription = 'floor(PI)');
103 
104  assertThat($this->folderDao->getFolderId('three'), is($folderId));
105  }
106 
107  public function testGetFolderPK_Null()
108  {
109  assertThat($this->folderDao->getFolderId('three'), is(null));
110  }
111 
112  public function testGetFolderWithWrongParent()
113  {
114  $this->folderDao->insertFolder($folderName = 'three', $folderDescription = 'floor(PI)+Epsilon',2);
115  assertThat($this->folderDao->getFolderId('three'), is(null));
116  }
117 
118  public function testEnsureTopLevelFolder()
119  {
120  $htlfFresh = $this->folderDao->hasTopLevelFolder();
121  assertThat($htlfFresh, is(false));
122  $this->folderDao->ensureTopLevelFolder();
123  $htlfFixed = $this->folderDao->hasTopLevelFolder();
124  assertThat($htlfFixed, is(true));
125  $this->folderDao->ensureTopLevelFolder();
126  $folders = $this->dbManager->getSingleRow('SELECT count(*) FROM folder');
127  assertThat($folders['count'],is(1));
128  }
129 
130  public function testIsWithoutReusableFolders()
131  {
132  assertThat($this->folderDao->isWithoutReusableFolders(array()),is(true));
133  $filledFolder = array(FolderDao::REUSE_KEY=>array(1=>array('group_id'=>1,'count'=>12,'group_name'=>'one')));
134  assertThat($this->folderDao->isWithoutReusableFolders(array($filledFolder)),is(false));
135  $emptyFolder = array(FolderDao::REUSE_KEY=>array(1=>array('group_id'=>1,'count'=>0,'group_name'=>'one')));
136  assertThat($this->folderDao->isWithoutReusableFolders(array($emptyFolder)),is(true));
137  $multiAccessibleFolder = array(FolderDao::REUSE_KEY=>array(1=>array('group_id'=>1,'count'=>0,'group_name'=>'one'),
138  2=>array('group_id'=>2,'count'=>20,'group_name'=>'two')));
139  assertThat($this->folderDao->isWithoutReusableFolders(array($multiAccessibleFolder)),is(false));
140 
141  assertThat($this->folderDao->isWithoutReusableFolders(array($filledFolder,$emptyFolder)),is(false));
142  }
143 
144  public function testGetFolderChildFolders()
145  {
146  $this->folderDao->ensureTopLevelFolder();
147  $folderA = $this->folderDao->insertFolder('A', '/A', FolderDao::TOP_LEVEL);
148  $this->folderDao->insertFolder('B', '/A/B', $folderA);
149  $this->folderDao->insertFolder('C', '/C', FolderDao::TOP_LEVEL);
150  assertThat($this->folderDao->getFolderChildFolders(FolderDao::TOP_LEVEL),is(arrayWithSize(2)));
151  }
152 
153  public function testMoveContent()
154  {
155  $this->folderDao->ensureTopLevelFolder();
156  $folderA = $this->folderDao->insertFolder($folderName='A', '/A', FolderDao::TOP_LEVEL);
157  $folderB = $this->folderDao->insertFolder($folderName='B', '/A/B', $folderA);
158  $fc = $this->dbManager->getSingleRow('SELECT foldercontents_pk FROM foldercontents WHERE child_id=$1',
159  array($folderB),__METHOD__.'.needs.the.foldercontent_pk');
160  $this->folderDao->moveContent($fc['foldercontents_pk'], FolderDao::TOP_LEVEL);
161  assertThat($this->folderDao->getFolderChildFolders(FolderDao::TOP_LEVEL),is(arrayWithSize(2)));
162  }
163 
168  {
169  $this->folderDao->ensureTopLevelFolder();
170  $folderA = $this->folderDao->insertFolder($folderName='A', '/A', FolderDao::TOP_LEVEL);
171  $folderB = $this->folderDao->insertFolder($folderName='B', '/A/B', $folderA);
172  $fc = $this->dbManager->getSingleRow('SELECT foldercontents_pk FROM foldercontents WHERE child_id=$1',
173  array($folderA),__METHOD__.'.needs.the.foldercontent_pk');
174  $this->folderDao->moveContent($fc['foldercontents_pk'], $folderB);
175  }
176 
177  public function testCopyContent()
178  {
179  $this->folderDao->ensureTopLevelFolder();
180  $folderA = $this->folderDao->insertFolder($folderName='A', '/A', FolderDao::TOP_LEVEL);
181  $folderB = $this->folderDao->insertFolder($folderName='B', '/A/B', $folderA);
182  $this->folderDao->insertFolder($folderName='C', '/C', FolderDao::TOP_LEVEL);
183  $fc = $this->dbManager->getSingleRow('SELECT foldercontents_pk FROM foldercontents WHERE child_id=$1',
184  array($folderB),__METHOD__.'.needs.the.foldercontent_pk');
185  $this->folderDao->copyContent($fc['foldercontents_pk'], FolderDao::TOP_LEVEL);
186  assertThat($this->folderDao->getFolderChildFolders($folderA),is(arrayWithSize(1)));
187  assertThat($this->folderDao->getFolderChildFolders(FolderDao::TOP_LEVEL),is(arrayWithSize(3)));
188  }
189 
190  public function testGetRemovableContents()
191  {
192  $this->folderDao->ensureTopLevelFolder();
193  $folderA = $this->folderDao->insertFolder($folderName='A', '/A', FolderDao::TOP_LEVEL);
194  $this->folderDao->insertFolder('B', '/A/B', $folderA);
195  $folderC = $this->folderDao->insertFolder('C', '/C', FolderDao::TOP_LEVEL);
196  assertThat($this->folderDao->getRemovableContents($folderA),arrayWithSize(0));
197  $this->dbManager->insertTableRow('foldercontents',array('foldercontents_mode'=> FolderDao::MODE_UPLOAD,'parent_fk'=>$folderA,'child_id'=>$folderC));
198  assertThat($this->folderDao->getRemovableContents($folderA),arrayWithSize(0));
199  $this->dbManager->insertTableRow('foldercontents',array('foldercontents_mode'=> FolderDao::MODE_FOLDER,'parent_fk'=>$folderA,'child_id'=>$folderC));
200  assertThat($this->folderDao->getRemovableContents($folderA),arrayWithSize(1));
201  }
202 
203  public function testGetFolder()
204  {
205  $this->folderDao->ensureTopLevelFolder();
206  $goodFolder = $this->folderDao->getFolder(FolderDao::TOP_LEVEL);
207  assertThat($goodFolder, is(anInstanceOf(\Fossology\Lib\Data\Folder\Folder::class)));
208  assertThat($goodFolder->getId(), equalTo(FolderDao::TOP_LEVEL));
209  $badFolder = $this->folderDao->getFolder(987);
210  assertThat($badFolder, is(nullValue()));
211  }
212 }
Namespace used by reuser agent.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28