From 544e0900e6d8aa24a302f5d80a9325af589bdcf8 Mon Sep 17 00:00:00 2001 From: rxu Date: Tue, 3 Sep 2024 00:04:25 +0700 Subject: [PATCH] [ticket/17387] Add more unicode tests PHPBB-17387 --- phpBB/includes/functions_content.php | 4 +- tests/functions_content/get_context_test.php | 102 +++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index 9034f785ff..d50a515c87 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -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 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 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); diff --git a/tests/functions_content/get_context_test.php b/tests/functions_content/get_context_test.php index 93136d69fb..f2865f31ee 100644 --- a/tests/functions_content/get_context_test.php +++ b/tests/functions_content/get_context_test.php @@ -113,6 +113,107 @@ class phpbb_functions_content_get_context_test extends TestCase 'length' => 10, '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' => 'Это пример текста, содержащего & и < и > лексемы.', + 'words' => ['пример', 'содержащего'], + 'length' => 40, + 'expected' => 'Это пример текста, содержащего & и < и ...', + ], + 'text with html entities and contains last word' => [ + 'text' => 'Это пример текста, содержащего & и < и > лексемы.', + 'words' => ['пример', 'лексемы'], + 'length' => 40, + 'expected' => 'Это пример текста ... и < и > лексемы.', + ], + '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' => [ 'text' => 'Manzana,plátano,naranja,fresa,mango,uva,piña,pera,kiwi,cereza,sandía,melón,papaya,arándano,durazno', 'words' => ['piña'], @@ -124,6 +225,7 @@ class phpbb_functions_content_get_context_test extends TestCase /** * @dataProvider data_get_context + * @dataProvider data_get_context_unicode */ public function test_get_context($text, $words, $length, $expected) {