Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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   * mod_h5pactivity generator tests
  19   *
  20   * @package    mod_h5pactivity
  21   * @category   test
  22   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_h5pactivity;
  27  
  28  use advanced_testcase;
  29  use backup;
  30  use backup_controller;
  31  use backup_setting;
  32  use restore_controller;
  33  use restore_dbops;
  34  use stdClass;
  35  
  36  /**
  37   * Genarator tests class for mod_h5pactivity.
  38   *
  39   * @package    mod_h5pactivity
  40   * @category   test
  41   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class restore_test extends advanced_testcase {
  45  
  46      /**
  47       * Setup to ensure that fixtures are loaded.
  48       */
  49      public static function setupBeforeClass(): void {
  50          global $CFG;
  51          require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  52          require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  53      }
  54  
  55      /**
  56       * Test on H5P activity backup and restore.
  57       *
  58       * @dataProvider backup_restore_data
  59       * @param bool $content if has to create attempts
  60       * @param bool $userdata if backup have userdata
  61       * @param array $result1 data to check on original course
  62       * @param array $result2 data to check on resotred course
  63       */
  64      public function test_backup_restore(bool $content, bool $userdata, array $result1, array $result2) {
  65          global $DB;
  66          $this->resetAfterTest();
  67  
  68          $this->setAdminUser();
  69  
  70          $course = $this->getDataGenerator()->create_course();
  71          $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
  72  
  73          // Create one activity.
  74          $this->assertFalse($DB->record_exists('h5pactivity', ['course' => $course->id]));
  75          $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
  76          $cm = get_coursemodule_from_id('h5pactivity', $activity->cmid, 0, false, MUST_EXIST);
  77  
  78          if ($content) {
  79              $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
  80              $params = ['cmid' => $cm->id, 'userid' => $user->id];
  81              $generator->create_content($activity, $params);
  82          }
  83  
  84          $this->assertEquals($result1[0], $DB->count_records('h5pactivity', ['course' => $course->id]));
  85          $this->assertEquals($result1[1], $DB->count_records('h5pactivity_attempts', ['h5pactivityid' => $activity->id]));
  86          $attemptid = $DB->get_field('h5pactivity_attempts', 'id', ['h5pactivityid' => $activity->id]);
  87          $this->assertEquals($result1[2], $DB->count_records('h5pactivity_attempts_results', ['attemptid' => $attemptid]));
  88  
  89          // Execute course backup and restore.
  90          $newcourseid = $this->backup_and_restore($course, $userdata);
  91  
  92          // Check original activity.
  93          $this->assertEquals($result1[0], $DB->count_records('h5pactivity', ['course' => $course->id]));
  94          $this->assertEquals($result1[1], $DB->count_records('h5pactivity_attempts', ['h5pactivityid' => $activity->id]));
  95          $attempt = $DB->get_record('h5pactivity_attempts', ['h5pactivityid' => $activity->id]);
  96          $attemptid = $attempt->id ?? 0;
  97          $this->assertEquals($result1[2], $DB->count_records('h5pactivity_attempts_results', ['attemptid' => $attemptid]));
  98  
  99          // Check original activity.
 100          $this->assertEquals($result2[0], $DB->count_records('h5pactivity', ['course' => $newcourseid]));
 101          $activity2 = $DB->get_record('h5pactivity', ['course' => $newcourseid]);
 102          $this->assertEquals($result2[1], $DB->count_records('h5pactivity_attempts', ['h5pactivityid' => $activity2->id]));
 103          $attempt2 = $DB->get_record('h5pactivity_attempts', ['h5pactivityid' => $activity2->id]);
 104          $attempt2id = $attempt2->id ?? 0;
 105          $this->assertEquals($result2[2], $DB->count_records('h5pactivity_attempts_results', ['attemptid' => $attempt2id]));
 106  
 107          // Compare activities.
 108          $this->assertEquals($newcourseid, $activity2->course);
 109          $this->assertEquals($activity->name, $activity2->name);
 110          $this->assertEquals($activity->intro, $activity2->intro);
 111          $this->assertEquals($activity->introformat, $activity2->introformat);
 112          $this->assertEquals($activity->grade, $activity2->grade);
 113          $this->assertEquals($activity->displayoptions, $activity2->displayoptions);
 114          $this->assertEquals($activity->enabletracking, $activity2->enabletracking);
 115          $this->assertEquals($activity->grademethod, $activity2->grademethod);
 116  
 117          // Compare attempts.
 118          if ($content && $userdata) {
 119              $this->assertEquals($activity2->id, $attempt2->h5pactivityid);
 120              $this->assertEquals($attempt->userid, $attempt2->userid);
 121              $this->assertEquals($attempt->timecreated, $attempt2->timecreated);
 122              $this->assertEquals($attempt->timemodified, $attempt2->timemodified);
 123              $this->assertEquals($attempt->attempt, $attempt2->attempt);
 124              $this->assertEquals($attempt->rawscore, $attempt2->rawscore);
 125              $this->assertEquals($attempt->maxscore, $attempt2->maxscore);
 126              $this->assertEquals($attempt->duration, $attempt2->duration);
 127              $this->assertEquals($attempt->completion, $attempt2->completion);
 128              $this->assertEquals($attempt->success, $attempt2->success);
 129  
 130              // Compare results.
 131              $results = $DB->get_records('h5pactivity_attempts_results', ['attemptid' => $attempt->id]);
 132              foreach ($results as $result) {
 133                  $result2 = $DB->get_record('h5pactivity_attempts_results', [
 134                      'subcontent' => $result->subcontent, 'attemptid' => $attempt2->id
 135                  ]);
 136                  $this->assertNotFalse($result2);
 137                  $this->assertEquals($result->timecreated, $result2->timecreated);
 138                  $this->assertEquals($result->interactiontype, $result2->interactiontype);
 139                  $this->assertEquals($result->description, $result2->description);
 140                  $this->assertEquals($result->correctpattern, $result2->correctpattern);
 141                  $this->assertEquals($result->response, $result2->response);
 142                  $this->assertEquals($result->additionals, $result2->additionals);
 143                  $this->assertEquals($result->rawscore, $result2->rawscore);
 144                  $this->assertEquals($result->maxscore, $result2->maxscore);
 145                  $this->assertEquals($result->duration, $result2->duration);
 146                  $this->assertEquals($result->completion, $result2->completion);
 147                  $this->assertEquals($result->success, $result2->success);
 148              }
 149          }
 150  
 151      }
 152  
 153      /**
 154       * Data provider for test_backup_restore.
 155       *
 156       * @return array
 157       */
 158      public function backup_restore_data(): array {
 159          return [
 160              'Activity attempts and restore with userdata' => [
 161                  true, true, [1, 1, 3], [1, 1, 3]
 162              ],
 163              'No activity attempts and restore with userdata' => [
 164                  false, true, [1, 0, 0], [1, 0, 0]
 165              ],
 166              'Activity attempts and restore with no userdata' => [
 167                  true, false, [1, 1, 3], [1, 0, 0]
 168              ],
 169              'No activity attempts and restore with no userdata' => [
 170                  false, false, [1, 0, 0], [1, 0, 0]
 171              ],
 172          ];
 173      }
 174  
 175      /**
 176       * Backs a course up and restores it.
 177       *
 178       * @param stdClass $srccourse Course object to backup
 179       * @param bool $userdata if the backup must be with user data
 180       * @return int ID of newly restored course
 181       */
 182      private function backup_and_restore(stdClass $srccourse, bool $userdata): int {
 183          global $USER, $CFG;
 184  
 185          require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
 186  
 187          // Turn off file logging, otherwise it can't delete the file (Windows).
 188          $CFG->backup_file_logger_level = backup::LOG_NONE;
 189  
 190          // Do backup with default settings. MODE_IMPORT means it will just
 191          // create the directory and not zip it.
 192          $bc = new backup_controller(backup::TYPE_1COURSE, $srccourse->id,
 193                  backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
 194                  $USER->id);
 195  
 196          $bc->get_plan()->get_setting('users')->set_status(backup_setting::NOT_LOCKED);
 197          $bc->get_plan()->get_setting('users')->set_value($userdata);
 198  
 199          $backupid = $bc->get_backupid();
 200          $bc->execute_plan();
 201          $bc->destroy();
 202  
 203          // Do restore to new course with default settings.
 204          $newcourseid = restore_dbops::create_new_course(
 205              $srccourse->fullname, $srccourse->shortname . '_2', $srccourse->category
 206          );
 207          $rc = new restore_controller($backupid, $newcourseid,
 208                  backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
 209                  backup::TARGET_NEW_COURSE);
 210  
 211          $rc->get_plan()->get_setting('users')->set_status(backup_setting::NOT_LOCKED);
 212          $rc->get_plan()->get_setting('users')->set_value($userdata);
 213  
 214          $this->assertTrue($rc->execute_precheck());
 215          $rc->execute_plan();
 216          $rc->destroy();
 217  
 218          return $newcourseid;
 219      }
 220  }