Skip to content

Commit 7567aec

Browse files
committed
Merge branch 'QA_5_2'
Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
2 parents 833796a + 796b01c commit 7567aec

8 files changed

Lines changed: 64 additions & 40 deletions

File tree

src/Dbal/DatabaseInterface.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,13 +1445,15 @@ public function isGrantUser(): bool
14451445
return $hasGrantPrivilege;
14461446
}
14471447

1448+
$collation = $this->getServerCollation();
1449+
14481450
[$user, $host] = $this->getCurrentUserAndHost();
1449-
$query = QueryGenerator::getInformationSchemaDataForGranteeRequest($user, $host);
1451+
$query = QueryGenerator::getInformationSchemaDataForGranteeRequest($user, $host, $collation);
14501452
$hasGrantPrivilege = (bool) $this->fetchValue($query);
14511453

14521454
if (! $hasGrantPrivilege) {
14531455
foreach ($this->getCurrentRolesAndHost() as [$role, $roleHost]) {
1454-
$query = QueryGenerator::getInformationSchemaDataForGranteeRequest($role, $roleHost ?? '');
1456+
$query = QueryGenerator::getInformationSchemaDataForGranteeRequest($role, $roleHost ?? '', $collation);
14551457
$hasGrantPrivilege = (bool) $this->fetchValue($query);
14561458

14571459
if ($hasGrantPrivilege) {
@@ -1492,13 +1494,15 @@ public function isCreateUser(): bool
14921494
return $hasCreatePrivilege;
14931495
}
14941496

1497+
$collation = $this->getServerCollation();
1498+
14951499
[$user, $host] = $this->getCurrentUserAndHost();
1496-
$query = QueryGenerator::getInformationSchemaDataForCreateRequest($user, $host);
1500+
$query = QueryGenerator::getInformationSchemaDataForCreateRequest($user, $host, $collation);
14971501
$hasCreatePrivilege = (bool) $this->fetchValue($query);
14981502

14991503
if (! $hasCreatePrivilege) {
15001504
foreach ($this->getCurrentRolesAndHost() as [$role, $roleHost]) {
1501-
$query = QueryGenerator::getInformationSchemaDataForCreateRequest($role, $roleHost ?? '');
1505+
$query = QueryGenerator::getInformationSchemaDataForCreateRequest($role, $roleHost ?? '', $collation);
15021506
$hasCreatePrivilege = (bool) $this->fetchValue($query);
15031507

15041508
if ($hasCreatePrivilege) {

src/Query/Generator.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,27 @@ public static function getCreateTrigger(Trigger $newTrigger, string $delimiter):
286286
);
287287
}
288288

289-
public static function getInformationSchemaDataForCreateRequest(string $user, string $host): string
290-
{
289+
public static function getInformationSchemaDataForCreateRequest(
290+
string $user,
291+
string $host,
292+
string $collation,
293+
): string {
291294
// second part of query is for MariaDB that not show roles inside INFORMATION_SCHEMA db
292295
return 'SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` '
293296
. "WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND "
294297
. "'''" . $user . "''@''" . $host . "''' LIKE `GRANTEE`"
295298
. ' UNION '
296299
. 'SELECT 1 FROM mysql.user '
297-
. "WHERE `create_user_priv` = 'Y' AND "
300+
. "WHERE `create_user_priv` = 'Y' COLLATE " . $collation . ' AND '
298301
. "'" . $user . "' LIKE `User` AND '' LIKE `Host`"
299302
. ' LIMIT 1';
300303
}
301304

302-
public static function getInformationSchemaDataForGranteeRequest(string $user, string $host): string
303-
{
305+
public static function getInformationSchemaDataForGranteeRequest(
306+
string $user,
307+
string $host,
308+
string $collation,
309+
): string {
304310
// second part of query is for MariaDB that not show roles inside INFORMATION_SCHEMA db
305311
return 'SELECT 1 FROM ('
306312
. 'SELECT `GRANTEE`, `IS_GRANTABLE` FROM '
@@ -315,7 +321,7 @@ public static function getInformationSchemaDataForGranteeRequest(string $user, s
315321
. "'''" . $user . "''@''" . $host . "''' LIKE `GRANTEE` "
316322
. ' UNION '
317323
. 'SELECT 1 FROM mysql.user '
318-
. "WHERE `create_user_priv` = 'Y' AND "
324+
. "WHERE `create_user_priv` = 'Y' COLLATE " . $collation . ' AND '
319325
. "'" . $user . "' LIKE `User` AND '' LIKE `Host`"
320326
. ' LIMIT 1';
321327
}

tests/unit/Controllers/Database/PrivilegesControllerTest.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,12 @@ public function testIndex(): void
4242
$config = Config::getInstance();
4343
$config->selectedServer['DisableIS'] = false;
4444

45-
$this->dummyDbi->addResult(
46-
'SHOW TABLES FROM `test_db`;',
47-
[['test_table']],
48-
['Tables_in_test_db'],
49-
);
50-
51-
// phpcs:disable Generic.Files.LineLength.TooLong
52-
$this->dummyDbi->addResult(
53-
'SELECT *, `TABLE_SCHEMA` AS `Db`, `TABLE_NAME` AS `Name`, `TABLE_TYPE` AS `TABLE_TYPE`, `ENGINE` AS `Engine`, `ENGINE` AS `Type`, `VERSION` AS `Version`, `ROW_FORMAT` AS `Row_format`, `TABLE_ROWS` AS `Rows`, `AVG_ROW_LENGTH` AS `Avg_row_length`, `DATA_LENGTH` AS `Data_length`, `MAX_DATA_LENGTH` AS `Max_data_length`, `INDEX_LENGTH` AS `Index_length`, `DATA_FREE` AS `Data_free`, `AUTO_INCREMENT` AS `Auto_increment`, `CREATE_TIME` AS `Create_time`, `UPDATE_TIME` AS `Update_time`, `CHECK_TIME` AS `Check_time`, `TABLE_COLLATION` AS `Collation`, `CHECKSUM` AS `Checksum`, `CREATE_OPTIONS` AS `Create_options`, `TABLE_COMMENT` AS `Comment` FROM `information_schema`.`TABLES` t WHERE `TABLE_SCHEMA` COLLATE utf8_bin IN (\'test_db\') ORDER BY Name ASC LIMIT 250 OFFSET 0',
54-
[['def', 'test_db', 'test_table', 'BASE TABLE', 'InnoDB', '10', 'Dynamic', '3', '5461', '16384', '0', '0', '0', '4', '2011-12-13 14:15:16', null, null, 'utf8mb4_general_ci', null, '', '', '0', 'N', 'test_db', 'test_table', 'BASE TABLE', 'InnoDB', 'InnoDB', '10', 'Dynamic', '3', '5461', '16384', '0', '0', '0', '4', '2011-12-13 14:15:16', null, null, 'utf8mb4_general_ci', null, '', '']],
55-
['TABLE_CATALOG', 'TABLE_SCHEMA', 'TABLE_NAME', 'TABLE_TYPE', 'ENGINE', 'VERSION', 'ROW_FORMAT', 'TABLE_ROWS', 'AVG_ROW_LENGTH', 'DATA_LENGTH', 'MAX_DATA_LENGTH', 'INDEX_LENGTH', 'DATA_FREE', 'AUTO_INCREMENT', 'CREATE_TIME', 'UPDATE_TIME', 'CHECK_TIME', 'TABLE_COLLATION', 'CHECKSUM', 'CREATE_OPTIONS', 'TABLE_COMMENT', 'MAX_INDEX_LENGTH', 'TEMPORARY', 'Db', 'Name', 'TABLE_TYPE', 'Engine', 'Type', 'Version', 'Row_format', 'Rows', 'Avg_row_length', 'Data_length', 'Max_data_length', 'Index_length', 'Data_free', 'Auto_increment', 'Create_time', 'Update_time', 'Check_time', 'Collation', 'Checksum', 'Create_options', 'Comment'],
56-
);
57-
// phpcs:enable
45+
$this->dummyDbi->addResult('SELECT @@collation_server', [['utf8mb4_uca1400_ai_ci']]);
46+
// phpcs:ignore Generic.Files.LineLength.TooLong
47+
$this->dummyDbi->addResult("SELECT 1 FROM (SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`COLUMN_PRIVILEGES` UNION SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`TABLE_PRIVILEGES` UNION SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`SCHEMA_PRIVILEGES` UNION SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES`) t WHERE `IS_GRANTABLE` = 'YES' AND '''pma_test''@''localhost''' LIKE `GRANTEE` UNION SELECT 1 FROM mysql.user WHERE `create_user_priv` = 'Y' COLLATE utf8mb4_uca1400_ai_ci AND 'pma_test' LIKE `User` AND '' LIKE `Host` LIMIT 1", [['1']]);
48+
$this->dummyDbi->addResult('SELECT @@collation_server', [['utf8mb4_uca1400_ai_ci']]);
49+
// phpcs:ignore Generic.Files.LineLength.TooLong
50+
$this->dummyDbi->addResult("SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND '''pma_test''@''localhost''' LIKE `GRANTEE` UNION SELECT 1 FROM mysql.user WHERE `create_user_priv` = 'Y' COLLATE utf8mb4_uca1400_ai_ci AND 'pma_test' LIKE `User` AND '' LIKE `Host` LIMIT 1", [['1']]);
5851

5952
$privileges = [];
6053

@@ -74,6 +67,8 @@ public function testIndex(): void
7467
))($request);
7568
$actual = $response->getHTMLResult();
7669

70+
$this->dummyDbi->assertAllQueriesConsumed();
71+
7772
self::assertStringContainsString(
7873
Url::getCommon(['db' => Current::$database], ''),
7974
$actual,

tests/unit/Controllers/Server/PrivilegesControllerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function testPrivilegesController(): void
6161
[['pma', 'localhost'], ['root', 'localhost']],
6262
['User', 'Host'],
6363
);
64+
$this->dummyDbi->addResult("SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND '''pma_test''@''localhost''' LIKE `GRANTEE` UNION SELECT 1 FROM mysql.user WHERE `create_user_priv` = 'Y' COLLATE utf8_general_ci AND 'pma_test' LIKE `User` AND '' LIKE `Host` LIMIT 1", [['1']]);
6465
// phpcs:enable
6566

6667
$request = self::createStub(ServerRequest::class);
@@ -79,6 +80,8 @@ public function testPrivilegesController(): void
7980
new Config(),
8081
))($request);
8182

83+
$this->dummyDbi->assertAllQueriesConsumed();
84+
8285
$actual = $response->getHTMLResult();
8386
self::assertStringContainsString('User accounts overview', $actual);
8487
self::assertStringContainsString(

tests/unit/Controllers/Table/PrivilegesControllerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public function testIndex(): void
3939
$config = Config::getInstance();
4040
$config->selectedServer['DisableIS'] = false;
4141

42+
$dbiDummy = $this->createDbiDummy();
43+
$dbiDummy->addResult('SELECT @@collation_server', [['utf8mb4_general_ci']]);
44+
// phpcs:ignore Generic.Files.LineLength.TooLong
45+
$dbiDummy->addResult("SELECT 1 FROM (SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`COLUMN_PRIVILEGES` UNION SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`TABLE_PRIVILEGES` UNION SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`SCHEMA_PRIVILEGES` UNION SELECT `GRANTEE`, `IS_GRANTABLE` FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES`) t WHERE `IS_GRANTABLE` = 'YES' AND '''pma_test''@''localhost''' LIKE `GRANTEE` UNION SELECT 1 FROM mysql.user WHERE `create_user_priv` = 'Y' COLLATE utf8mb4_general_ci AND 'pma_test' LIKE `User` AND '' LIKE `Host` LIMIT 1", [['1']]);
46+
$dbiDummy->addResult('SELECT @@collation_server', [['utf8mb4_general_ci']]);
47+
// phpcs:ignore Generic.Files.LineLength.TooLong
48+
$dbiDummy->addResult("SELECT 1 FROM `INFORMATION_SCHEMA`.`USER_PRIVILEGES` WHERE `PRIVILEGE_TYPE` = 'CREATE USER' AND '''pma_test''@''localhost''' LIKE `GRANTEE` UNION SELECT 1 FROM mysql.user WHERE `create_user_priv` = 'Y' COLLATE utf8mb4_general_ci AND 'pma_test' LIKE `User` AND '' LIKE `Host` LIMIT 1", [['1']]);
49+
$dbi = $this->createDatabaseInterface($dbiDummy);
50+
DatabaseInterface::$instance = $dbi;
51+
4252
$privileges = [];
4353

4454
$serverPrivileges = self::createMock(Privileges::class);
@@ -57,6 +67,8 @@ public function testIndex(): void
5767
))($request);
5868
$actual = $response->getHTMLResult();
5969

70+
$dbiDummy->assertAllQueriesConsumed();
71+
6072
self::assertStringContainsString(Current::$database . '.' . Current::$table, $actual);
6173

6274
//validate 2: Url::getCommon

0 commit comments

Comments
 (0)