MDL-87580 task: Fix task output autolinking
This commit is contained in:
@@ -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']);
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace tool_task;
|
||||
|
||||
/**
|
||||
* Test for the lib class.
|
||||
*
|
||||
* @package tool_task
|
||||
* @copyright 2026 Brendan Heywood <[email protected]>
|
||||
* @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 <a target="_blank" href="http://moodle.com">http://moodle.com</a>',
|
||||
],
|
||||
[
|
||||
'A url https://moodle.com',
|
||||
'A url <a target="_blank" href="https://moodle.com">https://moodle.com</a>',
|
||||
],
|
||||
[
|
||||
'A url https://moodle.com post text',
|
||||
'A url <a target="_blank" href="https://moodle.com">https://moodle.com</a> post text',
|
||||
],
|
||||
[
|
||||
'A url https://moodle.com. In a paragraph',
|
||||
'A url <a target="_blank" href="https://moodle.com">https://moodle.com</a>. In a paragraph',
|
||||
],
|
||||
[
|
||||
'A url https://localhost post text',
|
||||
'A url <a target="_blank" href="https://localhost">https://localhost</a> post text',
|
||||
],
|
||||
[
|
||||
'A url https://main.localhost post text',
|
||||
'A url <a target="_blank" href="https://main.localhost">https://main.localhost</a> post text',
|
||||
],
|
||||
[
|
||||
'email [email protected] after',
|
||||
'email <a href="mailto:[email protected]">[email protected]</a> after',
|
||||
],
|
||||
[
|
||||
'A sentence that ends in [email protected]. With another sentence.',
|
||||
'A sentence that ends in <a href="mailto:[email protected]">[email protected]</a>. 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user