weekly release 3.6dev
[moodle.git] / question / tests / backup_test.php
CommitLineData
7c33ba47
SR
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 * Unit tests for question backup and restore.
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
30require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
31
32/**
33 * Class core_question_backup_testcase
34 *
35 * @copyright 2018 Shamim Rezaie <shamim@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class core_question_backup_testcase extends advanced_testcase {
39
40 /**
41 * Makes a backup of the course.
42 *
43 * @param stdClass $course The course object.
44 * @return string Unique identifier for this backup.
45 */
46 protected function backup_course($course) {
47 global $CFG, $USER;
48
49 // Turn off file logging, otherwise it can't delete the file (Windows).
50 $CFG->backup_file_logger_level = backup::LOG_NONE;
51
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();
60
61 return $backupid;
62 }
63
64 /**
65 * Restores a backup that has been made earlier.
66 *
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.
73 */
74 protected function restore_course($backupid, $fullname, $shortname, $categoryid, $expectedprecheckwarning = []) {
75 global $CFG, $USER;
76
77 // Turn off file logging, otherwise it can't delete the file (Windows).
78 $CFG->backup_file_logger_level = backup::LOG_NONE;
79
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);
85
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);
92 }
93 $rc->execute_plan();
94 $rc->destroy();
95
96 return $newcourseid;
97 }
98
99 /**
100 * This function tests backup and restore of question tags and course level question tags.
101 */
102 public function test_backup_question_tags() {
103 global $DB;
104
105 $this->resetAfterTest();
106 $this->setAdminUser();
107
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;
113
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));
120
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']);
128
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);
132
133 // Backup the course twice for future use.
134 $backupid1 = $this->backup_course($course);
135 $backupid2 = $this->backup_course($course);
136
137 // Now delete almost everything.
138 delete_course($course, false);
139 question_delete_question($question1->id);
140 question_delete_question($question2->id);
141
142 // Restore the backup we had made earlier into a new course.
143 $courseid2 = $this->restore_course($backupid1, $coursefullname, $courseshortname . '_2', $category1->id);
144
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);
149
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);
153
154 // Each question is tagged with 4 tags (2 question tags + 2 course tags).
155 $this->assertCount(4, $tags);
156
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;
162 }
163 $this->assertEquals($expected, $tag->taginstancecontextid);
164 }
165 }
166
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);
171 }
172 $category1->delete_full(false);
173
174 // Create a new course category to restore the backup file into it.
175 $category2 = $this->getDataGenerator()->create_category();
176
177 $expectedwarnings = array(
178 get_string('qcategory2coursefallback', 'backup', (object) ['name' => 'top']),
179 get_string('qcategory2coursefallback', 'backup', (object) ['name' => $qcat->name])
180 );
181
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);
185
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.*
188 FROM {question} q
189 JOIN {question_categories} qc ON q.category = qc.id
190 WHERE qc.contextid = ?", array($coursecontext3->id));
191 $this->assertCount(2, $questions);
192
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);
196
197 // Each question is tagged with 4 tags (all are course tags now).
198 $this->assertCount(4, $tags);
199
200 foreach ($tags as $tag) {
201 $this->assertEquals($coursecontext3->id, $tag->taginstancecontextid);
202 }
203 }
204
205 }
206}