Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403] [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  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_test extends \advanced_testcase {
  31  
  32      /**
  33       * Tests set up.
  34       */
  35      protected function setUp(): void {
  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(
 220              'Library', 'Lib', 1, 2, 3, 'Semantics example', '/regex11/', 'http://tutorial.org/', 'http://example.org/'
 221          );
 222          unset($data->id);
 223  
 224          $expected = (object) [
 225              'machinename' => 'Library',
 226              'title' => 'Lib',
 227              'majorversion' => '1',
 228              'minorversion' => '2',
 229              'patchversion' => '3',
 230              'runnable' => '1',
 231              'fullscreen' => '1',
 232              'embedtypes' => '',
 233              'preloadedjs' => 'js/example.js',
 234              'preloadedcss' => 'css/example.css',
 235              'droplibrarycss' => '',
 236              'semantics' => 'Semantics example',
 237              'addto' => '/regex11/',
 238              'tutorial' => 'http://tutorial.org/',
 239              'example' => 'http://example.org/',
 240              'coremajor' => null,
 241              'coreminor' => null,
 242              'metadatasettings' => null,
 243              'enabled' => 1,
 244          ];
 245  
 246          $this->assertEquals($expected, $data);
 247      }
 248  
 249      /**
 250       * Test the behaviour of create_h5p_record(). Test whather the h5p content data is
 251       * properly saved in the database.
 252       *
 253       * @dataProvider create_h5p_record_provider
 254       * @param array $h5pdata The h5p content data
 255       * @param \stdClass $expected The expected saved data
 256       **/
 257      public function test_create_h5p_record(array $h5pdata, \stdClass $expected) {
 258          global $DB;
 259  
 260          $this->resetAfterTest();
 261  
 262          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
 263  
 264          $h5pid = call_user_func_array([$generator, 'create_h5p_record'], $h5pdata);
 265  
 266          $data = $DB->get_record('h5p', ['id' => $h5pid]);
 267          unset($data->id);
 268          unset($data->timecreated);
 269          unset($data->timemodified);
 270  
 271          $this->assertEquals($data, $expected);
 272      }
 273  
 274      /**
 275       * Data provider for test_create_h5p_record().
 276       *
 277       * @return array
 278       */
 279      public function create_h5p_record_provider(): array {
 280          $createdjsoncontent = json_encode(
 281              array(
 282                  'text' => '<p>Created dummy text<\/p>\n',
 283                  'questions' => '<p>Test created question<\/p>\n'
 284              )
 285          );
 286  
 287          $defaultjsoncontent = json_encode(
 288              array(
 289                  'text' => '<p>Dummy text<\/p>\n',
 290                  'questions' => '<p>Test question<\/p>\n'
 291              )
 292          );
 293  
 294          $createdfilteredcontent = json_encode(
 295              array(
 296                  'text' => 'Created dummy text',
 297                  'questions' => 'Test created question'
 298              )
 299          );
 300  
 301          $defaultfilteredcontent = json_encode(
 302              array(
 303                  'text' => 'Dummy text',
 304                  'questions' => 'Test question'
 305              )
 306          );
 307  
 308          return [
 309              'Create h5p content record with set json content and set filtered content' => [
 310                  [
 311                      1,
 312                      $createdjsoncontent,
 313                      $createdfilteredcontent
 314                  ],
 315                  (object) array(
 316                      'jsoncontent' => $createdjsoncontent,
 317                      'mainlibraryid' => '1',
 318                      'displayoptions' => '8',
 319                      'pathnamehash' => sha1('pathname'),
 320                      'contenthash' => sha1('content'),
 321                      'filtered' => $createdfilteredcontent,
 322                  )
 323              ],
 324              'Create h5p content record with set json content and default filtered content' => [
 325                  [
 326                      1,
 327                      $createdjsoncontent,
 328                      null
 329                  ],
 330                  (object) array(
 331                      'jsoncontent' => $createdjsoncontent,
 332                      'mainlibraryid' => '1',
 333                      'displayoptions' => '8',
 334                      'pathnamehash' => sha1('pathname'),
 335                      'contenthash' => sha1('content'),
 336                      'filtered' => $defaultfilteredcontent,
 337                  )
 338              ],
 339              'Create h5p content record with default json content and set filtered content' => [
 340                  [
 341                      1,
 342                      null,
 343                      $createdfilteredcontent
 344                  ],
 345                  (object) array(
 346                      'jsoncontent' => $defaultjsoncontent,
 347                      'mainlibraryid' => '1',
 348                      'displayoptions' => '8',
 349                      'pathnamehash' => sha1('pathname'),
 350                      'contenthash' => sha1('content'),
 351                      'filtered' => $createdfilteredcontent,
 352                  )
 353              ],
 354              'Create h5p content record with default json content and default filtered content' => [
 355                  [
 356                      1,
 357                      null,
 358                      null
 359                  ],
 360                  (object) array(
 361                      'jsoncontent' => $defaultjsoncontent,
 362                      'mainlibraryid' => '1',
 363                      'displayoptions' => '8',
 364                      'pathnamehash' => sha1('pathname'),
 365                      'contenthash' => sha1('content'),
 366                      'filtered' => $defaultfilteredcontent,
 367                  )
 368              ]
 369          ];
 370      }
 371  
 372      /**
 373       * Test the behaviour of create_contents_libraries_record(). Test whether the contents libraries
 374       * are properly saved in the database.
 375       *
 376       * @dataProvider create_contents_libraries_record_provider
 377       * @param array $contentslibrariestdata The h5p contents libraries data.
 378       * @param \stdClass $expected The expected saved data.
 379       **/
 380      public function test_create_contents_libraries_record(array $contentslibrariestdata, \stdClass $expected) {
 381          global $DB;
 382  
 383          $this->resetAfterTest();
 384  
 385          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
 386  
 387          $contentlibid = call_user_func_array([$generator, 'create_contents_libraries_record'], $contentslibrariestdata);
 388  
 389          $data = $DB->get_record('h5p_contents_libraries', ['id' => $contentlibid]);
 390          unset($data->id);
 391  
 392          $this->assertEquals($data, $expected);
 393      }
 394  
 395      /**
 396       * Data provider for test_create_contents_libraries_record().
 397       *
 398       * @return array
 399       */
 400      public function create_contents_libraries_record_provider(): array {
 401          return [
 402              'Create h5p content library with set dependency type' => [
 403                  [
 404                      1,
 405                      1,
 406                      'dynamic'
 407                  ],
 408                  (object) array(
 409                      'h5pid' => '1',
 410                      'libraryid' => '1',
 411                      'dependencytype' => 'dynamic',
 412                      'dropcss' => '0',
 413                      'weight' => '1'
 414                  )
 415              ],
 416              'Create h5p content library with a default dependency type' => [
 417                  [
 418                      1,
 419                      1
 420                  ],
 421                  (object) array(
 422                      'h5pid' => '1',
 423                      'libraryid' => '1',
 424                      'dependencytype' => 'preloaded',
 425                      'dropcss' => '0',
 426                      'weight' => '1'
 427                  )
 428              ]
 429          ];
 430      }
 431  
 432      /**
 433       * Test the behaviour of create_library_dependency_record(). Test whether the contents libraries
 434       * are properly saved in the database.
 435       *
 436       * @dataProvider create_library_dependency_record_provider
 437       * @param array $librarydependencydata The library dependency data.
 438       * @param \stdClass $expected The expected saved data.
 439       **/
 440      public function test_create_library_dependency_record(array $librarydependencydata, \stdClass $expected) {
 441          global $DB;
 442  
 443          $this->resetAfterTest();
 444  
 445          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
 446  
 447          $contentlibid = call_user_func_array([$generator, 'create_library_dependency_record'], $librarydependencydata);
 448  
 449          $data = $DB->get_record('h5p_library_dependencies', ['id' => $contentlibid]);
 450          unset($data->id);
 451  
 452          $this->assertEquals($data, $expected);
 453      }
 454  
 455      /**
 456       * Data provider for test_create_library_dependency_record().
 457       *
 458       * @return array
 459       */
 460      public function create_library_dependency_record_provider(): array {
 461          return [
 462              'Create h5p library dependency with set dependency type' => [
 463                  [
 464                      1,
 465                      1,
 466                      'dynamic'
 467                  ],
 468                  (object) array(
 469                      'libraryid' => '1',
 470                      'requiredlibraryid' => '1',
 471                      'dependencytype' => 'dynamic'
 472                  )
 473              ],
 474              'Create h5p library dependency with default dependency type' => [
 475                  [
 476                      1,
 477                      1
 478                  ],
 479                  (object) array(
 480                      'libraryid' => '1',
 481                      'requiredlibraryid' => '1',
 482                      'dependencytype' => 'preloaded'
 483                  )
 484              ]
 485          ];
 486      }
 487  
 488      /**
 489       * Test the behaviour of create_content_file(). Test whether a file belonging to a content is created.
 490       *
 491       * @dataProvider create_content_file_provider
 492       * @param array $filedata Data from the file to be created.
 493       * @param array $expecteddata Data expected.Data from the file to be created.
 494       */
 495      public function test_create_content_file($filedata, $expecteddata): void {
 496          $this->resetAfterTest();
 497  
 498          $generator = self::getDataGenerator()->get_plugin_generator('core_h5p');
 499  
 500          if ($expecteddata[1] === 'exception') {
 501              $this->expectException('coding_exception');
 502          }
 503          call_user_func_array([$generator, 'create_content_file'], $filedata);
 504  
 505          $systemcontext = \context_system::instance();
 506          $filearea = $filedata[1];
 507          $filepath = '/'. dirname($filedata[0]). '/';
 508          $filename = basename($filedata[0]);
 509          $itemid = $expecteddata[0];
 510  
 511          $fs = new \file_storage();
 512          $exists = $fs->file_exists($systemcontext->id, file_storage::COMPONENT, $filearea, $itemid, $filepath,
 513              $filename);
 514          if ($expecteddata[1] === true) {
 515              $this->assertTrue($exists);
 516          } else if ($expecteddata[1] === false) {
 517              $this->assertFalse($exists);
 518          }
 519      }
 520  
 521      /**
 522       * Data provider for test_create_content_file(). Data from different files to be created.
 523       *
 524       * @return array
 525       **/
 526      public function create_content_file_provider(): array {
 527          return [
 528              'Create file in content with id 4' => [
 529                  [
 530                      'images/img1.png',
 531                      'content',
 532                      4
 533                  ],
 534                  [
 535                      4,
 536                      true
 537                  ]
 538              ],
 539              'Create file in the editor' => [
 540                  [
 541                      'images/img1.png',
 542                      'editor'
 543                  ],
 544                  [
 545                      0,
 546                      true
 547                  ]
 548              ],
 549              'Create file in content without id' => [
 550                  [
 551                      'images/img1.png',
 552                      'content'
 553                  ],
 554                  [
 555                      0,
 556                      'exception'
 557                  ]
 558              ]
 559          ];
 560      }
 561  }