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_h5pactivity;
|
|
|
18 |
|
|
|
19 |
use mod_h5pactivity\local\manager;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Genarator tests class for mod_h5pactivity.
|
|
|
23 |
*
|
|
|
24 |
* @package mod_h5pactivity
|
|
|
25 |
* @category test
|
|
|
26 |
* @copyright 2020 Ferran Recio <ferran@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
11 |
efrain |
29 |
final class generator_test extends \advanced_testcase {
|
1 |
efrain |
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Test on H5P activity creation.
|
|
|
33 |
*/
|
11 |
efrain |
34 |
public function test_create_instance(): void {
|
1 |
efrain |
35 |
global $DB, $CFG, $USER;
|
|
|
36 |
$this->resetAfterTest();
|
|
|
37 |
$this->setAdminUser();
|
|
|
38 |
|
|
|
39 |
$course = $this->getDataGenerator()->create_course();
|
|
|
40 |
|
|
|
41 |
// Create one activity.
|
|
|
42 |
$this->assertFalse($DB->record_exists('h5pactivity', ['course' => $course->id]));
|
|
|
43 |
$activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
|
|
|
44 |
$records = $DB->get_records('h5pactivity', ['course' => $course->id], 'id');
|
|
|
45 |
$this->assertEquals(15, $activity->displayoptions);
|
|
|
46 |
$this->assertEquals(1, count($records));
|
|
|
47 |
$this->assertTrue(array_key_exists($activity->id, $records));
|
|
|
48 |
|
|
|
49 |
// Create a second one with different name and dusplay options.
|
|
|
50 |
$params = [
|
|
|
51 |
'course' => $course->id, 'name' => 'Another h5pactivity', 'displayoptions' => 6,
|
|
|
52 |
'enabletracking' => 0, 'grademethod' => manager::GRADELASTATTEMPT,
|
|
|
53 |
];
|
|
|
54 |
$activity = $this->getDataGenerator()->create_module('h5pactivity', $params);
|
|
|
55 |
$records = $DB->get_records('h5pactivity', ['course' => $course->id], 'id');
|
|
|
56 |
$this->assertEquals(6, $activity->displayoptions);
|
|
|
57 |
$this->assertEquals(0, $activity->enabletracking);
|
|
|
58 |
$this->assertEquals(manager::GRADELASTATTEMPT, $activity->grademethod);
|
|
|
59 |
$this->assertEquals(manager::REVIEWCOMPLETION, $activity->reviewmode);
|
|
|
60 |
$this->assertEquals(2, count($records));
|
|
|
61 |
$this->assertEquals('Another h5pactivity', $records[$activity->id]->name);
|
|
|
62 |
|
|
|
63 |
// Examples of specifying the package file (do not validate anything, just check for exceptions).
|
|
|
64 |
// 1. As path to the file in filesystem.
|
|
|
65 |
$params = [
|
|
|
66 |
'course' => $course->id,
|
|
|
67 |
'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/filltheblanks.h5p'
|
|
|
68 |
];
|
|
|
69 |
$activity = $this->getDataGenerator()->create_module('h5pactivity', $params);
|
|
|
70 |
|
|
|
71 |
// 2. As file draft area id.
|
|
|
72 |
$fs = get_file_storage();
|
|
|
73 |
$params = [
|
|
|
74 |
'course' => $course->id,
|
|
|
75 |
'packagefile' => file_get_unused_draft_itemid()
|
|
|
76 |
];
|
|
|
77 |
$usercontext = \context_user::instance($USER->id);
|
|
|
78 |
$filerecord = ['component' => 'user', 'filearea' => 'draft',
|
|
|
79 |
'contextid' => $usercontext->id, 'itemid' => $params['packagefile'],
|
|
|
80 |
'filename' => 'singlescobasic.zip', 'filepath' => '/'];
|
|
|
81 |
$filepath = $CFG->dirroot.'/h5p/tests/fixtures/filltheblanks.h5p';
|
|
|
82 |
$fs->create_file_from_pathname($filerecord, $filepath);
|
|
|
83 |
$activity = $this->getDataGenerator()->create_module('h5pactivity', $params);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Test that a new H5P activity cannot be generated without a valid file
|
|
|
88 |
* other user.
|
|
|
89 |
*/
|
11 |
efrain |
90 |
public function test_create_file_exception(): void {
|
1 |
efrain |
91 |
global $CFG;
|
|
|
92 |
$this->resetAfterTest();
|
|
|
93 |
$this->setAdminUser();
|
|
|
94 |
|
|
|
95 |
$course = $this->getDataGenerator()->create_course();
|
|
|
96 |
|
|
|
97 |
// Testing generator exceptions.
|
|
|
98 |
$params = [
|
|
|
99 |
'course' => $course->id,
|
|
|
100 |
'packagefilepath' => $CFG->dirroot.'/h5p/tests/fixtures/wrong_file_.xxx'
|
|
|
101 |
];
|
|
|
102 |
$this->expectException(\coding_exception::class);
|
|
|
103 |
$activity = $this->getDataGenerator()->create_module('h5pactivity', $params);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Test to create H5P attempts
|
|
|
108 |
*
|
|
|
109 |
* @dataProvider create_attempt_data
|
|
|
110 |
*
|
|
|
111 |
* @param array $tracks the attempt tracks objects
|
|
|
112 |
* @param int $attempts the final registered attempts
|
|
|
113 |
* @param int $results the final registered attempts results
|
|
|
114 |
* @param bool $exception if an exception is expected
|
|
|
115 |
*
|
|
|
116 |
*/
|
11 |
efrain |
117 |
public function test_create_attempt(array $tracks, int $attempts, int $results, bool $exception): void {
|
1 |
efrain |
118 |
global $DB;
|
|
|
119 |
$this->resetAfterTest();
|
|
|
120 |
$this->setAdminUser();
|
|
|
121 |
|
|
|
122 |
$course = $this->getDataGenerator()->create_course();
|
|
|
123 |
$activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
|
|
|
124 |
$user = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
125 |
|
|
|
126 |
$this->assertEquals(0, $DB->count_records('h5pactivity_attempts'));
|
|
|
127 |
$this->assertEquals(0, $DB->count_records('h5pactivity_attempts_results'));
|
|
|
128 |
|
|
|
129 |
if ($exception) {
|
|
|
130 |
$this->expectException(\Exception::class);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
foreach ($tracks as $track) {
|
|
|
134 |
$attemptinfo = [
|
|
|
135 |
'userid' => $user->id,
|
|
|
136 |
'h5pactivityid' => $activity->id,
|
|
|
137 |
'attempt' => $track['attempt'],
|
|
|
138 |
'interactiontype' => $track['interactiontype'],
|
|
|
139 |
'rawscore' => $track['rawscore'],
|
|
|
140 |
'maxscore' => $track['maxscore'],
|
|
|
141 |
'duration' => $track['duration'],
|
|
|
142 |
'completion' => $track['completion'],
|
|
|
143 |
'success' => $track['success'],
|
|
|
144 |
];
|
|
|
145 |
|
|
|
146 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
|
|
|
147 |
$generator->create_attempt($attemptinfo);
|
|
|
148 |
|
|
|
149 |
$this->assert_attempt_matches_info($attemptinfo);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
$this->assertEquals($attempts, $DB->count_records('h5pactivity_attempts'));
|
|
|
153 |
$this->assertEquals($results, $DB->count_records('h5pactivity_attempts_results'));
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Data provider for create attempt test.
|
|
|
158 |
*
|
|
|
159 |
* @return array
|
|
|
160 |
*/
|
11 |
efrain |
161 |
public static function create_attempt_data(): array {
|
1 |
efrain |
162 |
return [
|
|
|
163 |
'Compound statement' => [
|
|
|
164 |
[
|
|
|
165 |
[
|
|
|
166 |
'interactiontype' => 'compound', 'attempt' => 1, 'rawscore' => 2,
|
|
|
167 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
168 |
],
|
|
|
169 |
], 1, 1, false,
|
|
|
170 |
],
|
|
|
171 |
'Choice statement' => [
|
|
|
172 |
[
|
|
|
173 |
[
|
|
|
174 |
'interactiontype' => 'choice', 'attempt' => 1, 'rawscore' => 2,
|
|
|
175 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
176 |
],
|
|
|
177 |
], 1, 1, false,
|
|
|
178 |
],
|
|
|
179 |
'Matching statement' => [
|
|
|
180 |
[
|
|
|
181 |
[
|
|
|
182 |
'interactiontype' => 'matching', 'attempt' => 1, 'rawscore' => 2,
|
|
|
183 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
184 |
],
|
|
|
185 |
], 1, 1, false,
|
|
|
186 |
],
|
|
|
187 |
'Fill-in statement' => [
|
|
|
188 |
[
|
|
|
189 |
[
|
|
|
190 |
'interactiontype' => 'fill-in', 'attempt' => 1, 'rawscore' => 2,
|
|
|
191 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
192 |
],
|
|
|
193 |
], 1, 1, false,
|
|
|
194 |
],
|
|
|
195 |
'True-false statement' => [
|
|
|
196 |
[
|
|
|
197 |
[
|
|
|
198 |
'interactiontype' => 'true-false', 'attempt' => 1, 'rawscore' => 2,
|
|
|
199 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
200 |
],
|
|
|
201 |
], 1, 1, false,
|
|
|
202 |
],
|
|
|
203 |
'Long-fill-in statement' => [
|
|
|
204 |
[
|
|
|
205 |
[
|
|
|
206 |
'interactiontype' => 'long-fill-in', 'attempt' => 1, 'rawscore' => 2,
|
|
|
207 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
208 |
],
|
|
|
209 |
], 1, 1, false,
|
|
|
210 |
],
|
|
|
211 |
'Sequencing statement' => [
|
|
|
212 |
[
|
|
|
213 |
[
|
|
|
214 |
'interactiontype' => 'sequencing', 'attempt' => 1, 'rawscore' => 2,
|
|
|
215 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
216 |
],
|
|
|
217 |
], 1, 1, false,
|
|
|
218 |
],
|
|
|
219 |
'Other statement' => [
|
|
|
220 |
[
|
|
|
221 |
[
|
|
|
222 |
'interactiontype' => 'other', 'attempt' => 1, 'rawscore' => 2,
|
|
|
223 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
224 |
],
|
|
|
225 |
], 1, 1, false,
|
|
|
226 |
],
|
|
|
227 |
'No graded statement' => [
|
|
|
228 |
[
|
|
|
229 |
[
|
|
|
230 |
'interactiontype' => 'other', 'attempt' => 1, 'rawscore' => 0,
|
|
|
231 |
'maxscore' => 0, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
232 |
],
|
|
|
233 |
], 1, 1, false,
|
|
|
234 |
],
|
|
|
235 |
'Invalid statement type' => [
|
|
|
236 |
[
|
|
|
237 |
[
|
|
|
238 |
'interactiontype' => 'no-valid-statement-type', 'attempt' => 1, 'rawscore' => 2,
|
|
|
239 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
240 |
],
|
|
|
241 |
], 0, 0, true,
|
|
|
242 |
],
|
|
|
243 |
'Adding a second statement to attempt' => [
|
|
|
244 |
[
|
|
|
245 |
[
|
|
|
246 |
'interactiontype' => 'true-false', 'attempt' => 1, 'rawscore' => 2,
|
|
|
247 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
248 |
],
|
|
|
249 |
[
|
|
|
250 |
'interactiontype' => 'compound', 'attempt' => 1, 'rawscore' => 3,
|
|
|
251 |
'maxscore' => 3, 'duration' => 2, 'completion' => 1, 'success' => 0
|
|
|
252 |
],
|
|
|
253 |
], 1, 2, false,
|
|
|
254 |
],
|
|
|
255 |
'Creating two attempts' => [
|
|
|
256 |
[
|
|
|
257 |
[
|
|
|
258 |
'interactiontype' => 'compound', 'attempt' => 1, 'rawscore' => 2,
|
|
|
259 |
'maxscore' => 2, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
260 |
],
|
|
|
261 |
[
|
|
|
262 |
'interactiontype' => 'compound', 'attempt' => 2, 'rawscore' => 3,
|
|
|
263 |
'maxscore' => 3, 'duration' => 1, 'completion' => 1, 'success' => 0
|
|
|
264 |
],
|
|
|
265 |
], 2, 2, false,
|
|
|
266 |
],
|
|
|
267 |
];
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
/**
|
|
|
271 |
* Insert track into attempt, creating the attempt if necessary.
|
|
|
272 |
*
|
|
|
273 |
* @param array $attemptinfo the attempt track information
|
|
|
274 |
*/
|
|
|
275 |
private function assert_attempt_matches_info($attemptinfo): void {
|
|
|
276 |
global $DB;
|
|
|
277 |
|
|
|
278 |
$attempt = $DB->get_record('h5pactivity_attempts', [
|
|
|
279 |
'userid' => $attemptinfo['userid'],
|
|
|
280 |
'h5pactivityid' => $attemptinfo['h5pactivityid'],
|
|
|
281 |
'attempt' => $attemptinfo['attempt'],
|
|
|
282 |
]);
|
|
|
283 |
$this->assertEquals($attemptinfo['rawscore'], $attempt->rawscore);
|
|
|
284 |
$this->assertEquals($attemptinfo['maxscore'], $attempt->maxscore);
|
|
|
285 |
$this->assertEquals($attemptinfo['duration'], $attempt->duration);
|
|
|
286 |
$this->assertEquals($attemptinfo['completion'], $attempt->completion);
|
|
|
287 |
$this->assertEquals($attemptinfo['success'], $attempt->success);
|
|
|
288 |
|
|
|
289 |
$track = $DB->get_record('h5pactivity_attempts_results', [
|
|
|
290 |
'attemptid' => $attempt->id,
|
|
|
291 |
'interactiontype' => $attemptinfo['interactiontype'],
|
|
|
292 |
]);
|
|
|
293 |
$this->assertEquals($attemptinfo['rawscore'], $track->rawscore);
|
|
|
294 |
$this->assertEquals($attemptinfo['maxscore'], $track->maxscore);
|
|
|
295 |
$this->assertEquals($attemptinfo['duration'], $track->duration);
|
|
|
296 |
$this->assertEquals($attemptinfo['completion'], $track->completion);
|
|
|
297 |
$this->assertEquals($attemptinfo['success'], $track->success);
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
/**
|
|
|
301 |
* Test exceptions when creating an invalid attempt.
|
|
|
302 |
*
|
|
|
303 |
* @dataProvider create_attempt_exceptions_data
|
|
|
304 |
*
|
|
|
305 |
* @param bool $validmod if the activity id is provided
|
|
|
306 |
* @param bool $validuser if the user id is provided
|
|
|
307 |
*/
|
11 |
efrain |
308 |
public function test_create_attempt_exceptions(bool $validmod, bool $validuser): void {
|
1 |
efrain |
309 |
global $DB;
|
|
|
310 |
$this->resetAfterTest();
|
|
|
311 |
$this->setAdminUser();
|
|
|
312 |
|
|
|
313 |
$course = $this->getDataGenerator()->create_course();
|
|
|
314 |
$activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
|
|
|
315 |
$user = $this->getDataGenerator()->create_and_enrol($course, 'student');
|
|
|
316 |
|
|
|
317 |
$this->expectException(\coding_exception::class);
|
|
|
318 |
|
|
|
319 |
$attemptinfo = [
|
|
|
320 |
'attempt' => 1,
|
|
|
321 |
'interactiontype' => 'compound',
|
|
|
322 |
'rawscore' => 2,
|
|
|
323 |
'maxscore' => 1,
|
|
|
324 |
'duration' => 1,
|
|
|
325 |
'completion' => 1,
|
|
|
326 |
'success' => 0,
|
|
|
327 |
];
|
|
|
328 |
|
|
|
329 |
if ($validmod) {
|
|
|
330 |
$attemptinfo['h5pactivityid'] = $activity->id;
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
if ($validuser) {
|
|
|
334 |
$attemptinfo['userid'] = $user->id;
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
$generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
|
|
|
338 |
$generator->create_attempt($attemptinfo);
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
/**
|
|
|
342 |
* Data provider for data request creation tests.
|
|
|
343 |
*
|
|
|
344 |
* @return array
|
|
|
345 |
*/
|
11 |
efrain |
346 |
public static function create_attempt_exceptions_data(): array {
|
1 |
efrain |
347 |
return [
|
|
|
348 |
'Invalid user' => [true, false],
|
|
|
349 |
'Invalid activity' => [false, true],
|
|
|
350 |
'Invalid user and activity' => [false, false],
|
|
|
351 |
];
|
|
|
352 |
}
|
|
|
353 |
}
|