diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 95a5642f681..7c6da7a43f5 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -998,7 +998,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 5c9d9cca89a..9d5acc4bae0 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -199,6 +199,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 839b1917d17..3c3c2a02690 100644 --- a/lib/phpunit/tests/basic_test.php +++ b/lib/phpunit/tests/basic_test.php @@ -145,6 +145,27 @@ STRING; self::assertTag(['id' => 'testid'], "
"); } + /** + * 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) + ); + } + // Uncomment following tests to see logging of unexpected changes in global state and database. /* public function test_db_modification() { diff --git a/lib/tests/navigationlib_test.php b/lib/tests/navigationlib_test.php index 8d43958af9d..7713656f729 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);