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]

   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 qtype_essay;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . "/phpunit/classes/restore_date_testcase.php");
  23  
  24  /**
  25   * Test restore logic.
  26   *
  27   * @package    qtype_essay
  28   * @copyright  2019 The Open University
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class restore_test extends \restore_date_testcase {
  32  
  33      /**
  34       * Test missing qtype_essay_options creation.
  35       *
  36       * Old backup files may contain essays with no qtype_essay_options record.
  37       * During restore, we add default options for any questions like that.
  38       * That is what is tested in this file.
  39       */
  40      public function test_restore_create_missing_qtype_essay_options() {
  41          global $DB;
  42  
  43          // Create a course with one essay question in its question bank.
  44          $generator = $this->getDataGenerator();
  45          $course = $generator->create_course();
  46          $contexts = new \core_question\local\bank\question_edit_contexts(\context_course::instance($course->id));
  47          $category = question_make_default_categories($contexts->all());
  48          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  49          $essay = $questiongenerator->create_question('essay', null, array('category' => $category->id));
  50  
  51          // Remove the options record, which means that the backup will look like a backup made in an old Moodle.
  52          $DB->delete_records('qtype_essay_options', ['questionid' => $essay->id]);
  53  
  54          // Do backup and restore.
  55          $newcourseid = $this->backup_and_restore($course);
  56  
  57          // Verify that the restored question has options.
  58          $contexts = new \core_question\local\bank\question_edit_contexts(\context_course::instance($newcourseid));
  59          $newcategory = question_make_default_categories($contexts->all());
  60          $newessay = $DB->get_record_sql('SELECT q.*
  61                                                FROM {question} q
  62                                                JOIN {question_versions} qv ON qv.questionid = q.id
  63                                                JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
  64                                               WHERE qbe.questioncategoryid = ?
  65                                                 AND q.qtype = ?', [$newcategory->id, 'essay']);
  66          $this->assertTrue($DB->record_exists('qtype_essay_options', ['questionid' => $newessay->id]));
  67      }
  68  }