Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [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   * Asyncronhous restore tests.
  19   *
  20   * @package    core_backup
  21   * @copyright  2018 Matt Porritt <mattp@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  require_once($CFG->libdir . '/completionlib.php');
  31  
  32  /**
  33   * Asyncronhous restore tests.
  34   *
  35   * @copyright  2018 Matt Porritt <mattp@catalyst-au.net>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_backup_async_restore_testcase extends \core_privacy\tests\provider_testcase {
  39  
  40      /**
  41       * Tests the asynchronous backup.
  42       */
  43      public function test_async_restore() {
  44          global $DB, $CFG, $USER;
  45  
  46          $this->resetAfterTest(true);
  47          $this->setAdminUser();
  48          $CFG->enableavailability = true;
  49          $CFG->enablecompletion = true;
  50  
  51          // Create a course with some availability data set.
  52          $generator = $this->getDataGenerator();
  53          $course = $generator->create_course(
  54                  array('format' => 'topics', 'numsections' => 3,
  55                          'enablecompletion' => COMPLETION_ENABLED),
  56                  array('createsections' => true));
  57          $forum = $generator->create_module('forum', array(
  58                  'course' => $course->id));
  59          $forum2 = $generator->create_module('forum', array(
  60                  'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
  61  
  62          // We need a grade, easiest is to add an assignment.
  63          $assignrow = $generator->create_module('assign', array(
  64                  'course' => $course->id));
  65          $assign = new assign(context_module::instance($assignrow->cmid), false, false);
  66          $item = $assign->get_grade_item();
  67  
  68          // Make a test grouping as well.
  69          $grouping = $generator->create_grouping(array('courseid' => $course->id,
  70                  'name' => 'Grouping!'));
  71  
  72          $availability = '{"op":"|","show":false,"c":[' .
  73                  '{"type":"completion","cm":' . $forum2->cmid .',"e":1},' .
  74                  '{"type":"grade","id":' . $item->id . ',"min":4,"max":94},' .
  75                  '{"type":"grouping","id":' . $grouping->id . '}' .
  76                  ']}';
  77          $DB->set_field('course_modules', 'availability', $availability, array(
  78                  'id' => $forum->cmid));
  79          $DB->set_field('course_sections', 'availability', $availability, array(
  80                  'course' => $course->id, 'section' => 1));
  81  
  82          // Backup the course.
  83          $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
  84                  backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
  85          $bc->finish_ui();
  86          $backupid = $bc->get_backupid();
  87          $bc->execute_plan();
  88          $bc->destroy();
  89  
  90          // Get the backup file.
  91          $coursecontext = context_course::instance($course->id);
  92          $fs = get_file_storage();
  93          $files = $fs->get_area_files($coursecontext->id, 'backup', 'course', false, 'id ASC');
  94          $backupfile = reset($files);
  95  
  96          // Extract backup file.
  97          $backupdir = "restore_" . uniqid();
  98          $path = $CFG->tempdir . DIRECTORY_SEPARATOR . "backup" . DIRECTORY_SEPARATOR . $backupdir;
  99  
 100          $fp = get_file_packer('application/vnd.moodle.backup');
 101          $fp->extract_to_pathname($backupfile, $path);
 102  
 103          // Create restore controller.
 104          $newcourseid = restore_dbops::create_new_course(
 105                  $course->fullname, $course->shortname . '_2', $course->category);
 106          $rc = new restore_controller($backupdir, $newcourseid,
 107                  backup::INTERACTIVE_NO, backup::MODE_ASYNC, $USER->id,
 108                  backup::TARGET_NEW_COURSE);
 109  
 110          $this->assertTrue($rc->execute_precheck());
 111          $restoreid = $rc->get_restoreid();
 112  
 113          $prerestorerec = $DB->get_record('backup_controllers', array('backupid' => $restoreid));
 114          $prerestorerec->controller = '';
 115  
 116          $rc->destroy();
 117  
 118          // Create the adhoc task.
 119          $asynctask = new \core\task\asynchronous_restore_task();
 120          $asynctask->set_blocking(false);
 121          $asynctask->set_custom_data(array('backupid' => $restoreid));
 122          \core\task\manager::queue_adhoc_task($asynctask);
 123  
 124          // We are expecting trace output during this test.
 125          $this->expectOutputRegex("/$restoreid/");
 126  
 127          // Execute adhoc task.
 128          $now = time();
 129          $task = \core\task\manager::get_next_adhoc_task($now);
 130          $this->assertInstanceOf('\\core\\task\\asynchronous_restore_task', $task);
 131          $task->execute();
 132          \core\task\manager::adhoc_task_complete($task);
 133  
 134          $postrestorerec = $DB->get_record('backup_controllers', array('backupid' => $restoreid));
 135  
 136          // Check backup was created successfully.
 137          $this->assertEquals(backup::STATUS_FINISHED_OK, $postrestorerec->status);
 138          $this->assertEquals(1.0, $postrestorerec->progress);
 139      }
 140  }