From fdd88910912c3fff0c78db3398ae6e6838f4f210 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 27 Feb 2025 20:14:07 +0800 Subject: [PATCH] MDL-84543 core: Basepath guess should work on root domains --- lib/classes/router.php | 2 +- lib/tests/router_test.php | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/classes/router.php b/lib/classes/router.php index c654042f22e..f74ffa86834 100644 --- a/lib/classes/router.php +++ b/lib/classes/router.php @@ -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), '', diff --git a/lib/tests/router_test.php b/lib/tests/router_test.php index c8ccb54c7b4..980f0e4fc51 100644 --- a/lib/tests/router_test.php +++ b/lib/tests/router_test.php @@ -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 {