mirrored from git://git.moodle.org/moodle.git
/
controller_test.php
278 lines (232 loc) · 10.5 KB
/
controller_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?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/>.
/**
* @package core_backup
* @category test
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_backup;
use backup;
use backup_controller;
use restore_controller;
defined('MOODLE_INTERNAL') || die();
// Include all the needed stuff.
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
/**
* Tests for the backup and restore controller classes.
*
* @package core_backup
* @category test
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class controller_test extends \advanced_testcase {
protected $moduleid; // course_modules id used for testing
protected $sectionid; // course_sections id used for testing
protected $courseid; // course id used for testing
protected $userid; // user used if for testing
protected function setUp(): void {
global $DB, $CFG;
$this->resetAfterTest(true);
$course = $this->getDataGenerator()->create_course();
$page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
$coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
$this->moduleid = $coursemodule->id;
$this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
$this->courseid = $coursemodule->course;
$this->userid = 2; // admin
// 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 get_copy
*
* @covers \restore_controller::get_copy
*/
public function test_restore_controller_get_copy() {
$copydata = (object)["some" => "copydata"];
$rc = new \restore_controller(1729, $this->courseid, backup::INTERACTIVE_NO, backup::MODE_COPY,
$this->userid, backup::TARGET_NEW_COURSE, null, backup::RELEASESESSION_NO, $copydata);
$this->assertEquals($copydata, $rc->get_copy());
}
/**
* Test instantiating a restore controller for a course copy without providing copy data.
*
* @covers \restore_controller::__construct
*/
public function test_restore_controller_copy_without_copydata() {
$this->expectException(\restore_controller_exception::class);
new \restore_controller(1729, $this->courseid, backup::INTERACTIVE_NO, backup::MODE_COPY,
$this->userid, backup::TARGET_NEW_COURSE);
}
/*
* test base_setting class
*/
public function test_backup_controller() {
// Instantiate non interactive backup_controller
$bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
$this->assertTrue($bc instanceof backup_controller);
$this->assertEquals($bc->get_status(), backup::STATUS_AWAITING);
// Instantiate interactive backup_controller
$bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_YES, backup::MODE_GENERAL, $this->userid);
$this->assertTrue($bc instanceof backup_controller);
$this->assertEquals($bc->get_status(), backup::STATUS_SETTING_UI);
$this->assertEquals(strlen($bc->get_backupid()), 32); // is one md5
// Save and load one backup controller to check everything is in place
$bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
$recid = $bc->save_controller();
$newbc = mock_backup_controller::load_controller($bc->get_backupid());
$this->assertTrue($newbc instanceof backup_controller); // This means checksum and load worked ok
}
public function test_backup_controller_include_files() {
// A MODE_GENERAL controller - this should include files
$bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
$this->assertEquals($bc->get_include_files(), 1);
// The MODE_IMPORT and MODE_SAMESITE should not include files in the backup.
// A MODE_IMPORT controller
$bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$this->assertEquals($bc->get_include_files(), 0);
// A MODE_SAMESITE controller
$bc = new mock_backup_controller(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$this->assertEquals($bc->get_include_files(), 0);
}
/**
* Test set kept roles method.
*/
public function test_backup_controller_set_kept_roles() {
$this->expectException(\backup_controller_exception::class);
// Set up controller as a non-copy operation.
$bc = new \backup_controller(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid, backup::RELEASESESSION_YES);
$bc->set_kept_roles(array(1, 3, 5));
}
/**
* Tests the restore_controller.
*/
public function test_restore_controller_is_executing() {
global $CFG;
// Make a backup.
make_backup_temp_directory('');
$bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$backupid = $bc->get_backupid();
$bc->execute_plan();
$bc->destroy();
// The progress class will get called during restore, so we can use that
// to check the executing flag is true.
$progress = new core_backup_progress_restore_is_executing();
// Set up restore.
$rc = new restore_controller($backupid, $this->courseid,
backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid,
backup::TARGET_EXISTING_ADDING);
$this->assertTrue($rc->execute_precheck());
// Check restore is NOT executing.
$this->assertFalse(restore_controller::is_executing());
// Execute restore.
$rc->set_progress($progress);
$rc->execute_plan();
// Check restore is NOT executing afterward either.
$this->assertFalse(restore_controller::is_executing());
$rc->destroy();
// During restore, check that executing was true.
$this->assertTrue(count($progress->executing) > 0);
$alltrue = true;
foreach ($progress->executing as $executing) {
if (!$executing) {
$alltrue = false;
break;
}
}
$this->assertTrue($alltrue);
}
/**
* Test prepare copy method.
*/
public function test_restore_controller_prepare_copy() {
$this->expectException(\restore_controller_exception::class);
global $CFG;
// Make a backup.
make_backup_temp_directory('');
$bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
$backupid = $bc->get_backupid();
$bc->execute_plan();
$bc->destroy();
// Set up restore.
$rc = new restore_controller($backupid, $this->courseid,
backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid,
backup::TARGET_EXISTING_ADDING);
$rc->prepare_copy();
}
/**
* Test restore of deadlock causing backup.
*/
public function test_restore_of_deadlock_causing_backup() {
global $USER, $CFG;
$this->preventResetByRollback();
$foldername = 'deadlock';
$fp = get_file_packer('application/vnd.moodle.backup');
$tempdir = make_backup_temp_directory($foldername);
$files = $fp->extract_to_pathname($CFG->dirroot . '/backup/controller/tests/fixtures/deadlock.mbz', $tempdir);
$this->setAdminUser();
$controller = new restore_controller(
'deadlock',
$this->courseid,
backup::INTERACTIVE_NO,
backup::MODE_GENERAL,
$USER->id,
backup::TARGET_NEW_COURSE
);
$this->assertTrue($controller->execute_precheck());
$controller->execute_plan();
$controller->destroy();
}
}
/**
* Progress class that records the result of restore_controller::is_executing calls.
*
* @package core_backup
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_backup_progress_restore_is_executing extends \core\progress\base {
/** @var array Array of results from calling function */
public $executing = array();
public function update_progress() {
$this->executing[] = restore_controller::is_executing();
}
}
/*
* helper extended @backup_controller class that makes some methods public for testing
*/
class mock_backup_controller extends backup_controller {
public function save_controller($includeobj = true, $cleanobj = false) {
parent::save_controller($includeobj, $cleanobj);
}
}