[ticket/17387] Add more unicode tests

PHPBB-17387
This commit is contained in:
rxu 2024-09-03 00:04:25 +07:00
parent c26ded6025
commit 544e0900e6
No known key found for this signature in database
GPG key ID: 955F0567380E586A
2 changed files with 104 additions and 2 deletions

View file

@ -401,13 +401,13 @@ function get_context(string $text, array $words, int $length = 400): string
// Find the first valid alphanumeric character in the fragment to don't cut words // Find the first valid alphanumeric character in the fragment to don't cut words
if ($start > 0 && preg_match('/[^\p{L}\p{N}][\p{L}\p{N}]/u', $fragment, $matches, PREG_OFFSET_CAPTURE)) if ($start > 0 && preg_match('/[^\p{L}\p{N}][\p{L}\p{N}]/u', $fragment, $matches, PREG_OFFSET_CAPTURE))
{ {
$fragment_start = mb_strlen(substr($fragment, 0, (int) $matches[0][1])) + 1; $fragment_start = utf8_strlen(substr($fragment, 0, (int) $matches[0][1])) + 1;
} }
// Find the last valid alphanumeric character in the fragment to don't cut words // Find the last valid alphanumeric character in the fragment to don't cut words
if ($end < $text_length - 1 && preg_match_all('/[\p{L}\p{N}][^\p{L}\p{N}]/u', $fragment, $matches, PREG_OFFSET_CAPTURE)) if ($end < $text_length - 1 && preg_match_all('/[\p{L}\p{N}][^\p{L}\p{N}]/u', $fragment, $matches, PREG_OFFSET_CAPTURE))
{ {
$fragment_end = mb_strlen(substr($fragment, 0, end($matches[0])[1])); $fragment_end = utf8_strlen(substr($fragment, 0, end($matches[0])[1]));
} }
$output[] = utf8_substr($fragment, $fragment_start, $fragment_end - $fragment_start + 1); $output[] = utf8_substr($fragment, $fragment_start, $fragment_end - $fragment_start + 1);

View file

@ -113,6 +113,107 @@ class phpbb_functions_content_get_context_test extends TestCase
'length' => 10, 'length' => 10,
'expected' => 'word1 ... word2', 'expected' => 'word1 ... word2',
], ],
];
}
/**
* Data provider for unicode get_context test cases.
*
* @return array
*/
public function data_get_context_unicode(): array
{
return [
'text contains words and length greater than text' => [
'text' => 'Это пример текста, содержащего разнообразные слова, включая пример, текст и слова.',
'words' => ['пример', 'слова'],
'length' => 100,
'expected' => 'Это пример текста, содержащего разнообразные слова, включая пример, текст и слова.',
],
'text contains words and length less than text' => [
'text' => 'Это пример текста, содержащего разнообразные слова, включая шаблон, текст и слова.',
'words' => ['пример', 'слова'],
'length' => 50,
'expected' => 'Это пример текста, содержащего разнообразные слова ...',
],
'text does not contain words' => [
'text' => 'Это пример текста, содержащего разнообразные слова, но ни одно из них не совпадает с искомыми.',
'words' => ['nonexistent'],
'length' => 50,
'expected' => 'Это пример текста, содержащего разнообразные слова ...',
],
'desired length equal to text length' => [
'text' => 'Текст точной длины.',
'words' => ['Текст', 'точной'],
'length' => 19,
'expected' => 'Текст точной длины.',
],
'text with html entities' => [
'text' => 'Это пример текста, содержащего &amp; и &lt; и &gt; лексемы.',
'words' => ['пример', 'содержащего'],
'length' => 40,
'expected' => 'Это пример текста, содержащего &amp; и &lt; и ...',
],
'text with html entities and contains last word' => [
'text' => 'Это пример текста, содержащего &amp; и &lt; и &gt; лексемы.',
'words' => ['пример', 'лексемы'],
'length' => 40,
'expected' => 'Это пример текста ... и &lt; и &gt; лексемы.',
],
'text with multiple spaces and special characters' => [
'text' => 'Это пример текста, содержащего разнообразные слова.',
'words' => ['пример', 'разнообразные'],
'length' => 50,
'expected' => 'Это пример текста, содержащего разнообразные слова.',
],
'empty text' => [
'text' => '',
'words' => ['пример', 'слова'],
'length' => 50,
'expected' => '',
],
'empty words array' => [
'text' => 'Это пример текста, содержащего разнообразные слова.',
'words' => [],
'length' => 50,
'expected' => 'Это пример текста, содержащего разнообразные слова.',
],
'zero length' => [
'text' => 'Это пример текста.',
'words' => ['пример'],
'length' => 0,
'expected' => 'Это пример текста.',
],
'negative length' => [
'text' => 'Это пример текста.',
'words' => ['sample'],
'length' => -10,
'expected' => 'Это пример текста.',
],
'ellipses_beginning' => [
'text' => 'раз раз раз раз раз раз раз раз два',
'words' => ['два'],
'length' => 10,
'expected' => '... раз раз два',
],
'ellipsis_end' => [
'text' => 'два раз раз раз раз раз раз раз раз',
'words' => ['два'],
'length' => 10,
'expected' => 'два раз раз ...',
],
'ellipsis_middle' => [
'text' => 'раз слово1 раз раз раз раз раз раз раз раз раз слово2 раз',
'words' => ['слово1', 'слово2'],
'length' => 15,
'expected' => '... слово1 ... слово2 ...',
],
'ellipsis_middle2' => [
'text' => 'слово1 foo foo foo foo foo foo foo foo foo слово2',
'words' => ['слово1', 'слово2'],
'length' => 10,
'expected' => 'слово1 ... слово2',
],
'fruits_spanish' => [ 'fruits_spanish' => [
'text' => 'Manzana,plátano,naranja,fresa,mango,uva,piña,pera,kiwi,cereza,sandía,melón,papaya,arándano,durazno', 'text' => 'Manzana,plátano,naranja,fresa,mango,uva,piña,pera,kiwi,cereza,sandía,melón,papaya,arándano,durazno',
'words' => ['piña'], 'words' => ['piña'],
@ -124,6 +225,7 @@ class phpbb_functions_content_get_context_test extends TestCase
/** /**
* @dataProvider data_get_context * @dataProvider data_get_context
* @dataProvider data_get_context_unicode
*/ */
public function test_get_context($text, $words, $length, $expected) public function test_get_context($text, $words, $length, $expected)
{ {