Skip to content

Commit

Permalink
MDL-71136 backup: Fix moodle_exception path.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Mar 29, 2021
1 parent d65ed58 commit b6f6203
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
67 changes: 67 additions & 0 deletions backup/tests/automated_backup_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,73 @@ public function test_should_skip_not_modified_course_since_prev() {
$this->assertTrue($skipped);
$this->expectOutputRegex("/Skipping $course->fullname \(Not modified since previous backup\)/");
}

/**
* Test the task completes when coureid is missing.
*/
public function test_task_complete_when_courseid_is_missing() {
global $DB;
$admin = get_admin();
$classobject = $this->backupcronautomatedhelper->return_this();

// Create this backup course.
$backupcourse = new stdClass;
$backupcourse->courseid = $this->course->id;
$backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN;
$DB->insert_record('backup_courses', $backupcourse);
$backupcourse = $DB->get_record('backup_courses', ['courseid' => $this->course->id]);

// Create a backup task.
$method = new ReflectionMethod('\backup_cron_automated_helper', 'push_course_backup_adhoc_task');
$method->setAccessible(true); // Allow accessing of private method.
$method->invokeArgs($classobject, [$backupcourse, $admin]);

// Delete course for this test.
delete_course($this->course->id, false);

$task = core\task\manager::get_next_adhoc_task(time());

ob_start();
$task->execute();
$output = ob_get_clean();

$this->assertStringContainsString('Invalid course id: ' . $this->course->id . ', task aborted.', $output);
core\task\manager::adhoc_task_complete($task);
}

/**
* Test the task completes when backup course is missing.
*/
public function test_task_complete_when_backup_course_is_missing() {
global $DB;
$admin = get_admin();
$classobject = $this->backupcronautomatedhelper->return_this();

// Create this backup course.
$backupcourse = new stdClass;
$backupcourse->courseid = $this->course->id;
$backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_NOTYETRUN;
$DB->insert_record('backup_courses', $backupcourse);
$backupcourse = $DB->get_record('backup_courses', ['courseid' => $this->course->id]);

// Create a backup task.
$method = new ReflectionMethod('\backup_cron_automated_helper', 'push_course_backup_adhoc_task');
$method->setAccessible(true); // Allow accessing of private method.
$method->invokeArgs($classobject, [$backupcourse, $admin]);

// Delete backup course for this test.
$DB->delete_records('backup_courses', ['courseid' => $this->course->id]);

$task = core\task\manager::get_next_adhoc_task(time());

ob_start();
$task->execute();
$output = ob_get_clean();

$this->assertStringContainsString('Automated backup for course: ' . $this->course->fullname . ' encounters an error.',
$output);
core\task\manager::adhoc_task_complete($task);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/task/course_backup_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function execute() {

try {
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
} catch (moodle_exception $e) {
} catch (\moodle_exception $e) {
mtrace('Invalid course id: ' . $courseid . ', task aborted.');
return;
}
Expand Down

0 comments on commit b6f6203

Please sign in to comment.