Proyectos de Subversion Moodle

Rev

| 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 mod_quiz\backup;
18
 
19
use advanced_testcase;
20
use backup;
21
use restore_controller;
22
 
23
/**
24
 * Test restoring 3.9 backups including random questions.
25
 *
26
 * @package   mod_quiz
27
 * @copyright 2024 Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
28
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers    \restore_move_module_questions_categories
30
 */
31
final class restore_39_test extends advanced_testcase {
32
 
33
    public function test_restore_random_question_39(): void {
34
        global $DB, $CFG, $USER;
35
 
36
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
37
 
38
        $this->resetAfterTest();
39
        $this->setAdminUser();
40
 
41
        // The example Moodle 3.9 backup file used in this test is an activity-level backup of a quiz.
42
        // So, the backup contains just the quiz-level question bank which contains:
43
        // | - Question category: Top
44
        // |   - Question category: Default for Test MDL-78902 quiz
45
        // |     - Question: Test MDL-78902 T/F question
46
        // |     - Question: Random (Default for Test MDL-78902 quiz)
47
        // The quiz itself contains 1 question, the random question.
48
        // So, during the restore, the quiz_slot needs to be updated to use a question_set_reference.
49
        $backupfile = 'moodle_39_quiz_with_random_question_from_mod_context';
50
 
51
        // Extract backup file.
52
        $backupid = $backupfile;
53
        $backuppath = make_backup_temp_directory($backupid);
54
        get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
55
            __DIR__ . "/../fixtures/$backupfile.mbz", $backuppath);
56
 
57
        // Restore the quiz activity in the backup from Moodle 3.9 to a new course.
58
        $coursecat = self::getDataGenerator()->create_category();
59
        $course = self::getDataGenerator()->create_course(['category' => $coursecat->id]);
60
        $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_NO,
61
            backup::MODE_GENERAL, $USER->id, backup::TARGET_EXISTING_ADDING);
62
        $this->assertTrue($rc->execute_precheck());
63
        $rc->execute_plan();
64
        $rc->destroy();
65
 
66
        // Get information about the quiz activity and confirm the references are correct.
67
        $modinfo = get_fast_modinfo($course->id);
68
        $quizzes = array_values($modinfo->get_instances_of('quiz'));
69
        // Get contextid for the restored quiz activity.
70
        $contextid = $quizzes[0]->context->id;
71
        $qcats = $DB->get_records('question_categories', ['contextid' => $contextid], 'parent');
72
        // Confirm there are 2 question categories for the restored quiz activity.
73
        $this->assertEquals(['top', 'Default for Test MDL-78902 quiz'], array_column($qcats, 'name'));
74
        // Get question_set_references records for the restored quiz activity.
75
        $references = $DB->get_records('question_set_references', ['usingcontextid' => $contextid]);
76
        foreach ($references as $reference) {
77
            $filtercondition = json_decode($reference->filtercondition);
78
            // Confirm the questionscontextid is set correctly, which is from filter question category id.
79
            $this->assertEquals($reference->questionscontextid,
80
                $qcats[$filtercondition->questioncategoryid]->contextid);
81
        }
82
    }
83
}