Merge remote-tracking branch 'origin/support/3.2' into develop

# Conflicts:
#	tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectTest.php
This commit is contained in:
jf-cbd
2025-03-13 15:57:15 +01:00
5 changed files with 75 additions and 6 deletions

View File

@@ -4465,7 +4465,7 @@ class AttributeText extends AttributeString
{
// Is there a way to know the current limitation for mysql?
// See mysql_field_len()
return 65535;
return 16383; // number of characters (that can be 1-4 bytes long), not of bytes
}
public static function RenderWikiHtml($sText, $bWikiOnly = false)

View File

@@ -760,10 +760,10 @@ 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)) {
if (!$this->StringFitsInField($sAttCode, $sValue)) {
$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
$iMaxSize = $oAttDef->GetMaxSize();
$sLength = mb_strlen($sValue);
$sMessage = " -truncated ($sLength chars)";
$sValue = mb_substr($sValue, 0, $iMaxSize - mb_strlen($sMessage)).$sMessage;
}
@@ -818,6 +818,24 @@ abstract class DBObject implements iDisplay
$oKPI->ComputeStatsForExtension($this, 'AfterDelete');
}
/**
* @param string $sAttCode
* @param string $sValue
*
* @return bool
* @throws \Exception
*
* @Since 3.2.2
*/
public function StringFitsInField(string $sAttCode, string $sValue): bool
{
$oAttDef = MetaModel::GetAttributeDef(get_class($this), $sAttCode);
$iMaxSize = $oAttDef->GetMaxSize();
$sLength = mb_strlen($sValue);
return !($iMaxSize && ($sLength > $iMaxSize));
}
/**
* Compute (and optionally start) the StopWatches deadlines
*

View File

@@ -2,11 +2,19 @@
namespace Combodo\iTop\Test\UnitTest\Core;
use ArchivedObjectException;
use AttributeDate;
use AttributeDateTime;
use Change;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use CoreCannotSaveObjectException;
use CoreException;
use CoreUnexpectedValue;
use CoreWarning;
use EventRestService;
use MetaModel;
use MySQLException;
use OQLException;
use UserRequest;
class AttributeDefinitionTest extends ItopDataTestCase {
@@ -343,4 +351,23 @@ PHP
return $oAttribute;
}
/**
* @throws CoreException
* @throws CoreUnexpectedValue
* @throws OQLException
* @throws ArchivedObjectException
* @throws CoreCannotSaveObjectException
* @throws CoreWarning
* @throws MySQLException
*/
public function testTrimLogOnAttributeText()
{
// will throw MySQLException if GetMaxSize() of AttributeText is incorrect (should be number of bytes, not of characters)
$oLog = new EventRestService();
$sLongString = json_encode(array_fill(0, 5000, 'é😃 '),
JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
$oLog->SetTrim('json_input', $sLongString);
static::assertNotEquals($oLog->Get('json_input'), $sLongString);
static::assertStringContainsString('truncated', $oLog->Get('json_input'));
}
}

View File

@@ -24,6 +24,7 @@ use AttributeDateTime;
use Combodo\iTop\Application\WebPage\iTopWebPage;
use Combodo\iTop\Application\WebPage\WebPage;
use Combodo\iTop\Service\Events\EventData;
use EventRestService;
use Combodo\iTop\Test\UnitTest\ItopDataTestCase;
use CoreException;
use DateTime;
@@ -1439,4 +1440,22 @@ class DBObjectTest extends ItopDataTestCase
$oUserRequest->Get('functionalcis_list');
});
}
public function testStringFitsInField()
{
//🎁 character is 4 bytes long
$sTooLongText = str_repeat('🎁', 17000);
$oLog = new EventRestService();
$this->assertFalse($oLog->StringFitsInField('json_output', $sTooLongText));
$sCorrectLengthText = str_repeat('🎁', 16383);
$this->assertTrue($oLog->StringFitsInField('json_output', $sCorrectLengthText));
$sCorrectLengthString = str_repeat('🎁', 255);
$this->assertTrue($oLog->StringFitsInField('operation', $sCorrectLengthString));
$sTooLongString = str_repeat('🎁', 256);
$this->assertFalse($oLog->StringFitsInField('operation', $sTooLongString));
}
}

View File

@@ -299,7 +299,12 @@ if (MetaModel::GetConfig()->Get('log_rest_service'))
$oLog->SetTrim('message', $sMessage);
$oLog->Set('code', $oResult->code);
$oResult->SanitizeContent();
$oLog->SetTrim('json_output', json_encode($oResult, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$iUnescapeSlashAndUnicode = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
$sJsonOuputWithPrettyPrinting = json_encode($oResult, $iUnescapeSlashAndUnicode | JSON_PRETTY_PRINT);
$sJsonOutputWithoutPrettyPrinting = json_encode($oResult, $iUnescapeSlashAndUnicode);
!$oLog->StringFitsInField('json_output', $sJsonOuputWithPrettyPrinting) ?
$oLog->SetTrim('json_output', $sJsonOutputWithoutPrettyPrinting) : // too long, we don't make it pretty
$oLog->SetTrim('json_output', $sJsonOuputWithPrettyPrinting);
$oLog->DBInsertNoReload();
}