1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace quizaccess_seb;
|
|
|
18 |
|
1441 |
ariadna |
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
require_once(__DIR__ . '/test_helper_trait.php');
|
|
|
22 |
|
1 |
efrain |
23 |
/**
|
|
|
24 |
* PHPUnit tests for template class.
|
|
|
25 |
*
|
|
|
26 |
* @package quizaccess_seb
|
|
|
27 |
* @author Dmitrii Metelkin <dmitriim@catalyst-au.net>
|
|
|
28 |
* @copyright 2020 Catalyst IT
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
1441 |
ariadna |
31 |
final class template_test extends \advanced_testcase {
|
|
|
32 |
use \quizaccess_seb_test_helper_trait;
|
1 |
efrain |
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Called before every test.
|
|
|
36 |
*/
|
|
|
37 |
public function setUp(): void {
|
|
|
38 |
parent::setUp();
|
|
|
39 |
|
|
|
40 |
$this->resetAfterTest();
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Test that template saved with valid content.
|
|
|
45 |
*/
|
11 |
efrain |
46 |
public function test_template_is_saved(): void {
|
1 |
efrain |
47 |
global $DB;
|
|
|
48 |
$data = new \stdClass();
|
|
|
49 |
$data->name = 'Test name';
|
|
|
50 |
$data->description = 'Test description';
|
|
|
51 |
$data->enabled = 1;
|
|
|
52 |
$data->content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
|
|
53 |
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
|
|
|
54 |
<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
|
|
|
55 |
. "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
|
|
|
56 |
. "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
|
1441 |
ariadna |
57 |
. "<key>browserMediaCaptureCamera</key><true/><key>browserMediaCaptureMicrophone</key><true/>"
|
1 |
efrain |
58 |
. "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
|
|
|
59 |
. "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
|
|
|
60 |
. "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
|
|
|
61 |
. "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
|
|
|
62 |
. "<string>test.com</string><key>regex</key><false/></dict></array>"
|
|
|
63 |
. "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
|
|
|
64 |
$template = new template(0, $data);
|
|
|
65 |
$template->save();
|
|
|
66 |
|
|
|
67 |
$actual = $DB->get_record(template::TABLE, ['id' => $template->get('id')]);
|
|
|
68 |
$this->assertEquals($data->name, $actual->name);
|
|
|
69 |
$this->assertEquals($data->description, $actual->description);
|
|
|
70 |
$this->assertEquals($data->enabled, $actual->enabled);
|
|
|
71 |
$this->assertEquals($data->content, $actual->content);
|
|
|
72 |
$this->assertTrue($template->can_delete());
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Test that template is not saved with invalid content.
|
|
|
77 |
*/
|
11 |
efrain |
78 |
public function test_template_is_not_saved_with_invalid_content(): void {
|
1 |
efrain |
79 |
$this->expectException(\core\invalid_persistent_exception::class);
|
|
|
80 |
$this->expectExceptionMessage('Invalid SEB config template');
|
|
|
81 |
|
|
|
82 |
$data = new \stdClass();
|
|
|
83 |
$data->name = 'Test name';
|
|
|
84 |
$data->description = 'Test description';
|
|
|
85 |
$data->enabled = 1;
|
|
|
86 |
$data->content = "Invalid content";
|
|
|
87 |
$template = new template(0, $data);
|
|
|
88 |
$template->save();
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Test that a template cannot be deleted when assigned to a quiz.
|
|
|
93 |
*/
|
11 |
efrain |
94 |
public function test_cannot_delete_template_when_assigned_to_quiz(): void {
|
1 |
efrain |
95 |
global $DB;
|
|
|
96 |
|
|
|
97 |
$data = new \stdClass();
|
|
|
98 |
$data->name = 'Test name';
|
|
|
99 |
$data->description = 'Test description';
|
|
|
100 |
$data->enabled = 1;
|
|
|
101 |
$data->content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
|
|
102 |
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
|
|
|
103 |
<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
|
|
|
104 |
. "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
|
|
|
105 |
. "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
|
1441 |
ariadna |
106 |
. "<key>browserMediaCaptureCamera</key><true/><key>browserMediaCaptureMicrophone</key><true/>"
|
1 |
efrain |
107 |
. "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
|
|
|
108 |
. "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
|
|
|
109 |
. "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
|
|
|
110 |
. "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
|
|
|
111 |
. "<string>test.com</string><key>regex</key><false/></dict></array>"
|
|
|
112 |
. "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
|
|
|
113 |
$template = new template(0, $data);
|
|
|
114 |
|
|
|
115 |
$template->save();
|
|
|
116 |
$this->assertTrue($template->can_delete());
|
|
|
117 |
|
|
|
118 |
$DB->insert_record(seb_quiz_settings::TABLE, (object) [
|
|
|
119 |
'quizid' => 1,
|
|
|
120 |
'cmid' => 1,
|
|
|
121 |
'templateid' => $template->get('id'),
|
|
|
122 |
'requiresafeexambrowser' => '1',
|
|
|
123 |
'sebconfigfile' => '373552893',
|
|
|
124 |
'showsebtaskbar' => '1',
|
|
|
125 |
'showwificontrol' => '0',
|
|
|
126 |
'showreloadbutton' => '1',
|
|
|
127 |
'showtime' => '0',
|
|
|
128 |
'showkeyboardlayout' => '1',
|
|
|
129 |
'allowuserquitseb' => '1',
|
|
|
130 |
'quitpassword' => 'test',
|
|
|
131 |
'linkquitseb' => '',
|
|
|
132 |
'userconfirmquit' => '1',
|
|
|
133 |
'enableaudiocontrol' => '1',
|
1441 |
ariadna |
134 |
'allowcapturecamera' => '1',
|
|
|
135 |
'allowcapturemicrophone' => '1',
|
1 |
efrain |
136 |
'muteonstartup' => '0',
|
|
|
137 |
'allowspellchecking' => '0',
|
|
|
138 |
'allowreloadinexam' => '1',
|
|
|
139 |
'activateurlfiltering' => '1',
|
|
|
140 |
'filterembeddedcontent' => '0',
|
|
|
141 |
'expressionsallowed' => 'test.com',
|
|
|
142 |
'regexallowed' => '',
|
|
|
143 |
'expressionsblocked' => '',
|
|
|
144 |
'regexblocked' => '',
|
|
|
145 |
'showsebdownloadlink' => '1',
|
|
|
146 |
'config' => '',
|
|
|
147 |
]);
|
|
|
148 |
|
|
|
149 |
$this->assertFalse($template->can_delete());
|
|
|
150 |
}
|
|
|
151 |
|
1441 |
ariadna |
152 |
/**
|
|
|
153 |
* Test that a disabled template no longer shows up in quiz SEB settings other than quizzes already using it.
|
|
|
154 |
*
|
|
|
155 |
* @covers \quizaccess_seb\seb_quiz_settings::get_record
|
|
|
156 |
* @covers \quizaccess_seb\settings_provider::get_requiresafeexambrowser_options
|
|
|
157 |
*/
|
|
|
158 |
public function test_disabled_template_quiz_setting_options(): void {
|
|
|
159 |
// Create quiz and fetch standard SEB requirement options.
|
|
|
160 |
$this->setAdminUser();
|
|
|
161 |
$this->course = $this->getDataGenerator()->create_course();
|
|
|
162 |
|
|
|
163 |
$templateoptionstr = get_string('seb_use_template', 'quizaccess_seb');
|
|
|
164 |
|
|
|
165 |
// Create a quiz.
|
|
|
166 |
$this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
|
|
167 |
$context = \context_module::instance($this->quiz->cmid);
|
|
|
168 |
|
|
|
169 |
// Check there is no template option (as there aren't any).
|
|
|
170 |
$options = settings_provider::get_requiresafeexambrowser_options($context);
|
|
|
171 |
$this->assertNotContainsEquals($templateoptionstr, $options);
|
|
|
172 |
|
|
|
173 |
// Create a template.
|
|
|
174 |
$data = new \stdClass();
|
|
|
175 |
$data->name = 'Test name';
|
|
|
176 |
$data->description = 'Test description';
|
|
|
177 |
$data->enabled = 1;
|
|
|
178 |
$data->content = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
|
|
|
179 |
$template = new template(0, $data);
|
|
|
180 |
$template->save();
|
|
|
181 |
|
|
|
182 |
// Check options now include template option.
|
|
|
183 |
$options = settings_provider::get_requiresafeexambrowser_options($context);
|
|
|
184 |
$this->assertContainsEquals($templateoptionstr, $options);
|
|
|
185 |
|
|
|
186 |
// Set SEB setting to use template for quiz.
|
|
|
187 |
$settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
|
|
|
188 |
$settings->set('templateid', $template->get('id'));
|
|
|
189 |
$settings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
|
|
|
190 |
$settings->save();
|
|
|
191 |
|
|
|
192 |
// Disable template.
|
|
|
193 |
$template->set('enabled', 0);
|
|
|
194 |
$template->save();
|
|
|
195 |
|
|
|
196 |
// Check option still exists on current quiz.
|
|
|
197 |
$options = settings_provider::get_requiresafeexambrowser_options($context);
|
|
|
198 |
$this->assertContainsEquals($templateoptionstr, $options);
|
|
|
199 |
|
|
|
200 |
// Create a new quiz.
|
|
|
201 |
$newquiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
|
|
|
202 |
$context = \context_module::instance($newquiz->cmid);
|
|
|
203 |
|
|
|
204 |
// Check there is no template option (as the template is now disabled).
|
|
|
205 |
$options = settings_provider::get_requiresafeexambrowser_options($context);
|
|
|
206 |
$this->assertNotContainsEquals($templateoptionstr, $options);
|
|
|
207 |
}
|
1 |
efrain |
208 |
}
|