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