MDL-66609 core_h5p: Unit test framework interface implementation

This commit is contained in:
Mihail Geshoski 2019-09-26 16:59:12 +08:00 committed by Andrew Nicols
parent 09d8143d84
commit 27fa4ba3d4
3 changed files with 2717 additions and 4 deletions

2017
h5p/tests/framework_test.php Normal file

File diff suppressed because it is too large Load diff

View file

@ -59,7 +59,7 @@ class core_h5p_generator extends \component_generator_base {
* @param string $version Not really needed at the moment. * @param string $version Not really needed at the moment.
*/ */
protected function add_libfile_to_array(string $type, string $path, string $version, &$files): void { protected function add_libfile_to_array(string $type, string $path, string $version, &$files): void {
$files[$type][] = (object) [ $files[$type][] = (object)[
'path' => $path, 'path' => $path,
'version' => "?ver=$version" 'version' => "?ver=$version"
]; ];
@ -75,8 +75,8 @@ class core_h5p_generator extends \component_generator_base {
* @param int $minorversion Minor version (any number will do). * @param int $minorversion Minor version (any number will do).
* @return array A list of library data and files that the core API will understand. * @return array A list of library data and files that the core API will understand.
*/ */
public function create_library(string $uploaddirectory, int $libraryid, string $machinename, int $majorversion, int public function create_library(string $uploaddirectory, int $libraryid, string $machinename, int $majorversion,
$minorversion): array { int $minorversion): array {
/** @var array $files an array used in the cache tests. */ /** @var array $files an array used in the cache tests. */
$files = ['scripts' => [], 'styles' => []]; $files = ['scripts' => [], 'styles' => []];
@ -121,4 +121,222 @@ $minorversion): array {
return [$lib, $files]; return [$lib, $files];
} }
} /**
* Save the library files on the filesystem.
*
* @param stdClss $lib The library data
*/
private function save_library(stdClass $lib) {
// Get a temp path.
$filestorage = new \core_h5p\file_storage();
$temppath = $filestorage->getTmpPath();
// Create and save the library files on the filesystem.
$basedirectorymain = $temppath . '/' . $lib->machinename . '-' .
$lib->majorversion . '.' . $lib->minorversion;
list($library, $libraryfiles) = $this->create_library($basedirectorymain, $lib->id, $lib->machinename,
$lib->majorversion, $lib->minorversion);
$filestorage->saveLibrary($library);
}
/**
* Populate H5P database tables with relevant data to simulate the process of adding H5P content.
*
* @param bool $createlibraryfiles Whether to create and store library files on the filesystem
* @return stdClass An object representing the added H5P records
*/
public function generate_h5p_data(bool $createlibraryfiles = false): stdClass {
// Create libraries.
$mainlib = $libraries[] = $this->create_library_record('MainLibrary', 'Main Lib', 1, 0);
$lib1 = $libraries[] = $this->create_library_record('Library1', 'Lib1', 2, 0);
$lib2 = $libraries[] = $this->create_library_record('Library2', 'Lib2', 2, 1);
$lib3 = $libraries[] = $this->create_library_record('Library3', 'Lib3', 3, 2);
$lib4 = $libraries[] = $this->create_library_record('Library4', 'Lib4', 1, 1);
$lib5 = $libraries[] = $this->create_library_record('Library5', 'Lib5', 1, 3);
if ($createlibraryfiles) {
foreach ($libraries as $lib) {
// Create and save the library files on the filesystem.
$this->save_library($lib);
}
}
// Create h5p content.
$h5p = $this->create_h5p_record($mainlib->id);
// Create h5p content library dependencies.
$this->create_contents_libraries_record($h5p, $mainlib->id);
$this->create_contents_libraries_record($h5p, $lib1->id);
$this->create_contents_libraries_record($h5p, $lib2->id);
$this->create_contents_libraries_record($h5p, $lib3->id);
$this->create_contents_libraries_record($h5p, $lib4->id);
// Create library dependencies for $mainlib.
$this->create_library_dependency_record($mainlib->id, $lib1->id);
$this->create_library_dependency_record($mainlib->id, $lib2->id);
$this->create_library_dependency_record($mainlib->id, $lib3->id);
// Create library dependencies for $lib1.
$this->create_library_dependency_record($lib1->id, $lib2->id);
$this->create_library_dependency_record($lib1->id, $lib3->id);
$this->create_library_dependency_record($lib1->id, $lib4->id);
// Create library dependencies for $lib3.
$this->create_library_dependency_record($lib3->id, $lib5->id);
return (object) [
'h5pcontent' => (object) array(
'h5pid' => $h5p,
'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()
),
];
}
/**
* Create a record in the h5p_libraries database table.
*
* @param string $machinename The library machine name
* @param string $title The library's name
* @param int $majorversion The library's major version
* @param int $minorversion The library's minor version
* @param int $patchversion The library's patch version
* @param string $semantics Json describing the content structure for the library
* @param string $addto The plugin configuration data
* @return stdClass An object representing the added library record
*/
public function create_library_record(string $machinename, string $title, int $majorversion = 1,
int $minorversion = 0, int $patchversion = 1, string $semantics = '', string $addto = null): stdClass {
global $DB;
$content = array(
'machinename' => $machinename,
'title' => $title,
'majorversion' => $majorversion,
'minorversion' => $minorversion,
'patchversion' => $patchversion,
'runnable' => 1,
'fullscreen' => 1,
'preloadedjs' => 'js/example.js',
'preloadedcss' => 'css/example.css',
'droplibrarycss' => '',
'semantics' => $semantics,
'addto' => $addto
);
$libraryid = $DB->insert_record('h5p_libraries', $content);
return $DB->get_record('h5p_libraries', ['id' => $libraryid]);
}
/**
* Create a record in the h5p database table.
*
* @param int $mainlibid The ID of the content's main library
* @param string $jsoncontent The content in json format
* @param string $filtered The filtered content parameters
* @return int The ID of the added record
*/
public function create_h5p_record(int $mainlibid, string $jsoncontent = null, string $filtered = null): int {
global $DB;
if (!$jsoncontent) {
$jsoncontent = json_encode(
array(
'text' => '<p>Dummy text<\/p>\n',
'questions' => '<p>Test question<\/p>\n'
)
);
}
if (!$filtered) {
$filtered = json_encode(
array(
'text' => 'Dummy text',
'questions' => 'Test question'
)
);
}
return $DB->insert_record(
'h5p',
array(
'jsoncontent' => $jsoncontent,
'displayoptions' => 8,
'mainlibraryid' => $mainlibid,
'timecreated' => time(),
'timemodified' => time(),
'filtered' => $filtered,
'pathnamehash' => sha1('pathname'),
'contenthash' => sha1('content')
)
);
}
/**
* Create a record in the h5p_contents_libraries database table.
*
* @param string $h5pid The ID of the H5P content
* @param int $libid The ID of the library
* @param string $dependencytype The dependency type
* @return int The ID of the added record
*/
public function create_contents_libraries_record(string $h5pid, int $libid,
string $dependencytype = 'preloaded'): int {
global $DB;
return $DB->insert_record(
'h5p_contents_libraries',
array(
'h5pid' => $h5pid,
'libraryid' => $libid,
'dependencytype' => $dependencytype,
'dropcss' => 0,
'weight' => 1
)
);
}
/**
* Create a record in the h5p_library_dependencies database table.
*
* @param int $libid The ID of the library
* @param int $requiredlibid The ID of the required library
* @param string $dependencytype The dependency type
* @return int The ID of the added record
*/
public function create_library_dependency_record(int $libid, int $requiredlibid,
string $dependencytype = 'preloaded'): int {
global $DB;
return $DB->insert_record(
'h5p_library_dependencies',
array(
'libraryid' => $libid,
'requiredlibraryid' => $requiredlibid,
'dependencytype' => $dependencytype
)
);
}
}

View file

@ -0,0 +1,478 @@
<?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;
defined('MOODLE_INTERNAL') || die();
/**
* Generator testcase for the core_grading generator.
*
* @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
*/
class generator_testcase extends \advanced_testcase {
/**
* 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
* @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 {
return [
'Do not create library related files on the filesystem' => [
false,
false
],
'Create library related files on the filesystem' => [
true,
true
]
];
}
/**
* Test the behaviour of create_library_record(). Test whether the library data is properly
* saved in the database.
*/
public function test_create_library_record() {
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$data = $generator->create_library_record('Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/');
unset($data->id);
$expected = (object) [
'machinename' => 'Library',
'title' => 'Lib',
'majorversion' => '1',
'minorversion' => '2',
'patchversion' => '3',
'runnable' => '1',
'fullscreen' => '1',
'embedtypes' => '',
'preloadedjs' => 'js/example.js',
'preloadedcss' => 'css/example.css',
'droplibrarycss' => '',
'semantics' => 'Semantics example',
'addto' => '/regex11/'
];
$this->assertEquals($expected, $data);
}
/**
* Test the behaviour of create_h5p_record(). Test whather the h5p content data is
* properly saved in the database.
*
* @dataProvider test_create_h5p_record_provider
* @param array $h5pdata The h5p content data
* @param \stdClass $expected The expected saved data
**/
public function test_create_h5p_record(array $h5pdata, \stdClass $expected) {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
$h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata);
$data = $DB->get_record('h5p', ['id' => $h5pid]);
unset($data->id);
unset($data->timecreated);
unset($data->timemodified);
$this->assertEquals($data, $expected);
}
/**
* Data provider for test_create_h5p_record().
*
* @return array
*/
public function test_create_h5p_record_provider(): array {
$createdjsoncontent = json_encode(
array(
'text' => '<p>Created dummy text<\/p>\n',
'questions' => '<p>Test created question<\/p>\n'
)
);
$defaultjsoncontent = json_encode(
array(
'text' => '<p>Dummy text<\/p>\n',
'questions' => '<p>Test question<\/p>\n'
)
);
$createdfilteredcontent = json_encode(
array(
'text' => 'Created dummy text',
'questions' => 'Test created question'
)
);
$defaultfilteredcontent = json_encode(
array(
'text' => 'Dummy text',
'questions' => 'Test question'
)
);
return [
'Create h5p content record with set json content and set filtered content' => [
[
1,
$createdjsoncontent,
$createdfilteredcontent
],
(object) array(
'jsoncontent' => $createdjsoncontent,
'mainlibraryid' => '1',
'displayoptions' => '8',
'pathnamehash' => sha1('pathname'),
'contenthash' => sha1('content'),
'filtered' => $createdfilteredcontent,
)
],
'Create h5p content record with set json content and default filtered content' => [
[
1,
$createdjsoncontent,
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
* @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 {
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
* @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 {
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'
)
]
];
}
}