<?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/>.
< /**
< * 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
< */
<
namespace core_h5p;
use core_h5p\local\library\autoloader;
< defined('MOODLE_INTERNAL') || die();
<
/**
< * Generator testcase for the core_grading generator.
> * 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(): 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 test_generate_h5p_data_files_creation_provider
> * @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 test_generate_h5p_data_files_creation_provider(): 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(
> $this->resetAfterTest();
'Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/', 'http://tutorial.org/', 'http://example.org/'
>
);
> /** @var \core_h5p_generator $generator */
unset($data->id);
> $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
> $course = $this->getDataGenerator()->create_course();
$expected = (object) [
> $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
'machinename' => 'Library',
> $this->setUser($user);
'title' => 'Lib',
> $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
'majorversion' => '1',
> $activitycontext = \context_module::instance($activity->cmid);
'minorversion' => '2',
> if ($filerecord) {
'patchversion' => '3',
> $filerecord['contextid'] = $activitycontext->id;
'runnable' => '1',
> $filerecord['component'] = 'mod_h5pactivity';
'fullscreen' => '1',
> $filerecord['filearea'] = 'package';
'embedtypes' => '',
> $filerecord['itemid'] = 0;
'preloadedjs' => 'js/example.js',
> $filerecord['filepath'] = '/';
'preloadedcss' => 'css/example.css',
> $filerecord['filepath'] = '/';
'droplibrarycss' => '',
> $filerecord['filename'] = 'dummy.h5p';
'semantics' => 'Semantics example',
> }
'addto' => '/regex11/',
>
'tutorial' => 'http://tutorial.org/',
> $data = $generator->generate_h5p_data(false, $filerecord);
'example' => 'http://example.org/',
>
'coremajor' => null,
> $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']);
'coreminor' => null,
> $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']);
'metadatasettings' => null,
> $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']);
];
> $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']);
> $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']);
$this->assertEquals($expected, $data);
> $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']);
}
>
> $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]);
/**
>
* Test the behaviour of create_h5p_record(). Test whather the h5p content data is
> $expected = (object) [
* properly saved in the database.
> 'h5pcontent' => (object) [
*
> 'h5pid' => $h5p->id,
* @dataProvider test_create_h5p_record_provider
> 'contentdependencies' => [$mainlib, $lib1, $lib2, $lib3, $lib4],
* @param array $h5pdata The h5p content data
> ],
* @param \stdClass $expected The expected saved data
> 'mainlib' => (object) [
**/
> 'data' => $mainlib,
public function test_create_h5p_record(array $h5pdata, \stdClass $expected) {
> 'dependencies' => [$lib1, $lib2, $lib3],
global $DB;
> ],
> 'lib1' => (object) [
$this->resetAfterTest();
> 'data' => $lib1,
> 'dependencies' => [$lib2, $lib3, $lib4],
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
> ],
> 'lib2' => (object) [
$h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata);
> 'data' => $lib2,
> 'dependencies' => [],
$data = $DB->get_record('h5p', ['id' => $h5pid]);
> ],
unset($data->id);
> 'lib3' => (object) [
unset($data->timecreated);
> 'data' => $lib3,
unset($data->timemodified);
> 'dependencies' => [$lib5],
> ],
$this->assertEquals($data, $expected);
> 'lib4' => (object) [
}
> 'data' => $lib4,
> 'dependencies' => [],
/**
> ],
* Data provider for test_create_h5p_record().
> 'lib5' => (object) [
*
> 'data' => $lib5,
* @return array
> 'dependencies' => [],
*/
> ],
public function test_create_h5p_record_provider(): array {
> ];
$createdjsoncontent = json_encode(
>
array(
> $this->assertEquals($expected, $data);
'text' => '<p>Created dummy text<\/p>\n',
> if ($filerecord) {
'questions' => '<p>Test created question<\/p>\n'
> // Confirm the H5P file has been created (when $filerecord is not empty).
)
> $fs = get_file_storage();
);
> $this->assertNotFalse($fs->get_file_by_hash($h5p->pathnamehash));
> // Confirm xAPI state has been created when $filerecord['addxapistate'] is given.
$defaultjsoncontent = json_encode(
> if (array_key_exists('addxapistate', $filerecord) && $filerecord['addxapistate']) {
array(
> $this->assertEquals(1, $DB->count_records('xapi_states'));
'text' => '<p>Dummy text<\/p>\n',
> } else {
'questions' => '<p>Test question<\/p>\n'
> $this->assertEquals(0, $DB->count_records('xapi_states'));
)
> }
);
> } else {
> // Confirm the H5P file doesn't exist when $filerecord is null.
$createdfilteredcontent = json_encode(
> $fs = get_file_storage();
array(
> $this->assertFalse($fs->get_file_by_hash($h5p->pathnamehash));
'text' => 'Created dummy text',
> // Confirm xAPI state hasn't been created when $filerecord is null.
'questions' => 'Test created question'
> $this->assertEquals(0, $DB->count_records('xapi_states'));
)
> }
);
> }
>
$defaultfilteredcontent = json_encode(
> /**
array(
> * Data provider for test_generate_h5p_data_xapistates().
'text' => 'Dummy text',
> *
'questions' => 'Test question'
> * @return array
)
> */
);
> public function generate_h5p_data_xapistates_provider(): array {
> return [
return [
> 'Do not create the file nor xAPI states' => [
'Create h5p content record with set json content and set filtered content' => [
> 'filerecord' => null,
[
> ],
1,
> 'Create the H5P file but not create any xAPI state' => [
$createdjsoncontent,
> 'filerecord' => [
$createdfilteredcontent
> 'addxapistate' => false,
],
> ],
(object) array(
> ],
'jsoncontent' => $createdjsoncontent,
> 'Create the H5P file and the xAPI state' => [
'mainlibraryid' => '1',
> 'filerecord' => [
'displayoptions' => '8',
> 'addxapistate' => true,
'pathnamehash' => sha1('pathname'),
> ],
'contenthash' => sha1('content'),
> ],
'filtered' => $createdfilteredcontent,
> ];
)
> }
],
>
'Create h5p content record with set json content and default filtered content' => [
> /**
[
> 'enabled' => 1,
< * @dataProvider test_create_h5p_record_provider
> * @dataProvider create_h5p_record_provider
< public function test_create_h5p_record_provider(): array {
> public function create_h5p_record_provider(): array {
null
],
(object) array(
'jsoncontent' => $createdjsoncontent,
'mainlibraryid' => '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 test_create_contents_libraries_record_provider
> * @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 test_create_contents_libraries_record_provider(): 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 test_create_library_dependency_record_provider
> * @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 test_create_library_dependency_record_provider(): 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 test_create_content_file_provider
> * @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 test_create_content_file_provider(): 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'
]
]
];
}
}