Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions core/cmdbsource.class.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Exception;
use IssueLog;
use LogChannels;
use MetaModel;
use utils;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading