MDL-29332 tests: Cover the new sql_equal() function
[moodle.git] / question / tests / random_question_loader_test.php
blobb3a3389c4b68fc66076251415b871191ded11624
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Tests for the {@link core_question\bank\random_question_loader} class.
20 * @package core_question
21 * @copyright 2015 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Tests for the {@link core_question\bank\random_question_loader} class.
31 * @copyright 2015 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class random_question_loader_testcase extends advanced_testcase {
36 public function test_empty_category_gives_null() {
37 $this->resetAfterTest();
38 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
40 $cat = $generator->create_question_category();
41 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
43 $this->assertNull($loader->get_next_question_id($cat->id, 0));
44 $this->assertNull($loader->get_next_question_id($cat->id, 1));
47 public function test_unknown_category_behaves_like_empty() {
48 // It is up the caller to make sure the category id is valid.
49 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
50 $this->assertNull($loader->get_next_question_id(-1, 1));
53 public function test_descriptions_not_returned() {
54 $this->resetAfterTest();
55 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
57 $cat = $generator->create_question_category();
58 $info = $generator->create_question('description', null, array('category' => $cat->id));
59 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
61 $this->assertNull($loader->get_next_question_id($cat->id, 0));
64 public function test_hidden_questions_not_returned() {
65 global $DB;
66 $this->resetAfterTest();
67 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
69 $cat = $generator->create_question_category();
70 $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
71 $DB->set_field('question', 'hidden', 1, array('id' => $question1->id));
72 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
74 $this->assertNull($loader->get_next_question_id($cat->id, 0));
77 public function test_cloze_subquestions_not_returned() {
78 $this->resetAfterTest();
79 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
81 $cat = $generator->create_question_category();
82 $question1 = $generator->create_question('multianswer', null, array('category' => $cat->id));
83 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
85 $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
86 $this->assertNull($loader->get_next_question_id($cat->id, 0));
89 public function test_random_questions_not_returned() {
90 $this->resetAfterTest();
91 $this->setAdminUser();
92 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
94 $cat = $generator->create_question_category();
95 $course = $this->getDataGenerator()->create_course();
96 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course));
97 quiz_add_random_questions($quiz, 1, $cat->id, 1, false);
98 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
100 $this->assertNull($loader->get_next_question_id($cat->id, 0));
103 public function test_one_question_category_returns_that_q_then_null() {
104 $this->resetAfterTest();
105 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
107 $cat = $generator->create_question_category();
108 $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
109 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
111 $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 1));
112 $this->assertNull($loader->get_next_question_id($cat->id, 0));
115 public function test_two_question_category_returns_both_then_null() {
116 $this->resetAfterTest();
117 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
119 $cat = $generator->create_question_category();
120 $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
121 $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
122 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
124 $questionids = array();
125 $questionids[] = $loader->get_next_question_id($cat->id, 0);
126 $questionids[] = $loader->get_next_question_id($cat->id, 0);
127 sort($questionids);
128 $this->assertEquals(array($question1->id, $question2->id), $questionids);
130 $this->assertNull($loader->get_next_question_id($cat->id, 1));
133 public function test_nested_categories() {
134 $this->resetAfterTest();
135 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
137 $cat1 = $generator->create_question_category();
138 $cat2 = $generator->create_question_category(array('parent' => $cat1->id));
139 $question1 = $generator->create_question('shortanswer', null, array('category' => $cat1->id));
140 $question2 = $generator->create_question('shortanswer', null, array('category' => $cat2->id));
141 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
143 $this->assertEquals($question2->id, $loader->get_next_question_id($cat2->id, 1));
144 $this->assertEquals($question1->id, $loader->get_next_question_id($cat1->id, 1));
146 $this->assertNull($loader->get_next_question_id($cat1->id, 0));
149 public function test_used_question_not_returned_until_later() {
150 $this->resetAfterTest();
151 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
153 $cat = $generator->create_question_category();
154 $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
155 $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
156 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()),
157 array($question2->id => 2));
159 $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
160 $this->assertNull($loader->get_next_question_id($cat->id, 0));
163 public function test_previously_used_question_not_returned_until_later() {
164 $this->resetAfterTest();
165 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
167 $cat = $generator->create_question_category();
168 $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
169 $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
170 $quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
171 $quba->set_preferred_behaviour('deferredfeedback');
172 $question = question_bank::load_question($question2->id);
173 $quba->add_question($question);
174 $quba->add_question($question);
175 $quba->start_all_questions();
176 question_engine::save_questions_usage_by_activity($quba);
178 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array($quba->get_id())));
180 $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
181 $this->assertEquals($question2->id, $loader->get_next_question_id($cat->id, 0));
182 $this->assertNull($loader->get_next_question_id($cat->id, 0));
185 public function test_empty_category_does_not_have_question_available() {
186 $this->resetAfterTest();
187 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
189 $cat = $generator->create_question_category();
190 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
192 $this->assertFalse($loader->is_question_available($cat->id, 0, 1));
193 $this->assertFalse($loader->is_question_available($cat->id, 1, 1));
196 public function test_descriptions_not_available() {
197 $this->resetAfterTest();
198 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
200 $cat = $generator->create_question_category();
201 $info = $generator->create_question('description', null, array('category' => $cat->id));
202 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
204 $this->assertFalse($loader->is_question_available($cat->id, 0, $info->id));
205 $this->assertFalse($loader->is_question_available($cat->id, 1, $info->id));
208 public function test_existing_question_is_available_but_then_marked_used() {
209 $this->resetAfterTest();
210 $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
212 $cat = $generator->create_question_category();
213 $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
214 $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
216 $this->assertTrue($loader->is_question_available($cat->id, 0, $question1->id));
217 $this->assertFalse($loader->is_question_available($cat->id, 0, $question1->id));
219 $this->assertFalse($loader->is_question_available($cat->id, 0, -1));