From a7ad7397463dcf962a6ddc6ce45d2dcade78dfa4 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Fri, 20 Mar 2026 01:20:01 +1100 Subject: [PATCH] MDL-87580 task: Fix task output autolinking --- admin/tool/task/lib.php | 2 +- admin/tool/task/tests/lib_test.php | 82 ++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 admin/tool/task/tests/lib_test.php diff --git a/admin/tool/task/lib.php b/admin/tool/task/lib.php index c1813db730f..980d4ffd1ff 100644 --- a/admin/tool/task/lib.php +++ b/admin/tool/task/lib.php @@ -49,7 +49,7 @@ function tool_task_mtrace_wrapper(string $message, string $eol = ''): void { // We autolink urls and emails here but can't use format_text as it does // more than we need and has side effects which are not useful in this context. - $urlpattern = '/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/'; + $urlpattern = '~\b(?:https?|ftps?)://[a-z0-9-]+(?:\.[a-z0-9-]+)*(?::\d+)?(?:/[^\s<]*)?~i'; $message = preg_replace_callback($urlpattern, function($matches) { $url = $matches[0]; return html_writer::link($url, $url, ['target' => '_blank']); diff --git a/admin/tool/task/tests/lib_test.php b/admin/tool/task/tests/lib_test.php new file mode 100644 index 00000000000..d3577a6a459 --- /dev/null +++ b/admin/tool/task/tests/lib_test.php @@ -0,0 +1,82 @@ +. + +namespace tool_task; + +/** + * Test for the lib class. + * + * @package tool_task + * @copyright 2026 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class lib_test extends \advanced_testcase { + /** + * Data provider for mtrace + * + * @return array + */ + public static function tool_task_mtrace_wrapper_provider(): array { + return [ + [ + 'A url http://moodle.com', + 'A url http://moodle.com', + ], + [ + 'A url https://moodle.com', + 'A url https://moodle.com', + ], + [ + 'A url https://moodle.com post text', + 'A url https://moodle.com post text', + ], + [ + 'A url https://moodle.com. In a paragraph', + 'A url https://moodle.com. In a paragraph', + ], + [ + 'A url https://localhost post text', + 'A url https://localhost post text', + ], + [ + 'A url https://main.localhost post text', + 'A url https://main.localhost post text', + ], + [ + 'email info@moodle.com after', + 'email info@moodle.com after', + ], + [ + 'A sentence that ends in info@moodle.com. With another sentence.', + 'A sentence that ends in info@moodle.com. With another sentence.', + ], + ]; + } + /** + * Test validations for minute field. + * @dataProvider tool_task_mtrace_wrapper_provider + * @param string $output task output + * @param string $expected html + * @covers ::tool_task_mtrace_wrapper + */ + public function test_tool_task_mtrace_wrapper(string $output, string $expected): void { + global $CFG; + require_once("{$CFG->dirroot}/{$CFG->admin}/tool/task/lib.php"); + + $this->expectOutputString($expected); + $result = tool_task_mtrace_wrapper($output); + } +}