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.
   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   * Backup restore base tests.
  19   *
  20   * @package   core_backup
  21   * @copyright Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  29  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  30  
  31  /**
  32   * Basic testcase class for backup / restore functionality.
  33   */
  34  abstract class core_backup_backup_restore_base_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Setup test data.
  38       */
  39      protected function setUp(): void {
  40          $this->resetAfterTest();
  41          $this->setAdminUser();
  42      }
  43  
  44      /**
  45       * Backup the course by general mode.
  46       *
  47       * @param  stdClass $course Course for backup.
  48       * @return string Hash string ID from the backup.
  49       * @throws coding_exception
  50       * @throws moodle_exception
  51       */
  52      protected function perform_backup($course): string {
  53          global $CFG, $USER;
  54  
  55          $coursecontext = context_course::instance($course->id);
  56  
  57          // Start backup process.
  58          $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
  59                  backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id);
  60          $bc->execute_plan();
  61          $backupid = $bc->get_backupid();
  62          $bc->destroy();
  63  
  64          // Get the backup file.
  65          $fs = get_file_storage();
  66          $files = $fs->get_area_files($coursecontext->id, 'backup', 'course', false, 'id ASC');
  67          $backupfile = reset($files);
  68  
  69          // Extract backup file.
  70          $path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupid;
  71  
  72          $fp = get_file_packer('application/vnd.moodle.backup');
  73          $fp->extract_to_pathname($backupfile, $path);
  74  
  75          return $backupid;
  76      }
  77  
  78      /**
  79       * Restore from backupid to course.
  80       *
  81       * @param  string   $backupid Hash string ID from backup.
  82       * @param  stdClass $course Course which is restored for.
  83       * @throws restore_controller_exception
  84       */
  85      protected function perform_restore($backupid, $course): void {
  86          global $USER;
  87  
  88          // Set up restore.
  89          $rc = new restore_controller($backupid, $course->id,
  90                  backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, backup::TARGET_EXISTING_ADDING);
  91          // Execute restore.
  92          $rc->execute_precheck();
  93          $rc->execute_plan();
  94          $rc->destroy();
  95      }
  96  
  97      /**
  98       * Import course from course1 to course2.
  99       *
 100       * @param stdClass $course1 Course to be backuped up.
 101       * @param stdClass $course2 Course to be restored.
 102       * @throws restore_controller_exception
 103       */
 104      protected function perform_import($course1, $course2): void {
 105          global $USER;
 106  
 107          // Start backup process.
 108          $bc = new backup_controller(backup::TYPE_1COURSE, $course1->id, backup::FORMAT_MOODLE,
 109                  backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
 110          $backupid = $bc->get_backupid();
 111          $bc->execute_plan();
 112          $bc->destroy();
 113  
 114          // Set up restore.
 115          $rc = new restore_controller($backupid, $course2->id,
 116                  backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $USER->id, backup::TARGET_EXISTING_ADDING);
 117          // Execute restore.
 118          $rc->execute_precheck();
 119          $rc->execute_plan();
 120          $rc->destroy();
 121      }
 122  
 123  }