| 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 |
/**
|
|
|
18 |
* This file contains tests for the question_engine class.
|
|
|
19 |
*
|
|
|
20 |
* @package moodlecore
|
|
|
21 |
* @subpackage questionengine
|
|
|
22 |
* @copyright 2009 The Open University
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace core_question;
|
|
|
27 |
|
|
|
28 |
use advanced_testcase;
|
|
|
29 |
use moodle_exception;
|
|
|
30 |
use question_engine;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Unit tests for the question_engine class.
|
|
|
34 |
*
|
|
|
35 |
* @copyright 2009 The Open University
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
* @coversDefaultClass \question_engine
|
|
|
38 |
*/
|
| 1441 |
ariadna |
39 |
final class question_engine_test extends advanced_testcase {
|
| 1 |
efrain |
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Load required libraries.
|
|
|
43 |
*/
|
|
|
44 |
public static function setUpBeforeClass(): void {
|
|
|
45 |
global $CFG;
|
|
|
46 |
|
|
|
47 |
require_once("{$CFG->dirroot}/question/engine/lib.php");
|
| 1441 |
ariadna |
48 |
parent::setUpBeforeClass();
|
| 1 |
efrain |
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Tests for load_behaviour_class.
|
|
|
53 |
*
|
|
|
54 |
* @covers \question_engine::load_behaviour_class
|
|
|
55 |
*/
|
|
|
56 |
public function test_load_behaviour_class(): void {
|
|
|
57 |
// Exercise SUT.
|
|
|
58 |
question_engine::load_behaviour_class('deferredfeedback');
|
|
|
59 |
|
|
|
60 |
// Verify.
|
|
|
61 |
$this->assertTrue(class_exists('qbehaviour_deferredfeedback'));
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Tests for load_behaviour_class when a class is missing.
|
|
|
66 |
*
|
|
|
67 |
* @covers \question_engine::load_behaviour_class
|
|
|
68 |
*/
|
|
|
69 |
public function test_load_behaviour_class_missing(): void {
|
|
|
70 |
// Exercise SUT.
|
|
|
71 |
$this->expectException(moodle_exception::class);
|
|
|
72 |
question_engine::load_behaviour_class('nonexistantbehaviour');
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Test the get_behaviour_unused_display_options with various options.
|
|
|
77 |
*
|
|
|
78 |
* @covers \question_engine::get_behaviour_unused_display_options
|
|
|
79 |
* @dataProvider get_behaviour_unused_display_options_provider
|
|
|
80 |
* @param string $behaviour
|
|
|
81 |
* @param array $expected
|
|
|
82 |
*/
|
|
|
83 |
public function test_get_behaviour_unused_display_options(string $behaviour, array $expected): void {
|
|
|
84 |
$this->assertEquals($expected, question_engine::get_behaviour_unused_display_options($behaviour));
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Data provider for get_behaviour_unused_display_options.
|
|
|
89 |
*
|
|
|
90 |
* @return array
|
|
|
91 |
*/
|
| 1441 |
ariadna |
92 |
public static function get_behaviour_unused_display_options_provider(): array {
|
| 1 |
efrain |
93 |
return [
|
|
|
94 |
'interactive' => [
|
|
|
95 |
'interactive',
|
|
|
96 |
[],
|
|
|
97 |
],
|
|
|
98 |
'deferredfeedback' => [
|
|
|
99 |
'deferredfeedback',
|
|
|
100 |
['correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'],
|
|
|
101 |
],
|
|
|
102 |
'deferredcbm' => [
|
|
|
103 |
'deferredcbm',
|
|
|
104 |
['correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'],
|
|
|
105 |
],
|
|
|
106 |
'manualgraded' => [
|
|
|
107 |
'manualgraded',
|
|
|
108 |
['correctness', 'marks', 'specificfeedback', 'generalfeedback', 'rightanswer'],
|
|
|
109 |
],
|
|
|
110 |
];
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Tests for can_questions_finish_during_the_attempt.
|
|
|
115 |
*
|
|
|
116 |
* @covers \question_engine::can_questions_finish_during_the_attempt
|
|
|
117 |
* @dataProvider can_questions_finish_during_the_attempt_provider
|
|
|
118 |
* @param string $behaviour
|
|
|
119 |
* @param bool $expected
|
|
|
120 |
*/
|
|
|
121 |
public function test_can_questions_finish_during_the_attempt(string $behaviour, bool $expected): void {
|
|
|
122 |
$this->assertEquals($expected, question_engine::can_questions_finish_during_the_attempt($behaviour));
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Data provider for can_questions_finish_during_the_attempt_provider.
|
|
|
127 |
*
|
|
|
128 |
* @return array
|
|
|
129 |
*/
|
| 1441 |
ariadna |
130 |
public static function can_questions_finish_during_the_attempt_provider(): array {
|
| 1 |
efrain |
131 |
return [
|
|
|
132 |
['deferredfeedback', false],
|
|
|
133 |
['interactive', true],
|
|
|
134 |
];
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Tests for sort_behaviours
|
|
|
139 |
*
|
|
|
140 |
* @covers \question_engine::sort_behaviours
|
|
|
141 |
* @dataProvider sort_behaviours_provider
|
|
|
142 |
* @param array $input The params passed to sort_behaviours
|
|
|
143 |
* @param array $expected
|
|
|
144 |
*/
|
|
|
145 |
public function test_sort_behaviours(array $input, array $expected): void {
|
|
|
146 |
$this->assertSame($expected, question_engine::sort_behaviours(...$input));
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Data provider for sort_behaviours.
|
|
|
151 |
*
|
|
|
152 |
* @return array
|
|
|
153 |
*/
|
| 1441 |
ariadna |
154 |
public static function sort_behaviours_provider(): array {
|
| 1 |
efrain |
155 |
$in = [
|
|
|
156 |
'b1' => 'Behave 1',
|
|
|
157 |
'b2' => 'Behave 2',
|
|
|
158 |
'b3' => 'Behave 3',
|
|
|
159 |
'b4' => 'Behave 4',
|
|
|
160 |
'b5' => 'Behave 5',
|
|
|
161 |
'b6' => 'Behave 6',
|
|
|
162 |
];
|
|
|
163 |
|
|
|
164 |
return [
|
|
|
165 |
[
|
|
|
166 |
[$in, '', '', ''],
|
|
|
167 |
$in,
|
|
|
168 |
],
|
|
|
169 |
[
|
|
|
170 |
[$in, '', 'b4', 'b4'],
|
|
|
171 |
$in,
|
|
|
172 |
],
|
|
|
173 |
[
|
|
|
174 |
[$in, '', 'b1,b2,b3,b4', 'b4'],
|
|
|
175 |
['b4' => 'Behave 4', 'b5' => 'Behave 5', 'b6' => 'Behave 6'],
|
|
|
176 |
],
|
|
|
177 |
[
|
|
|
178 |
[$in, 'b6,b1,b4', 'b2,b3,b4,b5', 'b4'],
|
|
|
179 |
['b6' => 'Behave 6', 'b1' => 'Behave 1', 'b4' => 'Behave 4'],
|
|
|
180 |
],
|
|
|
181 |
[
|
|
|
182 |
[$in, 'b6,b5,b4', 'b1,b2,b3', 'b4'],
|
|
|
183 |
['b6' => 'Behave 6', 'b5' => 'Behave 5', 'b4' => 'Behave 4'],
|
|
|
184 |
],
|
|
|
185 |
[
|
|
|
186 |
[$in, 'b1,b6,b5', 'b1,b2,b3,b4', 'b4'],
|
|
|
187 |
['b6' => 'Behave 6', 'b5' => 'Behave 5', 'b4' => 'Behave 4'],
|
|
|
188 |
],
|
|
|
189 |
[
|
|
|
190 |
[$in, 'b2,b4,b6', 'b1,b3,b5', 'b2'],
|
|
|
191 |
['b2' => 'Behave 2', 'b4' => 'Behave 4', 'b6' => 'Behave 6'],
|
|
|
192 |
],
|
|
|
193 |
// Ignore unknown input in the order argument.
|
|
|
194 |
[
|
|
|
195 |
[$in, 'unknown', '', ''],
|
|
|
196 |
$in,
|
|
|
197 |
],
|
|
|
198 |
// Ignore unknown input in the disabled argument.
|
|
|
199 |
[
|
|
|
200 |
[$in, '', 'unknown', ''],
|
|
|
201 |
$in,
|
|
|
202 |
],
|
|
|
203 |
];
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Tests for is_manual_grade_in_range.
|
|
|
208 |
*
|
|
|
209 |
* @dataProvider is_manual_grade_in_range_provider
|
|
|
210 |
* @covers \question_engine::is_manual_grade_in_range
|
|
|
211 |
* @param array $post The values to add to $_POST
|
| 1441 |
ariadna |
212 |
* @param array $range The params to pass to is_manual_grade_in_range
|
| 1 |
efrain |
213 |
* @param bool $expected
|
|
|
214 |
*/
|
| 1441 |
ariadna |
215 |
public function test_is_manual_grade_in_range(array $post, array $range, bool $expected): void {
|
| 1 |
efrain |
216 |
$_POST[] = $post;
|
| 1441 |
ariadna |
217 |
$this->assertEquals($expected, question_engine::is_manual_grade_in_range(...$range));
|
| 1 |
efrain |
218 |
}
|
|
|
219 |
|
|
|
220 |
/**
|
|
|
221 |
* Data provider for is_manual_grade_in_range tests.
|
|
|
222 |
*
|
|
|
223 |
* @return array
|
|
|
224 |
*/
|
| 1441 |
ariadna |
225 |
public static function is_manual_grade_in_range_provider(): array {
|
| 1 |
efrain |
226 |
return [
|
|
|
227 |
'In range' => [
|
|
|
228 |
'post' => [
|
|
|
229 |
'q1:2_-mark' => 0.5,
|
|
|
230 |
'q1:2_-maxmark' => 1.0,
|
|
|
231 |
'q1:2_:minfraction' => 0,
|
|
|
232 |
'q1:2_:maxfraction' => 1,
|
|
|
233 |
],
|
|
|
234 |
'range' => [1, 2],
|
|
|
235 |
'expected' => true,
|
|
|
236 |
],
|
|
|
237 |
'Bottom end' => [
|
|
|
238 |
'post' => [
|
|
|
239 |
'q1:2_-mark' => -1.0,
|
|
|
240 |
'q1:2_-maxmark' => 2.0,
|
|
|
241 |
'q1:2_:minfraction' => -0.5,
|
|
|
242 |
'q1:2_:maxfraction' => 1,
|
|
|
243 |
],
|
|
|
244 |
'range' => [1, 2],
|
|
|
245 |
'expected' => true,
|
|
|
246 |
],
|
|
|
247 |
'Too low' => [
|
|
|
248 |
'post' => [
|
|
|
249 |
'q1:2_-mark' => -1.1,
|
|
|
250 |
'q1:2_-maxmark' => 2.0,
|
|
|
251 |
'q1:2_:minfraction' => -0.5,
|
|
|
252 |
'q1:2_:maxfraction' => 1,
|
|
|
253 |
],
|
|
|
254 |
'range' => [1, 2],
|
|
|
255 |
'expected' => true,
|
|
|
256 |
],
|
|
|
257 |
'Top end' => [
|
|
|
258 |
'post' => [
|
|
|
259 |
'q1:2_-mark' => 3.0,
|
|
|
260 |
'q1:2_-maxmark' => 1.0,
|
|
|
261 |
'q1:2_:minfraction' => -6.0,
|
|
|
262 |
'q1:2_:maxfraction' => 3.0,
|
|
|
263 |
],
|
|
|
264 |
'range' => [1, 2],
|
|
|
265 |
'expected' => true,
|
|
|
266 |
],
|
|
|
267 |
'Too high' => [
|
|
|
268 |
'post' => [
|
|
|
269 |
'q1:2_-mark' => 3.1,
|
|
|
270 |
'q1:2_-maxmark' => 1.0,
|
|
|
271 |
'q1:2_:minfraction' => -6.0,
|
|
|
272 |
'q1:2_:maxfraction' => 3.0,
|
|
|
273 |
],
|
|
|
274 |
'range' => [1, 2],
|
|
|
275 |
'expected' => true,
|
|
|
276 |
],
|
|
|
277 |
];
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
|
|
281 |
* Tests for is_manual_grade_in_range.
|
|
|
282 |
*
|
|
|
283 |
* @covers \question_engine::is_manual_grade_in_range
|
|
|
284 |
*/
|
|
|
285 |
public function test_is_manual_grade_in_range_ungraded(): void {
|
|
|
286 |
$this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
/**
|
|
|
290 |
* Ensure that the number renderer performs as expected.
|
|
|
291 |
*
|
|
|
292 |
* @covers \core_question_renderer::number
|
|
|
293 |
* @dataProvider render_question_number_provider
|
|
|
294 |
* @param mixed $value
|
|
|
295 |
* @param string $expected
|
|
|
296 |
*/
|
|
|
297 |
public function test_render_question_number($value, string $expected): void {
|
|
|
298 |
global $PAGE;
|
|
|
299 |
|
|
|
300 |
$renderer = new \core_question_renderer($PAGE, 'core_question');
|
|
|
301 |
$rc = new \ReflectionClass($renderer);
|
|
|
302 |
$rcm = $rc->getMethod('number');
|
|
|
303 |
|
|
|
304 |
$this->assertEquals($expected, $rcm->invoke($renderer, $value));
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* Data provider for test_render_question_number.
|
|
|
309 |
*
|
|
|
310 |
* @return array
|
|
|
311 |
*/
|
| 1441 |
ariadna |
312 |
public static function render_question_number_provider(): array {
|
| 1 |
efrain |
313 |
return [
|
|
|
314 |
'Test with number is i character' => [
|
|
|
315 |
'i',
|
|
|
316 |
'<h3 class="no">Information</h3>',
|
|
|
317 |
],
|
|
|
318 |
'Test with number is empty string' => [
|
|
|
319 |
'',
|
|
|
320 |
'',
|
|
|
321 |
],
|
|
|
322 |
'Test with null' => [
|
|
|
323 |
null,
|
|
|
324 |
'',
|
|
|
325 |
],
|
|
|
326 |
'Test with number is 0' => [
|
|
|
327 |
0,
|
|
|
328 |
'<h3 class="no">Question <span class="qno">0</span></h3>',
|
|
|
329 |
],
|
|
|
330 |
'Test with number is numeric' => [
|
|
|
331 |
1,
|
|
|
332 |
'<h3 class="no">Question <span class="qno">1</span></h3>',
|
|
|
333 |
],
|
|
|
334 |
'Test with number is string' => [
|
|
|
335 |
'1 of 2',
|
|
|
336 |
'<h3 class="no">Question <span class="qno">1 of 2</span></h3>',
|
|
|
337 |
],
|
|
|
338 |
];
|
|
|
339 |
}
|
|
|
340 |
}
|