1441 |
ariadna |
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 core_question;
|
|
|
18 |
|
|
|
19 |
use context_course;
|
|
|
20 |
use context_module;
|
|
|
21 |
use moodle_url;
|
|
|
22 |
use core_question\local\bank\question_edit_contexts;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Unit tests for category_manager
|
|
|
26 |
*
|
|
|
27 |
* @package core_question
|
|
|
28 |
* @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
|
|
|
29 |
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @covers \core_question\category_manager
|
|
|
32 |
*/
|
|
|
33 |
final class category_manager_test extends \advanced_testcase {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Create a course and qbank module and return the module context for use in tests.
|
|
|
37 |
*
|
|
|
38 |
* @return \core\context\module|false
|
|
|
39 |
*/
|
|
|
40 |
private function create_course_and_get_qbank_context() {
|
|
|
41 |
$course = self::getDataGenerator()->create_course();
|
|
|
42 |
$qbank = self::getDataGenerator()->create_module('qbank', ['course' => $course->id]);
|
|
|
43 |
return context_module::instance($qbank->cmid);
|
|
|
44 |
}
|
|
|
45 |
/**
|
|
|
46 |
* Test creating a category.
|
|
|
47 |
*/
|
|
|
48 |
public function test_add_category_no_idnumber(): void {
|
|
|
49 |
global $DB;
|
|
|
50 |
|
|
|
51 |
$this->setAdminUser();
|
|
|
52 |
$this->resetAfterTest();
|
|
|
53 |
$context = $this->create_course_and_get_qbank_context();
|
|
|
54 |
$topcat = question_get_top_category($context->id, true);
|
|
|
55 |
$manager = new category_manager();
|
|
|
56 |
$id = $manager->add_category(
|
|
|
57 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
58 |
'New category',
|
|
|
59 |
'',
|
|
|
60 |
FORMAT_HTML,
|
|
|
61 |
'', // No idnumber passed as '' to match form data.
|
|
|
62 |
);
|
|
|
63 |
|
|
|
64 |
$newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
|
|
|
65 |
$this->assertSame('New category', $newcat->name);
|
|
|
66 |
$this->assertNull($newcat->idnumber);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Test creating a category with a tricky idnumber.
|
|
|
71 |
*/
|
|
|
72 |
public function test_add_category_set_idnumber_0(): void {
|
|
|
73 |
global $DB;
|
|
|
74 |
|
|
|
75 |
$this->setAdminUser();
|
|
|
76 |
$this->resetAfterTest();
|
|
|
77 |
$context = $this->create_course_and_get_qbank_context();
|
|
|
78 |
$topcat = question_get_top_category($context->id, true);
|
|
|
79 |
$manager = new category_manager();
|
|
|
80 |
$id = $manager->add_category(
|
|
|
81 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
82 |
'New category',
|
|
|
83 |
'',
|
|
|
84 |
FORMAT_HTML,
|
|
|
85 |
'0',
|
|
|
86 |
);
|
|
|
87 |
|
|
|
88 |
$newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
|
|
|
89 |
$this->assertSame('New category', $newcat->name);
|
|
|
90 |
$this->assertSame('0', $newcat->idnumber);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Trying to add a category with duplicate idnumber throws an exception.
|
|
|
95 |
*/
|
|
|
96 |
public function test_add_category_try_to_set_duplicate_idnumber(): void {
|
|
|
97 |
global $DB;
|
|
|
98 |
|
|
|
99 |
$this->setAdminUser();
|
|
|
100 |
$this->resetAfterTest();
|
|
|
101 |
$context = $this->create_course_and_get_qbank_context();
|
|
|
102 |
$topcat = question_get_top_category($context->id, true);
|
|
|
103 |
$manager = new category_manager();
|
|
|
104 |
$manager->add_category(
|
|
|
105 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
106 |
'Existing category',
|
|
|
107 |
'',
|
|
|
108 |
FORMAT_HTML,
|
|
|
109 |
'frog',
|
|
|
110 |
);
|
|
|
111 |
$this->expectExceptionMessage(get_string('idnumbertaken', 'error'));
|
|
|
112 |
$manager->add_category(
|
|
|
113 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
114 |
'New category',
|
|
|
115 |
'',
|
|
|
116 |
FORMAT_HTML,
|
|
|
117 |
'frog',
|
|
|
118 |
);
|
|
|
119 |
$this->assertFalse($DB->record_exists('question_categories', ['name' => 'New category']));
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Test updating a category.
|
|
|
124 |
*/
|
|
|
125 |
public function test_update_category(): void {
|
|
|
126 |
global $DB;
|
|
|
127 |
|
|
|
128 |
$this->setAdminUser();
|
|
|
129 |
$this->resetAfterTest();
|
|
|
130 |
$context = $this->create_course_and_get_qbank_context();
|
|
|
131 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
132 |
$topcat = question_get_top_category($context->id, true);
|
|
|
133 |
$category = $questiongenerator->create_question_category([
|
|
|
134 |
'contextid' => $context->id,
|
|
|
135 |
'name' => 'Old name',
|
|
|
136 |
'info' => 'Description',
|
|
|
137 |
'idnumber' => 'frog',
|
|
|
138 |
]);
|
|
|
139 |
$existingcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
140 |
$this->assertSame('Old name', $existingcat->name);
|
|
|
141 |
$this->assertSame('Description', $existingcat->info);
|
|
|
142 |
$this->assertSame('frog', $existingcat->idnumber);
|
|
|
143 |
|
|
|
144 |
$manager = new category_manager(new moodle_url('/'));
|
|
|
145 |
$manager->update_category(
|
|
|
146 |
$category->id,
|
|
|
147 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
148 |
'New name',
|
|
|
149 |
'New description',
|
|
|
150 |
FORMAT_HTML,
|
|
|
151 |
'0'
|
|
|
152 |
);
|
|
|
153 |
|
|
|
154 |
$updatedcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
155 |
$this->assertSame('New name', $updatedcat->name);
|
|
|
156 |
$this->assertSame('New description', $updatedcat->info);
|
|
|
157 |
$this->assertSame('0', $updatedcat->idnumber);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Test updating a category to remove the idnumber.
|
|
|
162 |
*/
|
|
|
163 |
public function test_update_category_removing_idnumber(): void {
|
|
|
164 |
global $DB;
|
|
|
165 |
|
|
|
166 |
$this->setAdminUser();
|
|
|
167 |
$this->resetAfterTest();
|
|
|
168 |
$context = $this->create_course_and_get_qbank_context();
|
|
|
169 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
170 |
$topcat = question_get_top_category($context->id, true);
|
|
|
171 |
$category = $questiongenerator->create_question_category([
|
|
|
172 |
'contextid' => $context->id,
|
|
|
173 |
'name' => 'Old name',
|
|
|
174 |
'info' => 'Description',
|
|
|
175 |
'idnumber' => 'frog',
|
|
|
176 |
]);
|
|
|
177 |
|
|
|
178 |
$existingcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
179 |
$this->assertSame('Old name', $existingcat->name);
|
|
|
180 |
$this->assertSame('Description', $existingcat->info);
|
|
|
181 |
$this->assertSame('frog', $existingcat->idnumber);
|
|
|
182 |
|
|
|
183 |
$manager = new category_manager(new moodle_url('/'));
|
|
|
184 |
$manager->update_category(
|
|
|
185 |
$category->id,
|
|
|
186 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
187 |
'New name',
|
|
|
188 |
'New description',
|
|
|
189 |
FORMAT_HTML,
|
|
|
190 |
''
|
|
|
191 |
);
|
|
|
192 |
|
|
|
193 |
$updatedcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
194 |
$this->assertSame('New name', $updatedcat->name);
|
|
|
195 |
$this->assertSame('New description', $updatedcat->info);
|
|
|
196 |
$this->assertNull($updatedcat->idnumber);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* Test updating a category without changing the idnumber.
|
|
|
201 |
*/
|
|
|
202 |
public function test_update_category_dont_change_idnumber(): void {
|
|
|
203 |
global $DB;
|
|
|
204 |
|
|
|
205 |
$this->setAdminUser();
|
|
|
206 |
$this->resetAfterTest();
|
|
|
207 |
$context = $this->create_course_and_get_qbank_context();
|
|
|
208 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
209 |
$topcat = question_get_top_category($context->id, true);
|
|
|
210 |
$category = $questiongenerator->create_question_category([
|
|
|
211 |
'contextid' => $context->id,
|
|
|
212 |
'name' => 'Old name',
|
|
|
213 |
'info' => 'Description',
|
|
|
214 |
'idnumber' => 'frog',
|
|
|
215 |
]);
|
|
|
216 |
$existingcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
217 |
$this->assertSame('Old name', $existingcat->name);
|
|
|
218 |
$this->assertSame('Description', $existingcat->info);
|
|
|
219 |
$this->assertSame('frog', $existingcat->idnumber);
|
|
|
220 |
|
|
|
221 |
$manager = new category_manager(new moodle_url('/'));
|
|
|
222 |
$manager->update_category(
|
|
|
223 |
$category->id,
|
|
|
224 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
225 |
'New name',
|
|
|
226 |
'New description',
|
|
|
227 |
FORMAT_HTML,
|
|
|
228 |
'frog'
|
|
|
229 |
);
|
|
|
230 |
|
|
|
231 |
$updatedcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
232 |
$this->assertSame('New name', $updatedcat->name);
|
|
|
233 |
$this->assertSame('New description', $updatedcat->info);
|
|
|
234 |
$this->assertSame('frog', $updatedcat->idnumber);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Trying to update a category so its idnumber is a duplicate throws an exception and does not update.
|
|
|
239 |
*/
|
|
|
240 |
public function test_update_category_try_to_set_duplicate_idnumber(): void {
|
|
|
241 |
global $DB;
|
|
|
242 |
|
|
|
243 |
$this->setAdminUser();
|
|
|
244 |
$this->resetAfterTest();
|
|
|
245 |
$context = $this->create_course_and_get_qbank_context();
|
|
|
246 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
247 |
$topcat = question_get_top_category($context->id, true);
|
|
|
248 |
$questiongenerator->create_question_category([
|
|
|
249 |
'contextid' => $context->id,
|
|
|
250 |
'name' => 'Toad category',
|
|
|
251 |
'idnumber' => 'toad',
|
|
|
252 |
]);
|
|
|
253 |
$category = $questiongenerator->create_question_category([
|
|
|
254 |
'contextid' => $context->id,
|
|
|
255 |
'name' => 'Frog category',
|
|
|
256 |
'idnumber' => 'frog',
|
|
|
257 |
]);
|
|
|
258 |
$existingcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
259 |
$this->assertSame('Frog category', $existingcat->name);
|
|
|
260 |
$this->assertSame('frog', $existingcat->idnumber);
|
|
|
261 |
|
|
|
262 |
$manager = new category_manager();
|
|
|
263 |
$this->expectExceptionMessage(get_string('idnumbertaken', 'error'));
|
|
|
264 |
$manager->update_category(
|
|
|
265 |
$category->id,
|
|
|
266 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
267 |
'New name',
|
|
|
268 |
'',
|
|
|
269 |
FORMAT_HTML,
|
|
|
270 |
'toad'
|
|
|
271 |
);
|
|
|
272 |
|
|
|
273 |
$updatedcat = $DB->get_record('question_categories', ['id' => $category->id], '*', MUST_EXIST);
|
|
|
274 |
$this->assertEquals('Frog category', $updatedcat->name);
|
|
|
275 |
$this->assertEquals('frog', $updatedcat->idnumber);
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Test the question category created event.
|
|
|
280 |
*/
|
|
|
281 |
public function test_question_category_created(): void {
|
|
|
282 |
$this->setAdminUser();
|
|
|
283 |
$this->resetAfterTest();
|
|
|
284 |
$generator = $this->getDataGenerator();
|
|
|
285 |
$course = $generator->create_course();
|
|
|
286 |
$quiz = $generator->get_plugin_generator('mod_quiz')->create_instance(['course' => $course->id]);
|
|
|
287 |
$context = context_module::instance($quiz->cmid);
|
|
|
288 |
$topcat = question_get_top_category($context->id, true);
|
|
|
289 |
$manager = new category_manager(new moodle_url('/'));
|
|
|
290 |
// Trigger and capture the event.
|
|
|
291 |
$sink = $this->redirectEvents();
|
|
|
292 |
$manager->add_category(
|
|
|
293 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
294 |
'New category',
|
|
|
295 |
'Description',
|
|
|
296 |
FORMAT_HTML,
|
|
|
297 |
'frog',
|
|
|
298 |
);
|
|
|
299 |
$events = $sink->get_events();
|
|
|
300 |
$event = reset($events);
|
|
|
301 |
|
|
|
302 |
// Check that the event data is valid.
|
|
|
303 |
$this->assertInstanceOf('\core\event\question_category_created', $event);
|
|
|
304 |
$this->assertEquals($context, $event->get_context());
|
|
|
305 |
$this->assertEventContextNotUsed($event);
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* Test the question category deleted event.
|
|
|
310 |
*/
|
|
|
311 |
public function test_question_category_deleted(): void {
|
|
|
312 |
$this->setAdminUser();
|
|
|
313 |
$this->resetAfterTest();
|
|
|
314 |
$generator = $this->getDataGenerator();
|
|
|
315 |
$course = $generator->create_course();
|
|
|
316 |
$quiz = $generator->get_plugin_generator('mod_quiz')->create_instance(['course' => $course->id]);
|
|
|
317 |
$contexts = new question_edit_contexts(context_module::instance($quiz->cmid));
|
|
|
318 |
$defaultcat = question_get_default_category($contexts->lowest()->id, true);
|
|
|
319 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
320 |
// Create the category.
|
|
|
321 |
$category = $questiongenerator->create_question_category([
|
|
|
322 |
'contextid' => $contexts->lowest()->id,
|
|
|
323 |
'name' => 'New category',
|
|
|
324 |
'info' => 'Description',
|
|
|
325 |
'idnumber' => 'newcategory',
|
|
|
326 |
'parent' => $defaultcat->id,
|
|
|
327 |
]);
|
|
|
328 |
|
|
|
329 |
// Trigger and capture the event.
|
|
|
330 |
$sink = $this->redirectEvents();
|
|
|
331 |
$manager = new category_manager(new moodle_url('/'));
|
|
|
332 |
$manager->delete_category($category->id);
|
|
|
333 |
$events = $sink->get_events();
|
|
|
334 |
$event = reset($events);
|
|
|
335 |
|
|
|
336 |
// Check that the event data is valid.
|
|
|
337 |
$this->assertInstanceOf('\core\event\question_category_deleted', $event);
|
|
|
338 |
$this->assertEquals($contexts->lowest(), $event->get_context());
|
|
|
339 |
$this->assertEquals($category->id, $event->objectid);
|
|
|
340 |
$this->assertDebuggingNotCalled();
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Test the question category updated event.
|
|
|
345 |
*/
|
|
|
346 |
public function test_question_category_updated(): void {
|
|
|
347 |
$this->setAdminUser();
|
|
|
348 |
$this->resetAfterTest();
|
|
|
349 |
$generator = $this->getDataGenerator();
|
|
|
350 |
$course = $generator->create_course();
|
|
|
351 |
$quiz = $generator->get_plugin_generator('mod_quiz')->create_instance(['course' => $course->id]);
|
|
|
352 |
$context = context_module::instance($quiz->cmid);
|
|
|
353 |
$topcat = question_get_top_category($context->id, true);
|
|
|
354 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
355 |
// Create the category.
|
|
|
356 |
$category = $questiongenerator->create_question_category([
|
|
|
357 |
'contextid' => $context->id,
|
|
|
358 |
'name' => 'New category',
|
|
|
359 |
'info' => 'Description',
|
|
|
360 |
'idnumber' => 'newcategory',
|
|
|
361 |
]);
|
|
|
362 |
|
|
|
363 |
// Trigger and capture the event.
|
|
|
364 |
$sink = $this->redirectEvents();
|
|
|
365 |
$manager = new category_manager(new moodle_url('/'));
|
|
|
366 |
$manager->update_category(
|
|
|
367 |
$category->id,
|
|
|
368 |
$topcat->id . ',' . $topcat->contextid,
|
|
|
369 |
'Updated category',
|
|
|
370 |
'',
|
|
|
371 |
true,
|
|
|
372 |
FORMAT_HTML,
|
|
|
373 |
);
|
|
|
374 |
$events = $sink->get_events();
|
|
|
375 |
$event = reset($events);
|
|
|
376 |
|
|
|
377 |
// Check that the event data is valid.
|
|
|
378 |
$this->assertInstanceOf('\core\event\question_category_updated', $event);
|
|
|
379 |
$this->assertEquals($context, $event->get_context());
|
|
|
380 |
$this->assertEquals($category->id, $event->objectid);
|
|
|
381 |
$this->assertDebuggingNotCalled();
|
|
|
382 |
}
|
|
|
383 |
|
|
|
384 |
/**
|
|
|
385 |
* Test that get_real_question_ids_in_category() returns question id
|
|
|
386 |
* of a shortanswer question in a category.
|
|
|
387 |
*/
|
|
|
388 |
public function test_get_real_question_ids_in_category_shortanswer(): void {
|
|
|
389 |
$this->resetAfterTest();
|
|
|
390 |
$generator = $this->getDataGenerator();
|
|
|
391 |
$course = $generator->create_course();
|
|
|
392 |
$quiz = $generator->create_module('quiz', ['course' => $course->id]);
|
|
|
393 |
$contexts = new question_edit_contexts(context_module::instance($quiz->cmid));
|
|
|
394 |
|
|
|
395 |
$defaultcategory = question_get_default_category($contexts->lowest()->id, true);
|
|
|
396 |
$questiongerator = $generator->get_plugin_generator('core_question');
|
|
|
397 |
|
|
|
398 |
// Short answer question is made of one question.
|
|
|
399 |
$shortanswer = $questiongerator->create_question('shortanswer', null, ['category' => $defaultcategory->id]);
|
|
|
400 |
$manager = new category_manager();
|
|
|
401 |
$questionids = $manager->get_real_question_ids_in_category($defaultcategory->id);
|
|
|
402 |
$this->assertCount(1, $questionids);
|
|
|
403 |
$this->assertContains($shortanswer->id, $questionids);
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
/**
|
|
|
407 |
* Test that get_real_question_ids_in_category() returns question id
|
|
|
408 |
* of a multianswer question in a category.
|
|
|
409 |
*/
|
|
|
410 |
public function test_get_real_question_ids_in_category_multianswer(): void {
|
|
|
411 |
global $DB;
|
|
|
412 |
$this->resetAfterTest();
|
|
|
413 |
$countq = $DB->count_records('question');
|
|
|
414 |
$countqbe = $DB->count_records('question_bank_entries');
|
|
|
415 |
$generator = $this->getDataGenerator();
|
|
|
416 |
$course = $generator->create_course();
|
|
|
417 |
$quiz = $generator->create_module('quiz', ['course' => $course->id]);
|
|
|
418 |
$contexts = new question_edit_contexts(context_module::instance($quiz->cmid));
|
|
|
419 |
|
|
|
420 |
$defaultcategory = question_get_default_category($contexts->lowest()->id, true);
|
|
|
421 |
$questiongerator = $generator->get_plugin_generator('core_question');
|
|
|
422 |
|
|
|
423 |
// Multi answer question is made of one parent and two child questions.
|
|
|
424 |
$multianswer = $questiongerator->create_question('multianswer', null, ['category' => $defaultcategory->id]);
|
|
|
425 |
$manager = new category_manager();
|
|
|
426 |
$questionids = $manager->get_real_question_ids_in_category($defaultcategory->id);
|
|
|
427 |
$this->assertCount(1, $questionids);
|
|
|
428 |
$this->assertContains($multianswer->id, $questionids);
|
|
|
429 |
$this->assertEquals(3, $DB->count_records('question') - $countq);
|
|
|
430 |
$this->assertEquals(3, $DB->count_records('question_bank_entries') - $countqbe);
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
/**
|
|
|
434 |
* Test that get_real_question_ids_in_category() returns question ids
|
|
|
435 |
* of two versions of a multianswer question in a category.
|
|
|
436 |
*/
|
|
|
437 |
public function test_get_real_question_ids_in_category_multianswer_two_versions(): void {
|
|
|
438 |
global $DB;
|
|
|
439 |
$this->resetAfterTest();
|
|
|
440 |
$countq = $DB->count_records('question');
|
|
|
441 |
$countqv = $DB->count_records('question_versions');
|
|
|
442 |
$countqbe = $DB->count_records('question_bank_entries');
|
|
|
443 |
|
|
|
444 |
$generator = $this->getDataGenerator();
|
|
|
445 |
$course = $generator->create_course();
|
|
|
446 |
$quiz = $generator->create_module('quiz', ['course' => $course->id]);
|
|
|
447 |
$contexts = new question_edit_contexts(context_module::instance($quiz->cmid));
|
|
|
448 |
|
|
|
449 |
$defaultcategory = question_get_default_category($contexts->lowest()->id, true);
|
|
|
450 |
$questiongerator = $generator->get_plugin_generator('core_question');
|
|
|
451 |
|
|
|
452 |
// Create two versions of a multianswer question which will lead to
|
|
|
453 |
// 2 parents and 4 child questions in the question bank.
|
|
|
454 |
$multianswer = $questiongerator->create_question('multianswer', null, ['category' => $defaultcategory->id]);
|
|
|
455 |
$multianswernew = $questiongerator->update_question($multianswer, null, ['name' => 'This is a new version']);
|
|
|
456 |
$manager = new category_manager();
|
|
|
457 |
$questionids = $manager->get_real_question_ids_in_category($defaultcategory->id);
|
|
|
458 |
$this->assertCount(2, $questionids);
|
|
|
459 |
$this->assertContains($multianswer->id, $questionids);
|
|
|
460 |
$this->assertContains($multianswernew->id, $questionids);
|
|
|
461 |
$this->assertEquals(6, $DB->count_records('question') - $countq);
|
|
|
462 |
$this->assertEquals(6, $DB->count_records('question_versions') - $countqv);
|
|
|
463 |
$this->assertEquals(3, $DB->count_records('question_bank_entries') - $countqbe);
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
/**
|
|
|
467 |
* Test that get_real_question_ids_in_category() returns question id
|
|
|
468 |
* of a multianswer question in a category even if their child questions are
|
|
|
469 |
* linked to a category that doesn't exist.
|
|
|
470 |
*/
|
|
|
471 |
public function test_get_real_question_ids_in_category_multianswer_bad_data(): void {
|
|
|
472 |
global $DB;
|
|
|
473 |
$this->resetAfterTest();
|
|
|
474 |
$countqbe = $DB->count_records('question_bank_entries');
|
|
|
475 |
|
|
|
476 |
$generator = $this->getDataGenerator();
|
|
|
477 |
$course = $generator->create_course();
|
|
|
478 |
$quiz = $generator->create_module('quiz', ['course' => $course->id]);
|
|
|
479 |
$contexts = new question_edit_contexts(context_module::instance($quiz->cmid));
|
|
|
480 |
|
|
|
481 |
$defaultcategory = question_get_default_category($contexts->lowest()->id, true);
|
|
|
482 |
$questiongerator = $generator->get_plugin_generator('core_question');
|
|
|
483 |
|
|
|
484 |
// Multi answer question is made of one parent and two child questions.
|
|
|
485 |
$multianswer = $questiongerator->create_question('multianswer', null, ['category' => $defaultcategory->id]);
|
|
|
486 |
$qversion = $DB->get_record('question_versions', ['questionid' => $multianswer->id]);
|
|
|
487 |
|
|
|
488 |
// Update category id for child questions to a category that doesn't exist.
|
|
|
489 |
$DB->set_field_select(
|
|
|
490 |
'question_bank_entries',
|
|
|
491 |
'questioncategoryid',
|
|
|
492 |
123456,
|
|
|
493 |
'id <> :id',
|
|
|
494 |
['id' => $qversion->questionbankentryid]
|
|
|
495 |
);
|
|
|
496 |
|
|
|
497 |
$manager = new category_manager();
|
|
|
498 |
$questionids = $manager->get_real_question_ids_in_category($defaultcategory->id);
|
|
|
499 |
$this->assertCount(1, $questionids);
|
|
|
500 |
$this->assertContains($multianswer->id, $questionids);
|
|
|
501 |
$this->assertEquals(3, $DB->count_records('question_bank_entries') - $countqbe);
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
/**
|
|
|
505 |
* Test delete top category in function question_can_delete_cat.
|
|
|
506 |
*/
|
|
|
507 |
public function test_question_can_delete_cat_top_category(): void {
|
|
|
508 |
$this->setAdminUser();
|
|
|
509 |
$this->resetAfterTest();
|
|
|
510 |
|
|
|
511 |
$manager = new category_manager();
|
|
|
512 |
|
|
|
513 |
// Create a category.
|
|
|
514 |
$course = $this->getDataGenerator()->create_course();
|
|
|
515 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
516 |
$context = \context_module::instance($quiz->cmid);
|
|
|
517 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
518 |
$qcategory1 = $questiongenerator->create_question_category(['contextid' => $context->id]);
|
|
|
519 |
|
|
|
520 |
// Try to delete a top category.
|
|
|
521 |
$categorytop = question_get_top_category($qcategory1->contextid, true)->id;
|
|
|
522 |
$this->expectException('moodle_exception');
|
|
|
523 |
$this->expectExceptionMessage(get_string('cannotdeletetopcat', 'question'));
|
|
|
524 |
$manager->require_can_delete_category($categorytop);
|
|
|
525 |
}
|
|
|
526 |
|
|
|
527 |
/**
|
|
|
528 |
* Test delete only child category in function question_can_delete_cat.
|
|
|
529 |
*/
|
|
|
530 |
public function test_question_can_delete_cat_child_category(): void {
|
|
|
531 |
$this->setAdminUser();
|
|
|
532 |
$this->resetAfterTest();
|
|
|
533 |
|
|
|
534 |
$manager = new category_manager();
|
|
|
535 |
|
|
|
536 |
// Create a category.
|
|
|
537 |
$course = $this->getDataGenerator()->create_course();
|
|
|
538 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
539 |
$context = \context_module::instance($quiz->cmid);
|
|
|
540 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
541 |
$qcategory1 = question_get_default_category($context->id);
|
|
|
542 |
|
|
|
543 |
// Try to delete an only child of top category having also at least one child.
|
|
|
544 |
$this->expectException('moodle_exception');
|
|
|
545 |
$this->expectExceptionMessage(get_string('cannotdeletecate', 'question'));
|
|
|
546 |
$manager->require_can_delete_category($qcategory1->id);
|
|
|
547 |
}
|
|
|
548 |
|
|
|
549 |
/**
|
|
|
550 |
* Test delete category in function question_can_delete_cat without capabilities.
|
|
|
551 |
*/
|
|
|
552 |
public function test_can_delete_category_capability(): void {
|
|
|
553 |
$this->setAdminUser();
|
|
|
554 |
$this->resetAfterTest();
|
|
|
555 |
|
|
|
556 |
$manager = new category_manager();
|
|
|
557 |
|
|
|
558 |
// Create 2 categories.
|
|
|
559 |
$course = $this->getDataGenerator()->create_course();
|
|
|
560 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
561 |
$context = \context_module::instance($quiz->cmid);
|
|
|
562 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
563 |
$qcategory1 = $questiongenerator->create_question_category(['contextid' => $context->id]);
|
|
|
564 |
$qcategory2 = $questiongenerator->create_question_category(['contextid' => $context->id, 'parent' => $qcategory1->id]);
|
|
|
565 |
|
|
|
566 |
// This call should not throw an exception as admin user has the capabilities moodle/question:managecategory.
|
|
|
567 |
$manager->require_can_delete_category($qcategory2->id);
|
|
|
568 |
|
|
|
569 |
// Try to delete a category with and user without the capability.
|
|
|
570 |
$manager = new category_manager();
|
|
|
571 |
$user = $this->getDataGenerator()->create_user();
|
|
|
572 |
$this->setUser($user);
|
|
|
573 |
|
|
|
574 |
$this->expectException(\required_capability_exception::class);
|
|
|
575 |
$this->expectExceptionMessage(get_string('nopermissions', 'error', get_string('question:managecategory', 'role')));
|
|
|
576 |
$manager->require_can_delete_category($qcategory2->id);
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
/**
|
|
|
580 |
* Test get max sortorder
|
|
|
581 |
*/
|
|
|
582 |
public function test_get_max_sortorder(): void {
|
|
|
583 |
$this->setAdminUser();
|
|
|
584 |
$this->resetAfterTest();
|
|
|
585 |
|
|
|
586 |
$manager = new category_manager();
|
|
|
587 |
|
|
|
588 |
// Create question categories for a course.
|
|
|
589 |
$course = $this->getDataGenerator()->create_course();
|
|
|
590 |
$quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
591 |
$context = \context_module::instance($quiz->cmid);
|
|
|
592 |
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
|
|
|
593 |
$topcategory = question_get_top_category($context->id, true);
|
|
|
594 |
$qcategory1 = question_get_default_category($context->id);
|
|
|
595 |
$this->assertEquals(999, $manager->get_max_sortorder($topcategory->id));
|
|
|
596 |
|
|
|
597 |
$qcategory2 = $questiongenerator->create_question_category(['contextid' => $context->id, 'parent' => $qcategory1->id]);
|
|
|
598 |
|
|
|
599 |
$this->assertEquals(1, $manager->get_max_sortorder($qcategory1->id));
|
|
|
600 |
|
|
|
601 |
$questiongenerator->create_question_category(['contextid' => $context->id]);
|
|
|
602 |
$this->assertEquals(1000, $manager->get_max_sortorder($topcategory->id));
|
|
|
603 |
|
|
|
604 |
$this->assertEquals(0, $manager->get_max_sortorder($qcategory2->id));
|
|
|
605 |
$questiongenerator->create_question_category(['contextid' => $context->id, 'parent' => $qcategory2->id]);
|
|
|
606 |
$this->assertEquals(1, $manager->get_max_sortorder($qcategory2->id));
|
|
|
607 |
}
|
|
|
608 |
}
|