FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadBrowseProxyTest.php
1 <?php
2 /*
3 Copyright (C) 2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Proxy;
20 
25 
26 class UploadBrowseProxyTest extends \PHPUnit\Framework\TestCase
27 {
28  private $testDb;
29  private $groupId = 401;
30 
31  protected function setUp()
32  {
33  $this->testDb = new TestPgDb();
34  $this->testDb->createPlainTables( array('upload','upload_clearing','perm_upload') );
35  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>1,'upload_filename'=>'for.all','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_READ,'pfile_fk'=>31415));
36  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>2,'upload_filename'=>'for.this','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_NONE));
37  $this->testDb->getDbManager()->insertTableRow('perm_upload', array('perm_upload_pk'=>1, 'upload_fk'=>2,'group_fk'=>$this->groupId,'perm'=>Auth::PERM_READ));
38  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>3,'upload_filename'=>'for.noone','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_NONE));
39  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
40  }
41 
42  protected function tearDown()
43  {
44  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
45  $this->testDb = null;
46  }
47 
48  public function testConstructAndSanity()
49  {
50  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager(), true);
51  assertThat($uploadBrowseProxy,is(anInstanceOf(UploadBrowseProxy::class)));
52  }
53 
54  public function testUpdateTableStatus()
55  {
56  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::OPEN));
57  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
58  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::IN_PROGRESS);
59  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
60  assertThat($updatedRow['status_fk'],equalTo($newStatus));
61  }
62 
63  public function testUpdateTableStatusFromRejectByUser()
64  {
65  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::REJECTED));
66  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
67  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::IN_PROGRESS);
68  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
69  assertThat($updatedRow['status_fk'],equalTo(UploadStatus::REJECTED));
70  }
71 
72  public function testUpdateTableStatusByAdvisor()
73  {
74  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::OPEN));
75  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::ADVISOR, $this->testDb->getDbManager());
76  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::IN_PROGRESS);
77  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
78  assertThat($updatedRow['status_fk'],equalTo($newStatus));
79  }
80 
85  {
86  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::OPEN));
87  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
88  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::REJECTED);
89  }
90 
91  public function testUpdateTableStatusToRejectByAdvisor()
92  {
93  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::OPEN));
94  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::ADVISOR, $this->testDb->getDbManager());
95  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::REJECTED);
96  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
97  assertThat($updatedRow['status_fk'],equalTo($newStatus));
98  }
99 
104  {
105  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
106  $uploadBrowseProxy->updateTable('nonEditableColumn', 1, 123);
107  }
108 
109  public function testUpdateTableAssigneeByAdvisor()
110  {
111  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::ADVISOR, $this->testDb->getDbManager());
112  $uploadBrowseProxy->updateTable('assignee', $uploadId=1, $newAssignee=123);
113  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT assignee FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
114  assertThat($updatedRow['assignee'],equalTo($newAssignee));
115  }
116 
121  {
122  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
123  $uploadBrowseProxy->updateTable('assignee', 1, 123);
124  }
125 
126 
127  private function wrapperTestMoveUploadToInfinity($uploadId, $order='DESC')
128  {
129  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>1));
130  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>2,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>2));
131 
132  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
133  $uploadBrowseProxy->moveUploadToInfinity($uploadId, 'DESC'==$order);
134 
135  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT upload_fk FROM upload_clearing WHERE group_fk=$1 ORDER BY '.UploadBrowseProxy::PRIO_COLUMN." $order LIMIT 1",array($this->groupId));
136  assertThat($updatedRow['upload_fk'],equalTo($uploadId));
137  }
138 
139  public function testMoveUploadToInfinityTop()
140  {
141  $this->wrapperTestMoveUploadToInfinity(1, 'DESC');
142  }
143 
144  public function testMoveUploadToInfinityDown()
145  {
146  $this->wrapperTestMoveUploadToInfinity(2, 'ASC');
147  }
148 
149 
150  private function wrapperTestMoveUploadBeyond($moveUpload=4, $beyondUpload=2, $expectedPrio = 1.5)
151  {
152  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>4,'upload_filename'=>'for.all4','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_READ));
153  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>5,'upload_filename'=>'for.all5','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_READ));
154 
155  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>1));
156  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>2,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>2));
157  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>4,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>4));
158  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>5,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>5));
159 
160  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
161  $uploadBrowseProxy->moveUploadBeyond($moveUpload, $beyondUpload);
162  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT '.UploadBrowseProxy::PRIO_COLUMN.' FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($moveUpload,$this->groupId));
163  assertThat($updatedRow[UploadBrowseProxy::PRIO_COLUMN],equalTo($expectedPrio));
164 
165  }
166 
167  public function testMoveUploadBeyondDown()
168  {
169  $this->wrapperTestMoveUploadBeyond(4,2,1.5);
170  }
171 
172  public function testMoveUploadBeyondUp()
173  {
174  $this->wrapperTestMoveUploadBeyond(2,4,4.5);
175  }
176 
177  public function testMoveUploadBeyondFarDown()
178  {
179  $this->wrapperTestMoveUploadBeyond(4,1,0.5);
180  }
181 
182  public function testMoveUploadBeyondFarUp()
183  {
184  $this->wrapperTestMoveUploadBeyond(4,5,5.5);
185  }
186 
187 
188  public function testGetFolderPartialQuery()
189  {
190  $this->testDb->createPlainTables(array('foldercontents','uploadtree'));
191  $folderId = 701;
192  $uploadId = 1;
193  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>1));
194  $this->testDb->getDbManager()->insertTableRow('uploadtree',array('uploadtree_pk'=>201, 'upload_fk'=>$uploadId, 'lft'=>1, 'ufile_name'=>'A.zip','pfile_fk'=>31415));
195  $this->testDb->getDbManager()->insertTableRow('foldercontents',array('foldercontents_pk'=>1, 'parent_fk'=>$folderId, 'foldercontents_mode'=>2, 'child_id'=>$uploadId));
196  $params = array($folderId);
197  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
198  $view = $uploadBrowseProxy->getFolderPartialQuery($params);
199  $row = $this->testDb->getDbManager()->getSingleRow("SELECT count(*) FROM $view", $params);
200  assertThat($row['count'],equalTo(1));
201  }
202 
203  public function testGetFolderPartialQueryWithUserInTwoGoodGroups()
204  {
205  $this->testDb->createPlainTables(array('foldercontents','uploadtree'));
206  $folderId = 701;
207  $uploadId = 1;
208  $otherGroupId = $this->groupId+1;
209  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>1));
210  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$otherGroupId, UploadBrowseProxy::PRIO_COLUMN=>1));
211  $this->testDb->getDbManager()->insertTableRow('uploadtree',array('uploadtree_pk'=>201, 'upload_fk'=>$uploadId, 'lft'=>1, 'ufile_name'=>'A.zip','pfile_fk'=>31415));
212  $this->testDb->getDbManager()->insertTableRow('foldercontents',array('foldercontents_pk'=>1, 'parent_fk'=>$folderId, 'foldercontents_mode'=>2, 'child_id'=>$uploadId));
213  $params = array($folderId);
214  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
215  $view = $uploadBrowseProxy->getFolderPartialQuery($params);
216  $row = $this->testDb->getDbManager()->getSingleRow("SELECT count(*) FROM $view", $params);
217  assertThat($row['count'],equalTo(1));
218  }
219 
220 
225  {
226  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
227  $params = array();
228  $uploadBrowseProxy->getFolderPartialQuery($params);
229  }
230 
231  public function testGetStatus()
232  {
233  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
234  $uploadId = 1;
235  assertThat($uploadBrowseProxy->getStatus($uploadId), equalTo(UploadStatus::OPEN));
236  $uploadBrowseProxy->updateTable('status_fk', $uploadId, $newStatus=UploadStatus::IN_PROGRESS);
237  assertThat($uploadBrowseProxy->getStatus($uploadId), equalTo($newStatus));
238  }
239 
244  public function testGetStatusException()
245  {
246  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager(), false);
247  $uploadBrowseProxy->getStatus(-1);
248  }
249 }