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 (some of) ../questionlib.php.
20 * @package core_question
22 * @copyright 2006 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir . '/questionlib.php');
31 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
33 // Get the necessary files to perform backup and restore.
34 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
35 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
38 * Unit tests for (some of) ../questionlib.php.
40 * @copyright 2006 The Open University
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class core_questionlib_testcase extends advanced_testcase {
48 * This is executed before running any test in this file.
50 public function setUp() {
51 $this->resetAfterTest();
55 * Tidy up open files that may be left open.
57 protected function tearDown() {
62 * Return true and false to test functions with feedback on and off.
64 * @return array Test data
66 public function provider_feedback() {
68 'Feedback test' => array(true),
69 'No feedback test' => array(false)
74 * Setup a course, a quiz, a question category and a question for testing.
76 * @param string $type The type of question category to create.
77 * @return array The created data objects
79 public function setup_quiz_and_questions($type = 'module') {
80 // Create course category.
81 $category = $this->getDataGenerator()->create_category();
84 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
87 'course' => $course->id,
91 // Generate an assignment with due date (will generate a course event).
92 $quiz = $this->getDataGenerator()->create_module('quiz', $options);
94 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
96 if ('course' == $type) {
97 $context = context_course::instance($course->id);
98 } else if ('category' == $type) {
99 $context = context_coursecat::instance($category->id);
101 $context = context_module::instance($quiz->cmid);
104 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
107 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
108 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
111 quiz_add_quiz_question($questions[0]->id, $quiz);
113 return array($category, $course, $quiz, $qcat, $questions);
116 public function test_question_reorder_qtypes() {
118 array(0 => 't2', 1 => 't1', 2 => 't3'),
119 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1));
121 array(0 => 't1', 1 => 't2', 2 => 't3'),
122 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', -1));
124 array(0 => 't2', 1 => 't1', 2 => 't3'),
125 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't2', -1));
127 array(0 => 't1', 1 => 't2', 2 => 't3'),
128 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't3', +1));
130 array(0 => 't1', 1 => 't2', 2 => 't3'),
131 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 'missing', +1));
134 public function test_match_grade_options() {
135 $gradeoptions = question_bank::fraction_options_full();
137 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'error'));
138 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'error'));
139 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'error'));
140 $this->assertFalse(match_grade_options($gradeoptions, 0.3333, 'error'));
142 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'nearest'));
143 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'nearest'));
144 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'nearest'));
145 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33, 'nearest'));
147 $this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
151 * This function tests that the functions responsible for moving questions to
152 * different contexts also updates the tag instances associated with the questions.
154 public function test_altering_tag_instance_context() {
157 // Set to admin user.
158 $this->setAdminUser();
160 // Create two course categories - we are going to delete one of these later and will expect
161 // all the questions belonging to the course in the deleted category to be moved.
162 $coursecat1 = $this->getDataGenerator()->create_category();
163 $coursecat2 = $this->getDataGenerator()->create_category();
165 // Create a couple of categories and questions.
166 $context1 = context_coursecat::instance($coursecat1->id);
167 $context2 = context_coursecat::instance($coursecat2->id);
168 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
169 $questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
171 $questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
173 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
174 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
175 $question3 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
176 $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
178 // Now lets tag these questions.
179 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $context1, array('tag 1', 'tag 2'));
180 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $context1, array('tag 3', 'tag 4'));
181 core_tag_tag::set_item_tags('core_question', 'question', $question3->id, $context2, array('tag 5', 'tag 6'));
182 core_tag_tag::set_item_tags('core_question', 'question', $question4->id, $context2, array('tag 7', 'tag 8'));
184 // Test moving the questions to another category.
185 question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id);
187 // Test that all tag_instances belong to one context.
188 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
189 'contextid' => $questioncat2->contextid)));
191 // Test moving them back.
192 question_move_questions_to_category(array($question1->id, $question2->id), $questioncat1->id);
194 // Test that all tag_instances are now reset to how they were initially.
195 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
196 'contextid' => $questioncat1->contextid)));
197 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
198 'contextid' => $questioncat2->contextid)));
200 // Now test moving a whole question category to another context.
201 question_move_category_to_context($questioncat1->id, $questioncat1->contextid, $questioncat2->contextid);
203 // Test that all tag_instances belong to one context.
204 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
205 'contextid' => $questioncat2->contextid)));
207 // Now test moving them back.
208 question_move_category_to_context($questioncat1->id, $questioncat2->contextid,
209 context_coursecat::instance($coursecat1->id)->id);
211 // Test that all tag_instances are now reset to how they were initially.
212 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
213 'contextid' => $questioncat1->contextid)));
214 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
215 'contextid' => $questioncat2->contextid)));
217 // Now we want to test deleting the course category and moving the questions to another category.
218 question_delete_course_category($coursecat1, $coursecat2, false);
220 // Test that all tag_instances belong to one context.
221 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
222 'contextid' => $questioncat2->contextid)));
225 $course = $this->getDataGenerator()->create_course();
227 // Create some question categories and questions in this course.
228 $coursecontext = context_course::instance($course->id);
229 $questioncat = $questiongenerator->create_question_category(array('contextid' =>
230 $coursecontext->id));
231 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
232 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
234 // Add some tags to these questions.
235 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, array('tag 1', 'tag 2'));
236 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, array('tag 1', 'tag 2'));
238 // Create a course that we are going to restore the other course to.
239 $course2 = $this->getDataGenerator()->create_course();
241 // Create backup file and save it to the backup location.
242 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
243 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
245 $results = $bc->get_results();
246 $file = $results['backup_destination'];
247 $fp = get_file_packer('application/vnd.moodle.backup');
248 $filepath = $CFG->dataroot . '/temp/backup/test-restore-course';
249 $file->extract_to_pathname($fp, $filepath);
253 // Now restore the course.
254 $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO,
255 backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE);
256 $rc->execute_precheck();
259 // Get the created question category.
260 $restoredcategory = $DB->get_record('question_categories', array('contextid' => context_course::instance($course2->id)->id),
263 // Check that there are two questions in the restored to course's context.
264 $this->assertEquals(2, $DB->count_records('question', array('category' => $restoredcategory->id)));
268 * This function tests the question_category_delete_safe function.
270 public function test_question_category_delete_safe() {
272 $this->resetAfterTest(true);
273 $this->setAdminUser();
275 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
277 question_category_delete_safe($qcat);
279 // Verify category deleted.
280 $criteria = array('id' => $qcat->id);
281 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
283 // Verify questions deleted or moved.
284 $criteria = array('category' => $qcat->id);
285 $this->assertEquals(0, $DB->count_records('question', $criteria));
287 // Verify question not deleted.
288 $criteria = array('id' => $questions[0]->id);
289 $this->assertEquals(1, $DB->count_records('question', $criteria));
293 * This function tests the question_delete_activity function.
295 * @param bool $feedback Whether to return feedback
296 * @dataProvider provider_feedback
298 public function test_question_delete_activity($feedback) {
300 $this->resetAfterTest(true);
301 $this->setAdminUser();
303 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
305 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
306 // Test that the feedback works.
308 $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
310 question_delete_activity($cm, $feedback);
312 // Verify category deleted.
313 $criteria = array('id' => $qcat->id);
314 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
316 // Verify questions deleted or moved.
317 $criteria = array('category' => $qcat->id);
318 $this->assertEquals(0, $DB->count_records('question', $criteria));
322 * This function tests the question_delete_context function.
324 public function test_question_delete_context() {
326 $this->resetAfterTest(true);
327 $this->setAdminUser();
329 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
331 // Get the module context id.
332 $result = question_delete_context($qcat->contextid);
334 // Verify category deleted.
335 $criteria = array('id' => $qcat->id);
336 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
338 // Verify questions deleted or moved.
339 $criteria = array('category' => $qcat->id);
340 $this->assertEquals(0, $DB->count_records('question', $criteria));
342 // Test that the feedback works.
343 $expected[] = array($qcat->name, get_string('unusedcategorydeleted', 'question'));
344 $this->assertEquals($expected, $result);
348 * This function tests the question_delete_course function.
350 * @param bool $feedback Whether to return feedback
351 * @dataProvider provider_feedback
353 public function test_question_delete_course($feedback) {
355 $this->resetAfterTest(true);
356 $this->setAdminUser();
358 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
360 // Test that the feedback works.
362 $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
364 question_delete_course($course, $feedback);
366 // Verify category deleted.
367 $criteria = array('id' => $qcat->id);
368 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
370 // Verify questions deleted or moved.
371 $criteria = array('category' => $qcat->id);
372 $this->assertEquals(0, $DB->count_records('question', $criteria));
376 * This function tests the question_delete_course_category function.
378 * @param bool $feedback Whether to return feedback
379 * @dataProvider provider_feedback
381 public function test_question_delete_course_category($feedback) {
383 $this->resetAfterTest(true);
384 $this->setAdminUser();
386 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
388 // Test that the feedback works.
390 $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
392 question_delete_course_category($category, 0, $feedback);
394 // Verify category deleted.
395 $criteria = array('id' => $qcat->id);
396 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
398 // Verify questions deleted or moved.
399 $criteria = array('category' => $qcat->id);
400 $this->assertEquals(0, $DB->count_records('question', $criteria));