MDL-72325 user: Do not show user tours without site policy agreed

Fetching user tours used to fail on external_api::validate_context() and
require_login() calls if the user did not have the site policy agreed.

The patch introduces a check to see if the user is fully set up and
ready to use the site before attempting to load the tours.
This commit is contained in:
David Mudrák
2021-08-12 13:05:38 +02:00
parent 7a8eae027f
commit 124b82831b
2 changed files with 38 additions and 0 deletions
+6
View File
@@ -624,6 +624,12 @@ class manager {
public static function get_matching_tours(\moodle_url $pageurl): array {
global $PAGE;
if (\core_user::awaiting_action()) {
// User not fully ready to use the site. Don't show any tours, we need the user to get properly set up so
// that all require_login() and other bits work as expected.
return [];
}
$tours = cache::get_matching_tourdata($pageurl);
$matches = [];
@@ -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);
}
}