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