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