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 * Testing the H5peditorStorage interface implementation.
22 * @copyright 2020 Victor Deniz <victor@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 use core_h5p\local\library\autoloader
;
32 * Test class covering the H5peditorStorage interface implementation.
35 * @copyright 2020 Victor Deniz <victor@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @runTestsInSeparateProcesses
40 class editor_framework_test
extends \advanced_testcase
{
42 /** @var editor_framework H5P editor_framework instance */
43 protected $editorframework;
46 * Setup to ensure that fixtures are loaded.
48 public static function setupBeforeClass(): void
{
49 autoloader
::register();
53 * Set up function for tests.
55 protected function setUp(): void
{
58 $this->editorframework
= new editor_framework();
62 * Test that the method getLanguage retrieves the translation of a library in the requested language.
64 * @dataProvider get_language_provider
66 * @param array $datalib Library data to create
67 * @param string $lang Language to retrieve the translation
68 * @param bool $emptyexpected True when false value is expected; false, otherwise
69 * @param string $machinename The machine readable name of the library(content type)
70 * @param int $majorversion Major part of version number
71 * @param int $minorversion Minor part of version number
73 public function test_get_language(array $datalib, string $lang, ?
bool $emptyexpected = false, ?
string $machinename = '',
74 ?
int $majorversion = 1, ?
int $minorversion = 0): void
{
75 $this->resetAfterTest(true);
78 $generator = \testing_util
::get_data_generator();
79 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
81 $h5pfilestorage = new file_storage();
82 $h5ptempath = $h5pfilestorage->getTmpPath();
87 if (array_key_exists('translation', $datalib)) {
88 $translations = $datalib['translation'];
90 // Create DB entry for this library.
91 $tmplib = $h5pgenerator->create_library_record($datalib['machinename'], $datalib['title'], $datalib['majorversion'],
92 $datalib['minorversion']);
93 // Create the files for this libray.
94 [$library, $files] = $h5pgenerator->create_library($h5ptempath, $tmplib->id
, $datalib['machinename'],
95 $datalib['majorversion'], $datalib['minorversion'], $translations);
96 $h5pfilestorage->saveLibrary($library);
98 // If machinename, majorversion or minorversion are empty, use the value in datalib.
99 if (empty($machinename)) {
100 $machinename = $datalib['machinename'];
102 if (empty($majorversion)) {
103 $majorversion = $datalib['majorversion'];
105 if (empty($minorversion)) {
106 $minorversion = $datalib['minorversion'];
108 if (!$emptyexpected && array_key_exists($lang, $translations)) {
109 $expectedresult = $translations[$lang];
114 $json = $this->editorframework
->getLanguage($machinename, $majorversion, $minorversion, $lang);
116 if ($emptyexpected) {
117 $this->assertFalse($json);
119 $this->assertEquals($expectedresult, $json);
124 * Data provider for test_get_language().
128 public function get_language_provider(): array {
138 'One library created but getting translation from an unexisting one' => [
140 'machinename' => 'Library1',
145 'es' => '{"libraryStrings": {"key": "valor"}}',
146 'fr' => '{"libraryStrings": {"key": "valeur"}}',
153 'One library without any translation' => [
155 'machinename' => 'Library1',
163 'One library with 2 translations (es and fr) - es' => [
165 'machinename' => 'Library1',
170 'es' => '{"libraryStrings": {"key": "valor"}}',
171 'fr' => '{"libraryStrings": {"key": "valeur"}}',
176 'One library with 2 translations (es and fr) - fr' => [
178 'machinename' => 'Library1',
183 'es' => '{"libraryStrings": {"key": "valor"}}',
184 'fr' => '{"libraryStrings": {"key": "valeur"}}',
189 'One library with 2 translations (es and fr) - unexisting translation (de)' => [
191 'machinename' => 'Library1',
196 'es' => '{"libraryStrings": {"key": "valor"}}',
197 'fr' => '{"libraryStrings": {"key": "valeur"}}',
203 'One library with 3 translations (one of them English) - fr' => [
205 'machinename' => 'Library1',
210 'en' => '{"libraryStrings": {"key": "value"}}',
211 'es' => '{"libraryStrings": {"key": "valor"}}',
212 'fr' => '{"libraryStrings": {"key": "valeur"}}',
217 'One library with 3 translations (one of them English) - en' => [
219 'machinename' => 'Library1',
224 'en' => '{"libraryStrings": {"key": "value"}}',
225 'es' => '{"libraryStrings": {"key": "valor"}}',
226 'fr' => '{"libraryStrings": {"key": "valeur"}}',
235 * Test that the method getAvailableLanguages retrieves all the language available of a library.
237 * @dataProvider get_available_languages_provider
239 * @param array $datalib Library data to create
240 * @param array $expectedlangs Available languages expected.
241 * @param string $machinename The machine readable name of the library(content type)
242 * @param int $majorversion Major part of version number
243 * @param int $minorversion Minor part of version number
245 public function test_get_available_languages(array $datalib, ?
array $expectedlangs = null, ?
string $machinename = '',
246 ?
int $majorversion = 1, ?
int $minorversion = 0): void
{
247 $this->resetAfterTest(true);
250 $generator = \testing_util
::get_data_generator();
251 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
253 $h5pfilestorage = new file_storage();
254 $h5ptempath = $h5pfilestorage->getTmpPath();
258 if (array_key_exists('translation', $datalib)) {
259 $translations = $datalib['translation'];
261 // Create DB entry for this library.
262 $tmplib = $h5pgenerator->create_library_record($datalib['machinename'], $datalib['title'], $datalib['majorversion'],
263 $datalib['minorversion']);
264 // Create the files for this libray.
265 [$library, $files] = $h5pgenerator->create_library($h5ptempath, $tmplib->id
, $datalib['machinename'],
266 $datalib['majorversion'], $datalib['minorversion'], $translations);
267 $h5pfilestorage->saveLibrary($library);
269 if (empty($machinename)) {
270 $machinename = $datalib['machinename'];
272 if (empty($majorversion)) {
273 $majorversion = $datalib['majorversion'];
275 if (empty($minorversion)) {
276 $minorversion = $datalib['minorversion'];
280 // Get available languages.
281 $langs = $this->editorframework
->getAvailableLanguages($machinename, $majorversion, $minorversion);
283 $this->assertCount(count($expectedlangs), $langs);
284 $this->assertEquals(ksort($expectedlangs), ksort($langs));
288 * Data provider for test_get_available_languages().
292 public function get_available_languages_provider(): array {
301 'One library created but getting available from an unexisting one' => [
303 'machinename' => 'Library1',
308 'es' => '{"libraryStrings": {"key": "valor"}}',
309 'fr' => '{"libraryStrings": {"key": "valeur"}}',
317 'One library without any translation' => [
319 'machinename' => 'Library1',
326 'One library with 2 translations (es and fr)' => [
328 'machinename' => 'Library1',
333 'es' => '{"libraryStrings": {"key": "valor"}}',
334 'fr' => '{"libraryStrings": {"key": "valeur"}}',
339 'One library with 3 translations (one of them English)' => [
341 'machinename' => 'Library1',
346 'en' => '{"libraryStrings": {"key": "value"}}',
347 'es' => '{"libraryStrings": {"key": "valor"}}',
348 'fr' => '{"libraryStrings": {"key": "valeur"}}',
357 * Test that the method getLibraries get the specified libraries or all the content types (runnable = 1).
359 public function test_getLibraries(): void
{
360 $this->resetAfterTest(true);
362 $generator = \testing_util
::get_data_generator();
363 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
365 // Generate some h5p related data.
366 $data = $h5pgenerator->generate_h5p_data();
368 $expectedlibraries = [];
369 foreach ($data as $key => $value) {
370 if (isset($value->data
) && $value->data
->runnable
) {
371 $value->data
->name
= $value->data
->machinename
;
372 $value->data
->majorVersion
= $value->data
->majorversion
;
373 $value->data
->minorVersion
= $value->data
->minorversion
;
374 $expectedlibraries[$value->data
->title
] = $value->data
;
377 ksort($expectedlibraries);
379 // Get all libraries.
380 $libraries = $this->editorframework
->getLibraries();
381 foreach ($libraries as $library) {
382 $actuallibraries[] = $library->title
;
384 sort($actuallibraries);
386 $this->assertEquals(array_keys($expectedlibraries), $actuallibraries);
388 // Get a subset of libraries.
389 $librariessubset = array_slice($expectedlibraries, 0, 4);
391 $actuallibraries = [];
392 $libraries = $this->editorframework
->getLibraries($librariessubset);
393 foreach ($libraries as $library) {
394 $actuallibraries[] = $library->title
;
397 $this->assertEquals(array_keys($librariessubset), $actuallibraries);