MDL-84543 core: Basepath guess should work on root domains

This commit is contained in:
Andrew Nicols 2025-02-27 20:14:07 +08:00
parent ae31be1e34
commit d68cf00b25
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
2 changed files with 17 additions and 4 deletions

View File

@ -91,7 +91,7 @@ class router {
// Moodle is not guaranteed to exist at the domain root.
// Strip out the current script.
$scriptroot = parse_url($CFG->wwwroot, PHP_URL_PATH);
$scriptroot = parse_url($CFG->wwwroot, PHP_URL_PATH) ?? '';
$scriptfile = str_replace(
realpath($CFG->dirroot),
'',

View File

@ -88,13 +88,26 @@ final class router_test extends route_testcase {
$this->assertEquals('/example', $router->basepath);
}
public function test_basepath_guessed(): void {
/**
* @dataProvider basepath_provider
*/
public function test_basepath(
string $wwwroot,
string $expected,
): void {
global $CFG;
$wwwroot = new \moodle_url($CFG->wwwroot);
$this->resetAfterTest();
$CFG->wwwroot = $wwwroot;
$router = di::get(router::class);
$this->assertEquals($wwwroot->get_path(), $router->basepath);
$this->assertEquals($expected, $router->basepath);
}
public static function basepath_provider(): \Iterator {
yield 'Domain' => ['http://example.com', ''];
yield 'Subdirectory' => ['http://example.com/moodle', '/moodle'];
}
public function test_basepath_guessed_rphp(): void {