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