-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathUserPrivilegesFactoryTest.php
More file actions
145 lines (116 loc) · 5.82 KB
/
Copy pathUserPrivilegesFactoryTest.php
File metadata and controls
145 lines (116 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests;
use PhpMyAdmin\ShowGrants;
use PhpMyAdmin\UserPrivileges;
use PhpMyAdmin\UserPrivilegesFactory;
use PhpMyAdmin\Utils\SessionCache;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(UserPrivilegesFactory::class)]
#[CoversClass(UserPrivileges::class)]
#[CoversClass(ShowGrants::class)]
final class UserPrivilegesFactoryTest extends AbstractTestCase
{
public function testCheckRequiredPrivilegesForAdjust(): void
{
$userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface());
// TEST CASE 1
$userPrivileges = new UserPrivileges();
$showGrants = new ShowGrants('GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'localhost\' WITH GRANT OPTION');
// call the to-be-tested function
$userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
self::assertTrue($userPrivileges->column);
self::assertTrue($userPrivileges->database);
self::assertTrue($userPrivileges->routines);
self::assertTrue($userPrivileges->table);
// TEST CASE 2
$userPrivileges = new UserPrivileges();
$showGrants = new ShowGrants('GRANT ALL PRIVILEGES ON `mysql`.* TO \'root\'@\'localhost\' WITH GRANT OPTION');
// call the to-be-tested function
$userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
self::assertTrue($userPrivileges->column);
self::assertTrue($userPrivileges->database);
self::assertTrue($userPrivileges->routines);
self::assertTrue($userPrivileges->table);
// TEST CASE 3
$userPrivileges = new UserPrivileges();
$showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO \'root\'@\'localhost\'');
// call the to-be-tested function
$userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
self::assertTrue($userPrivileges->column);
self::assertTrue($userPrivileges->database);
self::assertTrue($userPrivileges->routines);
self::assertTrue($userPrivileges->table);
// TEST CASE 4
$userPrivileges = new UserPrivileges();
$showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`db` TO \'root\'@\'localhost\'');
// call the to-be-tested function
$userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
self::assertFalse($userPrivileges->column);
self::assertTrue($userPrivileges->database);
self::assertFalse($userPrivileges->routines);
self::assertFalse($userPrivileges->table);
}
public function testGetPrivilegesWithSkipGrantTables(): void
{
SessionCache::set('mysql_cur_user', '@');
$userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface());
$expected = new UserPrivileges(
database: true,
table: true,
column: true,
routines: true,
isReload: true,
isCreateDatabase: true,
);
self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
}
public function testGetPrivilegesFromSessionCache(): void
{
SessionCache::set('mysql_cur_user', 'test_user@localhost');
SessionCache::set('is_create_db_priv', true);
SessionCache::set('is_reload_priv', true);
SessionCache::set('db_to_create', 'databaseToCreate');
SessionCache::set('dbs_to_test', ['databasesToTest']);
SessionCache::set('proc_priv', true);
SessionCache::set('table_priv', true);
SessionCache::set('col_priv', true);
SessionCache::set('db_priv', true);
$userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface());
$expected = new UserPrivileges(true, true, true, true, true, true, 'databaseToCreate', ['databasesToTest']);
self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
}
public function testGetPrivilegesWithoutShowGrantsResult(): void
{
$dbiDummy = $this->createDbiDummy();
$dbiDummy->addResult('SHOW GRANTS', false);
SessionCache::set('mysql_cur_user', 'test_user@localhost');
$userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface($dbiDummy));
$expected = new UserPrivileges(databasesToTest: ['information_schema', 'performance_schema', 'mysql', 'sys']);
self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
$dbiDummy->assertAllQueriesConsumed();
}
public function testGetPrivilegesWithoutGrants(): void
{
$dbiDummy = $this->createDbiDummy();
$dbiDummy->addResult('SHOW GRANTS', []);
SessionCache::set('mysql_cur_user', 'test_user@localhost');
$userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface($dbiDummy));
$expected = new UserPrivileges(databasesToTest: ['information_schema', 'performance_schema', 'mysql', 'sys']);
self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
$dbiDummy->assertAllQueriesConsumed();
}
public function testGetPrivilegesWithAllPrivileges(): void
{
$dbiDummy = $this->createDbiDummy();
$dbiDummy->addResult(
'SHOW GRANTS',
[['GRANT ALL PRIVILEGES ON *.* TO \'test_user\'@\'localhost\' WITH GRANT OPTION']],
);
SessionCache::set('mysql_cur_user', 'test_user@localhost');
$userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface($dbiDummy));
$expected = new UserPrivileges(true, true, true, true, true, true);
self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
$dbiDummy->assertAllQueriesConsumed();
}
}