MDL-63263 tool_recyclebin: Apply automated backup setting

Use Automated backup settings as settings for Recycle bin backup and restore.
This commit is contained in:
Nathan Nguyen 2019-02-15 16:20:14 +11:00 committed by Nathan Nguyen
parent 9665abad31
commit cebfb23fb1
4 changed files with 88 additions and 50 deletions

View File

@ -337,6 +337,10 @@ if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) {
// Automated defaults section.
$temp->add(new admin_setting_heading('automatedsettings', new lang_string('automatedsettings','backup'), ''));
$temp->add(new admin_setting_description('recyclebin',
new lang_string('recyclebin', 'backup'),
new lang_string('recyclebin_desc', 'backup')
));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_users', new lang_string('generalusers', 'backup'), new lang_string('configgeneralusers', 'backup'), 1));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_role_assignments', new lang_string('generalroleassignments','backup'), new lang_string('configgeneralroleassignments','backup'), 1));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_activities', new lang_string('generalactivities','backup'), new lang_string('configgeneralactivities','backup'), 1));

View File

@ -112,9 +112,6 @@ 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(
@ -122,7 +119,7 @@ class course_bin extends base_bin {
$cm->id,
\backup::FORMAT_MOODLE,
\backup::INTERACTIVE_NO,
\backup::MODE_GENERAL,
\backup::MODE_AUTOMATED,
$user->id
);
$controller->execute_plan();
@ -227,15 +224,11 @@ class course_bin extends base_bin {
$tempdir,
$this->_courseid,
\backup::INTERACTIVE_NO,
\backup::MODE_GENERAL,
\backup::MODE_AUTOMATED,
$user->id,
\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();

View File

@ -24,9 +24,6 @@
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/assign/tests/fixtures/testable_assign.php');
/**
* Recycle bin course tests.
*
@ -58,7 +55,7 @@ class tool_recyclebin_course_bin_tests extends advanced_testcase {
$this->course = $this->getDataGenerator()->create_course();
$this->quiz = $this->getDataGenerator()->get_plugin_generator('mod_quiz')->create_instance(array(
'course' => $this->course->id
'course' => $this->course->id, 'grade' => 100.0, 'sumgrades' => 1
));
}
@ -180,53 +177,95 @@ class tool_recyclebin_course_bin_tests extends advanced_testcase {
/**
* 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.
public function test_coursemodule_restore_with_userdata() {
$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);
$this->setUser($student);
// Verify that user submission exists.
$submission = $assign->get_user_submission($student->id, false);
$this->assertNotFalse($submission);
set_config('backup_auto_users', true, 'backup');
$this->create_quiz_attempt($this->quiz, $student);
// Delete assignment.
// Delete quiz.
$cm = get_coursemodule_from_instance('quiz', $this->quiz->id);
course_delete_module($cm->id);
phpunit_util::run_all_adhoc_tasks();
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(0, count($quizzes));
// Restore assignment.
// Restore quiz.
$recyclebin = new \tool_recyclebin\course_bin($this->course->id);
foreach ($recyclebin->get_items() as $item) {
$recyclebin->restore_item($item);
}
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(1, count($quizzes));
$cm = array_pop($quizzes);
// 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);
// Check if user quiz attempt data is restored.
$attempts = quiz_get_user_attempts($cm->instance, $student->id);
$this->assertEquals(1, count($attempts));
$attempt = array_pop($attempts);
$attemptobj = quiz_attempt::create($attempt->id);
$this->assertEquals($student->id, $attemptobj->get_userid());
$this->assertEquals(true, $attemptobj->is_finished());
}
/**
* Tests that user data is not restored when module is restored.
*/
public function test_coursemodule_restore_without_userdata() {
$student = $this->getDataGenerator()->create_and_enrol($this->course, 'student');
$this->setUser($student);
set_config('backup_auto_users', false, 'backup');
$this->create_quiz_attempt($this->quiz, $student);
// Delete quiz.
$cm = get_coursemodule_from_instance('quiz', $this->quiz->id);
course_delete_module($cm->id);
phpunit_util::run_all_adhoc_tasks();
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(0, count($quizzes));
// Restore quiz.
$recyclebin = new \tool_recyclebin\course_bin($this->course->id);
foreach ($recyclebin->get_items() as $item) {
$recyclebin->restore_item($item);
}
$quizzes = get_coursemodules_in_course('quiz', $this->course->id);
$this->assertEquals(1, count($quizzes));
$cm = array_pop($quizzes);
// Check if user quiz attempt data is restored.
$attempts = quiz_get_user_attempts($cm->instance, $student->id);
$this->assertEquals(0, count($attempts));
}
/**
* Add a question to quiz and create a quiz attempt.
* @param \stdClass $quiz Quiz
* @param \stdClass $student User
* @throws coding_exception
* @throws moodle_exception
*/
private function create_quiz_attempt($quiz, $student) {
// Add Question.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
quiz_add_quiz_question($numq->id, $quiz);
// Create quiz attempt.
$quizobj = quiz::create($quiz->id, $student->id);
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
$timenow = time();
$attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $student->id);
quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
quiz_attempt_save_started($quizobj, $quba, $attempt);
$attemptobj = quiz_attempt::create($attempt->id);
$tosubmit = array(1 => array('answer' => '0'));
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
$attemptobj = quiz_attempt::create($attempt->id);
$attemptobj->process_finish($timenow, false);
}
}

View File

@ -332,3 +332,5 @@ $string['unnamedsection'] = 'Unnamed section';
$string['userinfo'] = 'Userinfo';
$string['module'] = 'Module';
$string['morecoursesearchresults'] = 'More than {$a} courses found, showing first {$a} results';
$string['recyclebin'] = 'Recycle bin';
$string['recyclebin_desc'] = 'These settings will be also applied to recycle bin';