Merge branch 'MDL-51434-master' of git://github.com/jleyva/moodle
[moodle.git] / lib / tests / questionlib_test.php
blob6455616711f13f35a7cc1d8572d2a4eed04b5584
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 (some of) ../questionlib.php.
20 * @package core_question
21 * @category phpunit
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();
28 global $CFG;
30 require_once($CFG->libdir . '/questionlib.php');
31 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
32 require_once($CFG->dirroot . '/tag/lib.php');
34 // Get the necessary files to perform backup and restore.
35 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
36 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
38 /**
39 * Unit tests for (some of) ../questionlib.php.
41 * @copyright 2006 The Open University
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class core_questionlib_testcase extends advanced_testcase {
46 /**
47 * Test set up.
49 * This is executed before running any test in this file.
51 public function setUp() {
52 $this->resetAfterTest();
55 /**
56 * Tidy up open files that may be left open.
58 protected function tearDown() {
59 gc_collect_cycles();
62 /**
63 * Return true and false to test functions with feedback on and off.
65 * @return array Test data
67 public function provider_feedback() {
68 return array(
69 'Feedback test' => array(true),
70 'No feedback test' => array(false)
74 /**
75 * Setup a course, a quiz, a question category and a question for testing.
77 * @param string $type The type of question category to create.
78 * @return array The created data objects
80 public function setup_quiz_and_questions($type = 'module') {
81 // Create course category.
82 $category = $this->getDataGenerator()->create_category();
84 // Create course.
85 $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
87 $options = array(
88 'course' => $course->id,
89 'duedate' => time(),
92 // Generate an assignment with due date (will generate a course event).
93 $quiz = $this->getDataGenerator()->create_module('quiz', $options);
95 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
97 if ('course' == $type) {
98 $context = context_course::instance($course->id);
99 } else if ('category' == $type) {
100 $context = context_coursecat::instance($category->id);
101 } else {
102 $context = context_module::instance($quiz->cmid);
105 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
107 $questions = array(
108 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
109 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
112 quiz_add_quiz_question($questions[0]->id, $quiz);
114 return array($category, $course, $quiz, $qcat, $questions);
117 public function test_question_reorder_qtypes() {
118 $this->assertEquals(
119 array(0 => 't2', 1 => 't1', 2 => 't3'),
120 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1));
121 $this->assertEquals(
122 array(0 => 't1', 1 => 't2', 2 => 't3'),
123 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', -1));
124 $this->assertEquals(
125 array(0 => 't2', 1 => 't1', 2 => 't3'),
126 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't2', -1));
127 $this->assertEquals(
128 array(0 => 't1', 1 => 't2', 2 => 't3'),
129 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't3', +1));
130 $this->assertEquals(
131 array(0 => 't1', 1 => 't2', 2 => 't3'),
132 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 'missing', +1));
135 public function test_match_grade_options() {
136 $gradeoptions = question_bank::fraction_options_full();
138 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'error'));
139 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'error'));
140 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'error'));
141 $this->assertFalse(match_grade_options($gradeoptions, 0.3333, 'error'));
143 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'nearest'));
144 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'nearest'));
145 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'nearest'));
146 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33, 'nearest'));
148 $this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
152 * This function tests that the functions responsible for moving questions to
153 * different contexts also updates the tag instances associated with the questions.
155 public function test_altering_tag_instance_context() {
156 global $CFG, $DB;
158 // Set to admin user.
159 $this->setAdminUser();
161 // Create two course categories - we are going to delete one of these later and will expect
162 // all the questions belonging to the course in the deleted category to be moved.
163 $coursecat1 = $this->getDataGenerator()->create_category();
164 $coursecat2 = $this->getDataGenerator()->create_category();
166 // Create a couple of categories and questions.
167 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
168 $questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
169 context_coursecat::instance($coursecat1->id)->id));
170 $questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
171 context_coursecat::instance($coursecat2->id)->id));
172 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
173 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
174 $question3 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
175 $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
177 // Now lets tag these questions.
178 tag_set('question', $question1->id, array('tag 1', 'tag 2'), 'core_question', $questioncat1->contextid);
179 tag_set('question', $question2->id, array('tag 3', 'tag 4'), 'core_question', $questioncat1->contextid);
180 tag_set('question', $question3->id, array('tag 5', 'tag 6'), 'core_question', $questioncat2->contextid);
181 tag_set('question', $question4->id, array('tag 7', 'tag 8'), 'core_question', $questioncat2->contextid);
183 // Test moving the questions to another category.
184 question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id);
186 // Test that all tag_instances belong to one context.
187 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
188 'contextid' => $questioncat2->contextid)));
190 // Test moving them back.
191 question_move_questions_to_category(array($question1->id, $question2->id), $questioncat1->id);
193 // Test that all tag_instances are now reset to how they were initially.
194 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
195 'contextid' => $questioncat1->contextid)));
196 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
197 'contextid' => $questioncat2->contextid)));
199 // Now test moving a whole question category to another context.
200 question_move_category_to_context($questioncat1->id, $questioncat1->contextid, $questioncat2->contextid);
202 // Test that all tag_instances belong to one context.
203 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
204 'contextid' => $questioncat2->contextid)));
206 // Now test moving them back.
207 question_move_category_to_context($questioncat1->id, $questioncat2->contextid,
208 context_coursecat::instance($coursecat1->id)->id);
210 // Test that all tag_instances are now reset to how they were initially.
211 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
212 'contextid' => $questioncat1->contextid)));
213 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
214 'contextid' => $questioncat2->contextid)));
216 // Now we want to test deleting the course category and moving the questions to another category.
217 question_delete_course_category($coursecat1, $coursecat2, false);
219 // Test that all tag_instances belong to one context.
220 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
221 'contextid' => $questioncat2->contextid)));
223 // Create a course.
224 $course = $this->getDataGenerator()->create_course();
226 // Create some question categories and questions in this course.
227 $questioncat = $questiongenerator->create_question_category(array('contextid' =>
228 context_course::instance($course->id)->id));
229 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
230 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
232 // Add some tags to these questions.
233 tag_set('question', $question1->id, array('tag 1', 'tag 2'), 'core_question', $questioncat->contextid);
234 tag_set('question', $question2->id, array('tag 1', 'tag 2'), 'core_question', $questioncat->contextid);
236 // Create a course that we are going to restore the other course to.
237 $course2 = $this->getDataGenerator()->create_course();
239 // Create backup file and save it to the backup location.
240 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
241 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
242 $bc->execute_plan();
243 $results = $bc->get_results();
244 $file = $results['backup_destination'];
245 $fp = get_file_packer('application/vnd.moodle.backup');
246 $filepath = $CFG->dataroot . '/temp/backup/test-restore-course';
247 $file->extract_to_pathname($fp, $filepath);
248 $bc->destroy();
249 unset($bc);
251 // Now restore the course.
252 $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO,
253 backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE);
254 $rc->execute_precheck();
255 $rc->execute_plan();
257 // Get the created question category.
258 $restoredcategory = $DB->get_record('question_categories', array('contextid' => context_course::instance($course2->id)->id),
259 '*', MUST_EXIST);
261 // Check that there are two questions in the restored to course's context.
262 $this->assertEquals(2, $DB->count_records('question', array('category' => $restoredcategory->id)));
266 * This function tests the question_category_delete_safe function.
268 public function test_question_category_delete_safe() {
269 global $DB;
270 $this->resetAfterTest(true);
271 $this->setAdminUser();
273 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
275 question_category_delete_safe($qcat);
277 // Verify category deleted.
278 $criteria = array('id' => $qcat->id);
279 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
281 // Verify questions deleted or moved.
282 $criteria = array('category' => $qcat->id);
283 $this->assertEquals(0, $DB->count_records('question', $criteria));
285 // Verify question not deleted.
286 $criteria = array('id' => $questions[0]->id);
287 $this->assertEquals(1, $DB->count_records('question', $criteria));
291 * This function tests the question_delete_activity function.
293 * @param bool $feedback Whether to return feedback
294 * @dataProvider provider_feedback
296 public function test_question_delete_activity($feedback) {
297 global $DB;
298 $this->resetAfterTest(true);
299 $this->setAdminUser();
301 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
303 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
304 // Test that the feedback works.
305 if ($feedback) {
306 $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
308 question_delete_activity($cm, $feedback);
310 // Verify category deleted.
311 $criteria = array('id' => $qcat->id);
312 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
314 // Verify questions deleted or moved.
315 $criteria = array('category' => $qcat->id);
316 $this->assertEquals(0, $DB->count_records('question', $criteria));
320 * This function tests the question_delete_context function.
322 public function test_question_delete_context() {
323 global $DB;
324 $this->resetAfterTest(true);
325 $this->setAdminUser();
327 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
329 // Get the module context id.
330 $result = question_delete_context($qcat->contextid);
332 // Verify category deleted.
333 $criteria = array('id' => $qcat->id);
334 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
336 // Verify questions deleted or moved.
337 $criteria = array('category' => $qcat->id);
338 $this->assertEquals(0, $DB->count_records('question', $criteria));
340 // Test that the feedback works.
341 $expected[] = array($qcat->name, get_string('unusedcategorydeleted', 'question'));
342 $this->assertEquals($expected, $result);
346 * This function tests the question_delete_course function.
348 * @param bool $feedback Whether to return feedback
349 * @dataProvider provider_feedback
351 public function test_question_delete_course($feedback) {
352 global $DB;
353 $this->resetAfterTest(true);
354 $this->setAdminUser();
356 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
358 // Test that the feedback works.
359 if ($feedback) {
360 $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
362 question_delete_course($course, $feedback);
364 // Verify category deleted.
365 $criteria = array('id' => $qcat->id);
366 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
368 // Verify questions deleted or moved.
369 $criteria = array('category' => $qcat->id);
370 $this->assertEquals(0, $DB->count_records('question', $criteria));
374 * This function tests the question_delete_course_category function.
376 * @param bool $feedback Whether to return feedback
377 * @dataProvider provider_feedback
379 public function test_question_delete_course_category($feedback) {
380 global $DB;
381 $this->resetAfterTest(true);
382 $this->setAdminUser();
384 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
386 // Test that the feedback works.
387 if ($feedback) {
388 $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
390 question_delete_course_category($category, 0, $feedback);
392 // Verify category deleted.
393 $criteria = array('id' => $qcat->id);
394 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
396 // Verify questions deleted or moved.
397 $criteria = array('category' => $qcat->id);
398 $this->assertEquals(0, $DB->count_records('question', $criteria));