weekly release 5.0dev
[moodle.git] / h5p / tests / editor_framework_test.php
blob1c93f87beb5ef2b77b91caa2881f7edb4a28ea84
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Testing the H5peditorStorage interface implementation.
20 * @package core_h5p
21 * @category test
22 * @copyright 2020 Victor Deniz <victor@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core_h5p;
28 use core_h5p\local\library\autoloader;
30 /**
32 * Test class covering the H5peditorStorage interface implementation.
34 * @package core_h5p
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;
45 /**
46 * Setup to ensure that fixtures are loaded.
48 public static function setupBeforeClass(): void {
49 autoloader::register();
52 /**
53 * Set up function for tests.
55 protected function setUp(): void {
56 parent::setUp();
58 $this->editorframework = new editor_framework();
61 /**
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);
77 // Fetch generator.
78 $generator = \testing_util::get_data_generator();
79 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
81 $h5pfilestorage = new file_storage();
82 $h5ptempath = $h5pfilestorage->getTmpPath();
84 $expectedresult = '';
85 if ($datalib) {
86 $translations = [];
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];
113 // Get Language.
114 $json = $this->editorframework->getLanguage($machinename, $majorversion, $minorversion, $lang);
116 if ($emptyexpected) {
117 $this->assertFalse($json);
118 } else {
119 $this->assertEquals($expectedresult, $json);
124 * Data provider for test_get_language().
126 * @return array
128 public function get_language_provider(): array {
129 return [
130 'No library' => [
132 'en',
133 true,
134 'Library1',
138 'One library created but getting translation from an unexisting one' => [
139 'Library1 1.2' => [
140 'machinename' => 'Library1',
141 'title' => 'Lib1',
142 'majorversion' => 1,
143 'minorversion' => 2,
144 'translation' => [
145 'es' => '{"libraryStrings": {"key": "valor"}}',
146 'fr' => '{"libraryStrings": {"key": "valeur"}}',
149 'es',
150 true,
151 'AnotherLibrary',
153 'One library without any translation' => [
154 'Library1 1.2' => [
155 'machinename' => 'Library1',
156 'title' => 'Lib1',
157 'majorversion' => 1,
158 'minorversion' => 2,
160 'es',
161 true,
163 'One library with 2 translations (es and fr) - es' => [
164 'Library1 1.2' => [
165 'machinename' => 'Library1',
166 'title' => 'Lib1',
167 'majorversion' => 1,
168 'minorversion' => 2,
169 'translation' => [
170 'es' => '{"libraryStrings": {"key": "valor"}}',
171 'fr' => '{"libraryStrings": {"key": "valeur"}}',
174 'es',
176 'One library with 2 translations (es and fr) - fr' => [
177 'Library1 1.2' => [
178 'machinename' => 'Library1',
179 'title' => 'Lib1',
180 'majorversion' => 1,
181 'minorversion' => 2,
182 'translation' => [
183 'es' => '{"libraryStrings": {"key": "valor"}}',
184 'fr' => '{"libraryStrings": {"key": "valeur"}}',
187 'fr',
189 'One library with 2 translations (es and fr) - unexisting translation (de)' => [
190 'Library1 1.2' => [
191 'machinename' => 'Library1',
192 'title' => 'Lib1',
193 'majorversion' => 1,
194 'minorversion' => 2,
195 'translation' => [
196 'es' => '{"libraryStrings": {"key": "valor"}}',
197 'fr' => '{"libraryStrings": {"key": "valeur"}}',
200 'de',
201 true
203 'One library with 3 translations (one of them English) - fr' => [
204 'Library1 1.2' => [
205 'machinename' => 'Library1',
206 'title' => 'Lib1',
207 'majorversion' => 1,
208 'minorversion' => 2,
209 'translation' => [
210 'en' => '{"libraryStrings": {"key": "value"}}',
211 'es' => '{"libraryStrings": {"key": "valor"}}',
212 'fr' => '{"libraryStrings": {"key": "valeur"}}',
215 'fr',
217 'One library with 3 translations (one of them English) - en' => [
218 'Library1 1.2' => [
219 'machinename' => 'Library1',
220 'title' => 'Lib1',
221 'majorversion' => 1,
222 'minorversion' => 2,
223 'translation' => [
224 'en' => '{"libraryStrings": {"key": "value"}}',
225 'es' => '{"libraryStrings": {"key": "valor"}}',
226 'fr' => '{"libraryStrings": {"key": "valeur"}}',
229 'en',
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);
249 // Fetch generator.
250 $generator = \testing_util::get_data_generator();
251 $h5pgenerator = $generator->get_plugin_generator('core_h5p');
253 $h5pfilestorage = new file_storage();
254 $h5ptempath = $h5pfilestorage->getTmpPath();
256 $translations = [];
257 if ($datalib) {
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().
290 * @return array
292 public function get_available_languages_provider(): array {
293 return [
294 'No library' => [
297 'Library1',
301 'One library created but getting available from an unexisting one' => [
302 'Library1 1.2' => [
303 'machinename' => 'Library1',
304 'title' => 'Lib1',
305 'majorversion' => 1,
306 'minorversion' => 2,
307 'translation' => [
308 'es' => '{"libraryStrings": {"key": "valor"}}',
309 'fr' => '{"libraryStrings": {"key": "valeur"}}',
313 'Library2',
317 'One library without any translation' => [
318 'Library1 1.2' => [
319 'machinename' => 'Library1',
320 'title' => 'Lib1',
321 'majorversion' => 1,
322 'minorversion' => 2,
324 ['en'],
326 'One library with 2 translations (es and fr)' => [
327 'Library1 1.2' => [
328 'machinename' => 'Library1',
329 'title' => 'Lib1',
330 'majorversion' => 1,
331 'minorversion' => 2,
332 'translation' => [
333 'es' => '{"libraryStrings": {"key": "valor"}}',
334 'fr' => '{"libraryStrings": {"key": "valeur"}}',
337 ['en', 'es', 'fr'],
339 'One library with 3 translations (one of them English)' => [
340 'Library1 1.2' => [
341 'machinename' => 'Library1',
342 'title' => 'Lib1',
343 'majorversion' => 1,
344 'minorversion' => 2,
345 'translation' => [
346 'en' => '{"libraryStrings": {"key": "value"}}',
347 'es' => '{"libraryStrings": {"key": "valor"}}',
348 'fr' => '{"libraryStrings": {"key": "valeur"}}',
351 ['en', 'es', 'fr'],
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);