2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for question backup and restore.
20 * @package core_question
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();
29 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
30 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
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
{
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) {
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
,
57 $backupid = $bc->get_backupid();
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 = []) {
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);
90 $precheckresults = $rc->get_precheck_results();
91 $this->assertEquals(['warnings' => $expectedprecheckwarning], $precheckresults);
100 * This function tests backup and restore of question tags and course level question tags.
102 public function test_backup_question_tags() {
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
));
119 $question2 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
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
));
148 $this->assertCount(2, $questions);
150 // Retrieve tags for each question and check if they are assigned at the right context.
151 foreach ($questions as $question) {
152 $tags = core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
154 // Each question is tagged with 4 tags (2 question tags + 2 course tags).
155 $this->assertCount(4, $tags);
157 foreach ($tags as $tag) {
158 if (in_array($tag->name
, ['ctag1', 'ctag2', 'ctag3', 'ctag4'])) {
159 $expected = context_course
::instance($courseid2)->id
;
160 } else if (in_array($tag->name
, ['qtag1', 'qtag2', 'qtag3', 'qtag4'])) {
161 $expected = $qcontext->id
;
163 $this->assertEquals($expected, $tag->taginstancecontextid
);
167 // Now, again, delete everything including the course category.
168 delete_course($courseid2, false);
169 foreach ($questions as $question) {
170 question_delete_question($question->id
);
172 $category1->delete_full(false);
174 // Create a new course category to restore the backup file into it.
175 $category2 = $this->getDataGenerator()->create_category();
177 $expectedwarnings = array(
178 get_string('qcategory2coursefallback', 'backup', (object) ['name' => 'top']),
179 get_string('qcategory2coursefallback', 'backup', (object) ['name' => $qcat->name
])
182 // Restore to a new course in the new course category.
183 $courseid3 = $this->restore_course($backupid2, $coursefullname, $courseshortname . '_3', $category2->id
, $expectedwarnings);
184 $coursecontext3 = context_course
::instance($courseid3);
186 // The questions should have been moved to a question category that belongs to a course context.
187 $questions = $DB->get_records_sql("SELECT q.*
189 JOIN {question_categories} qc ON q.category = qc.id
190 WHERE qc.contextid = ?", array($coursecontext3->id
));
191 $this->assertCount(2, $questions);
193 // Now, retrieve tags for each question and check if they are assigned at the right context.
194 foreach ($questions as $question) {
195 $tags = core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
197 // Each question is tagged with 4 tags (all are course tags now).
198 $this->assertCount(4, $tags);
200 foreach ($tags as $tag) {
201 $this->assertEquals($coursecontext3->id
, $tag->taginstancecontextid
);