Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
1441 ariadna 34
final class quiz_restore_decode_links_test extends \advanced_testcase {
1 efrain 35
 
36
    /**
37
     * Test restore_decode_rule class
38
     */
11 efrain 39
    public function test_restore_quiz_decode_links(): void {
1 efrain 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));
1441 ariadna 52
        $qbank = $this->getDataGenerator()->create_module('qbank', ['course' => $course->id]);
1 efrain 53
 
54
        // Create questions.
55
 
56
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
1441 ariadna 57
        $context = \context_module::instance($qbank->cmid);
1 efrain 58
        $cat = $questiongenerator->create_question_category(array('contextid' => $context->id));
59
        $question = $questiongenerator->create_question('multichoice', null, array('category' => $cat->id));
60
 
61
        // Add to the quiz.
62
        quiz_add_quiz_question($question->id, $quiz);
63
        \mod_quiz\external\submit_question_version::execute(
64
                $DB->get_field('quiz_slots', 'id', ['quizid' => $quiz->id, 'slot' => 1]), 1);
65
 
66
        $questiondata = \question_bank::load_question_data($question->id);
67
 
1441 ariadna 68
        $DB->set_field('question', 'questiontext', $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid, ['id' => $question->id]);
69
 
1 efrain 70
        $firstanswer = array_shift($questiondata->options->answers);
71
        $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/course/view.php?id=' . $course->id,
72
            ['id' => $firstanswer->id]);
73
 
74
        $secondanswer = array_shift($questiondata->options->answers);
75
        $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid,
76
            ['id' => $secondanswer->id]);
77
 
78
        $thirdanswer = array_shift($questiondata->options->answers);
79
        $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid,
80
            ['id' => $thirdanswer->id]);
81
 
82
        $fourthanswer = array_shift($questiondata->options->answers);
83
        $DB->set_field('question_answers', 'answer', $CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid,
84
            ['id' => $fourthanswer->id]);
85
 
86
        $newcm = duplicate_module($course, get_fast_modinfo($course)->get_cm($quiz->cmid));
87
 
88
        $quizquestions = \mod_quiz\question\bank\qbank_helper::get_question_structure(
89
                $newcm->instance, \context_module::instance($newcm->id));
90
        $questionids = [];
91
        foreach ($quizquestions as $quizquestion) {
92
            if ($quizquestion->questionid) {
1441 ariadna 93
                $this->assertEquals($CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid, $quizquestion->questiontext);
1 efrain 94
                $questionids[] = $quizquestion->questionid;
95
            }
96
        }
97
        list($condition, $param) = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED, 'questionid');
98
        $condition = 'WHERE qa.question ' . $condition;
99
 
100
        $sql = "SELECT qa.id,
101
                       qa.answer
102
                  FROM {question_answers} qa
103
                  $condition";
104
        $answers = $DB->get_records_sql($sql, $param);
105
 
106
        $this->assertEquals($CFG->wwwroot . '/course/view.php?id=' . $course->id, $answers[$firstanswer->id]->answer);
107
        $this->assertEquals($CFG->wwwroot . '/mod/quiz/view.php?id=' . $quiz->cmid, $answers[$secondanswer->id]->answer);
108
        $this->assertEquals($CFG->wwwroot . '/grade/report/index.php?id=' . $quiz->cmid, $answers[$thirdanswer->id]->answer);
109
        $this->assertEquals($CFG->wwwroot . '/mod/quiz/index.php?id=' . $quiz->cmid, $answers[$fourthanswer->id]->answer);
110
    }
111
}