Merge branch 'MDL-73883-master' of https://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / questionlib_test.php
blob4275b81be863e2d7d268b13b86e24ad56ad50905
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
25 use core_tag\output\tag;
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
32 require_once($CFG->libdir . '/questionlib.php');
33 require_once($CFG->dirroot . '/mod/quiz/locallib.php');
35 // Get the necessary files to perform backup and restore.
36 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
37 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
39 /**
40 * Unit tests for (some of) ../questionlib.php.
42 * @copyright 2006 The Open University
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class core_questionlib_testcase extends advanced_testcase {
47 /**
48 * Test set up.
50 * This is executed before running any test in this file.
52 public function setUp(): void {
53 $this->resetAfterTest();
56 /**
57 * Setup a course, a quiz, a question category and a question for testing.
59 * @param string $type The type of question category to create.
60 * @return array The created data objects
62 public function setup_quiz_and_questions($type = 'module') {
63 // Create course category.
64 $category = $this->getDataGenerator()->create_category();
66 // Create course.
67 $course = $this->getDataGenerator()->create_course(array(
68 'numsections' => 5,
69 'category' => $category->id
70 ));
72 $options = array(
73 'course' => $course->id,
74 'duedate' => time(),
77 // Generate an assignment with due date (will generate a course event).
78 $quiz = $this->getDataGenerator()->create_module('quiz', $options);
80 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
82 switch ($type) {
83 case 'course':
84 $context = context_course::instance($course->id);
85 break;
87 case 'category':
88 $context = context_coursecat::instance($category->id);
89 break;
91 case 'system':
92 $context = context_system::instance();
93 break;
95 default:
96 $context = context_module::instance($quiz->cmid);
97 break;
100 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
102 $questions = array(
103 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
104 $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
107 quiz_add_quiz_question($questions[0]->id, $quiz);
109 return array($category, $course, $quiz, $qcat, $questions);
113 * Assert that a category contains a specific number of questions.
115 * @param int $categoryid int Category id.
116 * @param int $numberofquestions Number of question in a category.
117 * @return void Questions in a category.
119 protected function assert_category_contains_questions(int $categoryid, int $numberofquestions): void {
120 $questionsid = question_bank::get_finder()->get_questions_from_categories([$categoryid], null);
121 $this->assertEquals($numberofquestions, count($questionsid));
124 public function test_question_reorder_qtypes() {
125 $this->assertEquals(
126 array(0 => 't2', 1 => 't1', 2 => 't3'),
127 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1));
128 $this->assertEquals(
129 array(0 => 't1', 1 => 't2', 2 => 't3'),
130 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', -1));
131 $this->assertEquals(
132 array(0 => 't2', 1 => 't1', 2 => 't3'),
133 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't2', -1));
134 $this->assertEquals(
135 array(0 => 't1', 1 => 't2', 2 => 't3'),
136 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't3', +1));
137 $this->assertEquals(
138 array(0 => 't1', 1 => 't2', 2 => 't3'),
139 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 'missing', +1));
142 public function test_match_grade_options() {
143 $gradeoptions = question_bank::fraction_options_full();
145 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'error'));
146 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'error'));
147 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'error'));
148 $this->assertFalse(match_grade_options($gradeoptions, 0.3333, 'error'));
150 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'nearest'));
151 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'nearest'));
152 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'nearest'));
153 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33, 'nearest'));
155 $this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
159 * This function tests that the functions responsible for moving questions to
160 * different contexts also updates the tag instances associated with the questions.
162 public function test_altering_tag_instance_context() {
163 global $CFG, $DB;
165 // Set to admin user.
166 $this->setAdminUser();
168 // Create two course categories - we are going to delete one of these later and will expect
169 // all the questions belonging to the course in the deleted category to be moved.
170 $coursecat1 = $this->getDataGenerator()->create_category();
171 $coursecat2 = $this->getDataGenerator()->create_category();
173 // Create a couple of categories and questions.
174 $context1 = context_coursecat::instance($coursecat1->id);
175 $context2 = context_coursecat::instance($coursecat2->id);
176 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
177 $questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
178 $context1->id));
179 $questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
180 $context2->id));
181 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
182 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
183 $question3 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
184 $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
186 // Now lets tag these questions.
187 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $context1, array('tag 1', 'tag 2'));
188 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $context1, array('tag 3', 'tag 4'));
189 core_tag_tag::set_item_tags('core_question', 'question', $question3->id, $context2, array('tag 5', 'tag 6'));
190 core_tag_tag::set_item_tags('core_question', 'question', $question4->id, $context2, array('tag 7', 'tag 8'));
192 // Test moving the questions to another category.
193 question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id);
195 // Test that all tag_instances belong to one context.
196 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
197 'contextid' => $questioncat2->contextid)));
199 // Test moving them back.
200 question_move_questions_to_category(array($question1->id, $question2->id), $questioncat1->id);
202 // Test that all tag_instances are now reset to how they were initially.
203 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
204 'contextid' => $questioncat1->contextid)));
205 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
206 'contextid' => $questioncat2->contextid)));
208 // Now test moving a whole question category to another context.
209 question_move_category_to_context($questioncat1->id, $questioncat1->contextid, $questioncat2->contextid);
211 // Test that all tag_instances belong to one context.
212 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
213 'contextid' => $questioncat2->contextid)));
215 // Now test moving them back.
216 question_move_category_to_context($questioncat1->id, $questioncat2->contextid,
217 context_coursecat::instance($coursecat1->id)->id);
219 // Test that all tag_instances are now reset to how they were initially.
220 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
221 'contextid' => $questioncat1->contextid)));
222 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
223 'contextid' => $questioncat2->contextid)));
225 // Now we want to test deleting the course category and moving the questions to another category.
226 question_delete_course_category($coursecat1, $coursecat2);
228 // Test that all tag_instances belong to one context.
229 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
230 'contextid' => $questioncat2->contextid)));
232 // Create a course.
233 $course = $this->getDataGenerator()->create_course();
235 // Create some question categories and questions in this course.
236 $coursecontext = context_course::instance($course->id);
237 $questioncat = $questiongenerator->create_question_category(array('contextid' => $coursecontext->id));
238 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
239 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
241 // Add some tags to these questions.
242 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, array('tag 1', 'tag 2'));
243 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, array('tag 1', 'tag 2'));
245 // Create a course that we are going to restore the other course to.
246 $course2 = $this->getDataGenerator()->create_course();
248 // Create backup file and save it to the backup location.
249 $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
250 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
251 $bc->execute_plan();
252 $results = $bc->get_results();
253 $file = $results['backup_destination'];
254 $fp = get_file_packer('application/vnd.moodle.backup');
255 $filepath = $CFG->dataroot . '/temp/backup/test-restore-course';
256 $file->extract_to_pathname($fp, $filepath);
257 $bc->destroy();
259 // Now restore the course.
260 $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO,
261 backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE);
262 $rc->execute_precheck();
263 $rc->execute_plan();
265 // Get the created question category.
266 $restoredcategory = $DB->get_record_select('question_categories', 'contextid = ? AND parent <> 0',
267 array(context_course::instance($course2->id)->id), '*', MUST_EXIST);
269 // Check that there are two questions in the restored to course's context.
270 $this->assertEquals(2, $DB->get_record_sql('SELECT COUNT(q.id) as questioncount
271 FROM {question} q
272 JOIN {question_versions} qv
273 ON qv.questionid = q.id
274 JOIN {question_bank_entries} qbe
275 ON qbe.id = qv.questionbankentryid
276 WHERE qbe.questioncategoryid = ?',
277 [$restoredcategory->id])->questioncount);
278 $rc->destroy();
282 * Test that deleting a question from the question bank works in the normal case.
284 public function test_question_delete_question() {
285 global $DB;
287 // Setup.
288 $context = context_system::instance();
289 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
290 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
291 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
292 $q2 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
294 // Do.
295 question_delete_question($q1->id);
297 // Verify.
298 $this->assertFalse($DB->record_exists('question', ['id' => $q1->id]));
299 // Check that we did not delete too much.
300 $this->assertTrue($DB->record_exists('question', ['id' => $q2->id]));
304 * Test that deleting a broken question from the question bank does not cause fatal errors.
306 public function test_question_delete_question_broken_data() {
307 global $DB;
309 // Setup.
310 $context = context_system::instance();
311 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
312 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
313 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
315 // Now delete the category, to simulate what happens in old sites where
316 // referential integrity has failed.
317 $DB->delete_records('question_categories', ['id' => $qcat->id]);
319 // Do.
320 question_delete_question($q1->id);
322 // Verify.
323 $this->assertDebuggingCalled('Deleting question ' . $q1->id .
324 ' which is no longer linked to a context. Assuming system context ' .
325 'to avoid errors, but this may mean that some data like ' .
326 'files, tags, are not cleaned up.');
327 $this->assertFalse($DB->record_exists('question', ['id' => $q1->id]));
331 * This function tests the question_category_delete_safe function.
333 public function test_question_category_delete_safe() {
334 global $DB;
335 $this->resetAfterTest(true);
336 $this->setAdminUser();
338 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
340 question_category_delete_safe($qcat);
342 // Verify category deleted.
343 $criteria = array('id' => $qcat->id);
344 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
346 // Verify questions deleted or moved.
347 $this->assert_category_contains_questions($qcat->id, 0);
349 // Verify question not deleted.
350 $criteria = array('id' => $questions[0]->id);
351 $this->assertEquals(1, $DB->count_records('question', $criteria));
355 * This function tests the question_delete_activity function.
357 public function test_question_delete_activity() {
358 global $DB;
359 $this->resetAfterTest(true);
360 $this->setAdminUser();
362 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
364 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
366 // Test the deletion.
367 question_delete_activity($cm);
369 // Verify category deleted.
370 $criteria = array('id' => $qcat->id);
371 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
373 // Verify questions deleted or moved.
374 $this->assert_category_contains_questions($qcat->id, 0);
378 * This function tests the question_delete_context function.
380 public function test_question_delete_context() {
381 global $DB;
382 $this->resetAfterTest(true);
383 $this->setAdminUser();
385 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
387 // Get the module context id.
388 $result = question_delete_context($qcat->contextid);
390 // Verify category deleted.
391 $criteria = array('id' => $qcat->id);
392 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
394 // Verify questions deleted or moved.
395 $this->assert_category_contains_questions($qcat->id, 0);
399 * This function tests the question_delete_course function.
401 public function test_question_delete_course() {
402 global $DB;
403 $this->resetAfterTest(true);
404 $this->setAdminUser();
406 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
408 // Test the deletion.
409 question_delete_course($course);
411 // Verify category deleted.
412 $criteria = array('id' => $qcat->id);
413 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
415 // Verify questions deleted or moved.
416 $this->assert_category_contains_questions($qcat->id, 0);
420 * This function tests the question_delete_course_category function.
422 public function test_question_delete_course_category() {
423 global $DB;
424 $this->resetAfterTest(true);
425 $this->setAdminUser();
427 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
429 // Test that the feedback works.
430 question_delete_course_category($category, null);
432 // Verify category deleted.
433 $criteria = array('id' => $qcat->id);
434 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
436 // Verify questions deleted or moved.
437 $this->assert_category_contains_questions($qcat->id, 0);
441 * This function tests the question_delete_course_category function when it is supposed to move question categories.
443 public function test_question_delete_course_category_move_qcats() {
444 global $DB;
445 $this->resetAfterTest(true);
446 $this->setAdminUser();
448 list($category1, $course1, $quiz1, $qcat1, $questions1) = $this->setup_quiz_and_questions('category');
449 list($category2, $course2, $quiz2, $qcat2, $questions2) = $this->setup_quiz_and_questions('category');
451 $questionsinqcat1 = count($questions1);
452 $questionsinqcat2 = count($questions2);
454 // Test the delete.
455 question_delete_course_category($category1, $category2);
457 // Verify category not deleted.
458 $criteria = array('id' => $qcat1->id);
459 $this->assertEquals(1, $DB->count_records('question_categories', $criteria));
461 // Verify questions are moved.
462 $params = array($qcat2->contextid);
463 $actualquestionscount = $DB->count_records_sql("SELECT COUNT(*)
464 FROM {question} q
465 JOIN {question_versions} qv ON qv.questionid = q.id
466 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
467 JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
468 WHERE qc.contextid = ?", $params);
469 $this->assertEquals($questionsinqcat1 + $questionsinqcat2, $actualquestionscount);
471 // Verify there is just a single top-level category.
472 $criteria = array('contextid' => $qcat2->contextid, 'parent' => 0);
473 $this->assertEquals(1, $DB->count_records('question_categories', $criteria));
475 // Verify there is no question category in previous context.
476 $criteria = array('contextid' => $qcat1->contextid);
477 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
481 * This function tests the question_save_from_deletion function when it is supposed to make a new category and
482 * move question categories to that new category.
484 public function test_question_save_from_deletion() {
485 global $DB;
486 $this->resetAfterTest(true);
487 $this->setAdminUser();
489 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
491 $context = context::instance_by_id($qcat->contextid);
493 $newcat = question_save_from_deletion(array_column($questions, 'id'),
494 $context->get_parent_context()->id, $context->get_context_name());
496 // Verify that the newcat itself is not a tep level category.
497 $this->assertNotEquals(0, $newcat->parent);
499 // Verify there is just a single top-level category.
500 $this->assertEquals(1, $DB->count_records('question_categories', ['contextid' => $qcat->contextid, 'parent' => 0]));
504 * This function tests the question_save_from_deletion function when it is supposed to make a new category and
505 * move question categories to that new category when quiz name is very long but less than 256 characters.
507 public function test_question_save_from_deletion_quiz_with_long_name() {
508 global $DB;
509 $this->resetAfterTest(true);
510 $this->setAdminUser();
512 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
514 // Moodle doesn't allow you to enter a name longer than 255 characters.
515 $quiz->name = shorten_text(str_repeat('123456789 ', 26), 255);
517 $DB->update_record('quiz', $quiz);
519 $context = context::instance_by_id($qcat->contextid);
521 $newcat = question_save_from_deletion(array_column($questions, 'id'),
522 $context->get_parent_context()->id, $context->get_context_name());
524 // Verifying that the inserted record's name is expected or not.
525 $this->assertEquals($DB->get_record('question_categories', ['id' => $newcat->id])->name, $newcat->name);
527 // Verify that the newcat itself is not a top level category.
528 $this->assertNotEquals(0, $newcat->parent);
530 // Verify there is just a single top-level category.
531 $this->assertEquals(1, $DB->count_records('question_categories', ['contextid' => $qcat->contextid, 'parent' => 0]));
535 * get_question_options should add the category object to the given question.
537 public function test_get_question_options_includes_category_object_single_question() {
538 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
539 $question = array_shift($questions);
541 get_question_options($question);
543 $this->assertEquals($qcat, $question->categoryobject);
547 * get_question_options should add the category object to all of the questions in
548 * the given list.
550 public function test_get_question_options_includes_category_object_multiple_questions() {
551 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
553 get_question_options($questions);
555 foreach ($questions as $question) {
556 $this->assertEquals($qcat, $question->categoryobject);
561 * get_question_options includes the tags for all questions in the list.
563 public function test_get_question_options_includes_question_tags() {
564 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
565 $question1 = $questions[0];
566 $question2 = $questions[1];
567 $qcontext = context::instance_by_id($qcat->contextid);
569 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
570 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
572 get_question_options($questions, true);
574 foreach ($questions as $question) {
575 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
576 $expectedtags = [];
577 $actualtags = $question->tags;
578 foreach ($tags as $tag) {
579 $expectedtags[$tag->id] = $tag->get_display_name();
582 // The question should have a tags property populated with each tag id
583 // and display name as a key vale pair.
584 $this->assertEquals($expectedtags, $actualtags);
586 $actualtagobjects = $question->tagobjects;
587 sort($tags);
588 sort($actualtagobjects);
590 // The question should have a full set of each tag object.
591 $this->assertEquals($tags, $actualtagobjects);
592 // The question should not have any course tags.
593 $this->assertEmpty($question->coursetagobjects);
598 * get_question_options includes the course tags for all questions in the list.
600 public function test_get_question_options_includes_course_tags() {
601 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
602 $question1 = $questions[0];
603 $question2 = $questions[1];
604 $coursecontext = context_course::instance($course->id);
606 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
607 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
609 get_question_options($questions, true);
611 foreach ($questions as $question) {
612 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
613 $expectedcoursetags = [];
614 $actualcoursetags = $question->coursetags;
615 foreach ($tags as $tag) {
616 $expectedcoursetags[$tag->id] = $tag->get_display_name();
619 // The question should have a coursetags property populated with each tag id
620 // and display name as a key vale pair.
621 $this->assertEquals($expectedcoursetags, $actualcoursetags);
623 $actualcoursetagobjects = $question->coursetagobjects;
624 sort($tags);
625 sort($actualcoursetagobjects);
627 // The question should have a full set of the course tag objects.
628 $this->assertEquals($tags, $actualcoursetagobjects);
629 // The question should not have any other tags.
630 $this->assertEmpty($question->tagobjects);
631 $this->assertEmpty($question->tags);
636 * get_question_options only categorises a tag as a course tag if it is in a
637 * course context that is different from the question context.
639 public function test_get_question_options_course_tags_in_course_question_context() {
640 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
641 $question1 = $questions[0];
642 $question2 = $questions[1];
643 $coursecontext = context_course::instance($course->id);
645 // Create course level tags in the course context that matches the question
646 // course context.
647 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
648 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
650 get_question_options($questions, true);
652 foreach ($questions as $question) {
653 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
655 $actualtagobjects = $question->tagobjects;
656 sort($tags);
657 sort($actualtagobjects);
659 // The tags should not be considered course tags because they are in
660 // the same context as the question. That makes them question tags.
661 $this->assertEmpty($question->coursetagobjects);
662 // The course context tags should be returned in the regular tag object
663 // list.
664 $this->assertEquals($tags, $actualtagobjects);
669 * get_question_options includes the tags and course tags for all questions in the list
670 * if each question has course and question level tags.
672 public function test_get_question_options_includes_question_and_course_tags() {
673 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
674 $question1 = $questions[0];
675 $question2 = $questions[1];
676 $qcontext = context::instance_by_id($qcat->contextid);
677 $coursecontext = context_course::instance($course->id);
679 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
680 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['cfoo', 'cbar']);
681 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
682 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['cbaz', 'cbop']);
684 get_question_options($questions, true);
686 foreach ($questions as $question) {
687 $alltags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
688 $tags = array_filter($alltags, function($tag) use ($qcontext) {
689 return $tag->taginstancecontextid == $qcontext->id;
691 $coursetags = array_filter($alltags, function($tag) use ($coursecontext) {
692 return $tag->taginstancecontextid == $coursecontext->id;
695 $expectedtags = [];
696 $actualtags = $question->tags;
697 foreach ($tags as $tag) {
698 $expectedtags[$tag->id] = $tag->get_display_name();
701 // The question should have a tags property populated with each tag id
702 // and display name as a key vale pair.
703 $this->assertEquals($expectedtags, $actualtags);
705 $actualtagobjects = $question->tagobjects;
706 sort($tags);
707 sort($actualtagobjects);
708 // The question should have a full set of each tag object.
709 $this->assertEquals($tags, $actualtagobjects);
711 $actualcoursetagobjects = $question->coursetagobjects;
712 sort($coursetags);
713 sort($actualcoursetagobjects);
714 // The question should have a full set of course tag objects.
715 $this->assertEquals($coursetags, $actualcoursetagobjects);
720 * get_question_options should update the context id to the question category
721 * context id for any non-course context tag that isn't in the question category
722 * context.
724 public function test_get_question_options_normalises_question_tags() {
725 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
726 $question1 = $questions[0];
727 $question2 = $questions[1];
728 $qcontext = context::instance_by_id($qcat->contextid);
729 $systemcontext = context_system::instance();
731 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
732 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
734 $q1tags = core_tag_tag::get_item_tags('core_question', 'question', $question1->id);
735 $q2tags = core_tag_tag::get_item_tags('core_question', 'question', $question2->id);
736 $q1tag = array_shift($q1tags);
737 $q2tag = array_shift($q2tags);
739 // Change two of the tag instances to be a different (non-course) context to the
740 // question tag context. These tags should then be normalised back to the question
741 // tag context.
742 core_tag_tag::change_instances_context([$q1tag->taginstanceid, $q2tag->taginstanceid], $systemcontext);
744 get_question_options($questions, true);
746 foreach ($questions as $question) {
747 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
749 // The database should have been updated with the correct context id.
750 foreach ($tags as $tag) {
751 $this->assertEquals($qcontext->id, $tag->taginstancecontextid);
754 // The tag objects on the question should have been updated with the
755 // correct context id.
756 foreach ($question->tagobjects as $tag) {
757 $this->assertEquals($qcontext->id, $tag->taginstancecontextid);
763 * get_question_options if the question is a course level question then tags
764 * in that context should not be consdered course tags, they are question tags.
766 public function test_get_question_options_includes_course_context_question_tags() {
767 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
768 $question1 = $questions[0];
769 $question2 = $questions[1];
770 $coursecontext = context_course::instance($course->id);
772 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
773 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
775 get_question_options($questions, true);
777 foreach ($questions as $question) {
778 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
779 // Tags in a course context that matches the question context should
780 // not be considered course tags.
781 $this->assertEmpty($question->coursetagobjects);
782 $this->assertEmpty($question->coursetags);
784 $actualtagobjects = $question->tagobjects;
785 sort($tags);
786 sort($actualtagobjects);
787 // The tags should be considered question tags not course tags.
788 $this->assertEquals($tags, $actualtagobjects);
793 * get_question_options should return tags from all course contexts by default.
795 public function test_get_question_options_includes_multiple_courses_tags() {
796 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
797 $question1 = $questions[0];
798 $question2 = $questions[1];
799 $coursecontext = context_course::instance($course->id);
800 // Create a sibling course.
801 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
802 $siblingcoursecontext = context_course::instance($siblingcourse->id);
804 // Create course tags.
805 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']);
806 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']);
807 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']);
808 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']);
810 get_question_options($questions, true);
812 foreach ($questions as $question) {
813 $this->assertCount(2, $question->coursetagobjects);
815 foreach ($question->coursetagobjects as $tag) {
816 if ($tag->name == 'c1') {
817 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
818 } else {
819 $this->assertEquals($siblingcoursecontext->id, $tag->taginstancecontextid);
826 * get_question_options should filter the course tags by the given list of courses.
828 public function test_get_question_options_includes_filter_course_tags() {
829 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
830 $question1 = $questions[0];
831 $question2 = $questions[1];
832 $coursecontext = context_course::instance($course->id);
833 // Create a sibling course.
834 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
835 $siblingcoursecontext = context_course::instance($siblingcourse->id);
837 // Create course tags.
838 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']);
839 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']);
840 // Create sibling course tags. These should be filtered out.
841 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']);
842 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']);
844 // Ask to only receive course tags from $course (ignoring $siblingcourse tags).
845 get_question_options($questions, true, [$course]);
847 foreach ($questions as $question) {
848 foreach ($question->coursetagobjects as $tag) {
849 // We should only be seeing course tags from $course. The tags from
850 // $siblingcourse should have been filtered out.
851 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
857 * question_move_question_tags_to_new_context should update all of the
858 * question tags contexts when they are moving down (from system to course
859 * category context).
861 public function test_question_move_question_tags_to_new_context_system_to_course_cat_qtags() {
862 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
863 $question1 = $questions[0];
864 $question2 = $questions[1];
865 $qcontext = context::instance_by_id($qcat->contextid);
866 $newcontext = context_coursecat::instance($category->id);
868 foreach ($questions as $question) {
869 $question->contextid = $qcat->contextid;
872 // Create tags in the system context.
873 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
874 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']);
876 question_move_question_tags_to_new_context($questions, $newcontext);
878 foreach ($questions as $question) {
879 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
881 // All of the tags should have their context id set to the new context.
882 foreach ($tags as $tag) {
883 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
889 * question_move_question_tags_to_new_context should update all of the question tags
890 * contexts when they are moving down (from system to course category context)
891 * but leave any tags in the course context where they are.
893 public function test_question_move_question_tags_to_new_context_system_to_course_cat_qtags_and_course_tags() {
894 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
895 $question1 = $questions[0];
896 $question2 = $questions[1];
897 $qcontext = context::instance_by_id($qcat->contextid);
898 $coursecontext = context_course::instance($course->id);
899 $newcontext = context_coursecat::instance($category->id);
901 foreach ($questions as $question) {
902 $question->contextid = $qcat->contextid;
905 // Create tags in the system context.
906 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
907 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
908 // Create tags in the course context.
909 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
910 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
912 question_move_question_tags_to_new_context($questions, $newcontext);
914 foreach ($questions as $question) {
915 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
917 foreach ($tags as $tag) {
918 if ($tag->name == 'ctag') {
919 // Course tags should remain in the course context.
920 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
921 } else {
922 // Other tags should be updated.
923 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
930 * question_move_question_tags_to_new_context should update all of the question
931 * contexts tags when they are moving up (from course category to system context).
933 public function test_question_move_question_tags_to_new_context_course_cat_to_system_qtags() {
934 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
935 $question1 = $questions[0];
936 $question2 = $questions[1];
937 $qcontext = context::instance_by_id($qcat->contextid);
938 $newcontext = context_system::instance();
940 foreach ($questions as $question) {
941 $question->contextid = $qcat->contextid;
944 // Create tags in the course category context.
945 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
946 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']);
948 question_move_question_tags_to_new_context($questions, $newcontext);
950 foreach ($questions as $question) {
951 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
953 // All of the tags should have their context id set to the new context.
954 foreach ($tags as $tag) {
955 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
961 * question_move_question_tags_to_new_context should update all of the question
962 * tags contexts when they are moving up (from course category context to system
963 * context) but leave any tags in the course context where they are.
965 public function test_question_move_question_tags_to_new_context_course_cat_to_system_qtags_and_course_tags() {
966 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
967 $question1 = $questions[0];
968 $question2 = $questions[1];
969 $qcontext = context::instance_by_id($qcat->contextid);
970 $coursecontext = context_course::instance($course->id);
971 $newcontext = context_system::instance();
973 foreach ($questions as $question) {
974 $question->contextid = $qcat->contextid;
977 // Create tags in the system context.
978 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
979 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
980 // Create tags in the course context.
981 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
982 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
984 question_move_question_tags_to_new_context($questions, $newcontext);
986 foreach ($questions as $question) {
987 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
989 foreach ($tags as $tag) {
990 if ($tag->name == 'ctag') {
991 // Course tags should remain in the course context.
992 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
993 } else {
994 // Other tags should be updated.
995 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1002 * question_move_question_tags_to_new_context should merge all tags into the course
1003 * context when moving down from course category context into course context.
1005 public function test_question_move_question_tags_to_new_context_course_cat_to_coures_qtags_and_course_tags() {
1006 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1007 $question1 = $questions[0];
1008 $question2 = $questions[1];
1009 $qcontext = context::instance_by_id($qcat->contextid);
1010 $coursecontext = context_course::instance($course->id);
1011 $newcontext = $coursecontext;
1013 foreach ($questions as $question) {
1014 $question->contextid = $qcat->contextid;
1017 // Create tags in the system context.
1018 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1019 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1020 // Create tags in the course context.
1021 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
1022 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
1024 question_move_question_tags_to_new_context($questions, $newcontext);
1026 foreach ($questions as $question) {
1027 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1028 // Each question should have 2 tags.
1029 $this->assertCount(2, $tags);
1031 foreach ($tags as $tag) {
1032 // All tags should be updated to the course context and merged in.
1033 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1039 * question_move_question_tags_to_new_context should delete all of the tag
1040 * instances from sibling courses when moving the context of a question down
1041 * from a course category into a course context because the other courses will
1042 * no longer have access to the question.
1044 public function test_question_move_question_tags_to_new_context_remove_other_course_tags() {
1045 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1046 // Create a sibling course.
1047 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
1048 $question1 = $questions[0];
1049 $question2 = $questions[1];
1050 $qcontext = context::instance_by_id($qcat->contextid);
1051 $coursecontext = context_course::instance($course->id);
1052 $siblingcoursecontext = context_course::instance($siblingcourse->id);
1053 $newcontext = $coursecontext;
1055 foreach ($questions as $question) {
1056 $question->contextid = $qcat->contextid;
1059 // Create tags in the system context.
1060 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1061 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1062 // Create tags in the target course context.
1063 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
1064 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
1065 // Create tags in the sibling course context. These should be deleted as
1066 // part of the move.
1067 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['stag']);
1068 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['stag']);
1070 question_move_question_tags_to_new_context($questions, $newcontext);
1072 foreach ($questions as $question) {
1073 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1074 // Each question should have 2 tags, 'foo' and 'ctag'.
1075 $this->assertCount(2, $tags);
1077 foreach ($tags as $tag) {
1078 $tagname = $tag->name;
1079 // The 'stag' should have been deleted because it's in a sibling
1080 // course context.
1081 $this->assertContains($tagname, ['foo', 'ctag']);
1082 // All tags should be in the course context now.
1083 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
1089 * question_move_question_tags_to_new_context should update all of the question
1090 * tags to be the course category context when moving the tags from a course
1091 * context to a course category context.
1093 public function test_question_move_question_tags_to_new_context_course_to_course_cat() {
1094 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
1095 $question1 = $questions[0];
1096 $question2 = $questions[1];
1097 $qcontext = context::instance_by_id($qcat->contextid);
1098 // Moving up into the course category context.
1099 $newcontext = context_coursecat::instance($category->id);
1101 foreach ($questions as $question) {
1102 $question->contextid = $qcat->contextid;
1105 // Create tags in the course context.
1106 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1107 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1109 question_move_question_tags_to_new_context($questions, $newcontext);
1111 foreach ($questions as $question) {
1112 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1114 // All of the tags should have their context id set to the new context.
1115 foreach ($tags as $tag) {
1116 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1122 * question_move_question_tags_to_new_context should update all of the
1123 * question tags contexts when they are moving down (from system to course
1124 * category context).
1126 public function test_question_move_question_tags_to_new_context_orphaned_tag_contexts() {
1127 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
1128 $question1 = $questions[0];
1129 $question2 = $questions[1];
1130 $othercategory = $this->getDataGenerator()->create_category();
1131 $qcontext = context::instance_by_id($qcat->contextid);
1132 $newcontext = context_coursecat::instance($category->id);
1133 $othercategorycontext = context_coursecat::instance($othercategory->id);
1135 foreach ($questions as $question) {
1136 $question->contextid = $qcat->contextid;
1139 // Create tags in the system context.
1140 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1141 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1142 // Create tags in the other course category context. These should be
1143 // update to the next context id because they represent erroneous data
1144 // from a time before context id was mandatory in the tag API.
1145 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercategorycontext, ['bar']);
1146 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercategorycontext, ['bar']);
1148 question_move_question_tags_to_new_context($questions, $newcontext);
1150 foreach ($questions as $question) {
1151 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1152 // Each question should have two tags, 'foo' and 'bar'.
1153 $this->assertCount(2, $tags);
1155 // All of the tags should have their context id set to the new context
1156 // (course category context).
1157 foreach ($tags as $tag) {
1158 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1164 * When moving from a course category context down into an activity context
1165 * all question context tags and course tags (where the course is a parent of
1166 * the activity) should move into the new context.
1168 public function test_question_move_question_tags_to_new_context_course_cat_to_activity_qtags_and_course_tags() {
1169 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1170 $question1 = $questions[0];
1171 $question2 = $questions[1];
1172 $qcontext = context::instance_by_id($qcat->contextid);
1173 $coursecontext = context_course::instance($course->id);
1174 $newcontext = context_module::instance($quiz->cmid);
1176 foreach ($questions as $question) {
1177 $question->contextid = $qcat->contextid;
1180 // Create tags in the course category context.
1181 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1182 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1183 // Move the questions to the activity context which is a child context of
1184 // $coursecontext.
1185 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
1186 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
1188 question_move_question_tags_to_new_context($questions, $newcontext);
1190 foreach ($questions as $question) {
1191 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1192 // Each question should have 2 tags.
1193 $this->assertCount(2, $tags);
1195 foreach ($tags as $tag) {
1196 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1202 * When moving from a course category context down into an activity context
1203 * all question context tags and course tags (where the course is a parent of
1204 * the activity) should move into the new context. Tags in course contexts
1205 * that are not a parent of the activity context should be deleted.
1207 public function test_question_move_question_tags_to_new_context_course_cat_to_activity_orphaned_tags() {
1208 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1209 $question1 = $questions[0];
1210 $question2 = $questions[1];
1211 $qcontext = context::instance_by_id($qcat->contextid);
1212 $coursecontext = context_course::instance($course->id);
1213 $newcontext = context_module::instance($quiz->cmid);
1214 $othercourse = $this->getDataGenerator()->create_course();
1215 $othercoursecontext = context_course::instance($othercourse->id);
1217 foreach ($questions as $question) {
1218 $question->contextid = $qcat->contextid;
1221 // Create tags in the course category context.
1222 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1223 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1224 // Create tags in the course context.
1225 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
1226 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
1227 // Create tags in the other course context. These should be deleted.
1228 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']);
1229 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']);
1231 // Move the questions to the activity context which is a child context of
1232 // $coursecontext.
1233 question_move_question_tags_to_new_context($questions, $newcontext);
1235 foreach ($questions as $question) {
1236 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1237 // Each question should have 2 tags.
1238 $this->assertCount(2, $tags);
1240 foreach ($tags as $tag) {
1241 // Make sure we don't have any 'delete' tags.
1242 $this->assertContains($tag->name, ['foo', 'ctag']);
1243 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1249 * When moving from a course context down into an activity context all of the
1250 * course tags should move into the activity context.
1252 public function test_question_move_question_tags_to_new_context_course_to_activity_qtags() {
1253 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
1254 $question1 = $questions[0];
1255 $question2 = $questions[1];
1256 $qcontext = context::instance_by_id($qcat->contextid);
1257 $newcontext = context_module::instance($quiz->cmid);
1259 foreach ($questions as $question) {
1260 $question->contextid = $qcat->contextid;
1263 // Create tags in the course context.
1264 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1265 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1267 question_move_question_tags_to_new_context($questions, $newcontext);
1269 foreach ($questions as $question) {
1270 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1272 foreach ($tags as $tag) {
1273 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1279 * When moving from a course context down into an activity context all of the
1280 * course tags should move into the activity context.
1282 public function test_question_move_question_tags_to_new_context_activity_to_course_qtags() {
1283 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
1284 $question1 = $questions[0];
1285 $question2 = $questions[1];
1286 $qcontext = context::instance_by_id($qcat->contextid);
1287 $newcontext = context_course::instance($course->id);
1289 foreach ($questions as $question) {
1290 $question->contextid = $qcat->contextid;
1293 // Create tags in the activity context.
1294 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1295 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1297 question_move_question_tags_to_new_context($questions, $newcontext);
1299 foreach ($questions as $question) {
1300 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1302 foreach ($tags as $tag) {
1303 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1309 * question_move_question_tags_to_new_context should update all of the
1310 * question tags contexts when they are moving down (from system to course
1311 * category context).
1313 * Course tags within the new category context should remain while any course
1314 * tags in course contexts that can no longer access the question should be
1315 * deleted.
1317 public function test_question_move_question_tags_to_new_context_system_to_course_cat_with_orphaned_tags() {
1318 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
1319 $question1 = $questions[0];
1320 $question2 = $questions[1];
1321 $othercategory = $this->getDataGenerator()->create_category();
1322 $othercourse = $this->getDataGenerator()->create_course(['category' => $othercategory->id]);
1323 $qcontext = context::instance_by_id($qcat->contextid);
1324 $newcontext = context_coursecat::instance($category->id);
1325 $othercategorycontext = context_coursecat::instance($othercategory->id);
1326 $coursecontext = context_course::instance($course->id);
1327 $othercoursecontext = context_course::instance($othercourse->id);
1329 foreach ($questions as $question) {
1330 $question->contextid = $qcat->contextid;
1333 // Create tags in the system context.
1334 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
1335 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
1336 // Create tags in the child course context of the new context.
1337 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['bar']);
1338 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']);
1339 // Create tags in the other course context. These should be deleted when
1340 // the question moves to the new course category context because this
1341 // course belongs to a different category, which means it will no longer
1342 // have access to the question.
1343 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']);
1344 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']);
1346 question_move_question_tags_to_new_context($questions, $newcontext);
1348 foreach ($questions as $question) {
1349 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1350 // Each question should have two tags, 'foo' and 'bar'.
1351 $this->assertCount(2, $tags);
1353 // All of the tags should have their context id set to the new context
1354 // (course category context).
1355 foreach ($tags as $tag) {
1356 $this->assertContains($tag->name, ['foo', 'bar']);
1358 if ($tag->name == 'foo') {
1359 $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
1360 } else {
1361 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
1368 * question_sort_tags() includes the tags for all questions in the list.
1370 public function test_question_sort_tags_includes_question_tags() {
1372 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1373 $question1 = $questions[0];
1374 $question2 = $questions[1];
1375 $qcontext = context::instance_by_id($qcat->contextid);
1377 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
1378 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
1380 foreach ($questions as $question) {
1381 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1382 $categorycontext = context::instance_by_id($qcat->contextid);
1383 $tagobjects = question_sort_tags($tags, $categorycontext);
1384 $expectedtags = [];
1385 $actualtags = $tagobjects->tags;
1386 foreach ($tagobjects->tagobjects as $tag) {
1387 $expectedtags[$tag->id] = $tag->name;
1390 // The question should have a tags property populated with each tag id
1391 // and display name as a key vale pair.
1392 $this->assertEquals($expectedtags, $actualtags);
1394 $actualtagobjects = $tagobjects->tagobjects;
1395 sort($tags);
1396 sort($actualtagobjects);
1398 // The question should have a full set of each tag object.
1399 $this->assertEquals($tags, $actualtagobjects);
1400 // The question should not have any course tags.
1401 $this->assertEmpty($tagobjects->coursetagobjects);
1406 * question_sort_tags() includes course tags for all questions in the list.
1408 public function test_question_sort_tags_includes_question_course_tags() {
1409 global $DB;
1411 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1412 $question1 = $questions[0];
1413 $question2 = $questions[1];
1414 $coursecontext = context_course::instance($course->id);
1416 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
1417 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
1419 foreach ($questions as $question) {
1420 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1421 $tagobjects = question_sort_tags($tags, $qcat);
1423 $expectedtags = [];
1424 $actualtags = $tagobjects->coursetags;
1425 foreach ($actualtags as $coursetagid => $coursetagname) {
1426 $expectedtags[$coursetagid] = $coursetagname;
1429 // The question should have a tags property populated with each tag id
1430 // and display name as a key vale pair.
1431 $this->assertEquals($expectedtags, $actualtags);
1433 $actualtagobjects = $tagobjects->coursetagobjects;
1434 sort($tags);
1435 sort($actualtagobjects);
1437 // The question should have a full set of each tag object.
1438 $this->assertEquals($tags, $actualtagobjects);
1439 // The question should not have any course tags.
1440 $this->assertEmpty($tagobjects->tagobjects);
1445 * question_sort_tags() should return tags from all course contexts by default.
1447 public function test_question_sort_tags_includes_multiple_courses_tags() {
1448 global $DB;
1450 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1451 $question1 = $questions[0];
1452 $question2 = $questions[1];
1453 $coursecontext = context_course::instance($course->id);
1454 // Create a sibling course.
1455 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
1456 $siblingcoursecontext = context_course::instance($siblingcourse->id);
1458 // Create course tags.
1459 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']);
1460 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']);
1461 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']);
1462 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']);
1464 foreach ($questions as $question) {
1465 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1466 $tagobjects = question_sort_tags($tags, $qcat);
1467 $this->assertCount(2, $tagobjects->coursetagobjects);
1469 foreach ($tagobjects->coursetagobjects as $tag) {
1470 if ($tag->name == 'c1') {
1471 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
1472 } else {
1473 $this->assertEquals($siblingcoursecontext->id, $tag->taginstancecontextid);
1480 * question_sort_tags() should filter the course tags by the given list of courses.
1482 public function test_question_sort_tags_includes_filter_course_tags() {
1483 global $DB;
1485 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1486 $question1 = $questions[0];
1487 $question2 = $questions[1];
1488 $coursecontext = context_course::instance($course->id);
1489 // Create a sibling course.
1490 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
1491 $siblingcoursecontext = context_course::instance($siblingcourse->id);
1493 // Create course tags.
1494 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']);
1495 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']);
1496 // Create sibling course tags. These should be filtered out.
1497 core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']);
1498 core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']);
1500 foreach ($questions as $question) {
1501 $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
1502 $tagobjects = question_sort_tags($tags, $qcat, [$course]);
1503 foreach ($tagobjects->coursetagobjects as $tag) {
1505 // We should only be seeing course tags from $course. The tags from
1506 // $siblingcourse should have been filtered out.
1507 $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
1513 * Data provider for tests of question_has_capability_on_context and question_require_capability_on_context.
1515 * @return array
1517 public function question_capability_on_question_provider() {
1518 return [
1519 'Unrelated capability which is present' => [
1520 'capabilities' => [
1521 'moodle/question:config' => CAP_ALLOW,
1523 'testcapability' => 'config',
1524 'isowner' => true,
1525 'expect' => true,
1527 'Unrelated capability which is present (not owner)' => [
1528 'capabilities' => [
1529 'moodle/question:config' => CAP_ALLOW,
1531 'testcapability' => 'config',
1532 'isowner' => false,
1533 'expect' => true,
1535 'Unrelated capability which is not set' => [
1536 'capabilities' => [
1538 'testcapability' => 'config',
1539 'isowner' => true,
1540 'expect' => false,
1542 'Unrelated capability which is not set (not owner)' => [
1543 'capabilities' => [
1545 'testcapability' => 'config',
1546 'isowner' => false,
1547 'expect' => false,
1549 'Unrelated capability which is prevented' => [
1550 'capabilities' => [
1551 'moodle/question:config' => CAP_PREVENT,
1553 'testcapability' => 'config',
1554 'isowner' => true,
1555 'expect' => false,
1557 'Unrelated capability which is prevented (not owner)' => [
1558 'capabilities' => [
1559 'moodle/question:config' => CAP_PREVENT,
1561 'testcapability' => 'config',
1562 'isowner' => false,
1563 'expect' => false,
1565 'Related capability which is not set' => [
1566 'capabilities' => [
1568 'testcapability' => 'edit',
1569 'isowner' => true,
1570 'expect' => false,
1572 'Related capability which is not set (not owner)' => [
1573 'capabilities' => [
1575 'testcapability' => 'edit',
1576 'isowner' => false,
1577 'expect' => false,
1579 'Related capability which is allowed at all, unset at mine' => [
1580 'capabilities' => [
1581 'moodle/question:editall' => CAP_ALLOW,
1583 'testcapability' => 'edit',
1584 'isowner' => true,
1585 'expect' => true,
1587 'Related capability which is allowed at all, unset at mine (not owner)' => [
1588 'capabilities' => [
1589 'moodle/question:editall' => CAP_ALLOW,
1591 'testcapability' => 'edit',
1592 'isowner' => false,
1593 'expect' => true,
1595 'Related capability which is allowed at all, prevented at mine' => [
1596 'capabilities' => [
1597 'moodle/question:editall' => CAP_ALLOW,
1598 'moodle/question:editmine' => CAP_PREVENT,
1600 'testcapability' => 'edit',
1601 'isowner' => true,
1602 'expect' => true,
1604 'Related capability which is allowed at all, prevented at mine (not owner)' => [
1605 'capabilities' => [
1606 'moodle/question:editall' => CAP_ALLOW,
1607 'moodle/question:editmine' => CAP_PREVENT,
1609 'testcapability' => 'edit',
1610 'isowner' => false,
1611 'expect' => true,
1613 'Related capability which is unset all, allowed at mine' => [
1614 'capabilities' => [
1615 'moodle/question:editall' => CAP_PREVENT,
1616 'moodle/question:editmine' => CAP_ALLOW,
1618 'testcapability' => 'edit',
1619 'isowner' => true,
1620 'expect' => true,
1622 'Related capability which is unset all, allowed at mine (not owner)' => [
1623 'capabilities' => [
1624 'moodle/question:editall' => CAP_PREVENT,
1625 'moodle/question:editmine' => CAP_ALLOW,
1627 'testcapability' => 'edit',
1628 'isowner' => false,
1629 'expect' => false,
1635 * Tests that question_has_capability_on does not throw exception on broken questions.
1637 public function test_question_has_capability_on_broken_question() {
1638 global $DB;
1640 // Create the test data.
1641 $generator = $this->getDataGenerator();
1642 $questiongenerator = $generator->get_plugin_generator('core_question');
1644 $category = $generator->create_category();
1645 $context = context_coursecat::instance($category->id);
1646 $questioncat = $questiongenerator->create_question_category([
1647 'contextid' => $context->id,
1650 // Create a cloze question.
1651 $question = $questiongenerator->create_question('multianswer', null, [
1652 'category' => $questioncat->id,
1654 // Now, break the question.
1655 $DB->delete_records('question_multianswer', ['question' => $question->id]);
1657 $this->setAdminUser();
1659 $result = question_has_capability_on($question->id, 'tag');
1660 $this->assertTrue($result);
1662 $this->assertDebuggingCalled();
1666 * Tests for the deprecated question_has_capability_on function when passing a stdClass as parameter.
1668 * @dataProvider question_capability_on_question_provider
1669 * @param array $capabilities The capability assignments to set.
1670 * @param string $capability The capability to test
1671 * @param bool $isowner Whether the user to create the question should be the owner or not.
1672 * @param bool $expect The expected result.
1674 public function test_question_has_capability_on_using_stdclass($capabilities, $capability, $isowner, $expect) {
1675 $this->resetAfterTest();
1677 // Create the test data.
1678 $user = $this->getDataGenerator()->create_user();
1679 $otheruser = $this->getDataGenerator()->create_user();
1680 $roleid = $this->getDataGenerator()->create_role();
1681 $category = $this->getDataGenerator()->create_category();
1682 $context = context_coursecat::instance($category->id);
1684 // Assign the user to the role.
1685 role_assign($roleid, $user->id, $context->id);
1687 // Assign the capabilities to the role.
1688 foreach ($capabilities as $capname => $capvalue) {
1689 assign_capability($capname, $capvalue, $roleid, $context->id);
1692 $this->setUser($user);
1694 // The current fake question we make use of is always a stdClass and typically has no ID.
1695 $fakequestion = (object) [
1696 'contextid' => $context->id,
1699 if ($isowner) {
1700 $fakequestion->createdby = $user->id;
1701 } else {
1702 $fakequestion->createdby = $otheruser->id;
1705 $result = question_has_capability_on($fakequestion, $capability);
1706 $this->assertEquals($expect, $result);
1710 * Tests for the deprecated question_has_capability_on function when using question definition.
1712 * @dataProvider question_capability_on_question_provider
1713 * @param array $capabilities The capability assignments to set.
1714 * @param string $capability The capability to test
1715 * @param bool $isowner Whether the user to create the question should be the owner or not.
1716 * @param bool $expect The expected result.
1718 public function test_question_has_capability_on_using_question_definition($capabilities, $capability, $isowner, $expect) {
1719 $this->resetAfterTest();
1721 // Create the test data.
1722 $generator = $this->getDataGenerator();
1723 $questiongenerator = $generator->get_plugin_generator('core_question');
1724 $user = $generator->create_user();
1725 $otheruser = $generator->create_user();
1726 $roleid = $generator->create_role();
1727 $category = $generator->create_category();
1728 $context = context_coursecat::instance($category->id);
1729 $questioncat = $questiongenerator->create_question_category([
1730 'contextid' => $context->id,
1733 // Assign the user to the role.
1734 role_assign($roleid, $user->id, $context->id);
1736 // Assign the capabilities to the role.
1737 foreach ($capabilities as $capname => $capvalue) {
1738 assign_capability($capname, $capvalue, $roleid, $context->id);
1741 // Create the question.
1742 $qtype = 'truefalse';
1743 $overrides = [
1744 'category' => $questioncat->id,
1745 'createdby' => ($isowner) ? $user->id : $otheruser->id,
1748 $question = $questiongenerator->create_question($qtype, null, $overrides);
1750 $this->setUser($user);
1751 $result = question_has_capability_on($question, $capability);
1752 $this->assertEquals($expect, $result);
1756 * Tests for the deprecated question_has_capability_on function when using a real question id.
1758 * @dataProvider question_capability_on_question_provider
1759 * @param array $capabilities The capability assignments to set.
1760 * @param string $capability The capability to test
1761 * @param bool $isowner Whether the user to create the question should be the owner or not.
1762 * @param bool $expect The expected result.
1764 public function test_question_has_capability_on_using_question_id($capabilities, $capability, $isowner, $expect) {
1765 $this->resetAfterTest();
1767 // Create the test data.
1768 $generator = $this->getDataGenerator();
1769 $questiongenerator = $generator->get_plugin_generator('core_question');
1770 $user = $generator->create_user();
1771 $otheruser = $generator->create_user();
1772 $roleid = $generator->create_role();
1773 $category = $generator->create_category();
1774 $context = context_coursecat::instance($category->id);
1775 $questioncat = $questiongenerator->create_question_category([
1776 'contextid' => $context->id,
1779 // Assign the user to the role.
1780 role_assign($roleid, $user->id, $context->id);
1782 // Assign the capabilities to the role.
1783 foreach ($capabilities as $capname => $capvalue) {
1784 assign_capability($capname, $capvalue, $roleid, $context->id);
1787 // Create the question.
1788 $qtype = 'truefalse';
1789 $overrides = [
1790 'category' => $questioncat->id,
1791 'createdby' => ($isowner) ? $user->id : $otheruser->id,
1794 $question = $questiongenerator->create_question($qtype, null, $overrides);
1796 $this->setUser($user);
1797 $result = question_has_capability_on($question->id, $capability);
1798 $this->assertEquals($expect, $result);
1802 * Tests for the deprecated question_has_capability_on function when using a string as question id.
1804 * @dataProvider question_capability_on_question_provider
1805 * @param array $capabilities The capability assignments to set.
1806 * @param string $capability The capability to test
1807 * @param bool $isowner Whether the user to create the question should be the owner or not.
1808 * @param bool $expect The expected result.
1810 public function test_question_has_capability_on_using_question_string_id($capabilities, $capability, $isowner, $expect) {
1811 $this->resetAfterTest();
1813 // Create the test data.
1814 $generator = $this->getDataGenerator();
1815 $questiongenerator = $generator->get_plugin_generator('core_question');
1816 $user = $generator->create_user();
1817 $otheruser = $generator->create_user();
1818 $roleid = $generator->create_role();
1819 $category = $generator->create_category();
1820 $context = context_coursecat::instance($category->id);
1821 $questioncat = $questiongenerator->create_question_category([
1822 'contextid' => $context->id,
1825 // Assign the user to the role.
1826 role_assign($roleid, $user->id, $context->id);
1828 // Assign the capabilities to the role.
1829 foreach ($capabilities as $capname => $capvalue) {
1830 assign_capability($capname, $capvalue, $roleid, $context->id);
1833 // Create the question.
1834 $qtype = 'truefalse';
1835 $overrides = [
1836 'category' => $questioncat->id,
1837 'createdby' => ($isowner) ? $user->id : $otheruser->id,
1840 $question = $questiongenerator->create_question($qtype, null, $overrides);
1842 $this->setUser($user);
1843 $result = question_has_capability_on((string) $question->id, $capability);
1844 $this->assertEquals($expect, $result);
1848 * Tests for the question_has_capability_on function when using a moved question.
1850 * @dataProvider question_capability_on_question_provider
1851 * @param array $capabilities The capability assignments to set.
1852 * @param string $capability The capability to test
1853 * @param bool $isowner Whether the user to create the question should be the owner or not.
1854 * @param bool $expect The expected result.
1856 public function test_question_has_capability_on_using_moved_question($capabilities, $capability, $isowner, $expect) {
1857 $this->resetAfterTest();
1859 // Create the test data.
1860 $generator = $this->getDataGenerator();
1861 $questiongenerator = $generator->get_plugin_generator('core_question');
1862 $user = $generator->create_user();
1863 $otheruser = $generator->create_user();
1864 $roleid = $generator->create_role();
1865 $category = $generator->create_category();
1866 $context = context_coursecat::instance($category->id);
1867 $questioncat = $questiongenerator->create_question_category([
1868 'contextid' => $context->id,
1871 $newcategory = $generator->create_category();
1872 $newcontext = context_coursecat::instance($newcategory->id);
1873 $newquestioncat = $questiongenerator->create_question_category([
1874 'contextid' => $newcontext->id,
1877 // Assign the user to the role in the _new_ context..
1878 role_assign($roleid, $user->id, $newcontext->id);
1880 // Assign the capabilities to the role in the _new_ context.
1881 foreach ($capabilities as $capname => $capvalue) {
1882 assign_capability($capname, $capvalue, $roleid, $newcontext->id);
1885 // Create the question.
1886 $qtype = 'truefalse';
1887 $overrides = [
1888 'category' => $questioncat->id,
1889 'createdby' => ($isowner) ? $user->id : $otheruser->id,
1892 $question = $questiongenerator->create_question($qtype, null, $overrides);
1894 // Move the question.
1895 question_move_questions_to_category([$question->id], $newquestioncat->id);
1897 // Test that the capability is correct after the question has been moved.
1898 $this->setUser($user);
1899 $result = question_has_capability_on($question->id, $capability);
1900 $this->assertEquals($expect, $result);
1904 * Tests for the question_has_capability_on function when using a real question.
1906 * @dataProvider question_capability_on_question_provider
1907 * @param array $capabilities The capability assignments to set.
1908 * @param string $capability The capability to test
1909 * @param bool $isowner Whether the user to create the question should be the owner or not.
1910 * @param bool $expect The expected result.
1912 public function test_question_has_capability_on_using_question($capabilities, $capability, $isowner, $expect) {
1913 $this->resetAfterTest();
1915 // Create the test data.
1916 $generator = $this->getDataGenerator();
1917 $questiongenerator = $generator->get_plugin_generator('core_question');
1918 $user = $generator->create_user();
1919 $otheruser = $generator->create_user();
1920 $roleid = $generator->create_role();
1921 $category = $generator->create_category();
1922 $context = context_coursecat::instance($category->id);
1923 $questioncat = $questiongenerator->create_question_category([
1924 'contextid' => $context->id,
1927 // Assign the user to the role.
1928 role_assign($roleid, $user->id, $context->id);
1930 // Assign the capabilities to the role.
1931 foreach ($capabilities as $capname => $capvalue) {
1932 assign_capability($capname, $capvalue, $roleid, $context->id);
1935 // Create the question.
1936 $question = $questiongenerator->create_question('truefalse', null, [
1937 'category' => $questioncat->id,
1938 'createdby' => ($isowner) ? $user->id : $otheruser->id,
1940 $question = question_bank::load_question_data($question->id);
1942 $this->setUser($user);
1943 $result = question_has_capability_on($question, $capability);
1944 $this->assertEquals($expect, $result);
1948 * Tests that question_has_capability_on throws an exception for wrong parameter types.
1950 public function test_question_has_capability_on_wrong_param_type() {
1951 // Create the test data.
1952 $generator = $this->getDataGenerator();
1953 $questiongenerator = $generator->get_plugin_generator('core_question');
1954 $user = $generator->create_user();
1956 $category = $generator->create_category();
1957 $context = context_coursecat::instance($category->id);
1958 $questioncat = $questiongenerator->create_question_category([
1959 'contextid' => $context->id,
1962 // Create the question.
1963 $question = $questiongenerator->create_question('truefalse', null, [
1964 'category' => $questioncat->id,
1965 'createdby' => $user->id,
1967 $question = question_bank::load_question_data($question->id);
1969 $this->setUser($user);
1970 $result = question_has_capability_on((string)$question->id, 'tag');
1971 $this->assertFalse($result);
1973 $this->expectException('coding_exception');
1974 $this->expectExceptionMessage('$questionorid parameter needs to be an integer or an object.');
1975 question_has_capability_on('one', 'tag');
1979 * Test of question_categorylist_parents function.
1981 public function test_question_categorylist_parents() {
1982 $this->resetAfterTest();
1983 $generator = $this->getDataGenerator();
1984 $questiongenerator = $generator->get_plugin_generator('core_question');
1985 $category = $generator->create_category();
1986 $context = context_coursecat::instance($category->id);
1987 // Create a top category.
1988 $cat0 = question_get_top_category($context->id, true);
1989 // Add sub-categories.
1990 $cat1 = $questiongenerator->create_question_category(['parent' => $cat0->id]);
1991 $cat2 = $questiongenerator->create_question_category(['parent' => $cat1->id]);
1992 // Test the 'get parents' function.
1993 $parentcategories = question_categorylist_parents($cat2->id);
1994 $this->assertEquals($cat0->id, $parentcategories[0]);
1995 $this->assertEquals($cat1->id, $parentcategories[1]);
1996 $this->assertCount(2, $parentcategories);
2000 * Get test cases for test_core_question_find_next_unused_idnumber.
2002 * @return array test cases.
2004 public function find_next_unused_idnumber_cases(): array {
2005 return [
2006 ['id', null],
2007 ['id1a', null],
2008 ['id001', 'id002'],
2009 ['id9', 'id10'],
2010 ['id009', 'id010'],
2011 ['id999', 'id1000'],
2012 ['0', '1'],
2013 ['-1', '-2'],
2014 ['01', '02'],
2015 ['09', '10'],
2016 ['1.0E+29', '1.0E+30'], // Idnumbers are strings, not floats.
2017 ['1.0E-29', '1.0E-30'], // By the way, this is not a sensible idnumber!
2018 ['10.1', '10.2'],
2019 ['10.9', '10.10'],
2025 * Test core_question_find_next_unused_idnumber in the case when there are no other questions.
2027 * @dataProvider find_next_unused_idnumber_cases
2028 * @param string $oldidnumber value to pass to core_question_find_next_unused_idnumber.
2029 * @param string|null $expectednewidnumber expected result.
2031 public function test_core_question_find_next_unused_idnumber(string $oldidnumber, ?string $expectednewidnumber) {
2032 $this->assertSame($expectednewidnumber, core_question_find_next_unused_idnumber($oldidnumber, 0));
2035 public function test_core_question_find_next_unused_idnumber_skips_used() {
2036 $this->resetAfterTest();
2038 /** @var core_question_generator $generator */
2039 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
2040 $category = $generator->create_question_category();
2041 $othercategory = $generator->create_question_category();
2042 $generator->create_question('truefalse', null, ['category' => $category->id, 'idnumber' => 'id9']);
2043 $generator->create_question('truefalse', null, ['category' => $category->id, 'idnumber' => 'id10']);
2044 // Next one to make sure only idnumbers from the right category are ruled out.
2045 $generator->create_question('truefalse', null, ['category' => $othercategory->id, 'idnumber' => 'id11']);
2047 $this->assertSame('id11', core_question_find_next_unused_idnumber('id9', $category->id));
2048 $this->assertSame('id11', core_question_find_next_unused_idnumber('id8', $category->id));
2052 * Tests for the question_move_questions_to_category function.
2054 * @covers ::question_move_questions_to_category
2056 public function test_question_move_questions_to_category() {
2057 $this->resetAfterTest();
2059 // Create the test data.
2060 list($category1, $course1, $quiz1, $questioncat1, $questions1) = $this->setup_quiz_and_questions();
2061 list($category2, $course2, $quiz2, $questioncat2, $questions2) = $this->setup_quiz_and_questions();
2063 $this->assertCount(2, $questions1);
2064 $this->assertCount(2, $questions2);
2065 $questionsidtomove = [];
2066 foreach ($questions1 as $question1) {
2067 $questionsidtomove[] = $question1->id;
2070 // Move the question from quiz 1 to quiz 2.
2071 question_move_questions_to_category($questionsidtomove, $questioncat2->id);
2072 $this->assert_category_contains_questions($questioncat2->id, 4);
2076 * Tests for the idnumber_exist_in_question_category function.
2078 * @covers ::idnumber_exist_in_question_category
2080 public function test_idnumber_exist_in_question_category() {
2081 global $DB;
2083 $this->resetAfterTest();
2085 // Create the test data.
2086 list($category1, $course1, $quiz1, $questioncat1, $questions1) = $this->setup_quiz_and_questions();
2087 list($category2, $course2, $quiz2, $questioncat2, $questions2) = $this->setup_quiz_and_questions();
2089 $questionbankentry1 = get_question_bank_entry($questions1[0]->id);
2090 $entry = new stdClass();
2091 $entry->id = $questionbankentry1->id;
2092 $entry->idnumber = 1;
2093 $DB->update_record('question_bank_entries', $entry);
2095 $questionbankentry2 = get_question_bank_entry($questions2[0]->id);
2096 $entry2 = new stdClass();
2097 $entry2->id = $questionbankentry2->id;
2098 $entry2->idnumber = 1;
2099 $DB->update_record('question_bank_entries', $entry2);
2101 $questionbe = $DB->get_record('question_bank_entries', ['id' => $questionbankentry1->id]);
2103 // Validate that a first stage of an idnumber exists (this format: xxxx_x).
2104 list($response, $record) = idnumber_exist_in_question_category($questionbe->idnumber, $questioncat1->id);
2105 $this->assertEquals([], $record);
2106 $this->assertEquals(true, $response);
2108 // Move the question to a category that has a question with the same idnumber.
2109 question_move_questions_to_category($questions1[0]->id, $questioncat2->id);
2111 // Validate that function return the last record used for the idnumber.
2112 list($response, $record) = idnumber_exist_in_question_category($questionbe->idnumber, $questioncat2->id);
2113 $record = reset($record);
2114 $idnumber = $record->idnumber;
2115 $this->assertEquals($idnumber, '1_1');
2116 $this->assertEquals(true, $response);
2120 * Test method is_latest().
2122 * @covers ::is_latest
2125 public function test_is_latest() {
2126 global $DB;
2127 $this->resetAfterTest();
2128 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
2129 $qcat1 = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat']);
2130 $question = $generator->create_question('shortanswer', null, ['name' => 'q1', 'category' => $qcat1->id]);
2131 $record = $DB->get_record('question_versions', ['questionid' => $question->id]);
2132 $firstversion = $record->version;
2133 $questionbankentryid = $record->questionbankentryid;
2134 $islatest = is_latest($firstversion, $questionbankentryid);
2135 $this->assertTrue($islatest);
2139 * Test question bank entry deletion.
2141 * @covers ::delete_question_bank_entry
2143 public function test_delete_question_bank_entry() {
2144 global $DB;
2145 $this->resetAfterTest();
2146 // Setup.
2147 $context = context_system::instance();
2148 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2149 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
2150 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
2151 // Make sure there is an entry in the entry table.
2152 $sql = 'SELECT qbe.id as id,
2153 qv.id as versionid
2154 FROM {question_bank_entries} qbe
2155 JOIN {question_versions} qv
2156 ON qbe.id = qv.questionbankentryid
2157 JOIN {question} q
2158 ON qv.questionid = q.id
2159 WHERE q.id = ?';
2160 $records = $DB->get_records_sql($sql, [$q1->id]);
2161 $this->assertCount(1, $records);
2162 // Delete the record.
2163 $record = reset($records);
2164 delete_question_bank_entry($record->id);
2165 $records = $DB->get_records('question_bank_entries', ['id' => $record->id]);
2166 // As the version record exists, it wont delete the data to resolve any errors.
2167 $this->assertCount(1, $records);
2168 $DB->delete_records('question_versions', ['id' => $record->versionid]);
2169 delete_question_bank_entry($record->id);
2170 $records = $DB->get_records('question_bank_entries', ['id' => $record->id]);
2171 $this->assertCount(0, $records);
2175 * Test question bank entry object.
2177 * @covers ::get_question_bank_entry
2179 public function test_get_question_bank_entry() {
2180 global $DB;
2181 $this->resetAfterTest();
2182 // Setup.
2183 $context = context_system::instance();
2184 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2185 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
2186 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
2187 // Make sure there is an entry in the entry table.
2188 $sql = 'SELECT qbe.id as id,
2189 qv.id as versionid
2190 FROM {question_bank_entries} qbe
2191 JOIN {question_versions} qv
2192 ON qbe.id = qv.questionbankentryid
2193 JOIN {question} q
2194 ON qv.questionid = q.id
2195 WHERE q.id = ?';
2196 $records = $DB->get_records_sql($sql, [$q1->id]);
2197 $this->assertCount(1, $records);
2198 $record = reset($records);
2199 $questionbankentry = get_question_bank_entry($q1->id);
2200 $this->assertEquals($questionbankentry->id, $record->id);
2204 * Test the version objects for a question.
2206 * @covers ::get_question_version
2208 public function test_get_question_version() {
2209 global $DB;
2210 $this->resetAfterTest();
2211 // Setup.
2212 $context = context_system::instance();
2213 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2214 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
2215 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
2216 // Make sure there is an entry in the entry table.
2217 $sql = 'SELECT qbe.id as id,
2218 qv.id as versionid
2219 FROM {question_bank_entries} qbe
2220 JOIN {question_versions} qv
2221 ON qbe.id = qv.questionbankentryid
2222 JOIN {question} q
2223 ON qv.questionid = q.id
2224 WHERE q.id = ?';
2225 $records = $DB->get_records_sql($sql, [$q1->id]);
2226 $this->assertCount(1, $records);
2227 $record = reset($records);
2228 $questionversions = get_question_version($q1->id);
2229 $questionversion = reset($questionversions);
2230 $this->assertEquals($questionversion->id, $record->versionid);
2234 * Test get next version of a question.
2236 * @covers ::get_next_version
2238 public function test_get_next_version() {
2239 global $DB;
2240 $this->resetAfterTest();
2241 // Setup.
2242 $context = context_system::instance();
2243 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2244 $qcat = $qgen->create_question_category(array('contextid' => $context->id));
2245 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
2246 // Make sure there is an entry in the entry table.
2247 $sql = 'SELECT qbe.id as id,
2248 qv.id as versionid,
2249 qv.version
2250 FROM {question_bank_entries} qbe
2251 JOIN {question_versions} qv
2252 ON qbe.id = qv.questionbankentryid
2253 JOIN {question} q
2254 ON qv.questionid = q.id
2255 WHERE q.id = ?';
2256 $records = $DB->get_records_sql($sql, [$q1->id]);
2257 $this->assertCount(1, $records);
2258 $record = reset($records);
2259 $this->assertEquals(1, $record->version);
2260 $nextversion = get_next_version($record->id);
2261 $this->assertEquals(2, $nextversion);