Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 * Automated backup tests. 19 * 20 * @package core_backup 21 * @copyright 2019 John Yao <johnyao@catalyst-au.net> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core_backup; 26 27 use backup_cron_automated_helper; 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 global $CFG; 32 require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php'); 33 require_once($CFG->libdir.'/cronlib.php'); 34 require_once($CFG->libdir . '/completionlib.php'); 35 36 /** 37 * Automated backup tests. 38 * 39 * @package core_backup 40 * @copyright 2019 John Yao <johnyao@catalyst-au.net> 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 42 */ 43 class automated_backup_test extends \advanced_testcase { 44 /** 45 * @var \backup_cron_automated_helper 46 */ 47 protected $backupcronautomatedhelper; 48 49 /** 50 * @var \stdClass $course 51 */ 52 protected $course; 53 54 protected function setUp(): void { 55 global $DB, $CFG; 56 57 $this->resetAfterTest(true); 58 $this->setAdminUser(); 59 $CFG->enableavailability = true; 60 $CFG->enablecompletion = true; 61 62 // Getting a testable backup_cron_automated_helper class. 63 $this->backupcronautomatedhelper = new test_backup_cron_automated_helper(); 64 65 $generator = $this->getDataGenerator(); 66 $this->course = $generator->create_course( 67 array('format' => 'topics', 'numsections' => 3, 68 'enablecompletion' => COMPLETION_ENABLED), 69 array('createsections' => true)); 70 $forum = $generator->create_module('forum', array( 71 'course' => $this->course->id)); 72 $forum2 = $generator->create_module('forum', array( 73 'course' => $this->course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); 74 75 // We need a grade, easiest is to add an assignment. 76 $assignrow = $generator->create_module('assign', array( 77 'course' => $this->course->id)); 78 $assign = new \assign(\context_module::instance($assignrow->cmid), false, false); 79 $item = $assign->get_grade_item(); 80 81 // Make a test grouping as well. 82 $grouping = $generator->create_grouping(array('courseid' => $this->course->id, 83 'name' => 'Grouping!')); 84 85 $availability = '{"op":"|","show":false,"c":[' . 86 '{"type":"completion","cm":' . $forum2->cmid .',"e":1},' . 87 '{"type":"grade","id":' . $item->id . ',"min":4,"max":94},' . 88 '{"type":"grouping","id":' . $grouping->id . '}' . 89 ']}'; 90 $DB->set_field('course_modules', 'availability', $availability, array( 91 'id' => $forum->cmid)); 92 $DB->set_field('course_sections', 'availability', $availability, array( 93 'course' => $this->course->id, 'section' => 1)); 94 } 95 96 /** 97 * Tests the automated backup run when the there is course backup should be skipped. 98 */ 99 public function test_automated_backup_skipped_run() { 100 global $DB; 101 102 // Enable automated back up. 103 set_config('backup_auto_active', true, 'backup'); 104 set_config('backup_auto_weekdays', '1111111', 'backup'); 105 106 // Start backup process. 107 $admin = get_admin(); 108 109 // Backup entry should not exist. 110 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $this->course->id)); 111 $this->assertFalse($backupcourse); 112 $this->assertInstanceOf( 113 backup_cron_automated_helper::class, 114 $this->backupcronautomatedhelper->return_this() 115 ); 116 117 $classobject = $this->backupcronautomatedhelper->return_this(); 118 119 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'get_courses'); 120 $method->setAccessible(true); // Allow accessing of private method. 121 $courses = $method->invoke($classobject); 122 123 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'check_and_push_automated_backups'); 124 $method->setAccessible(true); // Allow accessing of private method. 125 $emailpending = $method->invokeArgs($classobject, [$courses, $admin]); 126 127 $coursename = $this->course->fullname; 128 $this->expectOutputRegex("/Skipping $coursename \(Not scheduled for backup until/"); 129 $this->assertFalse($emailpending); 130 131 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $this->course->id)); 132 $this->assertNotNull($backupcourse->laststatus); 133 } 134 135 /** 136 * Tests the automated backup run when the there is course backup can be pushed to adhoc task. 137 */ 138 public function test_automated_backup_push_run() { 139 global $DB; 140 141 // Enable automated back up. 142 set_config('backup_auto_active', true, 'backup'); 143 set_config('backup_auto_weekdays', '1111111', 'backup'); 144 145 $admin = get_admin(); 146 147 $classobject = $this->backupcronautomatedhelper->return_this(); 148 149 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'get_courses'); 150 $method->setAccessible(true); // Allow accessing of private method. 151 $courses = $method->invoke($classobject); 152 153 // Create this backup course. 154 $backupcourse = new \stdClass; 155 $backupcourse->courseid = $this->course->id; 156 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN; 157 $DB->insert_record('backup_courses', $backupcourse); 158 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $this->course->id)); 159 160 // We now manually trigger a backup pushed to adhoc task. 161 // Make sure is in the past, which means should run now. 162 $backupcourse->nextstarttime = time() - 10; 163 $DB->update_record('backup_courses', $backupcourse); 164 165 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'check_and_push_automated_backups'); 166 $method->setAccessible(true); // Allow accessing of private method. 167 $emailpending = $method->invokeArgs($classobject, [$courses, $admin]); 168 $this->assertTrue($emailpending); 169 170 $coursename = $this->course->fullname; 171 $this->expectOutputRegex("/Putting backup of $coursename in adhoc task queue/"); 172 173 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $this->course->id)); 174 // Now this backup course status should be queued. 175 $this->assertEquals(backup_cron_automated_helper::BACKUP_STATUS_QUEUED, $backupcourse->laststatus); 176 } 177 178 /** 179 * Tests the automated backup inactive run. 180 */ 181 public function test_inactive_run() { 182 backup_cron_automated_helper::run_automated_backup(); 183 $this->expectOutputString("Checking automated backup status...INACTIVE\n"); 184 } 185 186 /** 187 * Tests the invisible course being skipped. 188 */ 189 public function test_should_skip_invisible_course() { 190 global $DB; 191 192 set_config('backup_auto_active', true, 'backup'); 193 set_config('backup_auto_skip_hidden', true, 'backup'); 194 set_config('backup_auto_weekdays', '1111111', 'backup'); 195 // Create this backup course. 196 $backupcourse = new \stdClass; 197 $backupcourse->courseid = $this->course->id; 198 // This is the status we believe last run was OK. 199 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED; 200 $DB->insert_record('backup_courses', $backupcourse); 201 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $this->course->id)); 202 203 $this->assertTrue(course_change_visibility($this->course->id, false)); 204 $course = $DB->get_record('course', array('id' => $this->course->id)); 205 $this->assertEquals('0', $course->visible); 206 $classobject = $this->backupcronautomatedhelper->return_this(); 207 $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup(null, time()); 208 209 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'should_skip_course_backup'); 210 $method->setAccessible(true); // Allow accessing of private method. 211 $skipped = $method->invokeArgs($classobject, [$backupcourse, $course, $nextstarttime]); 212 213 $this->assertTrue($skipped); 214 $this->expectOutputRegex("/Skipping $course->fullname \(Not visible\)/"); 215 } 216 217 /** 218 * Tests the not modified course being skipped. 219 */ 220 public function test_should_skip_not_modified_course_in_days() { 221 global $DB; 222 223 set_config('backup_auto_active', true, 'backup'); 224 // Skip if not modified in two days. 225 set_config('backup_auto_skip_modif_days', 2, 'backup'); 226 set_config('backup_auto_weekdays', '1111111', 'backup'); 227 228 // Create this backup course. 229 $backupcourse = new \stdClass; 230 $backupcourse->courseid = $this->course->id; 231 // This is the status we believe last run was OK. 232 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED; 233 $backupcourse->laststarttime = time() - 2 * DAYSECS; 234 $backupcourse->lastendtime = time() - 1 * DAYSECS; 235 $DB->insert_record('backup_courses', $backupcourse); 236 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $this->course->id)); 237 $course = $DB->get_record('course', array('id' => $this->course->id)); 238 239 $course->timemodified = time() - 2 * DAYSECS - 1; 240 241 $classobject = $this->backupcronautomatedhelper->return_this(); 242 $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup(null, time()); 243 244 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'should_skip_course_backup'); 245 $method->setAccessible(true); // Allow accessing of private method. 246 $skipped = $method->invokeArgs($classobject, [$backupcourse, $course, $nextstarttime]); 247 248 $this->assertTrue($skipped); 249 $this->expectOutputRegex("/Skipping $course->fullname \(Not modified in the past 2 days\)/"); 250 } 251 252 /** 253 * Tests the backup not modified course being skipped. 254 */ 255 public function test_should_skip_not_modified_course_since_prev() { 256 global $DB; 257 258 set_config('backup_auto_active', true, 'backup'); 259 // Skip if not modified in two days. 260 set_config('backup_auto_skip_modif_prev', 2, 'backup'); 261 set_config('backup_auto_weekdays', '1111111', 'backup'); 262 263 // Create this backup course. 264 $backupcourse = new \stdClass; 265 $backupcourse->courseid = $this->course->id; 266 // This is the status we believe last run was OK. 267 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED; 268 $backupcourse->laststarttime = time() - 2 * DAYSECS; 269 $backupcourse->lastendtime = time() - 1 * DAYSECS; 270 $DB->insert_record('backup_courses', $backupcourse); 271 $backupcourse = $DB->get_record('backup_courses', array('courseid' => $this->course->id)); 272 $course = $DB->get_record('course', array('id' => $this->course->id)); 273 274 $course->timemodified = time() - 2 * DAYSECS - 1; 275 276 $classobject = $this->backupcronautomatedhelper->return_this(); 277 $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup(null, time()); 278 279 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'should_skip_course_backup'); 280 $method->setAccessible(true); // Allow accessing of private method. 281 $skipped = $method->invokeArgs($classobject, [$backupcourse, $course, $nextstarttime]); 282 283 $this->assertTrue($skipped); 284 $this->expectOutputRegex("/Skipping $course->fullname \(Not modified since previous backup\)/"); 285 } 286 287 /** 288 * Test the task completes when coureid is missing. 289 */ 290 public function test_task_complete_when_courseid_is_missing() { 291 global $DB; 292 $admin = get_admin(); 293 $classobject = $this->backupcronautomatedhelper->return_this(); 294 295 // Create this backup course. 296 $backupcourse = new \stdClass; 297 $backupcourse->courseid = $this->course->id; 298 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN; 299 $DB->insert_record('backup_courses', $backupcourse); 300 $backupcourse = $DB->get_record('backup_courses', ['courseid' => $this->course->id]); 301 302 // Create a backup task. 303 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'push_course_backup_adhoc_task'); 304 $method->setAccessible(true); // Allow accessing of private method. 305 $method->invokeArgs($classobject, [$backupcourse, $admin]); 306 307 // Delete course for this test. 308 delete_course($this->course->id, false); 309 310 $task = \core\task\manager::get_next_adhoc_task(time()); 311 312 ob_start(); 313 $task->execute(); 314 $output = ob_get_clean(); 315 316 $this->assertStringContainsString('Invalid course id: ' . $this->course->id . ', task aborted.', $output); 317 \core\task\manager::adhoc_task_complete($task); 318 } 319 320 /** 321 * Test the task completes when backup course is missing. 322 */ 323 public function test_task_complete_when_backup_course_is_missing() { 324 global $DB; 325 $admin = get_admin(); 326 $classobject = $this->backupcronautomatedhelper->return_this(); 327 328 // Create this backup course. 329 $backupcourse = new \stdClass; 330 $backupcourse->courseid = $this->course->id; 331 $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN; 332 $DB->insert_record('backup_courses', $backupcourse); 333 $backupcourse = $DB->get_record('backup_courses', ['courseid' => $this->course->id]); 334 335 // Create a backup task. 336 $method = new \ReflectionMethod('\backup_cron_automated_helper', 'push_course_backup_adhoc_task'); 337 $method->setAccessible(true); // Allow accessing of private method. 338 $method->invokeArgs($classobject, [$backupcourse, $admin]); 339 340 // Delete backup course for this test. 341 $DB->delete_records('backup_courses', ['courseid' => $this->course->id]); 342 343 $task = \core\task\manager::get_next_adhoc_task(time()); 344 345 ob_start(); 346 $task->execute(); 347 $output = ob_get_clean(); 348 349 $this->assertStringContainsString('Automated backup for course: ' . $this->course->fullname . ' encounters an error.', 350 $output); 351 \core\task\manager::adhoc_task_complete($task); 352 } 353 } 354 355 /** 356 * New backup_cron_automated_helper class for testing. 357 * 358 * This class extends the helper backup_cron_automated_helper class 359 * in order to utilise abstract class for testing. 360 * 361 * @package core 362 * @copyright 2019 John Yao <johnyao@catalyst-au.net> 363 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 364 */ 365 class test_backup_cron_automated_helper extends backup_cron_automated_helper { 366 /** 367 * Returning this for testing. 368 */ 369 public function return_this() { 370 return $this; 371 } 372 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body