Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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  /**
  18   * Testing the H5P core methods.
  19   *
  20   * @package    core_h5p
  21   * @category   test
  22   * @copyright  2019 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  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  /**
  33   * Test class covering the H5PFileStorage interface implementation.
  34   *
  35   * @package    core_h5p
  36   * @copyright  2019 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 h5p_core_test extends \advanced_testcase {
  42  
  43      protected function setUp(): void {
  44          global $CFG;
  45          parent::setUp();
  46  
  47          autoloader::register();
  48  
  49          require_once($CFG->libdir . '/tests/fixtures/testable_core_h5p.php');
  50  
  51          $factory = new h5p_test_factory();
  52          $this->core = $factory->get_core();
  53          $this->core->set_endpoint($this->getExternalTestFileUrl(''));
  54      }
  55  
  56      /**
  57       * Check that given an H5P content type machine name, the required library are fetched and installed from the official H5P
  58       * repository.
  59       */
  60      public function test_fetch_content_type(): void {
  61          global $DB;
  62  
  63          if (!PHPUNIT_LONGTEST) {
  64              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
  65          }
  66  
  67          $this->resetAfterTest(true);
  68  
  69          // Get info of latest content types versions.
  70          $contenttypes = $this->core->get_latest_content_types()->contentTypes;
  71          // We are installing the first content type with tutorial and example fields (or the first one if none has them).
  72          $librarydata = $contenttypes[0];
  73          foreach ($contenttypes as $contentype) {
  74              if (isset($contenttype->tutorial) && isset($contenttype->example)) {
  75                  $librarydata = $contenttype;
  76                  break;
  77              }
  78          }
  79  
  80          $library = [
  81                  'machineName' => $librarydata->id,
  82                  'majorVersion' => $librarydata->version->major,
  83                  'minorVersion' => $librarydata->version->minor,
  84                  'patchVersion' => $librarydata->version->patch,
  85          ];
  86          // Add example and tutorial to the library.
  87          if (isset($librarydata->example)) {
  88              $library['example'] = $librarydata->example;
  89          }
  90          if (isset($librarydata->tutorial)) {
  91              $library['tutorial'] = $librarydata->tutorial;
  92          }
  93  
  94          // Verify that the content type is not yet installed.
  95          $conditions['machinename'] = $library['machineName'];
  96          $typeinstalled = $DB->count_records('h5p_libraries', $conditions);
  97  
  98          $this->assertEquals(0, $typeinstalled);
  99  
 100          // Fetch the content type.
 101          $this->core->fetch_content_type($library);
 102  
 103          // Check that the content type is now installed.
 104          $typeinstalled = $DB->get_record('h5p_libraries', $conditions);
 105          $this->assertEquals($librarydata->id, $typeinstalled->machinename);
 106          $this->assertEquals($librarydata->coreApiVersionNeeded->major, $typeinstalled->coremajor);
 107          $this->assertEquals($librarydata->coreApiVersionNeeded->minor, $typeinstalled->coreminor);
 108          if (isset($librarydata->tutorial)) {
 109              $this->assertEquals($librarydata->tutorial, $typeinstalled->tutorial);
 110              $this->assertEquals($librarydata->example, $typeinstalled->example);
 111          }
 112      }
 113  
 114      /**
 115       * Test that latest version of non installed H5P content type libraries are fetched and installed from the
 116       * official H5P repository. To speed up the test, only if checked that one content type is installed.
 117       */
 118      public function test_fetch_latest_content_types(): void {
 119          global $DB;
 120  
 121          if (!PHPUNIT_LONGTEST) {
 122              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 123          }
 124  
 125          $this->resetAfterTest(true);
 126  
 127          $contentfiles = $DB->count_records('h5p_libraries');
 128  
 129          // Initially there are no h5p records in database.
 130          $this->assertEquals(0, $contentfiles);
 131  
 132          $contenttypespending = ['H5P.Accordion'];
 133  
 134          // Fetch generator.
 135          $generator = \testing_util::get_data_generator();
 136          $h5pgenerator = $generator->get_plugin_generator('core_h5p');
 137  
 138          // Get info of latest content types versions.
 139          [$installedtypes, $typesnotinstalled] = $h5pgenerator->create_content_types($contenttypespending, $this->core);
 140          // Number of H5P content types.
 141          $numcontenttypes = $installedtypes + $typesnotinstalled;
 142  
 143          // Content type libraries has runnable set to 1.
 144          $conditions = ['runnable' => 1];
 145          $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
 146  
 147          // There is a record for each installed content type, except the one that was hold for later.
 148          $this->assertEquals($numcontenttypes - 1, count($contentfiles));
 149          $this->assertArrayNotHasKey($contenttypespending[0], $contentfiles);
 150  
 151          $result = $this->core->fetch_latest_content_types();
 152  
 153          $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
 154  
 155          // There is a new record for the new installed content type.
 156          $this->assertCount($numcontenttypes, $contentfiles);
 157          $this->assertArrayHasKey($contenttypespending[0], $contentfiles);
 158          $this->assertCount(1, $result->typesinstalled);
 159          $this->assertStringStartsWith($contenttypespending[0], $result->typesinstalled[0]['name']);
 160  
 161          // New execution doesn't install any content type.
 162          $result = $this->core->fetch_latest_content_types();
 163  
 164          $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
 165  
 166          $this->assertEquals($numcontenttypes, count($contentfiles));
 167          $this->assertCount(0, $result->typesinstalled);
 168      }
 169  
 170      /**
 171       * Test that if site_uuid is not set, the site is registered and site_uuid is set.
 172       *
 173       */
 174      public function test_get_site_uuid(): void {
 175          $this->resetAfterTest(true);
 176  
 177          if (!PHPUNIT_LONGTEST) {
 178              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 179          }
 180  
 181          // Check that site_uuid does not have a value.
 182          $this->assertFalse(get_config('core_h5p', 'site_uuid'));
 183  
 184          $siteuuid = $this->core->get_site_uuid();
 185  
 186          $this->assertSame($siteuuid, get_config('core_h5p', 'site_uuid'));
 187  
 188          // Check that after a new request the site_uuid remains the same.
 189          $siteuuid2 = $this->core->get_site_uuid();
 190          $this->assertEquals( $siteuuid, $siteuuid2);
 191      }
 192  
 193      /**
 194       * Test if no handler has been defined.
 195       */
 196      public function test_get_default_handler() {
 197          global $CFG;
 198  
 199          $this->resetAfterTest(true);
 200          // Emtpy the h5plibraryhandler setting.
 201          $CFG->h5plibraryhandler = '';
 202  
 203          // Get the default habdler library to use in the settings h5p page.
 204          // For instance, h5plib_v124.
 205          $handlerlib = autoloader::get_default_handler_library();
 206          $this->assertNotNull($handlerlib);
 207          $this->assertStringNotContainsString($handlerlib, '\local\library\handler');
 208          // Get the default handler class.
 209          // For instance, \h5plib_v124\local\library\handler.
 210          $handlerclass = autoloader::get_default_handler();
 211          $this->assertStringEndsWith('\local\library\handler', $handlerclass);
 212      }
 213  }