FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadBrowseProxyTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Proxy;
9 
10 use Exception;
15 
16 class UploadBrowseProxyTest extends \PHPUnit\Framework\TestCase
17 {
18  private $testDb;
19  private $groupId = 401;
20 
21  protected function setUp() : void
22  {
23  $this->testDb = new TestPgDb();
24  $this->testDb->createPlainTables( array('upload','upload_clearing','perm_upload','upload_events') );
25  $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));
26  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>2,'upload_filename'=>'for.this','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_NONE));
27  $this->testDb->getDbManager()->insertTableRow('perm_upload', array('perm_upload_pk'=>1, 'upload_fk'=>2,'group_fk'=>$this->groupId,'perm'=>Auth::PERM_READ));
28  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>3,'upload_filename'=>'for.noone','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_NONE));
29  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
30  }
31 
32  protected function tearDown() : void
33  {
34  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
35  $this->testDb = null;
36  }
37 
38  public function testConstructAndSanity()
39  {
40  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager(), true);
41  assertThat($uploadBrowseProxy,is(anInstanceOf(UploadBrowseProxy::class)));
42  }
43 
44  public function testUpdateTableStatus()
45  {
46  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::OPEN));
47  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
48  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::IN_PROGRESS);
49  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
50  assertThat($updatedRow['status_fk'],equalTo($newStatus));
51  }
52 
53  public function testUpdateTableStatusFromRejectByUser()
54  {
55  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::REJECTED));
56  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
57  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::IN_PROGRESS);
58  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
59  assertThat($updatedRow['status_fk'],equalTo(UploadStatus::REJECTED));
60  }
61 
62  public function testUpdateTableStatusByAdvisor()
63  {
64  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::OPEN));
65  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::ADVISOR, $this->testDb->getDbManager());
66  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::IN_PROGRESS);
67  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
68  assertThat($updatedRow['status_fk'],equalTo($newStatus));
69  }
70 
71  public function testUpdateTableStatusToRejectByUser()
72  {
73  $this->expectException(Exception::class);
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::USER, $this->testDb->getDbManager());
76  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::REJECTED);
77  }
78 
79  public function testUpdateTableStatusToRejectByAdvisor()
80  {
81  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, 'status_fk'=> UploadStatus::OPEN));
82  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::ADVISOR, $this->testDb->getDbManager());
83  $uploadBrowseProxy->updateTable('status_fk', $uploadId=1, $newStatus=UploadStatus::REJECTED);
84  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
85  assertThat($updatedRow['status_fk'],equalTo($newStatus));
86  }
87 
88  public function testUpdateTableNonEditableColum()
89  {
90  $this->expectException(Exception::class);
91  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
92  $uploadBrowseProxy->updateTable('nonEditableColumn', 1, 123);
93  }
94 
95  public function testUpdateTableAssigneeByAdvisor()
96  {
97  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::ADVISOR, $this->testDb->getDbManager());
98  $uploadBrowseProxy->updateTable('assignee', $uploadId=1, $newAssignee=123);
99  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT assignee FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($uploadId,$this->groupId));
100  assertThat($updatedRow['assignee'],equalTo($newAssignee));
101  }
102 
103  public function testUpdateTableAssigneeForbidden()
104  {
105  $this->expectException(Exception::class);
106  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
107  $uploadBrowseProxy->updateTable('assignee', 1, 123);
108  }
109 
110 
111  private function wrapperTestMoveUploadToInfinity($uploadId, $order='DESC')
112  {
113  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>1));
114  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>2,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>2));
115 
116  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
117  $uploadBrowseProxy->moveUploadToInfinity($uploadId, 'DESC'==$order);
118 
119  $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));
120  assertThat($updatedRow['upload_fk'],equalTo($uploadId));
121  }
122 
123  public function testMoveUploadToInfinityTop()
124  {
125  $this->wrapperTestMoveUploadToInfinity(1, 'DESC');
126  }
127 
128  public function testMoveUploadToInfinityDown()
129  {
130  $this->wrapperTestMoveUploadToInfinity(2, 'ASC');
131  }
132 
133 
134  private function wrapperTestMoveUploadBeyond($moveUpload=4, $beyondUpload=2, $expectedPrio = 1.5)
135  {
136  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>4,'upload_filename'=>'for.all4','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_READ));
137  $this->testDb->getDbManager()->insertTableRow('upload', array('upload_pk'=>5,'upload_filename'=>'for.all5','user_fk'=>1,'upload_mode'=>1,'public_perm'=>Auth::PERM_READ));
138 
139  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>1));
140  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>2,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>2));
141  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>4,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>4));
142  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>5,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>5));
143 
144  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
145  $uploadBrowseProxy->moveUploadBeyond($moveUpload, $beyondUpload);
146  $updatedRow = $this->testDb->getDbManager()->getSingleRow('SELECT '.UploadBrowseProxy::PRIO_COLUMN.' FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2',array($moveUpload,$this->groupId));
147  assertThat($updatedRow[UploadBrowseProxy::PRIO_COLUMN],equalTo($expectedPrio));
148 
149  }
150 
151  public function testMoveUploadBeyondDown()
152  {
153  $this->wrapperTestMoveUploadBeyond(4,2,1.5);
154  }
155 
156  public function testMoveUploadBeyondUp()
157  {
158  $this->wrapperTestMoveUploadBeyond(2,4,4.5);
159  }
160 
161  public function testMoveUploadBeyondFarDown()
162  {
163  $this->wrapperTestMoveUploadBeyond(4,1,0.5);
164  }
165 
166  public function testMoveUploadBeyondFarUp()
167  {
168  $this->wrapperTestMoveUploadBeyond(4,5,5.5);
169  }
170 
171 
172  public function testGetFolderPartialQuery()
173  {
174  $this->testDb->createPlainTables(array('foldercontents','uploadtree'));
175  $folderId = 701;
176  $uploadId = 1;
177  $this->testDb->getDbManager()->insertTableRow('upload_clearing', array('upload_fk'=>1,'group_fk'=>$this->groupId, UploadBrowseProxy::PRIO_COLUMN=>1));
178  $this->testDb->getDbManager()->insertTableRow('uploadtree',array('uploadtree_pk'=>201, 'upload_fk'=>$uploadId, 'lft'=>1, 'ufile_name'=>'A.zip','pfile_fk'=>31415));
179  $this->testDb->getDbManager()->insertTableRow('foldercontents',array('foldercontents_pk'=>1, 'parent_fk'=>$folderId, 'foldercontents_mode'=>2, 'child_id'=>$uploadId));
180  $params = array($folderId);
181  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
182  $view = $uploadBrowseProxy->getFolderPartialQuery($params);
183  $row = $this->testDb->getDbManager()->getSingleRow("SELECT count(*) FROM $view", $params);
184  assertThat($row['count'],equalTo(1));
185  }
186 
187  public function testGetFolderPartialQueryWithUserInTwoGoodGroups()
188  {
189  $this->testDb->createPlainTables(array('foldercontents','uploadtree'));
190  $folderId = 701;
191  $uploadId = 1;
192  $otherGroupId = $this->groupId+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('upload_clearing', array('upload_fk'=>1,'group_fk'=>$otherGroupId, UploadBrowseProxy::PRIO_COLUMN=>1));
195  $this->testDb->getDbManager()->insertTableRow('uploadtree',array('uploadtree_pk'=>201, 'upload_fk'=>$uploadId, 'lft'=>1, 'ufile_name'=>'A.zip','pfile_fk'=>31415));
196  $this->testDb->getDbManager()->insertTableRow('foldercontents',array('foldercontents_pk'=>1, 'parent_fk'=>$folderId, 'foldercontents_mode'=>2, 'child_id'=>$uploadId));
197  $params = array($folderId);
198  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
199  $view = $uploadBrowseProxy->getFolderPartialQuery($params);
200  $row = $this->testDb->getDbManager()->getSingleRow("SELECT count(*) FROM $view", $params);
201  assertThat($row['count'],equalTo(1));
202  }
203 
204 
205  public function testGetFolderPartialQueryWithInvalidParamCount()
206  {
207  $this->expectException(Exception::class);
208  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
209  $params = array();
210  $uploadBrowseProxy->getFolderPartialQuery($params);
211  }
212 
213  public function testGetStatus()
214  {
215  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager());
216  $uploadId = 1;
217  assertThat($uploadBrowseProxy->getStatus($uploadId), equalTo(UploadStatus::OPEN));
218  $uploadBrowseProxy->updateTable('status_fk', $uploadId, $newStatus=UploadStatus::IN_PROGRESS);
219  assertThat($uploadBrowseProxy->getStatus($uploadId), equalTo($newStatus));
220  }
221 
222  public function testGetStatusException()
223  {
224  $this->expectException(Exception::class);
225  $uploadBrowseProxy = new UploadBrowseProxy($this->groupId, UserDao::USER, $this->testDb->getDbManager(), false);
226  $uploadBrowseProxy->getStatus(-1);
227  }
228 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24