weekly release 3.8dev+
[moodle.git] / h5p / tests / generator_test.php
CommitLineData
27fa4ba3
MG
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* Test class covering the h5p data generator class.
19*
20* @package core_h5p
21* @category test
22* @copyright 2019 Mihail Geshoski <mihail@moodle.com>
23* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24*/
25
26namespace core_h5p;
27
28defined('MOODLE_INTERNAL') || die();
29
30/**
31* Generator testcase for the core_grading generator.
32*
33* @package core_h5p
34* @category test
35* @copyright 2019 Mihail Geshoski <mihail@moodle.com>
36* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37*/
38class generator_testcase extends \advanced_testcase {
39
40 /**
41 * Test the returned data of generate_h5p_data() when the method is called without requesting
42 * creation of library files.
43 */
44 public function test_generate_h5p_data_no_files_created_return_data() {
45 global $DB;
46
47 $this->resetAfterTest();
48
49 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
50
51 $data = $generator->generate_h5p_data();
52
53 $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
54 $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
55 $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
56 $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
57 $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
58 $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
59
60 $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]);
61
62 $expected = (object) [
63 'h5pcontent' => (object) array(
64 'h5pid' => $h5p->id,
65 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4)
66 ),
67 'mainlib' => (object) array(
68 'data' => $mainlib,
69 'dependencies' => array($lib1, $lib2, $lib3)
70 ),
71 'lib1' => (object) array(
72 'data' => $lib1,
73 'dependencies' => array($lib2, $lib3, $lib4)
74 ),
75 'lib2' => (object) array(
76 'data' => $lib2,
77 'dependencies' => array()
78 ),
79 'lib3' => (object) array(
80 'data' => $lib3,
81 'dependencies' => array($lib5)
82 ),
83 'lib4' => (object) array(
84 'data' => $lib4,
85 'dependencies' => array()
86 ),
87 'lib5' => (object) array(
88 'data' => $lib5,
89 'dependencies' => array()
90 ),
91 ];
92
93 $this->assertEquals($expected, $data);
94 }
95
96 /**
97 * Test the returned data of generate_h5p_data() when the method requests
98 * creation of library files.
99 */
100 public function test_generate_h5p_data_files_created_return_data() {
101 global $DB;
102
103 $this->resetAfterTest();
104
105 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
106
107 $data = $generator->generate_h5p_data(true);
108
109 $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
110 $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
111 $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
112 $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
113 $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
114 $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
115
116 $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]);
117
118 $expected = (object) [
119 'h5pcontent' => (object) array(
120 'h5pid' => $h5p->id,
121 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4)
122 ),
123 'mainlib' => (object) array(
124 'data' => $mainlib,
125 'dependencies' => array($lib1, $lib2, $lib3)
126 ),
127 'lib1' => (object) array(
128 'data' => $lib1,
129 'dependencies' => array($lib2, $lib3, $lib4)
130 ),
131 'lib2' => (object) array(
132 'data' => $lib2,
133 'dependencies' => array()
134 ),
135 'lib3' => (object) array(
136 'data' => $lib3,
137 'dependencies' => array($lib5)
138 ),
139 'lib4' => (object) array(
140 'data' => $lib4,
141 'dependencies' => array()
142 ),
143 'lib5' => (object) array(
144 'data' => $lib5,
145 'dependencies' => array()
146 ),
147 ];
148
149 $this->assertEquals($expected, $data);
150 }
151
152 /**
153 * Test the behaviour of generate_h5p_data(). Test whether library files are created or not
154 * on filesystem depending what the method defines.
155 *
156 * @dataProvider test_generate_h5p_data_files_creation_provider
157 * @param bool $createlibraryfiles Whether to create library files on the filesystem
158 * @param bool $expected The expectation whether the files have been created or not
159 **/
160 public function test_generate_h5p_data_files_creation(bool $createlibraryfiles, bool $expected) {
161 global $DB;
162
163 $this->resetAfterTest();
164
165 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
166 $generator->generate_h5p_data($createlibraryfiles);
167
168 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
169 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
170 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
171 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
172 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
173 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
174
175 foreach($libraries as $lib) {
176 // Return the created library files.
177 $libraryfiles = $DB->get_records('files',
178 array(
179 'component' => \core_h5p\file_storage::COMPONENT,
180 'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA,
181 'itemid' => $lib->id
182 )
183 );
184
185 $haslibraryfiles = !empty($libraryfiles);
186
187 $this->assertEquals($expected, $haslibraryfiles);
188 }
189 }
190
191 /**
192 * Data provider for test_generate_h5p_data_files_creation().
193 *
194 * @return array
195 */
196 public function test_generate_h5p_data_files_creation_provider(): array {
197 return [
198 'Do not create library related files on the filesystem' => [
199 false,
200 false
201 ],
202 'Create library related files on the filesystem' => [
203 true,
204 true
205 ]
206 ];
207 }
208
209 /**
210 * Test the behaviour of create_library_record(). Test whether the library data is properly
211 * saved in the database.
212 */
213 public function test_create_library_record() {
214 $this->resetAfterTest();
215
216 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
217
218 $data = $generator->create_library_record('Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/');
219 unset($data->id);
220
221 $expected = (object) [
222 'machinename' => 'Library',
223 'title' => 'Lib',
224 'majorversion' => '1',
225 'minorversion' => '2',
226 'patchversion' => '3',
227 'runnable' => '1',
228 'fullscreen' => '1',
229 'embedtypes' => '',
230 'preloadedjs' => 'js/example.js',
231 'preloadedcss' => 'css/example.css',
232 'droplibrarycss' => '',
233 'semantics' => 'Semantics example',
234 'addto' => '/regex11/'
235 ];
236
237 $this->assertEquals($expected, $data);
238 }
239
240 /**
241 * Test the behaviour of create_h5p_record(). Test whather the h5p content data is
242 * properly saved in the database.
243 *
244 * @dataProvider test_create_h5p_record_provider
245 * @param array $h5pdata The h5p content data
246 * @param \stdClass $expected The expected saved data
247 **/
248 public function test_create_h5p_record(array $h5pdata, \stdClass $expected) {
249 global $DB;
250
251 $this->resetAfterTest();
252
253 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
254
255 $h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata);
256
257 $data = $DB->get_record('h5p', ['id' => $h5pid]);
258 unset($data->id);
259 unset($data->timecreated);
260 unset($data->timemodified);
261
262 $this->assertEquals($data, $expected);
263 }
264
265 /**
266 * Data provider for test_create_h5p_record().
267 *
268 * @return array
269 */
270 public function test_create_h5p_record_provider(): array {
271 $createdjsoncontent = json_encode(
272 array(
273 'text' => '<p>Created dummy text<\/p>\n',
274 'questions' => '<p>Test created question<\/p>\n'
275 )
276 );
277
278 $defaultjsoncontent = json_encode(
279 array(
280 'text' => '<p>Dummy text<\/p>\n',
281 'questions' => '<p>Test question<\/p>\n'
282 )
283 );
284
285 $createdfilteredcontent = json_encode(
286 array(
287 'text' => 'Created dummy text',
288 'questions' => 'Test created question'
289 )
290 );
291
292 $defaultfilteredcontent = json_encode(
293 array(
294 'text' => 'Dummy text',
295 'questions' => 'Test question'
296 )
297 );
298
299 return [
300 'Create h5p content record with set json content and set filtered content' => [
301 [
302 1,
303 $createdjsoncontent,
304 $createdfilteredcontent
305 ],
306 (object) array(
307 'jsoncontent' => $createdjsoncontent,
308 'mainlibraryid' => '1',
309 'displayoptions' => '8',
310 'pathnamehash' => sha1('pathname'),
311 'contenthash' => sha1('content'),
312 'filtered' => $createdfilteredcontent,
313 )
314 ],
315 'Create h5p content record with set json content and default filtered content' => [
316 [
317 1,
318 $createdjsoncontent,
319 null
320 ],
321 (object) array(
322 'jsoncontent' => $createdjsoncontent,
323 'mainlibraryid' => '1',
324 'displayoptions' => '8',
325 'pathnamehash' => sha1('pathname'),
326 'contenthash' => sha1('content'),
327 'filtered' => $defaultfilteredcontent,
328 )
329 ],
330 'Create h5p content record with default json content and set filtered content' => [
331 [
332 1,
333 null,
334 $createdfilteredcontent
335 ],
336 (object) array(
337 'jsoncontent' => $defaultjsoncontent,
338 'mainlibraryid' => '1',
339 'displayoptions' => '8',
340 'pathnamehash' => sha1('pathname'),
341 'contenthash' => sha1('content'),
342 'filtered' => $createdfilteredcontent,
343 )
344 ],
345 'Create h5p content record with default json content and default filtered content' => [
346 [
347 1,
348 null,
349 null
350 ],
351 (object) array(
352 'jsoncontent' => $defaultjsoncontent,
353 'mainlibraryid' => '1',
354 'displayoptions' => '8',
355 'pathnamehash' => sha1('pathname'),
356 'contenthash' => sha1('content'),
357 'filtered' => $defaultfilteredcontent,
358 )
359 ]
360 ];
361 }
362
363 /**
364 * Test the behaviour of create_contents_libraries_record(). Test whether the contents libraries
365 * are properly saved in the database.
366 *
367 * @dataProvider test_create_contents_libraries_record_provider
368 * @param array $contentslibrariestdata The h5p contents libraries data.
369 * @param \stdClass $expected The expected saved data.
370 **/
371 public function test_create_contents_libraries_record(array $contentslibrariestdata, \stdClass $expected) {
372 global $DB;
373
374 $this->resetAfterTest();
375
376 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
377
378 $contentlibid = call_user_func_array([$generator, 'create_contents_libraries_record'], $contentslibrariestdata);
379
380 $data = $DB->get_record('h5p_contents_libraries', ['id' => $contentlibid]);
381 unset($data->id);
382
383 $this->assertEquals($data, $expected);
384 }
385
386 /**
387 * Data provider for test_create_contents_libraries_record().
388 *
389 * @return array
390 */
391 public function test_create_contents_libraries_record_provider(): array {
392 return [
393 'Create h5p content library with set dependency type' => [
394 [
395 1,
396 1,
397 'dynamic'
398 ],
399 (object) array(
400 'h5pid' => '1',
401 'libraryid' => '1',
402 'dependencytype' => 'dynamic',
403 'dropcss' => '0',
404 'weight' => '1'
405 )
406 ],
407 'Create h5p content library with a default dependency type' => [
408 [
409 1,
410 1
411 ],
412 (object) array(
413 'h5pid' => '1',
414 'libraryid' => '1',
415 'dependencytype' => 'preloaded',
416 'dropcss' => '0',
417 'weight' => '1'
418 )
419 ]
420 ];
421 }
422
423 /**
424 * Test the behaviour of create_library_dependency_record(). Test whether the contents libraries
425 * are properly saved in the database.
426 *
427 * @dataProvider test_create_library_dependency_record_provider
428 * @param array $librarydependencydata The library dependency data.
429 * @param \stdClass $expected The expected saved data.
430 **/
431 public function test_create_library_dependency_record(array $librarydependencydata, \stdClass $expected) {
432 global $DB;
433
434 $this->resetAfterTest();
435
436 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
437
438 $contentlibid = call_user_func_array([$generator, 'create_library_dependency_record'], $librarydependencydata);
439
440 $data = $DB->get_record('h5p_library_dependencies', ['id' => $contentlibid]);
441 unset($data->id);
442
443 $this->assertEquals($data, $expected);
444 }
445
446 /**
447 * Data provider for test_create_library_dependency_record().
448 *
449 * @return array
450 */
451 public function test_create_library_dependency_record_provider(): array {
452 return [
453 'Create h5p library dependency with set dependency type' => [
454 [
455 1,
456 1,
457 'dynamic'
458 ],
459 (object) array(
460 'libraryid' => '1',
461 'requiredlibraryid' => '1',
462 'dependencytype' => 'dynamic'
463 )
464 ],
465 'Create h5p library dependency with default dependency type' => [
466 [
467 1,
468 1
469 ],
470 (object) array(
471 'libraryid' => '1',
472 'requiredlibraryid' => '1',
473 'dependencytype' => 'preloaded'
474 )
475 ]
476 ];
477 }
478}