FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UserDaoTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, 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 Monolog\Logger;
25 
26 class UserDaoTest extends \PHPUnit\Framework\TestCase
27 {
29  private $testDb;
31  private $dbManager;
33  private $logger;
35  private $userDao;
37  private $assertCountBefore;
38 
39  protected function setUp()
40  {
41  $this->testDb = new TestLiteDb();
42  $this->dbManager = $this->testDb->getDbManager();
43  $this->logger = new Logger("test");
44  $this->userDao = new UserDao($this->dbManager, $this->logger);
45  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
46  }
47 
48  protected function tearDown()
49  {
50  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
51  $this->testDb = null;
52  $this->dbManager = null;
53  }
54 
55  public function testGetUserGroupMap()
56  {
57  $this->testDb->createPlainTables(array('groups','group_user_member'));
58  $this->testDb->insertData(array('groups','group_user_member'));
59 
60  $defaultGroups = $this->userDao->getUserGroupMap($userId=1);
61  assertThat($defaultGroups, equalTo(array(1=>'Default User')));
62  }
63 
64  public function testGetAdminGroupMap()
65  {
66  $this->testDb->createPlainTables(array('groups','group_user_member'));
67  $this->testDb->insertData(array('groups','group_user_member'));
68  defined('PLUGIN_DB_ADMIN') or define('PLUGIN_DB_ADMIN',10);
69 
70  $defaultGroups = $this->userDao->getAdminGroupMap($userId=2,$userLevel=PLUGIN_DB_ADMIN);
71  assertThat($defaultGroups, equalTo(array(1=>'Default User',2=>'fossy')));
72  }
73 
74 
75  public function testGetDeletableAdminGroupMap()
76  {
77  $this->testDb->createPlainTables(array('groups','group_user_member','users'));
78  $username = 'testi';
79  $userId = 101;
80  $this->dbManager->insertTableRow('users',array('user_pk'=>$userId,'user_name'=>$username));
81  $this->dbManager->insertTableRow('groups', array('group_pk'=>201,'group_name'=>$username));
82  $this->dbManager->insertTableRow('group_user_member', array('group_fk'=>201,'user_fk'=>$userId));
83  $deletable = array('group_pk'=>202,'group_name'=>'anyName');
84  $this->dbManager->insertTableRow('groups', $deletable);
85  $this->dbManager->insertTableRow('group_user_member', array('group_fk'=>202,'user_fk'=>$userId,'group_perm'=>1));
86 
87  $groupsAsAdmin = $this->userDao->getDeletableAdminGroupMap($userId,$userLevel=PLUGIN_DB_ADMIN);
88  assertThat($groupsAsAdmin, equalTo(array($deletable['group_pk']=>$deletable['group_name'])));
89 
90  $groups = $this->userDao->getDeletableAdminGroupMap($userId);
91  assertThat($groups, equalTo(array($deletable['group_pk']=>$deletable['group_name'])));
92 
93  $groupsAsForeign = $this->userDao->getDeletableAdminGroupMap($userId+1);
94  assertThat($groupsAsForeign, equalTo(array()));
95  }
96 
97  public function testAddGroup()
98  {
99  $this->dbManager->queryOnce('CREATE TABLE groups (group_pk integer NOT NULL PRIMARY KEY, group_name varchar(64))');
100  $this->testDb->insertData(array('groups'));
101  $groupId = $this->userDao->addGroup($groupName='newGroup');
102  $row = $this->dbManager->getSingleRow('SELECT group_name FROM groups WHERE group_pk=$1',array($groupId));
103  assertThat($row['group_name'], equalTo($groupName));
104  }
105 
110  {
111  $this->testDb->createPlainTables(array('groups','users'));
112  $this->testDb->insertData(array('groups','user'));
113  $this->userDao->addGroup('fossy');
114  }
115 
119  public function testAddGroupFailEmptyName()
120  {
121  $this->testDb->createPlainTables(array('groups','users'));
122  $this->testDb->insertData(array('groups','user'));
123  $this->userDao->addGroup('');
124  }
125 
126  public function testGetUserName()
127  {
128  $username = 'testi';
129  $userId = 101;
130  $this->testDb->createPlainTables(array('users'));
131  $this->dbManager->insertTableRow('users',array('user_pk'=>$userId,'user_name'=>$username));
132  $uName = $this->userDao->getUserName($userId);
133  assertThat($uName,equalTo($username));
134  }
135 
140  public function testGetUserNameFail()
141  {
142  $this->testDb->createPlainTables(array('users'));
143  $this->userDao->getUserName(101);
144  }
145 
146  public function testGetGroupIdByName()
147  {
148  $this->testDb->createPlainTables(array('groups'));
149  $this->testDb->insertData(array('groups'));
150  $groupId = $this->userDao->getGroupIdByName('fossy');
151  assertThat($groupId,equalTo(2));
152  }
153 
154  public function testAddGroupMembership()
155  {
156  $this->testDb->createPlainTables(array('users','groups','group_user_member'));
157  $this->testDb->insertData(array('users','groups','group_user_member'));
158  $this->userDao->addGroupMembership($groupId=2,$userId=1);
159  $map = $this->userDao->getUserGroupMap($userId);
160  assertThat($map,hasKey($groupId));
161  }
162 }
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28