2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Test class covering the h5p data generator class.
22 * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 use core_h5p\local\library\autoloader;
30 defined('MOODLE_INTERNAL') || die();
33 * Generator testcase for the core_grading generator.
37 * @copyright 2019 Mihail Geshoski <mihail@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 * @runTestsInSeparateProcesses
41 class generator_testcase extends \advanced_testcase {
46 protected function setUp() {
49 autoloader::register();
53 * Test the returned data of generate_h5p_data() when the method is called without requesting
54 * creation of library files.
56 public function test_generate_h5p_data_no_files_created_return_data() {
59 $this->resetAfterTest();
61 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
63 $data = $generator->generate_h5p_data();
65 $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
66 $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
67 $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
68 $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
69 $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
70 $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
72 $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]);
74 $expected = (object) [
75 'h5pcontent' => (object) array(
77 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4)
79 'mainlib' => (object) array(
81 'dependencies' => array($lib1, $lib2, $lib3)
83 'lib1' => (object) array(
85 'dependencies' => array($lib2, $lib3, $lib4)
87 'lib2' => (object) array(
89 'dependencies' => array()
91 'lib3' => (object) array(
93 'dependencies' => array($lib5)
95 'lib4' => (object) array(
97 'dependencies' => array()
99 'lib5' => (object) array(
101 'dependencies' => array()
105 $this->assertEquals($expected, $data);
109 * Test the returned data of generate_h5p_data() when the method requests
110 * creation of library files.
112 public function test_generate_h5p_data_files_created_return_data() {
115 $this->resetAfterTest();
117 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
119 $data = $generator->generate_h5p_data(true);
121 $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
122 $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
123 $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
124 $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
125 $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
126 $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
128 $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]);
130 $expected = (object) [
131 'h5pcontent' => (object) array(
133 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4)
135 'mainlib' => (object) array(
137 'dependencies' => array($lib1, $lib2, $lib3)
139 'lib1' => (object) array(
141 'dependencies' => array($lib2, $lib3, $lib4)
143 'lib2' => (object) array(
145 'dependencies' => array()
147 'lib3' => (object) array(
149 'dependencies' => array($lib5)
151 'lib4' => (object) array(
153 'dependencies' => array()
155 'lib5' => (object) array(
157 'dependencies' => array()
161 $this->assertEquals($expected, $data);
165 * Test the behaviour of generate_h5p_data(). Test whether library files are created or not
166 * on filesystem depending what the method defines.
168 * @dataProvider test_generate_h5p_data_files_creation_provider
169 * @param bool $createlibraryfiles Whether to create library files on the filesystem
170 * @param bool $expected The expectation whether the files have been created or not
172 public function test_generate_h5p_data_files_creation(bool $createlibraryfiles, bool $expected) {
175 $this->resetAfterTest();
177 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
178 $generator->generate_h5p_data($createlibraryfiles);
180 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
181 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
182 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
183 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
184 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
185 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
187 foreach($libraries as $lib) {
188 // Return the created library files.
189 $libraryfiles = $DB->get_records('files',
191 'component' => \core_h5p\file_storage::COMPONENT,
192 'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA,
197 $haslibraryfiles = !empty($libraryfiles);
199 $this->assertEquals($expected, $haslibraryfiles);
204 * Data provider for test_generate_h5p_data_files_creation().
208 public function test_generate_h5p_data_files_creation_provider(): array {
210 'Do not create library related files on the filesystem' => [
214 'Create library related files on the filesystem' => [
222 * Test the behaviour of create_library_record(). Test whether the library data is properly
223 * saved in the database.
225 public function test_create_library_record() {
226 $this->resetAfterTest();
228 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
230 $data = $generator->create_library_record('Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/');
233 $expected = (object) [
234 'machinename' => 'Library',
236 'majorversion' => '1',
237 'minorversion' => '2',
238 'patchversion' => '3',
242 'preloadedjs' => 'js/example.js',
243 'preloadedcss' => 'css/example.css',
244 'droplibrarycss' => '',
245 'semantics' => 'Semantics example',
246 'addto' => '/regex11/',
249 'metadatasettings' => null,
252 $this->assertEquals($expected, $data);
256 * Test the behaviour of create_h5p_record(). Test whather the h5p content data is
257 * properly saved in the database.
259 * @dataProvider test_create_h5p_record_provider
260 * @param array $h5pdata The h5p content data
261 * @param \stdClass $expected The expected saved data
263 public function test_create_h5p_record(array $h5pdata, \stdClass $expected) {
266 $this->resetAfterTest();
268 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
270 $h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata);
272 $data = $DB->get_record('h5p', ['id' => $h5pid]);
274 unset($data->timecreated);
275 unset($data->timemodified);
277 $this->assertEquals($data, $expected);
281 * Data provider for test_create_h5p_record().
285 public function test_create_h5p_record_provider(): array {
286 $createdjsoncontent = json_encode(
288 'text' => '<p>Created dummy text<\/p>\n',
289 'questions' => '<p>Test created question<\/p>\n'
293 $defaultjsoncontent = json_encode(
295 'text' => '<p>Dummy text<\/p>\n',
296 'questions' => '<p>Test question<\/p>\n'
300 $createdfilteredcontent = json_encode(
302 'text' => 'Created dummy text',
303 'questions' => 'Test created question'
307 $defaultfilteredcontent = json_encode(
309 'text' => 'Dummy text',
310 'questions' => 'Test question'
315 'Create h5p content record with set json content and set filtered content' => [
319 $createdfilteredcontent
322 'jsoncontent' => $createdjsoncontent,
323 'mainlibraryid' => '1',
324 'displayoptions' => '8',
325 'pathnamehash' => sha1('pathname'),
326 'contenthash' => sha1('content'),
327 'filtered' => $createdfilteredcontent,
330 'Create h5p content record with set json content and default filtered content' => [
337 'jsoncontent' => $createdjsoncontent,
338 'mainlibraryid' => '1',
339 'displayoptions' => '8',
340 'pathnamehash' => sha1('pathname'),
341 'contenthash' => sha1('content'),
342 'filtered' => $defaultfilteredcontent,
345 'Create h5p content record with default json content and set filtered content' => [
349 $createdfilteredcontent
352 'jsoncontent' => $defaultjsoncontent,
353 'mainlibraryid' => '1',
354 'displayoptions' => '8',
355 'pathnamehash' => sha1('pathname'),
356 'contenthash' => sha1('content'),
357 'filtered' => $createdfilteredcontent,
360 'Create h5p content record with default json content and default filtered content' => [
367 'jsoncontent' => $defaultjsoncontent,
368 'mainlibraryid' => '1',
369 'displayoptions' => '8',
370 'pathnamehash' => sha1('pathname'),
371 'contenthash' => sha1('content'),
372 'filtered' => $defaultfilteredcontent,
379 * Test the behaviour of create_contents_libraries_record(). Test whether the contents libraries
380 * are properly saved in the database.
382 * @dataProvider test_create_contents_libraries_record_provider
383 * @param array $contentslibrariestdata The h5p contents libraries data.
384 * @param \stdClass $expected The expected saved data.
386 public function test_create_contents_libraries_record(array $contentslibrariestdata, \stdClass $expected) {
389 $this->resetAfterTest();
391 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
393 $contentlibid = call_user_func_array([$generator, 'create_contents_libraries_record'], $contentslibrariestdata);
395 $data = $DB->get_record('h5p_contents_libraries', ['id' => $contentlibid]);
398 $this->assertEquals($data, $expected);
402 * Data provider for test_create_contents_libraries_record().
406 public function test_create_contents_libraries_record_provider(): array {
408 'Create h5p content library with set dependency type' => [
417 'dependencytype' => 'dynamic',
422 'Create h5p content library with a default dependency type' => [
430 'dependencytype' => 'preloaded',
439 * Test the behaviour of create_library_dependency_record(). Test whether the contents libraries
440 * are properly saved in the database.
442 * @dataProvider test_create_library_dependency_record_provider
443 * @param array $librarydependencydata The library dependency data.
444 * @param \stdClass $expected The expected saved data.
446 public function test_create_library_dependency_record(array $librarydependencydata, \stdClass $expected) {
449 $this->resetAfterTest();
451 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
453 $contentlibid = call_user_func_array([$generator, 'create_library_dependency_record'], $librarydependencydata);
455 $data = $DB->get_record('h5p_library_dependencies', ['id' => $contentlibid]);
458 $this->assertEquals($data, $expected);
462 * Data provider for test_create_library_dependency_record().
466 public function test_create_library_dependency_record_provider(): array {
468 'Create h5p library dependency with set dependency type' => [
476 'requiredlibraryid' => '1',
477 'dependencytype' => 'dynamic'
480 'Create h5p library dependency with default dependency type' => [
487 'requiredlibraryid' => '1',
488 'dependencytype' => 'preloaded'
495 * Test the behaviour of create_content_file(). Test whether a file belonging to a content is created.
497 * @dataProvider test_create_content_file_provider
498 * @param array $filedata Data from the file to be created.
499 * @param array $expecteddata Data expected.Data from the file to be created.
501 public function test_create_content_file($filedata, $expecteddata): void {
502 $this->resetAfterTest();
504 $generator = self::getDataGenerator()->get_plugin_generator('core_h5p');
506 if ($expecteddata[1] === 'exception') {
507 $this->expectException('coding_exception');
509 call_user_func_array([$generator, 'create_content_file'], $filedata);
511 $systemcontext = \context_system::instance();
512 $filearea = $filedata[1];
513 $filepath = '/'. dirname($filedata[0]). '/';
514 $filename = basename($filedata[0]);
515 $itemid = $expecteddata[0];
517 $fs = new \file_storage();
518 $exists = $fs->file_exists($systemcontext->id, file_storage::COMPONENT, $filearea, $itemid, $filepath,
520 if ($expecteddata[1] === true) {
521 $this->assertTrue($exists);
522 } else if ($expecteddata[1] === false) {
523 $this->assertFalse($exists);
528 * Data provider for test_create_content_file(). Data from different files to be created.
532 public function test_create_content_file_provider(): array {
534 'Create file in content with id 4' => [
545 'Create file in the editor' => [
555 'Create file in content without id' => [