Skip to content

Commit

Permalink
MDL-63263 tool_recyclebin: Recycle bin should always save user data.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlorenzo authored and Nathan Nguyen committed Mar 15, 2019
1 parent f3ad037 commit 8e6e56c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
9 changes: 8 additions & 1 deletion admin/tool/recyclebin/classes/course_bin.php
Expand Up @@ -112,6 +112,9 @@ public function store_item($cm) {
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(
Expand Down Expand Up @@ -229,6 +232,10 @@ public function restore_item($item) {
\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();
Expand Down Expand Up @@ -344,4 +351,4 @@ public function can_delete() {
$context = \context_course::instance($this->_courseid);
return has_capability('tool/recyclebin:deleteitems', $context);
}
}
}
56 changes: 56 additions & 0 deletions admin/tool/recyclebin/tests/course_bin_test.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -173,4 +176,57 @@ public function test_cleanup_task() {
$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);
}
}

0 comments on commit 8e6e56c

Please sign in to comment.