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 |
|
|
|
21 |
/**
|
|
|
22 |
* Tests for Moodle 2 restore steplib classes.
|
|
|
23 |
*
|
|
|
24 |
* @package core_backup
|
|
|
25 |
* @copyright 2023 Ferran Recio <ferran@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class restore_stepslib_test extends \advanced_testcase {
|
|
|
29 |
/**
|
|
|
30 |
* Setup to include all libraries.
|
|
|
31 |
*/
|
|
|
32 |
public static function setUpBeforeClass(): void {
|
|
|
33 |
global $CFG;
|
|
|
34 |
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
|
|
|
35 |
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
|
|
|
36 |
require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Makes a backup of the course.
|
|
|
41 |
*
|
|
|
42 |
* @param \stdClass $course The course object.
|
|
|
43 |
* @return string Unique identifier for this backup.
|
|
|
44 |
*/
|
|
|
45 |
protected function backup_course(\stdClass $course): string {
|
|
|
46 |
global $CFG, $USER;
|
|
|
47 |
|
|
|
48 |
// Turn off file logging, otherwise it can't delete the file (Windows).
|
|
|
49 |
$CFG->backup_file_logger_level = backup::LOG_NONE;
|
|
|
50 |
|
|
|
51 |
// Do backup with default settings. MODE_IMPORT means it will just
|
|
|
52 |
// create the directory and not zip it.
|
|
|
53 |
$bc = new \backup_controller(
|
|
|
54 |
backup::TYPE_1COURSE,
|
|
|
55 |
$course->id,
|
|
|
56 |
backup::FORMAT_MOODLE,
|
|
|
57 |
backup::INTERACTIVE_NO,
|
|
|
58 |
backup::MODE_IMPORT,
|
|
|
59 |
$USER->id
|
|
|
60 |
);
|
|
|
61 |
$backupid = $bc->get_backupid();
|
|
|
62 |
$bc->execute_plan();
|
|
|
63 |
$bc->destroy();
|
|
|
64 |
|
|
|
65 |
return $backupid;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Restores a backup that has been made earlier.
|
|
|
70 |
*
|
|
|
71 |
* @param string $backupid The unique identifier of the backup.
|
|
|
72 |
* @return int The new course id.
|
|
|
73 |
*/
|
|
|
74 |
protected function restore_replacing_content(string $backupid): int {
|
|
|
75 |
global $CFG, $USER;
|
|
|
76 |
|
|
|
77 |
// Create course to restore into, and a user to do the restore.
|
|
|
78 |
$generator = $this->getDataGenerator();
|
|
|
79 |
$course = $generator->create_course();
|
|
|
80 |
|
|
|
81 |
// Turn off file logging, otherwise it can't delete the file (Windows).
|
|
|
82 |
$CFG->backup_file_logger_level = backup::LOG_NONE;
|
|
|
83 |
|
|
|
84 |
// Do restore to new course with default settings.
|
|
|
85 |
$rc = new \restore_controller(
|
|
|
86 |
$backupid,
|
|
|
87 |
$course->id,
|
|
|
88 |
backup::INTERACTIVE_NO,
|
|
|
89 |
backup::MODE_GENERAL,
|
|
|
90 |
$USER->id,
|
|
|
91 |
backup::TARGET_EXISTING_DELETING
|
|
|
92 |
);
|
|
|
93 |
|
|
|
94 |
$precheck = $rc->execute_precheck();
|
|
|
95 |
$this->assertTrue($precheck);
|
|
|
96 |
$rc->get_plan()->get_setting('role_assignments')->set_value(true);
|
|
|
97 |
$rc->get_plan()->get_setting('permissions')->set_value(true);
|
|
|
98 |
$rc->execute_plan();
|
|
|
99 |
$rc->destroy();
|
|
|
100 |
|
|
|
101 |
return $course->id;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Test for the section structure step included elements.
|
|
|
106 |
*
|
|
|
107 |
* @covers \restore_section_structure_step::process_section
|
|
|
108 |
*/
|
|
|
109 |
public function test_restore_section_structure_step(): void {
|
|
|
110 |
global $DB;
|
|
|
111 |
|
|
|
112 |
$this->resetAfterTest();
|
|
|
113 |
$this->setAdminUser();
|
|
|
114 |
|
|
|
115 |
$course = $this->getDataGenerator()->create_course(['numsections' => 2, 'format' => 'topics']);
|
|
|
116 |
|
|
|
117 |
$backupid = $this->backup_course($course);
|
|
|
118 |
$newcourseid = $this->restore_replacing_content($backupid);
|
|
|
119 |
|
|
|
120 |
$originalsections = get_fast_modinfo($course->id)->get_section_info_all();
|
|
|
121 |
$restoredsections = get_fast_modinfo($newcourseid)->get_section_info_all();
|
|
|
122 |
|
|
|
123 |
$this->assertEquals(count($originalsections), count($restoredsections));
|
|
|
124 |
|
|
|
125 |
$validatefields = ['name', 'summary', 'summaryformat', 'visible', 'component', 'itemid'];
|
|
|
126 |
|
|
|
127 |
foreach ($validatefields as $field) {
|
|
|
128 |
$this->assertEquals($originalsections[1]->$field, $restoredsections[1]->$field);
|
|
|
129 |
$this->assertEquals($originalsections[2]->$field, $restoredsections[2]->$field);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
}
|
|
|
133 |
}
|