From 2ef01c0fdfabc93e3c7d2e8aedcd57968a11b47f Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Fri, 25 Nov 2022 09:38:50 +0700 Subject: [PATCH 1/3] MDL-68981 tool_recyclebin: Extra Behat test with General restore config Including in this commit: - Convert existing Behat scenario to use Data generators - Convert existing Behat scenario to use Provider - Add new scenario for Include users setting in General restore page --- .../tests/behat/backup_user_data.feature | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/admin/tool/recyclebin/tests/behat/backup_user_data.feature b/admin/tool/recyclebin/tests/behat/backup_user_data.feature index 7542ad61c47..b6012e84eef 100644 --- a/admin/tool/recyclebin/tests/behat/backup_user_data.feature +++ b/admin/tool/recyclebin/tests/behat/backup_user_data.feature @@ -22,35 +22,30 @@ Feature: Backup user data And the following "activities" exist: | activity | course | section | name | intro | | quiz | C1 | 1 | Quiz 1 | Test quiz description | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | questioncategory | qtype | name | questiontext | + | Test questions | truefalse | TF1 | First question | + | Test questions | truefalse | TF2 | Second question | + And quiz "Quiz 1" contains the following questions: + | question | page | + | TF1 | 1 | + | TF2 | 1 | @javascript - Scenario: Delete and restore a quiz with user data - Given I am on the "Quiz 1" "quiz activity" page logged in as teacher1 - And I add a "True/False" question to the "Quiz 1" quiz with: - | Question name | TF1 | - | Question text | First question | - | General feedback | Thank you, this is the general feedback | - | Correct answer | False | - | Feedback for the response 'True'. | So you think it is true | - | Feedback for the response 'False'. | So you think it is false | - And I add a "True/False" question to the "Quiz 1" quiz with: - | Question name | TF2 | - | Question text | Second question | - | General feedback | Thank you, this is the general feedback | - | Correct answer | False | - | Feedback for the response 'True'. | So you think it is true | - | Feedback for the response 'False'. | So you think it is false | - And I set the field "maxgrade" to "10.0" - And I press "savechanges" - And I log out - When I am on the "Quiz 1" "quiz activity" page logged in as student1 + Scenario Outline: Delete and restore a quiz with user data + Given the following config values are set as admin: + | restore_general_users | | restore | + And I am on the "Quiz 1" "quiz activity" page logged in as student1 And I press "Attempt quiz" And I click on "True" "radio" in the "First question" "question" And I click on "False" "radio" in the "Second question" "question" And I press "Finish attempt" And I press "Submit all and finish" And I click on "Submit" "button" in the "Submit all your answers and finish?" "dialogue" - And I should see "5.00 out of 10.00" + And I should see "50.00 out of 100.00" And I log out And I log in as "teacher1" And I am on "Course 1" course homepage with editing mode on @@ -63,5 +58,10 @@ Feature: Backup user data And I log in as "student1" And I am on "Course 1" course homepage When I navigate to "User report" in the course gradebook - Then "Quiz 1" row "Grade" column of "user-grade" table should contain "5" + Then "Quiz 1" row "Grade" column of "user-grade" table should contain "50" And "Quiz 1" row "Percentage" column of "user-grade" table should contain "50" + + Examples: + | include_user | case_explanation | + | 1 | Checked | + | 1 | Unchecked | From dcc115786dd5f448b87a9dc62cdc6ebc869d09be Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Fri, 25 Nov 2022 09:46:48 +0700 Subject: [PATCH 2/3] MDL-68981 tool_recyclebin: Extra PHPUnit with General restore config --- admin/tool/recyclebin/tests/category_bin_test.php | 5 +++++ admin/tool/recyclebin/tests/course_bin_test.php | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/admin/tool/recyclebin/tests/category_bin_test.php b/admin/tool/recyclebin/tests/category_bin_test.php index 1048bc7f708..b12aa4de0d3 100644 --- a/admin/tool/recyclebin/tests/category_bin_test.php +++ b/admin/tool/recyclebin/tests/category_bin_test.php @@ -190,6 +190,11 @@ class category_bin_test extends \advanced_testcase { (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 2], (object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true], ]], + + 'restore/restore_general_users moodle' => [[ + (object)['plugin' => 'restore', 'name' => 'restore_general_users', 'value' => 0], + (object)['plugin' => 'restore', 'name' => 'restore_general_groups', 'value' => 0], + ]], ]; } diff --git a/admin/tool/recyclebin/tests/course_bin_test.php b/admin/tool/recyclebin/tests/course_bin_test.php index b601b52920b..821ceedf749 100644 --- a/admin/tool/recyclebin/tests/course_bin_test.php +++ b/admin/tool/recyclebin/tests/course_bin_test.php @@ -187,6 +187,11 @@ class course_bin_test extends \advanced_testcase { (object)['plugin' => 'backup', 'name' => 'backup_auto_storage', 'value' => 2], (object)['plugin' => 'backup', 'name' => 'backup_auto_destination', 'value' => true], ]], + + 'restore/restore_general_users moodle' => [[ + (object)['plugin' => 'restore', 'name' => 'restore_general_users', 'value' => 0], + (object)['plugin' => 'restore', 'name' => 'restore_general_groups', 'value' => 0], + ]], ]; } From 19ed64f2d97ad18fb79e8bd779b3e228eb6ba699 Mon Sep 17 00:00:00 2001 From: Huong Nguyen Date: Fri, 25 Nov 2022 09:48:41 +0700 Subject: [PATCH 3/3] MDL-68981 tool_recyclebin: Force necessary settings when restoring Created code to force the necessary settings for restore_item in: + course_bin class + category_bin class --- admin/tool/recyclebin/classes/category_bin.php | 14 ++++++++++++++ admin/tool/recyclebin/classes/course_bin.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/admin/tool/recyclebin/classes/category_bin.php b/admin/tool/recyclebin/classes/category_bin.php index b5bde99297f..67bd0072d32 100644 --- a/admin/tool/recyclebin/classes/category_bin.php +++ b/admin/tool/recyclebin/classes/category_bin.php @@ -243,6 +243,16 @@ class category_bin extends base_bin { throw new \moodle_exception("Could not create course to restore into."); } + // As far as recycle bin is using MODE_AUTOMATED, it observes the General restore settings. + // For recycle bin we want to ensure that backup files are always restore the users and groups information. + // In order to achieve that, we hack the setting here via $CFG->forced_plugin_settings, + // so it won't interfere other operations. + // See MDL-65218 and MDL-35773 for more information. + // This hack will be removed once recycle bin switches to use its own backup mode, with + // own preferences and 100% separate from MOODLE_AUTOMATED. + // TODO: Remove this as part of MDL-65228. + $CFG->forced_plugin_settings['restore'] = ['restore_general_users' => 1, 'restore_general_groups' => 1]; + // Define the import. $controller = new \restore_controller( $tempdir, @@ -277,6 +287,10 @@ class category_bin extends base_bin { // Run the import. $controller->execute_plan(); + // We don't need the forced setting anymore, hence unsetting it. + // TODO: Remove this as part of MDL-65228. + unset($CFG->forced_plugin_settings['restore']); + // Have finished with the controller, let's destroy it, freeing mem and resources. $controller->destroy(); diff --git a/admin/tool/recyclebin/classes/course_bin.php b/admin/tool/recyclebin/classes/course_bin.php index 0353c2c49ef..001158156e6 100644 --- a/admin/tool/recyclebin/classes/course_bin.php +++ b/admin/tool/recyclebin/classes/course_bin.php @@ -234,6 +234,16 @@ class course_bin extends base_bin { $fb = get_file_packer('application/vnd.moodle.backup'); $fb->extract_to_pathname($file, $fulltempdir); + // As far as recycle bin is using MODE_AUTOMATED, it observes the General restore settings. + // For recycle bin we want to ensure that backup files are always restore the users and groups information. + // In order to achieve that, we hack the setting here via $CFG->forced_plugin_settings, + // so it won't interfere other operations. + // See MDL-65218 and MDL-35773 for more information. + // This hack will be removed once recycle bin switches to use its own backup mode, with + // own preferences and 100% separate from MOODLE_AUTOMATED. + // TODO: Remove this as part of MDL-65228. + $CFG->forced_plugin_settings['restore'] = ['restore_general_users' => 1, 'restore_general_groups' => 1]; + // Define the import. $controller = new \restore_controller( $tempdir, @@ -264,6 +274,10 @@ class course_bin extends base_bin { // Run the import. $controller->execute_plan(); + // We don't need the forced setting anymore, hence unsetting it. + // TODO: Remove this as part of MDL-65228. + unset($CFG->forced_plugin_settings['restore']); + // Have finished with the controller, let's destroy it, freeing mem and resources. $controller->destroy();