random_question_loader_test.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * Tests for the {@link core_question\bank\random_question_loader} class.
  18. *
  19. * @package core_question
  20. * @copyright 2015 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. /**
  25. * Tests for the {@link core_question\bank\random_question_loader} class.
  26. *
  27. * @copyright 2015 The Open University
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. class random_question_loader_testcase extends advanced_testcase {
  31. public function test_empty_category_gives_null() {
  32. $this->resetAfterTest();
  33. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  34. $cat = $generator->create_question_category();
  35. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  36. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  37. $this->assertNull($loader->get_next_question_id($cat->id, 1));
  38. }
  39. public function test_unknown_category_behaves_like_empty() {
  40. // It is up the caller to make sure the category id is valid.
  41. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  42. $this->assertNull($loader->get_next_question_id(-1, 1));
  43. }
  44. public function test_descriptions_not_returned() {
  45. $this->resetAfterTest();
  46. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  47. $cat = $generator->create_question_category();
  48. $info = $generator->create_question('description', null, array('category' => $cat->id));
  49. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  50. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  51. }
  52. public function test_hidden_questions_not_returned() {
  53. global $DB;
  54. $this->resetAfterTest();
  55. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  56. $cat = $generator->create_question_category();
  57. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  58. $DB->set_field('question', 'hidden', 1, array('id' => $question1->id));
  59. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  60. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  61. }
  62. public function test_cloze_subquestions_not_returned() {
  63. $this->resetAfterTest();
  64. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  65. $cat = $generator->create_question_category();
  66. $question1 = $generator->create_question('multianswer', null, array('category' => $cat->id));
  67. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  68. $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
  69. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  70. }
  71. public function test_random_questions_not_returned() {
  72. $this->resetAfterTest();
  73. $this->setAdminUser();
  74. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  75. $cat = $generator->create_question_category();
  76. $course = $this->getDataGenerator()->create_course();
  77. $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course));
  78. quiz_add_random_questions($quiz, 1, $cat->id, 1, false);
  79. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  80. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  81. }
  82. public function test_one_question_category_returns_that_q_then_null() {
  83. $this->resetAfterTest();
  84. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  85. $cat = $generator->create_question_category();
  86. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  87. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  88. $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 1));
  89. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  90. }
  91. public function test_two_question_category_returns_both_then_null() {
  92. $this->resetAfterTest();
  93. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  94. $cat = $generator->create_question_category();
  95. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  96. $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  97. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  98. $questionids = array();
  99. $questionids[] = $loader->get_next_question_id($cat->id, 0);
  100. $questionids[] = $loader->get_next_question_id($cat->id, 0);
  101. sort($questionids);
  102. $this->assertEquals(array($question1->id, $question2->id), $questionids);
  103. $this->assertNull($loader->get_next_question_id($cat->id, 1));
  104. }
  105. public function test_nested_categories() {
  106. $this->resetAfterTest();
  107. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  108. $cat1 = $generator->create_question_category();
  109. $cat2 = $generator->create_question_category(array('parent' => $cat1->id));
  110. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat1->id));
  111. $question2 = $generator->create_question('shortanswer', null, array('category' => $cat2->id));
  112. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  113. $this->assertEquals($question2->id, $loader->get_next_question_id($cat2->id, 1));
  114. $this->assertEquals($question1->id, $loader->get_next_question_id($cat1->id, 1));
  115. $this->assertNull($loader->get_next_question_id($cat1->id, 0));
  116. }
  117. public function test_used_question_not_returned_until_later() {
  118. $this->resetAfterTest();
  119. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  120. $cat = $generator->create_question_category();
  121. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  122. $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  123. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()),
  124. array($question2->id => 2));
  125. $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
  126. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  127. }
  128. public function test_previously_used_question_not_returned_until_later() {
  129. $this->resetAfterTest();
  130. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  131. $cat = $generator->create_question_category();
  132. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  133. $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  134. $quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
  135. $quba->set_preferred_behaviour('deferredfeedback');
  136. $question = question_bank::load_question($question2->id);
  137. $quba->add_question($question);
  138. $quba->add_question($question);
  139. $quba->start_all_questions();
  140. question_engine::save_questions_usage_by_activity($quba);
  141. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array($quba->get_id())));
  142. $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
  143. $this->assertEquals($question2->id, $loader->get_next_question_id($cat->id, 0));
  144. $this->assertNull($loader->get_next_question_id($cat->id, 0));
  145. }
  146. public function test_empty_category_does_not_have_question_available() {
  147. $this->resetAfterTest();
  148. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  149. $cat = $generator->create_question_category();
  150. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  151. $this->assertFalse($loader->is_question_available($cat->id, 0, 1));
  152. $this->assertFalse($loader->is_question_available($cat->id, 1, 1));
  153. }
  154. public function test_descriptions_not_available() {
  155. $this->resetAfterTest();
  156. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  157. $cat = $generator->create_question_category();
  158. $info = $generator->create_question('description', null, array('category' => $cat->id));
  159. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  160. $this->assertFalse($loader->is_question_available($cat->id, 0, $info->id));
  161. $this->assertFalse($loader->is_question_available($cat->id, 1, $info->id));
  162. }
  163. public function test_existing_question_is_available_but_then_marked_used() {
  164. $this->resetAfterTest();
  165. $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  166. $cat = $generator->create_question_category();
  167. $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  168. $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  169. $this->assertTrue($loader->is_question_available($cat->id, 0, $question1->id));
  170. $this->assertFalse($loader->is_question_available($cat->id, 0, $question1->id));
  171. $this->assertFalse($loader->is_question_available($cat->id, 0, -1));
  172. }
  173. }