Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 the access manager.
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
 * @covers \quizaccess_seb\seb_access_manager
31
 */
32
class access_manager_test extends \advanced_testcase {
33
    use \quizaccess_seb_test_helper_trait;
34
 
35
    /**
36
     * Called before every test.
37
     */
38
    public function setUp(): void {
39
        parent::setUp();
40
 
41
        $this->resetAfterTest();
42
        $this->setAdminUser();
43
        $this->course = $this->getDataGenerator()->create_course();
44
    }
45
 
46
    /**
47
     * Test access_manager private property quizsettings is null.
48
     */
11 efrain 49
    public function test_access_manager_quizsettings_null(): void {
1 efrain 50
        $this->quiz = $this->create_test_quiz($this->course);
51
 
52
        $accessmanager = $this->get_access_manager();
53
 
54
        $this->assertFalse($accessmanager->seb_required());
55
 
56
        $reflection = new \ReflectionClass('\quizaccess_seb\seb_access_manager');
57
        $property = $reflection->getProperty('quizsettings');
58
 
59
        $this->assertFalse($property->getValue($accessmanager));
60
    }
61
 
62
    /**
63
     * Test that SEB is not required.
64
     */
11 efrain 65
    public function test_seb_required_false(): void {
1 efrain 66
        $this->quiz = $this->create_test_quiz($this->course);
67
 
68
        $accessmanager = $this->get_access_manager();
69
        $this->assertFalse($accessmanager->seb_required());
70
    }
71
 
72
    /**
73
     * Test that SEB is required.
74
     */
11 efrain 75
    public function test_seb_required_true(): void {
1 efrain 76
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
77
 
78
        $accessmanager = $this->get_access_manager();
79
        $this->assertTrue($accessmanager->seb_required());
80
    }
81
 
82
    /**
83
     * Test that user has capability to bypass SEB check.
84
     */
11 efrain 85
    public function test_user_can_bypass_seb_check(): void {
1 efrain 86
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
87
 
88
        $user = $this->getDataGenerator()->create_user();
89
        $this->setUser($user);
90
 
91
        // Set the bypass SEB check capability to $USER.
92
        $this->assign_user_capability('quizaccess/seb:bypassseb', \context_module::instance($this->quiz->cmid)->id);
93
 
94
        $accessmanager = $this->get_access_manager();
95
        $this->assertTrue($accessmanager->can_bypass_seb());
96
    }
97
 
98
    /**
99
     * Test that user has capability to bypass SEB check.
100
     */
11 efrain 101
    public function test_admin_user_can_bypass_seb_check(): void {
1 efrain 102
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
103
 
104
        // Test normal user cannot bypass check.
105
        $user = $this->getDataGenerator()->create_user();
106
        $this->setUser($user);
107
        $accessmanager = $this->get_access_manager();
108
        $this->assertFalse($accessmanager->can_bypass_seb());
109
 
110
        // Test with admin user.
111
        $this->setAdminUser();
112
        $accessmanager = $this->get_access_manager();
113
        $this->assertTrue($accessmanager->can_bypass_seb());
114
    }
115
 
116
    /**
117
     * Test user does not have capability to bypass SEB check.
118
     */
11 efrain 119
    public function test_user_cannot_bypass_seb_check(): void {
1 efrain 120
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
121
 
122
        $user = $this->getDataGenerator()->create_user();
123
        $this->setUser($user);
124
 
125
        $accessmanager = $this->get_access_manager();
126
        $this->assertFalse($accessmanager->can_bypass_seb());
127
    }
128
 
129
    /**
130
     * Test we can detect SEB usage.
131
     */
11 efrain 132
    public function test_is_using_seb(): void {
1 efrain 133
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
134
 
135
        $accessmanager = $this->get_access_manager();
136
 
137
        $this->assertFalse($accessmanager->is_using_seb());
138
 
139
        $_SERVER['HTTP_USER_AGENT'] = 'Test';
140
        $this->assertFalse($accessmanager->is_using_seb());
141
 
142
        $_SERVER['HTTP_USER_AGENT'] = 'SEB';
143
        $this->assertTrue($accessmanager->is_using_seb());
144
    }
145
 
146
    /**
147
     * Test that the quiz Config Key matches the incoming request header.
148
     */
11 efrain 149
    public function test_access_keys_validate_with_config_key(): void {
1 efrain 150
        global $FULLME;
151
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
152
 
153
        $accessmanager = $this->get_access_manager();
154
 
155
        $configkey = seb_quiz_settings::get_record(['quizid' => $this->quiz->id])->get_config_key();
156
 
157
        // Set up dummy request.
158
        $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
159
        $expectedhash = hash('sha256', $FULLME . $configkey);
160
        $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = $expectedhash;
161
 
162
        $this->assertTrue($accessmanager->validate_config_key());
163
    }
164
 
165
    /**
166
     * Test that the quiz Config Key matches a provided config key with no incoming request header.
167
     */
11 efrain 168
    public function test_access_keys_validate_with_provided_config_key(): void {
1 efrain 169
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
170
        $url = 'https://www.example.com/moodle';
171
        $accessmanager = $this->get_access_manager();
172
 
173
        $configkey = seb_quiz_settings::get_record(['quizid' => $this->quiz->id])->get_config_key();
174
        $fullconfigkey = hash('sha256', $url . $configkey);
175
 
176
        $this->assertTrue($accessmanager->validate_config_key($fullconfigkey, $url));
177
    }
178
 
179
    /**
180
     * Test that the quiz Config Key does not match the incoming request header.
181
     */
11 efrain 182
    public function test_access_keys_fail_to_validate_with_config_key(): void {
1 efrain 183
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
184
        $accessmanager = $this->get_access_manager();
185
 
186
        $this->assertFalse($accessmanager->validate_config_key());
187
    }
188
 
189
    /**
190
     * Test that config key is not checked when using client configuration with SEB.
191
     */
11 efrain 192
    public function test_config_key_not_checked_if_client_requirement_is_selected(): void {
1 efrain 193
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
194
        $accessmanager = $this->get_access_manager();
195
        $this->assertFalse($accessmanager->should_validate_config_key());
196
    }
197
 
198
    /**
199
     * Test that if there are no browser exam keys for quiz, check is skipped.
200
     */
11 efrain 201
    public function test_no_browser_exam_keys_cause_check_to_be_successful(): void {
1 efrain 202
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
203
 
204
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
205
        $settings->set('allowedbrowserexamkeys', '');
206
        $settings->save();
207
        $accessmanager = $this->get_access_manager();
208
        $this->assertTrue($accessmanager->should_validate_browser_exam_key());
209
        $this->assertTrue($accessmanager->validate_browser_exam_key());
210
    }
211
 
212
    /**
213
     * Test that access fails if there is no hash in header.
214
     */
11 efrain 215
    public function test_access_keys_fail_if_browser_exam_key_header_does_not_exist(): void {
1 efrain 216
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
217
 
218
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
219
        $settings->set('allowedbrowserexamkeys', hash('sha256', 'one') . "\n" . hash('sha256', 'two'));
220
        $settings->save();
221
        $accessmanager = $this->get_access_manager();
222
        $this->assertFalse($accessmanager->validate_browser_exam_key());
223
    }
224
 
225
    /**
226
     * Test that access fails if browser exam key doesn't match hash in header.
227
     */
11 efrain 228
    public function test_access_keys_fail_if_browser_exam_key_header_does_not_match_provided_hash(): void {
1 efrain 229
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
230
 
231
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
232
        $settings->set('allowedbrowserexamkeys', hash('sha256', 'one') . "\n" . hash('sha256', 'two'));
233
        $settings->save();
234
        $accessmanager = $this->get_access_manager();
235
        $_SERVER['HTTP_X_SAFEEXAMBROWSER_REQUESTHASH'] = hash('sha256', 'notwhatyouwereexpectinghuh');
236
        $this->assertFalse($accessmanager->validate_browser_exam_key());
237
    }
238
 
239
    /**
240
     * Test that browser exam key matches hash in header.
241
     */
11 efrain 242
    public function test_browser_exam_keys_match_header_hash(): void {
1 efrain 243
        global $FULLME;
244
 
245
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
246
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
247
        $browserexamkey = hash('sha256', 'browserexamkey');
248
        $settings->set('allowedbrowserexamkeys', $browserexamkey); // Add a hashed BEK.
249
        $settings->save();
250
        $accessmanager = $this->get_access_manager();
251
 
252
        // Set up dummy request.
253
        $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
254
        $expectedhash = hash('sha256', $FULLME . $browserexamkey);
255
        $_SERVER['HTTP_X_SAFEEXAMBROWSER_REQUESTHASH'] = $expectedhash;
256
        $this->assertTrue($accessmanager->validate_browser_exam_key());
257
    }
258
 
259
    /**
260
     * Test that browser exam key matches a provided browser exam key.
261
     */
11 efrain 262
    public function test_browser_exam_keys_match_provided_browser_exam_key(): void {
1 efrain 263
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
264
        $url = 'https://www.example.com/moodle';
265
        $settings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
266
        $browserexamkey = hash('sha256', 'browserexamkey');
267
        $fullbrowserexamkey = hash('sha256', $url . $browserexamkey);
268
        $settings->set('allowedbrowserexamkeys', $browserexamkey); // Add a hashed BEK.
269
        $settings->save();
270
        $accessmanager = $this->get_access_manager();
271
 
272
        $this->assertTrue($accessmanager->validate_browser_exam_key($fullbrowserexamkey, $url));
273
    }
274
 
275
    /**
276
     * Test can get received config key.
277
     */
11 efrain 278
    public function test_get_received_config_key(): void {
1 efrain 279
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
280
        $accessmanager = $this->get_access_manager();
281
 
282
        $this->assertNull($accessmanager->get_received_config_key());
283
 
284
        $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'Test key';
285
        $this->assertEquals('Test key', $accessmanager->get_received_config_key());
286
    }
287
 
288
    /**
289
     * Test can get received browser key.
290
     */
291
    public function get_received_browser_exam_key() {
292
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
293
        $accessmanager = $this->get_access_manager();
294
 
295
        $this->assertNull($accessmanager->get_received_browser_exam_key());
296
 
297
        $_SERVER['HTTP_X_SAFEEXAMBROWSER_REQUESTHASH'] = 'Test browser key';
298
        $this->assertEquals('Test browser key', $accessmanager->get_received_browser_exam_key());
299
    }
300
 
301
    /**
302
     * Test can correctly get type of SEB usage for the quiz.
303
     */
11 efrain 304
    public function test_get_seb_use_type(): void {
1 efrain 305
        // No SEB.
306
        $this->quiz = $this->create_test_quiz($this->course);
307
        $accessmanager = $this->get_access_manager();
308
        $this->assertEquals(settings_provider::USE_SEB_NO, $accessmanager->get_seb_use_type());
309
 
310
        // Manually.
311
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
312
        $accessmanager = $this->get_access_manager();
313
        $this->assertEquals(settings_provider::USE_SEB_CONFIG_MANUALLY, $accessmanager->get_seb_use_type());
314
 
315
        // Use template.
316
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
317
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
318
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
319
        $quizsettings->set('templateid', $this->create_template()->get('id'));
320
        $quizsettings->save();
321
        $accessmanager = $this->get_access_manager();
322
        $this->assertEquals(settings_provider::USE_SEB_TEMPLATE, $accessmanager->get_seb_use_type());
323
 
324
        // Use uploaded config.
325
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
326
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
327
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG); // Doesn't check basic header.
328
        $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
329
        $this->create_module_test_file($xml, $this->quiz->cmid);
330
        $quizsettings->save();
331
        $accessmanager = $this->get_access_manager();
332
        $this->assertEquals(settings_provider::USE_SEB_UPLOAD_CONFIG, $accessmanager->get_seb_use_type());
333
 
334
        // Use client config.
335
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
336
        $accessmanager = $this->get_access_manager();
337
        $this->assertEquals(settings_provider::USE_SEB_CLIENT_CONFIG, $accessmanager->get_seb_use_type());
338
    }
339
 
340
    /**
341
     * Data provider for self::test_should_validate_basic_header.
342
     *
343
     * @return array
344
     */
345
    public function should_validate_basic_header_data_provider() {
346
        return [
347
            [settings_provider::USE_SEB_NO, false],
348
            [settings_provider::USE_SEB_CONFIG_MANUALLY, false],
349
            [settings_provider::USE_SEB_TEMPLATE, false],
350
            [settings_provider::USE_SEB_UPLOAD_CONFIG, false],
351
            [settings_provider::USE_SEB_CLIENT_CONFIG, true],
352
        ];
353
    }
354
 
355
    /**
356
     * Test we know when we should validate basic header.
357
     *
358
     * @param int $type Type of SEB usage.
359
     * @param bool $expected Expected result.
360
     *
361
     * @dataProvider should_validate_basic_header_data_provider
362
     */
11 efrain 363
    public function test_should_validate_basic_header($type, $expected): void {
1 efrain 364
        $accessmanager = $this->getMockBuilder(seb_access_manager::class)
365
            ->disableOriginalConstructor()
366
            ->onlyMethods(['get_seb_use_type'])
367
            ->getMock();
368
        $accessmanager->method('get_seb_use_type')->willReturn($type);
369
 
370
        $this->assertEquals($expected, $accessmanager->should_validate_basic_header());
371
 
372
    }
373
 
374
    /**
375
     * Data provider for self::test_should_validate_config_key.
376
     *
377
     * @return array
378
     */
379
    public function should_validate_config_key_data_provider() {
380
        return [
381
            [settings_provider::USE_SEB_NO, false],
382
            [settings_provider::USE_SEB_CONFIG_MANUALLY, true],
383
            [settings_provider::USE_SEB_TEMPLATE, true],
384
            [settings_provider::USE_SEB_UPLOAD_CONFIG, true],
385
            [settings_provider::USE_SEB_CLIENT_CONFIG, false],
386
        ];
387
    }
388
 
389
    /**
390
     * Test we know when we should validate config key.
391
     *
392
     * @param int $type Type of SEB usage.
393
     * @param bool $expected Expected result.
394
     *
395
     * @dataProvider should_validate_config_key_data_provider
396
     */
11 efrain 397
    public function test_should_validate_config_key($type, $expected): void {
1 efrain 398
        $accessmanager = $this->getMockBuilder(seb_access_manager::class)
399
            ->disableOriginalConstructor()
400
            ->onlyMethods(['get_seb_use_type'])
401
            ->getMock();
402
        $accessmanager->method('get_seb_use_type')->willReturn($type);
403
 
404
        $this->assertEquals($expected, $accessmanager->should_validate_config_key());
405
    }
406
 
407
    /**
408
     * Data provider for self::test_should_validate_browser_exam_key.
409
     *
410
     * @return array
411
     */
412
    public function should_validate_browser_exam_key_data_provider() {
413
        return [
414
            [settings_provider::USE_SEB_NO, false],
415
            [settings_provider::USE_SEB_CONFIG_MANUALLY, false],
416
            [settings_provider::USE_SEB_TEMPLATE, false],
417
            [settings_provider::USE_SEB_UPLOAD_CONFIG, true],
418
            [settings_provider::USE_SEB_CLIENT_CONFIG, true],
419
        ];
420
    }
421
 
422
    /**
423
     * Test we know when we should browser exam key.
424
     *
425
     * @param int $type Type of SEB usage.
426
     * @param bool $expected Expected result.
427
     *
428
     * @dataProvider should_validate_browser_exam_key_data_provider
429
     */
11 efrain 430
    public function test_should_validate_browser_exam_key($type, $expected): void {
1 efrain 431
        $accessmanager = $this->getMockBuilder(seb_access_manager::class)
432
            ->disableOriginalConstructor()
433
            ->onlyMethods(['get_seb_use_type'])
434
            ->getMock();
435
        $accessmanager->method('get_seb_use_type')->willReturn($type);
436
 
437
        $this->assertEquals($expected, $accessmanager->should_validate_browser_exam_key());
438
    }
439
 
440
    /**
441
     * Test that access manager uses cached Config Key.
442
     */
11 efrain 443
    public function test_access_manager_uses_cached_config_key(): void {
1 efrain 444
        global $FULLME;
445
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
446
 
447
        $accessmanager = $this->get_access_manager();
448
 
449
        $configkey = $accessmanager->get_valid_config_key();
450
 
451
        // Set up dummy request.
452
        $FULLME = 'https://example.com/moodle/mod/quiz/attempt.php?attemptid=123&page=4';
453
        $expectedhash = hash('sha256', $FULLME . $configkey);
454
        $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = $expectedhash;
455
 
456
        $this->assertTrue($accessmanager->validate_config_key());
457
 
458
        // Change settings (but don't save) and check that still can validate config key.
459
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
460
        $quizsettings->set('showsebtaskbar', 0);
461
        $this->assertNotEquals($quizsettings->get_config_key(), $configkey);
462
        $this->assertTrue($accessmanager->validate_config_key());
463
 
464
        // Now save settings which should purge caches but access manager still has config key.
465
        $quizsettings->save();
466
        $this->assertNotEquals($quizsettings->get_config_key(), $configkey);
467
        $this->assertTrue($accessmanager->validate_config_key());
468
 
469
        // Initialise a new access manager. Now validation should fail.
470
        $accessmanager = $this->get_access_manager();
471
        $this->assertFalse($accessmanager->validate_config_key());
472
    }
473
 
474
    /**
475
     * Check that valid SEB config key is null if quiz doesn't have SEB settings.
476
     */
11 efrain 477
    public function test_valid_config_key_is_null_if_no_settings(): void {
1 efrain 478
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_NO);
479
        $accessmanager = $this->get_access_manager();
480
 
481
        $this->assertEmpty(seb_quiz_settings::get_record(['quizid' => $this->quiz->id]));
482
        $this->assertNull($accessmanager->get_valid_config_key());
483
 
484
    }
485
 
486
    /**
487
     * Test if config key should not be validated.
488
     */
11 efrain 489
    public function test_if_config_key_should_not_be_validated(): void {
1 efrain 490
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_NO);
491
        $accessmanager = $this->get_access_manager();
492
 
493
        $this->assertTrue($accessmanager->validate_config_key());
494
    }
495
 
496
    /**
497
     * Test if browser exam key should not be validated.
498
     */
11 efrain 499
    public function test_if_browser_exam_key_should_not_be_validated(): void {
1 efrain 500
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
501
        $accessmanager = $this->get_access_manager();
502
 
503
        $this->assertTrue($accessmanager->validate_browser_exam_key());
504
    }
505
 
506
    /**
507
     * Test that access is set correctly in Moodle session.
508
     */
11 efrain 509
    public function test_set_session_access(): void {
1 efrain 510
        global $SESSION;
511
 
512
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
513
        $accessmanager = $this->get_access_manager();
514
 
515
        $this->assertTrue(empty($SESSION->quizaccess_seb_access[$this->quiz->cmid]));
516
 
517
        $accessmanager->set_session_access(true);
518
 
519
        $this->assertTrue($SESSION->quizaccess_seb_access[$this->quiz->cmid]);
520
    }
521
 
522
    /**
523
     * Test that access is set in Moodle session for only course module associated with access manager.
524
     */
11 efrain 525
    public function test_session_access_set_for_specific_course_module(): void {
1 efrain 526
        global $SESSION;
527
 
528
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
529
        $quiz2 = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
530
        $accessmanager = $this->get_access_manager();
531
 
532
        $accessmanager->set_session_access(true);
533
 
534
        $this->assertCount(1, $SESSION->quizaccess_seb_access);
535
        $this->assertTrue($SESSION->quizaccess_seb_access[$this->quiz->cmid]);
536
        $this->assertTrue(empty($SESSION->quizaccess_seb_access[$quiz2->cmid]));
537
    }
538
 
539
    /**
540
     * Test that access state can be retrieved from Moodle session.
541
     */
11 efrain 542
    public function test_validate_session_access(): void {
1 efrain 543
        global $SESSION;
544
 
545
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
546
        $accessmanager = $this->get_access_manager();
547
 
548
        $this->assertEmpty($accessmanager->validate_session_access());
549
 
550
        $SESSION->quizaccess_seb_access[$this->quiz->cmid] = true;
551
 
552
        $this->assertTrue($accessmanager->validate_session_access());
553
    }
554
 
555
    /**
556
     * Test that access can be cleared from Moodle session.
557
     */
11 efrain 558
    public function test_clear_session_access(): void {
1 efrain 559
        global $SESSION;
560
 
561
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CLIENT_CONFIG);
562
        $accessmanager = $this->get_access_manager();
563
 
564
        $SESSION->quizaccess_seb_access[$this->quiz->cmid] = true;
565
 
566
        $accessmanager->clear_session_access();
567
 
568
        $this->assertTrue(empty($SESSION->quizaccess_seb_access[$this->quiz->cmid]));
569
    }
570
 
571
    /**
572
     * Test we can decide if need to redirect to SEB config link.
573
     */
11 efrain 574
    public function test_should_redirect_to_seb_config_link(): void {
1 efrain 575
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
576
        $accessmanager = $this->get_access_manager();
577
 
578
        set_config('autoreconfigureseb', '1', 'quizaccess_seb');
579
        $_SERVER['HTTP_USER_AGENT'] = 'SEB';
580
        $this->assertFalse($accessmanager->should_redirect_to_seb_config_link());
581
 
582
        set_config('autoreconfigureseb', '1', 'quizaccess_seb');
583
        $_SERVER['HTTP_USER_AGENT'] = 'SEB';
584
        $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = hash('sha256', 'configkey');
585
        $this->assertTrue($accessmanager->should_redirect_to_seb_config_link());
586
    }
587
}