9ae0ae28143e57bb1a3a244537f74199c74e4c2b
[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 validateEditorToken validates an existing token.
84      */
85     public function test_validateEditorToken(): void {
86         // The action param is not used at all.
87         $token = core::createToken('editorajax');
88         $wrongaction = core::createToken('wrongaction');
89         $badtoken = 'xkadfpuealkdjsflkajsñf';
91         $validtoken = $this->editorajax->validateEditorToken($token);
92         $invalidaction = $this->editorajax->validateEditorToken($wrongaction);
93         $invalidtoken = $this->editorajax->validateEditorToken($badtoken);
95         $this->assertTrue($validtoken);
96         $this->assertTrue($invalidaction);
97         $this->assertFalse($invalidtoken);
98     }
100     /**
101      * Test that the method getTranslations retrieves the translations of several libraries.
102      *
103      * @dataProvider  get_translations_provider
104      *
105      * @param  array  $datalibs      Libraries to create
106      * @param  string $lang          Language to get the translations
107      * @param  bool   $emptyexpected True if empty translations are expected; false otherwise
108      * @param  array  $altstringlibs When defined, libraries are no created and the content here is used to call the method
109      */
110     public function test_get_translations(array $datalibs, string $lang, bool $emptyexpected, ?array $altstringlibs = []): void {
111         $this->resetAfterTest();
113         // Fetch generator.
114         $generator = \testing_util::get_data_generator();
115         $h5pgenerator = $generator->get_plugin_generator('core_h5p');
117         $h5pfilestorage = new file_storage();
118         $h5ptempath = $h5pfilestorage->getTmpPath();
120         if (!empty($altstringlibs)) {
121             // Libraries won't be created and the getTranslation method will be called with this $altstringlibs.
122             $stringlibs = $altstringlibs;
123         } else {
124             $stringlibs = [];
125             foreach ($datalibs as $datalib) {
126                 // Create DB entry for this library.
127                 $tmplib = $h5pgenerator->create_library_record($datalib['machinename'], $datalib['title'], $datalib['majorversion'],
128                     $datalib['minorversion']);
129                 // Create the files for this libray.
130                 [$library, $files] = $h5pgenerator->create_library($h5ptempath, $tmplib->id, $datalib['machinename'],
131                     $datalib['majorversion'], $datalib['minorversion'], $datalib['translation']);
132                 $h5pfilestorage->saveLibrary($library);
133                 $stringlibs[] = \H5PCore::libraryToString($library);
134             }
135         }
137         $translations = $this->editorajax->getTranslations($stringlibs, $lang);
139         if ($emptyexpected) {
140             $this->assertEmpty($translations);
141         } else {
142             foreach ($translations as $stringlib => $translation) {
143                 $this->assertEquals($datalibs[$stringlib]['translation'][$lang], $translation);
144             }
145         }
146     }
148     /**
149      * Data provider for test_get_translations().
150      *
151      * @return array
152      */
153     public function get_translations_provider(): array {
154         return [
155             'No library' => [
156                 [],
157                 'es',
158                 true,
159                 ['Library1 1.2']
160             ],
161             'One library with existing translation (es)' => [
162                 [
163                     'Library1 1.2' => [
164                         'machinename' => 'Library1',
165                         'title' => 'Lib1',
166                         'majorversion' => 1,
167                         'minorversion' => 2,
168                         'translation' => [
169                             'es' => '{"libraryStrings": {"key": "valor"}}',
170                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
171                         ],
172                     ]
173                 ],
174                 'es',
175                 false
176             ],
177             'One library with existing translation (fr)' => [
178                 [
179                     'Library1 1.2' => [
180                         'machinename' => 'Library1',
181                         'title' => 'Lib1',
182                         'majorversion' => 1,
183                         'minorversion' => 2,
184                         'translation' => [
185                             'es' => '{"libraryStrings": {"key": "valor"}}',
186                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
187                         ],
188                     ]
189                 ],
190                 'fr',
191                 false
192             ],
193             'One library with unexisting translation (de)' => [
194                 [
195                     'Library1 1.2' => [
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                 ],
206                 'de',
207                 true
208             ],
209             'Two libraries with existing translation (es)' => [
210                 [
211                     'Library1 1.2' => [
212                         'machinename' => 'Library1',
213                         'title' => 'Lib1',
214                         'majorversion' => 1,
215                         'minorversion' => 2,
216                         'translation' => [
217                             'es' => '{"libraryStrings": {"key": "valor"}}',
218                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
219                         ],
220                     ],
221                     'Library2 3.4' => [
222                         'machinename' => 'Library2',
223                         'title' => 'Lib1',
224                         'majorversion' => 3,
225                         'minorversion' => 4,
226                         'translation' => [
227                             'es' => '{"libraryStrings": {"key": "valor"}}',
228                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
229                         ],
230                     ]
231                 ],
232                 'es',
233                 false
234             ],
235             'Two libraries with unexisting translation (de)' => [
236                 [
237                     'Library1 1.2' => [
238                         'machinename' => 'Library1',
239                         'title' => 'Lib1',
240                         'majorversion' => 1,
241                         'minorversion' => 2,
242                         'translation' => [
243                             'es' => '{"libraryStrings": {"key": "valor"}}',
244                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
245                         ],
246                     ],
247                     'Library2 3.4' => [
248                         'machinename' => 'Library2',
249                         'title' => 'Lib1',
250                         'majorversion' => 3,
251                         'minorversion' => 4,
252                         'translation' => [
253                             'es' => '{"libraryStrings": {"key": "valor"}}',
254                             'fr' => '{"libraryStrings": {"key": "valeur"}}',
255                         ],
256                     ]
257                 ],
258                 'de',
259                 true
260             ],
261         ];
262     }