PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tests/questionlib_test.php

https://bitbucket.org/moodle/moodle
PHP | 2060 lines | 1240 code | 339 blank | 481 comment | 21 complexity | cfd828a42cb8e58cc0ab0b99e1932e6c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Unit tests for (some of) ../questionlib.php.
  18. *
  19. * @package core_question
  20. * @category phpunit
  21. * @copyright 2006 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. use core_tag\output\tag;
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once($CFG->libdir . '/questionlib.php');
  28. require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  29. // Get the necessary files to perform backup and restore.
  30. require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  31. require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  32. /**
  33. * Unit tests for (some of) ../questionlib.php.
  34. *
  35. * @copyright 2006 The Open University
  36. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37. */
  38. class core_questionlib_testcase extends advanced_testcase {
  39. /**
  40. * Test set up.
  41. *
  42. * This is executed before running any test in this file.
  43. */
  44. public function setUp(): void {
  45. $this->resetAfterTest();
  46. }
  47. /**
  48. * Setup a course, a quiz, a question category and a question for testing.
  49. *
  50. * @param string $type The type of question category to create.
  51. * @return array The created data objects
  52. */
  53. public function setup_quiz_and_questions($type = 'module') {
  54. // Create course category.
  55. $category = $this->getDataGenerator()->create_category();
  56. // Create course.
  57. $course = $this->getDataGenerator()->create_course(array(
  58. 'numsections' => 5,
  59. 'category' => $category->id
  60. ));
  61. $options = array(
  62. 'course' => $course->id,
  63. 'duedate' => time(),
  64. );
  65. // Generate an assignment with due date (will generate a course event).
  66. $quiz = $this->getDataGenerator()->create_module('quiz', $options);
  67. $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
  68. switch ($type) {
  69. case 'course':
  70. $context = context_course::instance($course->id);
  71. break;
  72. case 'category':
  73. $context = context_coursecat::instance($category->id);
  74. break;
  75. case 'system':
  76. $context = context_system::instance();
  77. break;
  78. default:
  79. $context = context_module::instance($quiz->cmid);
  80. break;
  81. }
  82. $qcat = $qgen->create_question_category(array('contextid' => $context->id));
  83. $questions = array(
  84. $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
  85. $qgen->create_question('shortanswer', null, array('category' => $qcat->id)),
  86. );
  87. quiz_add_quiz_question($questions[0]->id, $quiz);
  88. return array($category, $course, $quiz, $qcat, $questions);
  89. }
  90. public function test_question_reorder_qtypes() {
  91. $this->assertEquals(
  92. array(0 => 't2', 1 => 't1', 2 => 't3'),
  93. question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1));
  94. $this->assertEquals(
  95. array(0 => 't1', 1 => 't2', 2 => 't3'),
  96. question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', -1));
  97. $this->assertEquals(
  98. array(0 => 't2', 1 => 't1', 2 => 't3'),
  99. question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't2', -1));
  100. $this->assertEquals(
  101. array(0 => 't1', 1 => 't2', 2 => 't3'),
  102. question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't3', +1));
  103. $this->assertEquals(
  104. array(0 => 't1', 1 => 't2', 2 => 't3'),
  105. question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 'missing', +1));
  106. }
  107. public function test_match_grade_options() {
  108. $gradeoptions = question_bank::fraction_options_full();
  109. $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'error'));
  110. $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'error'));
  111. $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'error'));
  112. $this->assertFalse(match_grade_options($gradeoptions, 0.3333, 'error'));
  113. $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'nearest'));
  114. $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'nearest'));
  115. $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'nearest'));
  116. $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33, 'nearest'));
  117. $this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
  118. }
  119. /**
  120. * This function tests that the functions responsible for moving questions to
  121. * different contexts also updates the tag instances associated with the questions.
  122. */
  123. public function test_altering_tag_instance_context() {
  124. global $CFG, $DB;
  125. // Set to admin user.
  126. $this->setAdminUser();
  127. // Create two course categories - we are going to delete one of these later and will expect
  128. // all the questions belonging to the course in the deleted category to be moved.
  129. $coursecat1 = $this->getDataGenerator()->create_category();
  130. $coursecat2 = $this->getDataGenerator()->create_category();
  131. // Create a couple of categories and questions.
  132. $context1 = context_coursecat::instance($coursecat1->id);
  133. $context2 = context_coursecat::instance($coursecat2->id);
  134. $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  135. $questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
  136. $context1->id));
  137. $questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
  138. $context2->id));
  139. $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
  140. $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
  141. $question3 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
  142. $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
  143. // Now lets tag these questions.
  144. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $context1, array('tag 1', 'tag 2'));
  145. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $context1, array('tag 3', 'tag 4'));
  146. core_tag_tag::set_item_tags('core_question', 'question', $question3->id, $context2, array('tag 5', 'tag 6'));
  147. core_tag_tag::set_item_tags('core_question', 'question', $question4->id, $context2, array('tag 7', 'tag 8'));
  148. // Test moving the questions to another category.
  149. question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id);
  150. // Test that all tag_instances belong to one context.
  151. $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
  152. 'contextid' => $questioncat2->contextid)));
  153. // Test moving them back.
  154. question_move_questions_to_category(array($question1->id, $question2->id), $questioncat1->id);
  155. // Test that all tag_instances are now reset to how they were initially.
  156. $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
  157. 'contextid' => $questioncat1->contextid)));
  158. $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
  159. 'contextid' => $questioncat2->contextid)));
  160. // Now test moving a whole question category to another context.
  161. question_move_category_to_context($questioncat1->id, $questioncat1->contextid, $questioncat2->contextid);
  162. // Test that all tag_instances belong to one context.
  163. $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
  164. 'contextid' => $questioncat2->contextid)));
  165. // Now test moving them back.
  166. question_move_category_to_context($questioncat1->id, $questioncat2->contextid,
  167. context_coursecat::instance($coursecat1->id)->id);
  168. // Test that all tag_instances are now reset to how they were initially.
  169. $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
  170. 'contextid' => $questioncat1->contextid)));
  171. $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
  172. 'contextid' => $questioncat2->contextid)));
  173. // Now we want to test deleting the course category and moving the questions to another category.
  174. question_delete_course_category($coursecat1, $coursecat2);
  175. // Test that all tag_instances belong to one context.
  176. $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
  177. 'contextid' => $questioncat2->contextid)));
  178. // Create a course.
  179. $course = $this->getDataGenerator()->create_course();
  180. // Create some question categories and questions in this course.
  181. $coursecontext = context_course::instance($course->id);
  182. $questioncat = $questiongenerator->create_question_category(array('contextid' =>
  183. $coursecontext->id));
  184. $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
  185. $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
  186. // Add some tags to these questions.
  187. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, array('tag 1', 'tag 2'));
  188. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, array('tag 1', 'tag 2'));
  189. // Create a course that we are going to restore the other course to.
  190. $course2 = $this->getDataGenerator()->create_course();
  191. // Create backup file and save it to the backup location.
  192. $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
  193. backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
  194. $bc->execute_plan();
  195. $results = $bc->get_results();
  196. $file = $results['backup_destination'];
  197. $fp = get_file_packer('application/vnd.moodle.backup');
  198. $filepath = $CFG->dataroot . '/temp/backup/test-restore-course';
  199. $file->extract_to_pathname($fp, $filepath);
  200. $bc->destroy();
  201. // Now restore the course.
  202. $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO,
  203. backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE);
  204. $rc->execute_precheck();
  205. $rc->execute_plan();
  206. // Get the created question category.
  207. $restoredcategory = $DB->get_record_select('question_categories', 'contextid = ? AND parent <> 0',
  208. array(context_course::instance($course2->id)->id), '*', MUST_EXIST);
  209. // Check that there are two questions in the restored to course's context.
  210. $this->assertEquals(2, $DB->count_records('question', array('category' => $restoredcategory->id)));
  211. $rc->destroy();
  212. }
  213. /**
  214. * Test that deleting a question from the question bank works in the normal case.
  215. */
  216. public function test_question_delete_question() {
  217. global $DB;
  218. // Setup.
  219. $context = context_system::instance();
  220. $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
  221. $qcat = $qgen->create_question_category(array('contextid' => $context->id));
  222. $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
  223. $q2 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
  224. // Do.
  225. question_delete_question($q1->id);
  226. // Verify.
  227. $this->assertFalse($DB->record_exists('question', ['id' => $q1->id]));
  228. // Check that we did not delete too much.
  229. $this->assertTrue($DB->record_exists('question', ['id' => $q2->id]));
  230. }
  231. /**
  232. * Test that deleting a broken question from the question bank does not cause fatal errors.
  233. */
  234. public function test_question_delete_question_broken_data() {
  235. global $DB;
  236. // Setup.
  237. $context = context_system::instance();
  238. $qgen = $this->getDataGenerator()->get_plugin_generator('core_question');
  239. $qcat = $qgen->create_question_category(array('contextid' => $context->id));
  240. $q1 = $qgen->create_question('shortanswer', null, array('category' => $qcat->id));
  241. // Now delete the category, to simulate what happens in old sites where
  242. // referential integrity has failed.
  243. $DB->delete_records('question_categories', ['id' => $qcat->id]);
  244. // Do.
  245. question_delete_question($q1->id);
  246. // Verify.
  247. $this->assertDebuggingCalled('Deleting question ' . $q1->id .
  248. ' which is no longer linked to a context. Assuming system context ' .
  249. 'to avoid errors, but this may mean that some data like ' .
  250. 'files, tags, are not cleaned up.');
  251. $this->assertFalse($DB->record_exists('question', ['id' => $q1->id]));
  252. }
  253. /**
  254. * This function tests the question_category_delete_safe function.
  255. */
  256. public function test_question_category_delete_safe() {
  257. global $DB;
  258. $this->resetAfterTest(true);
  259. $this->setAdminUser();
  260. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
  261. question_category_delete_safe($qcat);
  262. // Verify category deleted.
  263. $criteria = array('id' => $qcat->id);
  264. $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
  265. // Verify questions deleted or moved.
  266. $criteria = array('category' => $qcat->id);
  267. $this->assertEquals(0, $DB->count_records('question', $criteria));
  268. // Verify question not deleted.
  269. $criteria = array('id' => $questions[0]->id);
  270. $this->assertEquals(1, $DB->count_records('question', $criteria));
  271. }
  272. /**
  273. * This function tests the question_delete_activity function.
  274. */
  275. public function test_question_delete_activity() {
  276. global $DB;
  277. $this->resetAfterTest(true);
  278. $this->setAdminUser();
  279. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
  280. $cm = get_coursemodule_from_instance('quiz', $quiz->id);
  281. // Test the deletion.
  282. question_delete_activity($cm);
  283. // Verify category deleted.
  284. $criteria = array('id' => $qcat->id);
  285. $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
  286. // Verify questions deleted or moved.
  287. $criteria = array('category' => $qcat->id);
  288. $this->assertEquals(0, $DB->count_records('question', $criteria));
  289. }
  290. /**
  291. * This function tests the question_delete_context function.
  292. */
  293. public function test_question_delete_context() {
  294. global $DB;
  295. $this->resetAfterTest(true);
  296. $this->setAdminUser();
  297. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
  298. // Get the module context id.
  299. $result = question_delete_context($qcat->contextid);
  300. // Verify category deleted.
  301. $criteria = array('id' => $qcat->id);
  302. $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
  303. // Verify questions deleted or moved.
  304. $criteria = array('category' => $qcat->id);
  305. $this->assertEquals(0, $DB->count_records('question', $criteria));
  306. }
  307. /**
  308. * This function tests the question_delete_course function.
  309. */
  310. public function test_question_delete_course() {
  311. global $DB;
  312. $this->resetAfterTest(true);
  313. $this->setAdminUser();
  314. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
  315. // Test the deletion.
  316. question_delete_course($course);
  317. // Verify category deleted.
  318. $criteria = array('id' => $qcat->id);
  319. $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
  320. // Verify questions deleted or moved.
  321. $criteria = array('category' => $qcat->id);
  322. $this->assertEquals(0, $DB->count_records('question', $criteria));
  323. }
  324. /**
  325. * This function tests the question_delete_course_category function.
  326. */
  327. public function test_question_delete_course_category() {
  328. global $DB;
  329. $this->resetAfterTest(true);
  330. $this->setAdminUser();
  331. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  332. // Test that the feedback works.
  333. question_delete_course_category($category, null);
  334. // Verify category deleted.
  335. $criteria = array('id' => $qcat->id);
  336. $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
  337. // Verify questions deleted or moved.
  338. $criteria = array('category' => $qcat->id);
  339. $this->assertEquals(0, $DB->count_records('question', $criteria));
  340. }
  341. /**
  342. * This function tests the question_delete_course_category function when it is supposed to move question categories.
  343. */
  344. public function test_question_delete_course_category_move_qcats() {
  345. global $DB;
  346. $this->resetAfterTest(true);
  347. $this->setAdminUser();
  348. list($category1, $course1, $quiz1, $qcat1, $questions1) = $this->setup_quiz_and_questions('category');
  349. list($category2, $course2, $quiz2, $qcat2, $questions2) = $this->setup_quiz_and_questions('category');
  350. $questionsinqcat1 = count($questions1);
  351. $questionsinqcat2 = count($questions2);
  352. // Test the delete.
  353. question_delete_course_category($category1, $category2);
  354. // Verify category not deleted.
  355. $criteria = array('id' => $qcat1->id);
  356. $this->assertEquals(1, $DB->count_records('question_categories', $criteria));
  357. // Verify questions are moved.
  358. $params = array($qcat2->contextid);
  359. $actualquestionscount = $DB->count_records_sql("SELECT COUNT(*)
  360. FROM {question} q
  361. JOIN {question_categories} qc ON q.category = qc.id
  362. WHERE qc.contextid = ?", $params);
  363. $this->assertEquals($questionsinqcat1 + $questionsinqcat2, $actualquestionscount);
  364. // Verify there is just a single top-level category.
  365. $criteria = array('contextid' => $qcat2->contextid, 'parent' => 0);
  366. $this->assertEquals(1, $DB->count_records('question_categories', $criteria));
  367. // Verify there is no question category in previous context.
  368. $criteria = array('contextid' => $qcat1->contextid);
  369. $this->assertEquals(0, $DB->count_records('question_categories', $criteria));
  370. }
  371. /**
  372. * This function tests the question_save_from_deletion function when it is supposed to make a new category and
  373. * move question categories to that new category.
  374. */
  375. public function test_question_save_from_deletion() {
  376. global $DB;
  377. $this->resetAfterTest(true);
  378. $this->setAdminUser();
  379. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
  380. $context = context::instance_by_id($qcat->contextid);
  381. $newcat = question_save_from_deletion(array_column($questions, 'id'),
  382. $context->get_parent_context()->id, $context->get_context_name());
  383. // Verify that the newcat itself is not a tep level category.
  384. $this->assertNotEquals(0, $newcat->parent);
  385. // Verify there is just a single top-level category.
  386. $this->assertEquals(1, $DB->count_records('question_categories', ['contextid' => $qcat->contextid, 'parent' => 0]));
  387. }
  388. /**
  389. * This function tests the question_save_from_deletion function when it is supposed to make a new category and
  390. * move question categories to that new category when quiz name is very long but less than 256 characters.
  391. */
  392. public function test_question_save_from_deletion_quiz_with_long_name() {
  393. global $DB;
  394. $this->resetAfterTest(true);
  395. $this->setAdminUser();
  396. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
  397. // Moodle doesn't allow you to enter a name longer than 255 characters.
  398. $quiz->name = shorten_text(str_repeat('123456789 ', 26), 255);
  399. $DB->update_record('quiz', $quiz);
  400. $context = context::instance_by_id($qcat->contextid);
  401. $newcat = question_save_from_deletion(array_column($questions, 'id'),
  402. $context->get_parent_context()->id, $context->get_context_name());
  403. // Verifying that the inserted record's name is expected or not.
  404. $this->assertEquals($DB->get_record('question_categories', ['id' => $newcat->id])->name, $newcat->name);
  405. // Verify that the newcat itself is not a top level category.
  406. $this->assertNotEquals(0, $newcat->parent);
  407. // Verify there is just a single top-level category.
  408. $this->assertEquals(1, $DB->count_records('question_categories', ['contextid' => $qcat->contextid, 'parent' => 0]));
  409. }
  410. /**
  411. * get_question_options should add the category object to the given question.
  412. */
  413. public function test_get_question_options_includes_category_object_single_question() {
  414. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  415. $question = array_shift($questions);
  416. get_question_options($question);
  417. $this->assertEquals($qcat, $question->categoryobject);
  418. }
  419. /**
  420. * get_question_options should add the category object to all of the questions in
  421. * the given list.
  422. */
  423. public function test_get_question_options_includes_category_object_multiple_questions() {
  424. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  425. get_question_options($questions);
  426. foreach ($questions as $question) {
  427. $this->assertEquals($qcat, $question->categoryobject);
  428. }
  429. }
  430. /**
  431. * get_question_options includes the tags for all questions in the list.
  432. */
  433. public function test_get_question_options_includes_question_tags() {
  434. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  435. $question1 = $questions[0];
  436. $question2 = $questions[1];
  437. $qcontext = context::instance_by_id($qcat->contextid);
  438. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
  439. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
  440. get_question_options($questions, true);
  441. foreach ($questions as $question) {
  442. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  443. $expectedtags = [];
  444. $actualtags = $question->tags;
  445. foreach ($tags as $tag) {
  446. $expectedtags[$tag->id] = $tag->get_display_name();
  447. }
  448. // The question should have a tags property populated with each tag id
  449. // and display name as a key vale pair.
  450. $this->assertEquals($expectedtags, $actualtags);
  451. $actualtagobjects = $question->tagobjects;
  452. sort($tags);
  453. sort($actualtagobjects);
  454. // The question should have a full set of each tag object.
  455. $this->assertEquals($tags, $actualtagobjects);
  456. // The question should not have any course tags.
  457. $this->assertEmpty($question->coursetagobjects);
  458. }
  459. }
  460. /**
  461. * get_question_options includes the course tags for all questions in the list.
  462. */
  463. public function test_get_question_options_includes_course_tags() {
  464. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  465. $question1 = $questions[0];
  466. $question2 = $questions[1];
  467. $coursecontext = context_course::instance($course->id);
  468. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
  469. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
  470. get_question_options($questions, true);
  471. foreach ($questions as $question) {
  472. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  473. $expectedcoursetags = [];
  474. $actualcoursetags = $question->coursetags;
  475. foreach ($tags as $tag) {
  476. $expectedcoursetags[$tag->id] = $tag->get_display_name();
  477. }
  478. // The question should have a coursetags property populated with each tag id
  479. // and display name as a key vale pair.
  480. $this->assertEquals($expectedcoursetags, $actualcoursetags);
  481. $actualcoursetagobjects = $question->coursetagobjects;
  482. sort($tags);
  483. sort($actualcoursetagobjects);
  484. // The question should have a full set of the course tag objects.
  485. $this->assertEquals($tags, $actualcoursetagobjects);
  486. // The question should not have any other tags.
  487. $this->assertEmpty($question->tagobjects);
  488. $this->assertEmpty($question->tags);
  489. }
  490. }
  491. /**
  492. * get_question_options only categorises a tag as a course tag if it is in a
  493. * course context that is different from the question context.
  494. */
  495. public function test_get_question_options_course_tags_in_course_question_context() {
  496. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
  497. $question1 = $questions[0];
  498. $question2 = $questions[1];
  499. $coursecontext = context_course::instance($course->id);
  500. // Create course level tags in the course context that matches the question
  501. // course context.
  502. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
  503. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
  504. get_question_options($questions, true);
  505. foreach ($questions as $question) {
  506. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  507. $actualtagobjects = $question->tagobjects;
  508. sort($tags);
  509. sort($actualtagobjects);
  510. // The tags should not be considered course tags because they are in
  511. // the same context as the question. That makes them question tags.
  512. $this->assertEmpty($question->coursetagobjects);
  513. // The course context tags should be returned in the regular tag object
  514. // list.
  515. $this->assertEquals($tags, $actualtagobjects);
  516. }
  517. }
  518. /**
  519. * get_question_options includes the tags and course tags for all questions in the list
  520. * if each question has course and question level tags.
  521. */
  522. public function test_get_question_options_includes_question_and_course_tags() {
  523. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  524. $question1 = $questions[0];
  525. $question2 = $questions[1];
  526. $qcontext = context::instance_by_id($qcat->contextid);
  527. $coursecontext = context_course::instance($course->id);
  528. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
  529. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['cfoo', 'cbar']);
  530. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
  531. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['cbaz', 'cbop']);
  532. get_question_options($questions, true);
  533. foreach ($questions as $question) {
  534. $alltags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  535. $tags = array_filter($alltags, function($tag) use ($qcontext) {
  536. return $tag->taginstancecontextid == $qcontext->id;
  537. });
  538. $coursetags = array_filter($alltags, function($tag) use ($coursecontext) {
  539. return $tag->taginstancecontextid == $coursecontext->id;
  540. });
  541. $expectedtags = [];
  542. $actualtags = $question->tags;
  543. foreach ($tags as $tag) {
  544. $expectedtags[$tag->id] = $tag->get_display_name();
  545. }
  546. // The question should have a tags property populated with each tag id
  547. // and display name as a key vale pair.
  548. $this->assertEquals($expectedtags, $actualtags);
  549. $actualtagobjects = $question->tagobjects;
  550. sort($tags);
  551. sort($actualtagobjects);
  552. // The question should have a full set of each tag object.
  553. $this->assertEquals($tags, $actualtagobjects);
  554. $actualcoursetagobjects = $question->coursetagobjects;
  555. sort($coursetags);
  556. sort($actualcoursetagobjects);
  557. // The question should have a full set of course tag objects.
  558. $this->assertEquals($coursetags, $actualcoursetagobjects);
  559. }
  560. }
  561. /**
  562. * get_question_options should update the context id to the question category
  563. * context id for any non-course context tag that isn't in the question category
  564. * context.
  565. */
  566. public function test_get_question_options_normalises_question_tags() {
  567. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  568. $question1 = $questions[0];
  569. $question2 = $questions[1];
  570. $qcontext = context::instance_by_id($qcat->contextid);
  571. $systemcontext = context_system::instance();
  572. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
  573. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
  574. $q1tags = core_tag_tag::get_item_tags('core_question', 'question', $question1->id);
  575. $q2tags = core_tag_tag::get_item_tags('core_question', 'question', $question2->id);
  576. $q1tag = array_shift($q1tags);
  577. $q2tag = array_shift($q2tags);
  578. // Change two of the tag instances to be a different (non-course) context to the
  579. // question tag context. These tags should then be normalised back to the question
  580. // tag context.
  581. core_tag_tag::change_instances_context([$q1tag->taginstanceid, $q2tag->taginstanceid], $systemcontext);
  582. get_question_options($questions, true);
  583. foreach ($questions as $question) {
  584. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  585. // The database should have been updated with the correct context id.
  586. foreach ($tags as $tag) {
  587. $this->assertEquals($qcontext->id, $tag->taginstancecontextid);
  588. }
  589. // The tag objects on the question should have been updated with the
  590. // correct context id.
  591. foreach ($question->tagobjects as $tag) {
  592. $this->assertEquals($qcontext->id, $tag->taginstancecontextid);
  593. }
  594. }
  595. }
  596. /**
  597. * get_question_options if the question is a course level question then tags
  598. * in that context should not be consdered course tags, they are question tags.
  599. */
  600. public function test_get_question_options_includes_course_context_question_tags() {
  601. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
  602. $question1 = $questions[0];
  603. $question2 = $questions[1];
  604. $coursecontext = context_course::instance($course->id);
  605. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
  606. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
  607. get_question_options($questions, true);
  608. foreach ($questions as $question) {
  609. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  610. // Tags in a course context that matches the question context should
  611. // not be considered course tags.
  612. $this->assertEmpty($question->coursetagobjects);
  613. $this->assertEmpty($question->coursetags);
  614. $actualtagobjects = $question->tagobjects;
  615. sort($tags);
  616. sort($actualtagobjects);
  617. // The tags should be considered question tags not course tags.
  618. $this->assertEquals($tags, $actualtagobjects);
  619. }
  620. }
  621. /**
  622. * get_question_options should return tags from all course contexts by default.
  623. */
  624. public function test_get_question_options_includes_multiple_courses_tags() {
  625. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  626. $question1 = $questions[0];
  627. $question2 = $questions[1];
  628. $coursecontext = context_course::instance($course->id);
  629. // Create a sibling course.
  630. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
  631. $siblingcoursecontext = context_course::instance($siblingcourse->id);
  632. // Create course tags.
  633. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']);
  634. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']);
  635. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']);
  636. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']);
  637. get_question_options($questions, true);
  638. foreach ($questions as $question) {
  639. $this->assertCount(2, $question->coursetagobjects);
  640. foreach ($question->coursetagobjects as $tag) {
  641. if ($tag->name == 'c1') {
  642. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  643. } else {
  644. $this->assertEquals($siblingcoursecontext->id, $tag->taginstancecontextid);
  645. }
  646. }
  647. }
  648. }
  649. /**
  650. * get_question_options should filter the course tags by the given list of courses.
  651. */
  652. public function test_get_question_options_includes_filter_course_tags() {
  653. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  654. $question1 = $questions[0];
  655. $question2 = $questions[1];
  656. $coursecontext = context_course::instance($course->id);
  657. // Create a sibling course.
  658. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
  659. $siblingcoursecontext = context_course::instance($siblingcourse->id);
  660. // Create course tags.
  661. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']);
  662. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']);
  663. // Create sibling course tags. These should be filtered out.
  664. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']);
  665. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']);
  666. // Ask to only receive course tags from $course (ignoring $siblingcourse tags).
  667. get_question_options($questions, true, [$course]);
  668. foreach ($questions as $question) {
  669. foreach ($question->coursetagobjects as $tag) {
  670. // We should only be seeing course tags from $course. The tags from
  671. // $siblingcourse should have been filtered out.
  672. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  673. }
  674. }
  675. }
  676. /**
  677. * question_move_question_tags_to_new_context should update all of the
  678. * question tags contexts when they are moving down (from system to course
  679. * category context).
  680. */
  681. public function test_question_move_question_tags_to_new_context_system_to_course_cat_qtags() {
  682. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
  683. $question1 = $questions[0];
  684. $question2 = $questions[1];
  685. $qcontext = context::instance_by_id($qcat->contextid);
  686. $newcontext = context_coursecat::instance($category->id);
  687. foreach ($questions as $question) {
  688. $question->contextid = $qcat->contextid;
  689. }
  690. // Create tags in the system context.
  691. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
  692. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']);
  693. question_move_question_tags_to_new_context($questions, $newcontext);
  694. foreach ($questions as $question) {
  695. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  696. // All of the tags should have their context id set to the new context.
  697. foreach ($tags as $tag) {
  698. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  699. }
  700. }
  701. }
  702. /**
  703. * question_move_question_tags_to_new_context should update all of the question tags
  704. * contexts when they are moving down (from system to course category context)
  705. * but leave any tags in the course context where they are.
  706. */
  707. public function test_question_move_question_tags_to_new_context_system_to_course_cat_qtags_and_course_tags() {
  708. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
  709. $question1 = $questions[0];
  710. $question2 = $questions[1];
  711. $qcontext = context::instance_by_id($qcat->contextid);
  712. $coursecontext = context_course::instance($course->id);
  713. $newcontext = context_coursecat::instance($category->id);
  714. foreach ($questions as $question) {
  715. $question->contextid = $qcat->contextid;
  716. }
  717. // Create tags in the system context.
  718. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  719. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  720. // Create tags in the course context.
  721. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
  722. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
  723. question_move_question_tags_to_new_context($questions, $newcontext);
  724. foreach ($questions as $question) {
  725. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  726. foreach ($tags as $tag) {
  727. if ($tag->name == 'ctag') {
  728. // Course tags should remain in the course context.
  729. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  730. } else {
  731. // Other tags should be updated.
  732. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  733. }
  734. }
  735. }
  736. }
  737. /**
  738. * question_move_question_tags_to_new_context should update all of the question
  739. * contexts tags when they are moving up (from course category to system context).
  740. */
  741. public function test_question_move_question_tags_to_new_context_course_cat_to_system_qtags() {
  742. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  743. $question1 = $questions[0];
  744. $question2 = $questions[1];
  745. $qcontext = context::instance_by_id($qcat->contextid);
  746. $newcontext = context_system::instance();
  747. foreach ($questions as $question) {
  748. $question->contextid = $qcat->contextid;
  749. }
  750. // Create tags in the course category context.
  751. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
  752. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo', 'bar']);
  753. question_move_question_tags_to_new_context($questions, $newcontext);
  754. foreach ($questions as $question) {
  755. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  756. // All of the tags should have their context id set to the new context.
  757. foreach ($tags as $tag) {
  758. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  759. }
  760. }
  761. }
  762. /**
  763. * question_move_question_tags_to_new_context should update all of the question
  764. * tags contexts when they are moving up (from course category context to system
  765. * context) but leave any tags in the course context where they are.
  766. */
  767. public function test_question_move_question_tags_to_new_context_course_cat_to_system_qtags_and_course_tags() {
  768. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  769. $question1 = $questions[0];
  770. $question2 = $questions[1];
  771. $qcontext = context::instance_by_id($qcat->contextid);
  772. $coursecontext = context_course::instance($course->id);
  773. $newcontext = context_system::instance();
  774. foreach ($questions as $question) {
  775. $question->contextid = $qcat->contextid;
  776. }
  777. // Create tags in the system context.
  778. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  779. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  780. // Create tags in the course context.
  781. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
  782. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
  783. question_move_question_tags_to_new_context($questions, $newcontext);
  784. foreach ($questions as $question) {
  785. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  786. foreach ($tags as $tag) {
  787. if ($tag->name == 'ctag') {
  788. // Course tags should remain in the course context.
  789. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  790. } else {
  791. // Other tags should be updated.
  792. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  793. }
  794. }
  795. }
  796. }
  797. /**
  798. * question_move_question_tags_to_new_context should merge all tags into the course
  799. * context when moving down from course category context into course context.
  800. */
  801. public function test_question_move_question_tags_to_new_context_course_cat_to_coures_qtags_and_course_tags() {
  802. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  803. $question1 = $questions[0];
  804. $question2 = $questions[1];
  805. $qcontext = context::instance_by_id($qcat->contextid);
  806. $coursecontext = context_course::instance($course->id);
  807. $newcontext = $coursecontext;
  808. foreach ($questions as $question) {
  809. $question->contextid = $qcat->contextid;
  810. }
  811. // Create tags in the system context.
  812. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  813. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  814. // Create tags in the course context.
  815. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
  816. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
  817. question_move_question_tags_to_new_context($questions, $newcontext);
  818. foreach ($questions as $question) {
  819. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  820. // Each question should have 2 tags.
  821. $this->assertCount(2, $tags);
  822. foreach ($tags as $tag) {
  823. // All tags should be updated to the course context and merged in.
  824. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  825. }
  826. }
  827. }
  828. /**
  829. * question_move_question_tags_to_new_context should delete all of the tag
  830. * instances from sibling courses when moving the context of a question down
  831. * from a course category into a course context because the other courses will
  832. * no longer have access to the question.
  833. */
  834. public function test_question_move_question_tags_to_new_context_remove_other_course_tags() {
  835. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  836. // Create a sibling course.
  837. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
  838. $question1 = $questions[0];
  839. $question2 = $questions[1];
  840. $qcontext = context::instance_by_id($qcat->contextid);
  841. $coursecontext = context_course::instance($course->id);
  842. $siblingcoursecontext = context_course::instance($siblingcourse->id);
  843. $newcontext = $coursecontext;
  844. foreach ($questions as $question) {
  845. $question->contextid = $qcat->contextid;
  846. }
  847. // Create tags in the system context.
  848. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  849. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  850. // Create tags in the target course context.
  851. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
  852. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
  853. // Create tags in the sibling course context. These should be deleted as
  854. // part of the move.
  855. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['stag']);
  856. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['stag']);
  857. question_move_question_tags_to_new_context($questions, $newcontext);
  858. foreach ($questions as $question) {
  859. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  860. // Each question should have 2 tags, 'foo' and 'ctag'.
  861. $this->assertCount(2, $tags);
  862. foreach ($tags as $tag) {
  863. $tagname = $tag->name;
  864. // The 'stag' should have been deleted because it's in a sibling
  865. // course context.
  866. $this->assertContains($tagname, ['foo', 'ctag']);
  867. // All tags should be in the course context now.
  868. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  869. }
  870. }
  871. }
  872. /**
  873. * question_move_question_tags_to_new_context should update all of the question
  874. * tags to be the course category context when moving the tags from a course
  875. * context to a course category context.
  876. */
  877. public function test_question_move_question_tags_to_new_context_course_to_course_cat() {
  878. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
  879. $question1 = $questions[0];
  880. $question2 = $questions[1];
  881. $qcontext = context::instance_by_id($qcat->contextid);
  882. // Moving up into the course category context.
  883. $newcontext = context_coursecat::instance($category->id);
  884. foreach ($questions as $question) {
  885. $question->contextid = $qcat->contextid;
  886. }
  887. // Create tags in the course context.
  888. core_tag_tag::set_item_tags('core_question', 'question', $question1-

Large files files are truncated, but you can click here to view the full file