Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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