Merge branch 'MDL-70422-310' of https://github.com/paulholden/moodle into MOODLE_310_...
[moodle.git] / backup / tests / quiz_restore_decode_links_test.php
CommitLineData
d83bf6e0
IT
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
25defined('MOODLE_INTERNAL') || die();
26
27// Include all the needed stuff.
28global $CFG;
29require_once($CFG->dirroot . '/course/lib.php');
30require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
31require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
32
33/**
34 * restore_decode tests (both rule and content)
35 */
36class 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
c8e335fb 85 $sql = "SELECT qa.id, qa.answer
d83bf6e0
IT
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);
c8e335fb
PH
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]);
d83bf6e0
IT
97 }
98}