From c3d725a3cd2cb070ec4afa6caf24f60c63c7dca3 Mon Sep 17 00:00:00 2001 From: Michael Kotlyar Date: Wed, 21 Aug 2024 13:36:26 +0100 Subject: [PATCH] MDL-80946 quizaccess_seb: Remove invalidkeys text if not using SEB. --- mod/quiz/accessrule/seb/rule.php | 7 ++++-- mod/quiz/accessrule/seb/tests/rule_test.php | 26 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/mod/quiz/accessrule/seb/rule.php b/mod/quiz/accessrule/seb/rule.php index 8ed6029c064f9..1ea21ae9e4fe8 100644 --- a/mod/quiz/accessrule/seb/rule.php +++ b/mod/quiz/accessrule/seb/rule.php @@ -376,8 +376,11 @@ private function get_reason_text(string $identifier) : string { */ private function get_invalid_key_error_message() : string { // Return error message with download link and links to get the seb config. - return get_string('invalidkeys', 'quizaccess_seb') - . $this->display_buttons($this->get_action_buttons()); + if ($this->accessmanager->is_using_seb()) { + return get_string('invalidkeys', 'quizaccess_seb') + . $this->display_buttons($this->get_action_buttons()); + } + return $this->display_buttons($this->get_action_buttons()); } /** diff --git a/mod/quiz/accessrule/seb/tests/rule_test.php b/mod/quiz/accessrule/seb/tests/rule_test.php index 9860f4006832a..7dbc66ad0c671 100644 --- a/mod/quiz/accessrule/seb/tests/rule_test.php +++ b/mod/quiz/accessrule/seb/tests/rule_test.php @@ -370,6 +370,7 @@ public function test_access_prevented_if_config_key_invalid() { // Set up dummy request. $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4'; + $_SERVER['HTTP_USER_AGENT'] = 'SEB'; $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key'; $user = $this->getDataGenerator()->create_user(); @@ -378,6 +379,28 @@ public function test_access_prevented_if_config_key_invalid() { $this->check_invalid_config_key(); } + /** + * Test access prevented if config key is invalid when not using the Safe Exam Browser. + */ + public function test_access_prevented_if_config_key_invalid_not_using_seb(): void { + global $FULLME; + + $this->setAdminUser(); + $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY); + + // Set up dummy request. + $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4'; + $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key'; + + $user = $this->getDataGenerator()->create_user(); + $this->setUser($user); + + $errormsg = $this->make_rule()->prevent_access(); + $this->assertNotEmpty($errormsg); + $this->assertStringNotContainsString("The Safe Exam Browser keys could not be validated. " + . "Check that you're using Safe Exam Browser with the correct configuration file.", $errormsg); + } + /** * Test access prevented if config keys is invalid and using uploaded config. */ @@ -396,6 +419,7 @@ public function test_access_prevented_if_config_key_invalid_uploaded_config() { // Set up dummy request. $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4'; + $_SERVER['HTTP_USER_AGENT'] = 'SEB'; $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key'; $user = $this->getDataGenerator()->create_user(); @@ -421,6 +445,7 @@ public function test_access_prevented_if_config_key_invalid_uploaded_template() // Set up dummy request. $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4'; + $_SERVER['HTTP_USER_AGENT'] = 'SEB'; $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key'; $user = $this->getDataGenerator()->create_user(); @@ -676,6 +701,7 @@ public function test_access_prevented_if_browser_exam_keys_are_invalid_use_uploa // Set up dummy request. $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4'; $expectedhash = hash('sha256', $FULLME . $quizsettings->get_config_key()); + $_SERVER['HTTP_USER_AGENT'] = 'SEB'; $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = $expectedhash; // Set up broken browser key.