Merge pull request #19552 from kamil-tekiela/Fix-default-values
[phpmyadmin.git] / tests / unit / UserPrivilegesFactoryTest.php
blob9f57190c225fcae83ca51ef9b97d396fc9cbe752
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\ShowGrants;
8 use PhpMyAdmin\UserPrivileges;
9 use PhpMyAdmin\UserPrivilegesFactory;
10 use PhpMyAdmin\Utils\SessionCache;
11 use PHPUnit\Framework\Attributes\CoversClass;
13 #[CoversClass(UserPrivilegesFactory::class)]
14 #[CoversClass(UserPrivileges::class)]
15 #[CoversClass(ShowGrants::class)]
16 final class UserPrivilegesFactoryTest extends AbstractTestCase
18 public function testCheckRequiredPrivilegesForAdjust(): void
20 $userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface());
22 // TEST CASE 1
23 $userPrivileges = new UserPrivileges();
24 $showGrants = new ShowGrants('GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'localhost\' WITH GRANT OPTION');
26 // call the to-be-tested function
27 $userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
29 self::assertTrue($userPrivileges->column);
30 self::assertTrue($userPrivileges->database);
31 self::assertTrue($userPrivileges->routines);
32 self::assertTrue($userPrivileges->table);
34 // TEST CASE 2
35 $userPrivileges = new UserPrivileges();
36 $showGrants = new ShowGrants('GRANT ALL PRIVILEGES ON `mysql`.* TO \'root\'@\'localhost\' WITH GRANT OPTION');
38 // call the to-be-tested function
39 $userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
41 self::assertTrue($userPrivileges->column);
42 self::assertTrue($userPrivileges->database);
43 self::assertTrue($userPrivileges->routines);
44 self::assertTrue($userPrivileges->table);
46 // TEST CASE 3
47 $userPrivileges = new UserPrivileges();
48 $showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO \'root\'@\'localhost\'');
50 // call the to-be-tested function
51 $userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
53 self::assertTrue($userPrivileges->column);
54 self::assertTrue($userPrivileges->database);
55 self::assertTrue($userPrivileges->routines);
56 self::assertTrue($userPrivileges->table);
58 // TEST CASE 4
59 $userPrivileges = new UserPrivileges();
60 $showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`db` TO \'root\'@\'localhost\'');
62 // call the to-be-tested function
63 $userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
65 self::assertFalse($userPrivileges->column);
66 self::assertTrue($userPrivileges->database);
67 self::assertFalse($userPrivileges->routines);
68 self::assertFalse($userPrivileges->table);
71 public function testGetPrivilegesWithSkipGrantTables(): void
73 SessionCache::set('mysql_cur_user', '@');
74 $userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface());
75 $expected = new UserPrivileges(
76 database: true,
77 table: true,
78 column: true,
79 routines: true,
80 isReload: true,
81 isCreateDatabase: true,
83 self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
86 public function testGetPrivilegesFromSessionCache(): void
88 SessionCache::set('mysql_cur_user', 'test_user@localhost');
90 SessionCache::set('is_create_db_priv', true);
91 SessionCache::set('is_reload_priv', true);
92 SessionCache::set('db_to_create', 'databaseToCreate');
93 SessionCache::set('dbs_to_test', ['databasesToTest']);
94 SessionCache::set('proc_priv', true);
95 SessionCache::set('table_priv', true);
96 SessionCache::set('col_priv', true);
97 SessionCache::set('db_priv', true);
99 $userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface());
100 $expected = new UserPrivileges(true, true, true, true, true, true, 'databaseToCreate', ['databasesToTest']);
101 self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
104 public function testGetPrivilegesWithoutShowGrantsResult(): void
106 $dbiDummy = $this->createDbiDummy();
107 $dbiDummy->addResult('SHOW GRANTS', false);
109 SessionCache::set('mysql_cur_user', 'test_user@localhost');
110 $userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface($dbiDummy));
111 $expected = new UserPrivileges(databasesToTest: ['information_schema', 'performance_schema', 'mysql', 'sys']);
112 self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
114 $dbiDummy->assertAllQueriesConsumed();
117 public function testGetPrivilegesWithoutGrants(): void
119 $dbiDummy = $this->createDbiDummy();
120 $dbiDummy->addResult('SHOW GRANTS', []);
122 SessionCache::set('mysql_cur_user', 'test_user@localhost');
123 $userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface($dbiDummy));
124 $expected = new UserPrivileges(databasesToTest: ['information_schema', 'performance_schema', 'mysql', 'sys']);
125 self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
127 $dbiDummy->assertAllQueriesConsumed();
130 public function testGetPrivilegesWithAllPrivileges(): void
132 $dbiDummy = $this->createDbiDummy();
133 $dbiDummy->addResult(
134 'SHOW GRANTS',
135 [['GRANT ALL PRIVILEGES ON *.* TO \'test_user\'@\'localhost\' WITH GRANT OPTION']],
138 SessionCache::set('mysql_cur_user', 'test_user@localhost');
139 $userPrivilegesFactory = new UserPrivilegesFactory($this->createDatabaseInterface($dbiDummy));
140 $expected = new UserPrivileges(true, true, true, true, true, true);
141 self::assertEquals($expected, $userPrivilegesFactory->getPrivileges());
143 $dbiDummy->assertAllQueriesConsumed();