Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

namespace core_h5p;

use core_h5p\local\library\autoloader;

/**
* Test class covering the h5p data generator class.
*
* @package    core_h5p
* @category   test
* @copyright  2019 Mihail Geshoski <mihail@moodle.com>
* @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @runTestsInSeparateProcesses
> * @covers \core_h5p_generator
*/
< class generator_testcase extends \advanced_testcase {
> class generator_test extends \advanced_testcase {
/** * Tests set up. */
< protected function setUp() {
> protected function setUp(): void {
parent::setUp(); autoloader::register(); } /** * Test the returned data of generate_h5p_data() when the method is called without requesting * creation of library files. */ public function test_generate_h5p_data_no_files_created_return_data() { global $DB; $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); $data = $generator->generate_h5p_data(); $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']); $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']); $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']); $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']); $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']); $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']); $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]); $expected = (object) [ 'h5pcontent' => (object) array( 'h5pid' => $h5p->id, 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4) ), 'mainlib' => (object) array( 'data' => $mainlib, 'dependencies' => array($lib1, $lib2, $lib3) ), 'lib1' => (object) array( 'data' => $lib1, 'dependencies' => array($lib2, $lib3, $lib4) ), 'lib2' => (object) array( 'data' => $lib2, 'dependencies' => array() ), 'lib3' => (object) array( 'data' => $lib3, 'dependencies' => array($lib5) ), 'lib4' => (object) array( 'data' => $lib4, 'dependencies' => array() ), 'lib5' => (object) array( 'data' => $lib5, 'dependencies' => array() ), ]; $this->assertEquals($expected, $data); } /** * Test the returned data of generate_h5p_data() when the method requests * creation of library files. */ public function test_generate_h5p_data_files_created_return_data() { global $DB; $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); $data = $generator->generate_h5p_data(true); $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']); $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']); $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']); $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']); $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']); $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']); $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]); $expected = (object) [ 'h5pcontent' => (object) array( 'h5pid' => $h5p->id, 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4) ), 'mainlib' => (object) array( 'data' => $mainlib, 'dependencies' => array($lib1, $lib2, $lib3) ), 'lib1' => (object) array( 'data' => $lib1, 'dependencies' => array($lib2, $lib3, $lib4) ), 'lib2' => (object) array( 'data' => $lib2, 'dependencies' => array() ), 'lib3' => (object) array( 'data' => $lib3, 'dependencies' => array($lib5) ), 'lib4' => (object) array( 'data' => $lib4, 'dependencies' => array() ), 'lib5' => (object) array( 'data' => $lib5, 'dependencies' => array() ), ]; $this->assertEquals($expected, $data); } /** * Test the behaviour of generate_h5p_data(). Test whether library files are created or not * on filesystem depending what the method defines. * * @dataProvider generate_h5p_data_files_creation_provider * @param bool $createlibraryfiles Whether to create library files on the filesystem * @param bool $expected The expectation whether the files have been created or not **/ public function test_generate_h5p_data_files_creation(bool $createlibraryfiles, bool $expected) { global $DB; $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); $generator->generate_h5p_data($createlibraryfiles); $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']); $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']); $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']); $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']); $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']); $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']); foreach($libraries as $lib) { // Return the created library files. $libraryfiles = $DB->get_records('files', array( 'component' => \core_h5p\file_storage::COMPONENT, 'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA, 'itemid' => $lib->id ) ); $haslibraryfiles = !empty($libraryfiles); $this->assertEquals($expected, $haslibraryfiles); } } /** * Data provider for test_generate_h5p_data_files_creation(). * * @return array */ public function generate_h5p_data_files_creation_provider(): array { return [ 'Do not create library related files on the filesystem' => [ false, false ], 'Create library related files on the filesystem' => [ true, true ] ]; } /**
> * Test the returned data of generate_h5p_data() when the method requests * Test the behaviour of create_library_record(). Test whether the library data is properly > * creation of H5P file and xAPI states. * saved in the database. > * */ > * @dataProvider generate_h5p_data_xapistates_provider public function test_create_library_record() { > * @param array|null $filerecord $this->resetAfterTest(); > */ > public function test_generate_h5p_data_xapistates(?array $filerecord) { $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); > global $DB; > $data = $generator->create_library_record('Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/'); > $this->resetAfterTest(); unset($data->id); > > /** @var \core_h5p_generator $generator */ $expected = (object) [ > $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 'machinename' => 'Library', > $course = $this->getDataGenerator()->create_course(); 'title' => 'Lib', > $user = $this->getDataGenerator()->create_and_enrol($course, 'student'); 'majorversion' => '1', > $this->setUser($user); 'minorversion' => '2', > $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]); 'patchversion' => '3', > $activitycontext = \context_module::instance($activity->cmid); 'runnable' => '1', > if ($filerecord) { 'fullscreen' => '1', > $filerecord['contextid'] = $activitycontext->id; 'embedtypes' => '', > $filerecord['component'] = 'mod_h5pactivity'; 'preloadedjs' => 'js/example.js', > $filerecord['filearea'] = 'package'; 'preloadedcss' => 'css/example.css', > $filerecord['itemid'] = 0; 'droplibrarycss' => '', > $filerecord['filepath'] = '/'; 'semantics' => 'Semantics example', > $filerecord['filepath'] = '/'; 'addto' => '/regex11/', > $filerecord['filename'] = 'dummy.h5p'; 'coremajor' => null, > } 'coreminor' => null, > 'metadatasettings' => null, > $data = $generator->generate_h5p_data(false, $filerecord); ]; > > $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']); $this->assertEquals($expected, $data); > $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']); } > $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']); > $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']); /** > $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']); * Test the behaviour of create_h5p_record(). Test whather the h5p content data is > $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']); * properly saved in the database. > * > $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]); * @dataProvider create_h5p_record_provider > * @param array $h5pdata The h5p content data > $expected = (object) [ * @param \stdClass $expected The expected saved data > 'h5pcontent' => (object) [ **/ > 'h5pid' => $h5p->id, public function test_create_h5p_record(array $h5pdata, \stdClass $expected) { > 'contentdependencies' => [$mainlib, $lib1, $lib2, $lib3, $lib4], global $DB; > ], > 'mainlib' => (object) [ $this->resetAfterTest(); > 'data' => $mainlib, > 'dependencies' => [$lib1, $lib2, $lib3], $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); > ], > 'lib1' => (object) [ $h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata); > 'data' => $lib1, > 'dependencies' => [$lib2, $lib3, $lib4], $data = $DB->get_record('h5p', ['id' => $h5pid]); > ], unset($data->id); > 'lib2' => (object) [ unset($data->timecreated); > 'data' => $lib2, unset($data->timemodified); > 'dependencies' => [], > ], $this->assertEquals($data, $expected); > 'lib3' => (object) [ } > 'data' => $lib3, > 'dependencies' => [$lib5], /** > ], * Data provider for test_create_h5p_record(). > 'lib4' => (object) [ * > 'data' => $lib4, * @return array > 'dependencies' => [], */ > ], public function create_h5p_record_provider(): array { > 'lib5' => (object) [ $createdjsoncontent = json_encode( > 'data' => $lib5, array( > 'dependencies' => [], 'text' => '<p>Created dummy text<\/p>\n', > ], 'questions' => '<p>Test created question<\/p>\n' > ]; ) > ); > $this->assertEquals($expected, $data); > if ($filerecord) { $defaultjsoncontent = json_encode( > // Confirm the H5P file has been created (when $filerecord is not empty). array( > $fs = get_file_storage(); 'text' => '<p>Dummy text<\/p>\n', > $this->assertNotFalse($fs->get_file_by_hash($h5p->pathnamehash)); 'questions' => '<p>Test question<\/p>\n' > // Confirm xAPI state has been created when $filerecord['addxapistate'] is given. ) > if (array_key_exists('addxapistate', $filerecord) && $filerecord['addxapistate']) { ); > $this->assertEquals(1, $DB->count_records('xapi_states')); > } else { $createdfilteredcontent = json_encode( > $this->assertEquals(0, $DB->count_records('xapi_states')); array( > } 'text' => 'Created dummy text', > } else { 'questions' => 'Test created question' > // Confirm the H5P file doesn't exist when $filerecord is null. ) > $fs = get_file_storage(); ); > $this->assertFalse($fs->get_file_by_hash($h5p->pathnamehash)); > // Confirm xAPI state hasn't been created when $filerecord is null. $defaultfilteredcontent = json_encode( > $this->assertEquals(0, $DB->count_records('xapi_states')); array( > } 'text' => 'Dummy text', > } 'questions' => 'Test question' > ) > /** ); > * Data provider for test_generate_h5p_data_xapistates(). > * return [ > * @return array 'Create h5p content record with set json content and set filtered content' => [ > */ [ > public function generate_h5p_data_xapistates_provider(): array { 1, > return [ $createdjsoncontent, > 'Do not create the file nor xAPI states' => [ $createdfilteredcontent > 'filerecord' => null, ], > ], (object) array( > 'Create the H5P file but not create any xAPI state' => [ 'jsoncontent' => $createdjsoncontent, > 'filerecord' => [ 'mainlibraryid' => '1', > 'addxapistate' => false, 'displayoptions' => '8', > ], 'pathnamehash' => sha1('pathname'), > ], 'contenthash' => sha1('content'), > 'Create the H5P file and the xAPI state' => [ 'filtered' => $createdfilteredcontent, > 'filerecord' => [ ) > 'addxapistate' => true, ], > ], 'Create h5p content record with set json content and default filtered content' => [ > ], [ > ]; 1, > } $createdjsoncontent, > null > /**
< $data = $generator->create_library_record('Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/');
> $data = $generator->create_library_record( > 'Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/', 'http://tutorial.org/', 'http://example.org/' > );
(object) array(
> 'tutorial' => 'http://tutorial.org/', 'jsoncontent' => $createdjsoncontent, > 'example' => 'http://example.org/',
'mainlibraryid' => '1',
> 'enabled' => 1,
'displayoptions' => '8', 'pathnamehash' => sha1('pathname'), 'contenthash' => sha1('content'), 'filtered' => $defaultfilteredcontent, ) ], 'Create h5p content record with default json content and set filtered content' => [ [ 1, null, $createdfilteredcontent ], (object) array( 'jsoncontent' => $defaultjsoncontent, 'mainlibraryid' => '1', 'displayoptions' => '8', 'pathnamehash' => sha1('pathname'), 'contenthash' => sha1('content'), 'filtered' => $createdfilteredcontent, ) ], 'Create h5p content record with default json content and default filtered content' => [ [ 1, null, null ], (object) array( 'jsoncontent' => $defaultjsoncontent, 'mainlibraryid' => '1', 'displayoptions' => '8', 'pathnamehash' => sha1('pathname'), 'contenthash' => sha1('content'), 'filtered' => $defaultfilteredcontent, ) ] ]; } /** * Test the behaviour of create_contents_libraries_record(). Test whether the contents libraries * are properly saved in the database. * * @dataProvider create_contents_libraries_record_provider * @param array $contentslibrariestdata The h5p contents libraries data. * @param \stdClass $expected The expected saved data. **/ public function test_create_contents_libraries_record(array $contentslibrariestdata, \stdClass $expected) { global $DB; $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); $contentlibid = call_user_func_array([$generator, 'create_contents_libraries_record'], $contentslibrariestdata); $data = $DB->get_record('h5p_contents_libraries', ['id' => $contentlibid]); unset($data->id); $this->assertEquals($data, $expected); } /** * Data provider for test_create_contents_libraries_record(). * * @return array */ public function create_contents_libraries_record_provider(): array { return [ 'Create h5p content library with set dependency type' => [ [ 1, 1, 'dynamic' ], (object) array( 'h5pid' => '1', 'libraryid' => '1', 'dependencytype' => 'dynamic', 'dropcss' => '0', 'weight' => '1' ) ], 'Create h5p content library with a default dependency type' => [ [ 1, 1 ], (object) array( 'h5pid' => '1', 'libraryid' => '1', 'dependencytype' => 'preloaded', 'dropcss' => '0', 'weight' => '1' ) ] ]; } /** * Test the behaviour of create_library_dependency_record(). Test whether the contents libraries * are properly saved in the database. * * @dataProvider create_library_dependency_record_provider * @param array $librarydependencydata The library dependency data. * @param \stdClass $expected The expected saved data. **/ public function test_create_library_dependency_record(array $librarydependencydata, \stdClass $expected) { global $DB; $this->resetAfterTest(); $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); $contentlibid = call_user_func_array([$generator, 'create_library_dependency_record'], $librarydependencydata); $data = $DB->get_record('h5p_library_dependencies', ['id' => $contentlibid]); unset($data->id); $this->assertEquals($data, $expected); } /** * Data provider for test_create_library_dependency_record(). * * @return array */ public function create_library_dependency_record_provider(): array { return [ 'Create h5p library dependency with set dependency type' => [ [ 1, 1, 'dynamic' ], (object) array( 'libraryid' => '1', 'requiredlibraryid' => '1', 'dependencytype' => 'dynamic' ) ], 'Create h5p library dependency with default dependency type' => [ [ 1, 1 ], (object) array( 'libraryid' => '1', 'requiredlibraryid' => '1', 'dependencytype' => 'preloaded' ) ] ]; } /** * Test the behaviour of create_content_file(). Test whether a file belonging to a content is created. * * @dataProvider create_content_file_provider * @param array $filedata Data from the file to be created. * @param array $expecteddata Data expected.Data from the file to be created. */ public function test_create_content_file($filedata, $expecteddata): void { $this->resetAfterTest(); $generator = self::getDataGenerator()->get_plugin_generator('core_h5p'); if ($expecteddata[1] === 'exception') { $this->expectException('coding_exception'); } call_user_func_array([$generator, 'create_content_file'], $filedata); $systemcontext = \context_system::instance(); $filearea = $filedata[1]; $filepath = '/'. dirname($filedata[0]). '/'; $filename = basename($filedata[0]); $itemid = $expecteddata[0]; $fs = new \file_storage(); $exists = $fs->file_exists($systemcontext->id, file_storage::COMPONENT, $filearea, $itemid, $filepath, $filename); if ($expecteddata[1] === true) { $this->assertTrue($exists); } else if ($expecteddata[1] === false) { $this->assertFalse($exists); } } /** * Data provider for test_create_content_file(). Data from different files to be created. * * @return array **/ public function create_content_file_provider(): array { return [ 'Create file in content with id 4' => [ [ 'images/img1.png', 'content', 4 ], [ 4, true ] ], 'Create file in the editor' => [ [ 'images/img1.png', 'editor' ], [ 0, true ] ], 'Create file in content without id' => [ [ 'images/img1.png', 'content' ], [ 0, 'exception' ] ] ]; } }