Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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   */
  36  class restore_stepslib_date_test extends \restore_date_testcase {
  37  
  38      /**
  39       * Restoring a manual grade item does not result in the timecreated or
  40       * timemodified dates being changed.
  41       */
  42      public function test_grade_item_date_restore() {
  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       */
 101      public function test_course_section_date_restore() {
 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       */
 120      public function test_badge_date_restore() {
 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       */
 167      public function test_calendarevents_date_restore() {
 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       */
 203      public function test_course_completion_date_restore() {
 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       */
 239      public function test_grade_grade_date_restore() {
 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       */
 290      public function test_usercompletion_date_restore() {
 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.
 323       * @covers ::backup_and_restore
 324       */
 325      public function test_usercompletion_view_restore() {
 326          global $DB;
 327          // More completion...
 328          $course = $this->getDataGenerator()->create_course(['startdate' => time(), 'enablecompletion' => 1]);
 329          $student = $this->getDataGenerator()->create_user();
 330          $this->getDataGenerator()->enrol_user($student->id, $course->id, 'student');
 331          $assign = $this->getDataGenerator()->create_module('assign', [
 332              'course' => $course->id,
 333              'completion' => COMPLETION_TRACKING_AUTOMATIC, // Show activity as complete when conditions are met.
 334              'completionview' => 1
 335          ]);
 336          $cm = $DB->get_record('course_modules', ['course' => $course->id, 'instance' => $assign->id]);
 337  
 338          // Mark the activity as completed.
 339          $completion = new \completion_info($course);
 340          $completion->set_module_viewed($cm, $student->id);
 341  
 342          $coursemodulecompletion = $DB->get_record('course_modules_viewed', ['coursemoduleid' => $cm->id]);
 343  
 344          // Back up and restore.
 345          $newcourseid = $this->backup_and_restore($course);
 346          $newcourse = get_course($newcourseid);
 347  
 348          $assignid = $DB->get_field('assign', 'id', ['course' => $newcourseid]);
 349          $cm = $DB->get_record('course_modules', ['course' => $newcourse->id, 'instance' => $assignid]);
 350          $newcoursemodulecompletion = $DB->get_record('course_modules_viewed', ['coursemoduleid' => $cm->id]);
 351  
 352          $this->assertEquals($coursemodulecompletion->timecreated, $newcoursemodulecompletion->timecreated);
 353      }
 354  
 355      /**
 356       * Ensuring that the timemodified field of the question attempt steps table does not change when
 357       * a course restore is done.
 358       */
 359      public function test_question_attempt_steps_date_restore() {
 360          global $DB;
 361  
 362          $course = $this->getDataGenerator()->create_course(['startdate' => time()]);
 363          // Make a quiz.
 364          $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
 365  
 366          $quiz = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0, 'grade' => 100.0,
 367                                                        'sumgrades' => 2));
 368  
 369          $cm = $DB->get_record('course_modules', ['course' => $course->id, 'instance' => $quiz->id]);
 370  
 371          // Create a couple of questions.
 372          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 373  
 374          $cat = $questiongenerator->create_question_category();
 375          $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
 376          $numq = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
 377  
 378          // Add them to the quiz.
 379          quiz_add_quiz_question($saq->id, $quiz);
 380          quiz_add_quiz_question($numq->id, $quiz);
 381  
 382          // Make a user to do the quiz.
 383          $user1 = $this->getDataGenerator()->create_user();
 384  
 385          $quizobj = quiz_settings::create($quiz->id, $user1->id);
 386  
 387          // Start the attempt.
 388          $quba = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
 389          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
 390  
 391          $timenow = time();
 392          $attempt = quiz_create_attempt($quizobj, 1, false, $timenow, false, $user1->id);
 393  
 394          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timenow);
 395  
 396          quiz_attempt_save_started($quizobj, $quba, $attempt);
 397  
 398          // Process some responses from the student.
 399          $attemptobj = quiz_attempt::create($attempt->id);
 400  
 401          $prefix1 = $quba->get_field_prefix(1);
 402          $prefix2 = $quba->get_field_prefix(2);
 403  
 404          $tosubmit = array(1 => array('answer' => 'frog'),
 405                            2 => array('answer' => '3.14'));
 406  
 407          $attemptobj->process_submitted_actions($timenow, false, $tosubmit);
 408  
 409          // Finish the attempt.
 410          $attemptobj = quiz_attempt::create($attempt->id);
 411          $attemptobj->process_finish($timenow, false);
 412  
 413          $questionattemptstepdates = [];
 414          $originaliterator = $quba->get_attempt_iterator();
 415          foreach ($originaliterator as $questionattempt) {
 416              $questionattemptstepdates[] = ['originaldate' => $questionattempt->get_last_action_time()];
 417          }
 418  
 419          // Back up and restore.
 420          $newcourseid = $this->backup_and_restore($course);
 421          $newcourse = get_course($newcourseid);
 422  
 423          // Get the quiz for this new restored course.
 424          $quizdata = $DB->get_record('quiz', ['course' => $newcourseid]);
 425          $quizobj = \mod_quiz\quiz_settings::create($quizdata->id, $user1->id);
 426  
 427          $questionusage = $DB->get_record('question_usages', [
 428                  'component' => 'mod_quiz',
 429                  'contextid' => $quizobj->get_context()->id
 430              ]);
 431  
 432          $newquba = \question_engine::load_questions_usage_by_activity($questionusage->id);
 433  
 434          $restorediterator = $newquba->get_attempt_iterator();
 435          $i = 0;
 436          foreach ($restorediterator as $restoredquestionattempt) {
 437              $questionattemptstepdates[$i]['restoredate'] = $restoredquestionattempt->get_last_action_time();
 438              $i++;
 439          }
 440  
 441          foreach ($questionattemptstepdates as $dates) {
 442              $this->assertEquals($dates['originaldate'], $dates['restoredate']);
 443          }
 444      }
 445  }