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 core_backup;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  // Include all the needed stuff.
  22  global $CFG;
  23  require_once($CFG->dirroot . '/course/lib.php');
  24  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  25  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  26  
  27  /**
  28   * Decode links quiz restore tests.
  29   *
  30   * @package    core_backup
  31   * @copyright  2020 Ilya Tregubov <mattp@catalyst-au.net>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class quiz_restore_decode_links_test extends \advanced_testcase {
  35  
  36      /**
  37       * Test restore_decode_rule class
  38       */
  39      public function test_restore_quiz_decode_links() {
  40          global $DB, $CFG, $USER;
  41  
  42          $this->resetAfterTest(true);
  43          $this->setAdminUser();
  44  
  45          $generator = $this->getDataGenerator();
  46          $course = $generator->create_course(
  47              array('format' => 'topics', 'numsections' => 3,
  48                  'enablecompletion' => COMPLETION_ENABLED),
  49              array('createsections' => true));
  50          $quiz = $generator->create_module('quiz', array(
  51              'course' => $course->id));
  52  
  53          // Create questions.
  54  
  55          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  56          $context = \context_course::instance($course->id);
  57          $cat = $questiongenerator->create_question_category(array('contextid' => $context->id));
  58          $question = $questiongenerator->create_question('multichoice', null, array('category' => $cat->id));
  59  
  60          // Add to the quiz.
  61          quiz_add_quiz_question($question->id, $quiz);
  62  
  63          $questiondata = \question_bank::load_question_data($question->id);
  64  
  65          $firstanswer = array_shift($questiondata->options->answers);
  66          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/course/view.php?id=' . $course->id,
  67              ['id' => $firstanswer->id]);
  68  
  69          $secondanswer = array_shift($questiondata->options->answers);
  70          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid,
  71              ['id' => $secondanswer->id]);
  72  
  73          $thirdanswer = array_shift($questiondata->options->answers);
  74          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid,
  75              ['id' => $thirdanswer->id]);
  76  
  77          $fourthanswer = array_shift($questiondata->options->answers);
  78          $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid,
  79              ['id' => $fourthanswer->id]);
  80  
  81          $newcm = duplicate_module($course, get_fast_modinfo($course)->get_cm($quiz->cmid));
  82  
  83          $sql = "SELECT qa.id, qa.answer
  84                    FROM {quiz} q
  85               LEFT JOIN {quiz_slots} qs ON qs.quizid = q.id
  86               LEFT JOIN {question_answers} qa ON qa.question = qs.questionid
  87                   WHERE q.id = :quizid";
  88          $params = array('quizid' => $newcm->instance);
  89          $answers = $DB->get_records_sql_menu($sql, $params);
  90  
  91          $this->assertEquals($CFG->wwwroot . '/course/view.php?id=' . $course->id, $answers[$firstanswer->id]);
  92          $this->assertEquals($CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid, $answers[$secondanswer->id]);
  93          $this->assertEquals($CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid, $answers[$thirdanswer->id]);
  94          $this->assertEquals($CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid, $answers[$fourthanswer->id]);
  95      }
  96  }