Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
/**
18
 * Testing the H5peditorStorage interface implementation.
19
 *
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
24
 */
25
 
26
namespace core_h5p;
27
 
28
use core_h5p\local\library\autoloader;
29
 
30
/**
31
 *
32
 * Test class covering the H5peditorStorage interface implementation.
33
 *
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
37
 *
38
 * @runTestsInSeparateProcesses
39
 */
1441 ariadna 40
final class editor_framework_test extends \advanced_testcase {
1 efrain 41
 
42
    /** @var editor_framework H5P editor_framework instance */
43
    protected $editorframework;
44
 
45
    /**
46
     * Setup to ensure that fixtures are loaded.
47
     */
48
    public static function setupBeforeClass(): void {
49
        autoloader::register();
50
    }
51
 
52
    /**
53
     * Set up function for tests.
54
     */
55
    protected function setUp(): void {
56
        parent::setUp();
57
 
58
        $this->editorframework = new editor_framework();
59
    }
60
 
61
    /**
62
     * Test that the method getLanguage retrieves the translation of a library in the requested language.
63
     *
64
     * @dataProvider  get_language_provider
65
     *
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
72
     */
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);
76
 
77
        // Fetch generator.
78
        $generator = \testing_util::get_data_generator();
79
        $h5pgenerator = $generator->get_plugin_generator('core_h5p');
80
 
81
        $h5pfilestorage = new file_storage();
82
        $h5ptempath = $h5pfilestorage->getTmpPath();
83
 
84
        $expectedresult = '';
85
        if ($datalib) {
86
            $translations = [];
87
            if (array_key_exists('translation', $datalib)) {
88
                $translations = $datalib['translation'];
89
            }
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);
97
 
98
            // If machinename, majorversion or minorversion are empty, use the value in datalib.
99
            if (empty($machinename)) {
100
                $machinename = $datalib['machinename'];
101
            }
102
            if (empty($majorversion)) {
103
                $majorversion = $datalib['majorversion'];
104
            }
105
            if (empty($minorversion)) {
106
                $minorversion = $datalib['minorversion'];
107
            }
108
            if (!$emptyexpected && array_key_exists($lang, $translations)) {
109
                $expectedresult = $translations[$lang];
110
            }
111
        }
112
 
113
        // Get Language.
114
        $json = $this->editorframework->getLanguage($machinename, $majorversion, $minorversion, $lang);
115
 
116
        if ($emptyexpected) {
117
            $this->assertFalse($json);
118
        } else {
119
            $this->assertEquals($expectedresult, $json);
120
        }
121
    }
122
 
123
    /**
124
     * Data provider for test_get_language().
125
     *
126
     * @return array
127
     */
1441 ariadna 128
    public static function get_language_provider(): array {
1 efrain 129
        return [
130
            'No library' => [
131
                [],
132
                'en',
133
                true,
134
                'Library1',
135
                1,
136
                2,
137
            ],
138
            'One library created but getting translation from an unexisting one' => [
1441 ariadna 139
                // Library1 1.2.
140
                [
1 efrain 141
                    'machinename' => 'Library1',
142
                    'title' => 'Lib1',
143
                    'majorversion' => 1,
144
                    'minorversion' => 2,
145
                    'translation' => [
146
                        'es' => '{"libraryStrings": {"key": "valor"}}',
147
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
148
                    ],
149
                ],
150
                'es',
151
                true,
152
                'AnotherLibrary',
153
            ],
154
            'One library without any translation' => [
1441 ariadna 155
                // Library1 1.2.
156
                [
1 efrain 157
                    'machinename' => 'Library1',
158
                    'title' => 'Lib1',
159
                    'majorversion' => 1,
160
                    'minorversion' => 2,
161
                ],
162
                'es',
163
                true,
164
            ],
165
            'One library with 2 translations (es and fr) - es' => [
1441 ariadna 166
                // Library1 1.2.
167
                [
1 efrain 168
                    'machinename' => 'Library1',
169
                    'title' => 'Lib1',
170
                    'majorversion' => 1,
171
                    'minorversion' => 2,
172
                    'translation' => [
173
                        'es' => '{"libraryStrings": {"key": "valor"}}',
174
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
175
                    ],
176
                ],
177
                'es',
178
            ],
179
            'One library with 2 translations (es and fr) - fr' => [
1441 ariadna 180
                // Library1 1.2.
181
                [
1 efrain 182
                    'machinename' => 'Library1',
183
                    'title' => 'Lib1',
184
                    'majorversion' => 1,
185
                    'minorversion' => 2,
186
                    'translation' => [
187
                        'es' => '{"libraryStrings": {"key": "valor"}}',
188
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
189
                    ],
190
                ],
191
                'fr',
192
            ],
193
            'One library with 2 translations (es and fr) - unexisting translation (de)' => [
1441 ariadna 194
                // Library1 1.2.
195
                [
1 efrain 196
                    'machinename' => 'Library1',
197
                    'title' => 'Lib1',
198
                    'majorversion' => 1,
199
                    'minorversion' => 2,
200
                    'translation' => [
201
                        'es' => '{"libraryStrings": {"key": "valor"}}',
202
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
203
                    ],
204
                ],
205
                'de',
206
                true
207
            ],
208
            'One library with 3 translations (one of them English) - fr' => [
1441 ariadna 209
                // Library1 1.2.
210
                [
1 efrain 211
                    'machinename' => 'Library1',
212
                    'title' => 'Lib1',
213
                    'majorversion' => 1,
214
                    'minorversion' => 2,
215
                    'translation' => [
216
                        'en' => '{"libraryStrings": {"key": "value"}}',
217
                        'es' => '{"libraryStrings": {"key": "valor"}}',
218
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
219
                    ],
220
                ],
221
                'fr',
222
            ],
223
            'One library with 3 translations (one of them English) - en' => [
1441 ariadna 224
                // Library1 1.2.
225
                [
1 efrain 226
                    'machinename' => 'Library1',
227
                    'title' => 'Lib1',
228
                    'majorversion' => 1,
229
                    'minorversion' => 2,
230
                    'translation' => [
231
                        'en' => '{"libraryStrings": {"key": "value"}}',
232
                        'es' => '{"libraryStrings": {"key": "valor"}}',
233
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
234
                    ],
235
                ],
236
                'en',
237
            ],
238
        ];
239
    }
240
 
241
    /**
242
     * Test that the method getAvailableLanguages retrieves all the language available of a library.
243
     *
244
     * @dataProvider  get_available_languages_provider
245
     *
246
     * @param  array  $datalib        Library data to create
247
     * @param  array  $expectedlangs  Available languages expected.
248
     * @param  string $machinename    The machine readable name of the library(content type)
249
     * @param  int    $majorversion   Major part of version number
250
     * @param  int    $minorversion   Minor part of version number
251
     */
252
    public function test_get_available_languages(array $datalib, ?array $expectedlangs = null, ?string $machinename = '',
253
            ?int $majorversion = 1, ?int $minorversion = 0): void {
254
        $this->resetAfterTest(true);
255
 
256
        // Fetch generator.
257
        $generator = \testing_util::get_data_generator();
258
        $h5pgenerator = $generator->get_plugin_generator('core_h5p');
259
 
260
        $h5pfilestorage = new file_storage();
261
        $h5ptempath = $h5pfilestorage->getTmpPath();
262
 
263
        $translations = [];
264
        if ($datalib) {
265
            if (array_key_exists('translation', $datalib)) {
266
                $translations = $datalib['translation'];
267
            }
268
            // Create DB entry for this library.
269
            $tmplib = $h5pgenerator->create_library_record($datalib['machinename'], $datalib['title'], $datalib['majorversion'],
270
                $datalib['minorversion']);
271
            // Create the files for this libray.
272
            [$library, $files] = $h5pgenerator->create_library($h5ptempath, $tmplib->id, $datalib['machinename'],
273
                $datalib['majorversion'], $datalib['minorversion'], $translations);
274
            $h5pfilestorage->saveLibrary($library);
275
 
276
            if (empty($machinename)) {
277
                $machinename = $datalib['machinename'];
278
            }
279
            if (empty($majorversion)) {
280
                $majorversion = $datalib['majorversion'];
281
            }
282
            if (empty($minorversion)) {
283
                $minorversion = $datalib['minorversion'];
284
            }
285
        }
286
 
287
        // Get available languages.
288
        $langs = $this->editorframework->getAvailableLanguages($machinename, $majorversion, $minorversion);
289
 
290
        $this->assertCount(count($expectedlangs), $langs);
291
        $this->assertEquals(ksort($expectedlangs), ksort($langs));
292
    }
293
 
294
    /**
295
     * Data provider for test_get_available_languages().
296
     *
297
     * @return array
298
     */
1441 ariadna 299
    public static function get_available_languages_provider(): array {
1 efrain 300
        return [
301
            'No library' => [
302
                [],
303
                [],
304
                'Library1',
305
                1,
306
                2,
307
            ],
308
            'One library created but getting available from an unexisting one' => [
1441 ariadna 309
                // Library1 1.2.
310
                [
1 efrain 311
                    'machinename' => 'Library1',
312
                    'title' => 'Lib1',
313
                    'majorversion' => 1,
314
                    'minorversion' => 2,
315
                    'translation' => [
316
                        'es' => '{"libraryStrings": {"key": "valor"}}',
317
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
318
                    ],
319
                ],
320
                [],
321
                'Library2',
322
                1,
323
                2,
324
            ],
325
            'One library without any translation' => [
1441 ariadna 326
                // Library1 1.2.
327
                [
1 efrain 328
                    'machinename' => 'Library1',
329
                    'title' => 'Lib1',
330
                    'majorversion' => 1,
331
                    'minorversion' => 2,
332
                ],
333
                ['en'],
334
            ],
335
            'One library with 2 translations (es and fr)' => [
1441 ariadna 336
                // Library1 1.2.
337
                [
1 efrain 338
                    'machinename' => 'Library1',
339
                    'title' => 'Lib1',
340
                    'majorversion' => 1,
341
                    'minorversion' => 2,
342
                    'translation' => [
343
                        'es' => '{"libraryStrings": {"key": "valor"}}',
344
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
345
                    ],
346
                ],
347
                ['en', 'es', 'fr'],
348
            ],
349
            'One library with 3 translations (one of them English)' => [
1441 ariadna 350
                // Library1 1.2.
351
                [
1 efrain 352
                    'machinename' => 'Library1',
353
                    'title' => 'Lib1',
354
                    'majorversion' => 1,
355
                    'minorversion' => 2,
356
                    'translation' => [
357
                        'en' => '{"libraryStrings": {"key": "value"}}',
358
                        'es' => '{"libraryStrings": {"key": "valor"}}',
359
                        'fr' => '{"libraryStrings": {"key": "valeur"}}',
360
                    ],
361
                ],
362
                ['en', 'es', 'fr'],
363
            ],
364
        ];
365
    }
366
 
367
    /**
368
     * Test that the method getLibraries get the specified libraries or all the content types (runnable = 1).
369
     */
370
    public function test_getLibraries(): void {
371
        $this->resetAfterTest(true);
372
 
373
        $generator = \testing_util::get_data_generator();
374
        $h5pgenerator = $generator->get_plugin_generator('core_h5p');
375
 
376
        // Generate some h5p related data.
377
        $data = $h5pgenerator->generate_h5p_data();
378
 
379
        $expectedlibraries = [];
380
        foreach ($data as $key => $value) {
381
            if (isset($value->data) && $value->data->runnable) {
382
                $value->data->name = $value->data->machinename;
383
                $value->data->majorVersion = $value->data->majorversion;
384
                $value->data->minorVersion = $value->data->minorversion;
385
                $expectedlibraries[$value->data->title] = $value->data;
386
            }
387
        }
388
        ksort($expectedlibraries);
389
 
390
        // Get all libraries.
391
        $libraries = $this->editorframework->getLibraries();
392
        foreach ($libraries as $library) {
393
            $actuallibraries[] = $library->title;
394
        }
395
        sort($actuallibraries);
396
 
397
        $this->assertEquals(array_keys($expectedlibraries), $actuallibraries);
398
 
399
        // Get a subset of libraries.
400
        $librariessubset = array_slice($expectedlibraries, 0, 4);
401
 
402
        $actuallibraries = [];
403
        $libraries = $this->editorframework->getLibraries($librariessubset);
404
        foreach ($libraries as $library) {
405
            $actuallibraries[] = $library->title;
406
        }
407
 
408
        $this->assertEquals(array_keys($librariessubset), $actuallibraries);
409
    }
410
}