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