Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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;
1441 ariadna 20
use core\di;
21
use core\hook\manager;
22
use restore_controller;
1 efrain 23
 
24
/**
25
 * Tests for Moodle 2 restore steplib classes.
26
 *
27
 * @package core_backup
28
 * @copyright 2023 Ferran Recio <ferran@moodle.com>
29
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
final class restore_stepslib_test extends \advanced_testcase {
1 efrain 32
    /**
33
     * Setup to include all libraries.
34
     */
35
    public static function setUpBeforeClass(): void {
36
        global $CFG;
37
        require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
38
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
39
        require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
1441 ariadna 40
        parent::setUpBeforeClass();
1 efrain 41
    }
42
 
43
    /**
44
     * Makes a backup of the course.
45
     *
46
     * @param \stdClass $course The course object.
47
     * @return string Unique identifier for this backup.
48
     */
49
    protected function backup_course(\stdClass $course): string {
50
        global $CFG, $USER;
51
 
52
        // Turn off file logging, otherwise it can't delete the file (Windows).
53
        $CFG->backup_file_logger_level = backup::LOG_NONE;
54
 
55
        // Do backup with default settings. MODE_IMPORT means it will just
56
        // create the directory and not zip it.
57
        $bc = new \backup_controller(
58
            backup::TYPE_1COURSE,
59
            $course->id,
60
            backup::FORMAT_MOODLE,
61
            backup::INTERACTIVE_NO,
62
            backup::MODE_IMPORT,
63
            $USER->id
64
        );
65
        $backupid = $bc->get_backupid();
66
        $bc->execute_plan();
67
        $bc->destroy();
68
 
69
        return $backupid;
70
    }
71
 
72
    /**
73
     * Restores a backup that has been made earlier.
74
     *
75
     * @param string $backupid The unique identifier of the backup.
76
     * @return int The new course id.
77
     */
78
    protected function restore_replacing_content(string $backupid): int {
79
        global $CFG, $USER;
80
 
81
        // Create course to restore into, and a user to do the restore.
82
        $generator = $this->getDataGenerator();
83
        $course = $generator->create_course();
84
 
85
        // Turn off file logging, otherwise it can't delete the file (Windows).
86
        $CFG->backup_file_logger_level = backup::LOG_NONE;
87
 
88
        // Do restore to new course with default settings.
89
        $rc = new \restore_controller(
90
            $backupid,
91
            $course->id,
92
            backup::INTERACTIVE_NO,
93
            backup::MODE_GENERAL,
94
            $USER->id,
95
            backup::TARGET_EXISTING_DELETING
96
        );
97
 
98
        $precheck = $rc->execute_precheck();
99
        $this->assertTrue($precheck);
100
        $rc->get_plan()->get_setting('role_assignments')->set_value(true);
101
        $rc->get_plan()->get_setting('permissions')->set_value(true);
102
        $rc->execute_plan();
103
        $rc->destroy();
104
 
105
        return $course->id;
106
    }
107
 
108
    /**
1441 ariadna 109
     * Test for delegate section behaviour.
1 efrain 110
     *
111
     * @covers \restore_section_structure_step::process_section
112
     */
113
    public function test_restore_section_structure_step(): void {
114
        global $DB;
115
 
116
        $this->resetAfterTest();
117
        $this->setAdminUser();
118
 
119
        $course = $this->getDataGenerator()->create_course(['numsections' => 2, 'format' => 'topics']);
1441 ariadna 120
        // Section 2 has an existing delegate class for component that is not an activity.
121
        course_update_section(
122
            $course,
123
            $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]),
124
            [
125
                'component' => 'test_component',
126
                'itemid' => 1,
127
            ]
128
        );
1 efrain 129
 
130
        $backupid = $this->backup_course($course);
131
        $newcourseid = $this->restore_replacing_content($backupid);
132
 
133
        $originalsections = get_fast_modinfo($course->id)->get_section_info_all();
134
        $restoredsections = get_fast_modinfo($newcourseid)->get_section_info_all();
135
 
1441 ariadna 136
        // Delegated sections depends on the plugin to be backuped and restored.
137
        // In this case, the plugin is not backuped and restored, so the section is not restored.
138
        $this->assertEquals(3, count($originalsections));
139
        $this->assertEquals(2, count($restoredsections));
1 efrain 140
 
141
        $validatefields = ['name', 'summary', 'summaryformat', 'visible', 'component', 'itemid'];
142
 
1441 ariadna 143
        $this->assertEquals($originalsections[1]->name, $restoredsections[1]->name);
144
 
1 efrain 145
        foreach ($validatefields as $field) {
1441 ariadna 146
            $this->assertEquals($originalsections[0]->$field, $restoredsections[0]->$field);
1 efrain 147
            $this->assertEquals($originalsections[1]->$field, $restoredsections[1]->$field);
148
        }
1441 ariadna 149
    }
1 efrain 150
 
1441 ariadna 151
    /**
152
     * Tests the hooks for restore task  settings definition.
153
     *
154
     * @covers \restore_root_task::define_settings
155
     */
156
    public function test_restore_hook(): void {
157
        // Load the callback classes.
158
        require_once(__DIR__ . '/fixtures/restore_task_hooks.php');
159
 
160
        $this->resetAfterTest();
161
        $this->setAdminUser();
162
 
163
        // Replace the version of the manager in the DI container with a phpunit one.
164
        di::set(
165
            manager::class,
166
            manager::phpunit_get_instance([
167
                // Load a list of hooks for `test_plugin1` from the fixture file.
168
                'test_plugin1' => __DIR__ .
169
                    '/fixtures/restore_task_hooks.php',
170
            ]),
171
        );
172
 
173
        global $CFG, $USER;
174
 
175
        // Create course to restore into, and a user to do the restore.
176
        $generator = $this->getDataGenerator();
177
        $course = $generator->create_course();
178
 
179
        $backupid = $this->backup_course($course);
180
 
181
        // Turn off file logging, otherwise it can't delete the file (Windows).
182
        $CFG->backup_file_logger_level = backup::LOG_NONE;
183
 
184
        $course = $generator->create_course();
185
 
186
        // Do restore to new course with default settings.
187
        $rc = new restore_controller(
188
            $backupid,
189
            $course->id,
190
            backup::INTERACTIVE_NO,
191
            backup::MODE_GENERAL,
192
            $USER->id,
193
            backup::TARGET_EXISTING_DELETING
194
        );
195
 
196
        $precheck = $rc->execute_precheck();
197
        $this->assertTrue($precheck);
198
        $setting = $rc->get_plan()->get_setting('extra_test');
199
        $this->assertNotEmpty($setting);
200
        $rc->execute_plan();
201
        $rc->destroy();
1 efrain 202
    }
203
}