Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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