From 2e3d05e5918a02bdc588c7da859dcd54e3903d5c Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 24 Jun 2019 12:57:17 +0800 Subject: [PATCH] MDL-65724 core: Force restore to use the settings from the plan When importing use the import settings. --- backup/moodle2/tests/moodle2_test.php | 46 +++++++++++++++++++ .../dbops/restore_controller_dbops.class.php | 9 +++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/backup/moodle2/tests/moodle2_test.php b/backup/moodle2/tests/moodle2_test.php index c4f9e2c7f24..38f53eaf76e 100644 --- a/backup/moodle2/tests/moodle2_test.php +++ b/backup/moodle2/tests/moodle2_test.php @@ -955,6 +955,52 @@ class core_backup_moodle2_testcase extends advanced_testcase { $this->assertEquals('', $requests[2]->searcharea); } + /** + * Test restoring courses based on the backup plan. Primarily used with + * the import functionality + */ + public function test_restore_course_using_plan_defaults() { + global $DB, $CFG, $USER; + + $this->resetAfterTest(true); + $this->setAdminUser(); + $CFG->enableglobalsearch = true; + + // Set admin config setting so that activities are not restored by default. + set_config('restore_general_activities', 0, 'restore'); + + // Create a course. + $generator = $this->getDataGenerator(); + $course = $generator->create_course(); + $course2 = $generator->create_course(); + $course3 = $generator->create_course(); + + // Add a forum. + $forum = $generator->create_module('forum', ['course' => $course->id]); + + // Backup course... + $CFG->backup_file_logger_level = backup::LOG_NONE; + $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, + backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, + $USER->id); + $backupid = $bc->get_backupid(); + $bc->execute_plan(); + $bc->destroy(); + + // Restore it on top of course2 (should duplicate the forum). + $rc = new restore_controller($backupid, $course2->id, + backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, + backup::TARGET_EXISTING_ADDING, null); + $this->assertTrue($rc->execute_precheck()); + $rc->execute_plan(); + $rc->destroy(); + + // Get the forums now on the old course. + $modinfo = get_fast_modinfo($course2->id); + $forums = $modinfo->get_instances_of('forum'); + $this->assertCount(0, $forums); + } + /** * The Question category hierarchical structure was changed in Moodle 3.5. * From 3.5, all question categories in each context are a child of a single top level question category for that context. diff --git a/backup/util/dbops/restore_controller_dbops.class.php b/backup/util/dbops/restore_controller_dbops.class.php index 1894eeb9f89..6649777c2b5 100644 --- a/backup/util/dbops/restore_controller_dbops.class.php +++ b/backup/util/dbops/restore_controller_dbops.class.php @@ -282,7 +282,14 @@ abstract class restore_controller_dbops extends restore_dbops { if ($plan->setting_exists($settingname)) { $setting = $plan->get_setting($settingname); $value = self::get_setting_default($config, $setting); - $locked = (get_config('restore', $config . '_locked') == true); + $locked = (get_config('restore',$config . '_locked') == true); + + // Use the original value when this is an import and the setting is unlocked. + if ($controller->get_mode() == backup::MODE_IMPORT && $controller->get_interactive()) { + if (!$uselocks || !$locked) { + $value = $setting->get_value(); + } + } // We can only update the setting if it isn't already locked by config or permission. if ($setting->get_status() != base_setting::LOCKED_BY_CONFIG