MDL-68641 core_h5p: test dropped because the tested method was deleted
[moodle.git] / h5p / tests / editor_ajax_test.php
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/>.
17 /**
18  * Testing the H5PEditorAjaxInterface 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  */
26 namespace core_h5p;
28 use core_h5p\local\library\autoloader;
29 use ReflectionMethod;
31 /**
32  *
33  * Test class covering the H5PEditorAjaxInterface interface implementation.
34  *
35  * @package    core_h5p
36  * @copyright  2020 Victor Deniz <victor@moodle.com>
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  *
39  * @runTestsInSeparateProcesses
40  */
41 class editor_ajax_testcase extends \advanced_testcase {
43     /** @var editor_ajax H5P editor ajax instance */
44     protected $editorajax;
46     /**
47      * Set up function for tests.
48      */
49     protected function setUp() {
50         parent::setUp();
52         autoloader::register();
54         $this->editorajax = new editor_ajax();
55     }
57     /**
58      * Test that getLatestLibraryVersions method retrieves the latest installed library versions.
59      */
60     public function test_getLatestLibraryVersions(): void {
61         $this->resetAfterTest();
63         $generator = \testing_util::get_data_generator();
64         $h5pgenerator = $generator->get_plugin_generator('core_h5p');
66         // Create several libraries records.
67         $h5pgenerator->create_library_record('Library1', 'Lib1', 2, 0);
68         $lib2 = $h5pgenerator->create_library_record('Library2', 'Lib2', 2, 1);
69         $expectedlibraries[] = $lib2->id;
70         $lib3 = $h5pgenerator->create_library_record('Library3', 'Lib3', 1, 3);
71         $expectedlibraries[] = $lib3->id;
72         $h5pgenerator->create_library_record('Library1', 'Lib1', 2, 1);
73         $lib12 = $h5pgenerator->create_library_record('Library1', 'Lib1', 3, 0);
74         $expectedlibraries[] = $lib12->id;
76         $actuallibraries = $this->editorajax->getLatestLibraryVersions();
77         ksort($actuallibraries);
79         $this->assertEquals($expectedlibraries, array_keys($actuallibraries));
80     }
82     /**
83      * Test that the method getTranslations retrieves the translations of several libraries.
84      *
85      * @dataProvider  get_translations_provider
86      *
87      * @param  array  $datalibs      Libraries to create
88      * @param  string $lang          Language to get the translations
89      * @param  bool   $emptyexpected True if empty translations are expected; false otherwise
90      * @param  array  $altstringlibs When defined, libraries are no created and the content here is used to call the method
91      */
92     public function test_get_translations(array $datalibs, string $lang, bool $emptyexpected, ?array $altstringlibs = []): void {
93         $this->resetAfterTest();
95         // Fetch generator.
96         $generator = \testing_util::get_data_generator();
97         $h5pgenerator = $generator->get_plugin_generator('core_h5p');
99         $h5pfilestorage = new file_storage();
100         $h5ptempath = $h5pfilestorage->getTmpPath();
102         if (!empty($altstringlibs)) {
103             // Libraries won't be created and the getTranslation method will be called with this $altstringlibs.
104             $stringlibs = $altstringlibs;
105         } else {
106             $stringlibs = [];
107             foreach ($datalibs as $datalib) {
108                 // Create DB entry for this library.
109                 $tmplib = $h5pgenerator->create_library_record($datalib['machinename'], $datalib['title'], $datalib['majorversion'],
110                     $datalib['minorversion']);
111                 // Create the files for this libray.
112                 [$library, $files] = $h5pgenerator->create_library($h5ptempath, $tmplib->id, $datalib['machinename'],
113                     $datalib['majorversion'], $datalib['minorversion'], $datalib['translation']);
114                 $h5pfilestorage->saveLibrary($library);
115                 $stringlibs[] = \H5PCore::libraryToString($library);
116             }
117         }
119         $translations = $this->editorajax->getTranslations($stringlibs, $lang);
121         if ($emptyexpected) {
122             $this->assertEmpty($translations);
123         } else {
124             foreach ($translations as $stringlib => $translation) {
125                 $this->assertEquals($datalibs[$stringlib]['translation'][$lang], $translation);
126             }
127         }
128     }
130     /**
131      * Data provider for test_get_translations().
132      *
133      * @return array
134      */
135     public function get_translations_provider(): array {
136         return [
137             'No library' => [
138                 [],
139                 'es',
140                 true,
141                 ['Library1 1.2']
142             ],
143             'One library with existing translation (es)' => [
144                 [
145                     'Library1 1.2' => [
146                         'machinename' => 'Library1',
147                         'title' => 'Lib1',
148                         'majorversion' => 1,
149                         'minorversion' => 2,
150                         'translation' => [
151                             'es' => '{"libraryStrings": {"key": "valor"}}',
152                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
153                         ],
154                     ]
155                 ],
156                 'es',
157                 false
158             ],
159             'One library with existing translation (fr)' => [
160                 [
161                     'Library1 1.2' => [
162                         'machinename' => 'Library1',
163                         'title' => 'Lib1',
164                         'majorversion' => 1,
165                         'minorversion' => 2,
166                         'translation' => [
167                             'es' => '{"libraryStrings": {"key": "valor"}}',
168                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
169                         ],
170                     ]
171                 ],
172                 'fr',
173                 false
174             ],
175             'One library with unexisting translation (de)' => [
176                 [
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                 ],
188                 'de',
189                 true
190             ],
191             'Two libraries with existing translation (es)' => [
192                 [
193                     'Library1 1.2' => [
194                         'machinename' => 'Library1',
195                         'title' => 'Lib1',
196                         'majorversion' => 1,
197                         'minorversion' => 2,
198                         'translation' => [
199                             'es' => '{"libraryStrings": {"key": "valor"}}',
200                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
201                         ],
202                     ],
203                     'Library2 3.4' => [
204                         'machinename' => 'Library2',
205                         'title' => 'Lib1',
206                         'majorversion' => 3,
207                         'minorversion' => 4,
208                         'translation' => [
209                             'es' => '{"libraryStrings": {"key": "valor"}}',
210                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
211                         ],
212                     ]
213                 ],
214                 'es',
215                 false
216             ],
217             'Two libraries with unexisting translation (de)' => [
218                 [
219                     'Library1 1.2' => [
220                         'machinename' => 'Library1',
221                         'title' => 'Lib1',
222                         'majorversion' => 1,
223                         'minorversion' => 2,
224                         'translation' => [
225                             'es' => '{"libraryStrings": {"key": "valor"}}',
226                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
227                         ],
228                     ],
229                     'Library2 3.4' => [
230                         'machinename' => 'Library2',
231                         'title' => 'Lib1',
232                         'majorversion' => 3,
233                         'minorversion' => 4,
234                         'translation' => [
235                             'es' => '{"libraryStrings": {"key": "valor"}}',
236                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
237                         ],
238                     ]
239                 ],
240                 'de',
241                 true
242             ],
243         ];
244     }