Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
  23  /**
  24   * Restore override tests.
  25   *
  26   * @package   mod_quiz
  27   * @author    2019 Nathan Nguyen <nathannguyen@catalyst-au.net>
  28   * @copyright Catalyst IT
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class restore_override_test extends \restore_date_testcase {
  32  
  33      /**
  34       * Test restore overrides.
  35       */
  36      public function test_restore_overrides() {
  37          global $DB, $USER;
  38          $this->resetAfterTest();
  39  
  40          $course = $this->getDataGenerator()->create_course();
  41          $quizgen = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
  42          $quiz = $quizgen->create_instance(['course' => $course->id]);
  43  
  44          $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
  45          $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
  46  
  47          $now = 100;
  48  
  49          // Group overrides.
  50          $groupoverride1 = (object)[
  51              'quiz' => $quiz->id,
  52              'groupid' => $group1->id,
  53              'timeopen' => $now,
  54              'timeclose' => $now + 20
  55          ];
  56          $DB->insert_record('quiz_overrides', $groupoverride1);
  57  
  58          $groupoverride2 = (object)[
  59              'quiz' => $quiz->id,
  60              'groupid' => $group2->id,
  61              'timeopen' => $now,
  62              'timeclose' => $now + 40
  63          ];
  64          $DB->insert_record('quiz_overrides', $groupoverride2);
  65  
  66          // Current quiz overrides.
  67          $overrides = $DB->get_records('quiz_overrides', ['quiz' => $quiz->id]);
  68          $this->assertEquals(2, count($overrides));
  69  
  70          // User Override.
  71          $useroverride = (object)[
  72              'quiz' => $quiz->id,
  73              'userid' => $USER->id,
  74              'sortorder' => 1,
  75              'timeopen' => 100,
  76              'timeclose' => 200
  77          ];
  78          $DB->insert_record('quiz_overrides', $useroverride);
  79  
  80          // Current quiz overrides.
  81          $overrides = $DB->get_records('quiz_overrides', ['quiz' => $quiz->id]);
  82          $this->assertEquals(3, count($overrides));
  83  
  84          // Back up and restore including group info and user info.
  85          set_config('backup_general_groups', 1, 'backup');
  86          $newcourseid = $this->backup_and_restore($course);
  87          $newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
  88          $overrides = $DB->get_records('quiz_overrides', ['quiz' => $newquiz->id]);
  89          // 2 groups overrides and 1 user override.
  90          $this->assertEquals(3, count($overrides));
  91  
  92          // Back up and restore with user info and without group info.
  93          set_config('backup_general_groups', 0, 'backup');
  94          $newcourseid = $this->backup_and_restore($course);
  95          $newquiz = $DB->get_record('quiz', ['course' => $newcourseid]);
  96          $overrides = $DB->get_records('quiz_overrides', ['quiz' => $newquiz->id]);
  97          // 1 user override.
  98          $this->assertEquals(1, count($overrides));
  99      }
 100  }