MDL-72783 usertours: Improve the tours maching

Improve the regex so the Tour can detect the URL match is pointing to Dashboard (/my/) or My Course (/my/courses.php) correctly
This commit is contained in:
Huong Nguyen 2021-12-17 11:01:31 +07:00 committed by Shamim Rezaie
parent 524f75797a
commit 87f233e698
2 changed files with 13 additions and 4 deletions

View File

@ -84,13 +84,22 @@ EOF;
// Attempt to determine whether this is the front page.
// This is a special case because the frontpage uses a shortened page path making it difficult to detect exactly.
$isfrontpage = $targetmatch->compare(new \moodle_url('/'), URL_MATCH_BASE);
$isdashboard = $targetmatch->compare(new \moodle_url('/my/'), URL_MATCH_BASE);
$target = $targetmatch->out_as_local_url();
return array_filter($tours, function($tour) use ($isfrontpage, $target) {
if ($isfrontpage && $tour->pathmatch === 'FRONTPAGE') {
return array_filter($tours, function($tour) use ($isfrontpage, $isdashboard, $target) {
if (($isfrontpage || $isdashboard) && $tour->pathmatch === 'FRONTPAGE') {
return true;
}
$pattern = preg_quote($tour->pathmatch, '@');
$pattern = str_replace('%', '.*', $pattern);
if (strpos($pattern, '%') !== false) {
// The URL match format is something like: /my/%.
// We need to find all the URLs which match the first part of the pattern.
$pattern = str_replace('%', '.*', $pattern);
} else {
// The URL match format is something like: /my/courses.php.
// We need to find all the URLs which match with whole pattern.
$pattern .= '$';
}
return !!preg_match("@{$pattern}@", $target);
});
}

View File

@ -164,7 +164,7 @@ class cache_testcase extends advanced_testcase {
'Matches expected glob' => [
$tourconfigs,
'/my/index.php',
['my_glob_1', 'my_glob_2'],
['my_glob_1', 'my_glob_2', 'frontpage_match'],
],
'Matches expected glob and exact' => [
$tourconfigs,