Extract privileges globals to static props of UserPrivileges class
[phpmyadmin.git] / tests / classes / CheckUserPrivilegesTest.php
blob46d3544d3c91ccca16a6efebec41f1dc654e3788
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\CheckUserPrivileges;
8 use PhpMyAdmin\Config;
9 use PhpMyAdmin\DatabaseInterface;
10 use PhpMyAdmin\ShowGrants;
11 use PhpMyAdmin\UserPrivileges;
12 use PHPUnit\Framework\Attributes\CoversClass;
14 #[CoversClass(CheckUserPrivileges::class)]
15 class CheckUserPrivilegesTest extends AbstractTestCase
17 private CheckUserPrivileges $checkUserPrivileges;
19 /**
20 * prepares environment for tests
22 protected function setUp(): void
24 parent::setUp();
26 DatabaseInterface::$instance = $this->createDatabaseInterface();
27 Config::getInstance()->selectedServer['DisableIS'] = false;
28 UserPrivileges::$column = false;
29 UserPrivileges::$database = false;
30 UserPrivileges::$routines = false;
31 UserPrivileges::$table = false;
32 UserPrivileges::$isReload = false;
34 $this->checkUserPrivileges = new CheckUserPrivileges(DatabaseInterface::getInstance());
37 /**
38 * Test for checkRequiredPrivilegesForAdjust
40 public function testCheckRequiredPrivilegesForAdjust(): void
42 // TEST CASE 1
43 $showGrants = new ShowGrants('GRANT ALL PRIVILEGES ON *.* TO \'root\'@\'localhost\' WITH GRANT OPTION');
45 // call the to-be-tested function
46 $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants);
48 $this->assertTrue(UserPrivileges::$column);
49 $this->assertTrue(UserPrivileges::$database);
50 $this->assertTrue(UserPrivileges::$routines);
51 $this->assertTrue(UserPrivileges::$table);
53 // re-initialise the privileges
54 $this->setUp();
56 // TEST CASE 2
57 $showGrants = new ShowGrants('GRANT ALL PRIVILEGES ON `mysql`.* TO \'root\'@\'localhost\' WITH GRANT OPTION');
59 // call the to-be-tested function
60 $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants);
62 $this->assertTrue(UserPrivileges::$column);
63 $this->assertTrue(UserPrivileges::$database);
64 $this->assertTrue(UserPrivileges::$routines);
65 $this->assertTrue(UserPrivileges::$table);
67 // re-initialise the privileges
68 $this->setUp();
70 // TEST CASE 3
71 $showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO \'root\'@\'localhost\'');
73 // call the to-be-tested function
74 $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants);
76 $this->assertTrue(UserPrivileges::$column);
77 $this->assertTrue(UserPrivileges::$database);
78 $this->assertTrue(UserPrivileges::$routines);
79 $this->assertTrue(UserPrivileges::$table);
81 // re-initialise the privileges
82 $this->setUp();
84 // TEST CASE 4
85 $showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`db` TO \'root\'@\'localhost\'');
87 // call the to-be-tested function
88 $this->checkUserPrivileges->checkRequiredPrivilegesForAdjust($showGrants);
90 $this->assertFalse(UserPrivileges::$column);
91 $this->assertTrue(UserPrivileges::$database);
92 $this->assertFalse(UserPrivileges::$routines);
93 $this->assertFalse(UserPrivileges::$table);