From 90583e2f3b889b41aaacc36e7999ec2373fe39e6 Mon Sep 17 00:00:00 2001 From: Matthew Hilton Date: Thu, 16 Dec 2021 13:13:10 +1000 Subject: [PATCH] MDL-71949 backup: Fix userid and add backup ID to logging --- backup/backup.php | 1 + backup/restore.php | 1 + backup/tests/async_backup_test.php | 21 ++++++++++++++++++++- backup/tests/async_restore_test.php | 2 ++ backup/util/plan/backup_plan.class.php | 1 + 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/backup/backup.php b/backup/backup.php index 80968967d5c..bc77c3883e2 100644 --- a/backup/backup.php +++ b/backup/backup.php @@ -203,6 +203,7 @@ if (!async_helper::is_async_pending($id, 'course', 'backup')) { $asynctask = new \core\task\asynchronous_backup_task(); $asynctask->set_blocking(false); $asynctask->set_custom_data(array('backupid' => $backupid)); + $asynctask->set_userid($USER->id); \core\task\manager::queue_adhoc_task($asynctask); // Add ajax progress bar and initiate ajax via a template. diff --git a/backup/restore.php b/backup/restore.php index a6b7d76280a..f4d2d3838f7 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -171,6 +171,7 @@ if ($restore->get_stage() != restore_ui::STAGE_PROCESS) { $restoreid = $restore->get_restoreid(); $asynctask = new \core\task\asynchronous_restore_task(); $asynctask->set_blocking(false); + $asynctask->set_userid($USER->id); $asynctask->set_custom_data(array('backupid' => $restoreid)); \core\task\manager::queue_adhoc_task($asynctask); diff --git a/backup/tests/async_backup_test.php b/backup/tests/async_backup_test.php index 509c83cf25f..76a0f740a3c 100644 --- a/backup/tests/async_backup_test.php +++ b/backup/tests/async_backup_test.php @@ -59,6 +59,10 @@ class core_backup_async_backup_testcase extends \core_privacy\tests\provider_tes $forum2 = $generator->create_module('forum', array( '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. $assignrow = $generator->create_module('assign', array( '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( '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. + $this->setUser($teacher->id); // Make the backup controller for an async backup. $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->set_blocking(false); $asynctask->set_custom_data(array('backupid' => $backupid)); + $asynctask->set_userid($USER->id); \core\task\manager::queue_adhoc_task($asynctask); // 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(); \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. $this->assertEquals(backup::STATUS_FINISHED_OK, $postbackuprec->status); $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); } } diff --git a/backup/tests/async_restore_test.php b/backup/tests/async_restore_test.php index 1121e371ad5..589b3b57d2d 100644 --- a/backup/tests/async_restore_test.php +++ b/backup/tests/async_restore_test.php @@ -119,6 +119,7 @@ class core_backup_async_restore_testcase extends \core_privacy\tests\provider_te $asynctask = new \core\task\asynchronous_restore_task(); $asynctask->set_blocking(false); $asynctask->set_custom_data(array('backupid' => $restoreid)); + $asynctask->set_userid($USER->id); \core\task\manager::queue_adhoc_task($asynctask); // 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. $this->assertEquals(backup::STATUS_FINISHED_OK, $postrestorerec->status); $this->assertEquals(1.0, $postrestorerec->progress); + $this->assertEquals($USER->id, $postrestorerec->userid); } } diff --git a/backup/util/plan/backup_plan.class.php b/backup/util/plan/backup_plan.class.php index a3dc2b0fdbc..ce23748bc99 100644 --- a/backup/util/plan/backup_plan.class.php +++ b/backup/util/plan/backup_plan.class.php @@ -151,6 +151,7 @@ class backup_plan extends base_plan implements loggable { 'mode' => $this->controller->get_mode(), 'interactive' => $this->controller->get_interactive(), 'type' => $this->controller->get_type(), + 'backupid' => $this->controller->get_backupid() ); $event = \core\event\course_backup_created::create(array( 'objectid' => $this->get_courseid(),