From 7b1e132db08b3aaa43d8431882fd612cd9497334 Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Mon, 4 May 2026 09:56:36 +0200 Subject: [PATCH 1/2] Create tests --- .../core/CMDBSource/CMDBSourceTest.php | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/php-unit-tests/unitary-tests/core/CMDBSource/CMDBSourceTest.php b/tests/php-unit-tests/unitary-tests/core/CMDBSource/CMDBSourceTest.php index b9eec2c644..b7e352bf2d 100644 --- a/tests/php-unit-tests/unitary-tests/core/CMDBSource/CMDBSourceTest.php +++ b/tests/php-unit-tests/unitary-tests/core/CMDBSource/CMDBSourceTest.php @@ -8,6 +8,7 @@ use Exception; use IssueLog; use LogChannels; +use MetaModel; use utils; /** @@ -23,6 +24,10 @@ protected function setUp(): void parent::setUp(); $this->RequireOnceItopFile('/core/cmdbsource.class.inc.php'); + $sEnv = 'production'; + $sConfigFile = APPCONF.$sEnv.'/config-itop.php'; + + MetaModel::Startup($sConfigFile, false, true, false, $sEnv); } protected function tearDown(): void @@ -122,6 +127,33 @@ public function compareFieldTypesProvider() ]; } + /** + * @covers CMDBSource::GetFieldSpec + * @dataProvider fieldSpecsProvider + */ + public function testGetFieldSpec(string $sTable, string $sField, false|string $expected): void + { + $this->assertEquals($expected, CMDBSource::getFieldSpec($sTable, $sField)); + } + + public function fieldSpecsProvider(): array + { + return [ + 'Unknown table' => ['unknown', 'unknown', false], + 'Unknown field' => ['ticket', 'unknown', false], + 'Primary key' => ['ticket', 'id', 'int(11) NOT NULL'], + 'External key' => ['ticket', 'org_id', 'int(11) DEFAULT 0'], + 'Integer nullable' => ['datacenterdevice' , 'nb_u', 'int(11) DEFAULT NULL'], + 'String empty default' => ['ticket', 'ref', "varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT ''"], + 'String simple default' => ['ticket', 'finalclass', "varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT 'Ticket'"], + 'String numeric default' => ['datacenterdevice', 'redundancy', "varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '1'"], +// 'String required' => ['', '', "varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ''"], // does not seem to exist in iTop model? + 'Enum simple' => ['physicaldevice', 'status', "enum('stock','implementation','production','obsolete') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT 'production'"], + 'Enum numbers' => ['ticket_request', 'priority', "enum('1','2','3','4') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '4'"], + 'Datetime nullable' => ['ticket', 'last_update', "datetime DEFAULT NULL"], + ]; + } + /** * @throws \ConfigException * @throws \CoreException From c14a7831c4998c231f820a953775c0e639750a89 Mon Sep 17 00:00:00 2001 From: Thomas Casteleyn Date: Mon, 4 May 2026 10:24:36 +0200 Subject: [PATCH 2/2] Fix method --- core/cmdbsource.class.inc.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index 4ea759ae93..49ee155679 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -1279,20 +1279,19 @@ public static function GetFieldSpec($sTable, $sField) } if (is_numeric($aFieldData["Default"])) { - if (strtolower(substr($aFieldData["Type"], 0, 5)) == 'enum(') { - // Force quotes to match the column declaration statement - $sRet .= ' DEFAULT '.self::Quote($aFieldData["Default"], true); + if (self::IsNumericType($aFieldData)) { + $sRet .= ' DEFAULT '.$aFieldData["Default"]; } else { - if (self::IsNumericType($aFieldData)) { - $sRet .= ' DEFAULT '.$aFieldData["Default"]; - } else { - $default = $aFieldData["Default"] + 0; // Coerce to a numeric variable - $sRet .= ' DEFAULT '.self::Quote($default); - } + $default = $aFieldData["Default"] + 0; // Coerce to a numeric variable + $sRet .= ' DEFAULT '.self::Quote($default, true); + } + } elseif (is_string($aFieldData["Default"])) { + if ($aFieldData["Null"] === 'YES' && $aFieldData["Default"] === 'NULL') { + $sRet .= ' DEFAULT NULL'; + } else { + $sDefaultValue = static::RemoveSurroundingQuotes($aFieldData["Default"]); + $sRet .= ' DEFAULT '.self::Quote($sDefaultValue); } - } elseif (is_string($aFieldData["Default"]) == 'string') { - $sDefaultValue = static::RemoveSurroundingQuotes($aFieldData["Default"]); - $sRet .= ' DEFAULT '.self::Quote($sDefaultValue); } return $sRet;