Skip to content

Commit

Permalink
Add some unit tests for UserPrivilegesFactory class
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Feb 24, 2024
1 parent cc62444 commit 13b3210
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 30 deletions.
6 changes: 0 additions & 6 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15043,12 +15043,6 @@
<code><![CDATA[$_SESSION['userconfig']['ts']]]></code>
</MixedArrayAccess>
</file>
<file src="tests/unit/UserPrivilegesFactoryTest.php">
<DeprecatedMethod>
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[DatabaseInterface::getInstance()]]></code>
</DeprecatedMethod>
</file>
<file src="tests/unit/UtilTest.php">
<ArgumentTypeCoercion>
<code><![CDATA[$tz]]></code>
Expand Down
108 changes: 84 additions & 24 deletions tests/unit/UserPrivilegesFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,27 @@

namespace PhpMyAdmin\Tests;

use PhpMyAdmin\Config;
use PhpMyAdmin\DatabaseInterface;
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
{
private UserPrivilegesFactory $userPrivilegesFactory;

/**
* prepares environment for tests
*/
protected function setUp(): void
{
parent::setUp();

DatabaseInterface::$instance = $this->createDatabaseInterface();
Config::getInstance()->selectedServer['DisableIS'] = false;

$this->userPrivilegesFactory = new UserPrivilegesFactory(DatabaseInterface::getInstance());
}

/**
* Test for checkRequiredPrivilegesForAdjust
*/
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
$this->userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
$userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);

self::assertTrue($userPrivileges->column);
self::assertTrue($userPrivileges->database);
Expand All @@ -51,7 +36,7 @@ public function testCheckRequiredPrivilegesForAdjust(): void
$showGrants = new ShowGrants('GRANT ALL PRIVILEGES ON `mysql`.* TO \'root\'@\'localhost\' WITH GRANT OPTION');

// call the to-be-tested function
$this->userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
$userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);

self::assertTrue($userPrivileges->column);
self::assertTrue($userPrivileges->database);
Expand All @@ -63,7 +48,7 @@ public function testCheckRequiredPrivilegesForAdjust(): void
$showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.* TO \'root\'@\'localhost\'');

// call the to-be-tested function
$this->userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
$userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);

self::assertTrue($userPrivileges->column);
self::assertTrue($userPrivileges->database);
Expand All @@ -75,11 +60,86 @@ public function testCheckRequiredPrivilegesForAdjust(): void
$showGrants = new ShowGrants('GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`db` TO \'root\'@\'localhost\'');

// call the to-be-tested function
$this->userPrivilegesFactory->checkRequiredPrivilegesForAdjust($userPrivileges, $showGrants);
$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();
}
}

0 comments on commit 13b3210

Please sign in to comment.