Merge branch 'MDL-64173-master' of git://github.com/bmbrands/moodle
[moodle.git] / question / tests / backup_test.php
blob389880cfd0f92ef98aee2081a3c2eb8bdf039ee3
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.
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/>.
17 /**
18 * Unit tests for question backup and restore.
20 * @package core_question
21 * @category test
22 * @copyright 2018 Shamim Rezaie <shamim@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
30 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
32 /**
33 * Class core_question_backup_testcase
35 * @copyright 2018 Shamim Rezaie <shamim@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_question_backup_testcase extends advanced_testcase {
40 /**
41 * Makes a backup of the course.
43 * @param stdClass $course The course object.
44 * @return string Unique identifier for this backup.
46 protected function backup_course($course) {
47 global $CFG, $USER;
49 // Turn off file logging, otherwise it can't delete the file (Windows).
50 $CFG->backup_file_logger_level = backup::LOG_NONE;
52 // Do backup with default settings. MODE_IMPORT means it will just
53 // create the directory and not zip it.
54 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id,
55 backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
56 $USER->id);
57 $backupid = $bc->get_backupid();
58 $bc->execute_plan();
59 $bc->destroy();
61 return $backupid;
64 /**
65 * Restores a backup that has been made earlier.
67 * @param string $backupid The unique identifier of the backup.
68 * @param string $fullname Full name of the new course that is going to be created.
69 * @param string $shortname Short name of the new course that is going to be created.
70 * @param int $categoryid The course category the backup is going to be restored in.
71 * @param string[] $expectedprecheckwarning
72 * @return int The new course id.
74 protected function restore_course($backupid, $fullname, $shortname, $categoryid, $expectedprecheckwarning = []) {
75 global $CFG, $USER;
77 // Turn off file logging, otherwise it can't delete the file (Windows).
78 $CFG->backup_file_logger_level = backup::LOG_NONE;
80 // Do restore to new course with default settings.
81 $newcourseid = restore_dbops::create_new_course($fullname, $shortname, $categoryid);
82 $rc = new restore_controller($backupid, $newcourseid,
83 backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
84 backup::TARGET_NEW_COURSE);
86 $precheck = $rc->execute_precheck();
87 if (!$expectedprecheckwarning) {
88 $this->assertTrue($precheck);
89 } else {
90 $precheckresults = $rc->get_precheck_results();
91 $this->assertEquals(['warnings' => $expectedprecheckwarning], $precheckresults);
93 $rc->execute_plan();
94 $rc->destroy();
96 return $newcourseid;
99 /**
100 * This function tests backup and restore of question tags and course level question tags.
102 public function test_backup_question_tags() {
103 global $DB;
105 $this->resetAfterTest();
106 $this->setAdminUser();
108 // Create a new course category and and a new course in that.
109 $category1 = $this->getDataGenerator()->create_category();
110 $course = $this->getDataGenerator()->create_course(array('category' => $category1->id));
111 $courseshortname = $course->shortname;
112 $coursefullname = $course->fullname;
114 // Create 2 questions.
115 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
116 $context = context_coursecat::instance($category1->id);
117 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
118 $question1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id, 'idnumber' => 'q1'));
119 $question2 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id, 'idnumber' => 'q2'));
121 // Tag the questions with 2 question tags and 2 course level question tags.
122 $qcontext = context::instance_by_id($qcat->contextid);
123 $coursecontext = context_course::instance($course->id);
124 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['qtag1', 'qtag2']);
125 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['qtag3', 'qtag4']);
126 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag1', 'ctag2']);
127 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag3', 'ctag4']);
129 // Create a quiz and add one of the questions to that.
130 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
131 quiz_add_quiz_question($question1->id, $quiz);
133 // Backup the course twice for future use.
134 $backupid1 = $this->backup_course($course);
135 $backupid2 = $this->backup_course($course);
137 // Now delete almost everything.
138 delete_course($course, false);
139 question_delete_question($question1->id);
140 question_delete_question($question2->id);
142 // Restore the backup we had made earlier into a new course.
143 $courseid2 = $this->restore_course($backupid1, $coursefullname, $courseshortname . '_2', $category1->id);
145 // The questions should remain in the question category they were which is
146 // a question category belonging to a course category context.
147 $questions = $DB->get_records('question', array('category' => $qcat->id), 'idnumber');
148 $this->assertCount(2, $questions);
150 // Retrieve tags for each question and check if they are assigned at the right context.
151 $qcount = 1;
152 foreach ($questions as $question) {
153 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
155 // Each question is tagged with 4 tags (2 question tags + 2 course tags).
156 $this->assertCount(4, $tags);
158 foreach ($tags as $tag) {
159 if (in_array($tag->name, ['ctag1', 'ctag2', 'ctag3', 'ctag4'])) {
160 $expected = context_course::instance($courseid2)->id;
161 } else if (in_array($tag->name, ['qtag1', 'qtag2', 'qtag3', 'qtag4'])) {
162 $expected = $qcontext->id;
164 $this->assertEquals($expected, $tag->taginstancecontextid);
167 // Also check idnumbers have been backed up and restored.
168 $this->assertEquals('q' . $qcount, $question->idnumber);
169 $qcount++;
172 // Now, again, delete everything including the course category.
173 delete_course($courseid2, false);
174 foreach ($questions as $question) {
175 question_delete_question($question->id);
177 $category1->delete_full(false);
179 // Create a new course category to restore the backup file into it.
180 $category2 = $this->getDataGenerator()->create_category();
182 $expectedwarnings = array(
183 get_string('qcategory2coursefallback', 'backup', (object) ['name' => 'top']),
184 get_string('qcategory2coursefallback', 'backup', (object) ['name' => $qcat->name])
187 // Restore to a new course in the new course category.
188 $courseid3 = $this->restore_course($backupid2, $coursefullname, $courseshortname . '_3', $category2->id, $expectedwarnings);
189 $coursecontext3 = context_course::instance($courseid3);
191 // The questions should have been moved to a question category that belongs to a course context.
192 $questions = $DB->get_records_sql("SELECT q.*
193 FROM {question} q
194 JOIN {question_categories} qc ON q.category = qc.id
195 WHERE qc.contextid = ?", array($coursecontext3->id));
196 $this->assertCount(2, $questions);
198 // Now, retrieve tags for each question and check if they are assigned at the right context.
199 foreach ($questions as $question) {
200 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
202 // Each question is tagged with 4 tags (all are course tags now).
203 $this->assertCount(4, $tags);
205 foreach ($tags as $tag) {
206 $this->assertEquals($coursecontext3->id, $tag->taginstancecontextid);