From fe38280a1a3336c38297d5004d483940a30d1915 Mon Sep 17 00:00:00 2001 From: rxu Date: Sun, 18 Oct 2020 14:47:03 +0700 Subject: [PATCH] [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 --- phpBB/includes/functions_content.php | 2 +- tests/error_collector_test.php | 10 ++++++---- tests/mock/container_builder.php | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/phpBB/includes/functions_content.php b/phpBB/includes/functions_content.php index bfd6b179cd..28d07bac7d 100644 --- a/phpBB/includes/functions_content.php +++ b/phpBB/includes/functions_content.php @@ -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; } diff --git a/tests/error_collector_test.php b/tests/error_collector_test.php index e1bbd89f12..6073eac6fe 100644 --- a/tests/error_collector_test.php +++ b/tests/error_collector_test.php @@ -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); } } diff --git a/tests/mock/container_builder.php b/tests/mock/container_builder.php index 17c4ae22dd..31513d0f31 100644 --- a/tests/mock/container_builder.php +++ b/tests/mock/container_builder.php @@ -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]); }