Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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 webservice tests.
  19   *
  20   * @package    core_backup
  21   * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>
  22   * @author     Matt Porritt <mattp@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  30  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  require_once($CFG->dirroot . '/backup/externallib.php');
  33  
  34  /**
  35   * Backup webservice tests.
  36   *
  37   * @package    core_backup
  38   * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>
  39   * @author     Matt Porritt <mattp@catalyst-au.net>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class backup_external_testcase extends externallib_advanced_testcase {
  43  
  44      /**
  45       * Set up tasks for all tests.
  46       */
  47      protected function setUp(): void {
  48          global $CFG;
  49  
  50          $this->resetAfterTest(true);
  51  
  52          // Disable all loggers.
  53          $CFG->backup_error_log_logger_level = backup::LOG_NONE;
  54          $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
  55          $CFG->backup_file_logger_level = backup::LOG_NONE;
  56          $CFG->backup_database_logger_level = backup::LOG_NONE;
  57          $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
  58      }
  59  
  60      /**
  61       * Test getting course copy progress.
  62       */
  63      public function test_get_copy_progress() {
  64          global $USER;
  65  
  66          $this->setAdminUser();
  67  
  68          // Create a course with some availability data set.
  69          $generator = $this->getDataGenerator();
  70          $course = $generator->create_course();
  71          $courseid = $course->id;
  72  
  73          // Mock up the form data for use in tests.
  74          $formdata = new \stdClass;
  75          $formdata->courseid = $courseid;
  76          $formdata->fullname = 'foo';
  77          $formdata->shortname = 'bar';
  78          $formdata->category = 1;
  79          $formdata->visible = 1;
  80          $formdata->startdate = 1582376400;
  81          $formdata->enddate = 0;
  82          $formdata->idnumber = 123;
  83          $formdata->userdata = 1;
  84          $formdata->role_1 = 1;
  85          $formdata->role_3 = 3;
  86          $formdata->role_5 = 5;
  87  
  88          $coursecopy = new \core_backup\copy\copy($formdata);
  89          $copydetails = $coursecopy->create_copy();
  90          $copydetails['operation'] = \backup::OPERATION_BACKUP;
  91  
  92          $params = array('copies' => $copydetails);
  93          $returnvalue = core_backup_external::get_copy_progress($params);
  94  
  95          // We need to execute the return values cleaning process to simulate the web service server.
  96          $returnvalue = external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue);
  97  
  98          $this->assertEquals(\backup::STATUS_AWAITING, $returnvalue[0]['status']);
  99          $this->assertEquals(0, $returnvalue[0]['progress']);
 100          $this->assertEquals($copydetails['backupid'], $returnvalue[0]['backupid']);
 101          $this->assertEquals(\backup::OPERATION_BACKUP, $returnvalue[0]['operation']);
 102  
 103          // We are expecting trace output during this test.
 104          $this->expectOutputRegex("/$courseid/");
 105  
 106          // Execute adhoc task and create the copy.
 107          $now = time();
 108          $task = \core\task\manager::get_next_adhoc_task($now);
 109          $this->assertInstanceOf('\\core\\task\\asynchronous_copy_task', $task);
 110          $task->execute();
 111          \core\task\manager::adhoc_task_complete($task);
 112  
 113          // Check the copy progress now.
 114          $params = array('copies' => $copydetails);
 115          $returnvalue = core_backup_external::get_copy_progress($params);
 116  
 117          $returnvalue = external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue);
 118  
 119          $this->assertEquals(\backup::STATUS_FINISHED_OK, $returnvalue[0]['status']);
 120          $this->assertEquals(1, $returnvalue[0]['progress']);
 121          $this->assertEquals($copydetails['restoreid'], $returnvalue[0]['backupid']);
 122          $this->assertEquals(\backup::OPERATION_RESTORE, $returnvalue[0]['operation']);
 123  
 124      }
 125  
 126      /**
 127       * Test ajax submission of course copy process.
 128       */
 129      public function test_submit_copy_form() {
 130          global $DB;
 131  
 132          $this->setAdminUser();
 133  
 134          // Create a course with some availability data set.
 135          $generator = $this->getDataGenerator();
 136          $course = $generator->create_course();
 137          $courseid = $course->id;
 138  
 139          // Moodle form requires this for validation.
 140          $sesskey = sesskey();
 141          $_POST['sesskey'] = $sesskey;
 142  
 143          // Mock up the form data for use in tests.
 144          $formdata = new \stdClass;
 145          $formdata->courseid = $courseid;
 146          $formdata->returnto = '';
 147          $formdata->returnurl = '';
 148          $formdata->sesskey = $sesskey;
 149          $formdata->_qf__core_backup_output_copy_form = 1;
 150          $formdata->fullname = 'foo';
 151          $formdata->shortname = 'bar';
 152          $formdata->category = 1;
 153          $formdata->visible = 1;
 154          $formdata->startdate = array('day' => 5, 'month' => 5, 'year' => 2020, 'hour' => 0, 'minute' => 0);
 155          $formdata->idnumber = 123;
 156          $formdata->userdata = 1;
 157          $formdata->role_1 = 1;
 158          $formdata->role_3 = 3;
 159          $formdata->role_5 = 5;
 160  
 161          $urlform = http_build_query($formdata, '', '&'); // Take the form data and url encode it.
 162          $jsonformdata = json_encode($urlform); // Take form string and JSON encode.
 163  
 164          $returnvalue = core_backup_external::submit_copy_form($jsonformdata);
 165  
 166          $returnjson = external_api::clean_returnvalue(core_backup_external::submit_copy_form_returns(), $returnvalue);
 167          $copyids = json_decode($returnjson, true);
 168  
 169          $backuprec = $DB->get_record('backup_controllers', array('backupid' => $copyids['backupid']));
 170          $restorerec = $DB->get_record('backup_controllers', array('backupid' => $copyids['restoreid']));
 171  
 172          // Check backup was completed successfully.
 173          $this->assertEquals(backup::STATUS_AWAITING, $backuprec->status);
 174          $this->assertEquals(0, $backuprec->progress);
 175          $this->assertEquals('backup', $backuprec->operation);
 176  
 177          // Check restore was completed successfully.
 178          $this->assertEquals(backup::STATUS_REQUIRE_CONV, $restorerec->status);
 179          $this->assertEquals(0, $restorerec->progress);
 180          $this->assertEquals('restore', $restorerec->operation);
 181      }
 182  }