Proyectos de Subversion Moodle

Rev

| 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
 */
40
class editor_framework_test extends \advanced_testcase {
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
     */
128
    public function get_language_provider(): array {
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' => [
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"}}',
147
                    ],
148
                ],
149
                'es',
150
                true,
151
                'AnotherLibrary',
152
            ],
153
            'One library without any translation' => [
154
                'Library1 1.2' => [
155
                    'machinename' => 'Library1',
156
                    'title' => 'Lib1',
157
                    'majorversion' => 1,
158
                    'minorversion' => 2,
159
                ],
160
                'es',
161
                true,
162
            ],
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"}}',
172
                    ],
173
                ],
174
                'es',
175
            ],
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"}}',
185
                    ],
186
                ],
187
                'fr',
188
            ],
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"}}',
198
                    ],
199
                ],
200
                'de',
201
                true
202
            ],
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"}}',
213
                    ],
214
                ],
215
                'fr',
216
            ],
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"}}',
227
                    ],
228
                ],
229
                'en',
230
            ],
231
        ];
232
    }
233
 
234
    /**
235
     * Test that the method getAvailableLanguages retrieves all the language available of a library.
236
     *
237
     * @dataProvider  get_available_languages_provider
238
     *
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
244
     */
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);
248
 
249
        // Fetch generator.
250
        $generator = \testing_util::get_data_generator();
251
        $h5pgenerator = $generator->get_plugin_generator('core_h5p');
252
 
253
        $h5pfilestorage = new file_storage();
254
        $h5ptempath = $h5pfilestorage->getTmpPath();
255
 
256
        $translations = [];
257
        if ($datalib) {
258
            if (array_key_exists('translation', $datalib)) {
259
                $translations = $datalib['translation'];
260
            }
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);
268
 
269
            if (empty($machinename)) {
270
                $machinename = $datalib['machinename'];
271
            }
272
            if (empty($majorversion)) {
273
                $majorversion = $datalib['majorversion'];
274
            }
275
            if (empty($minorversion)) {
276
                $minorversion = $datalib['minorversion'];
277
            }
278
        }
279
 
280
        // Get available languages.
281
        $langs = $this->editorframework->getAvailableLanguages($machinename, $majorversion, $minorversion);
282
 
283
        $this->assertCount(count($expectedlangs), $langs);
284
        $this->assertEquals(ksort($expectedlangs), ksort($langs));
285
    }
286
 
287
    /**
288
     * Data provider for test_get_available_languages().
289
     *
290
     * @return array
291
     */
292
    public function get_available_languages_provider(): array {
293
        return [
294
            'No library' => [
295
                [],
296
                [],
297
                'Library1',
298
                1,
299
                2,
300
            ],
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"}}',
310
                    ],
311
                ],
312
                [],
313
                'Library2',
314
                1,
315
                2,
316
            ],
317
            'One library without any translation' => [
318
                'Library1 1.2' => [
319
                    'machinename' => 'Library1',
320
                    'title' => 'Lib1',
321
                    'majorversion' => 1,
322
                    'minorversion' => 2,
323
                ],
324
                ['en'],
325
            ],
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"}}',
335
                    ],
336
                ],
337
                ['en', 'es', 'fr'],
338
            ],
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"}}',
349
                    ],
350
                ],
351
                ['en', 'es', 'fr'],
352
            ],
353
        ];
354
    }
355
 
356
    /**
357
     * Test that the method getLibraries get the specified libraries or all the content types (runnable = 1).
358
     */
359
    public function test_getLibraries(): void {
360
        $this->resetAfterTest(true);
361
 
362
        $generator = \testing_util::get_data_generator();
363
        $h5pgenerator = $generator->get_plugin_generator('core_h5p');
364
 
365
        // Generate some h5p related data.
366
        $data = $h5pgenerator->generate_h5p_data();
367
 
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;
375
            }
376
        }
377
        ksort($expectedlibraries);
378
 
379
        // Get all libraries.
380
        $libraries = $this->editorframework->getLibraries();
381
        foreach ($libraries as $library) {
382
            $actuallibraries[] = $library->title;
383
        }
384
        sort($actuallibraries);
385
 
386
        $this->assertEquals(array_keys($expectedlibraries), $actuallibraries);
387
 
388
        // Get a subset of libraries.
389
        $librariessubset = array_slice($expectedlibraries, 0, 4);
390
 
391
        $actuallibraries = [];
392
        $libraries = $this->editorframework->getLibraries($librariessubset);
393
        foreach ($libraries as $library) {
394
            $actuallibraries[] = $library->title;
395
        }
396
 
397
        $this->assertEquals(array_keys($librariessubset), $actuallibraries);
398
    }
399
}