N°8129 - Dont crash if date/time default value has a bad format

This commit is contained in:
XGUI
2025-01-30 14:14:31 +01:00
parent d12c8a183c
commit 803ebfbe33
4 changed files with 129 additions and 53 deletions

View File

@@ -6433,9 +6433,21 @@ class AttributeDateTime extends AttributeDBField
$sDefaultValue = $this->Get('default_value');
if (utils::IsNotNullOrEmptyString($sDefaultValue)) {
try {
$oDate = new DateTimeImmutable($sDefaultValue);
$sDefaultDate = Expression::FromOQL($sDefaultValue)->Evaluate([]);
} catch (Exception $e) {
$oDate = new DateTimeImmutable(Expression::FromOQL($sDefaultValue)->Evaluate([]));
try {
$sDefaultDate = Expression::FromOQL('"'.$sDefaultValue.'"')->Evaluate([]);
} catch (Exception $e) {
IssueLog::Error("Invalid default value '$sDefaultValue' for field '{$this->GetCode()}' on class '{$this->GetHostClass()}', defaulting to null");
return $this->GetNullValue();
}
}
try {
$oDate = new DateTimeImmutable($sDefaultDate);
} catch (Exception $e) {
IssueLog::Error("Invalid default value '$sDefaultValue' for field '{$this->GetCode()}' on class '{$this->GetHostClass()}', defaulting to null");
return $this->GetNullValue();
}
return $oDate->format($this->GetInternalFormat());
}

View File

@@ -987,6 +987,14 @@ abstract class ItopDataTestCase extends ItopTestCase
}
}
protected function AssertLastErrorLogEntryContains(string $sNeedle, string $sMessage = ''): void
{
$aLastLines = self::ReadTail(APPROOT.'/log/error.log');
$this->assertStringContainsString($sNeedle, $aLastLines[0], $sMessage);
}
/**
* Import a set of XML files describing a consistent set of iTop objects
* @param string[] $aFiles

View File

@@ -550,6 +550,27 @@ abstract class ItopTestCase extends TestCase
$this->AssertArraysHaveSameItems($aExpected, $aFiles, $sMessage);
}
/**
* @since 3.2.1
*/
static protected function AssertDateEqualsNow($sActualDate, $sMessage = ''): void
{
$oActualDate = \DateTime::createFromFormat(\AttributeDate::GetInternalFormat(), $sActualDate);
$oNow = new DateTime();
$iTimeInterval = $oNow->diff($oActualDate)->s;
self::assertLessThan(2, $iTimeInterval, $sMessage);
}
/**
* @since 3.2.1
*/
static protected function AssertDateTimeEqualsNow($sActualDate, $sMessage = ''): void
{
$oActualDateTime = \DateTime::createFromFormat(\AttributeDateTime::GetInternalFormat(), $sActualDate);
$oNow = new DateTime();
$iTimeInterval = $oNow->diff($oActualDateTime)->s;
self::assertLessThan(2, $iTimeInterval, $sMessage);
}
/**
* Control which Kernel will be loaded when invoking the bootKernel method
*
@@ -572,4 +593,37 @@ abstract class ItopTestCase extends TestCase
}
return parent::bootKernel($options);
}
/**
* @author Ain Tohvri <https://mstdn.social/@tekkie>
*
* @since 3.2.1
*/
static protected function ReadTail($sFilename, $iLines = 1)
{
$handle = fopen($sFilename, "r");
$iLineCounter = $iLines;
$iPos = -2;
$bBeginning = false;
$aLines = array();
while ($iLineCounter > 0) {
$sChar = " ";
while ($sChar != "\n") {
if(fseek($handle, $iPos, SEEK_END) == -1) {
$bBeginning = true;
break;
}
$sChar = fgetc($handle);
$iPos --;
}
$iLineCounter --;
if ($bBeginning) {
rewind($handle);
}
$aLines[$iLines - $iLineCounter - 1] = fgets($handle);
if ($bBeginning) break;
}
fclose ($handle);
return array_reverse($aLines);
}
}

View File

@@ -243,84 +243,76 @@ PHP
public function testDateTimeEmptyDefaultReturnsNullAsDefaultValue()
{
// Given
$oDateAttribute = new AttributeDateTime('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => '', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
$oDateAttribute->SetHostClass('WorkOrder');
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDateTime::class, '', false);
//When
$defaultValue = $oDateAttribute->GetDefaultValue();
// Then
self::assertNull($defaultValue, 'Empty default value for DateTime attribute should give null default value');
}
public function testDateTimeInvalidDefaultReturnsNullAsDefaultValue()
{
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDateTime::class, 'zabugomeuh', false);
$defaultValue = $oDateAttribute->GetDefaultValue();
self::assertNull($defaultValue, 'Invalid default value for DateTime attribute should give null default value');
self::AssertLastErrorLogEntryContains("Invalid default value 'zabugomeuh' for field 'start_date' on class 'WorkOrder', defaulting to null", "Last error log entry should contain a meaningful message");
}
public function testDateEmptyDefaultReturnsNullAsDefaultValue()
{
// Given
$oDateAttribute = new AttributeDate('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => '', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
$oDateAttribute->SetHostClass('WorkOrder');
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDate::class, '', false);
//When
$defaultValue = $oDateAttribute->GetDefaultValue();
// Then
self::assertNull($defaultValue, 'Empty default value for Date attribute should give null default value');
}
public function testDateTimeNowAsDefaultGivesCurrentDateAsDefaultValue()
public function testDateInvalidDefaultReturnsNullAsDefaultValue_Case1()
{
// Given
$oDateAttribute = new AttributeDateTime('start_date', [
'sql' => 'start_date',
'is_null_allowed' => false,
'default_value' => 'NOW()',
'allowed_values' => null,
'depends_on' => [],
'always_load_in_tables' => false
]);
$oDateAttribute->SetHostClass('WorkOrder');
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDate::class, 'zabugomeuh', false);
//When
$defaultValue = $oDateAttribute->GetDefaultValue();
self::AssertLastErrorLogEntryContains("Invalid default value 'zabugomeuh' for field 'start_date' on class 'WorkOrder', defaulting to null", "Last error log entry should contain a meaningful message");
// Then
$sNow = date($oDateAttribute->GetInternalFormat());
self::assertEquals($sNow, $defaultValue, 'Now as default value for DateTime attribute should give current date as default value');
self::assertNull($defaultValue, 'Invalid default value for Date attribute should give null default value');
}
public function testDateNowAsDefaultGivesCurrentDateAsDefaultValue()
public function testDateInvalidDefaultReturnsNullAsDefaultValue_Case2()
{
// Given
$oDateAttribute = new AttributeDate('start_date', [
'sql' => 'start_date',
'is_null_allowed' => false,
'default_value' => 'NOW()',
'allowed_values' => null,
'depends_on' => [],
'always_load_in_tables' => false
]);
$oDateAttribute->SetHostClass('WorkOrder');
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDate::class, '"27/01/2025"', false);
//When
$defaultValue = $oDateAttribute->GetDefaultValue();
// Then
$sNow = date($oDateAttribute->GetInternalFormat());
self::assertEquals($sNow, $defaultValue, 'Now as default value for Date attribute should give current date as default value');
self::AssertLastErrorLogEntryContains("Invalid default value '\"27/01/2025\"' for field 'start_date' on class 'WorkOrder', defaulting to null", "Last error log entry should contain a meaningful message");
self::assertNull($defaultValue, 'Invalid default value for Date attribute should give null default value');
}
public function testDateTimeNowAsDefaultGivesCurrentDateAsDefaultValue()
{
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDateTime::class, 'NOW()', false);
$sDefaultValue = $oDateAttribute->GetDefaultValue();
self::AssertDateTimeEqualsNow($sDefaultValue, 'NOW() should be evaluated as the current date and time');
}
public function testDateNowAsDefaultGivesCurrentDateAsDefaultValue()
{
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDate::class, 'NOW()', false);
$defaultValue = $oDateAttribute->GetDefaultValue();
self::AssertDateEqualsNow($defaultValue, 'NOW() should be evaluated as the current date');
}
public function testDateTimeIntervalAsDefaultGivesCorrectDateAsDefaultValue()
{
// Given
$oDateAttribute = new AttributeDateTime('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => 'DATE_ADD(NOW(), INTERVAL 1 DAY)', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
$oDateAttribute->SetHostClass('WorkOrder');
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDateTime::class, 'DATE_ADD(NOW(), INTERVAL 1 DAY)', false);
//When
$defaultValue = $oDateAttribute->GetDefaultValue();
// Then
$oDate = new \DateTimeImmutable('+1day');
$sExpected = $oDate->format($oDateAttribute->GetInternalFormat());
self::assertEquals($sExpected, $defaultValue, 'Interval as default value for DateTime attribute should give correct date as default value');
@@ -328,17 +320,27 @@ PHP
public function testDateIntervalAsDefaultGivesCorrectDateAsDefaultValue()
{
// Given
$oDateAttribute = new AttributeDate('start_date', ['sql' => 'start_date', 'is_null_allowed' => false, 'default_value' => 'DATE_ADD(NOW(), INTERVAL 1 DAY)', 'allowed_values' => null, 'depends_on' => [], 'always_load_in_tables' => false]);
$oDateAttribute->SetHostClass('WorkOrder');
$oDateAttribute = $this->GivenAttribute(\WorkOrder::class, 'start_date', AttributeDate::class, 'DATE_ADD(NOW(), INTERVAL 1 DAY)', false);
//When
$defaultValue = $oDateAttribute->GetDefaultValue();
// Then
$oDate = new \DateTimeImmutable('+1day');
$sExpected = $oDate->format($oDateAttribute->GetInternalFormat());
self::assertEquals($sExpected, $defaultValue, 'Interval as default value for Date attribute should give correct date as default value');
}
public function GivenAttribute(string $sHostClass, string $sAttCode, string $sAttributeClass, mixed $defaultValue, bool $bIsNullAllowed): \AttributeDefinition
{
$oAttribute = new $sAttributeClass($sAttCode, [
'sql' => $sAttCode,
'is_null_allowed' => $bIsNullAllowed,
'default_value' => $defaultValue,
'allowed_values' => null,
'depends_on' => [],
'always_load_in_tables' => false
]);
$oAttribute->SetHostClass($sHostClass);
return $oAttribute;
}
}