MDL-65724 core: Force restore to use the settings from the plan
authorPeter <peter@moodle.com>
Mon, 24 Jun 2019 04:57:17 +0000 (12:57 +0800)
committerPeter Dias <peter@moodle.com>
Fri, 6 Mar 2020 05:43:39 +0000 (13:43 +0800)
When importing use the import settings.

backup/moodle2/tests/moodle2_test.php
backup/util/dbops/restore_controller_dbops.class.php

index c4f9e2c..6649934 100644 (file)
@@ -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, backup::RELEASESESSION_NO);
+        $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.
index 1894eeb..6649777 100644 (file)
@@ -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