From 028728b561d4850bde3b0f0d94ffd94dc494d341 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 27 Aug 2025 14:35:48 +0800 Subject: [PATCH] MDL-86452 core: Reset navigation data between tests --- lib/navigationlib.php | 14 ++++++++++++++ lib/phpunit/classes/util.php | 1 + lib/phpunit/tests/basic_test.php | 21 +++++++++++++++++++++ lib/tests/navigationlib_test.php | 3 +++ 4 files changed, 39 insertions(+) diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 267675de75e..9a5e411f21e 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..a816f74d98b 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 \core\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 091251d9658..a270dec0579 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);