Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 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  /**
  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_testcase extends \advanced_testcase {
  42  
  43      protected function setup() {
  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.
  72          $librarydata = $contenttypes[0];
  73  
  74          $library = [
  75                  'machineName' => $librarydata->id,
  76                  'majorVersion' => $librarydata->version->major,
  77                  'minorVersion' => $librarydata->version->minor,
  78                  'patchVersion' => $librarydata->version->patch,
  79          ];
  80  
  81          // Verify that the content type is not yet installed.
  82          $conditions['machinename'] = $library['machineName'];
  83          $typeinstalled = $DB->count_records('h5p_libraries', $conditions);
  84  
  85          $this->assertEquals(0, $typeinstalled);
  86  
  87          // Fetch the content type.
  88          $this->core->fetch_content_type($library);
  89  
  90          // Check that the content type is now installed.
  91          $typeinstalled = $DB->get_record('h5p_libraries', $conditions);
  92          $this->assertEquals($librarydata->id, $typeinstalled->machinename);
  93          $this->assertEquals($librarydata->coreApiVersionNeeded->major, $typeinstalled->coremajor);
  94          $this->assertEquals($librarydata->coreApiVersionNeeded->minor, $typeinstalled->coreminor);
  95      }
  96  
  97      /**
  98       * Test that latest version of non installed H5P content type libraries are fetched and installed from the
  99       * official H5P repository. To speed up the test, only if checked that one content type is installed.
 100       */
 101      public function test_fetch_latest_content_types(): void {
 102          global $DB;
 103  
 104          if (!PHPUNIT_LONGTEST) {
 105              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 106          }
 107  
 108          $this->resetAfterTest(true);
 109  
 110          $contentfiles = $DB->count_records('h5p_libraries');
 111  
 112          // Initially there are no h5p records in database.
 113          $this->assertEquals(0, $contentfiles);
 114  
 115          $contenttypespending = ['H5P.Accordion'];
 116  
 117          // Fetch generator.
 118          $generator = \testing_util::get_data_generator();
 119          $h5pgenerator = $generator->get_plugin_generator('core_h5p');
 120  
 121          // Get info of latest content types versions.
 122          [$installedtypes, $typesnotinstalled] = $h5pgenerator->create_content_types($contenttypespending, $this->core);
 123          // Number of H5P content types.
 124          $numcontenttypes = $installedtypes + $typesnotinstalled;
 125  
 126          // Content type libraries has runnable set to 1.
 127          $conditions = ['runnable' => 1];
 128          $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
 129  
 130          // There is a record for each installed content type, except the one that was hold for later.
 131          $this->assertEquals($numcontenttypes - 1, count($contentfiles));
 132          $this->assertArrayNotHasKey($contenttypespending[0], $contentfiles);
 133  
 134          $result = $this->core->fetch_latest_content_types();
 135  
 136          $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
 137  
 138          // There is a new record for the new installed content type.
 139          $this->assertCount($numcontenttypes, $contentfiles);
 140          $this->assertArrayHasKey($contenttypespending[0], $contentfiles);
 141          $this->assertCount(1, $result->typesinstalled);
 142          $this->assertStringStartsWith($contenttypespending[0], $result->typesinstalled[0]['name']);
 143  
 144          // New execution doesn't install any content type.
 145          $result = $this->core->fetch_latest_content_types();
 146  
 147          $contentfiles = $DB->get_records('h5p_libraries', $conditions, '', 'machinename');
 148  
 149          $this->assertEquals($numcontenttypes, count($contentfiles));
 150          $this->assertCount(0, $result->typesinstalled);
 151      }
 152  
 153      /**
 154       * Test that if site_uuid is not set, the site is registered and site_uuid is set.
 155       *
 156       */
 157      public function test_get_site_uuid(): void {
 158          $this->resetAfterTest(true);
 159  
 160          if (!PHPUNIT_LONGTEST) {
 161              $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
 162          }
 163  
 164          // Check that site_uuid does not have a value.
 165          $this->assertFalse(get_config('core_h5p', 'site_uuid'));
 166  
 167          $siteuuid = $this->core->get_site_uuid();
 168  
 169          $this->assertSame($siteuuid, get_config('core_h5p', 'site_uuid'));
 170  
 171          // Check that after a new request the site_uuid remains the same.
 172          $siteuuid2 = $this->core->get_site_uuid();
 173          $this->assertEquals( $siteuuid, $siteuuid2);
 174      }
 175  
 176      /**
 177       * Test if no handler has been defined.
 178       */
 179      public function test_get_default_handler() {
 180          global $CFG;
 181  
 182          $this->resetAfterTest(true);
 183          // Emtpy the h5plibraryhandler setting.
 184          $CFG->h5plibraryhandler = '';
 185  
 186          // Get the default habdler library to use in the settings h5p page.
 187          // For instance, h5plib_v124.
 188          $handlerlib = autoloader::get_default_handler_library();
 189          $this->assertNotNull($handlerlib);
 190          $this->assertStringNotContainsString($handlerlib, '\local\library\handler');
 191          // Get the default handler class.
 192          // For instance, \h5plib_v124\local\library\handler.
 193          $handlerclass = autoloader::get_default_handler();
 194          $this->assertStringEndsWith('\local\library\handler', $handlerclass);
 195      }
 196  }