| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 3 | //
 | 
        
           |  |  | 4 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 5 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 6 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 7 | // (at your option) any later version.
 | 
        
           |  |  | 8 | //
 | 
        
           |  |  | 9 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 12 | // GNU General Public License for more details.
 | 
        
           |  |  | 13 | //
 | 
        
           |  |  | 14 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 15 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | namespace core_backup;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use mod_quiz\quiz_attempt;
 | 
        
           |  |  | 20 | use mod_quiz\quiz_settings;
 | 
        
           |  |  | 21 |   | 
        
           |  |  | 22 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 | global $CFG;
 | 
        
           |  |  | 25 | require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
 | 
        
           |  |  | 26 | require_once($CFG->libdir . "/badgeslib.php");
 | 
        
           |  |  | 27 | require_once($CFG->dirroot . '/mod/assign/tests/base_test.php');
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * Restore date tests.
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @package    core_backup
 | 
        
           |  |  | 33 |  * @copyright  2017 Adrian Greeve <adrian@moodle.com>
 | 
        
           |  |  | 34 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 35 |  */
 | 
        
           | 1441 | ariadna | 36 | final class restore_stepslib_date_test extends \restore_date_testcase {
 | 
        
           | 1 | efrain | 37 |   | 
        
           |  |  | 38 |     /**
 | 
        
           |  |  | 39 |      * Restoring a manual grade item does not result in the timecreated or
 | 
        
           |  |  | 40 |      * timemodified dates being changed.
 | 
        
           |  |  | 41 |      */
 | 
        
           | 11 | efrain | 42 |     public function test_grade_item_date_restore(): void {
 | 
        
           | 1 | efrain | 43 |   | 
        
           |  |  | 44 |         $course = $this->getDataGenerator()->create_course(['startdate' => time()]);
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |         $params = new \stdClass();
 | 
        
           |  |  | 47 |         $params->courseid = $course->id;
 | 
        
           |  |  | 48 |         $params->fullname = 'unittestgradecalccategory';
 | 
        
           |  |  | 49 |         $params->aggregation = GRADE_AGGREGATE_MEAN;
 | 
        
           |  |  | 50 |         $params->aggregateonlygraded = 0;
 | 
        
           |  |  | 51 |         $gradecategory = new \grade_category($params, false);
 | 
        
           |  |  | 52 |         $gradecategory->insert();
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |         $gradecategory->load_grade_item();
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         $gradeitems = new \grade_item();
 | 
        
           |  |  | 57 |         $gradeitems->courseid = $course->id;
 | 
        
           |  |  | 58 |         $gradeitems->categoryid = $gradecategory->id;
 | 
        
           |  |  | 59 |         $gradeitems->itemname = 'manual grade_item';
 | 
        
           |  |  | 60 |         $gradeitems->itemtype = 'manual';
 | 
        
           |  |  | 61 |         $gradeitems->itemnumber = 0;
 | 
        
           |  |  | 62 |         $gradeitems->needsupdate = false;
 | 
        
           |  |  | 63 |         $gradeitems->gradetype = GRADE_TYPE_VALUE;
 | 
        
           |  |  | 64 |         $gradeitems->grademin = 0;
 | 
        
           |  |  | 65 |         $gradeitems->grademax = 10;
 | 
        
           |  |  | 66 |         $gradeitems->iteminfo = 'Manual grade item used for unit testing';
 | 
        
           |  |  | 67 |         $gradeitems->timecreated = time();
 | 
        
           |  |  | 68 |         $gradeitems->timemodified = time();
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |         $gradeitems->aggregationcoef = GRADE_AGGREGATE_SUM;
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         $gradeitems->insert();
 | 
        
           |  |  | 73 |   | 
        
           |  |  | 74 |         $gradeitemparams = [
 | 
        
           |  |  | 75 |             'itemtype' => 'manual',
 | 
        
           |  |  | 76 |             'itemname' => $gradeitems->itemname,
 | 
        
           |  |  | 77 |             'courseid' => $course->id,
 | 
        
           |  |  | 78 |         ];
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         $gradeitem = \grade_item::fetch($gradeitemparams);
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 |         // Do backup and restore.
 | 
        
           |  |  | 83 |   | 
        
           |  |  | 84 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 85 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 86 |         $newgradeitemparams = [
 | 
        
           |  |  | 87 |             'itemtype' => 'manual',
 | 
        
           |  |  | 88 |             'itemname' => $gradeitems->itemname,
 | 
        
           |  |  | 89 |             'courseid' => $course->id,
 | 
        
           |  |  | 90 |         ];
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 |         $newgradeitem = \grade_item::fetch($newgradeitemparams);
 | 
        
           |  |  | 93 |         $this->assertEquals($gradeitem->timecreated, $newgradeitem->timecreated);
 | 
        
           |  |  | 94 |         $this->assertEquals($gradeitem->timemodified, $newgradeitem->timemodified);
 | 
        
           |  |  | 95 |     }
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |     /**
 | 
        
           |  |  | 98 |      * The course section timemodified date does not get rolled forward
 | 
        
           |  |  | 99 |      * when the course is restored.
 | 
        
           |  |  | 100 |      */
 | 
        
           | 11 | efrain | 101 |     public function test_course_section_date_restore(): void {
 | 
        
           | 1 | efrain | 102 |         global $DB;
 | 
        
           |  |  | 103 |         // Create a course.
 | 
        
           |  |  | 104 |         $course = $this->getDataGenerator()->create_course(['startdate' => time()]);
 | 
        
           |  |  | 105 |         // Get the second course section.
 | 
        
           |  |  | 106 |         $section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => '1']);
 | 
        
           |  |  | 107 |         // Do a backup and restore.
 | 
        
           |  |  | 108 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 109 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 |         $newsection = $DB->get_record('course_sections', ['course' => $newcourse->id, 'section' => '1']);
 | 
        
           |  |  | 112 |         // Compare dates.
 | 
        
           |  |  | 113 |         $this->assertEquals($section->timemodified, $newsection->timemodified);
 | 
        
           |  |  | 114 |     }
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |     /**
 | 
        
           |  |  | 117 |      * Test that the timecreated and timemodified dates are not rolled forward when restoring
 | 
        
           |  |  | 118 |      * badge data.
 | 
        
           |  |  | 119 |      */
 | 
        
           | 11 | efrain | 120 |     public function test_badge_date_restore(): void {
 | 
        
           | 1 | efrain | 121 |         global $DB, $USER;
 | 
        
           |  |  | 122 |         // Create a course.
 | 
        
           |  |  | 123 |         $course = $this->getDataGenerator()->create_course(['startdate' => time()]);
 | 
        
           |  |  | 124 |         // Create a badge.
 | 
        
           |  |  | 125 |         $fordb = new \stdClass();
 | 
        
           |  |  | 126 |         $fordb->id = null;
 | 
        
           |  |  | 127 |         $fordb->name = "Test badge";
 | 
        
           |  |  | 128 |         $fordb->description = "Testing badges";
 | 
        
           |  |  | 129 |         $fordb->timecreated = time();
 | 
        
           |  |  | 130 |         $fordb->timemodified = time();
 | 
        
           |  |  | 131 |         $fordb->usercreated = $USER->id;
 | 
        
           |  |  | 132 |         $fordb->usermodified = $USER->id;
 | 
        
           |  |  | 133 |         $fordb->issuername = "Test issuer";
 | 
        
           |  |  | 134 |         $fordb->issuerurl = "http://issuer-url.domain.co.nz";
 | 
        
           |  |  | 135 |         $fordb->issuercontact = "issuer@example.com";
 | 
        
           |  |  | 136 |         $fordb->expiredate = time();
 | 
        
           |  |  | 137 |         $fordb->expireperiod = null;
 | 
        
           |  |  | 138 |         $fordb->type = BADGE_TYPE_COURSE;
 | 
        
           |  |  | 139 |         $fordb->courseid = $course->id;
 | 
        
           |  |  | 140 |         $fordb->messagesubject = "Test message subject";
 | 
        
           |  |  | 141 |         $fordb->message = "Test message body";
 | 
        
           |  |  | 142 |         $fordb->attachment = 1;
 | 
        
           |  |  | 143 |         $fordb->notification = 0;
 | 
        
           |  |  | 144 |         $fordb->status = BADGE_STATUS_INACTIVE;
 | 
        
           |  |  | 145 |         $fordb->nextcron = time();
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         $DB->insert_record('badge', $fordb, true);
 | 
        
           |  |  | 148 |         // Do a backup and restore.
 | 
        
           |  |  | 149 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 150 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 151 |   | 
        
           |  |  | 152 |         $badges = badges_get_badges(BADGE_TYPE_COURSE, $newcourseid);
 | 
        
           |  |  | 153 |   | 
        
           |  |  | 154 |         // Compare dates.
 | 
        
           |  |  | 155 |         $badge = array_shift($badges);
 | 
        
           |  |  | 156 |         $this->assertEquals($fordb->timecreated, $badge->timecreated);
 | 
        
           |  |  | 157 |         $this->assertEquals($fordb->timemodified, $badge->timemodified);
 | 
        
           |  |  | 158 |         $this->assertEquals($fordb->nextcron, $badge->nextcron);
 | 
        
           |  |  | 159 |         // Expire date should be moved forward.
 | 
        
           |  |  | 160 |         $this->assertNotEquals($fordb->expiredate, $badge->expiredate);
 | 
        
           |  |  | 161 |     }
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |     /**
 | 
        
           |  |  | 164 |      * Test that course calendar events timemodified field is not rolled forward
 | 
        
           |  |  | 165 |      * when restoring the course.
 | 
        
           |  |  | 166 |      */
 | 
        
           | 11 | efrain | 167 |     public function test_calendarevents_date_restore(): void {
 | 
        
           | 1 | efrain | 168 |         global $USER, $DB;
 | 
        
           |  |  | 169 |         // Create course.
 | 
        
           |  |  | 170 |         $course = $this->getDataGenerator()->create_course(['startdate' => time()]);
 | 
        
           |  |  | 171 |         // Create calendar event.
 | 
        
           |  |  | 172 |         $starttime = time();
 | 
        
           |  |  | 173 |         $event = [
 | 
        
           |  |  | 174 |                 'name' => 'Start of assignment',
 | 
        
           |  |  | 175 |                 'description' => '',
 | 
        
           |  |  | 176 |                 'format' => 1,
 | 
        
           |  |  | 177 |                 'courseid' => $course->id,
 | 
        
           |  |  | 178 |                 'groupid' => 0,
 | 
        
           |  |  | 179 |                 'userid' => $USER->id,
 | 
        
           |  |  | 180 |                 'modulename' => 0,
 | 
        
           |  |  | 181 |                 'instance' => 0,
 | 
        
           |  |  | 182 |                 'eventtype' => 'course',
 | 
        
           |  |  | 183 |                 'timestart' => $starttime,
 | 
        
           |  |  | 184 |                 'timeduration' => 86400,
 | 
        
           |  |  | 185 |                 'visible' => 1
 | 
        
           |  |  | 186 |         ];
 | 
        
           |  |  | 187 |         $calendarevent = \calendar_event::create($event, false);
 | 
        
           |  |  | 188 |   | 
        
           |  |  | 189 |         // Backup and restore.
 | 
        
           |  |  | 190 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 191 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 192 |   | 
        
           |  |  | 193 |         $newevent = $DB->get_record('event', ['courseid' => $newcourseid, 'eventtype' => 'course']);
 | 
        
           |  |  | 194 |         // Compare dates.
 | 
        
           |  |  | 195 |         $this->assertEquals($calendarevent->timemodified, $newevent->timemodified);
 | 
        
           |  |  | 196 |         $this->assertNotEquals($calendarevent->timestart, $newevent->timestart);
 | 
        
           |  |  | 197 |     }
 | 
        
           |  |  | 198 |   | 
        
           |  |  | 199 |     /**
 | 
        
           |  |  | 200 |      * Testing that the timeenrolled, timestarted, and timecompleted fields are not rolled forward / back
 | 
        
           |  |  | 201 |      * when doing a course restore.
 | 
        
           |  |  | 202 |      */
 | 
        
           | 11 | efrain | 203 |     public function test_course_completion_date_restore(): void {
 | 
        
           | 1 | efrain | 204 |         global $DB;
 | 
        
           |  |  | 205 |   | 
        
           |  |  | 206 |         // Create course with course completion enabled.
 | 
        
           |  |  | 207 |         $course = $this->getDataGenerator()->create_course(['startdate' => time(), 'enablecompletion' => 1]);
 | 
        
           |  |  | 208 |   | 
        
           |  |  | 209 |         // Enrol a user in the course.
 | 
        
           |  |  | 210 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 211 |         $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 | 
        
           |  |  | 212 |         $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
 | 
        
           |  |  | 213 |         // Complete the course with a user.
 | 
        
           |  |  | 214 |         $ccompletion = new \completion_completion(['course' => $course->id,
 | 
        
           |  |  | 215 |                                                   'userid' => $user->id,
 | 
        
           |  |  | 216 |                                                   'timeenrolled' => time(),
 | 
        
           |  |  | 217 |                                                   'timestarted' => time()
 | 
        
           |  |  | 218 |                                                 ]);
 | 
        
           |  |  | 219 |         // Now, mark the course as completed.
 | 
        
           |  |  | 220 |         $ccompletion->mark_complete();
 | 
        
           |  |  | 221 |         $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
 | 
        
           |  |  | 222 |   | 
        
           |  |  | 223 |         // Back up and restore.
 | 
        
           |  |  | 224 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 225 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 226 |   | 
        
           |  |  | 227 |         $newcompletion = \completion_completion::fetch(['course' => $newcourseid, 'userid' => $user->id]);
 | 
        
           |  |  | 228 |   | 
        
           |  |  | 229 |         // Compare dates.
 | 
        
           |  |  | 230 |         $this->assertEquals($ccompletion->timeenrolled, $newcompletion->timeenrolled);
 | 
        
           |  |  | 231 |         $this->assertEquals($ccompletion->timestarted, $newcompletion->timestarted);
 | 
        
           |  |  | 232 |         $this->assertEquals($ccompletion->timecompleted, $newcompletion->timecompleted);
 | 
        
           |  |  | 233 |     }
 | 
        
           |  |  | 234 |   | 
        
           |  |  | 235 |     /**
 | 
        
           |  |  | 236 |      * Testing that the grade grade date information is not changed in the gradebook when a course
 | 
        
           |  |  | 237 |      * restore is performed.
 | 
        
           |  |  | 238 |      */
 | 
        
           | 11 | efrain | 239 |     public function test_grade_grade_date_restore(): void {
 | 
        
           | 1 | efrain | 240 |         global $USER, $DB;
 | 
        
           |  |  | 241 |         // Testing the restore of an overridden grade.
 | 
        
           |  |  | 242 |         list($course, $assign) = $this->create_course_and_module('assign', []);
 | 
        
           |  |  | 243 |         $cm = $DB->get_record('course_modules', ['course' => $course->id, 'instance' => $assign->id]);
 | 
        
           |  |  | 244 |         $assignobj = new \mod_assign_testable_assign(\context_module::instance($cm->id), $cm, $course);
 | 
        
           |  |  | 245 |         $submission = $assignobj->get_user_submission($USER->id, true);
 | 
        
           |  |  | 246 |         $grade = $assignobj->get_user_grade($USER->id, true);
 | 
        
           |  |  | 247 |         $grade->grade = 75;
 | 
        
           |  |  | 248 |         $assignobj->update_grade($grade);
 | 
        
           |  |  | 249 |   | 
        
           |  |  | 250 |         // Find the grade item.
 | 
        
           |  |  | 251 |         $gradeitemparams = [
 | 
        
           |  |  | 252 |             'itemtype' => 'mod',
 | 
        
           |  |  | 253 |             'iteminstance' => $assign->id,
 | 
        
           |  |  | 254 |             'itemmodule' => 'assign',
 | 
        
           |  |  | 255 |             'courseid' => $course->id,
 | 
        
           |  |  | 256 |         ];
 | 
        
           |  |  | 257 |         $gradeitem = \grade_item::fetch($gradeitemparams);
 | 
        
           |  |  | 258 |   | 
        
           |  |  | 259 |         // Next the grade grade.
 | 
        
           |  |  | 260 |         $gradegrade = \grade_grade::fetch(['itemid' => $gradeitem->id, 'userid' => $USER->id]);
 | 
        
           |  |  | 261 |         $gradegrade->set_overridden(true);
 | 
        
           |  |  | 262 |   | 
        
           |  |  | 263 |         // Back up and restore.
 | 
        
           |  |  | 264 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 265 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 266 |   | 
        
           |  |  | 267 |         // Find assignment.
 | 
        
           |  |  | 268 |         $assignid = $DB->get_field('assign', 'id', ['course' => $newcourseid]);
 | 
        
           |  |  | 269 |         // Find grade item.
 | 
        
           |  |  | 270 |         $newgradeitemparams = [
 | 
        
           |  |  | 271 |             'itemtype' => 'mod',
 | 
        
           |  |  | 272 |             'iteminstance' => $assignid,
 | 
        
           |  |  | 273 |             'itemmodule' => 'assign',
 | 
        
           |  |  | 274 |             'courseid' => $newcourse->id,
 | 
        
           |  |  | 275 |         ];
 | 
        
           |  |  | 276 |   | 
        
           |  |  | 277 |         $newgradeitem = \grade_item::fetch($newgradeitemparams);
 | 
        
           |  |  | 278 |         // Find grade grade.
 | 
        
           |  |  | 279 |         $newgradegrade = \grade_grade::fetch(['itemid' => $newgradeitem->id, 'userid' => $USER->id]);
 | 
        
           |  |  | 280 |         // Compare dates.
 | 
        
           |  |  | 281 |         $this->assertEquals($gradegrade->timecreated, $newgradegrade->timecreated);
 | 
        
           |  |  | 282 |         $this->assertEquals($gradegrade->timemodified, $newgradegrade->timemodified);
 | 
        
           |  |  | 283 |         $this->assertEquals($gradegrade->overridden, $newgradegrade->overridden);
 | 
        
           |  |  | 284 |     }
 | 
        
           |  |  | 285 |   | 
        
           |  |  | 286 |     /**
 | 
        
           |  |  | 287 |      * Checking that the user completion of an activity relating to the timemodified field does not change
 | 
        
           |  |  | 288 |      * when doing a course restore.
 | 
        
           |  |  | 289 |      */
 | 
        
           | 11 | efrain | 290 |     public function test_usercompletion_date_restore(): void {
 | 
        
           | 1 | efrain | 291 |         global $USER, $DB;
 | 
        
           |  |  | 292 |         // More completion...
 | 
        
           |  |  | 293 |         $course = $this->getDataGenerator()->create_course(['startdate' => time(), 'enablecompletion' => 1]);
 | 
        
           |  |  | 294 |         $assign = $this->getDataGenerator()->create_module('assign', [
 | 
        
           |  |  | 295 |                 'course' => $course->id,
 | 
        
           |  |  | 296 |                 'completion' => COMPLETION_TRACKING_AUTOMATIC, // Show activity as complete when conditions are met.
 | 
        
           |  |  | 297 |                 'completionusegrade' => 1 // Student must receive a grade to complete this activity.
 | 
        
           |  |  | 298 |             ]);
 | 
        
           |  |  | 299 |         $cm = $DB->get_record('course_modules', ['course' => $course->id, 'instance' => $assign->id]);
 | 
        
           |  |  | 300 |         $assignobj = new \mod_assign_testable_assign(\context_module::instance($cm->id), $cm, $course);
 | 
        
           |  |  | 301 |         $submission = $assignobj->get_user_submission($USER->id, true);
 | 
        
           |  |  | 302 |         $grade = $assignobj->get_user_grade($USER->id, true);
 | 
        
           |  |  | 303 |         $grade->grade = 75;
 | 
        
           |  |  | 304 |         $assignobj->update_grade($grade);
 | 
        
           |  |  | 305 |   | 
        
           |  |  | 306 |         $coursemodulecompletion = $DB->get_record('course_modules_completion', ['coursemoduleid' => $cm->id]);
 | 
        
           |  |  | 307 |   | 
        
           |  |  | 308 |         // Back up and restore.
 | 
        
           |  |  | 309 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 310 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 311 |   | 
        
           |  |  | 312 |         // Find assignment.
 | 
        
           |  |  | 313 |         $assignid = $DB->get_field('assign', 'id', ['course' => $newcourseid]);
 | 
        
           |  |  | 314 |         $cm = $DB->get_record('course_modules', ['course' => $newcourse->id, 'instance' => $assignid]);
 | 
        
           |  |  | 315 |         $newcoursemodulecompletion = $DB->get_record('course_modules_completion', ['coursemoduleid' => $cm->id]);
 | 
        
           |  |  | 316 |   | 
        
           |  |  | 317 |         $this->assertEquals($coursemodulecompletion->timemodified, $newcoursemodulecompletion->timemodified);
 | 
        
           |  |  | 318 |     }
 | 
        
           |  |  | 319 |   | 
        
           |  |  | 320 |     /**
 | 
        
           |  |  | 321 |      * Checking that the user completion of an activity relating to the view field does not change
 | 
        
           |  |  | 322 |      * when doing a course restore.
 | 
        
           | 1441 | ariadna | 323 |      * @covers \backup_userscompletion_structure_step
 | 
        
           |  |  | 324 |      * @covers \restore_userscompletion_structure_step
 | 
        
           | 1 | efrain | 325 |      */
 | 
        
           | 11 | efrain | 326 |     public function test_usercompletion_view_restore(): void {
 | 
        
           | 1 | efrain | 327 |         global $DB;
 | 
        
           |  |  | 328 |         // More completion...
 | 
        
           |  |  | 329 |         $course = $this->getDataGenerator()->create_course(['startdate' => time(), 'enablecompletion' => 1]);
 | 
        
           |  |  | 330 |         $student = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 331 |         $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student');
 | 
        
           |  |  | 332 |         $assign = $this->getDataGenerator()->create_module('assign', [
 | 
        
           |  |  | 333 |             'course' => $course->id,
 | 
        
           |  |  | 334 |             'completion' => COMPLETION_TRACKING_AUTOMATIC, // Show activity as complete when conditions are met.
 | 
        
           |  |  | 335 |             'completionview' => 1
 | 
        
           |  |  | 336 |         ]);
 | 
        
           |  |  | 337 |         $cm = $DB->get_record('course_modules', ['course' => $course->id, 'instance' => $assign->id]);
 | 
        
           |  |  | 338 |   | 
        
           |  |  | 339 |         // Mark the activity as completed.
 | 
        
           |  |  | 340 |         $completion = new \completion_info($course);
 | 
        
           |  |  | 341 |         $completion->set_module_viewed($cm, $student->id);
 | 
        
           |  |  | 342 |   | 
        
           |  |  | 343 |         $coursemodulecompletion = $DB->get_record('course_modules_viewed', ['coursemoduleid' => $cm->id]);
 | 
        
           |  |  | 344 |   | 
        
           |  |  | 345 |         // Back up and restore.
 | 
        
           |  |  | 346 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 347 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 348 |   | 
        
           |  |  | 349 |         $assignid = $DB->get_field('assign', 'id', ['course' => $newcourseid]);
 | 
        
           |  |  | 350 |         $cm = $DB->get_record('course_modules', ['course' => $newcourse->id, 'instance' => $assignid]);
 | 
        
           |  |  | 351 |         $newcoursemodulecompletion = $DB->get_record('course_modules_viewed', ['coursemoduleid' => $cm->id]);
 | 
        
           |  |  | 352 |   | 
        
           |  |  | 353 |         $this->assertEquals($coursemodulecompletion->timecreated, $newcoursemodulecompletion->timecreated);
 | 
        
           |  |  | 354 |     }
 | 
        
           |  |  | 355 |   | 
        
           |  |  | 356 |     /**
 | 
        
           |  |  | 357 |      * Ensuring that the timemodified field of the question attempt steps table does not change when
 | 
        
           |  |  | 358 |      * a course restore is done.
 | 
        
           |  |  | 359 |      */
 | 
        
           | 11 | efrain | 360 |     public function test_question_attempt_steps_date_restore(): void {
 | 
        
           | 1 | efrain | 361 |         global $DB;
 | 
        
           |  |  | 362 |   | 
        
           |  |  | 363 |         $course = $this->getDataGenerator()->create_course(['startdate' => time()]);
 | 
        
           |  |  | 364 |         // Make a quiz.
 | 
        
           |  |  | 365 |         $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 | 
        
           |  |  | 366 |   | 
        
           |  |  | 367 |         $quiz = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0, 'grade' => 100.0,
 | 
        
           |  |  | 368 |                                                       'sumgrades' => 2));
 | 
        
           |  |  | 369 |   | 
        
           |  |  | 370 |         $cm = $DB->get_record('course_modules', ['course' => $course->id, 'instance' => $quiz->id]);
 | 
        
           |  |  | 371 |   | 
        
           |  |  | 372 |         // Create a couple of questions.
 | 
        
           |  |  | 373 |         $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 | 
        
           |  |  | 374 |   | 
        
           |  |  | 375 |         $cat = $questiongenerator->create_question_category();
 | 
        
           |  |  | 376 |         $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 | 
        
           |  |  | 377 |         $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
 | 
        
           |  |  | 378 |   | 
        
           |  |  | 379 |         // Add them to the quiz.
 | 
        
           |  |  | 380 |         quiz_add_quiz_question($saq->id, $quiz);
 | 
        
           |  |  | 381 |         quiz_add_quiz_question($numq->id, $quiz);
 | 
        
           |  |  | 382 |   | 
        
           |  |  | 383 |         // Make a user to do the quiz.
 | 
        
           |  |  | 384 |         $user1 = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 385 |   | 
        
           |  |  | 386 |         $quizobj = quiz_settings::create($quiz->id, $user1->id);
 | 
        
           |  |  | 387 |   | 
        
           |  |  | 388 |         // Start the attempt.
 | 
        
           |  |  | 389 |         $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 | 
        
           |  |  | 390 |         $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 | 
        
           |  |  | 391 |   | 
        
           |  |  | 392 |         $timenow = time();
 | 
        
           |  |  | 393 |         $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id);
 | 
        
           |  |  | 394 |   | 
        
           |  |  | 395 |         quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
 | 
        
           |  |  | 396 |   | 
        
           |  |  | 397 |         quiz_attempt_save_started($quizobj, $quba, $attempt);
 | 
        
           |  |  | 398 |   | 
        
           |  |  | 399 |         // Process some responses from the student.
 | 
        
           |  |  | 400 |         $attemptobj = quiz_attempt::create($attempt->id);
 | 
        
           |  |  | 401 |   | 
        
           |  |  | 402 |         $prefix1 = $quba->get_field_prefix(1);
 | 
        
           |  |  | 403 |         $prefix2 = $quba->get_field_prefix(2);
 | 
        
           |  |  | 404 |   | 
        
           |  |  | 405 |         $tosubmit = array(1 => array('answer' => 'frog'),
 | 
        
           |  |  | 406 |                           2 => array('answer' => '3.14'));
 | 
        
           |  |  | 407 |   | 
        
           |  |  | 408 |         $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
 | 
        
           |  |  | 409 |   | 
        
           |  |  | 410 |         // Finish the attempt.
 | 
        
           |  |  | 411 |         $attemptobj = quiz_attempt::create($attempt->id);
 | 
        
           | 1441 | ariadna | 412 |         $attemptobj->process_submit($timenow, false);
 | 
        
           |  |  | 413 |         $attemptobj->process_grade_submission($timenow);
 | 
        
           | 1 | efrain | 414 |   | 
        
           |  |  | 415 |         $questionattemptstepdates = [];
 | 
        
           | 1441 | ariadna | 416 |         $originaliterator = $attemptobj->get_question_usage()->get_attempt_iterator();
 | 
        
           | 1 | efrain | 417 |         foreach ($originaliterator as $questionattempt) {
 | 
        
           |  |  | 418 |             $questionattemptstepdates[] = ['originaldate' => $questionattempt->get_last_action_time()];
 | 
        
           |  |  | 419 |         }
 | 
        
           |  |  | 420 |   | 
        
           |  |  | 421 |         // Back up and restore.
 | 
        
           |  |  | 422 |         $newcourseid = $this->backup_and_restore($course);
 | 
        
           |  |  | 423 |         $newcourse = get_course($newcourseid);
 | 
        
           |  |  | 424 |   | 
        
           |  |  | 425 |         // Get the quiz for this new restored course.
 | 
        
           |  |  | 426 |         $quizdata = $DB->get_record('quiz', ['course' => $newcourseid]);
 | 
        
           |  |  | 427 |         $quizobj = \mod_quiz\quiz_settings::create($quizdata->id, $user1->id);
 | 
        
           |  |  | 428 |   | 
        
           |  |  | 429 |         $questionusage = $DB->get_record('question_usages', [
 | 
        
           |  |  | 430 |                 'component' => 'mod_quiz',
 | 
        
           |  |  | 431 |                 'contextid' => $quizobj->get_context()->id
 | 
        
           |  |  | 432 |             ]);
 | 
        
           |  |  | 433 |   | 
        
           |  |  | 434 |         $newquba = \question_engine::load_questions_usage_by_activity($questionusage->id);
 | 
        
           |  |  | 435 |   | 
        
           |  |  | 436 |         $restorediterator = $newquba->get_attempt_iterator();
 | 
        
           |  |  | 437 |         $i = 0;
 | 
        
           |  |  | 438 |         foreach ($restorediterator as $restoredquestionattempt) {
 | 
        
           |  |  | 439 |             $questionattemptstepdates[$i]['restoredate'] = $restoredquestionattempt->get_last_action_time();
 | 
        
           |  |  | 440 |             $i++;
 | 
        
           |  |  | 441 |         }
 | 
        
           |  |  | 442 |   | 
        
           |  |  | 443 |         foreach ($questionattemptstepdates as $dates) {
 | 
        
           |  |  | 444 |             $this->assertEquals($dates['originaldate'], $dates['restoredate']);
 | 
        
           |  |  | 445 |         }
 | 
        
           |  |  | 446 |     }
 | 
        
           |  |  | 447 | }
 |