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 backup and restore functionality.
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 backup_restore_test extends \advanced_testcase {
1 efrain 32
    use \quizaccess_seb_test_helper_trait;
33
 
34
 
35
    /** @var template $template A test template. */
36
    protected $template;
37
 
38
    /**
39
     * Called before every test.
40
     */
41
    public function setUp(): void {
42
        global $USER;
43
 
44
        parent::setUp();
45
 
46
        $this->resetAfterTest();
47
        $this->setAdminUser();
48
 
49
        $this->course = $this->getDataGenerator()->create_course();
50
        $this->template = $this->create_template();
51
        $this->user = $USER;
52
    }
53
 
54
    /**
55
     * A helper method to create a quiz with template usage of SEB.
56
     *
57
     * @return seb_quiz_settings
58
     */
59
    protected function create_quiz_with_template() {
60
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
61
        $quizsettings = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
62
        $quizsettings->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
63
        $quizsettings->set('templateid', $this->template->get('id'));
64
        $quizsettings->save();
65
 
66
        return $quizsettings;
67
    }
68
 
69
    /**
70
     * A helper method to emulate backup and restore of the quiz.
71
     *
72
     * @return \cm_info|null
73
     */
74
    protected function backup_and_restore_quiz() {
75
        return duplicate_module($this->course, get_fast_modinfo($this->course)->get_cm($this->quiz->cmid));
76
    }
77
 
78
    /**
79
     * A helper method to backup test quiz.
80
     *
81
     * @return mixed A backup ID ready to be restored.
82
     */
83
    protected function backup_quiz() {
84
        global $CFG;
85
 
86
        // Get the necessary files to perform backup and restore.
87
        require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
88
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
89
 
90
        $backupid = 'test-seb-backup-restore';
91
 
92
        $bc = new \backup_controller(\backup::TYPE_1ACTIVITY, $this->quiz->coursemodule, \backup::FORMAT_MOODLE,
93
            \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $this->user->id);
94
        $bc->execute_plan();
95
 
96
        $results = $bc->get_results();
97
        $file = $results['backup_destination'];
98
        $fp = get_file_packer('application/vnd.moodle.backup');
99
        $filepath = $CFG->dataroot . '/temp/backup/' . $backupid;
100
        $file->extract_to_pathname($fp, $filepath);
101
        $bc->destroy();
102
 
103
        return $backupid;
104
    }
105
 
106
    /**
107
     * A helper method to restore provided backup.
108
     *
109
     * @param string $backupid Backup ID to restore.
110
     */
111
    protected function restore_quiz($backupid) {
112
        $rc = new \restore_controller($backupid, $this->course->id,
113
            \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $this->user->id, \backup::TARGET_CURRENT_ADDING);
114
        $this->assertTrue($rc->execute_precheck());
115
        $rc->execute_plan();
116
        $rc->destroy();
117
    }
118
 
119
    /**
120
     * A helper method to emulate restoring to a different site.
121
     */
122
    protected function change_site() {
123
        set_config('siteidentifier', random_string(32) . 'not the same site');
124
    }
125
 
126
    /**
127
     * A helper method to validate backup and restore results.
128
     *
129
     * @param cm_info $newcm Restored course_module object.
130
     */
131
    protected function validate_backup_restore(\cm_info $newcm) {
132
        $this->assertEquals(2, seb_quiz_settings::count_records());
133
        $actual = seb_quiz_settings::get_record(['quizid' => $newcm->instance]);
134
 
135
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
136
        $this->assertEquals($expected->get('templateid'), $actual->get('templateid'));
137
        $this->assertEquals($expected->get('requiresafeexambrowser'), $actual->get('requiresafeexambrowser'));
138
        $this->assertEquals($expected->get('showsebdownloadlink'), $actual->get('showsebdownloadlink'));
139
        $this->assertEquals($expected->get('allowuserquitseb'), $actual->get('allowuserquitseb'));
140
        $this->assertEquals($expected->get('quitpassword'), $actual->get('quitpassword'));
141
        $this->assertEquals($expected->get('allowedbrowserexamkeys'), $actual->get('allowedbrowserexamkeys'));
142
 
143
        // Validate specific SEB config settings.
144
        foreach (settings_provider::get_seb_config_elements() as $name => $notused) {
145
            $name = preg_replace("/^seb_/", "", $name);
146
            $this->assertEquals($expected->get($name), $actual->get($name));
147
        }
148
    }
149
 
150
    /**
151
     * Test backup and restore when no seb.
152
     */
11 efrain 153
    public function test_backup_restore_no_seb(): void {
1 efrain 154
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_NO);
155
        $this->assertEquals(0, seb_quiz_settings::count_records());
156
 
157
        $this->backup_and_restore_quiz();
158
        $this->assertEquals(0, seb_quiz_settings::count_records());
159
    }
160
 
161
    /**
162
     * Test backup and restore when manually configured.
163
     */
11 efrain 164
    public function test_backup_restore_manual_config(): void {
1 efrain 165
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
166
 
167
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
168
        $expected->set('showsebdownloadlink', 0);
169
        $expected->set('quitpassword', '123');
170
        $expected->save();
171
 
172
        $this->assertEquals(1, seb_quiz_settings::count_records());
173
 
174
        $newcm = $this->backup_and_restore_quiz();
175
        $this->validate_backup_restore($newcm);
176
    }
177
 
178
    /**
179
     * Test backup and restore when using template.
180
     */
11 efrain 181
    public function test_backup_restore_template_config(): void {
1 efrain 182
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
183
 
184
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
185
        $template = $this->create_template();
186
        $expected->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
187
        $expected->set('templateid', $template->get('id'));
188
        $expected->save();
189
 
190
        $this->assertEquals(1, seb_quiz_settings::count_records());
191
 
192
        $newcm = $this->backup_and_restore_quiz();
193
        $this->validate_backup_restore($newcm);
194
    }
195
 
196
    /**
1441 ariadna 197
     * Test backup and restore when using template when said template is disabled.
198
     *
199
     * @covers \quizaccess_seb\seb_quiz_settings::get_record
200
     * @covers \restore_quizaccess_seb_subplugin::process_quizaccess_seb_quizsettings
201
     */
202
    public function test_backup_restore_disabled_template_config(): void {
203
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
204
 
205
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
206
        $template = $this->create_template();
207
        $expected->set('requiresafeexambrowser', settings_provider::USE_SEB_TEMPLATE);
208
        $expected->set('templateid', $template->get('id'));
209
        $expected->save();
210
 
211
        // Disable template.
212
        $template->set('enabled', 0);
213
        $template->save();
214
 
215
        $this->assertEquals(1, seb_quiz_settings::count_records());
216
 
217
        $newcm = $this->backup_and_restore_quiz();
218
 
219
        $this->assertEquals(2, seb_quiz_settings::count_records());
220
        $actual = seb_quiz_settings::get_record(['quizid' => $newcm->instance]);
221
 
222
        // Test that the restored quiz no longer uses SEB.
223
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
224
        $this->assertEquals(0, $actual->get('templateid'));
225
        $this->assertEquals(settings_provider::USE_SEB_NO, $actual->get('requiresafeexambrowser'));
226
        $this->assertEquals($expected->get('showsebdownloadlink'), $actual->get('showsebdownloadlink'));
227
        $this->assertEquals($expected->get('allowuserquitseb'), $actual->get('allowuserquitseb'));
228
        $this->assertEquals($expected->get('quitpassword'), $actual->get('quitpassword'));
229
        $this->assertEquals($expected->get('allowedbrowserexamkeys'), $actual->get('allowedbrowserexamkeys'));
230
 
231
        // Validate specific SEB config settings.
232
        foreach (settings_provider::get_seb_config_elements() as $name => $notused) {
233
            $name = preg_replace("/^seb_/", "", $name);
234
            $this->assertEquals($expected->get($name), $actual->get($name));
235
        }
236
    }
237
 
238
    /**
1 efrain 239
     * Test backup and restore when using uploaded file.
240
     */
11 efrain 241
    public function test_backup_restore_uploaded_config(): void {
1 efrain 242
        $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
243
 
244
        $expected = seb_quiz_settings::get_record(['quizid' => $this->quiz->id]);
245
        $expected->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
1441 ariadna 246
        $xml = file_get_contents(self::get_fixture_path(__NAMESPACE__, 'unencrypted.seb'));
1 efrain 247
        $this->create_module_test_file($xml, $this->quiz->cmid);
248
        $expected->save();
249
 
250
        $this->assertEquals(1, seb_quiz_settings::count_records());
251
 
252
        $newcm = $this->backup_and_restore_quiz();
253
        $this->validate_backup_restore($newcm);
254
 
255
        $expectedfile = settings_provider::get_module_context_sebconfig_file($this->quiz->cmid);
256
        $actualfile = settings_provider::get_module_context_sebconfig_file($newcm->id);
257
 
258
        $this->assertEquals($expectedfile->get_content(), $actualfile->get_content());
259
    }
260
 
261
    /**
262
     * No new template should be restored if restoring to a different site,
263
     * but the template with  the same name and content exists..
264
     */
11 efrain 265
    public function test_restore_template_to_a_different_site_when_the_same_template_exists(): void {
1 efrain 266
        $this->create_quiz_with_template();
267
        $backupid = $this->backup_quiz();
268
 
269
        $this->assertEquals(1, seb_quiz_settings::count_records());
270
        $this->assertEquals(1, template::count_records());
271
 
272
        $this->change_site();
273
        $this->restore_quiz($backupid);
274
 
275
        // Should see additional setting record, but no new template record.
276
        $this->assertEquals(2, seb_quiz_settings::count_records());
277
        $this->assertEquals(1, template::count_records());
278
    }
279
 
280
    /**
281
     * A new template should be restored if restoring to a different site, but existing template
282
     * has the same content, but different name.
283
     */
11 efrain 284
    public function test_restore_template_to_a_different_site_when_the_same_content_but_different_name(): void {
1 efrain 285
        $this->create_quiz_with_template();
286
        $backupid = $this->backup_quiz();
287
 
288
        $this->assertEquals(1, seb_quiz_settings::count_records());
289
        $this->assertEquals(1, template::count_records());
290
 
291
        $this->template->set('name', 'New name for template');
292
        $this->template->save();
293
 
294
        $this->change_site();
295
        $this->restore_quiz($backupid);
296
 
297
        // Should see additional setting record, and new template record.
298
        $this->assertEquals(2, seb_quiz_settings::count_records());
299
        $this->assertEquals(2, template::count_records());
300
    }
301
 
302
    /**
303
     * A new template should be restored if restoring to a different site, but existing template
304
     * has the same name, but different content.
305
     */
11 efrain 306
    public function test_restore_template_to_a_different_site_when_the_same_name_but_different_content(): void {
1 efrain 307
        global $CFG;
308
 
309
        $this->create_quiz_with_template();
310
        $backupid = $this->backup_quiz();
311
 
312
        $this->assertEquals(1, seb_quiz_settings::count_records());
313
        $this->assertEquals(1, template::count_records());
314
 
315
        $newxml = file_get_contents($CFG->dirroot . '/mod/quiz/accessrule/seb/tests/fixtures/simpleunencrypted.seb');
316
        $this->template->set('content', $newxml);
317
        $this->template->save();
318
 
319
        $this->change_site();
320
        $this->restore_quiz($backupid);
321
 
322
        // Should see additional setting record, and new template record.
323
        $this->assertEquals(2, seb_quiz_settings::count_records());
324
        $this->assertEquals(2, template::count_records());
325
    }
326
 
327
}