Skip to content

Do not automatically truncate text fields #19820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
71 changes: 0 additions & 71 deletions phpunit/functional/CommonDBTMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1327,77 +1327,6 @@ public function testGetById()
$this->assertFalse($output);
}


public static function textValueProvider(): iterable
{
$value = 'This is not a long value';
yield [
'value' => $value,
'truncated' => $value,
'length' => 24,
];

// 500 1-byte chars
// truncated string should contains 255 1-byte chars
yield [
'value' => str_repeat('12345', 100),
'truncated' => str_repeat('12345', 51), // 5 * 51 = 255
'length' => 500,
];

// value that have a `\` as 255th char
yield [
'value' => str_repeat('a', 254) . '\\abcdefg',
'truncated' => str_repeat('a', 254) . '\\',
'length' => 262,
];

// 253 1-byte chars followed by a 4-bytes char
// string should not be truncated because the size in the database is expressed in number of characters and not in bytes
$value = str_repeat('x', 253) . '𝄠';
yield [
'value' => $value,
'truncated' => $value,
'length' => 254,
];

// 224 (7 * 32) 4-bytes chars
// string should not be truncated because the size in the database is expressed in number of characters and not in bytes
$value = str_repeat('🂧🂨🂩🂪🂫🂭🂮🂡🂷🂸🂹🂺🂻🂽🂾🂱🃇🃈🃉🃊🃋🃍🃎🃁🃗🃘🃙🃚🃛🃝🃞🃑', 7);
yield [
'value' => $value,
'truncated' => $value,
'length' => 224,
];

// 500 4-bytes chars
// truncated string should contains 255 4-bytes chars
yield [
'value' => str_repeat('🂡🂢🂣🂤🂥', 100),
'truncated' => str_repeat('🂡🂢🂣🂤🂥', 51), // 5 * 51 = 255
'length' => 500,
];
}

#[DataProvider('textValueProvider')]
public function testTextValueTuncation(string $value, string $truncated, int $length)
{
$computer = new \Computer();

$this->assertGreaterThan(0, $computer->add(['name' => $value, 'entities_id' => 0]));
$this->assertEquals($truncated, $computer->fields['name']);
if ($value !== $truncated) {
$this->hasPhpLogRecordThatContains(
sprintf(
'%s exceed 255 characters long (%s), it will be truncated.',
$value,
$length
),
LogLevel::WARNING
);
}
}

public function testCheckUnicity()
{
$this->login();
Expand Down
15 changes: 0 additions & 15 deletions src/CommonDBTM.php
Original file line number Diff line number Diff line change
Expand Up @@ -4419,21 +4419,6 @@ public function filterValues($display = true)
}
break;

case 'email':
case 'string':
case 'itemlink':
// Some 'completename' fields may be typed as 'itemlink' (e.g., in CommonTreeDropdown::class).
// These should be excluded from the length check, as they are stored as TEXT or MEDIUMTEXT,
// not VARCHAR. These fields are used solely as item links, not as standard string fields.
if ($key !== 'completename' && is_string($value) && ($length = mb_strlen($value, 'UTF-8')) > 255) {
trigger_error(
"{$value} exceed 255 characters long ({$length}), it will be truncated.",
E_USER_WARNING
);
$this->input[$key] = mb_substr($value, 0, 255, 'UTF-8');
}
break;

default:
//Plugins can implement their own checks
if (!$this->checkSpecificValues($searchOption['datatype'], $value)) {
Expand Down