See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 namespace core_h5p; 18 19 use core_h5p\local\library\autoloader; 20 21 /** 22 * Test class covering the h5p data generator class. 23 * 24 * @package core_h5p 25 * @category test 26 * @copyright 2019 Mihail Geshoski <mihail@moodle.com> 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 * @runTestsInSeparateProcesses 29 */ 30 class generator_testcase extends \advanced_testcase { 31 32 /** 33 * Tests set up. 34 */ 35 protected function setUp() { 36 parent::setUp(); 37 38 autoloader::register(); 39 } 40 41 /** 42 * Test the returned data of generate_h5p_data() when the method is called without requesting 43 * creation of library files. 44 */ 45 public function test_generate_h5p_data_no_files_created_return_data() { 46 global $DB; 47 48 $this->resetAfterTest(); 49 50 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 51 52 $data = $generator->generate_h5p_data(); 53 54 $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']); 55 $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']); 56 $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']); 57 $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']); 58 $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']); 59 $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']); 60 61 $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]); 62 63 $expected = (object) [ 64 'h5pcontent' => (object) array( 65 'h5pid' => $h5p->id, 66 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4) 67 ), 68 'mainlib' => (object) array( 69 'data' => $mainlib, 70 'dependencies' => array($lib1, $lib2, $lib3) 71 ), 72 'lib1' => (object) array( 73 'data' => $lib1, 74 'dependencies' => array($lib2, $lib3, $lib4) 75 ), 76 'lib2' => (object) array( 77 'data' => $lib2, 78 'dependencies' => array() 79 ), 80 'lib3' => (object) array( 81 'data' => $lib3, 82 'dependencies' => array($lib5) 83 ), 84 'lib4' => (object) array( 85 'data' => $lib4, 86 'dependencies' => array() 87 ), 88 'lib5' => (object) array( 89 'data' => $lib5, 90 'dependencies' => array() 91 ), 92 ]; 93 94 $this->assertEquals($expected, $data); 95 } 96 97 /** 98 * Test the returned data of generate_h5p_data() when the method requests 99 * creation of library files. 100 */ 101 public function test_generate_h5p_data_files_created_return_data() { 102 global $DB; 103 104 $this->resetAfterTest(); 105 106 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 107 108 $data = $generator->generate_h5p_data(true); 109 110 $mainlib = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']); 111 $lib1 = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']); 112 $lib2 = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']); 113 $lib3 = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']); 114 $lib4 = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']); 115 $lib5 = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']); 116 117 $h5p = $DB->get_record('h5p', ['mainlibraryid' => $mainlib->id]); 118 119 $expected = (object) [ 120 'h5pcontent' => (object) array( 121 'h5pid' => $h5p->id, 122 'contentdependencies' => array($mainlib, $lib1, $lib2, $lib3, $lib4) 123 ), 124 'mainlib' => (object) array( 125 'data' => $mainlib, 126 'dependencies' => array($lib1, $lib2, $lib3) 127 ), 128 'lib1' => (object) array( 129 'data' => $lib1, 130 'dependencies' => array($lib2, $lib3, $lib4) 131 ), 132 'lib2' => (object) array( 133 'data' => $lib2, 134 'dependencies' => array() 135 ), 136 'lib3' => (object) array( 137 'data' => $lib3, 138 'dependencies' => array($lib5) 139 ), 140 'lib4' => (object) array( 141 'data' => $lib4, 142 'dependencies' => array() 143 ), 144 'lib5' => (object) array( 145 'data' => $lib5, 146 'dependencies' => array() 147 ), 148 ]; 149 150 $this->assertEquals($expected, $data); 151 } 152 153 /** 154 * Test the behaviour of generate_h5p_data(). Test whether library files are created or not 155 * on filesystem depending what the method defines. 156 * 157 * @dataProvider generate_h5p_data_files_creation_provider 158 * @param bool $createlibraryfiles Whether to create library files on the filesystem 159 * @param bool $expected The expectation whether the files have been created or not 160 **/ 161 public function test_generate_h5p_data_files_creation(bool $createlibraryfiles, bool $expected) { 162 global $DB; 163 164 $this->resetAfterTest(); 165 166 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 167 $generator->generate_h5p_data($createlibraryfiles); 168 169 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'MainLibrary']); 170 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library1']); 171 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library2']); 172 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library3']); 173 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library4']); 174 $libraries[] = $DB->get_record('h5p_libraries', ['machinename' => 'Library5']); 175 176 foreach($libraries as $lib) { 177 // Return the created library files. 178 $libraryfiles = $DB->get_records('files', 179 array( 180 'component' => \core_h5p\file_storage::COMPONENT, 181 'filearea' => \core_h5p\file_storage::LIBRARY_FILEAREA, 182 'itemid' => $lib->id 183 ) 184 ); 185 186 $haslibraryfiles = !empty($libraryfiles); 187 188 $this->assertEquals($expected, $haslibraryfiles); 189 } 190 } 191 192 /** 193 * Data provider for test_generate_h5p_data_files_creation(). 194 * 195 * @return array 196 */ 197 public function generate_h5p_data_files_creation_provider(): array { 198 return [ 199 'Do not create library related files on the filesystem' => [ 200 false, 201 false 202 ], 203 'Create library related files on the filesystem' => [ 204 true, 205 true 206 ] 207 ]; 208 } 209 210 /** 211 * Test the behaviour of create_library_record(). Test whether the library data is properly 212 * saved in the database. 213 */ 214 public function test_create_library_record() { 215 $this->resetAfterTest(); 216 217 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 218 219 $data = $generator->create_library_record('Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/'); 220 unset($data->id); 221 222 $expected = (object) [ 223 'machinename' => 'Library', 224 'title' => 'Lib', 225 'majorversion' => '1', 226 'minorversion' => '2', 227 'patchversion' => '3', 228 'runnable' => '1', 229 'fullscreen' => '1', 230 'embedtypes' => '', 231 'preloadedjs' => 'js/example.js', 232 'preloadedcss' => 'css/example.css', 233 'droplibrarycss' => '', 234 'semantics' => 'Semantics example', 235 'addto' => '/regex11/', 236 'coremajor' => null, 237 'coreminor' => null, 238 'metadatasettings' => null, 239 ]; 240 241 $this->assertEquals($expected, $data); 242 } 243 244 /** 245 * Test the behaviour of create_h5p_record(). Test whather the h5p content data is 246 * properly saved in the database. 247 * 248 * @dataProvider create_h5p_record_provider 249 * @param array $h5pdata The h5p content data 250 * @param \stdClass $expected The expected saved data 251 **/ 252 public function test_create_h5p_record(array $h5pdata, \stdClass $expected) { 253 global $DB; 254 255 $this->resetAfterTest(); 256 257 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 258 259 $h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata); 260 261 $data = $DB->get_record('h5p', ['id' => $h5pid]); 262 unset($data->id); 263 unset($data->timecreated); 264 unset($data->timemodified); 265 266 $this->assertEquals($data, $expected); 267 } 268 269 /** 270 * Data provider for test_create_h5p_record(). 271 * 272 * @return array 273 */ 274 public function create_h5p_record_provider(): array { 275 $createdjsoncontent = json_encode( 276 array( 277 'text' => '<p>Created dummy text<\/p>\n', 278 'questions' => '<p>Test created question<\/p>\n' 279 ) 280 ); 281 282 $defaultjsoncontent = json_encode( 283 array( 284 'text' => '<p>Dummy text<\/p>\n', 285 'questions' => '<p>Test question<\/p>\n' 286 ) 287 ); 288 289 $createdfilteredcontent = json_encode( 290 array( 291 'text' => 'Created dummy text', 292 'questions' => 'Test created question' 293 ) 294 ); 295 296 $defaultfilteredcontent = json_encode( 297 array( 298 'text' => 'Dummy text', 299 'questions' => 'Test question' 300 ) 301 ); 302 303 return [ 304 'Create h5p content record with set json content and set filtered content' => [ 305 [ 306 1, 307 $createdjsoncontent, 308 $createdfilteredcontent 309 ], 310 (object) array( 311 'jsoncontent' => $createdjsoncontent, 312 'mainlibraryid' => '1', 313 'displayoptions' => '8', 314 'pathnamehash' => sha1('pathname'), 315 'contenthash' => sha1('content'), 316 'filtered' => $createdfilteredcontent, 317 ) 318 ], 319 'Create h5p content record with set json content and default filtered content' => [ 320 [ 321 1, 322 $createdjsoncontent, 323 null 324 ], 325 (object) array( 326 'jsoncontent' => $createdjsoncontent, 327 'mainlibraryid' => '1', 328 'displayoptions' => '8', 329 'pathnamehash' => sha1('pathname'), 330 'contenthash' => sha1('content'), 331 'filtered' => $defaultfilteredcontent, 332 ) 333 ], 334 'Create h5p content record with default json content and set filtered content' => [ 335 [ 336 1, 337 null, 338 $createdfilteredcontent 339 ], 340 (object) array( 341 'jsoncontent' => $defaultjsoncontent, 342 'mainlibraryid' => '1', 343 'displayoptions' => '8', 344 'pathnamehash' => sha1('pathname'), 345 'contenthash' => sha1('content'), 346 'filtered' => $createdfilteredcontent, 347 ) 348 ], 349 'Create h5p content record with default json content and default filtered content' => [ 350 [ 351 1, 352 null, 353 null 354 ], 355 (object) array( 356 'jsoncontent' => $defaultjsoncontent, 357 'mainlibraryid' => '1', 358 'displayoptions' => '8', 359 'pathnamehash' => sha1('pathname'), 360 'contenthash' => sha1('content'), 361 'filtered' => $defaultfilteredcontent, 362 ) 363 ] 364 ]; 365 } 366 367 /** 368 * Test the behaviour of create_contents_libraries_record(). Test whether the contents libraries 369 * are properly saved in the database. 370 * 371 * @dataProvider create_contents_libraries_record_provider 372 * @param array $contentslibrariestdata The h5p contents libraries data. 373 * @param \stdClass $expected The expected saved data. 374 **/ 375 public function test_create_contents_libraries_record(array $contentslibrariestdata, \stdClass $expected) { 376 global $DB; 377 378 $this->resetAfterTest(); 379 380 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 381 382 $contentlibid = call_user_func_array([$generator, 'create_contents_libraries_record'], $contentslibrariestdata); 383 384 $data = $DB->get_record('h5p_contents_libraries', ['id' => $contentlibid]); 385 unset($data->id); 386 387 $this->assertEquals($data, $expected); 388 } 389 390 /** 391 * Data provider for test_create_contents_libraries_record(). 392 * 393 * @return array 394 */ 395 public function create_contents_libraries_record_provider(): array { 396 return [ 397 'Create h5p content library with set dependency type' => [ 398 [ 399 1, 400 1, 401 'dynamic' 402 ], 403 (object) array( 404 'h5pid' => '1', 405 'libraryid' => '1', 406 'dependencytype' => 'dynamic', 407 'dropcss' => '0', 408 'weight' => '1' 409 ) 410 ], 411 'Create h5p content library with a default dependency type' => [ 412 [ 413 1, 414 1 415 ], 416 (object) array( 417 'h5pid' => '1', 418 'libraryid' => '1', 419 'dependencytype' => 'preloaded', 420 'dropcss' => '0', 421 'weight' => '1' 422 ) 423 ] 424 ]; 425 } 426 427 /** 428 * Test the behaviour of create_library_dependency_record(). Test whether the contents libraries 429 * are properly saved in the database. 430 * 431 * @dataProvider create_library_dependency_record_provider 432 * @param array $librarydependencydata The library dependency data. 433 * @param \stdClass $expected The expected saved data. 434 **/ 435 public function test_create_library_dependency_record(array $librarydependencydata, \stdClass $expected) { 436 global $DB; 437 438 $this->resetAfterTest(); 439 440 $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p'); 441 442 $contentlibid = call_user_func_array([$generator, 'create_library_dependency_record'], $librarydependencydata); 443 444 $data = $DB->get_record('h5p_library_dependencies', ['id' => $contentlibid]); 445 unset($data->id); 446 447 $this->assertEquals($data, $expected); 448 } 449 450 /** 451 * Data provider for test_create_library_dependency_record(). 452 * 453 * @return array 454 */ 455 public function create_library_dependency_record_provider(): array { 456 return [ 457 'Create h5p library dependency with set dependency type' => [ 458 [ 459 1, 460 1, 461 'dynamic' 462 ], 463 (object) array( 464 'libraryid' => '1', 465 'requiredlibraryid' => '1', 466 'dependencytype' => 'dynamic' 467 ) 468 ], 469 'Create h5p library dependency with default dependency type' => [ 470 [ 471 1, 472 1 473 ], 474 (object) array( 475 'libraryid' => '1', 476 'requiredlibraryid' => '1', 477 'dependencytype' => 'preloaded' 478 ) 479 ] 480 ]; 481 } 482 483 /** 484 * Test the behaviour of create_content_file(). Test whether a file belonging to a content is created. 485 * 486 * @dataProvider create_content_file_provider 487 * @param array $filedata Data from the file to be created. 488 * @param array $expecteddata Data expected.Data from the file to be created. 489 */ 490 public function test_create_content_file($filedata, $expecteddata): void { 491 $this->resetAfterTest(); 492 493 $generator = self::getDataGenerator()->get_plugin_generator('core_h5p'); 494 495 if ($expecteddata[1] === 'exception') { 496 $this->expectException('coding_exception'); 497 } 498 call_user_func_array([$generator, 'create_content_file'], $filedata); 499 500 $systemcontext = \context_system::instance(); 501 $filearea = $filedata[1]; 502 $filepath = '/'. dirname($filedata[0]). '/'; 503 $filename = basename($filedata[0]); 504 $itemid = $expecteddata[0]; 505 506 $fs = new \file_storage(); 507 $exists = $fs->file_exists($systemcontext->id, file_storage::COMPONENT, $filearea, $itemid, $filepath, 508 $filename); 509 if ($expecteddata[1] === true) { 510 $this->assertTrue($exists); 511 } else if ($expecteddata[1] === false) { 512 $this->assertFalse($exists); 513 } 514 } 515 516 /** 517 * Data provider for test_create_content_file(). Data from different files to be created. 518 * 519 * @return array 520 **/ 521 public function create_content_file_provider(): array { 522 return [ 523 'Create file in content with id 4' => [ 524 [ 525 'images/img1.png', 526 'content', 527 4 528 ], 529 [ 530 4, 531 true 532 ] 533 ], 534 'Create file in the editor' => [ 535 [ 536 'images/img1.png', 537 'editor' 538 ], 539 [ 540 0, 541 true 542 ] 543 ], 544 'Create file in content without id' => [ 545 [ 546 'images/img1.png', 547 'content' 548 ], 549 [ 550 0, 551 'exception' 552 ] 553 ] 554 ]; 555 } 556 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body