forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexternallib_test.php
180 lines (147 loc) · 6.88 KB
/
externallib_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_backup;
use backup;
use core_backup_external;
use core_external\external_api;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/backup/externallib.php');
/**
* Backup webservice tests.
*
* @package core_backup
* @copyright 2020 onward The Moodle Users Association <https://moodleassociation.org/>
* @author Matt Porritt <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class externallib_test extends externallib_advanced_testcase {
/**
* Set up tasks for all tests.
*/
protected function setUp(): void {
global $CFG;
$this->resetAfterTest(true);
// Disable all loggers.
$CFG->backup_error_log_logger_level = backup::LOG_NONE;
$CFG->backup_output_indented_logger_level = backup::LOG_NONE;
$CFG->backup_file_logger_level = backup::LOG_NONE;
$CFG->backup_database_logger_level = backup::LOG_NONE;
$CFG->backup_file_logger_level_extra = backup::LOG_NONE;
}
/**
* Test getting course copy progress.
*/
public function test_get_copy_progress() {
global $USER;
$this->setAdminUser();
// Create a course with some availability data set.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$courseid = $course->id;
// Mock up the form data for use in tests.
$formdata = new \stdClass;
$formdata->courseid = $courseid;
$formdata->fullname = 'foo';
$formdata->shortname = 'bar';
$formdata->category = 1;
$formdata->visible = 1;
$formdata->startdate = 1582376400;
$formdata->enddate = 0;
$formdata->idnumber = 123;
$formdata->userdata = 1;
$formdata->role_1 = 1;
$formdata->role_3 = 3;
$formdata->role_5 = 5;
$copydata = \copy_helper::process_formdata($formdata);
$copydetails = \copy_helper::create_copy($copydata);
$copydetails['operation'] = \backup::OPERATION_BACKUP;
$params = array('copies' => $copydetails);
$returnvalue = core_backup_external::get_copy_progress($params);
// We need to execute the return values cleaning process to simulate the web service server.
$returnvalue = external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue);
$this->assertEquals(\backup::STATUS_AWAITING, $returnvalue[0]['status']);
$this->assertEquals(0, $returnvalue[0]['progress']);
$this->assertEquals($copydetails['backupid'], $returnvalue[0]['backupid']);
$this->assertEquals(\backup::OPERATION_BACKUP, $returnvalue[0]['operation']);
// We are expecting trace output during this test.
$this->expectOutputRegex("/$courseid/");
// Execute adhoc task and create the copy.
$now = time();
$task = \core\task\manager::get_next_adhoc_task($now);
$this->assertInstanceOf('\\core\\task\\asynchronous_copy_task', $task);
$task->execute();
\core\task\manager::adhoc_task_complete($task);
// Check the copy progress now.
$params = array('copies' => $copydetails);
$returnvalue = core_backup_external::get_copy_progress($params);
$returnvalue = external_api::clean_returnvalue(core_backup_external::get_copy_progress_returns(), $returnvalue);
$this->assertEquals(\backup::STATUS_FINISHED_OK, $returnvalue[0]['status']);
$this->assertEquals(1, $returnvalue[0]['progress']);
$this->assertEquals($copydetails['restoreid'], $returnvalue[0]['backupid']);
$this->assertEquals(\backup::OPERATION_RESTORE, $returnvalue[0]['operation']);
}
/**
* Test ajax submission of course copy process.
*/
public function test_submit_copy_form() {
global $DB;
$this->setAdminUser();
// Create a course with some availability data set.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$courseid = $course->id;
// Moodle form requires this for validation.
$sesskey = sesskey();
$_POST['sesskey'] = $sesskey;
// Mock up the form data for use in tests.
$formdata = new \stdClass;
$formdata->courseid = $courseid;
$formdata->returnto = '';
$formdata->returnurl = '';
$formdata->sesskey = $sesskey;
$formdata->_qf__core_backup_output_copy_form = 1;
$formdata->fullname = 'foo';
$formdata->shortname = 'bar';
$formdata->category = 1;
$formdata->visible = 1;
$formdata->startdate = array('day' => 5, 'month' => 5, 'year' => 2020, 'hour' => 0, 'minute' => 0);
$formdata->idnumber = 123;
$formdata->userdata = 1;
$formdata->role_1 = 1;
$formdata->role_3 = 3;
$formdata->role_5 = 5;
$urlform = http_build_query($formdata, '', '&'); // Take the form data and url encode it.
$jsonformdata = json_encode($urlform); // Take form string and JSON encode.
$returnvalue = core_backup_external::submit_copy_form($jsonformdata);
$returnjson = external_api::clean_returnvalue(core_backup_external::submit_copy_form_returns(), $returnvalue);
$copyids = json_decode($returnjson, true);
$backuprec = $DB->get_record('backup_controllers', array('backupid' => $copyids['backupid']));
$restorerec = $DB->get_record('backup_controllers', array('backupid' => $copyids['restoreid']));
// Check backup was completed successfully.
$this->assertEquals(backup::STATUS_AWAITING, $backuprec->status);
$this->assertEquals(0, $backuprec->progress);
$this->assertEquals('backup', $backuprec->operation);
// Check restore was completed successfully.
$this->assertEquals(backup::STATUS_REQUIRE_CONV, $restorerec->status);
$this->assertEquals(0, $restorerec->progress);
$this->assertEquals('restore', $restorerec->operation);
}
}