Commit 8e8f3448 authored by Cédric Anne's avatar Cédric Anne
Browse files

Always test on MySQL 8.0 to detect deprecation warnings before merge

parent 65a7f3af
......@@ -131,6 +131,7 @@ jobs:
matrix:
include:
- {php-version: "8.0", db-image: "mariadb:10.5", always: true}
- {php-version: "8.0", db-image: "mysql:8.0", always: true}
- {php-version: "7.2", db-image: "mariadb:10.5", always: false}
- {php-version: "7.3", db-image: "mariadb:10.5", always: false}
- {php-version: "7.4", db-image: "mariadb:10.5", always: false}
......
......@@ -56,24 +56,43 @@ class DBConnection extends CommonDBTM {
* @param string $host The DB host
* @param string $user The DB user
* @param string $password The DB password
* @param string $DBname The name of the DB
* @param string $dbname The name of the DB
* @param boolean $use_utf8mb4 Flag that indicates if utf8mb4 charset/collation should be used
* @param boolean $log_deprecation_warnings Flag that indicates if DB deprecation warnings should be logged
* @param string $config_dir
*
* @return boolean
*/
static function createMainConfig($host, $user, $password, $DBname, $use_utf8mb4 = false, $log_deprecation_warnings = false) {
$DB_str = "<?php\nclass DB extends DBmysql {\n" .
" public \$dbhost = '$host';\n" .
" public \$dbuser = '$user';\n" .
" public \$dbpassword = '". rawurlencode($password) . "';\n" .
" public \$dbdefault = '$DBname';\n" .
" public \$use_utf8mb4 = " . ($use_utf8mb4 ? 'true' : 'false') . ";\n" .
" public \$log_deprecation_warnings = " . ($log_deprecation_warnings ? 'true' : 'false') . ";\n" .
"}\n";
return Toolbox::writeConfig('config_db.php', $DB_str);
static function createMainConfig(
string $host,
string $user,
string $password,
string $dbname,
bool $use_utf8mb4 = false,
bool $log_deprecation_warnings = false,
string $config_dir = GLPI_CONFIG_DIR
): bool {
$properties = [
'dbhost' => $host,
'dbuser' => $user,
'dbpassword' => rawurlencode($password),
'dbdefault' => $dbname,
];
if ($use_utf8mb4) {
$properties['use_utf8mb4'] = true;
}
if ($log_deprecation_warnings) {
$properties['log_deprecation_warnings'] = true;
}
$config_str = '<?php' . "\n" . 'class DB extends DBmysql {' . "\n";
foreach ($properties as $name => $value) {
$config_str .= sprintf(' public $%s = %s;', $name, var_export($value, true)) . "\n";
}
$config_str .= '}' . "\n";
return Toolbox::writeConfig('config_db.php', $config_str, $config_dir);
}
......@@ -138,52 +157,53 @@ class DBConnection extends CommonDBTM {
/**
* Create slave DB configuration file
*
* @param string $host The slave DB host(s)
* @param string $user The slave DB user
* @param string $password The slave DB password
* @param string $DBname The name of the slave DB
* @param string $host The DB host
* @param string $user The DB user
* @param string $password The DB password
* @param string $dbname The name of the DB
* @param boolean $use_utf8mb4 Flag that indicates if utf8mb4 charset/collation should be used
* @param boolean $log_deprecation_warnings Flag that indicates if DB deprecation warnings should be logged
* @param string $config_dir
*
* @return boolean for success
**/
static function createSlaveConnectionFile($host, $user, $password, $DBname) {
// Get use_utf8mb4 flag from master config
$master = new class() extends DB {
public function __construct() {
// Deactivate connection
}
};
$use_utf8mb4 = $master->use_utf8mb4;
$log_deprecation_warnings = $master->log_deprecation_warnings;
$DB_str = "<?php \n class DBSlave extends DBmysql { \n public \$slave = true; \n public \$dbhost = ";
$host = trim($host);
static function createSlaveConnectionFile(
string $host,
string $user,
string $password,
string $dbname,
bool $use_utf8mb4 = false,
bool $log_deprecation_warnings = false,
string $config_dir = GLPI_CONFIG_DIR
): bool {
// Explode host into array (multiple values separated by a space char)
$host = trim($host);
if (strpos($host, ' ')) {
$hosts = explode(' ', $host);
$first = true;
foreach ($hosts as $host) {
if (!empty($host)) {
$DB_str .= ($first ? "array('" : ",'").$host."'";
$first = false;
}
}
if ($first) {
// no host configured
return false;
}
$DB_str .= ");\n";
$host = explode(' ', $host);
}
} else {
$DB_str .= "'$host';\n";
$properties = [
'slave' => true,
'dbhost' => $host,
'dbuser' => $user,
'dbpassword' => rawurlencode($password),
'dbdefault' => $dbname,
];
if ($use_utf8mb4) {
$properties['use_utf8mb4'] = true;
}
$DB_str .= " public \$dbuser = '" . $user . "'; \n"
. " public \$dbpassword = '" . rawurlencode($password) . "'; \n"
. " public \$dbdefault = '" . $DBname . "'; \n"
. " public \$use_utf8mb4 = " . ($use_utf8mb4 ? 'true' : 'false') . "; \n"
. " public \$log_deprecation_warnings = " . ($log_deprecation_warnings ? 'true' : 'false') . "; \n"
. "}\n";
return Toolbox::writeConfig('config_db_slave.php', $DB_str);
if ($log_deprecation_warnings) {
$properties['log_deprecation_warnings'] = true;
}
$config_str = '<?php' . "\n" . 'class DB extends DBmysql {' . "\n";
foreach ($properties as $name => $value) {
$config_str .= sprintf(' public $%s = %s;', $name, var_export($value, true)) . "\n";
}
$config_str .= '}' . "\n";
return Toolbox::writeConfig('config_db_slave.php', $config_str, $config_dir);
}
......@@ -217,7 +237,8 @@ class DBConnection extends CommonDBTM {
* Create a default slave DB configuration file
**/
static function createDBSlaveConfig() {
self::createSlaveConnectionFile("localhost", "glpi", "glpi", "glpi");
global $DB;
self::createSlaveConnectionFile("localhost", "glpi", "glpi", "glpi", $DB->use_utf8mb4, $DB->log_deprecation_warnings);
}
......@@ -230,7 +251,8 @@ class DBConnection extends CommonDBTM {
* @param $DBname
**/
static function saveDBSlaveConf($host, $user, $password, $DBname) {
self::createSlaveConnectionFile($host, $user, $password, $DBname);
global $DB;
self::createSlaveConnectionFile($host, $user, $password, $DBname, $DB->use_utf8mb4, $DB->log_deprecation_warnings);
}
......
......@@ -71,6 +71,179 @@ class DBConnection extends \GLPITestCase {
$this->array($queries)->isIdenticalTo([$expected_query]);
}
protected function mainConfigPropertiesProvider() {
return [
[
'host' => 'localhost',
'user' => 'glpi',
'password' => 'secret',
'name' => 'glpi_db',
'use_utf8mb4' => false,
'log_deprecation_warnings' => false,
'expected' => <<<'PHP'
<?php
class DB extends DBmysql {
public $dbhost = 'localhost';
public $dbuser = 'glpi';
public $dbpassword = 'secret';
public $dbdefault = 'glpi_db';
}
PHP
],
[
'host' => '127.0.0.1',
'user' => 'root',
'password' => '',
'name' => 'db',
'use_utf8mb4' => true,
'log_deprecation_warnings' => false,
'expected' => <<<'PHP'
<?php
class DB extends DBmysql {
public $dbhost = '127.0.0.1';
public $dbuser = 'root';
public $dbpassword = '';
public $dbdefault = 'db';
public $use_utf8mb4 = true;
}
PHP
],
[
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'iT4%dU9*rI9#jT8>',
'name' => 'db',
'use_utf8mb4' => false,
'log_deprecation_warnings' => true,
'expected' => <<<'PHP'
<?php
class DB extends DBmysql {
public $dbhost = '127.0.0.1';
public $dbuser = 'root';
public $dbpassword = 'iT4%25dU9%2ArI9%23jT8%3E';
public $dbdefault = 'db';
public $log_deprecation_warnings = true;
}
PHP
],
];
}
/**
* @dataProvider mainConfigPropertiesProvider
*/
public function testCreateMainConfig(
string $host,
string $user,
string $password,
string $name,
bool $use_utf8mb4,
bool $log_deprecation_warnings,
string $expected
): void {
vfsStream::setup('config-dir', null, []);
$result = \DBConnection::createMainConfig($host, $user, $password, $name, $use_utf8mb4, $log_deprecation_warnings, vfsStream::url('config-dir'));
$this->boolean($result)->isTrue();
$path = vfsStream::url('config-dir/config_db.php');
$this->boolean(file_exists($path))->isTrue();
$this->string(file_get_contents($path))->isEqualTo($expected);
}
protected function slaveConfigPropertiesProvider() {
return [
[
'host' => 'slave.db.domain.org',
'user' => 'glpi',
'password' => 'secret',
'name' => 'glpi_db',
'use_utf8mb4' => false,
'log_deprecation_warnings' => false,
'expected' => <<<'PHP'
<?php
class DB extends DBmysql {
public $slave = true;
public $dbhost = 'slave.db.domain.org';
public $dbuser = 'glpi';
public $dbpassword = 'secret';
public $dbdefault = 'glpi_db';
}
PHP
],
[
'host' => 'slave1.db.domain.org slave2.db.domain.org slave3.db.domain.org ',
'user' => 'root',
'password' => '',
'name' => 'db',
'use_utf8mb4' => true,
'log_deprecation_warnings' => false,
'expected' => <<<'PHP'
<?php
class DB extends DBmysql {
public $slave = true;
public $dbhost = array (
0 => 'slave1.db.domain.org',
1 => 'slave2.db.domain.org',
2 => 'slave3.db.domain.org',
);
public $dbuser = 'root';
public $dbpassword = '';
public $dbdefault = 'db';
public $use_utf8mb4 = true;
}
PHP
],
[
'host' => '127.0.0.1',
'user' => 'root',
'password' => 'iT4%dU9*rI9#jT8>',
'name' => 'db',
'use_utf8mb4' => false,
'log_deprecation_warnings' => true,
'expected' => <<<'PHP'
<?php
class DB extends DBmysql {
public $slave = true;
public $dbhost = '127.0.0.1';
public $dbuser = 'root';
public $dbpassword = 'iT4%25dU9%2ArI9%23jT8%3E';
public $dbdefault = 'db';
public $log_deprecation_warnings = true;
}
PHP
],
];
}
/**
* @dataProvider slaveConfigPropertiesProvider
*/
public function testCreateSlaveConnectionFile(
string $host,
string $user,
string $password,
string $name,
bool $use_utf8mb4,
bool $log_deprecation_warnings,
string $expected
): void {
vfsStream::setup('config-dir', null, []);
$result = \DBConnection::createSlaveConnectionFile($host, $user, $password, $name, $use_utf8mb4, $log_deprecation_warnings, vfsStream::url('config-dir'));
$this->boolean($result)->isTrue();
$path = vfsStream::url('config-dir/config_db_slave.php');
$this->boolean(file_exists($path))->isTrue();
$this->string(file_get_contents($path))->isEqualTo($expected, file_get_contents($path));
}
protected function configFilesProvider() {
return [
[
......@@ -265,7 +438,8 @@ PHP
*/
public function testUpdateConfigProperty(
array $init_config_files,
string $name, $new_value,
string $name,
$new_value,
bool $update_slave,
array $expected_config_files,
bool $expected_result = true
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment