Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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  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   */
  31  class backup_restore_test extends \advanced_testcase {
  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 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 = 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, quiz_settings::count_records());
 133          $actual = quiz_settings::get_record(['quizid' => $newcm->instance]);
 134  
 135          $expected = 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       */
 153      public function test_backup_restore_no_seb() {
 154          $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_NO);
 155          $this->assertEquals(0, quiz_settings::count_records());
 156  
 157          $this->backup_and_restore_quiz();
 158          $this->assertEquals(0, quiz_settings::count_records());
 159      }
 160  
 161      /**
 162       * Test backup and restore when manually configured.
 163       */
 164      public function test_backup_restore_manual_config() {
 165          $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
 166  
 167          $expected = 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, 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       */
 181      public function test_backup_restore_template_config() {
 182          $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
 183  
 184          $expected = 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, quiz_settings::count_records());
 191  
 192          $newcm = $this->backup_and_restore_quiz();
 193          $this->validate_backup_restore($newcm);
 194      }
 195  
 196      /**
 197       * Test backup and restore when using uploaded file.
 198       */
 199      public function test_backup_restore_uploaded_config() {
 200          $this->quiz = $this->create_test_quiz($this->course, settings_provider::USE_SEB_CONFIG_MANUALLY);
 201  
 202          $expected = quiz_settings::get_record(['quizid' => $this->quiz->id]);
 203          $expected->set('requiresafeexambrowser', settings_provider::USE_SEB_UPLOAD_CONFIG);
 204          $xml = file_get_contents(__DIR__ . '/fixtures/unencrypted.seb');
 205          $this->create_module_test_file($xml, $this->quiz->cmid);
 206          $expected->save();
 207  
 208          $this->assertEquals(1, quiz_settings::count_records());
 209  
 210          $newcm = $this->backup_and_restore_quiz();
 211          $this->validate_backup_restore($newcm);
 212  
 213          $expectedfile = settings_provider::get_module_context_sebconfig_file($this->quiz->cmid);
 214          $actualfile = settings_provider::get_module_context_sebconfig_file($newcm->id);
 215  
 216          $this->assertEquals($expectedfile->get_content(), $actualfile->get_content());
 217      }
 218  
 219      /**
 220       * No new template should be restored if restoring to a different site,
 221       * but the template with  the same name and content exists..
 222       */
 223      public function test_restore_template_to_a_different_site_when_the_same_template_exists() {
 224          $this->create_quiz_with_template();
 225          $backupid = $this->backup_quiz();
 226  
 227          $this->assertEquals(1, quiz_settings::count_records());
 228          $this->assertEquals(1, template::count_records());
 229  
 230          $this->change_site();
 231          $this->restore_quiz($backupid);
 232  
 233          // Should see additional setting record, but no new template record.
 234          $this->assertEquals(2, quiz_settings::count_records());
 235          $this->assertEquals(1, template::count_records());
 236      }
 237  
 238      /**
 239       * A new template should be restored if restoring to a different site, but existing template
 240       * has the same content, but different name.
 241       */
 242      public function test_restore_template_to_a_different_site_when_the_same_content_but_different_name() {
 243          $this->create_quiz_with_template();
 244          $backupid = $this->backup_quiz();
 245  
 246          $this->assertEquals(1, quiz_settings::count_records());
 247          $this->assertEquals(1, template::count_records());
 248  
 249          $this->template->set('name', 'New name for template');
 250          $this->template->save();
 251  
 252          $this->change_site();
 253          $this->restore_quiz($backupid);
 254  
 255          // Should see additional setting record, and new template record.
 256          $this->assertEquals(2, quiz_settings::count_records());
 257          $this->assertEquals(2, template::count_records());
 258      }
 259  
 260      /**
 261       * A new template should be restored if restoring to a different site, but existing template
 262       * has the same name, but different content.
 263       */
 264      public function test_restore_template_to_a_different_site_when_the_same_name_but_different_content() {
 265          global $CFG;
 266  
 267          $this->create_quiz_with_template();
 268          $backupid = $this->backup_quiz();
 269  
 270          $this->assertEquals(1, quiz_settings::count_records());
 271          $this->assertEquals(1, template::count_records());
 272  
 273          $newxml = file_get_contents($CFG->dirroot . '/mod/quiz/accessrule/seb/tests/fixtures/simpleunencrypted.seb');
 274          $this->template->set('content', $newxml);
 275          $this->template->save();
 276  
 277          $this->change_site();
 278          $this->restore_quiz($backupid);
 279  
 280          // Should see additional setting record, and new template record.
 281          $this->assertEquals(2, quiz_settings::count_records());
 282          $this->assertEquals(2, template::count_records());
 283      }
 284  
 285  }