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
/**
18
 * @package   core_backup
19
 * @category  test
20
 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
namespace core_backup;
25
 
26
use backup;
27
use backup_controller;
28
use restore_controller;
29
 
30
defined('MOODLE_INTERNAL') || die();
31
 
32
// Include all the needed stuff.
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
 
37
/**
38
 * Tests for the backup and restore controller classes.
39
 *
40
 * @package   core_backup
41
 * @category  test
42
 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
43
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 */
1441 ariadna 45
final class controller_test extends \advanced_testcase {
1 efrain 46
 
47
    protected $moduleid;  // course_modules id used for testing
48
    protected $sectionid; // course_sections id used for testing
49
    protected $courseid;  // course id used for testing
50
    protected $userid;    // user used if for testing
51
 
52
    protected function setUp(): void {
53
        global $DB, $CFG;
1441 ariadna 54
        parent::setUp();
1 efrain 55
 
56
        $this->resetAfterTest(true);
57
 
58
        $course = $this->getDataGenerator()->create_course();
59
        $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
60
        $coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));
61
 
62
        $this->moduleid  = $coursemodule->id;
63
        $this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
64
        $this->courseid  = $coursemodule->course;
65
        $this->userid = 2; // admin
66
 
67
        // Disable all loggers
68
        $CFG->backup_error_log_logger_level = backup::LOG_NONE;
69
        $CFG->backup_output_indented_logger_level = backup::LOG_NONE;
70
        $CFG->backup_file_logger_level = backup::LOG_NONE;
71
        $CFG->backup_database_logger_level = backup::LOG_NONE;
72
        $CFG->backup_file_logger_level_extra = backup::LOG_NONE;
73
    }
74
 
75
    /**
76
     * Test get_copy
77
     *
78
     * @covers \restore_controller::get_copy
79
     */
11 efrain 80
    public function test_restore_controller_get_copy(): void {
1 efrain 81
        $copydata = (object)["some" => "copydata"];
82
        $rc = new \restore_controller(1729, $this->courseid, backup::INTERACTIVE_NO, backup::MODE_COPY,
83
                $this->userid, backup::TARGET_NEW_COURSE, null, backup::RELEASESESSION_NO, $copydata);
84
 
85
        $this->assertEquals($copydata, $rc->get_copy());
86
    }
87
 
88
    /**
89
     * Test instantiating a restore controller for a course copy without providing copy data.
90
     *
91
     * @covers \restore_controller::__construct
92
     */
11 efrain 93
    public function test_restore_controller_copy_without_copydata(): void {
1 efrain 94
        $this->expectException(\restore_controller_exception::class);
95
 
96
        new \restore_controller(1729, $this->courseid, backup::INTERACTIVE_NO, backup::MODE_COPY,
97
                $this->userid, backup::TARGET_NEW_COURSE);
98
    }
99
 
100
    /*
101
     * test base_setting class
102
     */
11 efrain 103
    public function test_backup_controller(): void {
1 efrain 104
        // Instantiate non interactive backup_controller
105
        $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
106
            backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
107
        $this->assertTrue($bc instanceof backup_controller);
108
        $this->assertEquals($bc->get_status(), backup::STATUS_AWAITING);
109
        // Instantiate interactive backup_controller
110
        $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
111
            backup::INTERACTIVE_YES, backup::MODE_GENERAL, $this->userid);
112
        $this->assertTrue($bc instanceof backup_controller);
113
        $this->assertEquals($bc->get_status(), backup::STATUS_SETTING_UI);
114
        $this->assertEquals(strlen($bc->get_backupid()), 32); // is one md5
115
 
116
        // Save and load one backup controller to check everything is in place
117
        $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
118
            backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
119
        $recid = $bc->save_controller();
120
        $newbc = mock_backup_controller::load_controller($bc->get_backupid());
121
        $this->assertTrue($newbc instanceof backup_controller); // This means checksum and load worked ok
122
    }
123
 
11 efrain 124
    public function test_backup_controller_include_files(): void {
1 efrain 125
        // A MODE_GENERAL controller - this should include files
126
        $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
127
            backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid);
128
        $this->assertEquals($bc->get_include_files(), 1);
129
 
130
 
131
        // The MODE_IMPORT and MODE_SAMESITE should not include files in the backup.
132
        // A MODE_IMPORT controller
133
        $bc = new mock_backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
134
            backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
135
        $this->assertEquals($bc->get_include_files(), 0);
136
 
137
        // A MODE_SAMESITE controller
138
        $bc = new mock_backup_controller(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
139
            backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
140
        $this->assertEquals($bc->get_include_files(), 0);
141
    }
142
 
143
    /**
144
     * Test set kept roles method.
145
     */
11 efrain 146
    public function test_backup_controller_set_kept_roles(): void {
1 efrain 147
        $this->expectException(\backup_controller_exception::class);
148
 
149
        // Set up controller as a non-copy operation.
150
        $bc = new \backup_controller(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
151
            backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid, backup::RELEASESESSION_YES);
152
 
153
        $bc->set_kept_roles(array(1, 3, 5));
154
    }
155
 
156
    /**
157
     * Tests the restore_controller.
158
     */
11 efrain 159
    public function test_restore_controller_is_executing(): void {
1 efrain 160
        global $CFG;
161
 
162
        // Make a backup.
163
        make_backup_temp_directory('');
164
        $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
165
            backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
166
        $backupid = $bc->get_backupid();
167
        $bc->execute_plan();
168
        $bc->destroy();
169
 
170
        // The progress class will get called during restore, so we can use that
171
        // to check the executing flag is true.
172
        $progress = new core_backup_progress_restore_is_executing();
173
 
174
        // Set up restore.
175
        $rc = new restore_controller($backupid, $this->courseid,
176
                backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid,
177
                backup::TARGET_EXISTING_ADDING);
178
        $this->assertTrue($rc->execute_precheck());
179
 
180
        // Check restore is NOT executing.
181
        $this->assertFalse(restore_controller::is_executing());
182
 
183
        // Execute restore.
184
        $rc->set_progress($progress);
185
        $rc->execute_plan();
186
 
187
        // Check restore is NOT executing afterward either.
188
        $this->assertFalse(restore_controller::is_executing());
189
        $rc->destroy();
190
 
191
        // During restore, check that executing was true.
192
        $this->assertTrue(count($progress->executing) > 0);
193
        $alltrue = true;
194
        foreach ($progress->executing as $executing) {
195
            if (!$executing) {
196
                $alltrue = false;
197
                break;
198
            }
199
        }
200
        $this->assertTrue($alltrue);
201
    }
202
 
203
    /**
204
     * Test prepare copy method.
205
     */
11 efrain 206
    public function test_restore_controller_prepare_copy(): void {
1 efrain 207
        $this->expectException(\restore_controller_exception::class);
208
 
209
        global $CFG;
210
 
211
        // Make a backup.
212
        make_backup_temp_directory('');
213
        $bc = new backup_controller(backup::TYPE_1ACTIVITY, $this->moduleid, backup::FORMAT_MOODLE,
214
            backup::INTERACTIVE_NO, backup::MODE_IMPORT, $this->userid);
215
        $backupid = $bc->get_backupid();
216
        $bc->execute_plan();
217
        $bc->destroy();
218
 
219
        // Set up restore.
220
        $rc = new restore_controller($backupid, $this->courseid,
221
            backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid,
222
            backup::TARGET_EXISTING_ADDING);
223
        $rc->prepare_copy();
224
    }
225
 
226
    /**
227
     * Test restore of deadlock causing backup.
228
     */
11 efrain 229
    public function test_restore_of_deadlock_causing_backup(): void {
1 efrain 230
        global $USER, $CFG;
231
        $this->preventResetByRollback();
232
 
233
        $foldername = 'deadlock';
234
        $fp = get_file_packer('application/vnd.moodle.backup');
235
        $tempdir = make_backup_temp_directory($foldername);
236
        $files = $fp->extract_to_pathname($CFG->dirroot . '/backup/controller/tests/fixtures/deadlock.mbz', $tempdir);
237
 
238
        $this->setAdminUser();
239
        $controller = new restore_controller(
240
            'deadlock',
241
            $this->courseid,
242
            backup::INTERACTIVE_NO,
243
            backup::MODE_GENERAL,
244
            $USER->id,
245
            backup::TARGET_NEW_COURSE
246
        );
247
        $this->assertTrue($controller->execute_precheck());
248
        $controller->execute_plan();
249
        $controller->destroy();
250
    }
251
}
252
 
253
 
254
/**
255
 * Progress class that records the result of restore_controller::is_executing calls.
256
 *
257
 * @package core_backup
258
 * @copyright 2014 The Open University
259
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
260
 */
261
class core_backup_progress_restore_is_executing extends \core\progress\base {
262
    /** @var array Array of results from calling function */
263
    public $executing = array();
264
 
265
    public function update_progress() {
266
        $this->executing[] = restore_controller::is_executing();
267
    }
268
}
269
 
270
 
271
/*
272
 * helper extended @backup_controller class that makes some methods public for testing
273
 */
274
class mock_backup_controller extends backup_controller {
275
 
276
    public function save_controller($includeobj = true, $cleanobj = false) {
277
        parent::save_controller($includeobj, $cleanobj);
278
    }
279
}