Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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