Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 311 and 400] [Versions 400 and 402] [Versions 400 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   * Core h5p external functions tests.
  19   *
  20   * @package    core_h5p
  21   * @category   external
  22   * @copyright  2019 Carlos Escobedo <carlos@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @since      Moodle 3.8
  25   */
  26  
  27  namespace core_h5p\external;
  28  
  29  use externallib_advanced_testcase;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  global $CFG;
  34  
  35  require_once($CFG->libdir . '/externallib.php');
  36  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  37  
  38  use core_h5p\external;
  39  use core_h5p\file_storage;
  40  use core_h5p\local\library\autoloader;
  41  
  42  /**
  43   * Core h5p external functions tests
  44   *
  45   * @package    core_h5p
  46   * @category   external
  47   * @copyright  2019 Carlos Escobedo <carlos@moodle.com>
  48   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  49   * @since      Moodle 3.8
  50   */
  51  class external_test extends externallib_advanced_testcase {
  52  
  53      protected function setUp(): void {
  54          parent::setUp();
  55          autoloader::register();
  56      }
  57  
  58      /**
  59       * test_get_trusted_h5p_file description
  60       */
  61      public function test_get_trusted_h5p_file() {
  62          $this->resetAfterTest(true);
  63          $this->setAdminUser();
  64  
  65          // This is a valid .H5P file.
  66          $filename = 'find-the-words.h5p';
  67          $syscontext = \context_system::instance();
  68  
  69          // Create a fake export H5P file with normal pluginfile call.
  70          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
  71          $deployedfile = $generator->create_export_file($filename,
  72              $syscontext->id,
  73              \core_h5p\file_storage::COMPONENT,
  74              \core_h5p\file_storage::EXPORT_FILEAREA,
  75              $generator::PLUGINFILE);
  76  
  77          // Make the URL to pass to the WS.
  78          $url  = \moodle_url::make_pluginfile_url(
  79              $syscontext->id,
  80              \core_h5p\file_storage::COMPONENT,
  81              \core_h5p\file_storage::EXPORT_FILEAREA,
  82              0,
  83              '/',
  84              $filename
  85          );
  86  
  87          // Call the WS.
  88          $result = external::get_trusted_h5p_file($url->out(false), 0, 0, 0, 0);
  89          $result = \external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
  90          // Expected result: Just 1 record on files and none on warnings.
  91          $this->assertCount(1, $result['files']);
  92          $this->assertCount(0, $result['warnings']);
  93  
  94          // Check info export file to compare with the ws's results.
  95          $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']);
  96          $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']);
  97          $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']);
  98          $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']);
  99          $this->assertStringContainsString($deployedfile['filename'], $result['files'][0]['filename']);
 100          $this->assertStringContainsString($deployedfile['fileurl'], $result['files'][0]['fileurl']);
 101      }
 102  
 103      /**
 104       * test_h5p_invalid_url description
 105       */
 106      public function test_h5p_invalid_url() {
 107          $this->resetAfterTest();
 108          $this->setAdminUser();
 109  
 110          // Create an empty url.
 111          $urlempty = '';
 112          $result = external::get_trusted_h5p_file($urlempty, 0, 0, 0, 0);
 113          $result = \external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
 114          // Expected result: Just 1 record on warnings and none on files.
 115          $this->assertCount(0, $result['files']);
 116          $this->assertCount(1, $result['warnings']);
 117          // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception.
 118          $this->assertEquals($urlempty, $result['warnings'][0]['item']);
 119          $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']);
 120  
 121          // Create a non-local URL.
 122          $urlnonlocal = 'http://www.google.com/pluginfile.php/644/block_html/content/arithmetic-quiz-1-1.h5p';
 123          $result = external::get_trusted_h5p_file($urlnonlocal, 0, 0, 0, 0);
 124          $result = \external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
 125          // Expected result: Just 1 record on warnings and none on files.
 126          $this->assertCount(0, $result['files']);
 127          $this->assertCount(1, $result['warnings']);
 128          // Check the warnings to be sure that h5pinvalidurl is the message by moodle_exception.
 129          $this->assertEquals($urlnonlocal, $result['warnings'][0]['item']);
 130          $this->assertEquals(get_string('h5pinvalidurl', 'core_h5p'), $result['warnings'][0]['message']);
 131      }
 132  
 133      /**
 134       * test_h5p_file_not_found description
 135       */
 136      public function test_h5p_file_not_found() {
 137          $this->resetAfterTest();
 138          $this->setAdminUser();
 139  
 140          // Create a valid url with an h5pfile which doesn't exist in DB.
 141          $syscontext = \context_system::instance();
 142          $filenotfoundurl  = \moodle_url::make_pluginfile_url(
 143              $syscontext->id,
 144              \core_h5p\file_storage::COMPONENT,
 145              'unittest',
 146              0,
 147              '/',
 148              'notfound.h5p'
 149          );
 150          // Call the ws.
 151          $result = external::get_trusted_h5p_file($filenotfoundurl->out(), 0, 0, 0, 0);
 152          $result = \external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
 153          // Expected result: Just 1 record on warnings and none on files.
 154          $this->assertCount(0, $result['files']);
 155          $this->assertCount(1, $result['warnings']);
 156          // Check the warnings to be sure that h5pfilenotfound is the message by h5p error.
 157          $this->assertEquals($filenotfoundurl->out(), $result['warnings'][0]['item']);
 158          $this->assertEquals(get_string('h5pfilenotfound', 'core_h5p'), $result['warnings'][0]['message']);
 159      }
 160  
 161      /**
 162       * Test the request to get_trusted_h5p_file
 163       * using webservice/pluginfile.php as url param.
 164       */
 165      public function test_allow_webservice_pluginfile_in_url_param() {
 166          $this->resetAfterTest(true);
 167          $this->setAdminUser();
 168  
 169          // This is a valid .H5P file.
 170          $filename = 'find-the-words.h5p';
 171          $syscontext = \context_system::instance();
 172  
 173          // Create a fake export H5P file with webservice call.
 174          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
 175          $deployedfile = $generator->create_export_file($filename,
 176              $syscontext->id,
 177              \core_h5p\file_storage::COMPONENT,
 178              \core_h5p\file_storage::EXPORT_FILEAREA);
 179  
 180          // Make the URL to pass to the WS.
 181          $url  = \moodle_url::make_webservice_pluginfile_url(
 182              $syscontext->id,
 183              \core_h5p\file_storage::COMPONENT,
 184              \core_h5p\file_storage::EXPORT_FILEAREA,
 185              0,
 186              '/',
 187              $filename
 188          );
 189  
 190          // Call the WS.
 191          $result = external::get_trusted_h5p_file($url->out(), 0, 0, 0, 0);
 192          $result = \external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
 193  
 194          // Check info export file to compare with the ws's results.
 195          $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']);
 196          $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']);
 197          $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']);
 198          $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']);
 199          $this->assertStringContainsString($deployedfile['filename'], $result['files'][0]['filename']);
 200          $this->assertStringContainsString($deployedfile['fileurl'], $result['files'][0]['fileurl']);
 201      }
 202  
 203      /**
 204       * Test the request to get_trusted_h5p_file
 205       * using tokenpluginfile.php as url param.
 206       */
 207      public function test_allow_tokenluginfile_in_url_param() {
 208          $this->resetAfterTest(true);
 209          $this->setAdminUser();
 210  
 211          // This is a valid .H5P file.
 212          $filename = 'find-the-words.h5p';
 213          $syscontext = \context_system::instance();
 214  
 215          // Create a fake export H5P file with tokenfile call.
 216          $generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
 217          $deployedfile = $generator->create_export_file($filename,
 218              $syscontext->id,
 219              \core_h5p\file_storage::COMPONENT,
 220              \core_h5p\file_storage::EXPORT_FILEAREA,
 221              $generator::TOKENPLUGINFILE);
 222  
 223          // Make the URL to pass to the WS.
 224          $url  = \moodle_url::make_pluginfile_url(
 225              $syscontext->id,
 226              \core_h5p\file_storage::COMPONENT,
 227              \core_h5p\file_storage::EXPORT_FILEAREA,
 228              0,
 229              '/',
 230              $filename,
 231              false,
 232              true
 233          );
 234  
 235          // Call the WS.
 236          $result = external::get_trusted_h5p_file($url->out(false), 0, 0, 0, 0);
 237          $result = \external_api::clean_returnvalue(external::get_trusted_h5p_file_returns(), $result);
 238          // Expected result: Just 1 record on files and none on warnings.
 239          $this->assertCount(1, $result['files']);
 240          $this->assertCount(0, $result['warnings']);
 241  
 242          // Check info export file to compare with the ws's results.
 243          $this->assertEquals($deployedfile['filepath'], $result['files'][0]['filepath']);
 244          $this->assertEquals($deployedfile['mimetype'], $result['files'][0]['mimetype']);
 245          $this->assertEquals($deployedfile['filesize'], $result['files'][0]['filesize']);
 246          $this->assertEquals($deployedfile['timemodified'], $result['files'][0]['timemodified']);
 247          $this->assertStringContainsString($deployedfile['filename'], $result['files'][0]['filename']);
 248          $this->assertStringContainsString($deployedfile['fileurl'], $result['files'][0]['fileurl']);
 249      }
 250  }