Compare commits

...

5 Commits

Author SHA1 Message Date
Stephen Abello
933d552e1e Fix variable types 2026-07-13 15:36:19 +02:00
Stephen Abello
5cfb15adde Fix variable types 2026-07-13 15:34:59 +02:00
Stephen Abello
39cadd64b6 Move TrimValue() base definition to AttributeDefinition class, assign DBObject::SetTrim old behavior to it 2026-07-13 15:34:09 +02:00
Anne-Cath
e08897a7ca WIP 2026-07-07 16:46:53 +02:00
Anne-Cath
89384852c1 N°9759 - Truncate AttributeText don't work as expected in case of multibytes characters 2026-07-07 11:39:01 +02:00
4 changed files with 89 additions and 38 deletions

View File

@@ -897,6 +897,25 @@ abstract class AttributeDefinition
return null;
}
/**
* Helper to set a value that fits the attribute max size
*
* Default behavior is what DBObject::SetTrim used to do, now delegated to AttributeDefinition
*
* @param string $sValue
*
* @return string
* @since 3.2.0 N°9643
*/
public function TrimValue(string $sValue)
{
$iMaxSize = $this->GetMaxSize();
if ($iMaxSize && (strlen($sValue) > $iMaxSize)) {
$sValue = substr($sValue, 0, $iMaxSize);
}
return $sValue;
}
/**
* @return mixed|null
* @deprecated never used
@@ -2586,6 +2605,11 @@ class AttributeDBFieldVoid extends AttributeDefinition
.($bFullSpec ? $this->GetSQLColSpec() : '');
}
public function GetSize($value)
{
return mb_strlen($value);
}
protected function GetSQLColSpec()
{
$default = $this->ScalarToSQL($this->GetDefaultValue());
@@ -3483,6 +3507,18 @@ class AttributeString extends AttributeDBField
.($bFullSpec ? $this->GetSQLColSpec() : '');
}
public function TrimValue(string $sValue)
{
$iMaxSize = $this->GetMaxSize();
$iLength = mb_strlen($sValue);
if ($iMaxSize && ($iLength > $iMaxSize)) {
$sMessage = " -truncated ($iLength chars)";
return mb_substr($sValue, 0, $iMaxSize - mb_strlen($sMessage)).$sMessage;
}
return $sValue;
}
public function GetValidationPattern()
{
$sPattern = $this->GetOptional('validation_pattern', '');
@@ -4219,6 +4255,26 @@ class AttributeText extends AttributeString
return "TEXT".CMDBSource::GetSqlStringColumnDefinition();
}
public function GetSize($value)
{
return strlen($value);
}
public function TrimValue(string $sValue)
{
$iMaxSize = $this->GetMaxSize();
$iLength = strlen($sValue);
$iLengthChar = mb_strlen($sValue);
if ($iMaxSize && ($iLength > $iMaxSize)) {
$sMessage = " -truncated ($iLengthChar chars)";
$sVal = substr($sValue, 0, $iMaxSize - strlen($sMessage));
//executes the "mb_substr" function to ensure a character is not truncated in the middle.
return mb_substr($sValue, 0, mb_strlen($sVal) - 1).$sMessage;
}
return $sValue;
}
public function GetSQLColumns($bFullSpec = false)
{
$aColumns = [];

View File

@@ -732,13 +732,8 @@ abstract class DBObject implements iDisplay
public function SetTrim($sAttCode, $sValue)
{
$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
$iMaxSize = $oAttDef->GetMaxSize();
$sLength = mb_strlen($sValue);
if ($iMaxSize && ($sLength > $iMaxSize)) {
$sMessage = " -truncated ($sLength chars)";
$sValue = mb_substr($sValue, 0, $iMaxSize - mb_strlen($sMessage)).$sMessage;
}
$this->Set($sAttCode, $sValue);
$this->Set($sAttCode, $oAttDef->TrimValue($sValue));
}
/**
@@ -2040,7 +2035,7 @@ abstract class DBObject implements iDisplay
}
}
if (!is_null($iMaxSize = $oAtt->GetMaxSize())) {
$iLen = mb_strlen($toCheck);
$iLen = $oAtt->GetSize($toCheck);
if ($iLen > $iMaxSize) {
return "String too long (found $iLen, limited to $iMaxSize)";
}

View File

@@ -1297,19 +1297,21 @@ class DBObjectTest extends ItopDataTestCase
{
return [
// UserRequest.title is an AttributeString (maxsize = 255)
'title 250 chars' => ['title', 250],
'title 254 chars' => ['title', 254],
'title 255 chars' => ['title', 255],
'title 256 chars' => ['title', 256],
'title 300 chars' => ['title', 300],
'title 250 chars' => ['title', 250, 250, true],
'title 254 chars' => ['title', 254, 254, true],
'title 255 chars' => ['title', 255, 255, true],
'title 256 chars' => ['title', 256, 255, false],
'title 300 chars' => ['title', 300, 255, false],
// UserRequest.pending_reason is an AttributeText (maxsize=65535) with format=text
'pending_reason 250 chars' => ['pending_reason', 250],
'pending_reason 60000 chars' => ['pending_reason', 60000],
'pending_reason 65534 chars' => ['pending_reason', 65534],
'pending_reason 65535 chars' => ['pending_reason', 65535],
'pending_reason 65536 chars' => ['pending_reason', 65536],
'pending_reason 70000 chars' => ['pending_reason', 70000],
'pending_reason 250 chars' => ['pending_reason', 250, 250, true],
'pending_reason 65534 chars' => ['pending_reason', 65534, 16403, false],
'pending_reason 65535 chars' => ['pending_reason', 65535, 16403, false],
'pending_reason 65536 chars' => ['pending_reason', 65536, 16403, false],
'pending_reason 16385 chars' => ['pending_reason', 16385, 16403, false],
'pending_reason 16384 chars' => ['pending_reason', 16384, 16383, true],
'pending_reason 16383 chars' => ['pending_reason', 16383, 16383, true],
'pending_reason 16382 chars' => ['pending_reason', 16382, 16382, true],
];
}
@@ -1322,7 +1324,7 @@ class DBObjectTest extends ItopDataTestCase
*
* @since 3.1.2 N°3448 - Framework field size check not correctly implemented for multi-bytes languages/strings
*/
public function testCheckLongValueInAttribute(string $sAttrCode, int $iValueLength)
public function testCheckLongValueInAttribute(string $sAttrCode, int $iValueLength, int $iExpectedLength, bool $bIsValueToSetBelowAttrMaxSize): void
{
$sPrefix = 'a'; // just a small prefix so that the emoji bytes won't have a power of 2 (we want a non even value)
$sEmojiToRepeat = '😎'; // this emoji is 4 bytes long
@@ -1343,7 +1345,6 @@ class DBObjectTest extends ItopDataTestCase
$oAttDef = MetaModel::GetAttributeDef(UserRequest::class, $sAttrCode);
$iAttrMaxSize = $oAttDef->GetMaxSize();
$bIsValueToSetBelowAttrMaxSize = ($iValueLength <= $iAttrMaxSize);
/** @noinspection PhpUnusedLocalVariableInspection */
[$bCheckStatus, $aCheckIssues, $bSecurityIssue] = $oTicket->CheckToWrite();
$this->assertEquals($bIsValueToSetBelowAttrMaxSize, $bCheckStatus, "CheckResult result:".var_export($aCheckIssues, true));
@@ -1353,7 +1354,7 @@ class DBObjectTest extends ItopDataTestCase
if ($bIsValueToSetBelowAttrMaxSize) {
$this->assertEquals($sValueToSet, $sValueInObject, 'Should not alter string that is already shorter than attribute max length');
} else {
$this->assertEquals($iAttrMaxSize, mb_strlen($sValueInObject), 'Should truncate at the same length than attribute max length');
$this->assertEquals($iExpectedLength, mb_strlen($sValueInObject), 'Should truncate at the same length than attribute max length');
$sLastCharsOfValueInObject = mb_substr($sValueInObject, -30);
$this->assertStringContainsString(' -truncated', $sLastCharsOfValueInObject, 'Should end with "truncated" comment');
}
@@ -1386,6 +1387,17 @@ class DBObjectTest extends ItopDataTestCase
$this->assertEquals($sResult, $oOrganisation->Get('name'), 'SetTrim must limit string to 255 characters');
}
/**
* @covers DBObject::SetTrim
*/
public function testSetTrimOnNonStringAttributeDoesNotTrim()
{
$oTicket = MetaModel::NewObject(UserRequest::class);
$oTicket->SetTrim('caller_id', '15');
$this->assertEquals(15, $oTicket->Get('caller_id'), 'SetTrim should keep non-string attributes untouched before regular Set conversion');
}
/**
* @covers DBObject::SetComputedDate
* @return void

View File

@@ -277,25 +277,13 @@ abstract class WebServicesBase
$oLog->Set('userinfo', UserRights::GetUser());
$oLog->Set('verb', $sVerb);
$oLog->Set('result', $oRes->IsOk());
$this->TrimAndSetValue($oLog, 'log_info', (string)$oRes->GetInfoAsText());
$this->TrimAndSetValue($oLog, 'log_warning', (string)$oRes->GetWarningsAsText());
$this->TrimAndSetValue($oLog, 'log_error', (string)$oRes->GetErrorsAsText());
$this->TrimAndSetValue($oLog, 'data', (string)$oRes->GetReturnedDataAsText());
$oLog->SetTrim('log_info', (string)$oRes->GetInfoAsText());
$oLog->SetTrim('log_warning', (string)$oRes->GetWarningsAsText());
$oLog->SetTrim('log_error', (string)$oRes->GetErrorsAsText());
$oLog->SetTrim('data', (string)$oRes->GetReturnedDataAsText());
$oLog->DBInsertNoReload();
}
protected function TrimAndSetValue($oLog, $sAttCode, $sValue)
{
$oAttDef = MetaModel::GetAttributeDef(get_class($oLog), $sAttCode);
if (is_object($oAttDef)) {
$iMaxSize = $oAttDef->GetMaxSize();
if ($iMaxSize && (mb_strlen($sValue) > $iMaxSize)) {
$sValue = mb_substr($sValue, 0, $iMaxSize);
}
$oLog->Set($sAttCode, $sValue);
}
}
/**
* Helper to set a scalar attribute
*