Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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