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.
   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   * Fixture for testing the functionality of core_h5p.
  19   *
  20   * @package     core
  21   * @subpackage  fixtures
  22   * @category    test
  23   * @copyright   2019 Victor Deniz <victor@moodle.com>
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  namespace core_h5p;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * H5P factory class stub for testing purposes.
  33   *
  34   * This class extends the real one to return the H5P core class stub.
  35   *
  36   * @copyright  2019 Victor Deniz <victor@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class h5p_test_factory extends factory {
  40      /**
  41       * Returns an instance of the \core_h5p\core stub class.
  42       *
  43       * @return core_h5p\core
  44       */
  45      public function get_core(): core {
  46          if ($this->core === null ) {
  47              $fs = new file_storage();
  48              $language = framework::get_language();
  49              $context = \context_system::instance();
  50  
  51              $url = \moodle_url::make_pluginfile_url($context->id, 'core_h5p', '', null,
  52                  '', '')->out();
  53  
  54              $this->core = new h5p_test_core($this->get_framework(), $fs, $url, $language, true);
  55          }
  56  
  57          return $this->core;
  58      }
  59  }
  60  
  61  /**
  62   * H5P core class stub for testing purposes.
  63   *
  64   * Modifies get_api_endpoint method to use local URLs.
  65   *
  66   * @copyright   2019 Victor Deniz <victor@moodle.com>
  67   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  68   */
  69  class h5p_test_core extends core {
  70      /** @var $endpoint Endpoint URL for testing H5P API */
  71      protected $endpoint;
  72  
  73      /**
  74       * Set the endpoint URL
  75       *
  76       * @param string $url Endpoint URL
  77       * @return void
  78       */
  79      public function set_endpoint($url): void {
  80          $this->endpoint = $url;
  81      }
  82  
  83      /**
  84       * Get the URL of the test endpoints instead of the H5P ones.
  85       *
  86       * If $endpoint = 'content' and $library is null, moodle_url is the endpoint of the latest version of the H5P content
  87       * types; however, if $library is the machine name of a content type, moodle_url is the endpoint to download the content type.
  88       * The SITES endpoint ($endpoint = 'site') may be use to get a site UUID or send site data.
  89       *
  90       * @param string|null $library The machineName of the library whose endpoint is requested.
  91       * @param string $endpoint The endpoint required. Valid values: "site", "content".
  92       * @return \moodle_url The endpoint moodle_url object.
  93       */
  94      public function get_api_endpoint(?string $library = null, string $endpoint = 'content'): \moodle_url {
  95  
  96          if ($library) {
  97              $h5purl = $this->endpoint . '/' . $library . '.h5p';
  98          } else if ($endpoint == 'content') {
  99              $h5purl = $this->endpoint . '/h5pcontenttypes.json';
 100          } else {
 101              $h5purl = $this->endpoint . '/h5puuid.json';
 102          }
 103  
 104          return new \moodle_url($h5purl);
 105      }
 106  }