2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
21 defined('MOODLE_INTERNAL') ||
die();
25 require_once($CFG->libdir
. '/questionlib.php');
26 require_once($CFG->dirroot
. '/mod/quiz/locallib.php');
28 // Get the necessary files to perform backup and restore.
29 require_once($CFG->dirroot
. '/backup/util/includes/backup_includes.php');
30 require_once($CFG->dirroot
. '/backup/util/includes/restore_includes.php');
33 * Unit tests for (some of) ../questionlib.php.
37 * @copyright 2006 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class questionlib_test
extends \advanced_testcase
{
45 * This is executed before running any test in this file.
47 public function setUp(): void
{
48 $this->resetAfterTest();
52 * Setup a course, a quiz, a question category and a question for testing.
54 * @param string $type The type of question category to create.
55 * @return array The created data objects
57 public function setup_quiz_and_questions($type = 'module') {
58 // Create course category.
59 $category = $this->getDataGenerator()->create_category();
62 $course = $this->getDataGenerator()->create_course(array(
64 'category' => $category->id
68 'course' => $course->id
,
72 // Generate an assignment with due date (will generate a course event).
73 $quiz = $this->getDataGenerator()->create_module('quiz', $options);
75 /** @var \core_question_generator $qgen */
76 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
80 $context = \context_course
::instance($course->id
);
84 $context = \context_coursecat
::instance($category->id
);
88 $context = \context_system
::instance();
92 $context = \context_module
::instance($quiz->cmid
);
96 $qcat = $qgen->create_question_category(array('contextid' => $context->id
));
99 $qgen->create_question('shortanswer', null, array('category' => $qcat->id
)),
100 $qgen->create_question('shortanswer', null, array('category' => $qcat->id
)),
103 quiz_add_quiz_question($questions[0]->id
, $quiz);
105 return array($category, $course, $quiz, $qcat, $questions);
109 * Assert that a category contains a specific number of questions.
111 * @param int $categoryid int Category id.
112 * @param int $numberofquestions Number of question in a category.
113 * @return void Questions in a category.
115 protected function assert_category_contains_questions(int $categoryid, int $numberofquestions): void
{
116 $questionsid = question_bank
::get_finder()->get_questions_from_categories([$categoryid], null);
117 $this->assertEquals($numberofquestions, count($questionsid));
120 public function test_question_reorder_qtypes() {
122 array(0 => 't2', 1 => 't1', 2 => 't3'),
123 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +
1));
125 array(0 => 't1', 1 => 't2', 2 => 't3'),
126 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', -1));
128 array(0 => 't2', 1 => 't1', 2 => 't3'),
129 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't2', -1));
131 array(0 => 't1', 1 => 't2', 2 => 't3'),
132 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't3', +
1));
134 array(0 => 't1', 1 => 't2', 2 => 't3'),
135 question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 'missing', +
1));
138 public function test_match_grade_options() {
139 $gradeoptions = question_bank
::fraction_options_full();
141 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'error'));
142 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'error'));
143 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'error'));
144 $this->assertFalse(match_grade_options($gradeoptions, 0.3333, 'error'));
146 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'nearest'));
147 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'nearest'));
148 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'nearest'));
149 $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33, 'nearest'));
151 $this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
155 * This function tests that the functions responsible for moving questions to
156 * different contexts also updates the tag instances associated with the questions.
158 public function test_altering_tag_instance_context() {
161 // Set to admin user.
162 $this->setAdminUser();
164 // Create two course categories - we are going to delete one of these later and will expect
165 // all the questions belonging to the course in the deleted category to be moved.
166 $coursecat1 = $this->getDataGenerator()->create_category();
167 $coursecat2 = $this->getDataGenerator()->create_category();
169 // Create a couple of categories and questions.
170 $context1 = \context_coursecat
::instance($coursecat1->id
);
171 $context2 = \context_coursecat
::instance($coursecat2->id
);
172 /** @var \core_question_generator $questiongenerator */
173 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
174 $questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
176 $questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
178 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id
));
179 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id
));
180 $question3 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id
));
181 $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id
));
183 // Now lets tag these questions.
184 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $context1, array('tag 1', 'tag 2'));
185 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $context1, array('tag 3', 'tag 4'));
186 \core_tag_tag
::set_item_tags('core_question', 'question', $question3->id
, $context2, array('tag 5', 'tag 6'));
187 \core_tag_tag
::set_item_tags('core_question', 'question', $question4->id
, $context2, array('tag 7', 'tag 8'));
189 // Test moving the questions to another category.
190 question_move_questions_to_category(array($question1->id
, $question2->id
), $questioncat2->id
);
192 // Test that all tag_instances belong to one context.
193 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
194 'contextid' => $questioncat2->contextid
)));
196 // Test moving them back.
197 question_move_questions_to_category(array($question1->id
, $question2->id
), $questioncat1->id
);
199 // Test that all tag_instances are now reset to how they were initially.
200 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
201 'contextid' => $questioncat1->contextid
)));
202 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
203 'contextid' => $questioncat2->contextid
)));
205 // Now test moving a whole question category to another context.
206 question_move_category_to_context($questioncat1->id
, $questioncat1->contextid
, $questioncat2->contextid
);
208 // Test that all tag_instances belong to one context.
209 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
210 'contextid' => $questioncat2->contextid
)));
212 // Now test moving them back.
213 question_move_category_to_context($questioncat1->id
, $questioncat2->contextid
,
214 \context_coursecat
::instance($coursecat1->id
)->id
);
216 // Test that all tag_instances are now reset to how they were initially.
217 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
218 'contextid' => $questioncat1->contextid
)));
219 $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
220 'contextid' => $questioncat2->contextid
)));
222 // Now we want to test deleting the course category and moving the questions to another category.
223 question_delete_course_category($coursecat1, $coursecat2);
225 // Test that all tag_instances belong to one context.
226 $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
227 'contextid' => $questioncat2->contextid
)));
230 $course = $this->getDataGenerator()->create_course();
232 // Create some question categories and questions in this course.
233 $coursecontext = \context_course
::instance($course->id
);
234 $questioncat = $questiongenerator->create_question_category(array('contextid' => $coursecontext->id
));
235 $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id
));
236 $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id
));
238 // Add some tags to these questions.
239 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, array('tag 1', 'tag 2'));
240 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, array('tag 1', 'tag 2'));
242 // Create a course that we are going to restore the other course to.
243 $course2 = $this->getDataGenerator()->create_course();
245 // Create backup file and save it to the backup location.
246 $bc = new \backup_controller
(\backup
::TYPE_1COURSE
, $course->id
, \backup
::FORMAT_MOODLE
,
247 \backup
::INTERACTIVE_NO
, \backup
::MODE_GENERAL
, 2);
249 $results = $bc->get_results();
250 $file = $results['backup_destination'];
251 $fp = get_file_packer('application/vnd.moodle.backup');
252 $filepath = $CFG->dataroot
. '/temp/backup/test-restore-course';
253 $file->extract_to_pathname($fp, $filepath);
256 // Now restore the course.
257 $rc = new \restore_controller
('test-restore-course', $course2->id
, \backup
::INTERACTIVE_NO
,
258 \backup
::MODE_GENERAL
, 2, \backup
::TARGET_NEW_COURSE
);
259 $rc->execute_precheck();
262 // Get the created question category.
263 $restoredcategory = $DB->get_record_select('question_categories', 'contextid = ? AND parent <> 0',
264 array(\context_course
::instance($course2->id
)->id
), '*', MUST_EXIST
);
266 // Check that there are two questions in the restored to course's context.
267 $this->assertEquals(2, $DB->get_record_sql('SELECT COUNT(q.id) as questioncount
269 JOIN {question_versions} qv
270 ON qv.questionid = q.id
271 JOIN {question_bank_entries} qbe
272 ON qbe.id = qv.questionbankentryid
273 WHERE qbe.questioncategoryid = ?',
274 [$restoredcategory->id
])->questioncount
);
279 * Test that deleting a question from the question bank works in the normal case.
281 public function test_question_delete_question() {
285 $context = \context_system
::instance();
286 /** @var \core_question_generator $qgen */
287 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
288 $qcat = $qgen->create_question_category(array('contextid' => $context->id
));
289 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
290 $q2 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
293 question_delete_question($q1->id
);
296 $this->assertFalse($DB->record_exists('question', ['id' => $q1->id
]));
297 // Check that we did not delete too much.
298 $this->assertTrue($DB->record_exists('question', ['id' => $q2->id
]));
302 * Test that deleting a broken question from the question bank does not cause fatal errors.
304 public function test_question_delete_question_broken_data() {
308 $context = \context_system
::instance();
309 /** @var \core_question_generator $qgen */
310 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
311 $qcat = $qgen->create_question_category(array('contextid' => $context->id
));
312 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
314 // Now delete the category, to simulate what happens in old sites where
315 // referential integrity has failed.
316 $DB->delete_records('question_categories', ['id' => $qcat->id
]);
319 question_delete_question($q1->id
);
322 $this->assertDebuggingCalled('Deleting question ' . $q1->id
.
323 ' which is no longer linked to a context. Assuming system context ' .
324 'to avoid errors, but this may mean that some data like ' .
325 'files, tags, are not cleaned up.');
326 $this->assertFalse($DB->record_exists('question', ['id' => $q1->id
]));
330 * Test deleting a broken question whose category refers to a missing context
332 public function test_question_delete_question_missing_context() {
335 $coursecategory = $this->getDataGenerator()->create_category();
336 $context = $coursecategory->get_context();
338 /** @var \core_question_generator $generator */
339 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
340 $questioncategory = $generator->create_question_category(['contextid' => $context->id
]);
341 $question = $generator->create_question('shortanswer', null, ['category' => $questioncategory->id
]);
343 // Now delete the context, to simulate what happens in old sites where
344 // referential integrity has failed.
345 $DB->delete_records('context', ['id' => $context->id
]);
347 question_delete_question($question->id
);
349 $this->assertDebuggingCalled('Deleting question ' . $question->id
.
350 ' which is no longer linked to a context. Assuming system context ' .
351 'to avoid errors, but this may mean that some data like ' .
352 'files, tags, are not cleaned up.');
353 $this->assertFalse($DB->record_exists('question', ['id' => $question->id
]));
357 * This function tests the question_category_delete_safe function.
359 public function test_question_category_delete_safe() {
361 $this->resetAfterTest(true);
362 $this->setAdminUser();
364 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
366 question_category_delete_safe($qcat);
368 // Verify category deleted.
369 $criteria = array('id' => $qcat->id
);
370 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
372 // Verify questions deleted or moved.
373 $this->assert_category_contains_questions($qcat->id
, 0);
375 // Verify question not deleted.
376 $criteria = array('id' => $questions[0]->id
);
377 $this->assertEquals(1, $DB->count_records('question', $criteria));
381 * This function tests the question_delete_activity function.
383 public function test_question_delete_activity() {
385 $this->resetAfterTest(true);
386 $this->setAdminUser();
388 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
390 $cm = get_coursemodule_from_instance('quiz', $quiz->id
);
392 // Test the deletion.
393 question_delete_activity($cm);
395 // Verify category deleted.
396 $criteria = array('id' => $qcat->id
);
397 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
399 // Verify questions deleted or moved.
400 $this->assert_category_contains_questions($qcat->id
, 0);
404 * This function tests the question_delete_context function.
406 public function test_question_delete_context() {
408 $this->resetAfterTest(true);
409 $this->setAdminUser();
411 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
413 // Get the module context id.
414 $result = question_delete_context($qcat->contextid
);
416 // Verify category deleted.
417 $criteria = array('id' => $qcat->id
);
418 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
420 // Verify questions deleted or moved.
421 $this->assert_category_contains_questions($qcat->id
, 0);
425 * This function tests the question_delete_course function.
427 public function test_question_delete_course() {
429 $this->resetAfterTest(true);
430 $this->setAdminUser();
432 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
434 // Test the deletion.
435 question_delete_course($course);
437 // Verify category deleted.
438 $criteria = array('id' => $qcat->id
);
439 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
441 // Verify questions deleted or moved.
442 $this->assert_category_contains_questions($qcat->id
, 0);
446 * This function tests the question_delete_course_category function.
448 public function test_question_delete_course_category() {
450 $this->resetAfterTest(true);
451 $this->setAdminUser();
453 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
455 // Test that the feedback works.
456 question_delete_course_category($category, null);
458 // Verify category deleted.
459 $criteria = array('id' => $qcat->id
);
460 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
462 // Verify questions deleted or moved.
463 $this->assert_category_contains_questions($qcat->id
, 0);
467 * This function tests the question_delete_course_category function when it is supposed to move question categories.
469 public function test_question_delete_course_category_move_qcats() {
471 $this->resetAfterTest(true);
472 $this->setAdminUser();
474 list($category1, $course1, $quiz1, $qcat1, $questions1) = $this->setup_quiz_and_questions('category');
475 list($category2, $course2, $quiz2, $qcat2, $questions2) = $this->setup_quiz_and_questions('category');
477 $questionsinqcat1 = count($questions1);
478 $questionsinqcat2 = count($questions2);
481 question_delete_course_category($category1, $category2);
483 // Verify category not deleted.
484 $criteria = array('id' => $qcat1->id
);
485 $this->assertEquals(1, $DB->count_records('question_categories', $criteria));
487 // Verify questions are moved.
488 $params = array($qcat2->contextid
);
489 $actualquestionscount = $DB->count_records_sql("SELECT COUNT(*)
491 JOIN {question_versions} qv ON qv.questionid = q.id
492 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
493 JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
494 WHERE qc.contextid = ?", $params);
495 $this->assertEquals($questionsinqcat1 +
$questionsinqcat2, $actualquestionscount);
497 // Verify there is just a single top-level category.
498 $criteria = array('contextid' => $qcat2->contextid
, 'parent' => 0);
499 $this->assertEquals(1, $DB->count_records('question_categories', $criteria));
501 // Verify there is no question category in previous context.
502 $criteria = array('contextid' => $qcat1->contextid
);
503 $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
507 * This function tests the question_save_from_deletion function when it is supposed to make a new category and
508 * move question categories to that new category.
510 public function test_question_save_from_deletion() {
512 $this->resetAfterTest(true);
513 $this->setAdminUser();
515 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
517 $context = \context
::instance_by_id($qcat->contextid
);
519 $newcat = question_save_from_deletion(array_column($questions, 'id'),
520 $context->get_parent_context()->id
, $context->get_context_name());
522 // Verify that the newcat itself is not a tep level category.
523 $this->assertNotEquals(0, $newcat->parent
);
525 // Verify there is just a single top-level category.
526 $this->assertEquals(1, $DB->count_records('question_categories', ['contextid' => $qcat->contextid
, 'parent' => 0]));
530 * This function tests the question_save_from_deletion function when it is supposed to make a new category and
531 * move question categories to that new category when quiz name is very long but less than 256 characters.
533 public function test_question_save_from_deletion_quiz_with_long_name() {
535 $this->resetAfterTest(true);
536 $this->setAdminUser();
538 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
540 // Moodle doesn't allow you to enter a name longer than 255 characters.
541 $quiz->name
= shorten_text(str_repeat('123456789 ', 26), 255);
543 $DB->update_record('quiz', $quiz);
545 $context = \context
::instance_by_id($qcat->contextid
);
547 $newcat = question_save_from_deletion(array_column($questions, 'id'),
548 $context->get_parent_context()->id
, $context->get_context_name());
550 // Verifying that the inserted record's name is expected or not.
551 $this->assertEquals($DB->get_record('question_categories', ['id' => $newcat->id
])->name
, $newcat->name
);
553 // Verify that the newcat itself is not a top level category.
554 $this->assertNotEquals(0, $newcat->parent
);
556 // Verify there is just a single top-level category.
557 $this->assertEquals(1, $DB->count_records('question_categories', ['contextid' => $qcat->contextid
, 'parent' => 0]));
561 * get_question_options should add the category object to the given question.
563 public function test_get_question_options_includes_category_object_single_question() {
564 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
565 $question = array_shift($questions);
567 get_question_options($question);
569 $this->assertEquals($qcat, $question->categoryobject
);
573 * get_question_options should add the category object to all of the questions in
576 public function test_get_question_options_includes_category_object_multiple_questions() {
577 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
579 get_question_options($questions);
581 foreach ($questions as $question) {
582 $this->assertEquals($qcat, $question->categoryobject
);
587 * get_question_options includes the tags for all questions in the list.
589 public function test_get_question_options_includes_question_tags() {
590 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
591 $question1 = $questions[0];
592 $question2 = $questions[1];
593 $qcontext = \context
::instance_by_id($qcat->contextid
);
595 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo', 'bar']);
596 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['baz', 'bop']);
598 get_question_options($questions, true);
600 foreach ($questions as $question) {
601 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
603 $actualtags = $question->tags
;
604 foreach ($tags as $tag) {
605 $expectedtags[$tag->id
] = $tag->get_display_name();
608 // The question should have a tags property populated with each tag id
609 // and display name as a key vale pair.
610 $this->assertEquals($expectedtags, $actualtags);
612 $actualtagobjects = $question->tagobjects
;
614 sort($actualtagobjects);
616 // The question should have a full set of each tag object.
617 $this->assertEquals($tags, $actualtagobjects);
618 // The question should not have any course tags.
619 $this->assertEmpty($question->coursetagobjects
);
624 * get_question_options includes the course tags for all questions in the list.
626 public function test_get_question_options_includes_course_tags() {
627 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
628 $question1 = $questions[0];
629 $question2 = $questions[1];
630 $coursecontext = \context_course
::instance($course->id
);
632 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['foo', 'bar']);
633 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['baz', 'bop']);
635 get_question_options($questions, true);
637 foreach ($questions as $question) {
638 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
639 $expectedcoursetags = [];
640 $actualcoursetags = $question->coursetags
;
641 foreach ($tags as $tag) {
642 $expectedcoursetags[$tag->id
] = $tag->get_display_name();
645 // The question should have a coursetags property populated with each tag id
646 // and display name as a key vale pair.
647 $this->assertEquals($expectedcoursetags, $actualcoursetags);
649 $actualcoursetagobjects = $question->coursetagobjects
;
651 sort($actualcoursetagobjects);
653 // The question should have a full set of the course tag objects.
654 $this->assertEquals($tags, $actualcoursetagobjects);
655 // The question should not have any other tags.
656 $this->assertEmpty($question->tagobjects
);
657 $this->assertEmpty($question->tags
);
662 * get_question_options only categorises a tag as a course tag if it is in a
663 * course context that is different from the question context.
665 public function test_get_question_options_course_tags_in_course_question_context() {
666 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
667 $question1 = $questions[0];
668 $question2 = $questions[1];
669 $coursecontext = \context_course
::instance($course->id
);
671 // Create course level tags in the course context that matches the question
673 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['foo', 'bar']);
674 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['baz', 'bop']);
676 get_question_options($questions, true);
678 foreach ($questions as $question) {
679 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
681 $actualtagobjects = $question->tagobjects
;
683 sort($actualtagobjects);
685 // The tags should not be considered course tags because they are in
686 // the same context as the question. That makes them question tags.
687 $this->assertEmpty($question->coursetagobjects
);
688 // The course context tags should be returned in the regular tag object
690 $this->assertEquals($tags, $actualtagobjects);
695 * get_question_options includes the tags and course tags for all questions in the list
696 * if each question has course and question level tags.
698 public function test_get_question_options_includes_question_and_course_tags() {
699 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
700 $question1 = $questions[0];
701 $question2 = $questions[1];
702 $qcontext = \context
::instance_by_id($qcat->contextid
);
703 $coursecontext = \context_course
::instance($course->id
);
705 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo', 'bar']);
706 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['cfoo', 'cbar']);
707 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['baz', 'bop']);
708 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['cbaz', 'cbop']);
710 get_question_options($questions, true);
712 foreach ($questions as $question) {
713 $alltags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
714 $tags = array_filter($alltags, function($tag) use ($qcontext) {
715 return $tag->taginstancecontextid
== $qcontext->id
;
717 $coursetags = array_filter($alltags, function($tag) use ($coursecontext) {
718 return $tag->taginstancecontextid
== $coursecontext->id
;
722 $actualtags = $question->tags
;
723 foreach ($tags as $tag) {
724 $expectedtags[$tag->id
] = $tag->get_display_name();
727 // The question should have a tags property populated with each tag id
728 // and display name as a key vale pair.
729 $this->assertEquals($expectedtags, $actualtags);
731 $actualtagobjects = $question->tagobjects
;
733 sort($actualtagobjects);
734 // The question should have a full set of each tag object.
735 $this->assertEquals($tags, $actualtagobjects);
737 $actualcoursetagobjects = $question->coursetagobjects
;
739 sort($actualcoursetagobjects);
740 // The question should have a full set of course tag objects.
741 $this->assertEquals($coursetags, $actualcoursetagobjects);
746 * get_question_options should update the context id to the question category
747 * context id for any non-course context tag that isn't in the question category
750 public function test_get_question_options_normalises_question_tags() {
751 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
752 $question1 = $questions[0];
753 $question2 = $questions[1];
754 $qcontext = \context
::instance_by_id($qcat->contextid
);
755 $systemcontext = \context_system
::instance();
757 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo', 'bar']);
758 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['baz', 'bop']);
760 $q1tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question1->id
);
761 $q2tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question2->id
);
762 $q1tag = array_shift($q1tags);
763 $q2tag = array_shift($q2tags);
765 // Change two of the tag instances to be a different (non-course) context to the
766 // question tag context. These tags should then be normalised back to the question
768 \core_tag_tag
::change_instances_context([$q1tag->taginstanceid
, $q2tag->taginstanceid
], $systemcontext);
770 get_question_options($questions, true);
772 foreach ($questions as $question) {
773 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
775 // The database should have been updated with the correct context id.
776 foreach ($tags as $tag) {
777 $this->assertEquals($qcontext->id
, $tag->taginstancecontextid
);
780 // The tag objects on the question should have been updated with the
781 // correct context id.
782 foreach ($question->tagobjects
as $tag) {
783 $this->assertEquals($qcontext->id
, $tag->taginstancecontextid
);
789 * get_question_options if the question is a course level question then tags
790 * in that context should not be consdered course tags, they are question tags.
792 public function test_get_question_options_includes_course_context_question_tags() {
793 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
794 $question1 = $questions[0];
795 $question2 = $questions[1];
796 $coursecontext = \context_course
::instance($course->id
);
798 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['foo', 'bar']);
799 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['baz', 'bop']);
801 get_question_options($questions, true);
803 foreach ($questions as $question) {
804 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
805 // Tags in a course context that matches the question context should
806 // not be considered course tags.
807 $this->assertEmpty($question->coursetagobjects
);
808 $this->assertEmpty($question->coursetags
);
810 $actualtagobjects = $question->tagobjects
;
812 sort($actualtagobjects);
813 // The tags should be considered question tags not course tags.
814 $this->assertEquals($tags, $actualtagobjects);
819 * get_question_options should return tags from all course contexts by default.
821 public function test_get_question_options_includes_multiple_courses_tags() {
822 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
823 $question1 = $questions[0];
824 $question2 = $questions[1];
825 $coursecontext = \context_course
::instance($course->id
);
826 // Create a sibling course.
827 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category
]);
828 $siblingcoursecontext = \context_course
::instance($siblingcourse->id
);
830 // Create course tags.
831 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['c1']);
832 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['c1']);
833 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $siblingcoursecontext, ['c2']);
834 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $siblingcoursecontext, ['c2']);
836 get_question_options($questions, true);
838 foreach ($questions as $question) {
839 $this->assertCount(2, $question->coursetagobjects
);
841 foreach ($question->coursetagobjects
as $tag) {
842 if ($tag->name
== 'c1') {
843 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
845 $this->assertEquals($siblingcoursecontext->id
, $tag->taginstancecontextid
);
852 * get_question_options should filter the course tags by the given list of courses.
854 public function test_get_question_options_includes_filter_course_tags() {
855 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
856 $question1 = $questions[0];
857 $question2 = $questions[1];
858 $coursecontext = \context_course
::instance($course->id
);
859 // Create a sibling course.
860 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category
]);
861 $siblingcoursecontext = \context_course
::instance($siblingcourse->id
);
863 // Create course tags.
864 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['foo']);
865 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['bar']);
866 // Create sibling course tags. These should be filtered out.
867 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $siblingcoursecontext, ['filtered1']);
868 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $siblingcoursecontext, ['filtered2']);
870 // Ask to only receive course tags from $course (ignoring $siblingcourse tags).
871 get_question_options($questions, true, [$course]);
873 foreach ($questions as $question) {
874 foreach ($question->coursetagobjects
as $tag) {
875 // We should only be seeing course tags from $course. The tags from
876 // $siblingcourse should have been filtered out.
877 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
883 * question_move_question_tags_to_new_context should update all of the
884 * question tags contexts when they are moving down (from system to course
887 public function test_question_move_question_tags_to_new_context_system_to_course_cat_qtags() {
888 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
889 $question1 = $questions[0];
890 $question2 = $questions[1];
891 $qcontext = \context
::instance_by_id($qcat->contextid
);
892 $newcontext = \context_coursecat
::instance($category->id
);
894 foreach ($questions as $question) {
895 $question->contextid
= $qcat->contextid
;
898 // Create tags in the system context.
899 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo', 'bar']);
900 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo', 'bar']);
902 question_move_question_tags_to_new_context($questions, $newcontext);
904 foreach ($questions as $question) {
905 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
907 // All of the tags should have their context id set to the new context.
908 foreach ($tags as $tag) {
909 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
915 * question_move_question_tags_to_new_context should update all of the question tags
916 * contexts when they are moving down (from system to course category context)
917 * but leave any tags in the course context where they are.
919 public function test_question_move_question_tags_to_new_context_system_to_course_cat_qtags_and_course_tags() {
920 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
921 $question1 = $questions[0];
922 $question2 = $questions[1];
923 $qcontext = \context
::instance_by_id($qcat->contextid
);
924 $coursecontext = \context_course
::instance($course->id
);
925 $newcontext = \context_coursecat
::instance($category->id
);
927 foreach ($questions as $question) {
928 $question->contextid
= $qcat->contextid
;
931 // Create tags in the system context.
932 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
933 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
934 // Create tags in the course context.
935 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['ctag']);
936 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['ctag']);
938 question_move_question_tags_to_new_context($questions, $newcontext);
940 foreach ($questions as $question) {
941 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
943 foreach ($tags as $tag) {
944 if ($tag->name
== 'ctag') {
945 // Course tags should remain in the course context.
946 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
948 // Other tags should be updated.
949 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
956 * question_move_question_tags_to_new_context should update all of the question
957 * contexts tags when they are moving up (from course category to system context).
959 public function test_question_move_question_tags_to_new_context_course_cat_to_system_qtags() {
960 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
961 $question1 = $questions[0];
962 $question2 = $questions[1];
963 $qcontext = \context
::instance_by_id($qcat->contextid
);
964 $newcontext = \context_system
::instance();
966 foreach ($questions as $question) {
967 $question->contextid
= $qcat->contextid
;
970 // Create tags in the course category context.
971 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo', 'bar']);
972 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo', 'bar']);
974 question_move_question_tags_to_new_context($questions, $newcontext);
976 foreach ($questions as $question) {
977 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
979 // All of the tags should have their context id set to the new context.
980 foreach ($tags as $tag) {
981 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
987 * question_move_question_tags_to_new_context should update all of the question
988 * tags contexts when they are moving up (from course category context to system
989 * context) but leave any tags in the course context where they are.
991 public function test_question_move_question_tags_to_new_context_course_cat_to_system_qtags_and_course_tags() {
992 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
993 $question1 = $questions[0];
994 $question2 = $questions[1];
995 $qcontext = \context
::instance_by_id($qcat->contextid
);
996 $coursecontext = \context_course
::instance($course->id
);
997 $newcontext = \context_system
::instance();
999 foreach ($questions as $question) {
1000 $question->contextid
= $qcat->contextid
;
1003 // Create tags in the system context.
1004 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1005 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1006 // Create tags in the course context.
1007 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['ctag']);
1008 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['ctag']);
1010 question_move_question_tags_to_new_context($questions, $newcontext);
1012 foreach ($questions as $question) {
1013 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1015 foreach ($tags as $tag) {
1016 if ($tag->name
== 'ctag') {
1017 // Course tags should remain in the course context.
1018 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
1020 // Other tags should be updated.
1021 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1028 * question_move_question_tags_to_new_context should merge all tags into the course
1029 * context when moving down from course category context into course context.
1031 public function test_question_move_question_tags_to_new_context_course_cat_to_coures_qtags_and_course_tags() {
1032 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1033 $question1 = $questions[0];
1034 $question2 = $questions[1];
1035 $qcontext = \context
::instance_by_id($qcat->contextid
);
1036 $coursecontext = \context_course
::instance($course->id
);
1037 $newcontext = $coursecontext;
1039 foreach ($questions as $question) {
1040 $question->contextid
= $qcat->contextid
;
1043 // Create tags in the system context.
1044 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1045 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1046 // Create tags in the course context.
1047 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['ctag']);
1048 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['ctag']);
1050 question_move_question_tags_to_new_context($questions, $newcontext);
1052 foreach ($questions as $question) {
1053 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1054 // Each question should have 2 tags.
1055 $this->assertCount(2, $tags);
1057 foreach ($tags as $tag) {
1058 // All tags should be updated to the course context and merged in.
1059 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1065 * question_move_question_tags_to_new_context should delete all of the tag
1066 * instances from sibling courses when moving the context of a question down
1067 * from a course category into a course context because the other courses will
1068 * no longer have access to the question.
1070 public function test_question_move_question_tags_to_new_context_remove_other_course_tags() {
1071 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1072 // Create a sibling course.
1073 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category
]);
1074 $question1 = $questions[0];
1075 $question2 = $questions[1];
1076 $qcontext = \context
::instance_by_id($qcat->contextid
);
1077 $coursecontext = \context_course
::instance($course->id
);
1078 $siblingcoursecontext = \context_course
::instance($siblingcourse->id
);
1079 $newcontext = $coursecontext;
1081 foreach ($questions as $question) {
1082 $question->contextid
= $qcat->contextid
;
1085 // Create tags in the system context.
1086 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1087 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1088 // Create tags in the target course context.
1089 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['ctag']);
1090 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['ctag']);
1091 // Create tags in the sibling course context. These should be deleted as
1092 // part of the move.
1093 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $siblingcoursecontext, ['stag']);
1094 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $siblingcoursecontext, ['stag']);
1096 question_move_question_tags_to_new_context($questions, $newcontext);
1098 foreach ($questions as $question) {
1099 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1100 // Each question should have 2 tags, 'foo' and 'ctag'.
1101 $this->assertCount(2, $tags);
1103 foreach ($tags as $tag) {
1104 $tagname = $tag->name
;
1105 // The 'stag' should have been deleted because it's in a sibling
1107 $this->assertContains($tagname, ['foo', 'ctag']);
1108 // All tags should be in the course context now.
1109 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
1115 * question_move_question_tags_to_new_context should update all of the question
1116 * tags to be the course category context when moving the tags from a course
1117 * context to a course category context.
1119 public function test_question_move_question_tags_to_new_context_course_to_course_cat() {
1120 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
1121 $question1 = $questions[0];
1122 $question2 = $questions[1];
1123 $qcontext = \context
::instance_by_id($qcat->contextid
);
1124 // Moving up into the course category context.
1125 $newcontext = \context_coursecat
::instance($category->id
);
1127 foreach ($questions as $question) {
1128 $question->contextid
= $qcat->contextid
;
1131 // Create tags in the course context.
1132 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1133 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1135 question_move_question_tags_to_new_context($questions, $newcontext);
1137 foreach ($questions as $question) {
1138 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1140 // All of the tags should have their context id set to the new context.
1141 foreach ($tags as $tag) {
1142 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1148 * question_move_question_tags_to_new_context should update all of the
1149 * question tags contexts when they are moving down (from system to course
1150 * category context).
1152 public function test_question_move_question_tags_to_new_context_orphaned_tag_contexts() {
1153 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
1154 $question1 = $questions[0];
1155 $question2 = $questions[1];
1156 $othercategory = $this->getDataGenerator()->create_category();
1157 $qcontext = \context
::instance_by_id($qcat->contextid
);
1158 $newcontext = \context_coursecat
::instance($category->id
);
1159 $othercategorycontext = \context_coursecat
::instance($othercategory->id
);
1161 foreach ($questions as $question) {
1162 $question->contextid
= $qcat->contextid
;
1165 // Create tags in the system context.
1166 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1167 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1168 // Create tags in the other course category context. These should be
1169 // update to the next context id because they represent erroneous data
1170 // from a time before context id was mandatory in the tag API.
1171 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $othercategorycontext, ['bar']);
1172 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $othercategorycontext, ['bar']);
1174 question_move_question_tags_to_new_context($questions, $newcontext);
1176 foreach ($questions as $question) {
1177 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1178 // Each question should have two tags, 'foo' and 'bar'.
1179 $this->assertCount(2, $tags);
1181 // All of the tags should have their context id set to the new context
1182 // (course category context).
1183 foreach ($tags as $tag) {
1184 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1190 * When moving from a course category context down into an activity context
1191 * all question context tags and course tags (where the course is a parent of
1192 * the activity) should move into the new context.
1194 public function test_question_move_question_tags_to_new_context_course_cat_to_activity_qtags_and_course_tags() {
1195 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1196 $question1 = $questions[0];
1197 $question2 = $questions[1];
1198 $qcontext = \context
::instance_by_id($qcat->contextid
);
1199 $coursecontext = \context_course
::instance($course->id
);
1200 $newcontext = \context_module
::instance($quiz->cmid
);
1202 foreach ($questions as $question) {
1203 $question->contextid
= $qcat->contextid
;
1206 // Create tags in the course category context.
1207 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1208 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1209 // Move the questions to the activity context which is a child context of
1211 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['ctag']);
1212 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['ctag']);
1214 question_move_question_tags_to_new_context($questions, $newcontext);
1216 foreach ($questions as $question) {
1217 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1218 // Each question should have 2 tags.
1219 $this->assertCount(2, $tags);
1221 foreach ($tags as $tag) {
1222 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1228 * When moving from a course category context down into an activity context
1229 * all question context tags and course tags (where the course is a parent of
1230 * the activity) should move into the new context. Tags in course contexts
1231 * that are not a parent of the activity context should be deleted.
1233 public function test_question_move_question_tags_to_new_context_course_cat_to_activity_orphaned_tags() {
1234 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1235 $question1 = $questions[0];
1236 $question2 = $questions[1];
1237 $qcontext = \context
::instance_by_id($qcat->contextid
);
1238 $coursecontext = \context_course
::instance($course->id
);
1239 $newcontext = \context_module
::instance($quiz->cmid
);
1240 $othercourse = $this->getDataGenerator()->create_course();
1241 $othercoursecontext = \context_course
::instance($othercourse->id
);
1243 foreach ($questions as $question) {
1244 $question->contextid
= $qcat->contextid
;
1247 // Create tags in the course category context.
1248 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1249 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1250 // Create tags in the course context.
1251 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['ctag']);
1252 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['ctag']);
1253 // Create tags in the other course context. These should be deleted.
1254 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $othercoursecontext, ['delete']);
1255 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $othercoursecontext, ['delete']);
1257 // Move the questions to the activity context which is a child context of
1259 question_move_question_tags_to_new_context($questions, $newcontext);
1261 foreach ($questions as $question) {
1262 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1263 // Each question should have 2 tags.
1264 $this->assertCount(2, $tags);
1266 foreach ($tags as $tag) {
1267 // Make sure we don't have any 'delete' tags.
1268 $this->assertContains($tag->name
, ['foo', 'ctag']);
1269 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1275 * When moving from a course context down into an activity context all of the
1276 * course tags should move into the activity context.
1278 public function test_question_move_question_tags_to_new_context_course_to_activity_qtags() {
1279 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
1280 $question1 = $questions[0];
1281 $question2 = $questions[1];
1282 $qcontext = \context
::instance_by_id($qcat->contextid
);
1283 $newcontext = \context_module
::instance($quiz->cmid
);
1285 foreach ($questions as $question) {
1286 $question->contextid
= $qcat->contextid
;
1289 // Create tags in the course context.
1290 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1291 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1293 question_move_question_tags_to_new_context($questions, $newcontext);
1295 foreach ($questions as $question) {
1296 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1298 foreach ($tags as $tag) {
1299 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1305 * When moving from a course context down into an activity context all of the
1306 * course tags should move into the activity context.
1308 public function test_question_move_question_tags_to_new_context_activity_to_course_qtags() {
1309 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
1310 $question1 = $questions[0];
1311 $question2 = $questions[1];
1312 $qcontext = \context
::instance_by_id($qcat->contextid
);
1313 $newcontext = \context_course
::instance($course->id
);
1315 foreach ($questions as $question) {
1316 $question->contextid
= $qcat->contextid
;
1319 // Create tags in the activity context.
1320 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1321 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1323 question_move_question_tags_to_new_context($questions, $newcontext);
1325 foreach ($questions as $question) {
1326 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1328 foreach ($tags as $tag) {
1329 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1335 * question_move_question_tags_to_new_context should update all of the
1336 * question tags contexts when they are moving down (from system to course
1337 * category context).
1339 * Course tags within the new category context should remain while any course
1340 * tags in course contexts that can no longer access the question should be
1343 public function test_question_move_question_tags_to_new_context_system_to_course_cat_with_orphaned_tags() {
1344 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
1345 $question1 = $questions[0];
1346 $question2 = $questions[1];
1347 $othercategory = $this->getDataGenerator()->create_category();
1348 $othercourse = $this->getDataGenerator()->create_course(['category' => $othercategory->id
]);
1349 $qcontext = \context
::instance_by_id($qcat->contextid
);
1350 $newcontext = \context_coursecat
::instance($category->id
);
1351 $othercategorycontext = \context_coursecat
::instance($othercategory->id
);
1352 $coursecontext = \context_course
::instance($course->id
);
1353 $othercoursecontext = \context_course
::instance($othercourse->id
);
1355 foreach ($questions as $question) {
1356 $question->contextid
= $qcat->contextid
;
1359 // Create tags in the system context.
1360 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo']);
1361 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['foo']);
1362 // Create tags in the child course context of the new context.
1363 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['bar']);
1364 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['bar']);
1365 // Create tags in the other course context. These should be deleted when
1366 // the question moves to the new course category context because this
1367 // course belongs to a different category, which means it will no longer
1368 // have access to the question.
1369 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $othercoursecontext, ['delete']);
1370 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $othercoursecontext, ['delete']);
1372 question_move_question_tags_to_new_context($questions, $newcontext);
1374 foreach ($questions as $question) {
1375 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1376 // Each question should have two tags, 'foo' and 'bar'.
1377 $this->assertCount(2, $tags);
1379 // All of the tags should have their context id set to the new context
1380 // (course category context).
1381 foreach ($tags as $tag) {
1382 $this->assertContains($tag->name
, ['foo', 'bar']);
1384 if ($tag->name
== 'foo') {
1385 $this->assertEquals($newcontext->id
, $tag->taginstancecontextid
);
1387 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
1394 * question_sort_tags() includes the tags for all questions in the list.
1396 public function test_question_sort_tags_includes_question_tags() {
1398 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1399 $question1 = $questions[0];
1400 $question2 = $questions[1];
1401 $qcontext = \context
::instance_by_id($qcat->contextid
);
1403 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $qcontext, ['foo', 'bar']);
1404 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $qcontext, ['baz', 'bop']);
1406 foreach ($questions as $question) {
1407 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1408 $categorycontext = \context
::instance_by_id($qcat->contextid
);
1409 $tagobjects = question_sort_tags($tags, $categorycontext);
1411 $actualtags = $tagobjects->tags
;
1412 foreach ($tagobjects->tagobjects
as $tag) {
1413 $expectedtags[$tag->id
] = $tag->name
;
1416 // The question should have a tags property populated with each tag id
1417 // and display name as a key vale pair.
1418 $this->assertEquals($expectedtags, $actualtags);
1420 $actualtagobjects = $tagobjects->tagobjects
;
1422 sort($actualtagobjects);
1424 // The question should have a full set of each tag object.
1425 $this->assertEquals($tags, $actualtagobjects);
1426 // The question should not have any course tags.
1427 $this->assertEmpty($tagobjects->coursetagobjects
);
1432 * question_sort_tags() includes course tags for all questions in the list.
1434 public function test_question_sort_tags_includes_question_course_tags() {
1437 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1438 $question1 = $questions[0];
1439 $question2 = $questions[1];
1440 $coursecontext = \context_course
::instance($course->id
);
1442 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['foo', 'bar']);
1443 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['baz', 'bop']);
1445 foreach ($questions as $question) {
1446 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1447 $tagobjects = question_sort_tags($tags, $qcat);
1450 $actualtags = $tagobjects->coursetags
;
1451 foreach ($actualtags as $coursetagid => $coursetagname) {
1452 $expectedtags[$coursetagid] = $coursetagname;
1455 // The question should have a tags property populated with each tag id
1456 // and display name as a key vale pair.
1457 $this->assertEquals($expectedtags, $actualtags);
1459 $actualtagobjects = $tagobjects->coursetagobjects
;
1461 sort($actualtagobjects);
1463 // The question should have a full set of each tag object.
1464 $this->assertEquals($tags, $actualtagobjects);
1465 // The question should not have any course tags.
1466 $this->assertEmpty($tagobjects->tagobjects
);
1471 * question_sort_tags() should return tags from all course contexts by default.
1473 public function test_question_sort_tags_includes_multiple_courses_tags() {
1476 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1477 $question1 = $questions[0];
1478 $question2 = $questions[1];
1479 $coursecontext = \context_course
::instance($course->id
);
1480 // Create a sibling course.
1481 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category
]);
1482 $siblingcoursecontext = \context_course
::instance($siblingcourse->id
);
1484 // Create course tags.
1485 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['c1']);
1486 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['c1']);
1487 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $siblingcoursecontext, ['c2']);
1488 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $siblingcoursecontext, ['c2']);
1490 foreach ($questions as $question) {
1491 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1492 $tagobjects = question_sort_tags($tags, $qcat);
1493 $this->assertCount(2, $tagobjects->coursetagobjects
);
1495 foreach ($tagobjects->coursetagobjects
as $tag) {
1496 if ($tag->name
== 'c1') {
1497 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
1499 $this->assertEquals($siblingcoursecontext->id
, $tag->taginstancecontextid
);
1506 * question_sort_tags() should filter the course tags by the given list of courses.
1508 public function test_question_sort_tags_includes_filter_course_tags() {
1511 list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
1512 $question1 = $questions[0];
1513 $question2 = $questions[1];
1514 $coursecontext = \context_course
::instance($course->id
);
1515 // Create a sibling course.
1516 $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category
]);
1517 $siblingcoursecontext = \context_course
::instance($siblingcourse->id
);
1519 // Create course tags.
1520 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $coursecontext, ['foo']);
1521 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $coursecontext, ['bar']);
1522 // Create sibling course tags. These should be filtered out.
1523 \core_tag_tag
::set_item_tags('core_question', 'question', $question1->id
, $siblingcoursecontext, ['filtered1']);
1524 \core_tag_tag
::set_item_tags('core_question', 'question', $question2->id
, $siblingcoursecontext, ['filtered2']);
1526 foreach ($questions as $question) {
1527 $tags = \core_tag_tag
::get_item_tags('core_question', 'question', $question->id
);
1528 $tagobjects = question_sort_tags($tags, $qcat, [$course]);
1529 foreach ($tagobjects->coursetagobjects
as $tag) {
1531 // We should only be seeing course tags from $course. The tags from
1532 // $siblingcourse should have been filtered out.
1533 $this->assertEquals($coursecontext->id
, $tag->taginstancecontextid
);
1539 * Data provider for tests of question_has_capability_on_context and question_require_capability_on_context.
1543 public function question_capability_on_question_provider() {
1545 'Unrelated capability which is present' => [
1547 'moodle/question:config' => CAP_ALLOW
,
1549 'testcapability' => 'config',
1553 'Unrelated capability which is present (not owner)' => [
1555 'moodle/question:config' => CAP_ALLOW
,
1557 'testcapability' => 'config',
1561 'Unrelated capability which is not set' => [
1564 'testcapability' => 'config',
1568 'Unrelated capability which is not set (not owner)' => [
1571 'testcapability' => 'config',
1575 'Unrelated capability which is prevented' => [
1577 'moodle/question:config' => CAP_PREVENT
,
1579 'testcapability' => 'config',
1583 'Unrelated capability which is prevented (not owner)' => [
1585 'moodle/question:config' => CAP_PREVENT
,
1587 'testcapability' => 'config',
1591 'Related capability which is not set' => [
1594 'testcapability' => 'edit',
1598 'Related capability which is not set (not owner)' => [
1601 'testcapability' => 'edit',
1605 'Related capability which is allowed at all, unset at mine' => [
1607 'moodle/question:editall' => CAP_ALLOW
,
1609 'testcapability' => 'edit',
1613 'Related capability which is allowed at all, unset at mine (not owner)' => [
1615 'moodle/question:editall' => CAP_ALLOW
,
1617 'testcapability' => 'edit',
1621 'Related capability which is allowed at all, prevented at mine' => [
1623 'moodle/question:editall' => CAP_ALLOW
,
1624 'moodle/question:editmine' => CAP_PREVENT
,
1626 'testcapability' => 'edit',
1630 'Related capability which is allowed at all, prevented at mine (not owner)' => [
1632 'moodle/question:editall' => CAP_ALLOW
,
1633 'moodle/question:editmine' => CAP_PREVENT
,
1635 'testcapability' => 'edit',
1639 'Related capability which is unset all, allowed at mine' => [
1641 'moodle/question:editall' => CAP_PREVENT
,
1642 'moodle/question:editmine' => CAP_ALLOW
,
1644 'testcapability' => 'edit',
1648 'Related capability which is unset all, allowed at mine (not owner)' => [
1650 'moodle/question:editall' => CAP_PREVENT
,
1651 'moodle/question:editmine' => CAP_ALLOW
,
1653 'testcapability' => 'edit',
1661 * Tests that question_has_capability_on does not throw exception on broken questions.
1663 public function test_question_has_capability_on_broken_question() {
1666 // Create the test data.
1667 $generator = $this->getDataGenerator();
1668 /** @var \core_question_generator $questiongenerator */
1669 $questiongenerator = $generator->get_plugin_generator('core_question');
1671 $category = $generator->create_category();
1672 $context = \context_coursecat
::instance($category->id
);
1673 $questioncat = $questiongenerator->create_question_category([
1674 'contextid' => $context->id
,
1677 // Create a cloze question.
1678 $question = $questiongenerator->create_question('multianswer', null, [
1679 'category' => $questioncat->id
,
1681 // Now, break the question.
1682 $DB->delete_records('question_multianswer', ['question' => $question->id
]);
1684 $this->setAdminUser();
1686 $result = question_has_capability_on($question->id
, 'tag');
1687 $this->assertTrue($result);
1689 $this->assertDebuggingCalled();
1693 * Tests for the deprecated question_has_capability_on function when passing a stdClass as parameter.
1695 * @dataProvider question_capability_on_question_provider
1696 * @param array $capabilities The capability assignments to set.
1697 * @param string $capability The capability to test
1698 * @param bool $isowner Whether the user to create the question should be the owner or not.
1699 * @param bool $expect The expected result.
1701 public function test_question_has_capability_on_using_stdClass($capabilities, $capability, $isowner, $expect) {
1702 $this->resetAfterTest();
1704 // Create the test data.
1705 $user = $this->getDataGenerator()->create_user();
1706 $otheruser = $this->getDataGenerator()->create_user();
1707 $roleid = $this->getDataGenerator()->create_role();
1708 $category = $this->getDataGenerator()->create_category();
1709 $context = \context_coursecat
::instance($category->id
);
1711 // Assign the user to the role.
1712 role_assign($roleid, $user->id
, $context->id
);
1714 // Assign the capabilities to the role.
1715 foreach ($capabilities as $capname => $capvalue) {
1716 assign_capability($capname, $capvalue, $roleid, $context->id
);
1719 $this->setUser($user);
1721 // The current fake question we make use of is always a stdClass and typically has no ID.
1722 $fakequestion = (object) [
1723 'contextid' => $context->id
,
1727 $fakequestion->createdby
= $user->id
;
1729 $fakequestion->createdby
= $otheruser->id
;
1732 $result = question_has_capability_on($fakequestion, $capability);
1733 $this->assertEquals($expect, $result);
1737 * Tests for the deprecated question_has_capability_on function when using question definition.
1739 * @dataProvider question_capability_on_question_provider
1740 * @param array $capabilities The capability assignments to set.
1741 * @param string $capability The capability to test
1742 * @param bool $isowner Whether the user to create the question should be the owner or not.
1743 * @param bool $expect The expected result.
1745 public function test_question_has_capability_on_using_question_definition($capabilities, $capability, $isowner, $expect) {
1746 $this->resetAfterTest();
1748 // Create the test data.
1749 $generator = $this->getDataGenerator();
1750 /** @var \core_question_generator $questiongenerator */
1751 $questiongenerator = $generator->get_plugin_generator('core_question');
1752 $user = $generator->create_user();
1753 $otheruser = $generator->create_user();
1754 $roleid = $generator->create_role();
1755 $category = $generator->create_category();
1756 $context = \context_coursecat
::instance($category->id
);
1757 $questioncat = $questiongenerator->create_question_category([
1758 'contextid' => $context->id
,
1761 // Assign the user to the role.
1762 role_assign($roleid, $user->id
, $context->id
);
1764 // Assign the capabilities to the role.
1765 foreach ($capabilities as $capname => $capvalue) {
1766 assign_capability($capname, $capvalue, $roleid, $context->id
);
1769 // Create the question.
1770 $qtype = 'truefalse';
1772 'category' => $questioncat->id
,
1773 'createdby' => ($isowner) ?
$user->id
: $otheruser->id
,
1776 $question = $questiongenerator->create_question($qtype, null, $overrides);
1778 $this->setUser($user);
1779 $result = question_has_capability_on($question, $capability);
1780 $this->assertEquals($expect, $result);
1784 * Tests for the deprecated question_has_capability_on function when using a real question id.
1786 * @dataProvider question_capability_on_question_provider
1787 * @param array $capabilities The capability assignments to set.
1788 * @param string $capability The capability to test
1789 * @param bool $isowner Whether the user to create the question should be the owner or not.
1790 * @param bool $expect The expected result.
1792 public function test_question_has_capability_on_using_question_id($capabilities, $capability, $isowner, $expect) {
1793 $this->resetAfterTest();
1795 // Create the test data.
1796 $generator = $this->getDataGenerator();
1797 /** @var \core_question_generator $questiongenerator */
1798 $questiongenerator = $generator->get_plugin_generator('core_question');
1799 $user = $generator->create_user();
1800 $otheruser = $generator->create_user();
1801 $roleid = $generator->create_role();
1802 $category = $generator->create_category();
1803 $context = \context_coursecat
::instance($category->id
);
1804 $questioncat = $questiongenerator->create_question_category([
1805 'contextid' => $context->id
,
1808 // Assign the user to the role.
1809 role_assign($roleid, $user->id
, $context->id
);
1811 // Assign the capabilities to the role.
1812 foreach ($capabilities as $capname => $capvalue) {
1813 assign_capability($capname, $capvalue, $roleid, $context->id
);
1816 // Create the question.
1817 $qtype = 'truefalse';
1819 'category' => $questioncat->id
,
1820 'createdby' => ($isowner) ?
$user->id
: $otheruser->id
,
1823 $question = $questiongenerator->create_question($qtype, null, $overrides);
1825 $this->setUser($user);
1826 $result = question_has_capability_on($question->id
, $capability);
1827 $this->assertEquals($expect, $result);
1831 * Tests for the deprecated question_has_capability_on function when using a string as question id.
1833 * @dataProvider question_capability_on_question_provider
1834 * @param array $capabilities The capability assignments to set.
1835 * @param string $capability The capability to test
1836 * @param bool $isowner Whether the user to create the question should be the owner or not.
1837 * @param bool $expect The expected result.
1839 public function test_question_has_capability_on_using_question_string_id($capabilities, $capability, $isowner, $expect) {
1840 $this->resetAfterTest();
1842 // Create the test data.
1843 $generator = $this->getDataGenerator();
1844 /** @var \core_question_generator $questiongenerator */
1845 $questiongenerator = $generator->get_plugin_generator('core_question');
1846 $user = $generator->create_user();
1847 $otheruser = $generator->create_user();
1848 $roleid = $generator->create_role();
1849 $category = $generator->create_category();
1850 $context = \context_coursecat
::instance($category->id
);
1851 $questioncat = $questiongenerator->create_question_category([
1852 'contextid' => $context->id
,
1855 // Assign the user to the role.
1856 role_assign($roleid, $user->id
, $context->id
);
1858 // Assign the capabilities to the role.
1859 foreach ($capabilities as $capname => $capvalue) {
1860 assign_capability($capname, $capvalue, $roleid, $context->id
);
1863 // Create the question.
1864 $qtype = 'truefalse';
1866 'category' => $questioncat->id
,
1867 'createdby' => ($isowner) ?
$user->id
: $otheruser->id
,
1870 $question = $questiongenerator->create_question($qtype, null, $overrides);
1872 $this->setUser($user);
1873 $result = question_has_capability_on((string) $question->id
, $capability);
1874 $this->assertEquals($expect, $result);
1878 * Tests for the question_has_capability_on function when using a moved question.
1880 * @dataProvider question_capability_on_question_provider
1881 * @param array $capabilities The capability assignments to set.
1882 * @param string $capability The capability to test
1883 * @param bool $isowner Whether the user to create the question should be the owner or not.
1884 * @param bool $expect The expected result.
1886 public function test_question_has_capability_on_using_moved_question($capabilities, $capability, $isowner, $expect) {
1887 $this->resetAfterTest();
1889 // Create the test data.
1890 $generator = $this->getDataGenerator();
1891 /** @var \core_question_generator $questiongenerator */
1892 $questiongenerator = $generator->get_plugin_generator('core_question');
1893 $user = $generator->create_user();
1894 $otheruser = $generator->create_user();
1895 $roleid = $generator->create_role();
1896 $category = $generator->create_category();
1897 $context = \context_coursecat
::instance($category->id
);
1898 $questioncat = $questiongenerator->create_question_category([
1899 'contextid' => $context->id
,
1902 $newcategory = $generator->create_category();
1903 $newcontext = \context_coursecat
::instance($newcategory->id
);
1904 $newquestioncat = $questiongenerator->create_question_category([
1905 'contextid' => $newcontext->id
,
1908 // Assign the user to the role in the _new_ context..
1909 role_assign($roleid, $user->id
, $newcontext->id
);
1911 // Assign the capabilities to the role in the _new_ context.
1912 foreach ($capabilities as $capname => $capvalue) {
1913 assign_capability($capname, $capvalue, $roleid, $newcontext->id
);
1916 // Create the question.
1917 $qtype = 'truefalse';
1919 'category' => $questioncat->id
,
1920 'createdby' => ($isowner) ?
$user->id
: $otheruser->id
,
1923 $question = $questiongenerator->create_question($qtype, null, $overrides);
1925 // Move the question.
1926 question_move_questions_to_category([$question->id
], $newquestioncat->id
);
1928 // Test that the capability is correct after the question has been moved.
1929 $this->setUser($user);
1930 $result = question_has_capability_on($question->id
, $capability);
1931 $this->assertEquals($expect, $result);
1935 * Tests for the question_has_capability_on function when using a real question.
1937 * @dataProvider question_capability_on_question_provider
1938 * @param array $capabilities The capability assignments to set.
1939 * @param string $capability The capability to test
1940 * @param bool $isowner Whether the user to create the question should be the owner or not.
1941 * @param bool $expect The expected result.
1943 public function test_question_has_capability_on_using_question($capabilities, $capability, $isowner, $expect) {
1944 $this->resetAfterTest();
1946 // Create the test data.
1947 $generator = $this->getDataGenerator();
1948 /** @var \core_question_generator $questiongenerator */
1949 $questiongenerator = $generator->get_plugin_generator('core_question');
1950 $user = $generator->create_user();
1951 $otheruser = $generator->create_user();
1952 $roleid = $generator->create_role();
1953 $category = $generator->create_category();
1954 $context = \context_coursecat
::instance($category->id
);
1955 $questioncat = $questiongenerator->create_question_category([
1956 'contextid' => $context->id
,
1959 // Assign the user to the role.
1960 role_assign($roleid, $user->id
, $context->id
);
1962 // Assign the capabilities to the role.
1963 foreach ($capabilities as $capname => $capvalue) {
1964 assign_capability($capname, $capvalue, $roleid, $context->id
);
1967 // Create the question.
1968 $question = $questiongenerator->create_question('truefalse', null, [
1969 'category' => $questioncat->id
,
1970 'createdby' => ($isowner) ?
$user->id
: $otheruser->id
,
1972 $question = question_bank
::load_question_data($question->id
);
1974 $this->setUser($user);
1975 $result = question_has_capability_on($question, $capability);
1976 $this->assertEquals($expect, $result);
1980 * Tests that question_has_capability_on throws an exception for wrong parameter types.
1982 public function test_question_has_capability_on_wrong_param_type() {
1983 // Create the test data.
1984 $generator = $this->getDataGenerator();
1985 /** @var \core_question_generator $questiongenerator */
1986 $questiongenerator = $generator->get_plugin_generator('core_question');
1987 $user = $generator->create_user();
1989 $category = $generator->create_category();
1990 $context = \context_coursecat
::instance($category->id
);
1991 $questioncat = $questiongenerator->create_question_category([
1992 'contextid' => $context->id
,
1995 // Create the question.
1996 $question = $questiongenerator->create_question('truefalse', null, [
1997 'category' => $questioncat->id
,
1998 'createdby' => $user->id
,
2000 $question = question_bank
::load_question_data($question->id
);
2002 $this->setUser($user);
2003 $result = question_has_capability_on((string)$question->id
, 'tag');
2004 $this->assertFalse($result);
2006 $this->expectException('coding_exception');
2007 $this->expectExceptionMessage('$questionorid parameter needs to be an integer or an object.');
2008 question_has_capability_on('one', 'tag');
2012 * Test of question_categorylist function.
2014 * @covers ::question_categorylist()
2016 public function test_question_categorylist(): void
{
2017 $this->resetAfterTest();
2019 // Create a category tree.
2020 /** @var \core_question_generator $questiongenerator */
2021 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
2022 $context = \context_system
::instance();
2024 $top = question_get_top_category($context->id
, true);
2025 $cat1 = $questiongenerator->create_question_category(['parent' => $top->id
]);
2026 $sub11 = $questiongenerator->create_question_category(['parent' => $cat1->id
]);
2027 $sub12 = $questiongenerator->create_question_category(['parent' => $cat1->id
]);
2028 $cat2 = $questiongenerator->create_question_category(['parent' => $top->id
]);
2029 $sub22 = $questiongenerator->create_question_category(['parent' => $cat2->id
]);
2031 // Test - returned array has keys and values the same.
2032 $this->assertEquals([$sub22->id
], array_keys(question_categorylist($sub22->id
)));
2033 $this->assertEquals([$sub22->id
], array_values(question_categorylist($sub22->id
)));
2034 $this->assertEquals([$cat1->id
, $sub11->id
, $sub12->id
], array_keys(question_categorylist($cat1->id
)));
2035 $this->assertEquals([$cat1->id
, $sub11->id
, $sub12->id
], array_values(question_categorylist($cat1->id
)));
2036 $this->assertEquals([$top->id
, $cat1->id
, $cat2->id
, $sub11->id
, $sub12->id
, $sub22->id
],
2037 array_keys(question_categorylist($top->id
)));
2038 $this->assertEquals([$top->id
, $cat1->id
, $cat2->id
, $sub11->id
, $sub12->id
, $sub22->id
],
2039 array_values(question_categorylist($top->id
)));
2043 * Test of question_categorylist function when there is bad data, with a category pointing to a parent in another context.
2045 * This is a situation that should never arise (parents and their children should always belong to the same context)
2046 * but it does, because bugs, so the code should be robust to it.
2048 * @covers ::question_categorylist()
2050 public function test_question_categorylist_bad_data(): void
{
2051 $this->resetAfterTest();
2053 // Create a category tree.
2054 $course = $this->getDataGenerator()->create_course();
2055 $coursecontext = \context_course
::instance($course->id
);
2056 /** @var \core_question_generator $questiongenerator */
2057 $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
2058 $context = \context_system
::instance();
2060 $top = question_get_top_category($context->id
, true);
2061 $cat1 = $questiongenerator->create_question_category(['parent' => $top->id
]);
2062 $sub11 = $questiongenerator->create_question_category(['parent' => $cat1->id
]);
2063 $sub12 = $questiongenerator->create_question_category(['parent' => $cat1->id
]);
2064 $cat2 = $questiongenerator->create_question_category(['parent' => $top->id
, 'contextid' => $coursecontext->id
]);
2065 $sub22 = $questiongenerator->create_question_category(['parent' => $cat2->id
]);
2067 // Test - returned array has keys and values the same.
2068 $this->assertEquals([$cat2->id
, $sub22->id
], array_keys(question_categorylist($cat2->id
)));
2069 $this->assertEquals([$top->id
, $cat1->id
, $sub11->id
, $sub12->id
],
2070 array_keys(question_categorylist($top->id
)));
2074 * Test of question_categorylist_parents function.
2076 * @covers ::question_categorylist_parents()
2078 public function test_question_categorylist_parents(): void
{
2079 $this->resetAfterTest();
2080 $generator = $this->getDataGenerator();
2081 /** @var \core_question_generator $questiongenerator */
2082 $questiongenerator = $generator->get_plugin_generator('core_question');
2083 $category = $generator->create_category();
2084 $context = \context_coursecat
::instance($category->id
);
2085 // Create a top category.
2086 $cat0 = question_get_top_category($context->id
, true);
2087 // Add sub-categories.
2088 $cat1 = $questiongenerator->create_question_category(['parent' => $cat0->id
]);
2089 $cat2 = $questiongenerator->create_question_category(['parent' => $cat1->id
]);
2091 // Test the 'get parents' function.
2092 $this->assertEquals([$cat0->id
, $cat1->id
], question_categorylist_parents($cat2->id
));
2096 * Test question_categorylist_parents when there is bad data, with a category pointing to a parent in another context.
2098 * This is a situation that should never arise (parents and their children should always belong to the same context)
2099 * but it does, because bugs, so the code should be robust to it.
2101 * @covers ::question_categorylist_parents()
2103 public function test_question_categorylist_parents_bad_data(): void
{
2104 $this->resetAfterTest();
2105 $generator = $this->getDataGenerator();
2106 /** @var \core_question_generator $questiongenerator */
2107 $questiongenerator = $generator->get_plugin_generator('core_question');
2108 $category = $generator->create_category();
2109 $context = \context_coursecat
::instance($category->id
);
2110 // Create a top category.
2111 $cat0 = question_get_top_category($context->id
, true);
2112 // Add sub-categories - but in a different context.
2113 $cat1 = $questiongenerator->create_question_category(
2114 ['parent' => $cat0->id
, 'contextid' => \context_system
::instance()->id
]);
2115 $cat2 = $questiongenerator->create_question_category(
2116 ['parent' => $cat1->id
, 'contextid' => \context_system
::instance()->id
]);
2118 // Test the 'get parents' function only returns categories in the same context.
2119 $this->assertEquals([$cat1->id
], question_categorylist_parents($cat2->id
));
2123 * Get test cases for test_core_question_find_next_unused_idnumber.
2125 * @return array test cases.
2127 public function find_next_unused_idnumber_cases(): array {
2135 ['id999', 'id1000'],
2140 ['1.0E+29', '1.0E+30'], // Idnumbers are strings, not floats.
2141 ['1.0E-29', '1.0E-30'], // By the way, this is not a sensible idnumber!
2149 * Test core_question_find_next_unused_idnumber in the case when there are no other questions.
2151 * @dataProvider find_next_unused_idnumber_cases
2152 * @param string|null $oldidnumber value to pass to core_question_find_next_unused_idnumber.
2153 * @param string|null $expectednewidnumber expected result.
2155 public function test_core_question_find_next_unused_idnumber(?
string $oldidnumber, ?
string $expectednewidnumber) {
2156 $this->assertSame($expectednewidnumber, core_question_find_next_unused_idnumber($oldidnumber, 0));
2159 public function test_core_question_find_next_unused_idnumber_skips_used() {
2160 $this->resetAfterTest();
2162 /** @var core_question_generator $generator */
2163 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
2164 $category = $generator->create_question_category();
2165 $othercategory = $generator->create_question_category();
2166 $generator->create_question('truefalse', null, ['category' => $category->id
, 'idnumber' => 'id9']);
2167 $generator->create_question('truefalse', null, ['category' => $category->id
, 'idnumber' => 'id10']);
2168 // Next one to make sure only idnumbers from the right category are ruled out.
2169 $generator->create_question('truefalse', null, ['category' => $othercategory->id
, 'idnumber' => 'id11']);
2171 $this->assertSame('id11', core_question_find_next_unused_idnumber('id9', $category->id
));
2172 $this->assertSame('id11', core_question_find_next_unused_idnumber('id8', $category->id
));
2176 * Tests for the question_move_questions_to_category function.
2178 * @covers ::question_move_questions_to_category
2180 public function test_question_move_questions_to_category() {
2181 $this->resetAfterTest();
2183 // Create the test data.
2184 list($category1, $course1, $quiz1, $questioncat1, $questions1) = $this->setup_quiz_and_questions();
2185 list($category2, $course2, $quiz2, $questioncat2, $questions2) = $this->setup_quiz_and_questions();
2187 $this->assertCount(2, $questions1);
2188 $this->assertCount(2, $questions2);
2189 $questionsidtomove = [];
2190 foreach ($questions1 as $question1) {
2191 $questionsidtomove[] = $question1->id
;
2194 // Move the question from quiz 1 to quiz 2.
2195 question_move_questions_to_category($questionsidtomove, $questioncat2->id
);
2196 $this->assert_category_contains_questions($questioncat2->id
, 4);
2200 * Tests for the idnumber_exist_in_question_category function.
2202 * @covers ::idnumber_exist_in_question_category
2204 public function test_idnumber_exist_in_question_category() {
2207 $this->resetAfterTest();
2209 // Create the test data.
2210 list($category1, $course1, $quiz1, $questioncat1, $questions1) = $this->setup_quiz_and_questions();
2211 list($category2, $course2, $quiz2, $questioncat2, $questions2) = $this->setup_quiz_and_questions();
2213 $questionbankentry1 = get_question_bank_entry($questions1[0]->id
);
2214 $entry = new \
stdClass();
2215 $entry->id
= $questionbankentry1->id
;
2216 $entry->idnumber
= 1;
2217 $DB->update_record('question_bank_entries', $entry);
2219 $questionbankentry2 = get_question_bank_entry($questions2[0]->id
);
2220 $entry2 = new \
stdClass();
2221 $entry2->id
= $questionbankentry2->id
;
2222 $entry2->idnumber
= 1;
2223 $DB->update_record('question_bank_entries', $entry2);
2225 $questionbe = $DB->get_record('question_bank_entries', ['id' => $questionbankentry1->id
]);
2227 // Validate that a first stage of an idnumber exists (this format: xxxx_x).
2228 list($response, $record) = idnumber_exist_in_question_category($questionbe->idnumber
, $questioncat1->id
);
2229 $this->assertEquals([], $record);
2230 $this->assertEquals(true, $response);
2232 // Move the question to a category that has a question with the same idnumber.
2233 question_move_questions_to_category($questions1[0]->id
, $questioncat2->id
);
2235 // Validate that function return the last record used for the idnumber.
2236 list($response, $record) = idnumber_exist_in_question_category($questionbe->idnumber
, $questioncat2->id
);
2237 $record = reset($record);
2238 $idnumber = $record->idnumber
;
2239 $this->assertEquals($idnumber, '1_1');
2240 $this->assertEquals(true, $response);
2244 * Test method is_latest().
2246 * @covers ::is_latest
2249 public function test_is_latest() {
2251 $this->resetAfterTest();
2252 /** @var \core_question_generator $generator */
2253 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
2254 $qcat1 = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat']);
2255 $question = $generator->create_question('shortanswer', null, ['name' => 'q1', 'category' => $qcat1->id
]);
2256 $record = $DB->get_record('question_versions', ['questionid' => $question->id
]);
2257 $firstversion = $record->version
;
2258 $questionbankentryid = $record->questionbankentryid
;
2259 $islatest = is_latest($firstversion, $questionbankentryid);
2260 $this->assertTrue($islatest);
2264 * Test question bank entry deletion.
2266 * @covers ::delete_question_bank_entry
2268 public function test_delete_question_bank_entry() {
2270 $this->resetAfterTest();
2272 $context = \context_system
::instance();
2273 /** @var \core_question_generator $qgen */
2274 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2275 $qcat = $qgen->create_question_category(array('contextid' => $context->id
));
2276 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
2277 // Make sure there is an entry in the entry table.
2278 $sql = 'SELECT qbe.id as id,
2280 FROM {question_bank_entries} qbe
2281 JOIN {question_versions} qv
2282 ON qbe.id = qv.questionbankentryid
2284 ON qv.questionid = q.id
2286 $records = $DB->get_records_sql($sql, [$q1->id
]);
2287 $this->assertCount(1, $records);
2288 // Delete the record.
2289 $record = reset($records);
2290 delete_question_bank_entry($record->id
);
2291 $records = $DB->get_records('question_bank_entries', ['id' => $record->id
]);
2292 // As the version record exists, it wont delete the data to resolve any errors.
2293 $this->assertCount(1, $records);
2294 $DB->delete_records('question_versions', ['id' => $record->versionid
]);
2295 delete_question_bank_entry($record->id
);
2296 $records = $DB->get_records('question_bank_entries', ['id' => $record->id
]);
2297 $this->assertCount(0, $records);
2301 * Test question bank entry object.
2303 * @covers ::get_question_bank_entry
2305 public function test_get_question_bank_entry() {
2307 $this->resetAfterTest();
2309 $context = \context_system
::instance();
2310 /** @var \core_question_generator $qgen */
2311 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2312 $qcat = $qgen->create_question_category(array('contextid' => $context->id
));
2313 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
2314 // Make sure there is an entry in the entry table.
2315 $sql = 'SELECT qbe.id as id,
2317 FROM {question_bank_entries} qbe
2318 JOIN {question_versions} qv
2319 ON qbe.id = qv.questionbankentryid
2321 ON qv.questionid = q.id
2323 $records = $DB->get_records_sql($sql, [$q1->id
]);
2324 $this->assertCount(1, $records);
2325 $record = reset($records);
2326 $questionbankentry = get_question_bank_entry($q1->id
);
2327 $this->assertEquals($questionbankentry->id
, $record->id
);
2331 * Test the version objects for a question.
2333 * @covers ::get_question_version
2335 public function test_get_question_version() {
2337 $this->resetAfterTest();
2339 $context = \context_system
::instance();
2340 /** @var \core_question_generator $qgen */
2341 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2342 $qcat = $qgen->create_question_category(array('contextid' => $context->id
));
2343 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
2344 // Make sure there is an entry in the entry table.
2345 $sql = 'SELECT qbe.id as id,
2347 FROM {question_bank_entries} qbe
2348 JOIN {question_versions} qv
2349 ON qbe.id = qv.questionbankentryid
2351 ON qv.questionid = q.id
2353 $records = $DB->get_records_sql($sql, [$q1->id
]);
2354 $this->assertCount(1, $records);
2355 $record = reset($records);
2356 $questionversions = get_question_version($q1->id
);
2357 $questionversion = reset($questionversions);
2358 $this->assertEquals($questionversion->id
, $record->versionid
);
2362 * Test get next version of a question.
2364 * @covers ::get_next_version
2366 public function test_get_next_version() {
2368 $this->resetAfterTest();
2370 $context = \context_system
::instance();
2371 /** @var \core_question_generator $qgen */
2372 $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
2373 $qcat = $qgen->create_question_category(array('contextid' => $context->id
));
2374 $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id
));
2375 // Make sure there is an entry in the entry table.
2376 $sql = 'SELECT qbe.id as id,
2379 FROM {question_bank_entries} qbe
2380 JOIN {question_versions} qv
2381 ON qbe.id = qv.questionbankentryid
2383 ON qv.questionid = q.id
2385 $records = $DB->get_records_sql($sql, [$q1->id
]);
2386 $this->assertCount(1, $records);
2387 $record = reset($records);
2388 $this->assertEquals(1, $record->version
);
2389 $nextversion = get_next_version($record->id
);
2390 $this->assertEquals(2, $nextversion);