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
defined('MOODLE_INTERNAL') || die();
20
 
21
require_once(__DIR__ . '/test_helper_trait.php');
22
 
23
/**
24
 * PHPUnit tests for settings_provider.
25
 *
26
 * @package   quizaccess_seb
27
 * @author    Andrew Madden <andrewmadden@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 settings_provider_test extends \advanced_testcase {
1 efrain 32
    use \quizaccess_seb_test_helper_trait;
33
 
34
    /**
35
     * Mocked quiz form instance.
36
     * @var \mod_quiz_mod_form
37
     */
38
    protected $mockedquizform;
39
 
40
    /**
41
     * Test moodle form.
42
     * @var \MoodleQuickForm
43
     */
44
    protected $mockedform;
45
 
46
    /**
47
     * Context for testing.
48
     * @var \context
49
     */
50
    protected $context;
51
 
52
    /**
53
     * Test user.
54
     * @var \stdClass
55
     */
56
    protected $user;
57
 
58
    /**
59
     * Test role ID.
60
     * @var int
61
     */
62
    protected $roleid;
63
 
64
    /**
65
     * Helper method to set up form mocks.
66
     */
67
    protected function set_up_form_mocks() {
68
        if (empty($this->context)) {
69
            $this->context = \context_module::instance($this->quiz->cmid);
70
        }
71
 
72
        $this->mockedquizform = $this->createMock('mod_quiz_mod_form');
73
        $this->mockedquizform->method('get_context')->willReturn($this->context);
74
        $this->mockedquizform->method('get_instance')->willReturn($this->quiz->id);
75
        $this->mockedform = new \MoodleQuickForm('test', 'post', '');
76
        $this->mockedform->addElement('static', 'security');
77
    }
78
 
79
    /**
80
     * Helper method to set up user and role for testing.
81
     */
82
    protected function set_up_user_and_role() {
83
        $this->user = $this->getDataGenerator()->create_user();
84
 
85
        $this->setUser($this->user);
86
        $this->roleid = $this->getDataGenerator()->create_role();
87
 
88
        $this->getDataGenerator()->role_assign($this->roleid, $this->user->id, $this->context->id);
89
    }
90
 
91
    /**
92
     * Capability data for testing.
93
     *
94
     * @return array
95
     */
1441 ariadna 96
    public static function settings_capability_data_provider(): array {
1 efrain 97
        $data = [];
98
 
99
        // Build first level SEB config settings. Any of this setting let us use SEB manual config.
100
        foreach (settings_provider::get_seb_settings_map()[settings_provider::USE_SEB_CONFIG_MANUALLY] as $name => $children) {
101
            if (key_exists($name, settings_provider::get_seb_config_elements())) {
102
                $cap = settings_provider::build_setting_capability_name($name);
103
                $data[] = [$cap];
104
            }
105
        }
106
 
107
        return $data;
108
    }
109
 
110
    /**
111
     * Test that settings types to be added to quiz settings, are part of quiz_settings persistent class.
112
     */
11 efrain 113
    public function test_setting_elements_are_part_of_quiz_settings_table(): void {
1 efrain 114
        $dbsettings = (array) (new seb_quiz_settings())->to_record();
115
        $settingelements = settings_provider::get_seb_config_elements();
116
        $settingelements = (array) $this->strip_all_prefixes((object) $settingelements);
117
 
118
        // Get all elements to be added to form, that are not in the persistent quiz_settings class.
119
        $diffelements = array_diff_key($settingelements, $dbsettings);
120
 
121
        $this->assertEmpty($diffelements);
122
    }
123
 
124
    /**
125
     * Make sure that all SEB settings have related capabilities.
126
     */
11 efrain 127
    public function test_that_all_seb_settings_have_capabilities(): void {
1 efrain 128
        foreach (settings_provider::get_seb_config_elements() as $name => $notused) {
129
            $this->assertNotEmpty(get_capability_info(settings_provider::build_setting_capability_name($name)));
130
        }
131
    }
132
 
133
    /**
134
     * Test that setting defaults only refer to settings defined in setting types.
135
     */
11 efrain 136
    public function test_setting_defaults_are_part_of_file_types(): void {
1 efrain 137
        $settingelements = settings_provider::get_seb_config_elements();
138
        $settingdefaults = settings_provider::get_seb_config_element_defaults();
139
 
140
        // Get all defaults that have no matching element in settings types.
141
        $diffelements = array_diff_key($settingdefaults, $settingelements);
142
 
143
        $this->assertEmpty($diffelements);
144
    }
145
 
146
    /**
147
     * Test that setting types only refer to settings defined in setting types.
148
     */
11 efrain 149
    public function test_setting_types_are_part_of_file_types(): void {
1 efrain 150
        $settingelements = settings_provider::get_seb_config_elements();
151
        $settingtypes = settings_provider::get_seb_config_element_types();
152
 
153
        // Get all defaults that have no matching element in settings types.
154
        $diffelements = array_diff_key($settingtypes, $settingelements);
155
 
156
        $this->assertEmpty($diffelements);
157
    }
158
 
159
    /**
160
     * Helper method to assert hide if element.
161
     * @param hideif_rule $hideif Rule to check.
162
     * @param string $element Expected element.
163
     * @param string $dependantname Expected dependant element name.
164
     * @param string $condition Expected condition.
165
     * @param mixed $value Expected value.
166
     */
167
    protected function assert_hide_if(hideif_rule $hideif, $element, $dependantname, $condition, $value) {
168
        $this->assertEquals($element, $hideif->get_element());
169
        $this->assertEquals($dependantname, $hideif->get_dependantname());
170
        $this->assertEquals($condition, $hideif->get_condition());
171
        $this->assertEquals($value, $hideif->get_dependantvalue());
172
    }
173
 
174
    /**
175
     * Test hideif rules.
176
     */
11 efrain 177
    public function test_hideifs(): void {
1 efrain 178
        $settinghideifs = settings_provider::get_quiz_hideifs();
179
 
1441 ariadna 180
        $this->assertCount(25, $settinghideifs);
1 efrain 181
 
182
        $this->assertArrayHasKey('seb_templateid', $settinghideifs);
183
        $this->assertCount(1, $settinghideifs['seb_templateid']);
184
        $this->assert_hide_if(
185
            $settinghideifs['seb_templateid'][0],
186
            'seb_templateid',
187
            'seb_requiresafeexambrowser',
188
            'noteq',
189
            settings_provider::USE_SEB_TEMPLATE
190
        );
191
 
192
        $this->assertArrayHasKey('filemanager_sebconfigfile', $settinghideifs);
193
        $this->assertCount(1, $settinghideifs['filemanager_sebconfigfile']);
194
        $this->assert_hide_if(
195
            $settinghideifs['filemanager_sebconfigfile'][0],
196
            'filemanager_sebconfigfile',
197
            'seb_requiresafeexambrowser',
198
            'noteq',
199
            settings_provider::USE_SEB_UPLOAD_CONFIG
200
        );
201
 
202
        $this->assertArrayHasKey('seb_showsebtaskbar', $settinghideifs);
203
        $this->assertCount(1, $settinghideifs['seb_showsebtaskbar']);
204
        $this->assert_hide_if(
205
            $settinghideifs['seb_showsebtaskbar'][0],
206
            'seb_showsebtaskbar',
207
            'seb_requiresafeexambrowser',
208
            'noteq',
209
            settings_provider::USE_SEB_CONFIG_MANUALLY
210
        );
211
 
212
        $this->assertArrayHasKey('seb_showwificontrol', $settinghideifs);
213
        $this->assertCount(2, $settinghideifs['seb_showwificontrol']);
214
        $this->assert_hide_if(
215
            $settinghideifs['seb_showwificontrol'][0],
216
            'seb_showwificontrol',
217
            'seb_requiresafeexambrowser',
218
            'noteq',
219
            settings_provider::USE_SEB_CONFIG_MANUALLY
220
        );
221
        $this->assert_hide_if(
222
            $settinghideifs['seb_showwificontrol'][1],
223
            'seb_showwificontrol',
224
            'seb_showsebtaskbar',
225
            'eq',
226
 
227
        );
228
 
229
        $this->assertArrayHasKey('seb_showreloadbutton', $settinghideifs);
230
        $this->assertCount(2, $settinghideifs['seb_showreloadbutton']);
231
        $this->assert_hide_if(
232
            $settinghideifs['seb_showreloadbutton'][0],
233
            'seb_showreloadbutton',
234
            'seb_requiresafeexambrowser',
235
            'noteq',
236
            settings_provider::USE_SEB_CONFIG_MANUALLY
237
        );
238
        $this->assert_hide_if(
239
            $settinghideifs['seb_showreloadbutton'][1],
240
            'seb_showreloadbutton',
241
            'seb_showsebtaskbar',
242
            'eq',
243
 
244
        );
245
 
246
        $this->assertArrayHasKey('seb_showtime', $settinghideifs);
247
        $this->assertCount(2, $settinghideifs['seb_showtime']);
248
        $this->assert_hide_if(
249
            $settinghideifs['seb_showtime'][0],
250
            'seb_showtime',
251
            'seb_requiresafeexambrowser',
252
            'noteq',
253
            settings_provider::USE_SEB_CONFIG_MANUALLY
254
        );
255
        $this->assert_hide_if(
256
            $settinghideifs['seb_showtime'][1],
257
            'seb_showtime',
258
            'seb_showsebtaskbar',
259
            'eq',
260
 
261
        );
262
 
263
        $this->assertArrayHasKey('seb_showkeyboardlayout', $settinghideifs);
264
        $this->assertCount(2, $settinghideifs['seb_showkeyboardlayout']);
265
        $this->assert_hide_if(
266
            $settinghideifs['seb_showkeyboardlayout'][0],
267
            'seb_showkeyboardlayout',
268
            'seb_requiresafeexambrowser',
269
            'noteq',
270
            settings_provider::USE_SEB_CONFIG_MANUALLY
271
        );
272
        $this->assert_hide_if(
273
            $settinghideifs['seb_showkeyboardlayout'][1],
274
            'seb_showkeyboardlayout',
275
            'seb_showsebtaskbar',
276
            'eq',
277
 
278
        );
279
 
280
        $this->assertArrayHasKey('seb_allowuserquitseb', $settinghideifs);
281
        $this->assertCount(3, $settinghideifs['seb_allowuserquitseb']);
282
        $this->assert_hide_if(
283
            $settinghideifs['seb_allowuserquitseb'][0],
284
            'seb_allowuserquitseb',
285
            'seb_requiresafeexambrowser',
286
            'eq',
287
            settings_provider::USE_SEB_NO
288
        );
289
        $this->assert_hide_if(
290
            $settinghideifs['seb_allowuserquitseb'][1],
291
            'seb_allowuserquitseb',
292
            'seb_requiresafeexambrowser',
293
            'eq',
294
            settings_provider::USE_SEB_CLIENT_CONFIG
295
        );
296
        $this->assert_hide_if(
297
            $settinghideifs['seb_allowuserquitseb'][2],
298
            'seb_allowuserquitseb',
299
            'seb_requiresafeexambrowser',
300
            'eq',
301
            settings_provider::USE_SEB_UPLOAD_CONFIG
302
        );
303
 
304
        $this->assertArrayHasKey('seb_quitpassword', $settinghideifs);
305
        $this->assertCount(4, $settinghideifs['seb_quitpassword']);
306
        $this->assert_hide_if(
307
            $settinghideifs['seb_quitpassword'][0],
308
            'seb_quitpassword',
309
            'seb_requiresafeexambrowser',
310
            'eq',
311
            settings_provider::USE_SEB_NO
312
        );
313
        $this->assert_hide_if(
314
            $settinghideifs['seb_quitpassword'][1],
315
            'seb_quitpassword',
316
            'seb_requiresafeexambrowser',
317
            'eq',
318
            settings_provider::USE_SEB_CLIENT_CONFIG
319
        );
320
        $this->assert_hide_if(
321
            $settinghideifs['seb_quitpassword'][2],
322
            'seb_quitpassword',
323
            'seb_requiresafeexambrowser',
324
            'eq',
325
            settings_provider::USE_SEB_UPLOAD_CONFIG
326
        );
327
        $this->assert_hide_if(
328
            $settinghideifs['seb_quitpassword'][3],
329
            'seb_quitpassword',
330
            'seb_allowuserquitseb',
331
            'eq',
332
 
333
        );
334
 
335
        $this->assertArrayHasKey('seb_linkquitseb', $settinghideifs);
336
        $this->assertCount(1, $settinghideifs['seb_linkquitseb']);
337
        $this->assert_hide_if(
338
            $settinghideifs['seb_linkquitseb'][0],
339
            'seb_linkquitseb',
340
            'seb_requiresafeexambrowser',
341
            'noteq',
342
            settings_provider::USE_SEB_CONFIG_MANUALLY
343
        );
344
 
345
        $this->assertArrayHasKey('seb_userconfirmquit', $settinghideifs);
346
        $this->assertCount(1, $settinghideifs['seb_userconfirmquit']);
347
        $this->assert_hide_if(
348
            $settinghideifs['seb_userconfirmquit'][0],
349
            'seb_userconfirmquit',
350
            'seb_requiresafeexambrowser',
351
            'noteq',
352
            settings_provider::USE_SEB_CONFIG_MANUALLY
353
        );
354
 
355
        $this->assertArrayHasKey('seb_enableaudiocontrol', $settinghideifs);
356
        $this->assertCount(1, $settinghideifs['seb_enableaudiocontrol']);
357
        $this->assert_hide_if(
358
            $settinghideifs['seb_enableaudiocontrol'][0],
359
            'seb_enableaudiocontrol',
360
            'seb_requiresafeexambrowser',
361
            'noteq',
362
            settings_provider::USE_SEB_CONFIG_MANUALLY
363
        );
364
 
1441 ariadna 365
        $this->assertArrayHasKey('seb_allowcapturecamera', $settinghideifs);
366
        $this->assertCount(1, $settinghideifs['seb_allowcapturecamera']);
367
        $this->assert_hide_if(
368
            $settinghideifs['seb_allowcapturecamera'][0],
369
            'seb_allowcapturecamera',
370
            'seb_requiresafeexambrowser',
371
            'noteq',
372
            settings_provider::USE_SEB_CONFIG_MANUALLY
373
        );
374
 
375
        $this->assertArrayHasKey('seb_allowcapturemicrophone', $settinghideifs);
376
        $this->assertCount(1, $settinghideifs['seb_allowcapturemicrophone']);
377
        $this->assert_hide_if(
378
            $settinghideifs['seb_allowcapturemicrophone'][0],
379
            'seb_allowcapturemicrophone',
380
            'seb_requiresafeexambrowser',
381
            'noteq',
382
            settings_provider::USE_SEB_CONFIG_MANUALLY
383
        );
384
 
1 efrain 385
        $this->assertArrayHasKey('seb_muteonstartup', $settinghideifs);
386
        $this->assertCount(2, $settinghideifs['seb_muteonstartup']);
387
        $this->assert_hide_if(
388
            $settinghideifs['seb_muteonstartup'][0],
389
            'seb_muteonstartup',
390
            'seb_requiresafeexambrowser',
391
            'noteq',
392
            settings_provider::USE_SEB_CONFIG_MANUALLY
393
        );
394
        $this->assert_hide_if(
395
            $settinghideifs['seb_muteonstartup'][1],
396
            'seb_muteonstartup',
397
            'seb_enableaudiocontrol',
398
            'eq',
399
 
400
        );
401
 
402
        $this->assertArrayHasKey('seb_allowspellchecking', $settinghideifs);
403
        $this->assertCount(1, $settinghideifs['seb_allowspellchecking']);
404
        $this->assert_hide_if(
405
            $settinghideifs['seb_allowspellchecking'][0],
406
            'seb_allowspellchecking',
407
            'seb_requiresafeexambrowser',
408
            'noteq',
409
            settings_provider::USE_SEB_CONFIG_MANUALLY
410
        );
411
 
412
        $this->assertArrayHasKey('seb_allowreloadinexam', $settinghideifs);
413
        $this->assertCount(1, $settinghideifs['seb_allowreloadinexam']);
414
        $this->assert_hide_if(
415
            $settinghideifs['seb_allowreloadinexam'][0],
416
            'seb_allowreloadinexam',
417
            'seb_requiresafeexambrowser',
418
            'noteq',
419
            settings_provider::USE_SEB_CONFIG_MANUALLY
420
        );
421
 
422
        $this->assertArrayHasKey('seb_activateurlfiltering', $settinghideifs);
423
        $this->assertCount(1, $settinghideifs['seb_activateurlfiltering']);
424
        $this->assert_hide_if(
425
            $settinghideifs['seb_activateurlfiltering'][0],
426
            'seb_activateurlfiltering',
427
            'seb_requiresafeexambrowser',
428
            'noteq',
429
            settings_provider::USE_SEB_CONFIG_MANUALLY
430
        );
431
 
432
        $this->assertArrayHasKey('seb_filterembeddedcontent', $settinghideifs);
433
        $this->assertCount(2, $settinghideifs['seb_filterembeddedcontent']);
434
        $this->assert_hide_if(
435
            $settinghideifs['seb_filterembeddedcontent'][0],
436
            'seb_filterembeddedcontent',
437
            'seb_requiresafeexambrowser',
438
            'noteq',
439
            settings_provider::USE_SEB_CONFIG_MANUALLY
440
        );
441
        $this->assert_hide_if(
442
            $settinghideifs['seb_filterembeddedcontent'][1],
443
            'seb_filterembeddedcontent',
444
            'seb_activateurlfiltering',
445
            'eq',
446
 
447
        );
448
 
449
        $this->assertArrayHasKey('seb_expressionsallowed', $settinghideifs);
450
        $this->assertCount(2, $settinghideifs['seb_expressionsallowed']);
451
        $this->assert_hide_if(
452
            $settinghideifs['seb_expressionsallowed'][0],
453
            'seb_expressionsallowed',
454
            'seb_requiresafeexambrowser',
455
            'noteq',
456
            settings_provider::USE_SEB_CONFIG_MANUALLY
457
        );
458
        $this->assert_hide_if(
459
            $settinghideifs['seb_expressionsallowed'][1],
460
            'seb_expressionsallowed',
461
            'seb_activateurlfiltering',
462
            'eq',
463
 
464
        );
465
 
466
        $this->assertArrayHasKey('seb_regexallowed', $settinghideifs);
467
        $this->assertCount(2, $settinghideifs['seb_regexallowed']);
468
        $this->assert_hide_if(
469
            $settinghideifs['seb_regexallowed'][0],
470
            'seb_regexallowed',
471
            'seb_requiresafeexambrowser',
472
            'noteq',
473
            settings_provider::USE_SEB_CONFIG_MANUALLY
474
        );
475
        $this->assert_hide_if(
476
            $settinghideifs['seb_regexallowed'][1],
477
            'seb_regexallowed',
478
            'seb_activateurlfiltering',
479
            'eq',
480
 
481
        );
482
 
483
        $this->assertArrayHasKey('seb_expressionsblocked', $settinghideifs);
484
        $this->assertCount(2, $settinghideifs['seb_expressionsblocked']);
485
        $this->assert_hide_if(
486
            $settinghideifs['seb_expressionsblocked'][0],
487
            'seb_expressionsblocked',
488
            'seb_requiresafeexambrowser',
489
            'noteq',
490
            settings_provider::USE_SEB_CONFIG_MANUALLY
491
        );
492
        $this->assert_hide_if(
493
            $settinghideifs['seb_expressionsblocked'][1],
494
            'seb_expressionsblocked',
495
            'seb_activateurlfiltering',
496
            'eq',
497
 
498
        );
499
 
500
        $this->assertArrayHasKey('seb_regexblocked', $settinghideifs);
501
        $this->assertCount(2, $settinghideifs['seb_regexblocked']);
502
        $this->assert_hide_if(
503
            $settinghideifs['seb_regexblocked'][0],
504
            'seb_regexblocked',
505
            'seb_requiresafeexambrowser',
506
            'noteq',
507
            settings_provider::USE_SEB_CONFIG_MANUALLY
508
        );
509
        $this->assert_hide_if(
510
            $settinghideifs['seb_regexblocked'][1],
511
            'seb_regexblocked',
512
            'seb_activateurlfiltering',
513
            'eq',
514
 
515
        );
516
 
517
        $this->assertArrayHasKey('seb_showsebdownloadlink', $settinghideifs);
518
        $this->assertCount(1, $settinghideifs['seb_showsebdownloadlink']);
519
        $this->assert_hide_if(
520
            $settinghideifs['seb_showsebdownloadlink'][0],
521
            'seb_showsebdownloadlink',
522
            'seb_requiresafeexambrowser',
523
            'eq',
524
            settings_provider::USE_SEB_NO
525
        );
526
 
527
        $this->assertArrayHasKey('seb_allowedbrowserexamkeys', $settinghideifs);
528
        $this->assertCount(3, $settinghideifs['seb_allowedbrowserexamkeys']);
529
        $this->assert_hide_if(
530
            $settinghideifs['seb_allowedbrowserexamkeys'][0],
531
            'seb_allowedbrowserexamkeys',
532
            'seb_requiresafeexambrowser',
533
            'eq',
534
            settings_provider::USE_SEB_NO
535
        );
536
        $this->assert_hide_if(
537
            $settinghideifs['seb_allowedbrowserexamkeys'][1],
538
            'seb_allowedbrowserexamkeys',
539
            'seb_requiresafeexambrowser',
540
            'eq',
541
            settings_provider::USE_SEB_CONFIG_MANUALLY
542
        );
543
        $this->assert_hide_if(
544
            $settinghideifs['seb_allowedbrowserexamkeys'][2],
545
            'seb_allowedbrowserexamkeys',
546
            'seb_requiresafeexambrowser',
547
            'eq',
548
            settings_provider::USE_SEB_TEMPLATE
549
        );
550
    }
551
 
552
    /**
553
     * Test that setting hideif rules only refer to settings defined in setting types, including the conditions.
554
     */
11 efrain 555
    public function test_setting_hideifs_are_part_of_file_types(): void {
1 efrain 556
        $settingelements = settings_provider::get_seb_config_elements();
557
        $settinghideifs = settings_provider::get_quiz_hideifs();
558
 
559
        // Add known additional elements.
560
        $settingelements['seb_templateid'] = '';
561
        $settingelements['filemanager_sebconfigfile'] = '';
562
        $settingelements['seb_showsebdownloadlink'] = '';
563
        $settingelements['seb_allowedbrowserexamkeys'] = '';
564
 
565
        // Get all defaults that have no matching element in settings types.
566
        $diffelements = array_diff_key($settinghideifs, $settingelements);
567
 
568
        // Check no diff for elements to hide.
569
        $this->assertEmpty($diffelements);
570
 
571
        // Check each element's to hide conditions that each condition refers to element in settings types.
572
        foreach ($settinghideifs as $conditions) {
573
            foreach ($conditions as $condition) {
574
                $this->assertTrue(array_key_exists($condition->get_element(), $settingelements));
575
            }
576
        }
577
    }
578
 
579
    /**
580
     * Test that exception thrown if we try to build capability name from the incorrect setting name.
581
     */
11 efrain 582
    public function test_build_setting_capability_name_incorrect_setting(): void {
1 efrain 583
        $this->expectException(\coding_exception::class);
584
        $this->expectExceptionMessage('Incorrect SEB quiz setting broken');
585
 
586
        $broken = settings_provider::build_setting_capability_name('broken');
587
    }
588
 
589
    /**
590
     * Test we can build capability name from the the setting name.
591
     */
11 efrain 592
    public function test_build_setting_capability_name_correct_setting(): void {
1 efrain 593
        foreach (settings_provider::get_seb_config_elements() as $name => $type) {
594
            $expected = 'quizaccess/seb:manage_' . $name;
595
            $actual = settings_provider::build_setting_capability_name($name);
596
 
597
            $this->assertSame($expected, $actual);
598
        }
599
    }
600
 
601
 
602
    /**
603
     * Test can check if can manage SEB settings respecting settings structure.
604
     */
11 efrain 605
    public function test_can_manage_seb_config_setting(): void {
1 efrain 606
        $this->resetAfterTest();
607
        $this->setAdminUser();
608
        $this->course = $this->getDataGenerator()->create_course();
609
 
610
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
611
        $this->context = \context_module::instance($this->quiz->cmid);
612
 
613
        $this->set_up_user_and_role();
614
 
615
        foreach (settings_provider::get_seb_settings_map()[settings_provider::USE_SEB_CONFIG_MANUALLY] as $setting => $children) {
616
            // Skip not SEB setting.
617
            if ($setting == 'seb_showsebdownloadlink') {
618
                continue;
619
            }
620
 
621
            $this->assertFalse(settings_provider::can_manage_seb_config_setting($setting, $this->context));
622
            foreach ($children as $child => $empty) {
623
                $this->assertFalse(settings_provider::can_manage_seb_config_setting($child, $this->context));
624
 
625
                // Assign child capability without having parent one. Should not have access to manage child.
626
                $childcap = settings_provider::build_setting_capability_name($child);
627
                assign_capability($childcap, CAP_ALLOW, $this->roleid, $this->context->id);
628
                $this->assertFalse(settings_provider::can_manage_seb_config_setting($child, $this->context));
629
            }
630
 
631
            // Assign parent capability. Should be able to manage children now.
632
            $parentcap = settings_provider::build_setting_capability_name($setting);
633
            assign_capability($parentcap, CAP_ALLOW, $this->roleid, $this->context->id);
634
 
635
            $this->assertTrue(settings_provider::can_manage_seb_config_setting($setting, $this->context));
636
            foreach ($children as $child => $empty) {
637
                $this->assertTrue(settings_provider::can_manage_seb_config_setting($child, $this->context));
638
            }
639
        }
640
    }
641
 
642
    /**
643
     * Test SEB usage options.
644
     *
1441 ariadna 645
     * @param string $settingcapability Setting capability to check options against.
1 efrain 646
     *
647
     * @dataProvider settings_capability_data_provider
648
     */
11 efrain 649
    public function test_get_requiresafeexambrowser_options($settingcapability): void {
1 efrain 650
        $this->resetAfterTest();
651
        $this->setAdminUser();
652
        $this->course = $this->getDataGenerator()->create_course();
653
 
654
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
655
        $this->context = \context_module::instance($this->quiz->cmid);
656
 
657
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
658
 
659
        $this->assertCount(4, $options);
660
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_NO, $options));
661
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CONFIG_MANUALLY, $options));
662
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
663
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_UPLOAD_CONFIG, $options));
664
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CLIENT_CONFIG, $options));
665
 
666
        // Create a template.
667
        $this->create_template();
668
 
669
        // The template options should be visible now.
670
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
671
        $this->assertCount(5, $options);
672
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
673
 
674
        // A new user does not have the capability to use the file manager and template.
675
        $this->set_up_user_and_role();
676
 
677
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
678
 
1441 ariadna 679
        $this->assertCount(1, $options);
1 efrain 680
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_CONFIG_MANUALLY, $options));
681
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
682
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_UPLOAD_CONFIG, $options));
1441 ariadna 683
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_CLIENT_CONFIG, $options));
1 efrain 684
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_NO, $options));
685
 
686
        assign_capability($settingcapability, CAP_ALLOW, $this->roleid, $this->context->id);
687
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
1441 ariadna 688
        $this->assertCount(1, $options);
689
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_CONFIG_MANUALLY, $options));
690
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
691
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_UPLOAD_CONFIG, $options));
692
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_CLIENT_CONFIG, $options));
693
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_NO, $options));
694
 
695
        assign_capability('quizaccess/seb:manage_seb_configuremanually', CAP_ALLOW, $this->roleid, $this->context->id);
696
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
697
        $this->assertCount(2, $options);
698
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CONFIG_MANUALLY, $options));
699
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
700
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_UPLOAD_CONFIG, $options));
701
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_CLIENT_CONFIG, $options));
702
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_NO, $options));
703
 
704
        assign_capability('quizaccess/seb:manage_seb_usesebclientconfig', CAP_ALLOW, $this->roleid, $this->context->id);
705
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
1 efrain 706
        $this->assertCount(3, $options);
707
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CONFIG_MANUALLY, $options));
708
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
709
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_UPLOAD_CONFIG, $options));
710
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CLIENT_CONFIG, $options));
711
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_NO, $options));
712
 
713
        assign_capability('quizaccess/seb:manage_seb_templateid', CAP_ALLOW, $this->roleid, $this->context->id);
714
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
715
        $this->assertCount(4, $options);
716
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CONFIG_MANUALLY, $options));
717
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
718
        $this->assertFalse(array_key_exists(settings_provider::USE_SEB_UPLOAD_CONFIG, $options));
719
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CLIENT_CONFIG, $options));
720
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_NO, $options));
721
 
722
        assign_capability('quizaccess/seb:manage_filemanager_sebconfigfile', CAP_ALLOW, $this->roleid, $this->context->id);
723
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
724
        $this->assertCount(5, $options);
725
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CONFIG_MANUALLY, $options));
726
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
727
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_UPLOAD_CONFIG, $options));
728
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_CLIENT_CONFIG, $options));
729
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_NO, $options));
730
    }
731
 
732
    /**
733
     * Test SEB usage options with conflicting permissions.
734
     */
11 efrain 735
    public function test_get_requiresafeexambrowser_options_with_conflicting_permissions(): void {
1 efrain 736
        $this->resetAfterTest();
737
        $this->setAdminUser();
738
        $this->course = $this->getDataGenerator()->create_course();
739
 
740
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
741
        $this->context = \context_module::instance($this->quiz->cmid);
742
 
743
        $template = $this->create_template();
744
 
745
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
746
        $settings->set('templateid', $template->get('id'));
747
        $settings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
748
        $settings->save();
749
 
750
        $this->set_up_user_and_role();
751
 
752
        $options = settings_provider::get_requiresafeexambrowser_options($this->context);
753
 
754
        // If there is nay conflict we return full list of options.
755
        $this->assertCount(5, $options);
756
        $this->assertTrue(array_key_exists(settings_provider::USE_SEB_TEMPLATE, $options));
757
    }
758
 
759
    /**
760
     * Test that SEB options and templates are frozen if conflicting permissions.
761
     */
11 efrain 762
    public function test_form_elements_are_frozen_if_conflicting_permissions(): void {
1 efrain 763
        $this->resetAfterTest();
764
        $this->setAdminUser();
765
        $this->course = $this->getDataGenerator()->create_course();
766
 
767
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
768
        $this->context = \context_module::instance($this->quiz->cmid);
769
 
770
        // Setup conflicting permissions.
771
        $template = $this->create_template();
772
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
773
        $settings->set('templateid', $template->get('id'));
774
        $settings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
775
        $settings->save();
776
 
777
        $this->set_up_user_and_role();
778
 
779
        assign_capability('quizaccess/seb:manage_seb_requiresafeexambrowser', CAP_ALLOW, $this->roleid, $this->context->id);
780
        assign_capability('quizaccess/seb:manage_seb_showsebdownloadlink', CAP_ALLOW, $this->roleid, $this->context->id);
781
        assign_capability('quizaccess/seb:manage_seb_allowedbrowserexamkeys', CAP_ALLOW, $this->roleid, $this->context->id);
782
 
783
        $this->set_up_form_mocks();
784
 
785
        settings_provider::add_seb_settings_fields($this->mockedquizform, $this->mockedform);
786
 
787
        $this->assertTrue($this->mockedform->isElementFrozen('seb_requiresafeexambrowser'));
788
        $this->assertTrue($this->mockedform->isElementFrozen('seb_templateid'));
789
        $this->assertTrue($this->mockedform->isElementFrozen('seb_showsebdownloadlink'));
790
        $this->assertTrue($this->mockedform->isElementFrozen('seb_allowedbrowserexamkeys'));
791
    }
792
 
793
    /**
794
     * Test that All settings are frozen if quiz was attempted and use seb with manual settings.
795
     */
11 efrain 796
    public function test_form_elements_are_locked_when_quiz_attempted_manual(): void {
1 efrain 797
        $this->resetAfterTest();
798
        $this->course = $this->getDataGenerator()->create_course();
799
 
800
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
801
        $this->context = \context_module::instance($this->quiz->cmid);
802
 
803
        $user = $this->getDataGenerator()->create_user();
804
        $this->attempt_quiz($this->quiz, $user);
805
 
806
        $this->setAdminUser();
807
        $this->set_up_form_mocks();
808
 
809
        settings_provider::add_seb_settings_fields($this->mockedquizform, $this->mockedform);
810
 
811
        $this->assertTrue($this->mockedform->isElementFrozen('seb_requiresafeexambrowser'));
812
        $this->assertTrue($this->mockedform->elementExists('filemanager_sebconfigfile'));
813
        $this->assertFalse($this->mockedform->elementExists('seb_templateid'));
814
        $this->assertTrue($this->mockedform->isElementFrozen('seb_showsebdownloadlink'));
815
        $this->assertTrue($this->mockedform->isElementFrozen('seb_allowedbrowserexamkeys'));
816
 
817
        foreach (settings_provider::get_seb_config_elements() as $name => $type) {
818
            $this->assertTrue($this->mockedform->isElementFrozen($name));
819
        }
820
    }
821
 
822
    /**
823
     * Test that All settings are frozen if a quiz was attempted and use template.
824
     */
11 efrain 825
    public function test_form_elements_are_locked_when_quiz_attempted_template(): void {
1 efrain 826
        $this->resetAfterTest();
827
        $this->setAdminUser();
828
        $this->course = $this->getDataGenerator()->create_course();
829
 
830
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
831
        $this->context = \context_module::instance($this->quiz->cmid);
832
 
833
        $template = $this->create_template();
834
 
835
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
836
        $settings->set('templateid', $template->get('id'));
837
        $settings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
838
        $settings->save();
839
 
840
        $user = $this->getDataGenerator()->create_user();
841
        $this->attempt_quiz($this->quiz, $user);
842
 
843
        $this->setAdminUser();
844
        $this->set_up_form_mocks();
845
 
846
        settings_provider::add_seb_settings_fields($this->mockedquizform, $this->mockedform);
847
 
848
        $this->assertTrue($this->mockedform->isElementFrozen('seb_requiresafeexambrowser'));
849
        $this->assertTrue($this->mockedform->elementExists('filemanager_sebconfigfile'));
850
        $this->assertTrue($this->mockedform->isElementFrozen('seb_templateid'));
851
        $this->assertTrue($this->mockedform->isElementFrozen('seb_showsebdownloadlink'));
852
        $this->assertTrue($this->mockedform->isElementFrozen('seb_allowedbrowserexamkeys'));
853
 
854
        foreach (settings_provider::get_seb_config_elements() as $name => $type) {
855
            $this->assertTrue($this->mockedform->isElementFrozen($name));
856
        }
857
    }
858
 
859
    /**
860
     * Test Show Safe Exam Browser download button setting in the form.
861
     */
11 efrain 862
    public function test_showsebdownloadlink_in_form(): void {
1 efrain 863
        $this->resetAfterTest();
864
        $this->setAdminUser();
865
        $this->course = $this->getDataGenerator()->create_course();
866
 
867
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
868
        $this->context = \context_module::instance($this->quiz->cmid);
869
 
870
        $this->set_up_user_and_role();
871
 
872
        assign_capability('quizaccess/seb:manage_seb_requiresafeexambrowser', CAP_ALLOW, $this->roleid, $this->context->id);
873
        $this->set_up_form_mocks();
874
 
875
        // Shouldn't be in the form if no permissions.
876
        settings_provider::add_seb_settings_fields($this->mockedquizform, $this->mockedform);
877
        $this->assertFalse($this->mockedform->elementExists('seb_showsebdownloadlink'));
878
 
879
        // Should be in the form if we grant require permissions.
880
        assign_capability('quizaccess/seb:manage_seb_showsebdownloadlink', CAP_ALLOW, $this->roleid, $this->context->id);
881
 
882
        settings_provider::add_seb_settings_fields($this->mockedquizform, $this->mockedform);
883
        $this->assertTrue($this->mockedform->elementExists('seb_showsebdownloadlink'));
884
    }
885
 
886
    /**
887
     * Test Allowed Browser Exam Keys setting in the form.
888
     */
11 efrain 889
    public function test_allowedbrowserexamkeys_in_form(): void {
1 efrain 890
        $this->resetAfterTest();
891
        $this->setAdminUser();
892
        $this->course = $this->getDataGenerator()->create_course();
893
 
894
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
895
        $this->context = \context_module::instance($this->quiz->cmid);
896
 
897
        $this->set_up_user_and_role();
898
 
899
        assign_capability('quizaccess/seb:manage_seb_requiresafeexambrowser', CAP_ALLOW, $this->roleid, $this->context->id);
900
        $this->set_up_form_mocks();
901
 
902
        // Shouldn't be in the form if no permissions.
903
        settings_provider::add_seb_settings_fields($this->mockedquizform, $this->mockedform);
904
        $this->assertFalse($this->mockedform->elementExists('seb_allowedbrowserexamkeys'));
905
 
906
        // Should be in the form if we grant require permissions.
907
        assign_capability('quizaccess/seb:manage_seb_allowedbrowserexamkeys', CAP_ALLOW, $this->roleid, $this->context->id);
908
        settings_provider::add_seb_settings_fields($this->mockedquizform, $this->mockedform);
909
        $this->assertTrue($this->mockedform->elementExists('seb_allowedbrowserexamkeys'));
910
    }
911
 
912
    /**
913
     * Test the validation of a seb config file.
914
     */
11 efrain 915
    public function test_validate_draftarea_configfile_success(): void {
1 efrain 916
        $this->resetAfterTest();
917
 
918
        $user = $this->getDataGenerator()->create_user();
919
        $this->setUser($user);
920
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
921
            . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
922
            . "<plist version=\"1.0\"><dict><key>hashedQuitPassword</key><string>hashedpassword</string>"
923
            . "<key>allowWlan</key><false/></dict></plist>\n";
924
        $itemid = $this->create_test_draftarea_file($xml);
925
        $errors = settings_provider::validate_draftarea_configfile($itemid);
926
        $this->assertEmpty($errors);
927
    }
928
 
929
    /**
930
     * Test the validation of a missing seb config file.
931
     */
11 efrain 932
    public function test_validate_draftarea_configfile_failure(): void {
1 efrain 933
        $this->resetAfterTest();
934
 
935
        $user = $this->getDataGenerator()->create_user();
936
        $this->setUser($user);
937
        $xml = "This is not a config file.";
938
        $itemid = $this->create_test_draftarea_file($xml);
939
        $errors = settings_provider::validate_draftarea_configfile($itemid);
940
        $this->assertEquals($errors, new \lang_string('fileparsefailed', 'quizaccess_seb'));
941
    }
942
 
943
    /**
944
     * Test obtaining the draftarea content.
945
     */
11 efrain 946
    public function test_get_current_user_draft_file(): void {
1 efrain 947
        $this->resetAfterTest();
948
 
949
        $user = $this->getDataGenerator()->create_user();
950
        $this->setUser($user);
951
 
1441 ariadna 952
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 953
        $itemid = $this->create_test_draftarea_file($xml);
954
        $file = settings_provider::get_current_user_draft_file($itemid);
955
        $content = $file->get_content();
956
 
957
        $this->assertEquals($xml, $content);
958
    }
959
 
960
    /**
961
     * Test saving files from the user draft area into the quiz context area storage.
962
     */
11 efrain 963
    public function test_save_filemanager_sebconfigfile_draftarea(): void {
1 efrain 964
        $this->resetAfterTest();
965
        $this->course = $this->getDataGenerator()->create_course();
966
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
967
        $this->context = \context_module::instance($this->quiz->cmid);
968
        $this->set_up_user_and_role();
969
 
1441 ariadna 970
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 971
 
972
        $draftitemid = $this->create_test_draftarea_file($xml);
973
 
974
        settings_provider::save_filemanager_sebconfigfile_draftarea($draftitemid, $this->quiz->cmid);
975
 
976
        $fs = get_file_storage();
977
        $files = $fs->get_area_files($this->context->id, 'quizaccess_seb', 'filemanager_sebconfigfile');
978
 
979
        $this->assertCount(2, $files);
980
    }
981
 
982
    /**
983
     * Test deleting the $this->quiz->cmid itemid from the file area.
984
     */
11 efrain 985
    public function test_delete_uploaded_config_file(): void {
1 efrain 986
        $this->resetAfterTest();
987
        $this->course = $this->getDataGenerator()->create_course();
988
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
989
        $this->context = \context_module::instance($this->quiz->cmid);
990
        $this->set_up_user_and_role();
991
 
1441 ariadna 992
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 993
        $draftitemid = $this->create_test_draftarea_file($xml);
994
 
995
        settings_provider::save_filemanager_sebconfigfile_draftarea($draftitemid, $this->quiz->cmid);
996
 
997
        $fs = get_file_storage();
998
        $files = $fs->get_area_files($this->context->id, 'quizaccess_seb', 'filemanager_sebconfigfile');
999
        $this->assertCount(2, $files);
1000
 
1001
        settings_provider::delete_uploaded_config_file($this->quiz->cmid);
1002
 
1003
        $files = $fs->get_area_files($this->context->id, 'quizaccess_seb', 'filemanager_sebconfigfile');
1004
        // The '.' directory.
1005
        $this->assertCount(1, $files);
1006
    }
1007
 
1008
    /**
1009
     * Test getting the file from the context module id file area.
1010
     */
11 efrain 1011
    public function test_get_module_context_sebconfig_file(): void {
1 efrain 1012
        $this->resetAfterTest();
1013
        $this->setAdminUser();
1014
 
1015
        $this->course = $this->getDataGenerator()->create_course();
1016
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
1017
        $this->context = \context_module::instance($this->quiz->cmid);
1018
 
1019
        $this->set_up_user_and_role();
1020
 
1441 ariadna 1021
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 1022
        $draftitemid = $this->create_test_draftarea_file($xml);
1023
 
1024
        $fs = get_file_storage();
1025
        $files = $fs->get_area_files($this->context->id, 'quizaccess_seb', 'filemanager_sebconfigfile');
1026
        $this->assertCount(0, $files);
1027
 
1028
        settings_provider::save_filemanager_sebconfigfile_draftarea($draftitemid, $this->quiz->cmid);
1029
 
1030
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
1031
        $settings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
1032
        $settings->save();
1033
 
1034
        $file = settings_provider::get_module_context_sebconfig_file($this->quiz->cmid);
1035
 
1036
        $this->assertSame($file->get_content(), $xml);
1037
    }
1038
 
1039
    /**
1040
     * Test file manager options.
1041
     */
11 efrain 1042
    public function test_get_filemanager_options(): void {
1 efrain 1043
        $expected = [
1044
            'subdirs' => 0,
1045
            'maxfiles' => 1,
1046
            'accepted_types' => ['.seb']
1047
        ];
1048
        $this->assertSame($expected, settings_provider::get_filemanager_options());
1049
    }
1050
 
1051
    /**
1052
     * Test that users can or can not configure seb settings.
1053
     */
11 efrain 1054
    public function test_can_configure_seb(): void {
1 efrain 1055
        $this->resetAfterTest();
1056
 
1057
        $this->course = $this->getDataGenerator()->create_course();
1058
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
1059
        $this->context = \context_module::instance($this->quiz->cmid);
1060
        $this->setAdminUser();
1061
 
1062
        $this->assertTrue(settings_provider::can_configure_seb($this->context));
1063
 
1064
        $this->set_up_user_and_role();
1065
 
1066
        $this->assertFalse(settings_provider::can_configure_seb($this->context));
1067
 
1068
        assign_capability('quizaccess/seb:manage_seb_requiresafeexambrowser', CAP_ALLOW, $this->roleid, $this->context->id);
1069
        $this->assertTrue(settings_provider::can_configure_seb($this->context));
1070
    }
1071
 
1072
    /**
1073
     * Test that users can or can not use seb template.
1074
     */
11 efrain 1075
    public function test_can_use_seb_template(): void {
1 efrain 1076
        $this->resetAfterTest();
1077
 
1078
        $this->course = $this->getDataGenerator()->create_course();
1079
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
1080
        $this->context = \context_module::instance($this->quiz->cmid);
1081
        $this->setAdminUser();
1082
 
1083
        $this->assertTrue(settings_provider::can_use_seb_template($this->context));
1084
 
1085
        $this->set_up_user_and_role();
1086
 
1087
        $this->assertFalse(settings_provider::can_use_seb_template($this->context));
1088
 
1089
        assign_capability('quizaccess/seb:manage_seb_templateid', CAP_ALLOW, $this->roleid, $this->context->id);
1090
        $this->assertTrue(settings_provider::can_use_seb_template($this->context));
1091
    }
1092
 
1093
    /**
1094
     * Test that users can or can not upload seb config file.
1095
     */
11 efrain 1096
    public function test_can_upload_seb_file(): void {
1 efrain 1097
        $this->resetAfterTest();
1098
 
1099
        $this->course = $this->getDataGenerator()->create_course();
1100
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
1101
        $this->context = \context_module::instance($this->quiz->cmid);
1102
        $this->setAdminUser();
1103
 
1104
        $this->assertTrue(settings_provider::can_upload_seb_file($this->context));
1105
 
1106
        $this->set_up_user_and_role();
1107
 
1108
        $this->assertFalse(settings_provider::can_upload_seb_file($this->context));
1109
 
1110
        assign_capability('quizaccess/seb:manage_filemanager_sebconfigfile', CAP_ALLOW, $this->roleid, $this->context->id);
1111
        $this->assertTrue(settings_provider::can_upload_seb_file($this->context));
1112
    }
1113
 
1114
    /**
1115
     * Test that users can or can not change Show Safe Exam Browser download button setting.
1116
     */
11 efrain 1117
    public function test_can_change_seb_showsebdownloadlink(): void {
1 efrain 1118
        $this->resetAfterTest();
1119
 
1120
        $this->course = $this->getDataGenerator()->create_course();
1121
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
1122
        $this->context = \context_module::instance($this->quiz->cmid);
1123
        $this->setAdminUser();
1124
        $this->assertTrue(settings_provider::can_change_seb_showsebdownloadlink($this->context));
1125
 
1126
        $this->set_up_user_and_role();
1127
 
1128
        $this->assertFalse(settings_provider::can_change_seb_showsebdownloadlink($this->context));
1129
 
1130
        assign_capability('quizaccess/seb:manage_seb_showsebdownloadlink', CAP_ALLOW, $this->roleid, $this->context->id);
1131
        $this->assertTrue(settings_provider::can_change_seb_showsebdownloadlink($this->context));
1132
    }
1133
 
1134
    /**
1135
     * Test that users can or can not change Allowed Browser Exam Keys setting.
1136
     */
11 efrain 1137
    public function test_can_change_seb_allowedbrowserexamkeys(): void {
1 efrain 1138
        $this->resetAfterTest();
1139
        $this->course = $this->getDataGenerator()->create_course();
1140
 
1141
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
1142
        $this->context = \context_module::instance($this->quiz->cmid);
1143
        $this->setAdminUser();
1144
        $this->assertTrue(settings_provider::can_change_seb_allowedbrowserexamkeys($this->context));
1145
 
1146
        $this->set_up_user_and_role();
1147
 
1148
        $this->assertFalse(settings_provider::can_change_seb_allowedbrowserexamkeys($this->context));
1149
 
1150
        assign_capability('quizaccess/seb:manage_seb_allowedbrowserexamkeys', CAP_ALLOW, $this->roleid, $this->context->id);
1151
        $this->assertTrue(settings_provider::can_change_seb_allowedbrowserexamkeys($this->context));
1152
    }
1153
 
1154
    /**
1155
     * Test that users can or can not Configure SEb manually
1156
     *
1157
     * @param string $settingcapability Setting capability to check manual option against.
1158
     *
1159
     * @dataProvider settings_capability_data_provider
1160
     */
11 efrain 1161
    public function test_can_configure_manually($settingcapability): void {
1 efrain 1162
        $this->resetAfterTest();
1163
        $this->course = $this->getDataGenerator()->create_course();
1164
 
1165
        $this->quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $this->course->id]);
1166
        $this->context = \context_module::instance($this->quiz->cmid);
1167
        $this->setAdminUser();
1168
 
1169
        $this->assertTrue(settings_provider::can_configure_manually($this->context));
1170
 
1171
        $this->set_up_user_and_role();
1172
 
1173
        $this->assertFalse(settings_provider::can_configure_manually($this->context));
1174
 
1441 ariadna 1175
        assign_capability('quizaccess/seb:manage_seb_configuremanually', CAP_ALLOW, $this->roleid, $this->context->id);
1176
        $this->assertFalse(settings_provider::can_configure_manually($this->context));
1177
 
1 efrain 1178
        assign_capability($settingcapability, CAP_ALLOW, $this->roleid, $this->context->id);
1179
        $this->assertTrue(settings_provider::can_configure_manually($this->context));
1180
    }
1181
 
1182
    /**
1183
     * Test that we can check if the seb settings are locked.
1184
     */
11 efrain 1185
    public function test_is_seb_settings_locked(): void {
1 efrain 1186
        $this->resetAfterTest();
1187
 
1188
        $this->course = $this->getDataGenerator()->create_course();
1189
        $this->quiz = $this->create_test_quiz($this->course);
1190
        $user = $this->getDataGenerator()->create_user();
1191
 
1192
        $this->assertFalse(settings_provider::is_seb_settings_locked($this->quiz->id));
1193
 
1194
        $this->attempt_quiz($this->quiz, $user);
1195
        $this->assertTrue(settings_provider::is_seb_settings_locked($this->quiz->id));
1196
    }
1197
 
1198
    /**
1199
     * Test that we can check identify conflicting permissions if set to use template.
1200
     */
11 efrain 1201
    public function test_is_conflicting_permissions_for_manage_templates(): void {
1 efrain 1202
        $this->resetAfterTest();
1203
        $this->setAdminUser();
1204
 
1205
        $this->course = $this->getDataGenerator()->create_course();
1206
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
1207
        $this->context = \context_module::instance($this->quiz->cmid);
1208
 
1209
        // Create a template.
1210
        $template = $this->create_template();
1211
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
1212
        $settings->set('templateid', $template->get('id'));
1213
        $settings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
1214
        $settings->save();
1215
 
1216
        $this->assertFalse(settings_provider::is_conflicting_permissions($this->context));
1217
 
1218
        $this->set_up_user_and_role();
1219
 
1220
        $this->assertTrue(settings_provider::is_conflicting_permissions($this->context));
1221
 
1222
        assign_capability('quizaccess/seb:manage_seb_templateid', CAP_ALLOW, $this->roleid, $this->context->id);
1223
        $this->assertFalse(settings_provider::is_conflicting_permissions($this->context));
1224
    }
1225
 
1226
    /**
1227
     * Test that we can check identify conflicting permissions if set to use own seb file.
1228
     */
11 efrain 1229
    public function test_is_conflicting_permissions_for_upload_seb_file(): void {
1 efrain 1230
        $this->resetAfterTest();
1231
        $this->setAdminUser();
1232
 
1233
        $this->course = $this->getDataGenerator()->create_course();
1234
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
1235
        $this->context = \context_module::instance($this->quiz->cmid);
1236
 
1237
        // Save file.
1441 ariadna 1238
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 1239
        $draftitemid = $this->create_test_draftarea_file($xml);
1240
        settings_provider::save_filemanager_sebconfigfile_draftarea($draftitemid, $this->quiz->cmid);
1241
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
1242
        $settings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
1243
        $settings->save();
1244
 
1245
        $this->assertFalse(settings_provider::is_conflicting_permissions($this->context));
1246
 
1247
        $this->set_up_user_and_role();
1248
 
1249
        assign_capability('quizaccess/seb:manage_filemanager_sebconfigfile', CAP_ALLOW, $this->roleid, $this->context->id);
1250
        $this->assertFalse(settings_provider::is_conflicting_permissions($this->context));
1251
    }
1252
 
1253
    /**
1254
     * Test that we can check identify conflicting permissions if set to use own configure manually.
1255
     *
1256
     * @param string $settingcapability Setting capability to check manual option against.
1257
     *
1258
     * @dataProvider settings_capability_data_provider
1259
     */
11 efrain 1260
    public function test_is_conflicting_permissions_for_configure_manually($settingcapability): void {
1 efrain 1261
        $this->resetAfterTest();
1262
        $this->setAdminUser();
1263
 
1264
        $this->course = $this->getDataGenerator()->create_course();
1265
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
1266
        $this->context = \context_module::instance($this->quiz->cmid);
1267
 
1268
        $this->assertFalse(settings_provider::is_conflicting_permissions($this->context));
1269
 
1270
        $this->set_up_user_and_role();
1271
 
1441 ariadna 1272
        $this->assertTrue(settings_provider::is_conflicting_permissions($this->context));
1273
 
1274
        assign_capability('quizaccess/seb:manage_seb_configuremanually', CAP_ALLOW, $this->roleid, $this->context->id);
1275
        $this->assertTrue(settings_provider::is_conflicting_permissions($this->context));
1276
 
1 efrain 1277
        assign_capability($settingcapability, CAP_ALLOW, $this->roleid, $this->context->id);
1278
        $this->assertFalse(settings_provider::is_conflicting_permissions($this->context));
1279
    }
1280
 
1281
    /**
1282
     * Test add_prefix helper method.
1283
     */
11 efrain 1284
    public function test_add_prefix(): void {
1 efrain 1285
        $this->assertEquals('seb_one', settings_provider::add_prefix('one'));
1286
        $this->assertEquals('seb_two', settings_provider::add_prefix('seb_two'));
1287
        $this->assertEquals('seb_seb_three', settings_provider::add_prefix('seb_seb_three'));
1288
        $this->assertEquals('seb_', settings_provider::add_prefix('seb_'));
1289
        $this->assertEquals('seb_', settings_provider::add_prefix(''));
1290
        $this->assertEquals('seb_one_seb', settings_provider::add_prefix('one_seb'));
1291
    }
1292
 
1293
    /**
1294
     * Test filter_plugin_settings helper method.
1295
     */
11 efrain 1296
    public function test_filter_plugin_settings(): void {
1 efrain 1297
        $test = new \stdClass();
1298
        $test->one = 'one';
1299
        $test->seb_two = 'two';
1300
        $test->seb_seb_three = 'three';
1301
        $test->four = 'four';
1302
 
1303
        $newsettings = (array)settings_provider::filter_plugin_settings($test);
1304
 
1305
        $this->assertFalse(key_exists('one', $newsettings));
1306
        $this->assertFalse(key_exists('four', $newsettings));
1307
 
1308
        $this->assertCount(2, $newsettings);
1309
        $this->assertEquals('two', $newsettings['two']);
1310
        $this->assertEquals('three', $newsettings['seb_three']);
1311
    }
1312
 
1313
    /**
1314
     * Helper method to get a list of settings.
1315
     *
1316
     * @return \stdClass
1317
     */
1318
    protected function get_settings() {
1319
        $allsettings = new \stdClass();
1320
        $allsettings->seb_showsebdownloadlink = 0;
1321
        $allsettings->seb_linkquitseb = 2;
1322
        $allsettings->seb_userconfirmquit = 3;
1323
        $allsettings->seb_allowuserquitseb = 4;
1324
        $allsettings->seb_quitpassword = 5;
1325
        $allsettings->seb_allowreloadinexam = 6;
1326
        $allsettings->seb_showsebtaskbar = 7;
1327
        $allsettings->seb_showreloadbutton = 8;
1328
        $allsettings->seb_showtime = 9;
1329
        $allsettings->seb_showkeyboardlayout = 10;
1330
        $allsettings->seb_showwificontrol = 11;
1331
        $allsettings->seb_enableaudiocontrol = 12;
1332
        $allsettings->seb_muteonstartup = 13;
1333
        $allsettings->seb_allowspellchecking = 14;
1334
        $allsettings->seb_activateurlfiltering = 15;
1335
        $allsettings->seb_filterembeddedcontent = 16;
1336
        $allsettings->seb_expressionsallowed = 17;
1337
        $allsettings->seb_regexallowed = 18;
1338
        $allsettings->seb_expressionsblocked = 19;
1339
        $allsettings->seb_regexblocked = 20;
1340
        $allsettings->seb_templateid = 21;
1341
        $allsettings->seb_allowedbrowserexamkeys = 22;
1441 ariadna 1342
        $allsettings->seb_allowcapturecamera = 23;
1343
        $allsettings->seb_allowcapturemicrophone = 24;
1 efrain 1344
 
1345
        return $allsettings;
1346
    }
1347
 
1348
    /**
1349
     * Helper method to assert results of filter_plugin_settings
1350
     *
1351
     * @param int $type Type of SEB usage.
1352
     * @param array $notnulls A list of expected not null settings.
1353
     */
1354
    protected function assert_filter_plugin_settings(int $type, array $notnulls) {
1355
        $allsettings = $this->get_settings();
1356
        $allsettings->seb_requiresafeexambrowser = $type;
1357
        $actual = settings_provider::filter_plugin_settings($allsettings);
1358
 
1359
        $expected = (array)$allsettings;
1360
        foreach ($actual as $name => $value) {
1361
            if (in_array($name, $notnulls)) {
1362
                $this->assertEquals($expected['seb_' . $name], $value);
1363
            } else {
1364
                $this->assertNull($value);
1365
            }
1366
        }
1367
    }
1368
 
1369
    /**
1370
     * Test filter_plugin_settings method for no SEB case.
1371
     */
11 efrain 1372
    public function test_filter_plugin_settings_for_no_seb(): void {
1 efrain 1373
        $notnulls = ['requiresafeexambrowser'];
1374
        $this->assert_filter_plugin_settings(settings_provider::USE_SEB_NO, $notnulls);
1375
    }
1376
 
1377
    /**
1378
     * Test filter_plugin_settings method for using uploaded config.
1379
     */
11 efrain 1380
    public function test_filter_plugin_settings_for_uploaded_config(): void {
1 efrain 1381
        $notnulls = ['requiresafeexambrowser', 'showsebdownloadlink', 'allowedbrowserexamkeys'];
1382
        $this->assert_filter_plugin_settings(settings_provider::USE_SEB_UPLOAD_CONFIG, $notnulls);
1383
    }
1384
 
1385
    /**
1386
     * Test filter_plugin_settings method for using template.
1387
     */
11 efrain 1388
    public function test_filter_plugin_settings_for_template(): void {
1 efrain 1389
        $notnulls = ['requiresafeexambrowser', 'showsebdownloadlink', 'allowuserquitseb', 'quitpassword', 'templateid'];
1390
        $this->assert_filter_plugin_settings(settings_provider::USE_SEB_TEMPLATE, $notnulls);
1391
    }
1392
 
1393
    /**
1394
     * Test filter_plugin_settings method for using client config.
1395
     */
11 efrain 1396
    public function test_filter_plugin_settings_for_client_config(): void {
1 efrain 1397
        $notnulls = ['requiresafeexambrowser', 'showsebdownloadlink', 'allowedbrowserexamkeys'];
1398
        $this->assert_filter_plugin_settings(settings_provider::USE_SEB_CLIENT_CONFIG, $notnulls);
1399
    }
1400
 
1401
    /**
1402
     * Test filter_plugin_settings method for manually configured SEB.
1403
     */
11 efrain 1404
    public function test_filter_plugin_settings_for_configure_manually(): void {
1 efrain 1405
        $allsettings = $this->get_settings();
1406
        $allsettings->seb_requiresafeexambrowser = settings_provider::USE_SEB_CONFIG_MANUALLY;
1407
        $actual = settings_provider::filter_plugin_settings($allsettings);
1408
 
1409
        // For manual it's easier to check nulls, as most of the settings are not null.
1410
        $nulls = ['templateid', 'allowedbrowserexamkeys'];
1411
 
1412
        $expected = (array)$allsettings;
1413
        foreach ($actual as $name => $value) {
1414
            if (in_array($name, $nulls)) {
1415
                $this->assertNull($value);
1416
            } else {
1417
                $this->assertEquals($expected['seb_' . $name], $value);
1418
            }
1419
        }
1420
    }
1421
 
1422
    /**
1423
     * Test settings map.
1424
     */
11 efrain 1425
    public function test_get_seb_settings_map(): void {
1 efrain 1426
        $expected = [
1427
            settings_provider::USE_SEB_NO => [
1428
 
1429
            ],
1430
            settings_provider::USE_SEB_CONFIG_MANUALLY => [
1431
                'seb_showsebdownloadlink' => [],
1432
                'seb_linkquitseb' => [],
1433
                'seb_userconfirmquit' => [],
1434
                'seb_allowuserquitseb' => [
1435
                    'seb_quitpassword' => []
1436
                ],
1437
                'seb_allowreloadinexam' => [],
1438
                'seb_showsebtaskbar' => [
1439
                    'seb_showreloadbutton' => [],
1440
                    'seb_showtime' => [],
1441
                    'seb_showkeyboardlayout' => [],
1442
                    'seb_showwificontrol' => [],
1443
                ],
1444
                'seb_enableaudiocontrol' => [
1445
                    'seb_muteonstartup' => [],
1446
                ],
1441 ariadna 1447
                'seb_allowcapturecamera' => [],
1448
                'seb_allowcapturemicrophone' => [],
1 efrain 1449
                'seb_allowspellchecking' => [],
1450
                'seb_activateurlfiltering' => [
1451
                    'seb_filterembeddedcontent' => [],
1452
                    'seb_expressionsallowed' => [],
1453
                    'seb_regexallowed' => [],
1454
                    'seb_expressionsblocked' => [],
1455
                    'seb_regexblocked' => [],
1456
                ],
1457
            ],
1458
            settings_provider::USE_SEB_TEMPLATE => [
1459
                'seb_templateid' => [],
1460
                'seb_showsebdownloadlink' => [],
1461
                'seb_allowuserquitseb' => [
1462
                    'seb_quitpassword' => [],
1463
                ],
1464
            ],
1465
            settings_provider::USE_SEB_UPLOAD_CONFIG => [
1466
                'filemanager_sebconfigfile' => [],
1467
                'seb_showsebdownloadlink' => [],
1468
                'seb_allowedbrowserexamkeys' => [],
1469
            ],
1470
            settings_provider::USE_SEB_CLIENT_CONFIG => [
1471
                'seb_showsebdownloadlink' => [],
1472
                'seb_allowedbrowserexamkeys' => [],
1473
            ],
1474
        ];
1475
 
1476
        $this->assertEquals($expected, settings_provider::get_seb_settings_map());
1477
    }
1478
 
1479
}