mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
course_copy_test.php
631 lines (522 loc) · 23.6 KB
/
course_copy_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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
<?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;
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->libdir . '/completionlib.php');
/**
* Course copy tests.
*
* @package core_backup
* @copyright 2020 onward The Moodle Users Association <https://moodleassociation.org/>
* @author Matt Porritt <mattp@catalyst-au.net>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_copy_test extends \advanced_testcase {
/**
*
* @var \stdClass Course used for testing.
*/
protected $course;
/**
*
* @var int User used to perform backups.
*/
protected $userid;
/**
*
* @var array Ids of users in test course.
*/
protected $courseusers;
/**
*
* @var array Names of the created activities.
*/
protected $activitynames;
/**
* Set up tasks for all tests.
*/
protected function setUp(): void {
global $DB, $CFG, $USER;
$this->resetAfterTest(true);
$CFG->enableavailability = true;
$CFG->enablecompletion = true;
// Create a course with some availability data set.
$generator = $this->getDataGenerator();
$course = $generator->create_course(
array('format' => 'topics', 'numsections' => 3,
'enablecompletion' => COMPLETION_ENABLED),
array('createsections' => true));
$forum = $generator->create_module('forum', array(
'course' => $course->id));
$forum2 = $generator->create_module('forum', array(
'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
// We need a grade, easiest is to add an assignment.
$assignrow = $generator->create_module('assign', array(
'course' => $course->id));
$assign = new \assign(\context_module::instance($assignrow->cmid), false, false);
$item = $assign->get_grade_item();
// Make a test grouping as well.
$grouping = $generator->create_grouping(array('courseid' => $course->id,
'name' => 'Grouping!'));
// Create some users.
$user1 = $generator->create_user();
$user2 = $generator->create_user();
$user3 = $generator->create_user();
$user4 = $generator->create_user();
$this->courseusers = array(
$user1->id, $user2->id, $user3->id, $user4->id
);
// Enrol users into the course.
$generator->enrol_user($user1->id, $course->id, 'student');
$generator->enrol_user($user2->id, $course->id, 'editingteacher');
$generator->enrol_user($user3->id, $course->id, 'manager');
$generator->enrol_user($user4->id, $course->id, 'editingteacher');
$generator->enrol_user($user4->id, $course->id, 'manager');
$availability = '{"op":"|","show":false,"c":[' .
'{"type":"completion","cm":' . $forum2->cmid .',"e":1},' .
'{"type":"grade","id":' . $item->id . ',"min":4,"max":94},' .
'{"type":"grouping","id":' . $grouping->id . '}' .
']}';
$DB->set_field('course_modules', 'availability', $availability, array(
'id' => $forum->cmid));
$DB->set_field('course_sections', 'availability', $availability, array(
'course' => $course->id, 'section' => 1));
// Add some user data to the course.
$discussion = $generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id,
'forum' => $forum->id, 'userid' => $user1->id, 'timemodified' => time(),
'name' => 'Frog']);
$generator->get_plugin_generator('mod_forum')->create_post(['discussion' => $discussion->id, 'userid' => $user1->id]);
$this->course = $course;
$this->userid = $USER->id; // Admin.
$this->activitynames = array(
$forum->name,
$forum2->name,
$assignrow->name
);
// Set the user doing the backup to be a manager in the course.
// By default Managers can restore courses AND users, teachers can only do users.
$this->setUser($user3);
// 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 creating a course copy.
*/
public function test_create_copy() {
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$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);
$result = \copy_helper::create_copy($copydata);
// Load the controllers, to extract the data we need.
$bc = \backup_controller::load_controller($result['backupid']);
$rc = \restore_controller::load_controller($result['restoreid']);
// Check the backup controller.
$this->assertEquals(backup::MODE_COPY, $bc->get_mode());
$this->assertEquals($this->course->id, $bc->get_courseid());
$this->assertEquals(backup::TYPE_1COURSE, $bc->get_type());
// Check the restore controller.
$newcourseid = $rc->get_courseid();
$newcourse = get_course($newcourseid);
$this->assertEquals(get_string('copyingcourse', 'backup'), $newcourse->fullname);
$this->assertEquals(get_string('copyingcourseshortname', 'backup'), $newcourse->shortname);
$this->assertEquals(backup::MODE_COPY, $rc->get_mode());
$this->assertEquals($newcourseid, $rc->get_courseid());
// Check the created ad-hoc task.
$now = time();
$task = \core\task\manager::get_next_adhoc_task($now);
$this->assertInstanceOf('\\core\\task\\asynchronous_copy_task', $task);
$this->assertEquals($result, (array)$task->get_custom_data());
$this->assertFalse($task->is_blocking());
\core\task\manager::adhoc_task_complete($task);
}
/**
* Test getting the current copies.
*/
public function test_get_copies() {
global $USER;
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'foo';
$formdata->shortname = 'bar';
$formdata->category = 1;
$formdata->visible = 1;
$formdata->startdate = 1582376400;
$formdata->enddate = 0;
$formdata->idnumber = '';
$formdata->userdata = 1;
$formdata->role_1 = 1;
$formdata->role_3 = 3;
$formdata->role_5 = 5;
$formdata2 = clone($formdata);
$formdata2->shortname = 'tree';
// Create some copies.
$copydata = \copy_helper::process_formdata($formdata);
$result = \copy_helper::create_copy($copydata);
// Backup, awaiting.
$copies = \copy_helper::get_copies($USER->id);
$this->assertEquals($result['backupid'], $copies[0]->backupid);
$this->assertEquals($result['restoreid'], $copies[0]->restoreid);
$this->assertEquals(\backup::STATUS_AWAITING, $copies[0]->status);
$this->assertEquals(\backup::OPERATION_BACKUP, $copies[0]->operation);
$bc = \backup_controller::load_controller($result['backupid']);
// Backup, in progress.
$bc->set_status(\backup::STATUS_EXECUTING);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEquals($result['backupid'], $copies[0]->backupid);
$this->assertEquals($result['restoreid'], $copies[0]->restoreid);
$this->assertEquals(\backup::STATUS_EXECUTING, $copies[0]->status);
$this->assertEquals(\backup::OPERATION_BACKUP, $copies[0]->operation);
// Restore, ready to process.
$bc->set_status(\backup::STATUS_FINISHED_OK);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEquals(null, $copies[0]->backupid);
$this->assertEquals($result['restoreid'], $copies[0]->restoreid);
$this->assertEquals(\backup::STATUS_REQUIRE_CONV, $copies[0]->status);
$this->assertEquals(\backup::OPERATION_RESTORE, $copies[0]->operation);
// No records.
$bc->set_status(\backup::STATUS_FINISHED_ERR);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEmpty($copies);
$copydata2 = \copy_helper::process_formdata($formdata2);
$result2 = \copy_helper::create_copy($copydata2);
// Set the second copy to be complete.
$bc = \backup_controller::load_controller($result2['backupid']);
$bc->set_status(\backup::STATUS_FINISHED_OK);
// Set the restore to be finished.
$rc = \backup_controller::load_controller($result2['restoreid']);
$rc->set_status(\backup::STATUS_FINISHED_OK);
// No records.
$copies = \copy_helper::get_copies($USER->id);
$this->assertEmpty($copies);
}
/**
* Test getting the current copies for specific course.
*/
public function test_get_copies_course() {
global $USER;
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'foo';
$formdata->shortname = 'bar';
$formdata->category = 1;
$formdata->visible = 1;
$formdata->startdate = 1582376400;
$formdata->enddate = 0;
$formdata->idnumber = '';
$formdata->userdata = 1;
$formdata->role_1 = 1;
$formdata->role_3 = 3;
$formdata->role_5 = 5;
// Create some copies.
$copydata = \copy_helper::process_formdata($formdata);
\copy_helper::create_copy($copydata);
// No copies match this course id.
$copies = \copy_helper::get_copies($USER->id, ($this->course->id + 1));
$this->assertEmpty($copies);
}
/**
* Test getting the current copies if course has been deleted.
*/
public function test_get_copies_course_deleted() {
global $USER;
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'foo';
$formdata->shortname = 'bar';
$formdata->category = 1;
$formdata->visible = 1;
$formdata->startdate = 1582376400;
$formdata->enddate = 0;
$formdata->idnumber = '';
$formdata->userdata = 1;
$formdata->role_1 = 1;
$formdata->role_3 = 3;
$formdata->role_5 = 5;
// Create some copies.
$copydata = \copy_helper::process_formdata($formdata);
\copy_helper::create_copy($copydata);
delete_course($this->course->id, false);
// No copies match this course id as it has been deleted.
$copies = \copy_helper::get_copies($USER->id, ($this->course->id));
$this->assertEmpty($copies);
}
/*
* Test course copy.
*/
public function test_course_copy() {
global $DB;
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'copy course';
$formdata->shortname = 'copy course short';
$formdata->category = 1;
$formdata->visible = 0;
$formdata->startdate = 1582376400;
$formdata->enddate = 1582386400;
$formdata->idnumber = 123;
$formdata->userdata = 1;
$formdata->role_1 = 1;
$formdata->role_3 = 3;
$formdata->role_5 = 5;
// Create the course copy records and associated ad-hoc task.
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);
$courseid = $this->course->id;
// We are expecting trace output during this test.
$this->expectOutputRegex("/$courseid/");
// Execute adhoc task.
$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);
$postbackuprec = $DB->get_record('backup_controllers', array('backupid' => $copyids['backupid']));
$postrestorerec = $DB->get_record('backup_controllers', array('backupid' => $copyids['restoreid']));
// Check backup was completed successfully.
$this->assertEquals(backup::STATUS_FINISHED_OK, $postbackuprec->status);
$this->assertEquals(1.0, $postbackuprec->progress);
// Check restore was completed successfully.
$this->assertEquals(backup::STATUS_FINISHED_OK, $postrestorerec->status);
$this->assertEquals(1.0, $postrestorerec->progress);
// Check the restored course itself.
$coursecontext = \context_course::instance($postrestorerec->itemid);
$users = get_enrolled_users($coursecontext);
$modinfo = get_fast_modinfo($postrestorerec->itemid);
$forums = $modinfo->get_instances_of('forum');
$forum = reset($forums);
$discussions = forum_get_discussions($forum);
$course = $modinfo->get_course();
$this->assertEquals($formdata->startdate, $course->startdate);
$this->assertEquals($formdata->enddate, $course->enddate);
$this->assertEquals('copy course', $course->fullname);
$this->assertEquals('copy course short', $course->shortname);
$this->assertEquals(0, $course->visible);
$this->assertEquals(123, $course->idnumber);
foreach ($modinfo->get_cms() as $cm) {
$this->assertContains($cm->get_formatted_name(), $this->activitynames);
}
foreach ($this->courseusers as $user) {
$this->assertEquals($user, $users[$user]->id);
}
$this->assertEquals(count($this->courseusers), count($users));
$this->assertEquals(2, count($discussions));
}
/*
* Test course copy, not including any users (or data).
*/
public function test_course_copy_no_users() {
global $DB;
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'copy course';
$formdata->shortname = 'copy course short';
$formdata->category = 1;
$formdata->visible = 0;
$formdata->startdate = 1582376400;
$formdata->enddate = 1582386400;
$formdata->idnumber = 123;
$formdata->userdata = 1;
$formdata->role_1 = 0;
$formdata->role_3 = 0;
$formdata->role_5 = 0;
// Create the course copy records and associated ad-hoc task.
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);
$courseid = $this->course->id;
// We are expecting trace output during this test.
$this->expectOutputRegex("/$courseid/");
// Execute adhoc task.
$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);
$postrestorerec = $DB->get_record('backup_controllers', array('backupid' => $copyids['restoreid']));
// Check the restored course itself.
$coursecontext = \context_course::instance($postrestorerec->itemid);
$users = get_enrolled_users($coursecontext);
$modinfo = get_fast_modinfo($postrestorerec->itemid);
$forums = $modinfo->get_instances_of('forum');
$forum = reset($forums);
$discussions = forum_get_discussions($forum);
$course = $modinfo->get_course();
$this->assertEquals($formdata->startdate, $course->startdate);
$this->assertEquals($formdata->enddate, $course->enddate);
$this->assertEquals('copy course', $course->fullname);
$this->assertEquals('copy course short', $course->shortname);
$this->assertEquals(0, $course->visible);
$this->assertEquals(123, $course->idnumber);
foreach ($modinfo->get_cms() as $cm) {
$this->assertContains($cm->get_formatted_name(), $this->activitynames);
}
// Should be no discussions as the user that made them wasn't included.
$this->assertEquals(0, count($discussions));
// There should only be one user in the new course, and that's the user who did the copy.
$this->assertEquals(1, count($users));
$this->assertEquals($this->courseusers[2], $users[$this->courseusers[2]]->id);
}
/*
* Test course copy, including students and their data.
*/
public function test_course_copy_students_data() {
global $DB;
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'copy course';
$formdata->shortname = 'copy course short';
$formdata->category = 1;
$formdata->visible = 0;
$formdata->startdate = 1582376400;
$formdata->enddate = 1582386400;
$formdata->idnumber = 123;
$formdata->userdata = 1;
$formdata->role_1 = 0;
$formdata->role_3 = 0;
$formdata->role_5 = 5;
// Create the course copy records and associated ad-hoc task.
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);
$courseid = $this->course->id;
// We are expecting trace output during this test.
$this->expectOutputRegex("/$courseid/");
// Execute adhoc task.
$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);
$postrestorerec = $DB->get_record('backup_controllers', array('backupid' => $copyids['restoreid']));
// Check the restored course itself.
$coursecontext = \context_course::instance($postrestorerec->itemid);
$users = get_enrolled_users($coursecontext);
$modinfo = get_fast_modinfo($postrestorerec->itemid);
$forums = $modinfo->get_instances_of('forum');
$forum = reset($forums);
$discussions = forum_get_discussions($forum);
$course = $modinfo->get_course();
$this->assertEquals($formdata->startdate, $course->startdate);
$this->assertEquals($formdata->enddate, $course->enddate);
$this->assertEquals('copy course', $course->fullname);
$this->assertEquals('copy course short', $course->shortname);
$this->assertEquals(0, $course->visible);
$this->assertEquals(123, $course->idnumber);
foreach ($modinfo->get_cms() as $cm) {
$this->assertContains($cm->get_formatted_name(), $this->activitynames);
}
// Should be no discussions as the user that made them wasn't included.
$this->assertEquals(2, count($discussions));
// There should only be two users in the new course. The copier and one student.
$this->assertEquals(2, count($users));
$this->assertEquals($this->courseusers[2], $users[$this->courseusers[2]]->id);
$this->assertEquals($this->courseusers[0], $users[$this->courseusers[0]]->id);
}
/*
* Test course copy, not including any users (or data).
*/
public function test_course_copy_no_data() {
global $DB;
// Mock up the form data.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'copy course';
$formdata->shortname = 'copy course short';
$formdata->category = 1;
$formdata->visible = 0;
$formdata->startdate = 1582376400;
$formdata->enddate = 1582386400;
$formdata->idnumber = 123;
$formdata->userdata = 0;
$formdata->role_1 = 1;
$formdata->role_3 = 3;
$formdata->role_5 = 5;
// Create the course copy records and associated ad-hoc task.
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);
$courseid = $this->course->id;
// We are expecting trace output during this test.
$this->expectOutputRegex("/$courseid/");
// Execute adhoc task.
$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);
$postrestorerec = $DB->get_record('backup_controllers', array('backupid' => $copyids['restoreid']));
// Check the restored course itself.
$coursecontext = \context_course::instance($postrestorerec->itemid);
$users = get_enrolled_users($coursecontext);
get_fast_modinfo($postrestorerec->itemid, 0, true);
$modinfo = get_fast_modinfo($postrestorerec->itemid);
$forums = $modinfo->get_instances_of('forum');
$forum = reset($forums);
$discussions = forum_get_discussions($forum);
$course = $modinfo->get_course();
$this->assertEquals($formdata->startdate, $course->startdate);
$this->assertEquals($formdata->enddate, $course->enddate);
$this->assertEquals('copy course', $course->fullname);
$this->assertEquals('copy course short', $course->shortname);
$this->assertEquals(0, $course->visible);
$this->assertEquals(123, $course->idnumber);
foreach ($modinfo->get_cms() as $cm) {
$this->assertContains($cm->get_formatted_name(), $this->activitynames);
}
// Should be no discussions as the user data wasn't included.
$this->assertEquals(0, count($discussions));
// There should only be all users in the new course.
$this->assertEquals(count($this->courseusers), count($users));
}
/*
* Test instantiation with incomplete formdata.
*/
public function test_malformed_instantiation() {
// Mock up the form data, missing things so we get an exception.
$formdata = new \stdClass;
$formdata->courseid = $this->course->id;
$formdata->fullname = 'copy course';
$formdata->shortname = 'copy course short';
$formdata->category = 1;
// Expect and exception as form data is incomplete.
$this->expectException(\moodle_exception::class);
$copydata = \copy_helper::process_formdata($formdata);
\copy_helper::create_copy($copydata);
}
}