PageRenderTime 98ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/quiz/accessrule/seb/tests/phpunit/backup_restore_test.php

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