MDL-71949 backup: Fix userid and add backup ID to logging

This commit is contained in:
Matthew Hilton 2021-12-16 13:13:10 +10:00
parent 696857a008
commit 90d21c6351
5 changed files with 25 additions and 1 deletions

View file

@ -202,6 +202,7 @@ if (!async_helper::is_async_pending($id, 'course', 'backup')) {
$asynctask = new \core\task\asynchronous_backup_task(); $asynctask = new \core\task\asynchronous_backup_task();
$asynctask->set_blocking(false); $asynctask->set_blocking(false);
$asynctask->set_custom_data(array('backupid' => $backupid)); $asynctask->set_custom_data(array('backupid' => $backupid));
$asynctask->set_userid($USER->id);
\core\task\manager::queue_adhoc_task($asynctask); \core\task\manager::queue_adhoc_task($asynctask);
// Add ajax progress bar and initiate ajax via a template. // Add ajax progress bar and initiate ajax via a template.

View file

@ -171,6 +171,7 @@ if ($restore->get_stage() != restore_ui::STAGE_PROCESS) {
$restoreid = $restore->get_restoreid(); $restoreid = $restore->get_restoreid();
$asynctask = new \core\task\asynchronous_restore_task(); $asynctask = new \core\task\asynchronous_restore_task();
$asynctask->set_blocking(false); $asynctask->set_blocking(false);
$asynctask->set_userid($USER->id);
$asynctask->set_custom_data(array('backupid' => $restoreid)); $asynctask->set_custom_data(array('backupid' => $restoreid));
\core\task\manager::queue_adhoc_task($asynctask); \core\task\manager::queue_adhoc_task($asynctask);

View file

@ -59,6 +59,10 @@ class core_backup_async_backup_testcase extends \core_privacy\tests\provider_tes
$forum2 = $generator->create_module('forum', array( $forum2 = $generator->create_module('forum', array(
'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL)); 'course' => $course->id, 'completion' => COMPLETION_TRACKING_MANUAL));
// Create a teacher user to call the backup.
$teacher = $generator->create_user();
$generator->enrol_user($teacher->id, $course->id, 'editingteacher');
// We need a grade, easiest is to add an assignment. // We need a grade, easiest is to add an assignment.
$assignrow = $generator->create_module('assign', array( $assignrow = $generator->create_module('assign', array(
'course' => $course->id)); 'course' => $course->id));
@ -79,7 +83,14 @@ class core_backup_async_backup_testcase extends \core_privacy\tests\provider_tes
$DB->set_field('course_sections', 'availability', $availability, array( $DB->set_field('course_sections', 'availability', $availability, array(
'course' => $course->id, 'section' => 1)); 'course' => $course->id, 'section' => 1));
// Enable logging.
$this->preventResetByRollback();
set_config('enabled_stores', 'logstore_standard', 'tool_log');
set_config('buffersize', 0, 'logstore_standard');
get_log_manager(true);
// Start backup process. // Start backup process.
$this->setUser($teacher->id);
// Make the backup controller for an async backup. // Make the backup controller for an async backup.
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE, $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
@ -98,6 +109,7 @@ class core_backup_async_backup_testcase extends \core_privacy\tests\provider_tes
$asynctask = new \core\task\asynchronous_backup_task(); $asynctask = new \core\task\asynchronous_backup_task();
$asynctask->set_blocking(false); $asynctask->set_blocking(false);
$asynctask->set_custom_data(array('backupid' => $backupid)); $asynctask->set_custom_data(array('backupid' => $backupid));
$asynctask->set_userid($USER->id);
\core\task\manager::queue_adhoc_task($asynctask); \core\task\manager::queue_adhoc_task($asynctask);
// We are expecting trace output during this test. // We are expecting trace output during this test.
@ -110,10 +122,17 @@ class core_backup_async_backup_testcase extends \core_privacy\tests\provider_tes
$task->execute(); $task->execute();
\core\task\manager::adhoc_task_complete($task); \core\task\manager::adhoc_task_complete($task);
$postbackuprec = $DB->get_record('backup_controllers', array('backupid' => $backupid)); $postbackuprec = $DB->get_record('backup_controllers', ['backupid' => $backupid]);
// Check backup was created successfully. // Check backup was created successfully.
$this->assertEquals(backup::STATUS_FINISHED_OK, $postbackuprec->status); $this->assertEquals(backup::STATUS_FINISHED_OK, $postbackuprec->status);
$this->assertEquals(1.0, $postbackuprec->progress); $this->assertEquals(1.0, $postbackuprec->progress);
$this->assertEquals($teacher->id, $postbackuprec->userid);
// Check that the backupid was logged correctly.
$logrec = $DB->get_record('logstore_standard_log', ['userid' => $postbackuprec->userid,
'target' => 'course_backup'], '*', MUST_EXIST);
$otherdata = json_decode($logrec->other);
$this->assertEquals($backupid, $otherdata->backupid);
} }
} }

View file

@ -119,6 +119,7 @@ class core_backup_async_restore_testcase extends \core_privacy\tests\provider_te
$asynctask = new \core\task\asynchronous_restore_task(); $asynctask = new \core\task\asynchronous_restore_task();
$asynctask->set_blocking(false); $asynctask->set_blocking(false);
$asynctask->set_custom_data(array('backupid' => $restoreid)); $asynctask->set_custom_data(array('backupid' => $restoreid));
$asynctask->set_userid($USER->id);
\core\task\manager::queue_adhoc_task($asynctask); \core\task\manager::queue_adhoc_task($asynctask);
// We are expecting trace output during this test. // We are expecting trace output during this test.
@ -136,5 +137,6 @@ class core_backup_async_restore_testcase extends \core_privacy\tests\provider_te
// Check backup was created successfully. // Check backup was created successfully.
$this->assertEquals(backup::STATUS_FINISHED_OK, $postrestorerec->status); $this->assertEquals(backup::STATUS_FINISHED_OK, $postrestorerec->status);
$this->assertEquals(1.0, $postrestorerec->progress); $this->assertEquals(1.0, $postrestorerec->progress);
$this->assertEquals($USER->id, $postrestorerec->userid);
} }
} }

View file

@ -151,6 +151,7 @@ class backup_plan extends base_plan implements loggable {
'mode' => $this->controller->get_mode(), 'mode' => $this->controller->get_mode(),
'interactive' => $this->controller->get_interactive(), 'interactive' => $this->controller->get_interactive(),
'type' => $this->controller->get_type(), 'type' => $this->controller->get_type(),
'backupid' => $this->controller->get_backupid()
); );
$event = \core\event\course_backup_created::create(array( $event = \core\event\course_backup_created::create(array(
'objectid' => $this->get_courseid(), 'objectid' => $this->get_courseid(),