MDL-63263 tool_recyclebin: Recycle bin should always save user data.
authorRex Lorenzo <rex@oid.ucla.edu>
Mon, 17 Sep 2018 20:25:34 +0000 (13:25 -0700)
committerNathan Nguyen <ext-nnguy@monash.edu>
Fri, 15 Mar 2019 03:56:34 +0000 (14:56 +1100)
admin/tool/recyclebin/classes/course_bin.php
admin/tool/recyclebin/tests/course_bin_test.php

index fb5d779..c3c5293 100644 (file)
@@ -112,6 +112,9 @@ class course_bin extends base_bin {
             return;
         }
 
+        $CFG->forced_plugin_settings['backup']['backup_general_users'] = 1;
+        $CFG->forced_plugin_settings['backup']['backup_general_groups'] = 1;
+
         // Backup the activity.
         $user = get_admin();
         $controller = new \backup_controller(
@@ -229,6 +232,10 @@ class course_bin extends base_bin {
             \backup::TARGET_EXISTING_ADDING
         );
 
+        // Make sure to restore user data.
+        $controller->get_plan()->get_setting('users')->set_value(1);
+        $controller->get_plan()->get_setting('groups')->set_value(1);
+
         // Prechecks.
         if (!$controller->execute_precheck()) {
             $results = $controller->get_precheck_results();
@@ -344,4 +351,4 @@ class course_bin extends base_bin {
         $context = \context_course::instance($this->_courseid);
         return has_capability('tool/recyclebin:deleteitems', $context);
     }
-}
+}
\ No newline at end of file
index 9a4e0c3..88ec96d 100644 (file)
@@ -24,6 +24,9 @@
 
 defined('MOODLE_INTERNAL') || die();
 
+global $CFG;
+require_once($CFG->dirroot . '/mod/assign/tests/fixtures/testable_assign.php');
+
 /**
  * Recycle bin course tests.
  *
@@ -173,4 +176,57 @@ class tool_recyclebin_course_bin_tests extends advanced_testcase {
         $deletedbook = reset($items);
         $this->assertEquals($book->name, $deletedbook->name);
     }
+
+    /**
+     * Tests that user data is restored when module is restored.
+     */
+    public function test_userdata_restore() {
+        set_config('backup_general_users', 0, 'backup');
+        set_config('restore_general_users', 0, 'restore');
+
+        // Create assignment and user submission.
+        $student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
+        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
+        $instance = $generator->create_instance([
+                'assignsubmission_onlinetext_enabled' => true,
+                'course' => $this->course->id
+            ]);
+        $cm = get_coursemodule_from_instance('assign', $instance->id);
+        $context = context_module::instance($cm->id);
+        $assign = new mod_assign_testable_assign($context, $cm, $this->course);
+        $this->setUser($student->id);
+        $submission = $assign->get_user_submission($student->id, true);
+        $data = (object) [
+            'onlinetext_editor' => [
+                'itemid' => file_get_unused_draft_itemid(),
+                'text' => 'Submission text',
+                'format' => FORMAT_PLAIN,
+            ],
+        ];
+        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
+        $plugin->save($submission, $data);
+
+        // Verify that user submission exists.
+        $submission = $assign->get_user_submission($student->id, false);
+        $this->assertNotFalse($submission);
+
+        // Delete assignment.
+        course_delete_module($cm->id);
+        phpunit_util::run_all_adhoc_tasks();
+
+        // Restore assignment.
+        $recyclebin = new \tool_recyclebin\course_bin($this->course->id);
+        foreach ($recyclebin->get_items() as $item) {
+            $recyclebin->restore_item($item);
+        }
+
+        // Verify that user submission exists.
+        $assignments = get_coursemodules_in_course('assign', $this->course->id);
+        $this->assertEquals(1, count($assignments));
+        $cm = array_pop($assignments);
+        $context = context_module::instance($cm->id);
+        $assign = new mod_assign_testable_assign($context, $cm, $this->course);
+        $submission = $assign->get_user_submission($student->id, false);
+        $this->assertNotFalse($submission);
+    }
 }