Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
19
use context_module;
20
use moodle_url;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
require_once(__DIR__ . '/test_helper_trait.php');
25
 
26
/**
27
 * PHPUnit tests for seb_quiz_settings class.
28
 *
29
 * @package   quizaccess_seb
30
 * @author    Andrew Madden <andrewmadden@catalyst-au.net>
31
 * @copyright 2020 Catalyst IT
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
1441 ariadna 34
final class quiz_settings_test extends \advanced_testcase {
1 efrain 35
    use \quizaccess_seb_test_helper_trait;
36
 
37
    /** @var context_module $context Test context. */
38
    protected $context;
39
 
40
    /** @var moodle_url $url Test quiz URL. */
41
    protected $url;
42
 
43
    /**
44
     * Called before every test.
45
     */
46
    public function setUp(): void {
47
        parent::setUp();
48
 
49
        $this->resetAfterTest();
50
 
51
        $this->setAdminUser();
52
        $this->course = $this->getDataGenerator()->create_course();
53
        $this->quiz = $this->getDataGenerator()->create_module('quiz', [
54
            'course' => $this->course->id,
55
            'seb_requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
56
        ]);
57
        $this->context = \context_module::instance($this->quiz->cmid);
58
        $this->url = new \moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]);
59
    }
60
 
61
    /**
62
     * Test that config is generated immediately prior to saving quiz settings.
63
     */
11 efrain 64
    public function test_config_is_created_from_quiz_settings(): void {
1 efrain 65
        // Test settings to populate the in the object.
66
        $settings = $this->get_test_settings([
67
            'quizid' => $this->quiz->id,
68
            'cmid' => $this->quiz->cmid,
69
        ]);
70
 
71
        // Obtain the existing record that is created when using a generator.
72
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
73
 
74
        // Update the settings with values from the test function.
75
        $quizsettings->from_record($settings);
76
        $quizsettings->save();
77
 
78
        $config = $quizsettings->get_config();
79
        $this->assertEquals(
80
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
81
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
82
<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
83
                . "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
84
                . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
1441 ariadna 85
                . "<key>browserMediaCaptureCamera</key><true/><key>browserMediaCaptureMicrophone</key><true/>"
1 efrain 86
                . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
87
                . "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
88
                . "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
89
                . "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
90
                . "<string>test.com</string><key>regex</key><false/></dict></array><key>startURL</key><string>$this->url</string>"
91
                . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
92
                . "<key>examSessionClearCookiesOnStart</key><false/><key>allowPreferencesWindow</key><false/></dict></plist>\n",
93
            $config);
94
    }
95
 
96
    /**
97
     * Test that config string gets updated from quiz settings.
98
     */
11 efrain 99
    public function test_config_is_updated_from_quiz_settings(): void {
1 efrain 100
        // Test settings to populate the in the object.
101
        $settings = $this->get_test_settings([
102
            'quizid' => $this->quiz->id,
103
            'cmid' => $this->quiz->cmid,
104
        ]);
105
 
106
        // Obtain the existing record that is created when using a generator.
107
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
108
 
109
        // Update the settings with values from the test function.
110
        $quizsettings->from_record($settings);
111
        $quizsettings->save();
112
 
113
        $config = $quizsettings->get_config();
114
        $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>
115
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
116
<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
117
            . "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
118
            . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
1441 ariadna 119
            . "<key>browserMediaCaptureCamera</key><true/><key>browserMediaCaptureMicrophone</key><true/>"
1 efrain 120
            . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
121
            . "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
122
            . "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
123
            . "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
124
            . "<string>test.com</string><key>regex</key><false/></dict></array><key>startURL</key><string>$this->url</string>"
125
            . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
126
            . "<key>examSessionClearCookiesOnStart</key><false/>"
127
            . "<key>allowPreferencesWindow</key><false/></dict></plist>\n", $config);
128
 
129
        $quizsettings->set('filterembeddedcontent', 1); // Alter the settings.
130
        $quizsettings->save();
131
        $config = $quizsettings->get_config();
132
        $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>
133
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
134
<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
135
            . "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
136
            . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
1441 ariadna 137
            . "<key>browserMediaCaptureCamera</key><true/><key>browserMediaCaptureMicrophone</key><true/>"
1 efrain 138
            . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
139
            . "<key>URLFilterEnableContentFilter</key><true/><key>hashedQuitPassword</key>"
140
            . "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
141
            . "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
142
            . "<string>test.com</string><key>regex</key><false/></dict></array><key>startURL</key><string>$this->url</string>"
143
            . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
144
            . "<key>examSessionClearCookiesOnStart</key><false/>"
145
            . "<key>allowPreferencesWindow</key><false/></dict></plist>\n", $config);
146
    }
147
 
148
    /**
149
     * Test that config key is generated immediately prior to saving quiz settings.
150
     */
11 efrain 151
    public function test_config_key_is_created_from_quiz_settings(): void {
1 efrain 152
        $settings = $this->get_test_settings();
153
 
154
        $quizsettings = new seb_quiz_settings(0, $settings);
155
        $configkey = $quizsettings->get_config_key();
1441 ariadna 156
        $this->assertEquals("24712dcecc69a2ca8ad19f4f8928375038f087e13ce9f504cfc4496846aaaf27",
1 efrain 157
            $configkey
158
        );
159
    }
160
 
161
    /**
162
     * Test that config key is generated immediately prior to saving quiz settings.
163
     */
11 efrain 164
    public function test_config_key_is_updated_from_quiz_settings(): void {
1 efrain 165
        $settings = $this->get_test_settings();
166
 
167
        $quizsettings = new seb_quiz_settings(0, $settings);
168
        $configkey = $quizsettings->get_config_key();
1441 ariadna 169
        $this->assertEquals("24712dcecc69a2ca8ad19f4f8928375038f087e13ce9f504cfc4496846aaaf27",
1 efrain 170
                $configkey);
171
 
172
        $quizsettings->set('filterembeddedcontent', 1); // Alter the settings.
173
        $configkey = $quizsettings->get_config_key();
1441 ariadna 174
        $this->assertEquals("e2f6dc2a6ddb9bdfa7530dc8afbae996499c34a954e1c635c4fed894e47c1abc",
1 efrain 175
            $configkey);
176
    }
177
 
178
    /**
179
     * Test that different URL filter expressions are turned into config XML.
180
     *
181
     * @param \stdClass $settings Quiz settings
182
     * @param string $expectedxml SEB Config XML.
183
     *
184
     * @dataProvider filter_rules_provider
185
     */
11 efrain 186
    public function test_filter_rules_added_to_config(\stdClass $settings, string $expectedxml): void {
1 efrain 187
        $quizsettings = new seb_quiz_settings(0, $settings);
188
        $config = $quizsettings->get_config();
189
        $this->assertEquals($expectedxml, $config);
190
    }
191
 
192
    /**
193
     * Test that browser keys are validated and retrieved as an array instead of string.
194
     */
11 efrain 195
    public function test_browser_exam_keys_are_retrieved_as_array(): void {
1 efrain 196
        $quizsettings = new seb_quiz_settings();
197
        $quizsettings->set('allowedbrowserexamkeys', "one two,three\nfour");
198
        $retrievedkeys = $quizsettings->get('allowedbrowserexamkeys');
199
        $this->assertEquals(['one', 'two', 'three', 'four'], $retrievedkeys);
200
    }
201
 
202
    /**
203
     * Test validation of Browser Exam Keys.
204
     *
205
     * @param string $bek Browser Exam Key.
206
     * @param string $expectederrorstring Expected error.
207
     *
208
     * @dataProvider bad_browser_exam_key_provider
209
     */
11 efrain 210
    public function test_browser_exam_keys_validation_errors($bek, $expectederrorstring): void {
1 efrain 211
        $quizsettings = new seb_quiz_settings();
212
        $quizsettings->set('allowedbrowserexamkeys', $bek);
213
        $quizsettings->validate();
214
        $errors = $quizsettings->get_errors();
215
        $this->assertContainsEquals($expectederrorstring, $errors);
216
    }
217
 
218
    /**
219
     * Test that uploaded seb file gets converted to config string.
220
     */
11 efrain 221
    public function test_config_file_uploaded_converted_to_config(): void {
1 efrain 222
        $url = new \moodle_url("/mod/quiz/view.php", ['id' => $this->quiz->cmid]);
223
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
224
                . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
225
                . "<plist version=\"1.0\"><dict><key>hashedQuitPassword</key><string>hashedpassword</string>"
226
                . "<key>allowWlan</key><false/><key>startURL</key><string>$url</string>"
227
                . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer></dict></plist>\n";
228
        $itemid = $this->create_module_test_file($xml, $this->quiz->cmid);
229
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
230
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
231
        $quizsettings->save();
232
        $config = $quizsettings->get_config();
233
        $this->assertEquals($xml, $config);
234
    }
235
 
236
    /**
237
     * Test test_no_config_file_uploaded
238
     */
11 efrain 239
    public function test_no_config_file_uploaded(): void {
1 efrain 240
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
241
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
242
        $cmid = $quizsettings->get('cmid');
243
        $this->expectException(\moodle_exception::class);
244
        $this->expectExceptionMessage("No uploaded SEB config file could be found for quiz with cmid: {$cmid}");
245
        $quizsettings->get_config();
246
    }
247
 
248
    /**
249
     * A helper function to build a config file.
250
     *
251
     * @param mixed $allowuserquitseb Required allowQuit setting.
252
     * @param mixed $quitpassword Required hashedQuitPassword setting.
253
     *
254
     * @return string
255
     */
256
    protected function get_config_xml($allowuserquitseb = null, $quitpassword = null) {
257
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
258
            . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
259
            . "<plist version=\"1.0\"><dict><key>allowWlan</key><false/><key>startURL</key>"
260
            . "<string>https://safeexambrowser.org/start</string>"
261
            . "<key>sendBrowserExamKey</key><true/>";
262
 
263
        if (!is_null($allowuserquitseb)) {
264
            $allowuserquitseb = empty($allowuserquitseb) ? 'false' : 'true';
265
            $xml .= "<key>allowQuit</key><{$allowuserquitseb}/>";
266
        }
267
 
268
        if (!is_null($quitpassword)) {
269
            $xml .= "<key>hashedQuitPassword</key><string>{$quitpassword}</string>";
270
        }
271
 
272
        $xml .= "</dict></plist>\n";
273
 
274
        return $xml;
275
    }
276
 
277
    /**
278
     * Test using USE_SEB_TEMPLATE and have it override settings from the template when they are set.
279
     */
11 efrain 280
    public function test_using_seb_template_override_settings_when_they_set_in_template(): void {
1 efrain 281
        $xml = $this->get_config_xml(true, 'password');
282
        $template = $this->create_template($xml);
283
 
284
        $this->assertStringContainsString("<key>startURL</key><string>https://safeexambrowser.org/start</string>", $template->get('content'));
285
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $template->get('content'));
286
        $this->assertStringContainsString("<key>hashedQuitPassword</key><string>password</string>", $template->get('content'));
287
 
288
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
289
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
290
        $quizsettings->set('templateid', $template->get('id'));
291
        $quizsettings->set('allowuserquitseb', 1);
292
        $quizsettings->save();
293
 
294
        $this->assertStringContainsString(
295
            "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id={$this->quiz->cmid}</string>",
296
            $quizsettings->get_config()
297
        );
298
 
299
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $quizsettings->get_config());
300
        $this->assertStringNotContainsString("hashedQuitPassword", $quizsettings->get_config());
301
 
302
        $quizsettings->set('quitpassword', 'new password');
303
        $quizsettings->save();
304
        $hashedpassword = hash('SHA256', 'new password');
305
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $quizsettings->get_config());
306
        $this->assertStringNotContainsString("<key>hashedQuitPassword</key><string>password</string>", $quizsettings->get_config());
307
        $this->assertStringContainsString("<key>hashedQuitPassword</key><string>{$hashedpassword}</string>", $quizsettings->get_config());
308
 
309
        $quizsettings->set('allowuserquitseb', 0);
310
        $quizsettings->set('quitpassword', '');
311
        $quizsettings->save();
312
        $this->assertStringContainsString("<key>allowQuit</key><false/>", $quizsettings->get_config());
313
        $this->assertStringNotContainsString("hashedQuitPassword", $quizsettings->get_config());
314
    }
315
 
316
    /**
317
     * Test using USE_SEB_TEMPLATE and have it override settings from the template when they are not set.
318
     */
11 efrain 319
    public function test_using_seb_template_override_settings_when_not_set_in_template(): void {
1 efrain 320
        $xml = $this->get_config_xml();
321
        $template = $this->create_template($xml);
322
 
323
        $this->assertStringContainsString("<key>startURL</key><string>https://safeexambrowser.org/start</string>", $template->get('content'));
324
        $this->assertStringNotContainsString("<key>allowQuit</key><true/>", $template->get('content'));
325
        $this->assertStringNotContainsString("<key>hashedQuitPassword</key><string>password</string>", $template->get('content'));
326
 
327
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
328
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
329
        $quizsettings->set('templateid', $template->get('id'));
330
        $quizsettings->set('allowuserquitseb', 1);
331
        $quizsettings->save();
332
 
333
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $quizsettings->get_config());
334
        $this->assertStringNotContainsString("hashedQuitPassword", $quizsettings->get_config());
335
 
336
        $quizsettings->set('quitpassword', 'new password');
337
        $quizsettings->save();
338
        $hashedpassword = hash('SHA256', 'new password');
339
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $quizsettings->get_config());
340
        $this->assertStringContainsString("<key>hashedQuitPassword</key><string>{$hashedpassword}</string>", $quizsettings->get_config());
341
 
342
        $quizsettings->set('allowuserquitseb', 0);
343
        $quizsettings->set('quitpassword', '');
344
        $quizsettings->save();
345
        $this->assertStringContainsString("<key>allowQuit</key><false/>", $quizsettings->get_config());
346
        $this->assertStringNotContainsString("hashedQuitPassword", $quizsettings->get_config());
347
    }
348
 
349
    /**
350
     * Test using USE_SEB_UPLOAD_CONFIG and use settings from the file if they are set.
351
     */
11 efrain 352
    public function test_using_own_config_settings_are_not_overridden_if_set(): void {
1 efrain 353
        $xml = $this->get_config_xml(true, 'password');
354
        $this->create_module_test_file($xml, $this->quiz->cmid);
355
 
356
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
357
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
358
        $quizsettings->set('allowuserquitseb', 0);
359
        $quizsettings->set('quitpassword', '');
360
        $quizsettings->save();
361
 
362
        $this->assertStringContainsString(
363
            "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id={$this->quiz->cmid}</string>",
364
            $quizsettings->get_config()
365
        );
366
 
367
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $quizsettings->get_config());
368
        $this->assertStringContainsString("<key>hashedQuitPassword</key><string>password</string>", $quizsettings->get_config());
369
 
370
        $quizsettings->set('quitpassword', 'new password');
371
        $quizsettings->save();
372
        $hashedpassword = hash('SHA256', 'new password');
373
 
374
        $this->assertStringNotContainsString("<key>hashedQuitPassword</key><string>{$hashedpassword}</string>", $quizsettings->get_config());
375
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $quizsettings->get_config());
376
        $this->assertStringContainsString("<key>hashedQuitPassword</key><string>password</string>", $quizsettings->get_config());
377
 
378
        $quizsettings->set('allowuserquitseb', 0);
379
        $quizsettings->set('quitpassword', '');
380
        $quizsettings->save();
381
 
382
        $this->assertStringContainsString("<key>allowQuit</key><true/>", $quizsettings->get_config());
383
        $this->assertStringContainsString("<key>hashedQuitPassword</key><string>password</string>", $quizsettings->get_config());
384
    }
385
 
386
    /**
387
     * Test using USE_SEB_UPLOAD_CONFIG and use settings from the file if they are not set.
388
     */
11 efrain 389
    public function test_using_own_config_settings_are_not_overridden_if_not_set(): void {
1 efrain 390
        $xml = $this->get_config_xml();
391
        $this->create_module_test_file($xml, $this->quiz->cmid);
392
 
393
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
394
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
395
        $quizsettings->set('allowuserquitseb', 1);
396
        $quizsettings->set('quitpassword', '');
397
        $quizsettings->save();
398
 
399
        $this->assertStringContainsString(
400
            "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id={$this->quiz->cmid}</string>",
401
            $quizsettings->get_config()
402
        );
403
 
404
        $this->assertStringNotContainsString("allowQuit", $quizsettings->get_config());
405
        $this->assertStringNotContainsString("hashedQuitPassword", $quizsettings->get_config());
406
 
407
        $quizsettings->set('quitpassword', 'new password');
408
        $quizsettings->save();
409
 
410
        $this->assertStringNotContainsString("allowQuit", $quizsettings->get_config());
411
        $this->assertStringNotContainsString("hashedQuitPassword", $quizsettings->get_config());
412
 
413
        $quizsettings->set('allowuserquitseb', 0);
414
        $quizsettings->set('quitpassword', '');
415
        $quizsettings->save();
416
 
417
        $this->assertStringNotContainsString("allowQuit", $quizsettings->get_config());
418
        $this->assertStringNotContainsString("hashedQuitPassword", $quizsettings->get_config());
419
    }
420
 
421
    /**
422
     * Test using USE_SEB_TEMPLATE populates the linkquitseb setting if a quitURL is found.
423
     */
11 efrain 424
    public function test_template_has_quit_url_set(): void {
1 efrain 425
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
426
            . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
427
            . "<plist version=\"1.0\"><dict><key>hashedQuitPassword</key><string>hashedpassword</string>"
428
            . "<key>allowWlan</key><false/><key>quitURL</key><string>http://seb.quit.url</string>"
429
            . "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
430
 
431
        $template = $this->create_template($xml);
432
 
433
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
434
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
435
        $quizsettings->set('templateid', $template->get('id'));
436
 
437
        $this->assertEmpty($quizsettings->get('linkquitseb'));
438
        $quizsettings->save();
439
 
440
        $this->assertNotEmpty($quizsettings->get('linkquitseb'));
441
        $this->assertEquals('http://seb.quit.url', $quizsettings->get('linkquitseb'));
442
    }
443
 
444
    /**
445
     * Test using USE_SEB_UPLOAD_CONFIG populates the linkquitseb setting if a quitURL is found.
446
     */
11 efrain 447
    public function test_config_file_uploaded_has_quit_url_set(): void {
1 efrain 448
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
449
            . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
450
            . "<plist version=\"1.0\"><dict><key>hashedQuitPassword</key><string>hashedpassword</string>"
451
            . "<key>allowWlan</key><false/><key>quitURL</key><string>http://seb.quit.url</string>"
452
            . "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
453
 
454
        $itemid = $this->create_module_test_file($xml, $this->quiz->cmid);
455
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
456
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
457
 
458
        $this->assertEmpty($quizsettings->get('linkquitseb'));
459
        $quizsettings->save();
460
 
461
        $this->assertNotEmpty($quizsettings->get('linkquitseb'));
462
        $this->assertEquals('http://seb.quit.url', $quizsettings->get('linkquitseb'));
463
    }
464
 
465
    /**
466
     * Test template id set correctly.
467
     */
11 efrain 468
    public function test_templateid_set_correctly_when_save_settings(): void {
1 efrain 469
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
470
        $this->assertEquals(0, $quizsettings->get('templateid'));
471
 
472
        $template = $this->create_template();
473
        $templateid = $template->get('id');
474
 
475
        // Initially set to USE_SEB_TEMPLATE with a template id.
476
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_TEMPLATE, $templateid);
477
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
478
        $this->assertEquals($templateid, $quizsettings->get('templateid'));
479
 
480
        // Case for USE_SEB_NO, ensure template id reverts to 0.
481
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_NO);
482
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
483
        $this->assertEquals(0, $quizsettings->get('templateid'));
484
 
485
        // Reverting back to USE_SEB_TEMPLATE.
486
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_TEMPLATE, $templateid);
487
 
488
        // Case for USE_SEB_CONFIG_MANUALLY, ensure template id reverts to 0.
489
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_CONFIG_MANUALLY);
490
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
491
        $this->assertEquals(0, $quizsettings->get('templateid'));
492
 
493
        // Reverting back to USE_SEB_TEMPLATE.
494
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_TEMPLATE, $templateid);
495
 
496
        // Case for USE_SEB_CLIENT_CONFIG, ensure template id reverts to 0.
497
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_CLIENT_CONFIG);
498
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
499
        $this->assertEquals(0, $quizsettings->get('templateid'));
500
 
501
        // Reverting back to USE_SEB_TEMPLATE.
502
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_TEMPLATE, $templateid);
503
 
504
        // Case for USE_SEB_UPLOAD_CONFIG, ensure template id reverts to 0.
1441 ariadna 505
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 506
        $this->create_module_test_file($xml, $this->quiz->cmid);
507
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_UPLOAD_CONFIG);
508
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
509
        $this->assertEquals(0, $quizsettings->get('templateid'));
510
 
511
        // Case for USE_SEB_TEMPLATE, ensure template id is correct.
512
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_TEMPLATE, $templateid);
513
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
514
        $this->assertEquals($templateid, $quizsettings->get('templateid'));
515
    }
516
 
517
    /**
518
     * Helper function in tests to set USE_SEB_TEMPLATE and a template id on the quiz settings.
519
     *
520
     * @param seb_quiz_settings $quizsettings Given quiz settings instance.
521
     * @param int $savetype Type of SEB usage.
522
     * @param int $templateid Template ID.
523
     */
524
    public function save_settings_with_optional_template($quizsettings, $savetype, $templateid = 0) {
525
        $quizsettings->set('requiresafeexambrowser', $savetype);
526
        if (!empty($templateid)) {
527
            $quizsettings->set('templateid', $templateid);
528
        }
529
        $quizsettings->save();
530
    }
531
 
532
    /**
533
     * Bad browser exam key data provider.
534
     *
535
     * @return array
536
     */
1441 ariadna 537
    public static function bad_browser_exam_key_provider(): array {
1 efrain 538
        return [
539
            'Short string' => ['fdsf434r',
540
                    'A key should be a 64-character hex string.'],
541
            'Non hex string' => ['aadf6799aadf6789aadf6789aadf6789aadf6789aadf6789aadf6789aadf678!',
542
                    'A key should be a 64-character hex string.'],
543
            'Non unique' => ["aadf6799aadf6789aadf6789aadf6789aadf6789aadf6789aadf6789aadf6789"
544
                    . "\naadf6799aadf6789aadf6789aadf6789aadf6789aadf6789aadf6789aadf6789", 'The keys must all be different.'],
545
        ];
546
    }
547
 
548
    /**
549
     * Provide settings for different filter rules.
550
     *
551
     * @return array Test data.
552
     */
1441 ariadna 553
    public static function filter_rules_provider(): array {
1 efrain 554
        return [
555
            'enabled simple expessions' => [
556
                (object) [
557
                    'requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
558
                    'quizid' => 1,
559
                    'cmid' => 1,
560
                    'expressionsallowed' => "test.com\r\nsecond.hello",
561
                    'regexallowed' => '',
562
                    'expressionsblocked' => '',
563
                    'regexblocked' => '',
564
                ],
565
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
566
                . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
567
                . "<plist version=\"1.0\"><dict><key>showTaskBar</key><true/>"
568
                . "<key>allowWlan</key><false/><key>showReloadButton</key>"
569
                . "<true/><key>showTime</key><true/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
570
                . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><false/><key>audioMute</key><false/>"
1441 ariadna 571
                . "<key>browserMediaCaptureCamera</key><false/><key>browserMediaCaptureMicrophone</key><false/>"
1 efrain 572
                . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><false/>"
573
                . "<key>URLFilterEnableContentFilter</key><false/><key>URLFilterRules</key><array>"
574
                . "<dict><key>action</key><integer>1</integer><key>active</key><true/>"
575
                . "<key>expression</key><string>test.com</string>"
576
                . "<key>regex</key><false/></dict><dict><key>action</key><integer>1</integer>"
577
                . "<key>active</key><true/><key>expression</key>"
578
                . "<string>second.hello</string><key>regex</key><false/></dict></array>"
579
                . "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id=1</string>"
580
                . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
581
                . "<key>examSessionClearCookiesOnStart</key><false/>"
582
                . "<key>allowPreferencesWindow</key><false/></dict></plist>\n",
583
            ],
584
            'blocked simple expessions' => [
585
                (object) [
586
                    'requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
587
                    'quizid' => 1,
588
                    'cmid' => 1,
589
                    'expressionsallowed' => '',
590
                    'regexallowed' => '',
591
                    'expressionsblocked' => "test.com\r\nsecond.hello",
592
                    'regexblocked' => '',
593
                ],
594
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
595
                . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
596
                . "<plist version=\"1.0\"><dict><key>showTaskBar</key><true/>"
597
                . "<key>allowWlan</key><false/><key>showReloadButton</key>"
598
                . "<true/><key>showTime</key><true/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
599
                . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><false/><key>audioMute</key><false/>"
1441 ariadna 600
                . "<key>browserMediaCaptureCamera</key><false/><key>browserMediaCaptureMicrophone</key><false/>"
1 efrain 601
                . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><false/>"
602
                . "<key>URLFilterEnableContentFilter</key><false/><key>URLFilterRules</key><array>"
603
                . "<dict><key>action</key><integer>0</integer><key>active</key><true/>"
604
                . "<key>expression</key><string>test.com</string>"
605
                . "<key>regex</key><false/></dict><dict><key>action</key><integer>0</integer>"
606
                . "<key>active</key><true/><key>expression</key>"
607
                . "<string>second.hello</string><key>regex</key><false/></dict></array>"
608
                . "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id=1</string>"
609
                . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
610
                . "<key>examSessionClearCookiesOnStart</key><false/>"
611
                . "<key>allowPreferencesWindow</key><false/></dict></plist>\n",
612
            ],
613
            'enabled regex expessions' => [
614
                (object) [
615
                    'requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
616
                    'quizid' => 1,
617
                    'cmid' => 1,
618
                    'expressionsallowed' => '',
619
                    'regexallowed' => "test.com\r\nsecond.hello",
620
                    'expressionsblocked' => '',
621
                    'regexblocked' => '',
622
                ],
623
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
624
                . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
625
                . "<plist version=\"1.0\"><dict><key>showTaskBar</key><true/>"
626
                . "<key>allowWlan</key><false/><key>showReloadButton</key>"
627
                . "<true/><key>showTime</key><true/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
628
                . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><false/><key>audioMute</key><false/>"
1441 ariadna 629
                . "<key>browserMediaCaptureCamera</key><false/><key>browserMediaCaptureMicrophone</key><false/>"
1 efrain 630
                . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><false/>"
631
                . "<key>URLFilterEnableContentFilter</key><false/><key>URLFilterRules</key><array>"
632
                . "<dict><key>action</key><integer>1</integer><key>active</key><true/>"
633
                . "<key>expression</key><string>test.com</string>"
634
                . "<key>regex</key><true/></dict><dict><key>action</key><integer>1</integer>"
635
                . "<key>active</key><true/><key>expression</key>"
636
                . "<string>second.hello</string><key>regex</key><true/></dict></array>"
637
                . "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id=1</string>"
638
                . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
639
                . "<key>examSessionClearCookiesOnStart</key><false/>"
640
                . "<key>allowPreferencesWindow</key><false/></dict></plist>\n",
641
            ],
642
            'blocked regex expessions' => [
643
                (object) [
644
                    'requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
645
                    'quizid' => 1,
646
                    'cmid' => 1,
647
                    'expressionsallowed' => '',
648
                    'regexallowed' => '',
649
                    'expressionsblocked' => '',
650
                    'regexblocked' => "test.com\r\nsecond.hello",
651
                ],
652
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
653
                . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
654
                . "<plist version=\"1.0\"><dict><key>showTaskBar</key><true/>"
655
                . "<key>allowWlan</key><false/><key>showReloadButton</key>"
656
                . "<true/><key>showTime</key><true/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
657
                . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><false/><key>audioMute</key><false/>"
1441 ariadna 658
                . "<key>browserMediaCaptureCamera</key><false/><key>browserMediaCaptureMicrophone</key><false/>"
1 efrain 659
                . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><false/>"
660
                . "<key>URLFilterEnableContentFilter</key><false/><key>URLFilterRules</key><array>"
661
                . "<dict><key>action</key><integer>0</integer><key>active</key><true/>"
662
                . "<key>expression</key><string>test.com</string>"
663
                . "<key>regex</key><true/></dict><dict><key>action</key><integer>0</integer>"
664
                . "<key>active</key><true/><key>expression</key>"
665
                . "<string>second.hello</string><key>regex</key><true/></dict></array>"
666
                . "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id=1</string>"
667
                . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
668
                . "<key>examSessionClearCookiesOnStart</key><false/>"
669
                . "<key>allowPreferencesWindow</key><false/></dict></plist>\n",
670
            ],
671
            'multiple simple expessions' => [
672
                (object) [
673
                    'requiresafeexambrowser' => settings_provider::USE_SEB_CONFIG_MANUALLY,
674
                    'quizid' => 1,
675
                    'cmid' => 1,
676
                    'expressionsallowed' => "*",
677
                    'regexallowed' => '',
678
                    'expressionsblocked' => '',
679
                    'regexblocked' => "test.com\r\nsecond.hello",
680
                ],
681
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
682
                . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
683
                . "<plist version=\"1.0\"><dict><key>showTaskBar</key><true/>"
684
                . "<key>allowWlan</key><false/><key>showReloadButton</key>"
685
                . "<true/><key>showTime</key><true/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
686
                . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><false/><key>audioMute</key><false/>"
1441 ariadna 687
                . "<key>browserMediaCaptureCamera</key><false/><key>browserMediaCaptureMicrophone</key><false/>"
1 efrain 688
                . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><false/>"
689
                . "<key>URLFilterEnableContentFilter</key><false/><key>URLFilterRules</key><array><dict><key>action</key>"
690
                . "<integer>1</integer><key>active</key><true/><key>expression</key><string>*</string>"
691
                . "<key>regex</key><false/></dict>"
692
                . "<dict><key>action</key><integer>0</integer><key>active</key><true/>"
693
                . "<key>expression</key><string>test.com</string>"
694
                . "<key>regex</key><true/></dict><dict><key>action</key><integer>0</integer>"
695
                . "<key>active</key><true/><key>expression</key>"
696
                . "<string>second.hello</string><key>regex</key><true/></dict></array>"
697
                . "<key>startURL</key><string>https://www.example.com/moodle/mod/quiz/view.php?id=1</string>"
698
                . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
699
                . "<key>examSessionClearCookiesOnStart</key><false/>"
700
                . "<key>allowPreferencesWindow</key><false/></dict></plist>\n",
701
            ],
702
        ];
703
    }
704
 
705
    /**
706
     * Test that config and config key are null when expected.
707
     */
11 efrain 708
    public function test_generates_config_values_as_null_when_expected(): void {
1 efrain 709
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
710
        $this->assertNotNull($quizsettings->get_config());
711
        $this->assertNotNull($quizsettings->get_config_key());
712
 
713
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_NO);
714
        $quizsettings->save();
715
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
716
        $this->assertNull($quizsettings->get_config());
717
        $this->assertNull($quizsettings->get_config());
718
 
719
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
1441 ariadna 720
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 721
        $this->create_module_test_file($xml, $this->quiz->cmid);
722
        $quizsettings->save();
723
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
724
        $this->assertNotNull($quizsettings->get_config());
725
        $this->assertNotNull($quizsettings->get_config_key());
726
 
727
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_CLIENT_CONFIG);
728
        $quizsettings->save();
729
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
730
        $this->assertNull($quizsettings->get_config());
731
        $this->assertNull($quizsettings->get_config_key());
732
 
733
        $template = $this->create_template();
734
        $templateid = $template->get('id');
735
        $this->save_settings_with_optional_template($quizsettings, settings_provider::USE_SEB_TEMPLATE, $templateid);
736
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
737
        $this->assertNotNull($quizsettings->get_config());
738
        $this->assertNotNull($quizsettings->get_config_key());
739
    }
740
 
741
    /**
742
     * Test that quizsettings cache exists after creation.
743
     */
11 efrain 744
    public function test_quizsettings_cache_exists_after_creation(): void {
1 efrain 745
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
746
        $this->assertEquals($expected->to_record(), \cache::make('quizaccess_seb', 'quizsettings')->get($this->quiz->id));
747
    }
748
 
749
    /**
750
     * Test that quizsettings cache gets deleted after deletion.
751
     */
11 efrain 752
    public function test_quizsettings_cache_purged_after_deletion(): void {
1 efrain 753
        $this->assertNotEmpty(\cache::make('quizaccess_seb', 'quizsettings')->get($this->quiz->id));
754
 
755
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
756
        $quizsettings->delete();
757
 
758
        $this->assertFalse(\cache::make('quizaccess_seb', 'quizsettings')->get($this->quiz->id));
759
    }
760
 
761
    /**
762
     * Test that we can get seb_quiz_settings by quiz id.
763
     */
11 efrain 764
    public function test_get_quiz_settings_by_quiz_id(): void {
1 efrain 765
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
766
 
767
        $this->assertEquals($expected->to_record(), seb_quiz_settings::get_by_quiz_id($this->quiz->id)->to_record());
768
 
769
        // Check that data is getting from cache.
770
        $expected->set('showsebtaskbar', 0);
771
        $this->assertNotEquals($expected->to_record(), seb_quiz_settings::get_by_quiz_id($this->quiz->id)->to_record());
772
 
773
        // Now save and check that cached as been updated.
774
        $expected->save();
775
        $this->assertEquals($expected->to_record(), seb_quiz_settings::get_by_quiz_id($this->quiz->id)->to_record());
776
 
777
        // Returns false for non existing quiz.
778
        $this->assertFalse(seb_quiz_settings::get_by_quiz_id(7777777));
779
    }
780
 
781
    /**
782
     * Test that SEB config cache exists after creation of the quiz.
783
     */
11 efrain 784
    public function test_config_cache_exists_after_creation(): void {
1 efrain 785
        $this->assertNotEmpty(\cache::make('quizaccess_seb', 'config')->get($this->quiz->id));
786
    }
787
 
788
    /**
789
     * Test that SEB config cache gets deleted after deletion.
790
     */
11 efrain 791
    public function test_config_cache_purged_after_deletion(): void {
1 efrain 792
        $this->assertNotEmpty(\cache::make('quizaccess_seb', 'config')->get($this->quiz->id));
793
 
794
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
795
        $quizsettings->delete();
796
 
797
        $this->assertFalse(\cache::make('quizaccess_seb', 'config')->get($this->quiz->id));
798
    }
799
 
800
    /**
801
     * Test that we can get SEB config by quiz id.
802
     */
11 efrain 803
    public function test_get_config_by_quiz_id(): void {
1 efrain 804
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
805
        $expected = $quizsettings->get_config();
806
 
807
        $this->assertEquals($expected, seb_quiz_settings::get_config_by_quiz_id($this->quiz->id));
808
 
809
        // Check that data is getting from cache.
810
        $quizsettings->set('showsebtaskbar', 0);
811
        $this->assertNotEquals($quizsettings->get_config(), seb_quiz_settings::get_config_by_quiz_id($this->quiz->id));
812
 
813
        // Now save and check that cached as been updated.
814
        $quizsettings->save();
815
        $this->assertEquals($quizsettings->get_config(), seb_quiz_settings::get_config_by_quiz_id($this->quiz->id));
816
 
817
        // Returns null for non existing quiz.
818
        $this->assertNull(seb_quiz_settings::get_config_by_quiz_id(7777777));
819
    }
820
 
821
    /**
822
     * Test that SEB config key cache exists after creation of the quiz.
823
     */
11 efrain 824
    public function test_config_key_cache_exists_after_creation(): void {
1 efrain 825
        $this->assertNotEmpty(\cache::make('quizaccess_seb', 'configkey')->get($this->quiz->id));
826
    }
827
 
828
    /**
829
     * Test that SEB config key cache gets deleted after deletion.
830
     */
11 efrain 831
    public function test_config_key_cache_purged_after_deletion(): void {
1 efrain 832
        $this->assertNotEmpty(\cache::make('quizaccess_seb', 'configkey')->get($this->quiz->id));
833
 
834
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
835
        $quizsettings->delete();
836
 
837
        $this->assertFalse(\cache::make('quizaccess_seb', 'configkey')->get($this->quiz->id));
838
    }
839
 
840
    /**
841
     * Test that we can get SEB config key by quiz id.
842
     */
11 efrain 843
    public function test_get_config_key_by_quiz_id(): void {
1 efrain 844
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
845
        $expected = $quizsettings->get_config_key();
846
 
847
        $this->assertEquals($expected, seb_quiz_settings::get_config_key_by_quiz_id($this->quiz->id));
848
 
849
        // Check that data is getting from cache.
850
        $quizsettings->set('showsebtaskbar', 0);
851
        $this->assertNotEquals($quizsettings->get_config_key(), seb_quiz_settings::get_config_key_by_quiz_id($this->quiz->id));
852
 
853
        // Now save and check that cached as been updated.
854
        $quizsettings->save();
855
        $this->assertEquals($quizsettings->get_config_key(), seb_quiz_settings::get_config_key_by_quiz_id($this->quiz->id));
856
 
857
        // Returns null for non existing quiz.
858
        $this->assertNull(seb_quiz_settings::get_config_key_by_quiz_id(7777777));
859
    }
860
 
861
}