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 helper class.
25
 *
26
 * @package    quizaccess_seb
27
 * @author     Dmitrii Metelkin <dmitriim@catalyst-au.net>
28
 * @copyright  2020 Catalyst IT
29
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
final class helper_test extends \advanced_testcase {
1 efrain 32
    use \quizaccess_seb_test_helper_trait;
33
 
34
    /**
35
     * Test that we can check valid seb string.
36
     */
11 efrain 37
    public function test_is_valid_seb_config(): void {
1 efrain 38
        $validseb = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
39
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
40
<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
41
            . "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
42
            . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
1441 ariadna 43
            . "<key>browserMediaCaptureCamera</key><true/><key>browserMediaCaptureMicrophone</key><true/>"
1 efrain 44
            . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
45
            . "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
46
            . "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
47
            . "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
48
            . "<string>test.com</string><key>regex</key><false/></dict></array>"
49
            . "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
50
        $invalidseb = 'Invalid seb';
51
        $emptyseb = '';
52
 
53
        $this->assertTrue(\quizaccess_seb\helper::is_valid_seb_config($validseb));
54
        $this->assertFalse(\quizaccess_seb\helper::is_valid_seb_config($invalidseb));
55
        $this->assertFalse(\quizaccess_seb\helper::is_valid_seb_config($emptyseb));
56
    }
57
 
58
    /**
59
     * Test that we can get seb file headers.
60
     */
11 efrain 61
    public function test_get_seb_file_headers(): void {
1 efrain 62
        $expiretime = 1582767914;
63
        $headers = \quizaccess_seb\helper::get_seb_file_headers($expiretime);
64
 
65
        $this->assertCount(5, $headers);
66
        $this->assertEquals('Cache-Control: private, max-age=1, no-transform', $headers[0]);
67
        $this->assertEquals('Expires: Thu, 27 Feb 2020 01:45:14 GMT', $headers[1]);
68
        $this->assertEquals('Pragma: no-cache', $headers[2]);
69
        $this->assertEquals('Content-Disposition: attachment; filename=config.seb', $headers[3]);
70
        $this->assertEquals('Content-Type: application/seb', $headers[4]);
71
    }
72
 
73
 
74
    /**
75
     * Test that the course module must exist to get a seb config file content.
76
     */
11 efrain 77
    public function test_can_not_get_config_content_with_invalid_cmid(): void {
1 efrain 78
        $this->resetAfterTest();
79
 
80
        $user = $this->getDataGenerator()->create_user();
81
        $course = $this->getDataGenerator()->create_course();
82
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
83
        $this->setUser($user); // Log user in.
84
 
85
        $this->expectException(\dml_exception::class);
86
        $this->expectExceptionMessage("Can't find data record in database. (SELECT cm.*, m.name, md.name AS modname \n"
87
            . "              FROM {course_modules} cm\n"
88
            . "                   JOIN {modules} md ON md.id = cm.module\n"
89
            . "                   JOIN {quiz} m ON m.id = cm.instance\n"
90
            . "                   \n"
91
            . "             WHERE cm.id = :cmid AND md.name = :modulename\n"
92
            . "                   \n"
93
            . "[array (\n"
94
            . "  'cmid' => '999',\n"
95
            . "  'modulename' => 'quiz',\n"
96
            .')])');
97
        \quizaccess_seb\helper::get_seb_config_content('999');
98
    }
99
 
100
    /**
101
     * Test that the user must be enrolled to get seb config content.
102
     */
11 efrain 103
    public function test_can_not_get_config_content_when_user_not_enrolled_in_course(): void {
1 efrain 104
        $this->resetAfterTest();
105
 
106
        $this->setAdminUser();
107
        $course = $this->getDataGenerator()->create_course();
108
        $quiz = $this->create_test_quiz($course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
109
 
110
        $user = $this->getDataGenerator()->create_user();
111
        $this->setUser($user); // Log user in.
112
 
113
        $this->expectException(\moodle_exception::class);
114
        $this->expectExceptionMessage('Unsupported redirect detected, script execution terminated');
115
        \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
116
    }
117
 
118
    /**
119
     * Test that if SEB quiz settings can't be found, a seb config content won't be provided.
120
     */
11 efrain 121
    public function test_can_not_get_config_content_if_config_not_found_for_cmid(): void {
1 efrain 122
        $this->resetAfterTest();
123
 
124
        $this->setAdminUser();
125
        $course = $this->getDataGenerator()->create_course();
126
        $quiz = $this->create_test_quiz($course);
127
 
128
        $user = $this->getDataGenerator()->create_user();
129
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
130
        $this->setUser($user); // Log user in.
131
 
132
        $this->expectException(\moodle_exception::class);
133
        $this->expectExceptionMessage("No SEB config could be found for quiz with cmid: $quiz->cmid");
134
        \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
135
    }
136
 
137
    /**
138
     * That that if config is empty for a quiz, a seb config content won't be provided.
139
     */
11 efrain 140
    public function test_can_not_get_config_content_if_config_empty(): void {
1 efrain 141
        $this->resetAfterTest();
142
 
143
        $this->setAdminUser();
144
 
145
        $course = $this->getDataGenerator()->create_course();
146
        $quiz = $this->create_test_quiz($course, \quizaccess_seb\settings_provider::USE_SEB_NO);
147
 
148
        $user = $this->getDataGenerator()->create_user();
149
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
150
        $this->setUser($user); // Log user in.
151
 
152
        $this->expectException(\moodle_exception::class);
153
        $this->expectExceptionMessage("No SEB config could be found for quiz with cmid: $quiz->cmid");
154
        \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
155
    }
156
 
157
    /**
158
     * Test config content is provided successfully.
159
     */
11 efrain 160
    public function test_config_provided(): void {
1 efrain 161
        $this->resetAfterTest();
162
 
163
        $this->setAdminUser();
164
 
165
        $course = $this->getDataGenerator()->create_course();
166
        $quiz = $this->create_test_quiz($course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
167
 
168
        $user = $this->getDataGenerator()->create_user();
169
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
170
        $this->setUser($user); // Log user in.
171
 
172
        $config = \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
173
 
174
        $url = new \moodle_url("/mod/quiz/view.php", ['id' => $quiz->cmid]);
175
 
176
        $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
177
            . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
178
            . "<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key>"
179
            . "<false/><key>showReloadButton</key><true/><key>showTime</key><true/><key>showInputLanguage</key>"
180
            . "<true/><key>allowQuit</key><true/><key>quitURLConfirm</key><true/><key>audioControlEnabled</key>"
1441 ariadna 181
            . "<false/><key>audioMute</key><false/><key>browserMediaCaptureCamera</key><false/><key>browserMediaCaptureMicrophone"
182
            . "</key><false/><key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key>"
1 efrain 183
            . "<true/><key>URLFilterEnable</key><false/><key>URLFilterEnableContentFilter</key><false/>"
184
            . "<key>URLFilterRules</key><array/><key>startURL</key><string>$url</string>"
185
            . "<key>sendBrowserExamKey</key><true/><key>browserWindowWebView</key><integer>3</integer>"
186
            . "<key>examSessionClearCookiesOnStart</key><false/>"
187
            . "<key>allowPreferencesWindow</key><false/></dict></plist>\n", $config);
188
    }
189
 
190
}