FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UserDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  Author: Steffen Weber
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
11 use Exception;
14 use Monolog\Logger;
15 
16 class UserDaoTest extends \PHPUnit\Framework\TestCase
17 {
19  private $testDb;
21  private $dbManager;
23  private $logger;
25  private $userDao;
27  private $assertCountBefore;
28 
29  protected function setUp() : void
30  {
31  $this->testDb = new TestLiteDb();
32  $this->dbManager = $this->testDb->getDbManager();
33  $this->logger = new Logger("test");
34  $this->userDao = new UserDao($this->dbManager, $this->logger);
35  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
36  }
37 
38  protected function tearDown() : void
39  {
40  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
41  $this->testDb = null;
42  $this->dbManager = null;
43  }
44 
45  public function testGetUserGroupMap()
46  {
47  $this->testDb->createPlainTables(array('groups','group_user_member'));
48  $this->testDb->insertData(array('groups','group_user_member'));
49 
50  $defaultGroups = $this->userDao->getUserGroupMap($userId=1);
51  assertThat($defaultGroups, equalTo(array(1=>'Default User')));
52  }
53 
54  public function testGetAdminGroupMap()
55  {
56  $this->testDb->createPlainTables(array('groups','group_user_member'));
57  $this->testDb->insertData(array('groups','group_user_member'));
58  defined('PLUGIN_DB_ADMIN') or define('PLUGIN_DB_ADMIN',10);
59 
60  $defaultGroups = $this->userDao->getAdminGroupMap($userId=2,$userLevel=PLUGIN_DB_ADMIN);
61  assertThat($defaultGroups, equalTo(array(1=>'Default User',2=>'fossy')));
62  }
63 
64 
65  public function testGetDeletableAdminGroupMap()
66  {
67  $this->testDb->createPlainTables(array('groups','group_user_member','users'));
68  $username = 'testi';
69  $userId = 101;
70  $this->dbManager->insertTableRow('users',array('user_pk'=>$userId,'user_name'=>$username));
71  $this->dbManager->insertTableRow('groups', array('group_pk'=>201,'group_name'=>$username));
72  $this->dbManager->insertTableRow('group_user_member', array('group_fk'=>201,'user_fk'=>$userId));
73  $deletable = array('group_pk'=>202,'group_name'=>'anyName');
74  $this->dbManager->insertTableRow('groups', $deletable);
75  $this->dbManager->insertTableRow('group_user_member', array('group_fk'=>202,'user_fk'=>$userId,'group_perm'=>1));
76 
77  $groupsAsAdmin = $this->userDao->getDeletableAdminGroupMap($userId,$userLevel=PLUGIN_DB_ADMIN);
78  assertThat($groupsAsAdmin, equalTo(array($deletable['group_pk']=>$deletable['group_name'])));
79 
80  $groups = $this->userDao->getDeletableAdminGroupMap($userId);
81  assertThat($groups, equalTo(array($deletable['group_pk']=>$deletable['group_name'])));
82 
83  $groupsAsForeign = $this->userDao->getDeletableAdminGroupMap($userId+1);
84  assertThat($groupsAsForeign, equalTo(array()));
85  }
86 
87  public function testAddGroup()
88  {
89  $this->dbManager->queryOnce('CREATE TABLE groups (group_pk integer NOT NULL PRIMARY KEY, group_name varchar(64))');
90  $this->testDb->insertData(array('groups'));
91  $groupId = $this->userDao->addGroup($groupName='newGroup');
92  $row = $this->dbManager->getSingleRow('SELECT group_name FROM groups WHERE group_pk=$1',array($groupId));
93  assertThat($row['group_name'], equalTo($groupName));
94  }
95 
96  public function testAddGroupFailIfAlreadyExists()
97  {
98  $this->expectException(Exception::class);
99  $this->testDb->createPlainTables(array('groups','users'));
100  $this->testDb->insertData(array('groups','user'));
101  $this->userDao->addGroup('fossy');
102  }
103 
104  public function testAddGroupFailEmptyName()
105  {
106  $this->expectException(Exception::class);
107  $this->testDb->createPlainTables(array('groups','users'));
108  $this->testDb->insertData(array('groups','user'));
109  $this->userDao->addGroup('');
110  }
111 
112  public function testGetUserName()
113  {
114  $username = 'testi';
115  $userId = 101;
116  $this->testDb->createPlainTables(array('users'));
117  $this->dbManager->insertTableRow('users',array('user_pk'=>$userId,'user_name'=>$username));
118  $uName = $this->userDao->getUserName($userId);
119  assertThat($uName,equalTo($username));
120  }
121 
122  public function testGetUserNameFail()
123  {
124  $this->expectException(Exception::class);
125  $this->expectExceptionMessage("unknown user with id=101");
126  $this->testDb->createPlainTables(array('users'));
127  $this->userDao->getUserName(101);
128  }
129 
130  public function testGetGroupIdByName()
131  {
132  $this->testDb->createPlainTables(array('groups'));
133  $this->testDb->insertData(array('groups'));
134  $groupId = $this->userDao->getGroupIdByName('fossy');
135  assertThat($groupId,equalTo(2));
136  }
137 
138  public function testAddGroupMembership()
139  {
140  $this->testDb->createPlainTables(array('users','groups','group_user_member'));
141  $this->testDb->insertData(array('users','groups','group_user_member'));
142  $this->userDao->addGroupMembership($groupId=2,$userId=1);
143  $map = $this->userDao->getUserGroupMap($userId);
144  assertThat($map,hasKey($groupId));
145  }
146 }
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16