Skip to content

Commit

Permalink
MDL-72325 user: Do not show user tours without site policy agreed
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mudrd8mz committed Aug 12, 2021
1 parent 7a8eae0 commit 124b828
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions admin/tool/usertours/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,12 @@ public static function get_current_tours($reset = false): array {
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 = [];
Expand Down
32 changes: 32 additions & 0 deletions admin/tool/usertours/tests/manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ public function get_matching_tours_provider() {
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()]);
Expand All @@ -336,4 +338,34 @@ public function test_get_matching_tours(array $alltours, string $url, array $exp
$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);
}
}

0 comments on commit 124b828

Please sign in to comment.