N°6640 - Broken unattended setup with XML backup configuration

This commit is contained in:
odain
2023-08-08 15:41:12 +02:00
parent bcb110d99e
commit 4c26a7a682
2 changed files with 38 additions and 4 deletions

View File

@@ -146,18 +146,23 @@ class DBBackup
/**
* Create a normalized backup name, depending on the current date/time and Database
*
* @param string $sNameSpec Name and path, eventually containing itop placeholders + time formatting following the strftime() format {@link https://www.php.net/manual/fr/function.strftime.php}
* @param ?\DateTime|null $oDateTime Date time to use for the name
* @param string|null $sNameSpec Name and path, eventually containing itop placeholders + time formatting following the strftime() format {@link https://www.php.net/manual/fr/function.strftime.php}
* @param \DateTime|null $oDateTime Date time to use for the name
*
* @return ?string Name of the backup file WITHOUT the file extension (eg. `.tar.gz`)
* @since 3.1.0 N°5279 Add $oDateTime parameter
*/
public function MakeName(?string $sNameSpec = "__DB__-%Y-%m-%d", ?DateTime $oDateTime = null)
public function MakeName(?string $sNameSpec = null, ?DateTime $oDateTime = null)
{
if ($oDateTime === null) {
$oDateTime = new DateTime();
}
//N°6640
if ($sNameSpec === null) {
$sNameSpec = "__DB__-%Y-%m-%d";
}
$sFileName = $sNameSpec;
$sFileName = str_replace('__HOST__', $this->sDBHost, $sFileName);
$sFileName = str_replace('__DB__', $this->sDBName, $sFileName);

View File

@@ -118,7 +118,7 @@ class DBBackupTest extends ItopTestCase
*
* @return void
*/
public function testMakeName(string $sInputFormat, DateTime $oBackupDateTime, string $sExpectedFilename): void
public function testMakeName(?string $sInputFormat, ?DateTime $oBackupDateTime, string $sExpectedFilename): void
{
$oConfig = utils::GetConfig();
@@ -138,6 +138,11 @@ class DBBackupTest extends ItopTestCase
$oBackupDateTime = DateTime::createFromFormat('Y-m-d H:i:s', '1985-07-30 15:30:59');
return [
'Default format - no params' => [
'__DB__-%Y-%m-%d',
$oBackupDateTime,
static::DUMMY_DB_NAME.'-1985-07-30',
],
'Default format' => [
'__DB__-%Y-%m-%d',
$oBackupDateTime,
@@ -160,4 +165,28 @@ class DBBackupTest extends ItopTestCase
],
];
}
/**
* N°6640 - Broken unattended setup with XML backup configuration
*/
public function testMakeNameWithoutParams(): void
{
$oConfig = utils::GetConfig();
// See https://github.com/Combodo/iTop/commit/f7ee21f1d7d1c23910506e9e31b57f33311bd5e0#diff-d693fb790e3463d1aa960c2b8b293532b1bbd12c3b8f885d568d315c404f926aR131
$oConfig->Set('db_host', static::DUMMY_DB_HOST);
$oConfig->Set('db_name', static::DUMMY_DB_NAME);
$oConfig->Set('db_subname', static::DUMMY_DB_SUBNAME);
$oDateTime = new DateTime();
$sExpectedFilename = static::DUMMY_DB_NAME . '-' . $oDateTime->format("Y-m-d");
$oBackup = new DBBackup($oConfig);
$sTestedFilename = $oBackup->MakeName(null);
$this->assertEquals($sExpectedFilename, $sTestedFilename, "Backup filename format doesn't match. Got '$sTestedFilename', expected '$sExpectedFilename'.");
$sTestedFilename = $oBackup->MakeName();
$this->assertEquals($sExpectedFilename, $sTestedFilename, "Backup filename format doesn't match. Got '$sTestedFilename', expected '$sExpectedFilename'.");
}
}