Proyectos de Subversion Moodle

Rev

Rev 1 | | 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;
20
use backup_controller;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
26
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
27
require_once($CFG->libdir . '/completionlib.php');
28
 
29
/**
30
 * Asyncronhous backup tests.
31
 *
32
 * @package    core_backup
33
 * @copyright  2018 Matt Porritt <mattp@catalyst-au.net>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class async_backup_test extends \advanced_testcase {
37
 
38
    /**
39
     * Tests the asynchronous backup.
40
     */
11 efrain 41
    public function test_async_backup(): void {
1 efrain 42
        global $CFG, $DB, $USER;
43
 
44
        $this->resetAfterTest(true);
45
        $this->setAdminUser();
46
        $CFG->enableavailability = true;
47
        $CFG->enablecompletion = true;
48
 
49
        // Create a course with some availability data set.
50
        $generator = $this->getDataGenerator();
51
        $course = $generator->create_course(
52
                array('format' => 'topics', 'numsections' => 3,
53
                        'enablecompletion' => COMPLETION_ENABLED),
54
                array('createsections' => true));
55
        $forum = $generator->create_module('forum', array(
56
                'course' => $course->id));
57
        $forum2 = $generator->create_module('forum', array(
58
                'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
59
 
60
        // Create a teacher user to call the backup.
61
        $teacher = $generator->create_user();
62
        $generator->enrol_user($teacher->id, $course->id, 'editingteacher');
63
 
64
        // We need a grade, easiest is to add an assignment.
65
        $assignrow = $generator->create_module('assign', array(
66
                'course' => $course->id));
67
        $assign = new \assign(\context_module::instance($assignrow->cmid), false, false);
68
        $item = $assign->get_grade_item();
69
 
70
        // Make a test grouping as well.
71
        $grouping = $generator->create_grouping(array('courseid' => $course->id,
72
                'name' => 'Grouping!'));
73
 
74
        $availability = '{"op":"|","show":false,"c":[' .
75
                '{"type":"completion","cm":' . $forum2->cmid .',"e":1},' .
76
                '{"type":"grade","id":' . $item->id . ',"min":4,"max":94},' .
77
                '{"type":"grouping","id":' . $grouping->id . '}' .
78
                ']}';
79
        $DB->set_field('course_modules', 'availability', $availability, array(
80
                'id' => $forum->cmid));
81
        $DB->set_field('course_sections', 'availability', $availability, array(
82
                'course' => $course->id, 'section' => 1));
83
 
84
        // Enable logging.
85
        $this->preventResetByRollback();
86
        set_config('enabled_stores', 'logstore_standard', 'tool_log');
87
        set_config('buffersize', 0, 'logstore_standard');
88
        get_log_manager(true);
89
 
90
        // Case 1: Make a course backup without users.
91
        $this->setUser($teacher->id);
92
 
93
        // Make the backup controller for an async backup.
94
        $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
95
                backup::INTERACTIVE_YES, backup::MODE_ASYNC, $USER->id);
96
        $bc->finish_ui();
97
        $backupid = $bc->get_backupid();
98
        $bc->destroy();
99
 
100
        $prebackuprec = $DB->get_record('backup_controllers', array('backupid' => $backupid));
101
 
102
        // Check the initial backup controller was created correctly.
103
        $this->assertEquals(backup::STATUS_AWAITING, $prebackuprec->status);
104
        $this->assertEquals(2, $prebackuprec->execution);
105
 
106
        // Create the adhoc task.
107
        $asynctask = new \core\task\asynchronous_backup_task();
108
        $asynctask->set_custom_data(['backupid' => $backupid]);
109
        $asynctask->set_userid($USER->id);
110
        \core\task\manager::queue_adhoc_task($asynctask);
111
 
112
        // We are expecting trace output during this test.
113
        $this->expectOutputRegex("/$backupid/");
114
 
115
        // Execute adhoc task.
116
        $now = time();
117
        $task = \core\task\manager::get_next_adhoc_task($now);
118
        $this->assertInstanceOf('\\core\\task\\asynchronous_backup_task', $task);
119
        $task->execute();
120
        \core\task\manager::adhoc_task_complete($task);
121
 
122
        $postbackuprec = $DB->get_record('backup_controllers', ['backupid' => $backupid]);
123
 
124
        // Check backup was created successfully.
125
        $this->assertEquals(backup::STATUS_FINISHED_OK, $postbackuprec->status);
126
        $this->assertEquals(1.0, $postbackuprec->progress);
127
        $this->assertEquals($teacher->id, $postbackuprec->userid);
128
 
129
        // Check that the backupid was logged correctly.
130
        $logrec = $DB->get_record('logstore_standard_log', ['userid' => $postbackuprec->userid,
131
                'target' => 'course_backup'], '*', MUST_EXIST);
132
        $otherdata = json_decode($logrec->other);
133
        $this->assertEquals($backupid, $otherdata->backupid);
134
 
135
        // Check backup was stored in correct area.
136
        $usercontextid = $DB->get_field('context', 'id', ['contextlevel' => CONTEXT_USER, 'instanceid' => $teacher->id]);
137
        $this->assertEquals(1, $DB->count_records('files', ['contextid' => $usercontextid,
138
                'component' => 'user', 'filearea' => 'backup', 'filename' => 'backup.mbz']));
139
 
140
        // Case 2: Make a second backup with users and not anonymised.
141
        $this->setAdminUser();
142
        $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
143
                backup::INTERACTIVE_YES, backup::MODE_ASYNC, $USER->id);
144
        $bc->get_plan()->get_setting('users')->set_status(\backup_setting::NOT_LOCKED);
145
        $bc->get_plan()->get_setting('users')->set_value(true);
146
        $bc->get_plan()->get_setting('anonymize')->set_value(false);
147
        $bc->finish_ui();
148
        $backupid = $bc->get_backupid();
149
        $bc->destroy();
150
 
151
        // Create the adhoc task.
152
        $asynctask = new \core\task\asynchronous_backup_task();
153
        $asynctask->set_custom_data(['backupid' => $backupid]);
154
        \core\task\manager::queue_adhoc_task($asynctask);
155
 
156
        // Execute adhoc task.
157
        $now = time();
158
        $task = \core\task\manager::get_next_adhoc_task($now);
159
        $task->execute();
160
        \core\task\manager::adhoc_task_complete($task);
161
 
162
        $postbackuprec = $DB->get_record('backup_controllers', ['backupid' => $backupid]);
163
 
164
        // Check backup was created successfully.
165
        $this->assertEquals(backup::STATUS_FINISHED_OK, $postbackuprec->status);
166
        $this->assertEquals(1.0, $postbackuprec->progress);
167
        $this->assertEquals($USER->id, $postbackuprec->userid);
168
 
169
        // Check that the backupid was logged correctly.
170
        $logrec = $DB->get_record('logstore_standard_log', ['userid' => $postbackuprec->userid,
171
                'target' => 'course_backup'], '*', MUST_EXIST);
172
        $otherdata = json_decode($logrec->other);
173
        $this->assertEquals($backupid, $otherdata->backupid);
174
 
175
        // Check backup was stored in correct area.
176
        $coursecontextid = $DB->get_field('context', 'id', ['contextlevel' => CONTEXT_COURSE, 'instanceid' => $course->id]);
177
        $this->assertEquals(1, $DB->count_records('files', ['contextid' => $coursecontextid,
178
                'component' => 'backup', 'filearea' => 'course', 'filename' => 'backup.mbz']));
179
    }
180
 
181
    /**
182
     * Tests the asynchronous backup will resolve in duplicate cases.
183
     */
11 efrain 184
    public function test_complete_async_backup(): void {
1 efrain 185
        global $CFG, $DB, $USER;
186
 
187
        $this->resetAfterTest(true);
188
        $this->setAdminUser();
189
        $CFG->enableavailability = true;
190
        $CFG->enablecompletion = true;
191
 
192
        // Create a course with some availability data set.
193
        $generator = $this->getDataGenerator();
194
        $course = $generator->create_course(
195
                array('format' => 'topics', 'numsections' => 3,
196
                        'enablecompletion' => COMPLETION_ENABLED),
197
                array('createsections' => true));
198
        $forum = $generator->create_module('forum', array(
199
                'course' => $course->id));
200
        $forum2 = $generator->create_module('forum', array(
201
                'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
202
 
203
        // We need a grade, easiest is to add an assignment.
204
        $assignrow = $generator->create_module('assign', array(
205
                'course' => $course->id));
206
        $assign = new \assign(\context_module::instance($assignrow->cmid), false, false);
207
        $item = $assign->get_grade_item();
208
 
209
        // Make a test grouping as well.
210
        $grouping = $generator->create_grouping(array('courseid' => $course->id,
211
                'name' => 'Grouping!'));
212
 
213
        $availability = '{"op":"|","show":false,"c":[' .
214
                '{"type":"completion","cm":' . $forum2->cmid .',"e":1},' .
215
                '{"type":"grade","id":' . $item->id . ',"min":4,"max":94},' .
216
                '{"type":"grouping","id":' . $grouping->id . '}' .
217
                ']}';
218
        $DB->set_field('course_modules', 'availability', $availability, array(
219
                'id' => $forum->cmid));
220
        $DB->set_field('course_sections', 'availability', $availability, array(
221
                'course' => $course->id, 'section' => 1));
222
 
223
        // Make the backup controller for an async backup.
224
        $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
225
                backup::INTERACTIVE_YES, backup::MODE_ASYNC, $USER->id);
226
        $bc->finish_ui();
227
        $backupid = $bc->get_backupid();
228
        $bc->destroy();
229
 
230
        // Now hack the record to remove the controller and set the status fields to complete.
231
        // This emulates a duplicate run for an already finished controller.
232
        $id = $DB->get_field('backup_controllers', 'id', ['backupid' => $backupid]);
233
        $data = [
234
            'id' => $id,
235
            'controller' => '',
236
            'progress' => 1.0,
237
            'status' => backup::STATUS_FINISHED_OK
238
        ];
239
        $DB->update_record('backup_controllers', $data);
240
 
241
        // Now queue an adhoc task and check it handles and completes gracefully.
242
        $asynctask = new \core\task\asynchronous_backup_task();
243
        $asynctask->set_custom_data(array('backupid' => $backupid));
244
        \core\task\manager::queue_adhoc_task($asynctask);
245
 
246
        // We are expecting a specific message output during this test.
247
        $this->expectOutputRegex('/invalid controller/');
248
 
249
        // Execute adhoc task.
250
        $now = time();
251
        $task = \core\task\manager::get_next_adhoc_task($now);
252
        $this->assertInstanceOf('\\core\\task\\asynchronous_backup_task', $task);
253
        $task->execute();
254
        \core\task\manager::adhoc_task_complete($task);
255
 
256
        // Check the task record is removed.
257
        $this->assertEquals(0, $DB->count_records('task_adhoc'));
258
    }
259
}