PageRenderTime 37ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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->id, $qcontext, ['foo']);
  889. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  890. question_move_question_tags_to_new_context($questions, $newcontext);
  891. foreach ($questions as $question) {
  892. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  893. // All of the tags should have their context id set to the new context.
  894. foreach ($tags as $tag) {
  895. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  896. }
  897. }
  898. }
  899. /**
  900. * question_move_question_tags_to_new_context should update all of the
  901. * question tags contexts when they are moving down (from system to course
  902. * category context).
  903. */
  904. public function test_question_move_question_tags_to_new_context_orphaned_tag_contexts() {
  905. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
  906. $question1 = $questions[0];
  907. $question2 = $questions[1];
  908. $othercategory = $this->getDataGenerator()->create_category();
  909. $qcontext = context::instance_by_id($qcat->contextid);
  910. $newcontext = context_coursecat::instance($category->id);
  911. $othercategorycontext = context_coursecat::instance($othercategory->id);
  912. foreach ($questions as $question) {
  913. $question->contextid = $qcat->contextid;
  914. }
  915. // Create tags in the system context.
  916. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  917. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  918. // Create tags in the other course category context. These should be
  919. // update to the next context id because they represent erroneous data
  920. // from a time before context id was mandatory in the tag API.
  921. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercategorycontext, ['bar']);
  922. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercategorycontext, ['bar']);
  923. question_move_question_tags_to_new_context($questions, $newcontext);
  924. foreach ($questions as $question) {
  925. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  926. // Each question should have two tags, 'foo' and 'bar'.
  927. $this->assertCount(2, $tags);
  928. // All of the tags should have their context id set to the new context
  929. // (course category context).
  930. foreach ($tags as $tag) {
  931. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  932. }
  933. }
  934. }
  935. /**
  936. * When moving from a course category context down into an activity context
  937. * all question context tags and course tags (where the course is a parent of
  938. * the activity) should move into the new context.
  939. */
  940. public function test_question_move_question_tags_to_new_context_course_cat_to_activity_qtags_and_course_tags() {
  941. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  942. $question1 = $questions[0];
  943. $question2 = $questions[1];
  944. $qcontext = context::instance_by_id($qcat->contextid);
  945. $coursecontext = context_course::instance($course->id);
  946. $newcontext = context_module::instance($quiz->cmid);
  947. foreach ($questions as $question) {
  948. $question->contextid = $qcat->contextid;
  949. }
  950. // Create tags in the course category context.
  951. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  952. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  953. // Move the questions to the activity context which is a child context of
  954. // $coursecontext.
  955. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
  956. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
  957. question_move_question_tags_to_new_context($questions, $newcontext);
  958. foreach ($questions as $question) {
  959. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  960. // Each question should have 2 tags.
  961. $this->assertCount(2, $tags);
  962. foreach ($tags as $tag) {
  963. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  964. }
  965. }
  966. }
  967. /**
  968. * When moving from a course category context down into an activity context
  969. * all question context tags and course tags (where the course is a parent of
  970. * the activity) should move into the new context. Tags in course contexts
  971. * that are not a parent of the activity context should be deleted.
  972. */
  973. public function test_question_move_question_tags_to_new_context_course_cat_to_activity_orphaned_tags() {
  974. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  975. $question1 = $questions[0];
  976. $question2 = $questions[1];
  977. $qcontext = context::instance_by_id($qcat->contextid);
  978. $coursecontext = context_course::instance($course->id);
  979. $newcontext = context_module::instance($quiz->cmid);
  980. $othercourse = $this->getDataGenerator()->create_course();
  981. $othercoursecontext = context_course::instance($othercourse->id);
  982. foreach ($questions as $question) {
  983. $question->contextid = $qcat->contextid;
  984. }
  985. // Create tags in the course category context.
  986. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  987. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  988. // Create tags in the course context.
  989. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['ctag']);
  990. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['ctag']);
  991. // Create tags in the other course context. These should be deleted.
  992. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']);
  993. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']);
  994. // Move the questions to the activity context which is a child context of
  995. // $coursecontext.
  996. question_move_question_tags_to_new_context($questions, $newcontext);
  997. foreach ($questions as $question) {
  998. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  999. // Each question should have 2 tags.
  1000. $this->assertCount(2, $tags);
  1001. foreach ($tags as $tag) {
  1002. // Make sure we don't have any 'delete' tags.
  1003. $this->assertContains($tag->name, ['foo', 'ctag']);
  1004. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  1005. }
  1006. }
  1007. }
  1008. /**
  1009. * When moving from a course context down into an activity context all of the
  1010. * course tags should move into the activity context.
  1011. */
  1012. public function test_question_move_question_tags_to_new_context_course_to_activity_qtags() {
  1013. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('course');
  1014. $question1 = $questions[0];
  1015. $question2 = $questions[1];
  1016. $qcontext = context::instance_by_id($qcat->contextid);
  1017. $newcontext = context_module::instance($quiz->cmid);
  1018. foreach ($questions as $question) {
  1019. $question->contextid = $qcat->contextid;
  1020. }
  1021. // Create tags in the course context.
  1022. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  1023. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  1024. question_move_question_tags_to_new_context($questions, $newcontext);
  1025. foreach ($questions as $question) {
  1026. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  1027. foreach ($tags as $tag) {
  1028. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  1029. }
  1030. }
  1031. }
  1032. /**
  1033. * When moving from a course context down into an activity context all of the
  1034. * course tags should move into the activity context.
  1035. */
  1036. public function test_question_move_question_tags_to_new_context_activity_to_course_qtags() {
  1037. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions();
  1038. $question1 = $questions[0];
  1039. $question2 = $questions[1];
  1040. $qcontext = context::instance_by_id($qcat->contextid);
  1041. $newcontext = context_course::instance($course->id);
  1042. foreach ($questions as $question) {
  1043. $question->contextid = $qcat->contextid;
  1044. }
  1045. // Create tags in the activity context.
  1046. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  1047. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  1048. question_move_question_tags_to_new_context($questions, $newcontext);
  1049. foreach ($questions as $question) {
  1050. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  1051. foreach ($tags as $tag) {
  1052. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  1053. }
  1054. }
  1055. }
  1056. /**
  1057. * question_move_question_tags_to_new_context should update all of the
  1058. * question tags contexts when they are moving down (from system to course
  1059. * category context).
  1060. *
  1061. * Course tags within the new category context should remain while any course
  1062. * tags in course contexts that can no longer access the question should be
  1063. * deleted.
  1064. */
  1065. public function test_question_move_question_tags_to_new_context_system_to_course_cat_with_orphaned_tags() {
  1066. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('system');
  1067. $question1 = $questions[0];
  1068. $question2 = $questions[1];
  1069. $othercategory = $this->getDataGenerator()->create_category();
  1070. $othercourse = $this->getDataGenerator()->create_course(['category' => $othercategory->id]);
  1071. $qcontext = context::instance_by_id($qcat->contextid);
  1072. $newcontext = context_coursecat::instance($category->id);
  1073. $othercategorycontext = context_coursecat::instance($othercategory->id);
  1074. $coursecontext = context_course::instance($course->id);
  1075. $othercoursecontext = context_course::instance($othercourse->id);
  1076. foreach ($questions as $question) {
  1077. $question->contextid = $qcat->contextid;
  1078. }
  1079. // Create tags in the system context.
  1080. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo']);
  1081. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['foo']);
  1082. // Create tags in the child course context of the new context.
  1083. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['bar']);
  1084. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']);
  1085. // Create tags in the other course context. These should be deleted when
  1086. // the question moves to the new course category context because this
  1087. // course belongs to a different category, which means it will no longer
  1088. // have access to the question.
  1089. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $othercoursecontext, ['delete']);
  1090. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $othercoursecontext, ['delete']);
  1091. question_move_question_tags_to_new_context($questions, $newcontext);
  1092. foreach ($questions as $question) {
  1093. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  1094. // Each question should have two tags, 'foo' and 'bar'.
  1095. $this->assertCount(2, $tags);
  1096. // All of the tags should have their context id set to the new context
  1097. // (course category context).
  1098. foreach ($tags as $tag) {
  1099. $this->assertContains($tag->name, ['foo', 'bar']);
  1100. if ($tag->name == 'foo') {
  1101. $this->assertEquals($newcontext->id, $tag->taginstancecontextid);
  1102. } else {
  1103. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  1104. }
  1105. }
  1106. }
  1107. }
  1108. /**
  1109. * question_sort_tags() includes the tags for all questions in the list.
  1110. */
  1111. public function test_question_sort_tags_includes_question_tags() {
  1112. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  1113. $question1 = $questions[0];
  1114. $question2 = $questions[1];
  1115. $qcontext = context::instance_by_id($qcat->contextid);
  1116. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $qcontext, ['foo', 'bar']);
  1117. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $qcontext, ['baz', 'bop']);
  1118. foreach ($questions as $question) {
  1119. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  1120. $categorycontext = context::instance_by_id($qcat->contextid);
  1121. $tagobjects = question_sort_tags($tags, $categorycontext);
  1122. $expectedtags = [];
  1123. $actualtags = $tagobjects->tags;
  1124. foreach ($tagobjects->tagobjects as $tag) {
  1125. $expectedtags[$tag->id] = $tag->name;
  1126. }
  1127. // The question should have a tags property populated with each tag id
  1128. // and display name as a key vale pair.
  1129. $this->assertEquals($expectedtags, $actualtags);
  1130. $actualtagobjects = $tagobjects->tagobjects;
  1131. sort($tags);
  1132. sort($actualtagobjects);
  1133. // The question should have a full set of each tag object.
  1134. $this->assertEquals($tags, $actualtagobjects);
  1135. // The question should not have any course tags.
  1136. $this->assertEmpty($tagobjects->coursetagobjects);
  1137. }
  1138. }
  1139. /**
  1140. * question_sort_tags() includes course tags for all questions in the list.
  1141. */
  1142. public function test_question_sort_tags_includes_question_course_tags() {
  1143. global $DB;
  1144. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  1145. $question1 = $questions[0];
  1146. $question2 = $questions[1];
  1147. $coursecontext = context_course::instance($course->id);
  1148. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo', 'bar']);
  1149. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['baz', 'bop']);
  1150. foreach ($questions as $question) {
  1151. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  1152. $tagobjects = question_sort_tags($tags, $qcat);
  1153. $expectedtags = [];
  1154. $actualtags = $tagobjects->coursetags;
  1155. foreach ($actualtags as $coursetagid => $coursetagname) {
  1156. $expectedtags[$coursetagid] = $coursetagname;
  1157. }
  1158. // The question should have a tags property populated with each tag id
  1159. // and display name as a key vale pair.
  1160. $this->assertEquals($expectedtags, $actualtags);
  1161. $actualtagobjects = $tagobjects->coursetagobjects;
  1162. sort($tags);
  1163. sort($actualtagobjects);
  1164. // The question should have a full set of each tag object.
  1165. $this->assertEquals($tags, $actualtagobjects);
  1166. // The question should not have any course tags.
  1167. $this->assertEmpty($tagobjects->tagobjects);
  1168. }
  1169. }
  1170. /**
  1171. * question_sort_tags() should return tags from all course contexts by default.
  1172. */
  1173. public function test_question_sort_tags_includes_multiple_courses_tags() {
  1174. global $DB;
  1175. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  1176. $question1 = $questions[0];
  1177. $question2 = $questions[1];
  1178. $coursecontext = context_course::instance($course->id);
  1179. // Create a sibling course.
  1180. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
  1181. $siblingcoursecontext = context_course::instance($siblingcourse->id);
  1182. // Create course tags.
  1183. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['c1']);
  1184. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['c1']);
  1185. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['c2']);
  1186. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['c2']);
  1187. foreach ($questions as $question) {
  1188. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  1189. $tagobjects = question_sort_tags($tags, $qcat);
  1190. $this->assertCount(2, $tagobjects->coursetagobjects);
  1191. foreach ($tagobjects->coursetagobjects as $tag) {
  1192. if ($tag->name == 'c1') {
  1193. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  1194. } else {
  1195. $this->assertEquals($siblingcoursecontext->id, $tag->taginstancecontextid);
  1196. }
  1197. }
  1198. }
  1199. }
  1200. /**
  1201. * question_sort_tags() should filter the course tags by the given list of courses.
  1202. */
  1203. public function test_question_sort_tags_includes_filter_course_tags() {
  1204. global $DB;
  1205. list($category, $course, $quiz, $qcat, $questions) = $this->setup_quiz_and_questions('category');
  1206. $question1 = $questions[0];
  1207. $question2 = $questions[1];
  1208. $coursecontext = context_course::instance($course->id);
  1209. // Create a sibling course.
  1210. $siblingcourse = $this->getDataGenerator()->create_course(['category' => $course->category]);
  1211. $siblingcoursecontext = context_course::instance($siblingcourse->id);
  1212. // Create course tags.
  1213. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $coursecontext, ['foo']);
  1214. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $coursecontext, ['bar']);
  1215. // Create sibling course tags. These should be filtered out.
  1216. core_tag_tag::set_item_tags('core_question', 'question', $question1->id, $siblingcoursecontext, ['filtered1']);
  1217. core_tag_tag::set_item_tags('core_question', 'question', $question2->id, $siblingcoursecontext, ['filtered2']);
  1218. foreach ($questions as $question) {
  1219. $tags = core_tag_tag::get_item_tags('core_question', 'question', $question->id);
  1220. $tagobjects = question_sort_tags($tags, $qcat, [$course]);
  1221. foreach ($tagobjects->coursetagobjects as $tag) {
  1222. // We should only be seeing course tags from $course. The tags from
  1223. // $siblingcourse should have been filtered out.
  1224. $this->assertEquals($coursecontext->id, $tag->taginstancecontextid);
  1225. }
  1226. }
  1227. }
  1228. /**
  1229. * Data provider for tests of question_has_capability_on_context and question_require_capability_on_context.
  1230. *
  1231. * @return array
  1232. */
  1233. public function question_capability_on_question_provider() {
  1234. return [
  1235. 'Unrelated capability which is present' => [
  1236. 'capabilities' => [
  1237. 'moodle/question:config' => CAP_ALLOW,
  1238. ],
  1239. 'testcapability' => 'config',
  1240. 'isowner' => true,
  1241. 'expect' => true,
  1242. ],
  1243. 'Unrelated capability which is present (not owner)' => [
  1244. 'capabilities' => [
  1245. 'moodle/question:config' => CAP_ALLOW,
  1246. ],
  1247. 'testcapability' => 'config',
  1248. 'isowner' => false,
  1249. 'expect' => true,
  1250. ],
  1251. 'Unrelated capability which is not set' => [
  1252. 'capabilities' => [
  1253. ],
  1254. 'testcapability' => 'config',
  1255. 'isowner' => true,
  1256. 'expect' => false,
  1257. ],
  1258. 'Unrelated capability which is not set (not owner)' => [
  1259. 'capabilities' => [
  1260. ],
  1261. 'testcapability' => 'config',
  1262. 'isowner' => false,
  1263. 'expect' => false,
  1264. ],
  1265. 'Unrelated capability which is prevented' => [
  1266. 'capabilities' => [
  1267. 'moodle/question:config' => CAP_PREVENT,
  1268. ],
  1269. 'testcapability' => 'config',
  1270. 'isowner' => true,
  1271. 'expect' => false,
  1272. ],
  1273. 'Unrelated capability which is prevented (not owner)' => [
  1274. 'capabilities' => [
  1275. 'moodle/question:config' => CAP_PREVENT,
  1276. ],
  1277. 'testcapability' => 'config',
  1278. 'isowner' => false,
  1279. 'expect' => false,
  1280. ],
  1281. 'Related capability which is not set' => [
  1282. 'capabilities' => [
  1283. ],
  1284. 'testcapability' => 'edit',
  1285. 'isowner' => true,
  1286. 'expect' => false,
  1287. ],
  1288. 'Related capability which is not set (not owner)' => [
  1289. 'capabilities' => [
  1290. ],
  1291. 'testcapability' => 'edit',
  1292. 'isowner' => false,
  1293. 'expect' => false,
  1294. ],
  1295. 'Related capability which is allowed at all, unset at mine' => [
  1296. 'capabilities' => [
  1297. 'moodle/question:editall' => CAP_ALLOW,
  1298. ],
  1299. 'testcapability' => 'edit',
  1300. 'isowner' => true,
  1301. 'expect' => true,
  1302. ],
  1303. 'Related capability which is allowed at all, unset at mine (not owner)' => [
  1304. 'capabilities' => [
  1305. 'moodle/question:editall' => CAP_ALLOW,
  1306. ],
  1307. 'testcapability' => 'edit',
  1308. 'isowner' => false,
  1309. 'expect' => true,
  1310. ],
  1311. 'Related capability which is allowed at all, prevented at mine' => [
  1312. 'capabilities' => [
  1313. 'moodle/question:editall' => CAP_ALLOW,
  1314. 'moodle/question:editmine' => CAP_PREVENT,
  1315. ],
  1316. 'testcapability' => 'edit',
  1317. 'isowner' => true,
  1318. 'expect' => true,
  1319. ],
  1320. 'Related capability which is allowed at all, prevented at mine (not owner)' => [
  1321. 'capabilities' => [
  1322. 'moodle/question:editall' => CAP_ALLOW,
  1323. 'moodle/question:editmine' => CAP_PREVENT,
  1324. ],
  1325. 'testcapability' => 'edit',
  1326. 'isowner' => false,
  1327. 'expect' => true,
  1328. ],
  1329. 'Related capability which is unset all, allowed at mine' => [
  1330. 'capabilities' => [
  1331. 'moodle/question:editall' => CAP_PREVENT,
  1332. 'moodle/question:editmine' => CAP_ALLOW,
  1333. ],
  1334. 'testcapability' => 'edit',
  1335. 'isowner' => true,
  1336. 'expect' => true,
  1337. ],
  1338. 'Related capability which is unset all, allowed at mine (not owner)' => [
  1339. 'capabilities' => [
  1340. 'moodle/question:editall' => CAP_PREVENT,
  1341. 'moodle/question:editmine' => CAP_ALLOW,
  1342. ],
  1343. 'testcapability' => 'edit',
  1344. 'isowner' => false,
  1345. 'expect' => false,
  1346. ],
  1347. ];
  1348. }
  1349. /**
  1350. * Tests that question_has_capability_on does not throw exception on broken questions.
  1351. */
  1352. public function test_question_has_capability_on_broken_question() {
  1353. global $DB;
  1354. // Create the test data.
  1355. $generator = $this->getDataGenerator();
  1356. $questiongenerator = $generator->get_plugin_generator('core_question');
  1357. $category = $generator->create_category();
  1358. $context = context_coursecat::instance($category->id);
  1359. $questioncat = $questiongenerator->create_question_category([
  1360. 'contextid' => $context->id,
  1361. ]);
  1362. // Create a cloze question.
  1363. $question = $questiongenerator->create_question('multianswer', null, [
  1364. 'category' => $questioncat->id,
  1365. ]);
  1366. // Now, break the question.
  1367. $DB->delete_records('question_multianswer', ['question' => $question->id]);
  1368. $this->setAdminUser();
  1369. $result = question_has_capability_on($question->id, 'tag');
  1370. $this->assertTrue($result);
  1371. $this->assertDebuggingCalled();
  1372. }
  1373. /**
  1374. * Tests for the deprecated question_has_capability_on function when passing a stdClass as parameter.
  1375. *
  1376. * @dataProvider question_capability_on_question_provider
  1377. * @param array $capabilities The capability assignments to set.
  1378. * @param string $capability The capability to test
  1379. * @param bool $isowner Whether the user to create the question should be the owner or not.
  1380. * @param bool $expect The expected result.
  1381. */
  1382. public function test_question_has_capability_on_using_stdclass($capabilities, $capability, $isowner, $expect) {
  1383. $this->resetAfterTest();
  1384. // Create the test data.
  1385. $user = $this->getDataGenerator()->create_user();
  1386. $otheruser = $this->getDataGenerator()->create_user();
  1387. $roleid = $this->getDataGenerator()->create_role();
  1388. $category = $this->getDataGenerator()->create_category();
  1389. $context = context_coursecat::instance($category->id);
  1390. // Assign the user to the role.
  1391. role_assign($roleid, $user->id, $context->id);
  1392. // Assign the capabilities to the role.
  1393. foreach ($capabilities as $capname => $capvalue) {
  1394. assign_capability($capname, $capvalue, $roleid, $context->id);
  1395. }
  1396. $this->setUser($user);
  1397. // The current fake question we make use of is always a stdClass and typically has no ID.
  1398. $fakequestion = (object) [
  1399. 'contextid' => $context->id,
  1400. ];
  1401. if ($isowner) {
  1402. $fakequestion->createdby = $user->id;
  1403. } else {
  1404. $fakequestion->createdby = $otheruser->id;
  1405. }
  1406. $result = question_has_capability_on($fakequestion, $capability);
  1407. $this->assertEquals($expect, $result);
  1408. }
  1409. /**
  1410. * Tests for the deprecated question_has_capability_on function when using question definition.
  1411. *
  1412. * @dataProvider question_capability_on_question_provider
  1413. * @param array $capabilities The capability assignments to set.
  1414. * @param string $capability The capability to test
  1415. * @param bool $isowner Whether the user to create the question should be the owner or not.
  1416. * @param bool $expect The expected result.
  1417. */
  1418. public function test_question_has_capability_on_using_question_definition($capabilities, $capability, $isowner, $expect) {
  1419. $this->resetAfterTest();
  1420. // Create the test data.
  1421. $generator = $this->getDataGenerator();
  1422. $questiongenerator = $generator->get_plugin_generator('core_question');
  1423. $user = $generator->create_user();
  1424. $otheruser = $generator->create_user();
  1425. $roleid = $generator->create_role();
  1426. $category = $generator->create_category();
  1427. $context = context_coursecat::instance($category->id);
  1428. $questioncat = $questiongenerator->create_question_category([
  1429. 'contextid' => $context->id,
  1430. ]);
  1431. // Assign the user to the role.
  1432. role_assign($roleid, $user->id, $context->id);
  1433. // Assign the capabilities to the role.
  1434. foreach ($capabilities as $capname => $capvalue) {
  1435. assign_capability($capname, $capvalue, $roleid, $context->id);
  1436. }
  1437. // Create the question.
  1438. $qtype = 'truefalse';
  1439. $overrides = [
  1440. 'category' => $questioncat->id,
  1441. ];
  1442. $question = $questiongenerator->create_question($qtype, null, $overrides);
  1443. // The question generator does not support setting of the createdby for some reason.
  1444. $question->createdby = ($isowner) ? $user->id : $otheruser->id;
  1445. $fromform = test_question_maker::get_question_form_data($qtype, null);
  1446. $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides);
  1447. question_bank::get_qtype($qtype)->save_question($question, $fromform);
  1448. $this->setUser($user);
  1449. $result = question_has_capability_on($question, $capability);
  1450. $this->assertEquals($expect, $result);
  1451. }
  1452. /**
  1453. * Tests for the deprecated question_has_capability_on function when using a real question id.
  1454. *
  1455. * @dataProvider question_capability_on_question_provider
  1456. * @param array $capabilities The capability assignments to set.
  1457. * @param string $capability The capability to test
  1458. * @param bool $isowner Whether the user to create the question should be the owner or not.
  1459. * @param bool $expect The expected result.
  1460. */
  1461. public function test_question_has_capability_on_using_question_id($capabilities, $capability, $isowner, $expect) {
  1462. $this->resetAfterTest();
  1463. // Create the test data.
  1464. $generator = $this->getDataGenerator();
  1465. $questiongenerator = $generator->get_plugin_generator('core_question');
  1466. $user = $generator->create_user();
  1467. $otheruser = $generator->create_user();
  1468. $roleid = $generator->create_role();
  1469. $category = $generator->create_category();
  1470. $context = context_coursecat::instance($category->id);
  1471. $questioncat = $questiongenerator->create_question_category([
  1472. 'contextid' => $context->id,
  1473. ]);
  1474. // Assign the user to the role.
  1475. role_assign($roleid, $user->id, $context->id);
  1476. // Assign the capabilities to the role.
  1477. foreach ($capabilities as $capname => $capvalue) {
  1478. assign_capability($capname, $capvalue, $roleid, $context->id);
  1479. }
  1480. // Create the question.
  1481. $qtype = 'truefalse';
  1482. $overrides = [
  1483. 'category' => $questioncat->id,
  1484. ];
  1485. $question = $questiongenerator->create_question($qtype, null, $overrides);
  1486. // The question generator does not support setting of the createdby for some reason.
  1487. $question->createdby = ($isowner) ? $user->id : $otheruser->id;
  1488. $fromform = test_question_maker::get_question_form_data($qtype, null);
  1489. $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides);
  1490. question_bank::get_qtype($qtype)->save_question($question, $fromform);
  1491. $this->setUser($user);
  1492. $result = question_has_capability_on($question->id, $capability);
  1493. $this->assertEquals($expect, $result);
  1494. }
  1495. /**
  1496. * Tests for the deprecated question_has_capability_on function when using a string as question id.
  1497. *
  1498. * @dataProvider question_capability_on_question_provider
  1499. * @param array $capabilities The capability assignments to set.
  1500. * @param string $capability The capability to test
  1501. * @param bool $isowner Whether the user to create the question should be the owner or not.
  1502. * @param bool $expect The expected result.
  1503. */
  1504. public function test_question_has_capability_on_using_question_string_id($capabilities, $capability, $isowner, $expect) {
  1505. $this->resetAfterTest();
  1506. // Create the test data.
  1507. $generator = $this->getDataGenerator();
  1508. $questiongenerator = $generator->get_plugin_generator('core_question');
  1509. $user = $generator->create_user();
  1510. $otheruser = $generator->create_user();
  1511. $roleid = $generator->create_role();
  1512. $category = $generator->create_category();
  1513. $context = context_coursecat::instance($category->id);
  1514. $questioncat = $questiongenerator->create_question_category([
  1515. 'contextid' => $context->id,
  1516. ]);
  1517. // Assign the user to the role.
  1518. role_assign($roleid, $user->id, $context->id);
  1519. // Assign the capabilities to the role.
  1520. foreach ($capabilities as $capname => $capvalue) {
  1521. assign_capability($capname, $capvalue, $roleid, $context->id);
  1522. }
  1523. // Create the question.
  1524. $qtype = 'truefalse';
  1525. $overrides = [
  1526. 'category' => $questioncat->id,
  1527. ];
  1528. $question = $questiongenerator->create_question($qtype, null, $overrides);
  1529. // The question generator does not support setting of the createdby for some reason.
  1530. $question->createdby = ($isowner) ? $user->id : $otheruser->id;
  1531. $fromform = test_question_maker::get_question_form_data($qtype, null);
  1532. $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides);
  1533. question_bank::get_qtype($qtype)->save_question($question, $fromform);
  1534. $this->setUser($user);
  1535. $result = question_has_capability_on((string) $question->id, $capability);
  1536. $this->assertEquals($expect, $result);
  1537. }
  1538. /**
  1539. * Tests for the question_has_capability_on function when using a moved question.
  1540. *
  1541. * @dataProvider question_capability_on_question_provider
  1542. * @param array $capabilities The capability assignments to set.
  1543. * @param string $capability The capability to test
  1544. * @param bool $isowner Whether the user to create the question should be the owner or not.
  1545. * @param bool $expect The expected result.
  1546. */
  1547. public function test_question_has_capability_on_using_moved_question($capabilities, $capability, $isowner, $expect) {
  1548. $this->resetAfterTest();
  1549. // Create the test data.
  1550. $generator = $this->getDataGenerator();
  1551. $questiongenerator = $generator->get_plugin_generator('core_question');
  1552. $user = $generator->create_user();
  1553. $otheruser = $generator->create_user();
  1554. $roleid = $generator->create_role();
  1555. $category = $generator->create_category();
  1556. $context = context_coursecat::instance($category->id);
  1557. $questioncat = $questiongenerator->create_question_category([
  1558. 'contextid' => $context->id,
  1559. ]);
  1560. $newcategory = $generator->create_category();
  1561. $newcontext = context_coursecat::instance($newcategory->id);
  1562. $newquestioncat = $questiongenerator->create_question_category([
  1563. 'contextid' => $newcontext->id,
  1564. ]);
  1565. // Assign the user to the role in the _new_ context..
  1566. role_assign($roleid, $user->id, $newcontext->id);
  1567. // Assign the capabilities to the role in the _new_ context.
  1568. foreach ($capabilities as $capname => $capvalue) {
  1569. assign_capability($capname, $capvalue, $roleid, $newcontext->id);
  1570. }
  1571. // Create the question.
  1572. $qtype = 'truefalse';
  1573. $overrides = [
  1574. 'category' => $questioncat->id,
  1575. ];
  1576. $question = $questiongenerator->create_question($qtype, null, $overrides);
  1577. // The question generator does not support setting of the createdby for some reason.
  1578. $question->createdby = ($isowner) ? $user->id : $otheruser->id;
  1579. $fromform = test_question_maker::get_question_form_data($qtype, null);
  1580. $fromform = (object) $generator->combine_defaults_and_record((array) $fromform, $overrides);
  1581. question_bank::get_qtype($qtype)->save_question($question, $fromform);
  1582. // Move the question.
  1583. question_move_questions_to_category([$question->id], $newquestioncat->id);
  1584. // Test that the capability is correct after the question has been moved.
  1585. $this->setUser($user);
  1586. $result = question_has_capability_on($question->id, $capability);
  1587. $this->assertEquals($expect, $result);
  1588. }
  1589. /**
  1590. * Tests for the question_has_capability_on function when using a real question.
  1591. *
  1592. * @dataProvider question_capability_on_question_provider
  1593. * @param array $capabilities The capability assignments to set.
  1594. * @param string $capability The capability to test
  1595. * @param bool $isowner Whether the user to create the question should be the owner or not.
  1596. * @param bool $expect The expected result.
  1597. */
  1598. public function test_question_has_capability_on_using_question($capabilities, $capability, $isowner, $expect) {
  1599. $this->resetAfterTest();
  1600. // Create the test data.
  1601. $generator = $this->getDataGenerator();
  1602. $questiongenerator = $generator->get_plugin_generator('core_question');
  1603. $user = $generator->create_user();
  1604. $otheruser = $generator->create_user();
  1605. $roleid = $generator->create_role();
  1606. $category = $generator->create_category();
  1607. $context = context_coursecat::instance($category->id);
  1608. $questioncat = $questiongenerator->create_question_category([
  1609. 'contextid' => $context->id,
  1610. ]);
  1611. // Assign the user to the role.
  1612. role_assign($roleid, $user->id, $context->id);
  1613. // Assign the capabilities to the role.
  1614. foreach ($capabilities as $capname => $capvalue) {
  1615. assign_capability($capname, $capvalue, $roleid, $context->id);
  1616. }
  1617. // Create the question.
  1618. $question = $questiongenerator->create_question('truefalse', null, [
  1619. 'category' => $questioncat->id,
  1620. ]);
  1621. $question = question_bank::load_question_data($question->id);
  1622. // The question generator does not support setting of the createdby for some reason.
  1623. $question->createdby = ($isowner) ? $user->id : $otheruser->id;
  1624. $this->setUser($user);
  1625. $result = question_has_capability_on($question, $capability);
  1626. $this->assertEquals($expect, $result);
  1627. }
  1628. /**
  1629. * Tests that question_has_capability_on throws an exception for wrong parameter types.
  1630. */
  1631. public function test_question_has_capability_on_wrong_param_type() {
  1632. // Create the test data.
  1633. $generator = $this->getDataGenerator();
  1634. $questiongenerator = $generator->get_plugin_generator('core_question');
  1635. $user = $generator->create_user();
  1636. $category = $generator->create_category();
  1637. $context = context_coursecat::instance($category->id);
  1638. $questioncat = $questiongenerator->create_question_category([
  1639. 'contextid' => $context->id,
  1640. ]);
  1641. // Create the question.
  1642. $question = $questiongenerator->create_question('truefalse', null, [
  1643. 'category' => $questioncat->id,
  1644. ]);
  1645. $question = question_bank::load_question_data($question->id);
  1646. // The question generator does not support setting of the createdby for some reason.
  1647. $question->createdby = $user->id;
  1648. $this->setUser($user);
  1649. $result = question_has_capability_on((string)$question->id, 'tag');
  1650. $this->assertFalse($result);
  1651. $this->expectException('coding_exception');
  1652. $this->expectExceptionMessage('$questionorid parameter needs to be an integer or an object.');
  1653. question_has_capability_on('one', 'tag');
  1654. }
  1655. /**
  1656. * Test of question_categorylist_parents function.
  1657. */
  1658. public function test_question_categorylist_parents() {
  1659. $this->resetAfterTest();
  1660. $generator = $this->getDataGenerator();
  1661. $questiongenerator = $generator->get_plugin_generator('core_question');
  1662. $category = $generator->create_category();
  1663. $context = context_coursecat::instance($category->id);
  1664. // Create a top category.
  1665. $cat0 = question_get_top_category($context->id, true);
  1666. // Add sub-categories.
  1667. $cat1 = $questiongenerator->create_question_category(['parent' => $cat0->id]);
  1668. $cat2 = $questiongenerator->create_question_category(['parent' => $cat1->id]);
  1669. // Test the 'get parents' function.
  1670. $parentcategories = question_categorylist_parents($cat2->id);
  1671. $this->assertEquals($cat0->id, $parentcategories[0]);
  1672. $this->assertEquals($cat1->id, $parentcategories[1]);
  1673. $this->assertCount(2, $parentcategories);
  1674. }
  1675. /**
  1676. * Get test cases for test_core_question_find_next_unused_idnumber.
  1677. *
  1678. * @return array test cases.
  1679. */
  1680. public function find_next_unused_idnumber_cases(): array {
  1681. return [
  1682. ['id', null],
  1683. ['id1a', null],
  1684. ['id001', 'id002'],
  1685. ['id9', 'id10'],
  1686. ['id009', 'id010'],
  1687. ['id999', 'id1000'],
  1688. ['0', '1'],
  1689. ['-1', '-2'],
  1690. ['01', '02'],
  1691. ['09', '10'],
  1692. ['1.0E+29', '1.0E+30'], // Idnumbers are strings, not floats.
  1693. ['1.0E-29', '1.0E-30'], // By the way, this is not a sensible idnumber!
  1694. ['10.1', '10.2'],
  1695. ['10.9', '10.10'],
  1696. ];
  1697. }
  1698. /**
  1699. * Test core_question_find_next_unused_idnumber in the case when there are no other questions.
  1700. *
  1701. * @dataProvider find_next_unused_idnumber_cases
  1702. * @param string $oldidnumber value to pass to core_question_find_next_unused_idnumber.
  1703. * @param string|null $expectednewidnumber expected result.
  1704. */
  1705. public function test_core_question_find_next_unused_idnumber(string $oldidnumber, ?string $expectednewidnumber) {
  1706. $this->assertSame($expectednewidnumber, core_question_find_next_unused_idnumber($oldidnumber, 0));
  1707. }
  1708. public function test_core_question_find_next_unused_idnumber_skips_used() {
  1709. $this->resetAfterTest();
  1710. /** @var core_question_generator $generator */
  1711. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  1712. $category = $generator->create_question_category();
  1713. $othercategory = $generator->create_question_category();
  1714. $generator->create_question('truefalse', null, ['category' => $category->id, 'idnumber' => 'id9']);
  1715. $generator->create_question('truefalse', null, ['category' => $category->id, 'idnumber' => 'id10']);
  1716. // Next one to make sure only idnumbers from the right category are ruled out.
  1717. $generator->create_question('truefalse', null, ['category' => $othercategory->id, 'idnumber' => 'id11']);
  1718. $this->assertSame('id11', core_question_find_next_unused_idnumber('id9', $category->id));
  1719. $this->assertSame('id11', core_question_find_next_unused_idnumber('id8', $category->id));
  1720. }
  1721. }