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