[ticket/16549] Fix tests

1. If $service array is empty, $service[0] call will throw fatal error in PHP 8.
2. Division by zero was promoted to fatal error
and throws DivisionByZeroError exception in PHP 8+
3. Adjust make_clickable() logic to avoid 'Undefined array key 3' error.

PHPBB3-16549
This commit is contained in:
rxu 2020-10-18 14:47:03 +07:00
parent fff200b1d8
commit fe38280a1a
No known key found for this signature in database
GPG key ID: 955F0567380E586A
3 changed files with 8 additions and 6 deletions

View file

@ -1011,7 +1011,7 @@ function make_clickable($text, $server_url = false, string $class = 'postlink')
if (preg_match($magic_args[0], $text, $matches))
{
// Only apply $class from the corresponding function call argument (excepting emails which never has a class)
if ($magic_args[3] != $static_class && $magic_args[1] != MAGIC_URL_EMAIL)
if ($magic_args[1] != MAGIC_URL_EMAIL && $magic_args[3] != $static_class)
{
continue;
}

View file

@ -28,7 +28,8 @@ class phpbb_error_collector_test extends phpbb_test_case
$collector->install();
// Cause a warning
1/0; $line = __LINE__;
// Division by zero was promoted to fatal error and throws DivisionByZeroError exception in PHP 8+
version_compare(PHP_VERSION, '8', '>=') ? '1b'['0xFF'] : 1/0; $line = __LINE__;
$collector->uninstall();
@ -39,7 +40,7 @@ class phpbb_error_collector_test extends phpbb_test_case
// Unfortunately $error_contents will contain the full path here,
// because the tests directory is outside of phpbb root path.
$this->assertStringStartsWith('Errno 2: Division by zero at ', $error_contents);
$this->assertStringStartsWith(version_compare(PHP_VERSION, '8', '>=') ? 'Errno 2: Illegal string offset "0xFF" at ' : 'Errno 2: Division by zero at ', $error_contents);
$this->assertStringEndsWith(" line $line", $error_contents);
}
@ -49,7 +50,8 @@ class phpbb_error_collector_test extends phpbb_test_case
$collector->install();
// Cause a warning
1/0; $line = __LINE__;
// Division by zero was promoted to fatal error and throws DivisionByZeroError exception in PHP 8+
version_compare(PHP_VERSION, '8', '>=') ? '1b'['0xFF'] : 1/0; $line = __LINE__;
// Cause a "Notice: unserialize(): Error at offset 0 of 27 bytes in ..."
// "Undefined array index" used earlier was promoted to warning in PHP 8.0,
@ -69,7 +71,7 @@ class phpbb_error_collector_test extends phpbb_test_case
// Unfortunately $error_contents will contain the full path here,
// because the tests directory is outside of phpbb root path.
$this->assertStringStartsWith('Errno 2: Division by zero at ', $error_contents);
$this->assertStringStartsWith(version_compare(PHP_VERSION, '8', '>=') ? 'Errno 2: Illegal string offset "0xFF" at ' : 'Errno 2: Division by zero at ', $error_contents);
$this->assertStringEndsWith(" line $line", $error_contents);
}
}

View file

@ -59,7 +59,7 @@ class phpbb_mock_container_builder implements ContainerInterface
if ($this->has($id))
{
$service = $this->services[$id];
if (is_array($service) && is_callable($service[0]))
if (is_array($service) && isset($service[0]) && is_callable($service[0]))
{
return call_user_func_array($service[0], $service[1]);
}