diff --git a/admin/tool/usertours/classes/manager.php b/admin/tool/usertours/classes/manager.php index 40758355454..a9c6f9bc9ea 100644 --- a/admin/tool/usertours/classes/manager.php +++ b/admin/tool/usertours/classes/manager.php @@ -622,7 +622,26 @@ class manager { * @return array */ public static function get_matching_tours(\moodle_url $pageurl): array { - global $PAGE; + global $PAGE, $USER; + + // The following three checks make sure that the user is fully ready to use the site. If not, we do not show any tours. + // We need the user to get properly set up so that all require_login() and other bits work as expected. + + if (user_not_fully_set_up($USER)) { + return []; + } + + if (get_user_preferences('auth_forcepasswordchange', false)) { + return []; + } + + if (empty($USER->policyagreed) && !is_siteadmin()) { + $manager = new \core_privacy\local\sitepolicy\manager(); + + if ($manager->is_defined(isguestuser())) { + return []; + } + } $tours = cache::get_matching_tourdata($pageurl); diff --git a/admin/tool/usertours/tests/manager_test.php b/admin/tool/usertours/tests/manager_test.php index 0deff0da644..2157581d0a4 100644 --- a/admin/tool/usertours/tests/manager_test.php +++ b/admin/tool/usertours/tests/manager_test.php @@ -325,6 +325,8 @@ class tool_usertours_manager_testcase extends advanced_testcase { public function test_get_matching_tours(array $alltours, string $url, array $expected) { $this->resetAfterTest(); + $this->setGuestUser(); + foreach ($alltours as $tourconfig) { $tour = $this->helper_create_tour((object) $tourconfig); $this->helper_create_step((object) ['tourid' => $tour->get_id()]); @@ -336,4 +338,34 @@ class tool_usertours_manager_testcase extends advanced_testcase { $this->assertEquals($expected[$i], $matches[$i]->get_name()); } } + + /** + * Test that no matching tours are returned if there is pending site policy agreement. + */ + public function test_get_matching_tours_for_user_without_site_policy_agreed() { + global $CFG; + + $this->resetAfterTest(); + $this->setGuestUser(); + + $tour = $this->helper_create_tour((object) [ + 'pathmatch' => '/%', + 'enabled' => true, + 'name' => 'Test tour', + 'description' => '', + 'configdata' => '', + ]); + + $this->helper_create_step((object) [ + 'tourid' => $tour->get_id(), + ]); + + $matches = \tool_usertours\manager::get_matching_tours(new moodle_url('/')); + $this->assertEquals(1, count($matches)); + + $CFG->sitepolicyguest = 'https://example.com'; + + $matches = \tool_usertours\manager::get_matching_tours(new moodle_url('/')); + $this->assertEmpty($matches); + } }