Skip to content
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

FIX Use empty array as a fallback for preg_split within DBText summary #9696

Merged
merged 3 commits into from
Jun 3, 2021
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
10 changes: 5 additions & 5 deletions src/ORM/FieldType/DBText.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function LimitSentences($maxSentences = 2)
}

// Do a word-search
$words = preg_split('/\s+/u', $value);
$words = preg_split('/\s+/u', $value) ?: [];
$sentences = 0;
foreach ($words as $i => $word) {
if (preg_match('/(!|\?|\.)$/', $word) && !preg_match('/(Dr|Mr|Mrs|Ms|Miss|Sr|Jr|No)\.$/i', $word)) {
Expand Down Expand Up @@ -133,8 +133,8 @@ public function Summary($maxWords = 50, $add = false)
// Split on sentences (don't remove period)
$sentences = array_filter(array_map(function ($str) {
return trim($str);
}, preg_split('@(?<=\.)@', $value)));
$wordCount = count(preg_split('#\s+#u', $sentences[0]));
}, preg_split('@(?<=\.)@', $value) ?: []));
$wordCount = count(preg_split('#\s+#u', $sentences[0]) ?: []);

// if the first sentence is too long, show only the first $maxWords words
if ($wordCount > $maxWords) {
Expand All @@ -149,7 +149,7 @@ public function Summary($maxWords = 50, $add = false)

// If more sentences to process, count number of words
if ($sentences) {
$wordCount += count(preg_split('#\s+#u', $sentences[0]));
$wordCount += count(preg_split('#\s+#u', $sentences[0]) ?: []);
}
} while ($wordCount < $maxWords && $sentences && trim($sentences[0]));

Expand All @@ -169,7 +169,7 @@ public function FirstParagraph()
}

// Split paragraphs and return first
$paragraphs = preg_split('#\n{2,}#', $value);
$paragraphs = preg_split('#\n{2,}#', $value) ?: [];
return reset($paragraphs);
}

Expand Down
59 changes: 54 additions & 5 deletions tests/php/ORM/DBTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function providerLimitWordCount()
['The little brown fox jumped over the lazy cow.', 3, 'The little brown…'],
[' This text has white space around the ends ', 3, 'This text has…'],

// Words less than the limt word count don't get truncated, ellipsis not added
// Words less than the limit word count don't get truncated, ellipsis not added
['Two words', 3, 'Two words'], // Two words shouldn't have an ellipsis
['These three words', 3, 'These three words'], // Three words shouldn't have an ellipsis
['One', 3, 'One'], // Neither should one word
Expand Down Expand Up @@ -216,7 +216,7 @@ public function testFirstSentence($originalValue, $expectedValue)
}

/**
* each test is in the format input, charactere limit, highlight, expected output
* each test is in the format input, character limit, highlight, expected output
*
* @return array
*/
Expand Down Expand Up @@ -268,18 +268,53 @@ public function providerContextSummary()
'both schön and können have umlauts',
21,
'',
// check non existant search term
// check non-existent search term
'both schön and können…',
]
];
}


/**
* each test is in the format input, word limit, add ellipsis (false or string), expected output
*
* @return array
*/
public function providerSummary()
{
return [
[
'This is some text. It is a test',
3,
false,
'This is some…',
],
[
// check custom ellipsis
'This is a test text in a longer sentence and a custom ellipsis.',
8,
'...', // regular dots instead of the ellipsis character
'This is a test text in a longer...',
],
[
'both schön and können have umlauts',
5,
false,
'both schön and können have…',
],
[
// check invalid UTF8 handling — input is an invalid UTF sequence, output should be empty string
"\xf0\x28\x8c\xbc",
50,
false,
'',
],
];
}

/**
* @dataProvider providerContextSummary
* @param string $originalValue Input
* @param int $limit Numer of characters
* @param int $limit Number of characters
* @param string $keywords Keywords to highlight
* @param string $expectedValue Expected output (XML encoded safely)
*/
Expand Down Expand Up @@ -352,4 +387,18 @@ public function testDefaultEllipsis()
$textObj = new DBText('Test');
$this->assertEquals('…', $textObj->defaultEllipsis());
}

/**
* @dataProvider providerSummary
* @param string $originalValue Input
* @param int $words Number of words
* @param mixed $add Ellipsis (false for default or string for custom text)
* @param string $expectedValue Expected output (XML encoded safely)
*/
public function testSummary($originalValue, $words, $add, $expectedValue)
{
$text = DBField::create_field(DBText::class, $originalValue);
$result = $text->obj('Summary', [$words, $add])->forTemplate();
$this->assertEquals($expectedValue, $result);
}
}