From 4c26a7a68293eecc8b885cbf9c49d3a8655a7a7f Mon Sep 17 00:00:00 2001 From: odain Date: Tue, 8 Aug 2023 15:41:12 +0200 Subject: [PATCH] =?UTF-8?q?N=C2=B06640=20-=20Broken=20unattended=20setup?= =?UTF-8?q?=20with=20XML=20backup=20configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup/backup.class.inc.php | 11 +++++-- .../unitary-tests/setup/DBBackupTest.php | 31 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/setup/backup.class.inc.php b/setup/backup.class.inc.php index f7f8fd9c0..7f7c7eea9 100644 --- a/setup/backup.class.inc.php +++ b/setup/backup.class.inc.php @@ -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); diff --git a/tests/php-unit-tests/unitary-tests/setup/DBBackupTest.php b/tests/php-unit-tests/unitary-tests/setup/DBBackupTest.php index c772d0223..83d9e8663 100644 --- a/tests/php-unit-tests/unitary-tests/setup/DBBackupTest.php +++ b/tests/php-unit-tests/unitary-tests/setup/DBBackupTest.php @@ -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'."); + } }