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.
   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    mod_quiz
  21   * @copyright  2017 onwards Ankit Agarwal <ankit.agrr@gmail.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  
  30  /**
  31   * Restore date tests.
  32   *
  33   * @package    mod_quiz
  34   * @copyright  2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_quiz_restore_date_testcase extends restore_date_testcase
  38  {
  39  
  40      /**
  41       * Test restore dates.
  42       */
  43      public function test_restore_dates() {
  44          global $DB, $USER;
  45  
  46          // Create quiz data.
  47          $record = ['timeopen' => 100, 'timeclose' => 100, 'timemodified' => 100, 'tiemcreated' => 100, 'questionsperpage' => 0,
  48              'grade' => 100.0, 'sumgrades' => 2];
  49          list($course, $quiz) = $this->create_course_and_module('quiz', $record);
  50  
  51          // Create questions.
  52          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  53          $cat = $questiongenerator->create_question_category();
  54          $saq = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
  55          // Add to the quiz.
  56          quiz_add_quiz_question($saq->id, $quiz);
  57  
  58          // Create an attempt.
  59          $timestamp = 100;
  60          $quizobj = quiz::create($quiz->id);
  61          $attempt = quiz_create_attempt($quizobj, 1, false, $timestamp, false);
  62          $quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
  63          $quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
  64          quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timestamp);
  65          quiz_attempt_save_started($quizobj, $quba, $attempt);
  66  
  67          // Quiz grade.
  68          $grade = new stdClass();
  69          $grade->quiz = $quiz->id;
  70          $grade->userid = $USER->id;
  71          $grade->grade = 8.9;
  72          $grade->timemodified = $timestamp;
  73          $grade->id = $DB->insert_record('quiz_grades', $grade);
  74  
  75          // User override.
  76          $override = (object)[
  77              'quiz' => $quiz->id,
  78              'groupid' => 0,
  79              'userid' => $USER->id,
  80              'sortorder' => 1,
  81              'timeopen' => 100,
  82              'timeclose' => 200
  83          ];
  84          $DB->insert_record('quiz_overrides', $override);
  85  
  86          // Set time fields to a constant for easy validation.
  87          $DB->set_field('quiz_attempts', 'timefinish', $timestamp);
  88  
  89          // Do backup and restore.
  90          $newcourseid = $this->backup_and_restore($course);
  91          $newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
  92  
  93          $this->assertFieldsNotRolledForward($quiz, $newquiz, ['timecreated', 'timemodified']);
  94          $props = ['timeclose', 'timeopen'];
  95          $this->assertFieldsRolledForward($quiz, $newquiz, $props);
  96  
  97          $newattempt = $DB->get_record('quiz_attempts', ['quiz' => $newquiz->id]);
  98          $newoverride = $DB->get_record('quiz_overrides', ['quiz' => $newquiz->id]);
  99          $newgrade = $DB->get_record('quiz_grades', ['quiz' => $newquiz->id]);
 100  
 101          // Attempt time checks.
 102          $diff = $this->get_diff();
 103          $this->assertEquals($timestamp, $newattempt->timemodified);
 104          $this->assertEquals($timestamp, $newattempt->timefinish);
 105          $this->assertEquals($timestamp, $newattempt->timestart);
 106          $this->assertEquals($timestamp + $diff, $newattempt->timecheckstate); // Should this be rolled?
 107  
 108          // Quiz override time checks.
 109          $diff = $this->get_diff();
 110          $this->assertEquals($override->timeopen + $diff, $newoverride->timeopen);
 111          $this->assertEquals($override->timeclose + $diff, $newoverride->timeclose);
 112  
 113          // Quiz grade time checks.
 114          $this->assertEquals($grade->timemodified, $newgrade->timemodified);
 115      }
 116  }