| Línea 29... |
Línea 29... |
| 29 |
* @author Andrew Madden <andrewmadden@catalyst-au.net>
|
29 |
* @author Andrew Madden <andrewmadden@catalyst-au.net>
|
| 30 |
* @copyright 2020 Catalyst IT
|
30 |
* @copyright 2020 Catalyst IT
|
| 31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 32 |
* @covers \quizaccess_seb
|
32 |
* @covers \quizaccess_seb
|
| 33 |
*/
|
33 |
*/
|
| 34 |
class rule_test extends \advanced_testcase {
|
34 |
final class rule_test extends \advanced_testcase {
|
| 35 |
use \quizaccess_seb_test_helper_trait;
|
35 |
use \quizaccess_seb_test_helper_trait;
|
| Línea 36... |
Línea 36... |
| 36 |
|
36 |
|
| 37 |
/**
|
37 |
/**
|
| 38 |
* Called before every test.
|
38 |
* Called before every test.
|
| Línea 51... |
Línea 51... |
| 51 |
global $SESSION;
|
51 |
global $SESSION;
|
| Línea 52... |
Línea 52... |
| 52 |
|
52 |
|
| 53 |
if (!empty($this->quiz)) {
|
53 |
if (!empty($this->quiz)) {
|
| 54 |
unset($SESSION->quizaccess_seb_access);
|
54 |
unset($SESSION->quizaccess_seb_access);
|
| - |
|
55 |
}
|
| 55 |
}
|
56 |
parent::tearDown();
|
| Línea 56... |
Línea 57... |
| 56 |
}
|
57 |
}
|
| 57 |
|
58 |
|
| 58 |
/**
|
59 |
/**
|
| Línea 85... |
Línea 86... |
| 85 |
/**
|
86 |
/**
|
| 86 |
* Provider to return valid form field data when saving settings.
|
87 |
* Provider to return valid form field data when saving settings.
|
| 87 |
*
|
88 |
*
|
| 88 |
* @return array
|
89 |
* @return array
|
| 89 |
*/
|
90 |
*/
|
| 90 |
public function valid_form_data_provider(): array {
|
91 |
public static function valid_form_data_provider(): array {
|
| 91 |
return [
|
92 |
return [
|
| 92 |
'valid seb_requiresafeexambrowser' => ['seb_requiresafeexambrowser', '0'],
|
93 |
'valid seb_requiresafeexambrowser' => ['seb_requiresafeexambrowser', '0'],
|
| 93 |
'valid seb_linkquitseb0' => ['seb_linkquitseb', 'http://safeexambrowser.org/macosx'],
|
94 |
'valid seb_linkquitseb0' => ['seb_linkquitseb', 'http://safeexambrowser.org/macosx'],
|
| 94 |
'valid seb_linkquitseb1' => ['seb_linkquitseb', 'safeexambrowser.org/macosx'],
|
95 |
'valid seb_linkquitseb1' => ['seb_linkquitseb', 'safeexambrowser.org/macosx'],
|
| 95 |
'valid seb_linkquitseb2' => ['seb_linkquitseb', 'www.safeexambrowser.org/macosx'],
|
96 |
'valid seb_linkquitseb2' => ['seb_linkquitseb', 'www.safeexambrowser.org/macosx'],
|
| Línea 101... |
Línea 102... |
| 101 |
/**
|
102 |
/**
|
| 102 |
* Provider to return invalid form field data when saving settings.
|
103 |
* Provider to return invalid form field data when saving settings.
|
| 103 |
*
|
104 |
*
|
| 104 |
* @return array
|
105 |
* @return array
|
| 105 |
*/
|
106 |
*/
|
| 106 |
public function invalid_form_data_provider(): array {
|
107 |
public static function invalid_form_data_provider(): array {
|
| 107 |
return [
|
108 |
return [
|
| 108 |
'invalid seb_requiresafeexambrowser' => ['seb_requiresafeexambrowser', 'Uh oh!'],
|
109 |
'invalid seb_requiresafeexambrowser' => ['seb_requiresafeexambrowser', 'Uh oh!'],
|
| 109 |
'invalid seb_linkquitseb0' => ['seb_linkquitseb', '\0'],
|
110 |
'invalid seb_linkquitseb0' => ['seb_linkquitseb', '\0'],
|
| 110 |
'invalid seb_linkquitseb1' => ['seb_linkquitseb', 'invalid url'],
|
111 |
'invalid seb_linkquitseb1' => ['seb_linkquitseb', 'invalid url'],
|
| 111 |
'invalid seb_linkquitseb2' => ['seb_linkquitseb', 'http]://safeexambrowser.org/macosx'],
|
112 |
'invalid seb_linkquitseb2' => ['seb_linkquitseb', 'http]://safeexambrowser.org/macosx'],
|
| Línea 368... |
Línea 369... |
| 368 |
$this->setAdminUser();
|
369 |
$this->setAdminUser();
|
| 369 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
370 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
| Línea 370... |
Línea 371... |
| 370 |
|
371 |
|
| 371 |
// Set up dummy request.
|
372 |
// Set up dummy request.
|
| - |
|
373 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
| 372 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
374 |
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
|
| Línea 373... |
Línea 375... |
| 373 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key';
|
375 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key';
|
| 374 |
|
376 |
|
| Línea 375... |
Línea 377... |
| 375 |
$user = $this->getDataGenerator()->create_user();
|
377 |
$user = $this->getDataGenerator()->create_user();
|
| 376 |
$this->setUser($user);
|
378 |
$this->setUser($user);
|
| Línea 377... |
Línea 379... |
| 377 |
|
379 |
|
| - |
|
380 |
$this->check_invalid_config_key();
|
| - |
|
381 |
}
|
| - |
|
382 |
|
| - |
|
383 |
/**
|
| - |
|
384 |
* Test access prevented if config key is invalid when not using the Safe Exam Browser.
|
| - |
|
385 |
*/
|
| - |
|
386 |
public function test_access_prevented_if_config_key_invalid_not_using_seb(): void {
|
| - |
|
387 |
global $FULLME;
|
| - |
|
388 |
|
| - |
|
389 |
$this->setAdminUser();
|
| - |
|
390 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
| - |
|
391 |
|
| - |
|
392 |
// Set up dummy request.
|
| - |
|
393 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
| - |
|
394 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key';
|
| - |
|
395 |
|
| - |
|
396 |
$user = $this->getDataGenerator()->create_user();
|
| - |
|
397 |
$this->setUser($user);
|
| - |
|
398 |
|
| - |
|
399 |
$errormsg = $this->make_rule()->prevent_access();
|
| - |
|
400 |
$this->assertNotEmpty($errormsg);
|
| - |
|
401 |
$this->assertStringNotContainsString("The Safe Exam Browser keys could not be validated. "
|
| 378 |
$this->check_invalid_config_key();
|
402 |
. "Check that you're using Safe Exam Browser with the correct configuration file.", $errormsg);
|
| 379 |
}
|
403 |
}
|
| 380 |
|
404 |
|
| 381 |
/**
|
405 |
/**
|
| Línea 388... |
Línea 412... |
| 388 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
412 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
| Línea 389... |
Línea 413... |
| 389 |
|
413 |
|
| 390 |
// Set quiz setting to require seb and save BEK.
|
414 |
// Set quiz setting to require seb and save BEK.
|
| 391 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
415 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
| 392 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
416 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
| 393 |
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
|
417 |
$xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
|
| 394 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
418 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
| Línea 395... |
Línea 419... |
| 395 |
$quizsettings->save();
|
419 |
$quizsettings->save();
|
| 396 |
|
420 |
|
| - |
|
421 |
// Set up dummy request.
|
| 397 |
// Set up dummy request.
|
422 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
| Línea 398... |
Línea 423... |
| 398 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
423 |
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
|
| 399 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key';
|
424 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key';
|
| Línea 419... |
Línea 444... |
| 419 |
$quizsettings->set('templateid', $this->create_template()->get('id'));
|
444 |
$quizsettings->set('templateid', $this->create_template()->get('id'));
|
| 420 |
$quizsettings->save();
|
445 |
$quizsettings->save();
|
| Línea 421... |
Línea 446... |
| 421 |
|
446 |
|
| 422 |
// Set up dummy request.
|
447 |
// Set up dummy request.
|
| - |
|
448 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
| 423 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
449 |
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
|
| Línea 424... |
Línea 450... |
| 424 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key';
|
450 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Broken config key';
|
| 425 |
|
451 |
|
| Línea 490... |
Línea 516... |
| 490 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
516 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
| Línea 491... |
Línea 517... |
| 491 |
|
517 |
|
| 492 |
// Set quiz setting to require seb and save BEK.
|
518 |
// Set quiz setting to require seb and save BEK.
|
| 493 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
519 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
| 494 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
520 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
| 495 |
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
|
521 |
$xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
|
| 496 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
522 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
| Línea 497... |
Línea 523... |
| 497 |
$quizsettings->save();
|
523 |
$quizsettings->save();
|
| 498 |
|
524 |
|
| Línea 549... |
Línea 575... |
| 549 |
// Set quiz setting to require seb and save BEK.
|
575 |
// Set quiz setting to require seb and save BEK.
|
| 550 |
$browserexamkey = hash('sha256', 'testkey');
|
576 |
$browserexamkey = hash('sha256', 'testkey');
|
| 551 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
577 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
| 552 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
578 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
| 553 |
$quizsettings->set('allowedbrowserexamkeys', $browserexamkey);
|
579 |
$quizsettings->set('allowedbrowserexamkeys', $browserexamkey);
|
| 554 |
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
|
580 |
$xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
|
| 555 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
581 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
| 556 |
$quizsettings->save();
|
582 |
$quizsettings->save();
|
| Línea 557... |
Línea 583... |
| 557 |
|
583 |
|
| 558 |
// Set up dummy request.
|
584 |
// Set up dummy request.
|
| Línea 667... |
Línea 693... |
| 667 |
// Set quiz setting to require seb and save BEK.
|
693 |
// Set quiz setting to require seb and save BEK.
|
| 668 |
$browserexamkey = hash('sha256', 'testkey');
|
694 |
$browserexamkey = hash('sha256', 'testkey');
|
| 669 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
695 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
| 670 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
696 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
| 671 |
$quizsettings->set('allowedbrowserexamkeys', $browserexamkey);
|
697 |
$quizsettings->set('allowedbrowserexamkeys', $browserexamkey);
|
| 672 |
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
|
698 |
$xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
|
| 673 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
699 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
| 674 |
$quizsettings->save();
|
700 |
$quizsettings->save();
|
| Línea 675... |
Línea 701... |
| 675 |
|
701 |
|
| 676 |
// Set up dummy request.
|
702 |
// Set up dummy request.
|
| 677 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
703 |
$FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
|
| - |
|
704 |
$expectedhash = hash('sha256', $FULLME . $quizsettings->get_config_key());
|
| 678 |
$expectedhash = hash('sha256', $FULLME . $quizsettings->get_config_key());
|
705 |
$_SERVER['HTTP_USER_AGENT'] = 'SEB';
|
| Línea 679... |
Línea 706... |
| 679 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = $expectedhash;
|
706 |
$_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = $expectedhash;
|
| 680 |
|
707 |
|
| Línea 795... |
Línea 822... |
| 795 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
822 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
| Línea 796... |
Línea 823... |
| 796 |
|
823 |
|
| 797 |
// Set quiz setting to require seb.
|
824 |
// Set quiz setting to require seb.
|
| 798 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
825 |
$quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
| 799 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG); // Doesn't check basic header.
|
826 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG); // Doesn't check basic header.
|
| 800 |
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
|
827 |
$xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
|
| 801 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
828 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
| Línea 802... |
Línea 829... |
| 802 |
$quizsettings->save();
|
829 |
$quizsettings->save();
|
| 803 |
|
830 |
|
| Línea 1054... |
Línea 1081... |
| 1054 |
$this->assertStringContainsString($this->get_seb_launch_link(), $method->invoke($this->make_rule()));
|
1081 |
$this->assertStringContainsString($this->get_seb_launch_link(), $method->invoke($this->make_rule()));
|
| 1055 |
$this->assertStringContainsString($this->get_seb_config_download_link(), $method->invoke($this->make_rule()));
|
1082 |
$this->assertStringContainsString($this->get_seb_config_download_link(), $method->invoke($this->make_rule()));
|
| Línea 1056... |
Línea 1083... |
| 1056 |
|
1083 |
|
| 1057 |
// Should see links when using uploaded config.
|
1084 |
// Should see links when using uploaded config.
|
| 1058 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
1085 |
$quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
|
| 1059 |
$xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
|
1086 |
$xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
|
| 1060 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
1087 |
$this->create_module_test_file($xml, $this->quiz->cmid);
|
| 1061 |
$quizsettings->save();
|
1088 |
$quizsettings->save();
|
| 1062 |
$this->assertStringContainsString($this->get_seb_launch_link(), $method->invoke($this->make_rule()));
|
1089 |
$this->assertStringContainsString($this->get_seb_launch_link(), $method->invoke($this->make_rule()));
|