1 |
efrain |
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 |
namespace mod_quiz;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
use mod_quiz\question\bank\qbank_helper;
|
|
|
22 |
|
|
|
23 |
global $CFG;
|
|
|
24 |
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
|
|
|
25 |
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Class mod_quiz_local_structure_slot_random_test
|
|
|
29 |
* Class for tests related to the {@link \mod_quiz\local\structure\slot_random} class.
|
|
|
30 |
*
|
|
|
31 |
* @package mod_quiz
|
|
|
32 |
* @category test
|
|
|
33 |
* @copyright 2018 Shamim Rezaie <shamim@moodle.com>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
* @covers \mod_quiz\local\structure\slot_random
|
|
|
36 |
*/
|
|
|
37 |
class local_structure_slot_random_test extends \advanced_testcase {
|
|
|
38 |
|
|
|
39 |
use \quiz_question_helper_test_trait;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Constructor test.
|
|
|
43 |
*/
|
11 |
efrain |
44 |
public function test_constructor(): void {
|
1 |
efrain |
45 |
global $SITE;
|
|
|
46 |
|
|
|
47 |
$this->resetAfterTest();
|
|
|
48 |
$this->setAdminUser();
|
|
|
49 |
|
|
|
50 |
// Create a quiz.
|
|
|
51 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
52 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0]);
|
|
|
53 |
|
|
|
54 |
// Create a question category in the system context.
|
|
|
55 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
56 |
$category = $questiongenerator->create_question_category();
|
|
|
57 |
|
|
|
58 |
// Create a random question without adding it to a quiz.
|
|
|
59 |
// We don't want to use quiz_add_random_questions because that itself, instantiates an object from the slot_random class.
|
|
|
60 |
$form = new \stdClass();
|
|
|
61 |
$form->category = $category->id . ',' . $category->contextid;
|
|
|
62 |
$form->includesubcategories = true;
|
|
|
63 |
$form->fromtags = [];
|
|
|
64 |
$form->defaultmark = 1;
|
|
|
65 |
$form->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN;
|
|
|
66 |
$form->stamp = make_unique_id_code();
|
|
|
67 |
|
|
|
68 |
// Set the filter conditions.
|
|
|
69 |
$filtercondition = new \stdClass();
|
|
|
70 |
$filtercondition->filters = \question_filter_test_helper::create_filters([$category->id], true);
|
|
|
71 |
|
|
|
72 |
// Slot data.
|
|
|
73 |
$randomslotdata = new \stdClass();
|
|
|
74 |
$randomslotdata->quizid = $quiz->id;
|
|
|
75 |
$randomslotdata->maxmark = 1;
|
|
|
76 |
$randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
|
|
|
77 |
$randomslotdata->questionscontextid = $category->contextid;
|
|
|
78 |
|
|
|
79 |
// Insert the random question to the quiz.
|
|
|
80 |
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
|
|
|
81 |
$randomslot->set_filter_condition(json_encode($filtercondition));
|
|
|
82 |
|
|
|
83 |
$rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
|
|
|
84 |
$rcp = $rc->getProperty('filtercondition');
|
|
|
85 |
$record = json_decode($rcp->getValue($randomslot));
|
|
|
86 |
|
|
|
87 |
$this->assertEquals($quiz->id, $randomslot->get_quiz()->id);
|
|
|
88 |
$this->assertEquals($category->id, $record->filters->category->values[0]);
|
|
|
89 |
$this->assertTrue($record->filters->category->filteroptions->includesubcategories);
|
|
|
90 |
|
|
|
91 |
$rcp = $rc->getProperty('record');
|
|
|
92 |
$record = $rcp->getValue($randomslot);
|
|
|
93 |
$this->assertEquals(1, $record->maxmark);
|
|
|
94 |
}
|
|
|
95 |
|
11 |
efrain |
96 |
public function test_get_quiz_quiz(): void {
|
1 |
efrain |
97 |
global $SITE, $DB;
|
|
|
98 |
|
|
|
99 |
$this->resetAfterTest();
|
|
|
100 |
$this->setAdminUser();
|
|
|
101 |
|
|
|
102 |
// Create a quiz.
|
|
|
103 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
104 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0]);
|
|
|
105 |
|
|
|
106 |
// Create a question category in the system context.
|
|
|
107 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
108 |
$category = $questiongenerator->create_question_category();
|
|
|
109 |
|
|
|
110 |
$this->add_random_questions($quiz->id, 0, $category->id, 1);
|
|
|
111 |
|
|
|
112 |
// Set the filter conditions.
|
|
|
113 |
$filtercondition = new \stdClass();
|
|
|
114 |
$filtercondition->filters = \question_filter_test_helper::create_filters([$category->id], 1);
|
|
|
115 |
|
|
|
116 |
// Slot data.
|
|
|
117 |
$randomslotdata = new \stdClass();
|
|
|
118 |
$randomslotdata->quizid = $quiz->id;
|
|
|
119 |
$randomslotdata->maxmark = 1;
|
|
|
120 |
$randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
|
|
|
121 |
$randomslotdata->questionscontextid = $category->contextid;
|
|
|
122 |
|
|
|
123 |
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
|
|
|
124 |
$randomslot->set_filter_condition(json_encode($filtercondition));
|
|
|
125 |
|
|
|
126 |
// The create_instance had injected an additional cmid propery to the quiz. Let's remove that.
|
|
|
127 |
unset($quiz->cmid);
|
|
|
128 |
|
|
|
129 |
$this->assertEquals($quiz, $randomslot->get_quiz());
|
|
|
130 |
}
|
|
|
131 |
|
11 |
efrain |
132 |
public function test_set_quiz(): void {
|
1 |
efrain |
133 |
global $SITE, $DB;
|
|
|
134 |
|
|
|
135 |
$this->resetAfterTest();
|
|
|
136 |
$this->setAdminUser();
|
|
|
137 |
|
|
|
138 |
// Create a quiz.
|
|
|
139 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
140 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0]);
|
|
|
141 |
|
|
|
142 |
// Create a question category in the system context.
|
|
|
143 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
144 |
$category = $questiongenerator->create_question_category();
|
|
|
145 |
|
|
|
146 |
$this->add_random_questions($quiz->id, 0, $category->id, 1);
|
|
|
147 |
|
|
|
148 |
// Set the filter conditions.
|
|
|
149 |
$filtercondition = new \stdClass();
|
|
|
150 |
$filtercondition->filters = \question_filter_test_helper::create_filters([$category->id], 1);
|
|
|
151 |
|
|
|
152 |
// Slot data.
|
|
|
153 |
$randomslotdata = new \stdClass();
|
|
|
154 |
$randomslotdata->quizid = $quiz->id;
|
|
|
155 |
$randomslotdata->maxmark = 1;
|
|
|
156 |
$randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
|
|
|
157 |
$randomslotdata->questionscontextid = $category->contextid;
|
|
|
158 |
|
|
|
159 |
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
|
|
|
160 |
$randomslot->set_filter_condition(json_encode($filtercondition));
|
|
|
161 |
|
|
|
162 |
// The create_instance had injected an additional cmid propery to the quiz. Let's remove that.
|
|
|
163 |
unset($quiz->cmid);
|
|
|
164 |
|
|
|
165 |
$randomslot->set_quiz($quiz);
|
|
|
166 |
|
|
|
167 |
$rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
|
|
|
168 |
$rcp = $rc->getProperty('quiz');
|
|
|
169 |
$quizpropery = $rcp->getValue($randomslot);
|
|
|
170 |
|
|
|
171 |
$this->assertEquals($quiz, $quizpropery);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
private function setup_for_test_tags($tagnames) {
|
|
|
175 |
global $SITE, $DB;
|
|
|
176 |
|
|
|
177 |
// Create a quiz.
|
|
|
178 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
179 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0]);
|
|
|
180 |
|
|
|
181 |
// Create a question category in the system context.
|
|
|
182 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
183 |
$category = $questiongenerator->create_question_category();
|
|
|
184 |
|
|
|
185 |
$this->add_random_questions($quiz->id, 0, $category->id, 1);
|
|
|
186 |
|
|
|
187 |
// Slot data.
|
|
|
188 |
$randomslotdata = new \stdClass();
|
|
|
189 |
$randomslotdata->quizid = $quiz->id;
|
|
|
190 |
$randomslotdata->maxmark = 1;
|
|
|
191 |
$randomslotdata->usingcontextid = \context_module::instance($quiz->cmid)->id;
|
|
|
192 |
$randomslotdata->questionscontextid = $category->contextid;
|
|
|
193 |
|
|
|
194 |
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
|
|
|
195 |
|
|
|
196 |
// Create tags.
|
|
|
197 |
foreach ($tagnames as $tagname) {
|
|
|
198 |
$tagrecord = [
|
|
|
199 |
'isstandard' => 1,
|
|
|
200 |
'flag' => 0,
|
|
|
201 |
'rawname' => $tagname,
|
|
|
202 |
'description' => $tagname . ' desc'
|
|
|
203 |
];
|
|
|
204 |
$tags[$tagname] = $this->getDataGenerator()->create_tag($tagrecord);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
return [$randomslot, $tags];
|
|
|
208 |
}
|
|
|
209 |
|
11 |
efrain |
210 |
public function test_set_tags_filter(): void {
|
1 |
efrain |
211 |
$this->resetAfterTest();
|
|
|
212 |
$this->setAdminUser();
|
|
|
213 |
|
|
|
214 |
list($randomslot, $tags) = $this->setup_for_test_tags(['foo', 'bar']);
|
|
|
215 |
|
|
|
216 |
$qtagids = [$tags['foo']->id, $tags['bar']->id];
|
|
|
217 |
$filtercondition = new \stdClass();
|
|
|
218 |
$filtercondition->filters = \question_filter_test_helper::create_filters([], 0, $qtagids);
|
|
|
219 |
$randomslot->set_filter_condition(json_encode($filtercondition));
|
|
|
220 |
|
|
|
221 |
$rc = new \ReflectionClass('\mod_quiz\local\structure\slot_random');
|
|
|
222 |
$rcp = $rc->getProperty('filtercondition');
|
|
|
223 |
$tagspropery = $rcp->getValue($randomslot);
|
|
|
224 |
|
|
|
225 |
$this->assertEquals([$tags['foo']->id, $tags['bar']->id],
|
|
|
226 |
(array)json_decode($tagspropery)->filters->qtagids->values);
|
|
|
227 |
}
|
|
|
228 |
|
11 |
efrain |
229 |
public function test_insert(): void {
|
1 |
efrain |
230 |
global $SITE;
|
|
|
231 |
|
|
|
232 |
$this->resetAfterTest();
|
|
|
233 |
$this->setAdminUser();
|
|
|
234 |
|
|
|
235 |
// Create a quiz.
|
|
|
236 |
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
|
|
|
237 |
$quiz = $quizgenerator->create_instance(['course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0]);
|
|
|
238 |
$quizcontext = \context_module::instance($quiz->cmid);
|
|
|
239 |
|
|
|
240 |
// Create a question category in the system context.
|
|
|
241 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
242 |
$category = $questiongenerator->create_question_category();
|
|
|
243 |
|
|
|
244 |
// Create a random question without adding it to a quiz.
|
|
|
245 |
$form = new \stdClass();
|
|
|
246 |
$form->category = $category->id . ',' . $category->contextid;
|
|
|
247 |
$form->includesubcategories = true;
|
|
|
248 |
$form->fromtags = [];
|
|
|
249 |
$form->defaultmark = 1;
|
|
|
250 |
$form->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN;
|
|
|
251 |
$form->stamp = make_unique_id_code();
|
|
|
252 |
|
|
|
253 |
// Prepare 2 tags.
|
|
|
254 |
$tagrecord = [
|
|
|
255 |
'isstandard' => 1,
|
|
|
256 |
'flag' => 0,
|
|
|
257 |
'rawname' => 'foo',
|
|
|
258 |
'description' => 'foo desc'
|
|
|
259 |
];
|
|
|
260 |
$footag = $this->getDataGenerator()->create_tag($tagrecord);
|
|
|
261 |
$tagrecord = [
|
|
|
262 |
'isstandard' => 1,
|
|
|
263 |
'flag' => 0,
|
|
|
264 |
'rawname' => 'bar',
|
|
|
265 |
'description' => 'bar desc'
|
|
|
266 |
];
|
|
|
267 |
$bartag = $this->getDataGenerator()->create_tag($tagrecord);
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
// Set the filter conditions.
|
|
|
271 |
$filtercondition = new \stdClass();
|
|
|
272 |
$filtercondition->filter = \question_filter_test_helper::create_filters([$category->id], true, [$footag->id, $bartag->id]);
|
|
|
273 |
|
|
|
274 |
// Slot data.
|
|
|
275 |
$randomslotdata = new \stdClass();
|
|
|
276 |
$randomslotdata->quizid = $quiz->id;
|
|
|
277 |
$randomslotdata->maxmark = 1;
|
|
|
278 |
$randomslotdata->usingcontextid = $quizcontext->id;
|
|
|
279 |
$randomslotdata->questionscontextid = $category->contextid;
|
|
|
280 |
|
|
|
281 |
// Insert the random question to the quiz.
|
|
|
282 |
$randomslot = new \mod_quiz\local\structure\slot_random($randomslotdata);
|
|
|
283 |
$randomslot->set_filter_condition(json_encode($filtercondition));
|
|
|
284 |
$randomslot->insert(1); // Put the question on the first page of the quiz.
|
|
|
285 |
|
|
|
286 |
$slots = qbank_helper::get_question_structure($quiz->id, $quizcontext);
|
|
|
287 |
$quizslot = reset($slots);
|
|
|
288 |
|
|
|
289 |
$filter = $quizslot->filtercondition['filter'];
|
|
|
290 |
|
|
|
291 |
$this->assertEquals($category->id, $filter['category']['values'][0]);
|
|
|
292 |
$this->assertTrue($filter['category']['filteroptions']['includesubcategories']);
|
|
|
293 |
$this->assertEquals(1, $quizslot->maxmark);
|
|
|
294 |
|
|
|
295 |
$this->assertCount(2, $filter['qtagids']['values']);
|
|
|
296 |
$this->assertEqualsCanonicalizing(
|
|
|
297 |
[
|
|
|
298 |
['tagid' => $footag->id],
|
|
|
299 |
['tagid' => $bartag->id]
|
|
|
300 |
],
|
|
|
301 |
array_map(function($tagid) {
|
|
|
302 |
return ['tagid' => $tagid];
|
|
|
303 |
}, $filter['qtagids']['values']));
|
|
|
304 |
}
|
|
|
305 |
}
|