Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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