diff --git a/privacy/classes/local/request/moodle_content_writer.php b/privacy/classes/local/request/moodle_content_writer.php index c212e271df8..2c642512bad 100644 --- a/privacy/classes/local/request/moodle_content_writer.php +++ b/privacy/classes/local/request/moodle_content_writer.php @@ -272,7 +272,9 @@ class moodle_content_writer implements content_writer { // Join the directory together with the name. $filepath = implode(DIRECTORY_SEPARATOR, $path) . DIRECTORY_SEPARATOR . $name; - return preg_replace('@' . DIRECTORY_SEPARATOR . '+@', DIRECTORY_SEPARATOR, $filepath); + // To use backslash, it must be doubled ("\\\\" PHP string). + $separator = str_replace('\\', '\\\\', DIRECTORY_SEPARATOR); + return preg_replace('@(' . $separator . '|/)+@', $separator, $filepath); } /** @@ -291,7 +293,9 @@ class moodle_content_writer implements content_writer { // Join the directory together with the name. $filepath = implode(DIRECTORY_SEPARATOR, $path); - return preg_replace('@' . DIRECTORY_SEPARATOR . '+@', DIRECTORY_SEPARATOR, $filepath); + // To use backslash, it must be doubled ("\\\\" PHP string). + $separator = str_replace('\\', '\\\\', DIRECTORY_SEPARATOR); + return preg_replace('@(' . $separator . '|/)+@', $separator, $filepath); } /** diff --git a/privacy/tests/moodle_content_writer_test.php b/privacy/tests/moodle_content_writer_test.php index 7e20f64879b..66e6759b8f1 100644 --- a/privacy/tests/moodle_content_writer_test.php +++ b/privacy/tests/moodle_content_writer_test.php @@ -1162,12 +1162,18 @@ class moodle_content_writer_test extends advanced_testcase { if (null === $subcontext) { $rcm = $rc->getMethod('get_context_path'); $rcm->setAccessible(true); - return $rcm->invoke($writer); + $path = $rcm->invoke($writer); } else { $rcm = $rc->getMethod('get_path'); $rcm->setAccessible(true); - return $rcm->invoke($writer, $subcontext, $name); + $path = $rcm->invoke($writer, $subcontext, $name); } + + // PHPUnit uses mikey179/vfsStream which is a stream wrapper for a virtual file system that uses '/' + // as the directory separator. + $path = str_replace(DIRECTORY_SEPARATOR, '/', $path); + + return $path; } /**