Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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