Differences Between: [Versions 39 and 311]
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 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 */ 25 26 namespace core_h5p; 27 28 use core_h5p\local\library\autoloader; 29 use Moodle\H5PCore; 30 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_test extends \advanced_testcase { 42 43 /** @var editor_ajax H5P editor ajax instance */ 44 protected $editorajax; 45 46 /** 47 * Set up function for tests. 48 */ 49 protected function setUp(): void { 50 parent::setUp(); 51 52 autoloader::register(); 53 54 $this->editorajax = new editor_ajax(); 55 } 56 57 /** 58 * Test that getLatestLibraryVersions method retrieves the latest installed library versions. 59 */ 60 public function test_getLatestLibraryVersions(): void { 61 $this->resetAfterTest(); 62 63 $generator = \testing_util::get_data_generator(); 64 $h5pgenerator = $generator->get_plugin_generator('core_h5p'); 65 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; 75 76 $actuallibraries = $this->editorajax->getLatestLibraryVersions(); 77 ksort($actuallibraries); 78 79 $this->assertEquals($expectedlibraries, array_keys($actuallibraries)); 80 } 81 82 /** 83 * Test that getContentTypeCache method retrieves the latest library versions that exists locally. 84 */ 85 public function test_getContentTypeCache(): void { 86 $this->resetAfterTest(); 87 88 $h5pgenerator = \testing_util::get_data_generator()->get_plugin_generator('core_h5p'); 89 90 // Create several libraries records. 91 $lib1 = $h5pgenerator->create_library_record('Library1', 'Lib1', 1, 0, 1, '', null, 'http://tutorial.org', 92 'http://example.org'); 93 $lib2 = $h5pgenerator->create_library_record('Library2', 'Lib2', 2, 0, 1, '', null, 'http://tutorial.org'); 94 $lib3 = $h5pgenerator->create_library_record('Library3', 'Lib3', 3, 0); 95 $libs = [$lib1, $lib2, $lib3]; 96 97 $libraries = $this->editorajax->getContentTypeCache(); 98 $this->assertCount(3, $libraries); 99 foreach ($libs as $lib) { 100 $library = $libraries[$lib->id]; 101 $this->assertEquals($library->id, $lib->id); 102 $this->assertEquals($library->machine_name, $lib->machinename); 103 $this->assertEquals($library->major_version, $lib->majorversion); 104 $this->assertEquals($library->tutorial, $lib->tutorial); 105 $this->assertEquals($library->example, $lib->example); 106 $this->assertEquals($library->is_recommended, 0); 107 $this->assertEquals($library->summary, ''); 108 } 109 } 110 111 /** 112 * Test that the method getTranslations retrieves the translations of several libraries. 113 * 114 * @dataProvider get_translations_provider 115 * 116 * @param array $datalibs Libraries to create 117 * @param string $lang Language to get the translations 118 * @param bool $emptyexpected True if empty translations are expected; false otherwise 119 * @param array $altstringlibs When defined, libraries are no created and the content here is used to call the method 120 */ 121 public function test_get_translations(array $datalibs, string $lang, bool $emptyexpected, ?array $altstringlibs = []): void { 122 $this->resetAfterTest(); 123 124 // Fetch generator. 125 $generator = \testing_util::get_data_generator(); 126 $h5pgenerator = $generator->get_plugin_generator('core_h5p'); 127 128 $h5pfilestorage = new file_storage(); 129 $h5ptempath = $h5pfilestorage->getTmpPath(); 130 131 if (!empty($altstringlibs)) { 132 // Libraries won't be created and the getTranslation method will be called with this $altstringlibs. 133 $stringlibs = $altstringlibs; 134 } else { 135 $stringlibs = []; 136 foreach ($datalibs as $datalib) { 137 // Create DB entry for this library. 138 $tmplib = $h5pgenerator->create_library_record($datalib['machinename'], $datalib['title'], $datalib['majorversion'], 139 $datalib['minorversion']); 140 // Create the files for this libray. 141 [$library, $files] = $h5pgenerator->create_library($h5ptempath, $tmplib->id, $datalib['machinename'], 142 $datalib['majorversion'], $datalib['minorversion'], $datalib['translation']); 143 $h5pfilestorage->saveLibrary($library); 144 $stringlibs[] = H5PCore::libraryToString($library); 145 } 146 } 147 148 $translations = $this->editorajax->getTranslations($stringlibs, $lang); 149 150 if ($emptyexpected) { 151 $this->assertEmpty($translations); 152 } else { 153 foreach ($translations as $stringlib => $translation) { 154 $this->assertEquals($datalibs[$stringlib]['translation'][$lang], $translation); 155 } 156 } 157 } 158 159 /** 160 * Data provider for test_get_translations(). 161 * 162 * @return array 163 */ 164 public function get_translations_provider(): array { 165 return [ 166 'No library' => [ 167 [], 168 'es', 169 true, 170 ['Library1 1.2'] 171 ], 172 'One library with existing translation (es)' => [ 173 [ 174 'Library1 1.2' => [ 175 'machinename' => 'Library1', 176 'title' => 'Lib1', 177 'majorversion' => 1, 178 'minorversion' => 2, 179 'translation' => [ 180 'es' => '{"libraryStrings": {"key": "valor"}}', 181 'fr' => '{"libraryStrings": {"key": "valeur"}}', 182 ], 183 ] 184 ], 185 'es', 186 false 187 ], 188 'One library with existing translation (fr)' => [ 189 [ 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 ], 201 'fr', 202 false 203 ], 204 'One library with unexisting translation (de)' => [ 205 [ 206 'Library1 1.2' => [ 207 'machinename' => 'Library1', 208 'title' => 'Lib1', 209 'majorversion' => 1, 210 'minorversion' => 2, 211 'translation' => [ 212 'es' => '{"libraryStrings": {"key": "valor"}}', 213 'fr' => '{"libraryStrings": {"key": "valeur"}}', 214 ], 215 ] 216 ], 217 'de', 218 true 219 ], 220 'Two libraries with existing translation (es)' => [ 221 [ 222 'Library1 1.2' => [ 223 'machinename' => 'Library1', 224 'title' => 'Lib1', 225 'majorversion' => 1, 226 'minorversion' => 2, 227 'translation' => [ 228 'es' => '{"libraryStrings": {"key": "valor"}}', 229 'fr' => '{"libraryStrings": {"key": "valeur"}}', 230 ], 231 ], 232 'Library2 3.4' => [ 233 'machinename' => 'Library2', 234 'title' => 'Lib1', 235 'majorversion' => 3, 236 'minorversion' => 4, 237 'translation' => [ 238 'es' => '{"libraryStrings": {"key": "valor"}}', 239 'fr' => '{"libraryStrings": {"key": "valeur"}}', 240 ], 241 ] 242 ], 243 'es', 244 false 245 ], 246 'Two libraries with unexisting translation (de)' => [ 247 [ 248 'Library1 1.2' => [ 249 'machinename' => 'Library1', 250 'title' => 'Lib1', 251 'majorversion' => 1, 252 'minorversion' => 2, 253 'translation' => [ 254 'es' => '{"libraryStrings": {"key": "valor"}}', 255 'fr' => '{"libraryStrings": {"key": "valeur"}}', 256 ], 257 ], 258 'Library2 3.4' => [ 259 'machinename' => 'Library2', 260 'title' => 'Lib1', 261 'majorversion' => 3, 262 'minorversion' => 4, 263 'translation' => [ 264 'es' => '{"libraryStrings": {"key": "valor"}}', 265 'fr' => '{"libraryStrings": {"key": "valeur"}}', 266 ], 267 ] 268 ], 269 'de', 270 true 271 ], 272 ]; 273 } 274 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body