diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 3b2ec783676..dcf6f9314f2 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1017,7 +1017,21 @@ class navigation_node implements renderable { ); } } + } + /** + * Reset all static data. + * + * @throws coding_exception if called outside of a unit test + */ + public static function reset_all_data(): void { + if (!defined('PHPUNIT_TEST') || !PHPUNIT_TEST) { + throw new coding_exception('Resetting all data is not allowed outside of PHPUnit tests.'); + } + + self::$fullmeurl = null; + self::$autofindactive = true; + self::$loadadmintree = false; } } diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index 3520d19c27f..86b52503aa3 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -205,6 +205,7 @@ class phpunit_util extends testing_util { // reinitialise following globals $OUTPUT = new bootstrap_renderer(); $PAGE = new moodle_page(); + \navigation_node::reset_all_data(); $FULLME = null; $ME = null; $SCRIPT = null; diff --git a/lib/phpunit/tests/basic_test.php b/lib/phpunit/tests/basic_test.php index 0fdb19db168..bb10b9c3471 100644 --- a/lib/phpunit/tests/basic_test.php +++ b/lib/phpunit/tests/basic_test.php @@ -377,4 +377,25 @@ STRING; $this->assertFalse($DB->is_transaction_started()); $this->assertEquals($originalname, $DB->get_field('course', 'fullname', ['id' => $COURSE->id])); } + + /** + * Test that the navigation node URL is overridden correctly. + */ + public function test_set_navigation_url(): void { + \navigation_node::override_active_url(new \moodle_url('/foo/bar/baz')); + $this->assertNotNull( + (new \ReflectionClass(\navigation_node::class))->getStaticPropertyValue('fullmeurl', null), + ); + } + + /** + * Test that the after-test teardown correctly resets the navigation node URL. + * + * @depends test_set_navigation_url + */ + public function test_navigation_url_reset(): void { + $this->assertNull( + (new \ReflectionClass(\navigation_node::class))->getStaticPropertyValue('fullmeurl', null), + ); + } } diff --git a/lib/tests/navigationlib_test.php b/lib/tests/navigationlib_test.php index 0c6f48ceb04..c02ad340686 100644 --- a/lib/tests/navigationlib_test.php +++ b/lib/tests/navigationlib_test.php @@ -48,6 +48,9 @@ final class navigationlib_test extends \advanced_testcase { protected function setup_node() { global $PAGE, $SITE; + // Perform a reset between tests to reset the PAGE. + $this->resetAfterTest(); + $PAGE->set_url('/'); $PAGE->set_course($SITE);