MDL-50851 core_question: use new tag API
[moodle.git] / lib / tests / questionlib_test.php
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/>.
17 /**
18  * Unit tests for (some of) ../questionlib.php.
19  *
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
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
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');
37 /**
38  * Unit tests for (some of) ../questionlib.php.
39  *
40  * @copyright  2006 The Open University
41  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42  */
43 class core_questionlib_testcase extends advanced_testcase {
45     /**
46      * Test set up.
47      *
48      * This is executed before running any test in this file.
49      */
50     public function setUp() {
51         $this->resetAfterTest();
52     }
54     /**
55      * Tidy up open files that may be left open.
56      */
57     protected function tearDown() {
58         gc_collect_cycles();
59     }
61     /**
62      * Return true and false to test functions with feedback on and off.
63      *
64      * @return array Test data
65      */
66     public function provider_feedback() {
67         return array(
68             'Feedback test' => array(true),
69             'No feedback test' => array(false)
70         );
71     }
73     /**
74      * Setup a course, a quiz, a question category and a question for testing.
75      *
76      * @param string $type The type of question category to create.
77      * @return array The created data objects
78      */
79     public function setup_quiz_and_questions($type = 'module') {
80         // Create course category.
81         $category = $this->getDataGenerator()->create_category();
83         // Create course.
84         $course = $this->getDataGenerator()->create_course(array('numsections' => 5));
86         $options = array(
87             'course' => $course->id,
88             'duedate' => time(),
89         );
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);
100         } else {
101             $context = context_module::instance($quiz->cmid);
102         }
104         $qcat = $qgen->create_question_category(array('contextid' => $context->id));
106         $questions = array(
107                 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
108                 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
109         );
111         quiz_add_quiz_question($questions[0]->id, $quiz);
113         return array($category, $course, $quiz, $qcat, $questions);
114     }
116     public function test_question_reorder_qtypes() {
117         $this->assertEquals(
118             array(0 => 't2', 1 => 't1', 2 => 't3'),
119             question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1));
120         $this->assertEquals(
121             array(0 => 't1', 1 => 't2', 2 => 't3'),
122             question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', -1));
123         $this->assertEquals(
124             array(0 => 't2', 1 => 't1', 2 => 't3'),
125             question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't2', -1));
126         $this->assertEquals(
127             array(0 => 't1', 1 => 't2', 2 => 't3'),
128             question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't3', +1));
129         $this->assertEquals(
130             array(0 => 't1', 1 => 't2', 2 => 't3'),
131             question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 'missing', +1));
132     }
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'));
148     }
150     /**
151      * This function tests that the functions responsible for moving questions to
152      * different contexts also updates the tag instances associated with the questions.
153      */
154     public function test_altering_tag_instance_context() {
155         global $CFG, $DB;
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' =>
170             $context1->id));
171         $questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
172             $context2->id));
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)));
224         // Create a course.
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);
244         $bc->execute_plan();
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);
250         $bc->destroy();
251         unset($bc);
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();
257         $rc->execute_plan();
259         // Get the created question category.
260         $restoredcategory = $DB->get_record('question_categories', array('contextid' => context_course::instance($course2->id)->id),
261             '*', MUST_EXIST);
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)));
265     }
267     /**
268      * This function tests the question_category_delete_safe function.
269      */
270     public function test_question_category_delete_safe() {
271         global $DB;
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));
290     }
292     /**
293      * This function tests the question_delete_activity function.
294      *
295      * @param bool $feedback Whether to return feedback
296      * @dataProvider provider_feedback
297      */
298     public function test_question_delete_activity($feedback) {
299         global $DB;
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.
307         if ($feedback) {
308             $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
309         }
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));
319     }
321     /**
322      * This function tests the question_delete_context function.
323      */
324     public function test_question_delete_context() {
325         global $DB;
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);
345     }
347     /**
348      * This function tests the question_delete_course function.
349      *
350      * @param bool $feedback Whether to return feedback
351      * @dataProvider provider_feedback
352      */
353     public function test_question_delete_course($feedback) {
354         global $DB;
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.
361         if ($feedback) {
362             $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
363         }
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));
373     }
375     /**
376      * This function tests the question_delete_course_category function.
377      *
378      * @param bool $feedback Whether to return feedback
379      * @dataProvider provider_feedback
380      */
381     public function test_question_delete_course_category($feedback) {
382         global $DB;
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.
389         if ($feedback) {
390             $this->expectOutputRegex('|'.get_string('unusedcategorydeleted', 'question').'|');
391         }
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));
401     }