diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index bbcb0494b..48d67fcf4 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -350,6 +350,12 @@ class CMDBSource } + /** + * @return string + * @throws \MySQLException + * + * @uses \CMDBSource::QueryToCol() so needs a connection opened ! + */ public static function GetDBVersion() { $aVersions = self::QueryToCol('SELECT Version() as version', 'version'); @@ -367,8 +373,10 @@ class CMDBSource /** * Get the DB vendor between MySQL and its main forks * @return string + * + * @uses \CMDBSource::GetServerVariable() so needs a connection opened ! */ - static public function GetDBVendor() + public static function GetDBVendor() { $sDBVendor = static::ENUM_DB_VENDOR_MYSQL; diff --git a/setup/backup.class.inc.php b/setup/backup.class.inc.php index ac1328140..1720502d1 100644 --- a/setup/backup.class.inc.php +++ b/setup/backup.class.inc.php @@ -254,7 +254,7 @@ class DBBackup return $aRet; } - protected static function EscapeShellArg($sValue) + public static function EscapeShellArg($sValue) { // Note: See comment from the 23-Apr-2004 03:30 in the PHP documentation // It suggests to rely on pctnl_* function instead of using escapeshellargs @@ -461,13 +461,15 @@ EOF; /** - * @see https://dev.mysql.com/doc/refman/5.6/en/encrypted-connection-options.html - * * @param Config $oConfig * * @return string TLS arguments for CLI programs such as mysqldump. Empty string if the config does not use TLS. * - * @since 2.5.0 + * @uses \CMDBSource::GetDBVendor() so needs a connection opened ! + * @uses \CMDBSource::GetDBVersion() so needs a connection opened ! + * + * @since 2.5.0 N°1260 + * @link https://dev.mysql.com/doc/refman/5.6/en/connection-options.html#encrypted-connection-options "Command Options for Encrypted Connections" */ public static function GetMysqlCliTlsOptions($oConfig) { diff --git a/test/ItopDataTestCase.php b/test/ItopDataTestCase.php index cbbd820f4..9c297f258 100644 --- a/test/ItopDataTestCase.php +++ b/test/ItopDataTestCase.php @@ -56,9 +56,16 @@ define('TAG_CLASS', 'FAQ'); define('TAG_ATTCODE', 'domains'); /** + * Helper class to extend for tests needing access to iTop's metamodel + * + * **⚠ Warning** Each class extending this one needs to add the following annotations : + * * @runTestsInSeparateProcesses * @preserveGlobalState disabled * @backupGlobals disabled + * + * @since 2.7.7 3.0.1 3.1.0 N°4624 processIsolation is disabled by default and must be enabled in each test needing it (basically all tests using + * iTop datamodel) */ class ItopDataTestCase extends ItopTestCase { diff --git a/test/itop-config/Validator/iTopConfigSyntaxValidatorTest.php b/test/itop-config/Validator/iTopConfigSyntaxValidatorTest.php index dc8a87a1c..74e779c38 100644 --- a/test/itop-config/Validator/iTopConfigSyntaxValidatorTest.php +++ b/test/itop-config/Validator/iTopConfigSyntaxValidatorTest.php @@ -13,7 +13,7 @@ use Combodo\iTop\Test\UnitTest\ItopTestCase; use PhpParser\Node; use PhpParser\PrettyPrinter\Standard; -class iTopConfigAstValidatorTest extends ItopTestCase +class iTopConfigSyntaxValidatorTest extends ItopTestCase { public function setUp() diff --git a/test/phpunit.xml.dist b/test/phpunit.xml.dist index 11f5ac8e1..083c35ee3 100644 --- a/test/phpunit.xml.dist +++ b/test/phpunit.xml.dist @@ -19,7 +19,7 @@ itop-tickets + + itop-config + application @@ -66,7 +69,7 @@ synchro - setup/SetupUtilsTest.php + setup integration diff --git a/test/setup/DBBackupTest.php b/test/setup/DBBackupTest.php index 798d453a5..7f767f439 100644 --- a/test/setup/DBBackupTest.php +++ b/test/setup/DBBackupTest.php @@ -2,9 +2,10 @@ namespace Combodo\iTop\Test\UnitTest\Core; +use CMDBSource; use Combodo\iTop\Test\UnitTest\ItopTestCase; -use Config; use DBBackup; +use utils; /** * @runTestsInSeparateProcesses @@ -13,29 +14,76 @@ use DBBackup; */ class DBBackupTest extends ItopTestCase { + /** + * @throws \CoreException + * @throws \MySQLException + * @throws \ConfigException + */ protected function setUp() { parent::setUp(); require_once(APPROOT.'core/config.class.inc.php'); require_once(APPROOT.'setup/backup.class.inc.php'); require_once(APPROOT.'core/cmdbsource.class.inc.php'); // DBBackup dependency + + // We need a connection to the DB, so let's open it ! + // We are using the default config file... as the server might not be configured for all the combination we are testing + // For example dev env and ci env won't accept TLS connection + $oConfigOnDisk = utils::GetConfig(); + CMDBSource::InitFromConfig($oConfigOnDisk); } - public function testGetMysqlCliTlsOptions() + /** + * No TLS connection = no additional CLI args ! + * + * @throws \CoreException + * @throws \ConfigException + * @throws \MySQLException + */ + public function testGetMysqlCliTlsOptionsNoTls() { - $oConfig = new Config(); - $oConfig->Set('db_tls.enabled', false); + $oConfigToTest = utils::GetConfig(); + + $oConfigToTest->Set('db_tls.enabled', false); + $sCliArgsNoTls = DBBackup::GetMysqlCliTlsOptions($oConfigToTest); - $sCliArgsNoTls = DBBackup::GetMysqlCliTlsOptions($oConfig); $this->assertEmpty($sCliArgsNoTls); + } - $oConfig->Set('db_tls.enabled', true); - $sCliArgsMinCfg = DBBackup::GetMysqlCliTlsOptions($oConfig); - $this->assertEquals(' --ssl', $sCliArgsMinCfg); + /** + * TLS connection configured = we need one CLI arg + * + * @return void + * @throws \ConfigException + * @throws \CoreException + */ + public function testGetMysqlCliTlsOptionsWithTlsNoCa() + { + $oConfigToTest = utils::GetConfig(); + $oConfigToTest->Set('db_tls.enabled', true); + $sCliArgsMinCfg = DBBackup::GetMysqlCliTlsOptions($oConfigToTest); + // depending on the MySQL version, we would have `--ssl` or `--ssl-mode=VERIFY_CA` + $this->assertStringStartsWith(' --ssl', $sCliArgsMinCfg); + } + + /** + * TLS connection configured + CA option = we need multiple CLI args + * + * @return void + * @throws \ConfigException + * @throws \CoreException + */ + public function testGetMysqlCliTlsOptionsWithTlsAndCa() + { + $oConfigToTest = utils::GetConfig(); $sTestCa = 'my_test_ca'; - $oConfig->Set('db_tls.ca', $sTestCa); - $sCliArgsCapathCfg = DBBackup::GetMysqlCliTlsOptions($oConfig); - $this->assertEquals(' --ssl --ssl-ca="'.$sTestCa.'"', $sCliArgsCapathCfg); + + $oConfigToTest->Set('db_tls.enabled', true); + $oConfigToTest->Set('db_tls.ca', $sTestCa); + $sCliArgsCapathCfg = DBBackup::GetMysqlCliTlsOptions($oConfigToTest); + + $this->assertStringStartsWith(' --ssl', $sCliArgsCapathCfg); + $this->assertStringEndsWith('--ssl-ca='.DBBackup::EscapeShellArg($sTestCa), $sCliArgsCapathCfg); } }