Skip to content

Commit

Permalink
Use iterator instead of raw queries, so it can be mocked in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Jan 16, 2025
1 parent 147c662 commit a61a56a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
22 changes: 11 additions & 11 deletions phpunit/functional/Glpi/System/Requirement/DbTimezonesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function testCheckWithUnavailableMysqlDb()
->getMock();

$db->method('request')->willReturnCallback(
function ($query) {
function ($criteria) {
$result = $this->getMockBuilder(\DBmysqlIterator::class)
->setConstructorArgs([null])
->onlyMethods(['count'])
->getMock();
if ($query === "SHOW DATABASES LIKE 'mysql'") {
if ($criteria['FROM'] == 'information_schema.schemata' && $criteria['WHERE']['schema_name'] == 'mysql') {
$result->method('count')->willReturn(0);
}
return $result;
Expand All @@ -73,14 +73,14 @@ public function testCheckWithUnavailableTimezonenameTable()
->getMock();

$db->method('request')->willReturnCallback(
function ($query) {
function ($criteria) {
$result = $this->getMockBuilder(\DBmysqlIterator::class)
->setConstructorArgs([null])
->onlyMethods(['count'])
->getMock();
if ($query === "SHOW DATABASES LIKE 'mysql'") {
if ($criteria['FROM'] == 'information_schema.schemata' && $criteria['WHERE']['schema_name'] == 'mysql') {
$result->method('count')->willReturn(1);
} else if ($query === "SHOW TABLES FROM `mysql` LIKE 'time_zone_name'") {
} elseif ($criteria['FROM'] == 'information_schema.tables' && $criteria['WHERE']['table_schema'] == 'mysql') {
$result->method('count')->willReturn(0);
}
return $result;
Expand All @@ -103,14 +103,14 @@ public function testCheckWithTimezonenameEmptyTable()
->getMock();

$db->method('request')->willReturnCallback(
function ($query) {
function ($criteria) {
$result = $this->getMockBuilder(\DBmysqlIterator::class)
->setConstructorArgs([null])
->onlyMethods(['count', 'current'])
->getMock();
if ($query === "SHOW DATABASES LIKE 'mysql'") {
if ($criteria['FROM'] == 'information_schema.schemata' && $criteria['WHERE']['schema_name'] == 'mysql') {
$result->method('count')->willReturn(1);
} else if ($query === "SHOW TABLES FROM `mysql` LIKE 'time_zone_name'") {
} elseif ($criteria['FROM'] == 'information_schema.tables' && $criteria['WHERE']['table_schema'] == 'mysql') {
$result->method('count')->willReturn(1);
} else {
$result->method('current')->willReturn(['cpt' => 0]);
Expand All @@ -135,14 +135,14 @@ public function testCheckWithAvailableData()
->getMock();

$db->method('request')->willReturnCallback(
function ($query) {
function ($criteria) {
$result = $this->getMockBuilder(\DBmysqlIterator::class)
->setConstructorArgs([null])
->onlyMethods(['count', 'current'])
->getMock();
if ($query === "SHOW DATABASES LIKE 'mysql'") {
if ($criteria['FROM'] == 'information_schema.schemata' && $criteria['WHERE']['schema_name'] == 'mysql') {
$result->method('count')->willReturn(1);
} else if ($query === "SHOW TABLES FROM `mysql` LIKE 'time_zone_name'") {
} elseif ($criteria['FROM'] == 'information_schema.tables' && $criteria['WHERE']['table_schema'] == 'mysql') {
$result->method('count')->willReturn(1);
} else {
$result->method('current')->willReturn(['cpt' => 30]);
Expand Down
25 changes: 16 additions & 9 deletions src/Glpi/System/Requirement/DbTimezones.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,27 @@ public function __construct(\DBmysql $db)

protected function check()
{
$mysql_db_res = $this->db->doQuery('SHOW DATABASES LIKE ' . $this->db->quoteValue('mysql'));
if ($this->db->numrows($mysql_db_res) === 0) {
$mysql_db_res = $this->db->request([
'FROM' => 'information_schema.schemata',
'WHERE' => [
'schema_name' => 'mysql'
]
]);
if ($mysql_db_res->count() === 0) {
$this->validated = false;
$this->validation_messages[] = __('Access to timezone database (mysql) is not allowed.');
return;
}

$tz_table_res = $this->db->doQuery(
'SHOW TABLES FROM '
. $this->db->quoteName('mysql')
. ' LIKE '
. $this->db->quoteValue('time_zone_name')
);
if ($this->db->numrows($tz_table_res) === 0) {
$tz_table_res = $this->db->request([
'FROM' => 'information_schema.tables',
'WHERE' => [
'table_schema' => 'mysql',
'table_type' => 'BASE TABLE',
'table_name' => ['LIKE', 'time_zone_name']
]
]);
if ($tz_table_res->count() === 0) {
$this->validated = false;
$this->validation_messages[] = __('Access to timezone table (mysql.time_zone_name) is not allowed.');
return;
Expand Down

0 comments on commit a61a56a

Please sign in to comment.