From 13b32109a1d2e8115e7f5327725344694db0796b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Fri, 23 Feb 2024 23:41:01 -0300 Subject: [PATCH 1/2] Add some unit tests for UserPrivilegesFactory class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- psalm-baseline.xml | 6 -- tests/unit/UserPrivilegesFactoryTest.php | 108 ++++++++++++++++++----- 2 files changed, 84 insertions(+), 30 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 79bed2b81f..32d307c24a 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -15043,12 +15043,6 @@ - - - - - - diff --git a/tests/unit/UserPrivilegesFactoryTest.php b/tests/unit/UserPrivilegesFactoryTest.php index 686b693c72..9f57190c22 100644 --- a/tests/unit/UserPrivilegesFactoryTest.php +++ b/tests/unit/UserPrivilegesFactoryTest.php @@ -4,42 +4,27 @@ declare(strict_types=1); 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); @@ -51,7 +36,7 @@ final class UserPrivilegesFactoryTest extends AbstractTestCase $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); @@ -63,7 +48,7 @@ final class UserPrivilegesFactoryTest extends AbstractTestCase $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); @@ -75,11 +60,86 @@ final class UserPrivilegesFactoryTest extends AbstractTestCase $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(); + } } From 02b6e0f98813b12f344fc141567ffa1cc41a436c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 24 Feb 2024 14:01:11 -0300 Subject: [PATCH 2/2] Add unit test for DatabaseInterface::getColumn() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- tests/unit/DatabaseInterfaceTest.php | 50 ++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/unit/DatabaseInterfaceTest.php b/tests/unit/DatabaseInterfaceTest.php index c30a31f917..94c8798880 100644 --- a/tests/unit/DatabaseInterfaceTest.php +++ b/tests/unit/DatabaseInterfaceTest.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace PhpMyAdmin\Tests; +use PhpMyAdmin\Column; +use PhpMyAdmin\ColumnFull; use PhpMyAdmin\Config; use PhpMyAdmin\Config\Settings\Server; use PhpMyAdmin\Current; @@ -12,6 +14,7 @@ use PhpMyAdmin\Dbal\ConnectionType; use PhpMyAdmin\Dbal\DbiExtension; use PhpMyAdmin\Dbal\ResultInterface; use PhpMyAdmin\Dbal\Statement; +use PhpMyAdmin\Index; use PhpMyAdmin\LanguageManager; use PhpMyAdmin\Query\Utilities; use PhpMyAdmin\SqlParser\Context; @@ -24,6 +27,8 @@ use ReflectionProperty; use function array_keys; #[CoversClass(DatabaseInterface::class)] +#[CoversClass(Column::class)] +#[CoversClass(ColumnFull::class)] class DatabaseInterfaceTest extends AbstractTestCase { protected function setUp(): void @@ -792,4 +797,49 @@ class DatabaseInterfaceTest extends AbstractTestCase yield 'null' => [null, 0]; yield 'false' => [false, 0]; } + + public function testGetColumn(): void + { + (new ReflectionProperty(Index::class, 'registry'))->setValue(null, []); + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->removeDefaultResults(); + $dbiDummy->addResult( + 'SHOW COLUMNS FROM `test_db`.`test_table` LIKE \'test\\\\_column\'', + [['test_column', 'varchar(45)', 'NO', '', null, '']], + ['Field', 'Type', 'Null', 'Key', 'Default', 'Extra'], + ); + $dbiDummy->addResult('SHOW INDEXES FROM `test_db`.`test_table`', []); + $dbi = $this->createDatabaseInterface($dbiDummy); + $column = new Column('test_column', 'varchar(45)', false, '', null, ''); + self::assertEquals($column, $dbi->getColumn('test_db', 'test_table', 'test_column')); + $dbiDummy->assertAllQueriesConsumed(); + } + + public function testGetColumnWithFullColumn(): void + { + (new ReflectionProperty(Index::class, 'registry'))->setValue(null, []); + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->removeDefaultResults(); + $dbiDummy->addResult( + 'SHOW FULL COLUMNS FROM `test_db`.`test_table` LIKE \'test\\\\_column\'', + // phpcs:ignore Generic.Files.LineLength.TooLong + [['test_column', 'varchar(45)', 'utf8mb4_general_ci', 'NO', '', null, '', 'select,insert,update,references', '']], + ['Field', 'Type', 'Collation', 'Null', 'Key', 'Default', 'Extra', 'Privileges', 'Comment'], + ); + $dbiDummy->addResult('SHOW INDEXES FROM `test_db`.`test_table`', []); + $dbi = $this->createDatabaseInterface($dbiDummy); + $column = new ColumnFull( + 'test_column', + 'varchar(45)', + 'utf8mb4_general_ci', + false, + '', + null, + '', + 'select,insert,update,references', + '', + ); + self::assertEquals($column, $dbi->getColumn('test_db', 'test_table', 'test_column', true)); + $dbiDummy->assertAllQueriesConsumed(); + } }