Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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_match;
  18  
  19  use question_bank;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  25  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  26  require_once($CFG->dirroot . '/course/externallib.php');
  27  
  28  /**
  29   * Tests for the matching question type backup and restore logic.
  30   *
  31   * @package   qtype_match
  32   * @copyright 2020 The Open University
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  34   */
  35  class backup_test extends \advanced_testcase {
  36  
  37      /**
  38       * Duplicate quiz with a matching question, and check it worked.
  39       */
  40      public function test_duplicate_match_question() {
  41          global $DB;
  42          $this->resetAfterTest();
  43          $this->setAdminUser();
  44  
  45          $coregenerator = $this->getDataGenerator();
  46          $questiongenerator = $coregenerator->get_plugin_generator('core_question');
  47  
  48          // Create a course with a page that embeds a question.
  49          $course = $coregenerator->create_course();
  50          $quiz = $coregenerator->create_module('quiz', ['course' => $course->id]);
  51          $quizcontext = \context_module::instance($quiz->cmid);
  52  
  53          $cat = $questiongenerator->create_question_category(['contextid' => $quizcontext->id]);
  54          $question = $questiongenerator->create_question('match', 'trickynums', ['category' => $cat->id]);
  55  
  56          // Store some counts.
  57          $numquizzes = count(get_fast_modinfo($course)->instances['quiz']);
  58          $nummatchquestions = $DB->count_records('question', ['qtype' => 'match']);
  59  
  60          // Duplicate the page.
  61          duplicate_module($course, get_fast_modinfo($course)->get_cm($quiz->cmid));
  62  
  63          // Verify the copied quiz exists.
  64          $this->assertCount($numquizzes + 1, get_fast_modinfo($course)->instances['quiz']);
  65  
  66          // Verify the copied question.
  67          $this->assertEquals($nummatchquestions + 1, $DB->count_records('question', ['qtype' => 'match']));
  68          $newmatchid = $DB->get_field_sql("
  69                  SELECT MAX(id)
  70                    FROM {question}
  71                   WHERE qtype = ?
  72                  ", ['match']);
  73          $matchdata = question_bank::load_question_data($newmatchid);
  74  
  75          $subquestions = array_values($matchdata->options->subquestions);
  76  
  77          $this->assertSame('System.out.println(0);', $subquestions[0]->questiontext);
  78          $this->assertSame('0', $subquestions[0]->answertext);
  79  
  80          $this->assertSame('System.out.println(0.0);', $subquestions[1]->questiontext);
  81          $this->assertSame('0.0', $subquestions[1]->answertext);
  82  
  83          $this->assertSame('', $subquestions[2]->questiontext);
  84          $this->assertSame('NULL', $subquestions[2]->answertext);
  85      }
  86  }