[ticket/299] Add tests for phpbb_fetch_posts

B3P-299
This commit is contained in:
Marc Alexander
2014-07-08 18:45:49 +02:00
parent cefd447a67
commit aeb912a559
3 changed files with 276 additions and 70 deletions

View File

@@ -48,76 +48,7 @@ class phpbb_unit_functions_functions_test extends \board3\portal\tests\testframe
*/
public function test_character_limit($expected, $input, $length)
{
$this->markTestIncomplete('Cannot test this due to issues with censor_text() and truncate_string()');
$this->assertSame($expected, character_limit($input, $length));
}
}
function censor_text($text)
{
$text = trim(str_replace('disallowed_word', '', $text));
return $text;
}
/**
* Truncates string while retaining special characters if going over the max length
* The default max length is 60 at the moment
* The maximum storage length is there to fit the string within the given length. The string may be further truncated due to html entities.
* For example: string given is 'a "quote"' (length: 9), would be a stored as 'a "quote"' (length: 19)
*
* @param string $string The text to truncate to the given length. String is specialchared.
* @param int $max_length Maximum length of string (multibyte character count as 1 char / Html entity count as 1 char)
* @param int $max_store_length Maximum character length of string (multibyte character count as 1 char / Html entity count as entity chars).
* @param bool $allow_reply Allow Re: in front of string
* NOTE: This parameter can cause undesired behavior (returning strings longer than $max_store_length) and is deprecated.
* @param string $append String to be appended
*/
function truncate_string($string, $max_length = 60, $max_store_length = 255, $allow_reply = false, $append = '')
{
$chars = array();
$strip_reply = false;
$stripped = false;
if ($allow_reply && strpos($string, 'Re: ') === 0)
{
$strip_reply = true;
$string = substr($string, 4);
}
$_chars = utf8_str_split(htmlspecialchars_decode($string));
$chars = array_map('utf8_htmlspecialchars', $_chars);
// Now check the length ;)
if (sizeof($chars) > $max_length)
{
// Cut off the last elements from the array
$string = implode('', array_slice($chars, 0, $max_length - utf8_strlen($append)));
$stripped = true;
}
// Due to specialchars, we may not be able to store the string...
if (utf8_strlen($string) > $max_store_length)
{
// let's split again, we do not want half-baked strings where entities are split
$_chars = utf8_str_split(htmlspecialchars_decode($string));
$chars = array_map('utf8_htmlspecialchars', $_chars);
do
{
array_pop($chars);
$string = implode('', $chars);
}
while (!empty($chars) && utf8_strlen($string) > $max_store_length);
}
if ($strip_reply)
{
$string = 'Re: ' . $string;
}
if ($append != '' && $stripped)
{
$string = $string . $append;
}
return $string;
}