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]

   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   * Unit tests for the filter_displayh5p
  19   *
  20   * @package    filter_displayh5p
  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 filter_displayh5p;
  27  
  28  use filter_displayh5p;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once($CFG->dirroot.'/filter/displayh5p/filter.php');
  34  
  35  /**
  36   * Unit tests for the display H5P filter.
  37   *
  38   * @copyright 2019 Victor Deniz <victor@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class filter_test extends \advanced_testcase {
  42  
  43      public function setUp(): void {
  44          parent::setUp();
  45  
  46          $this->resetAfterTest(true);
  47  
  48          set_config('allowedsources',
  49              "https://moodle.h5p.com/content/[id]/embed\nhttps://moodle.h5p.com/content/[id]
  50                  \nhttps://generic.wordpress.soton.ac.uk/altc/wp-admin/admin-ajax.php?action=h5p_embed&id=[id]",
  51              'filter_displayh5p');
  52      }
  53  
  54      /**
  55       * Check that h5p tags with urls from allowed domains are filtered.
  56       *
  57       * @param string $text Original text
  58       * @param string $filteredtextpattern Text pattern after display H5P filter
  59       *
  60       * @dataProvider texts_provider
  61       */
  62      public function test_filter_urls($text, $filteredtextpattern) {
  63  
  64          $filterplugin = new filter_displayh5p(null, array());
  65  
  66          $filteredtext = $filterplugin->filter($text);
  67          $this->assertMatchesRegularExpression($filteredtextpattern, $filteredtext);
  68      }
  69  
  70      /**
  71       * Provides texts to filter for the {@link self::test_filter_urls} method.
  72       *
  73       * @return array
  74       */
  75      public function texts_provider() {
  76          global $CFG;
  77  
  78          return [
  79              ["http:://example.com", "#http:://example.com#"],
  80              ["http://google.es/h5p/embed/3425234", "#http://google.es/h5p/embed/3425234#"],
  81              ["https://moodle.h5p.com/content/1290729733828858779/embed", "#<iframe src=\"https://moodle.h5p.com/content/1290729733828858779/embed\"[^>]+?>#"],
  82              ["https://moodle.h5p.com/content/1290729733828858779", "#<iframe src=\"https://moodle.h5p.com/content/1290729733828858779/embed\"[^>]+?>#"],
  83              ["<a href=\"https://moodle.h5p.com/content/1290848995208939539/embed\">https://moodle.h5p.com/content/1290848995208939539/embed</a>",
  84                  "#<iframe src=\"https://moodle.h5p.com/content/1290848995208939539/embed\"[^>]+?>#"],
  85              ["<a href=\"https://moodle.org\">https://moodle.h5p.com/content/1290848995208939539/embed</a>",
  86                  "#^((?!iframe).)*$#"],
  87              ["<a href=\"https://moodle.h5p.com/content/1290848995208939539/embed\">link</a>",  "#^((?!iframe).)*$#"],
  88              ["this is a text with an h5p url https://moodle.h5p.com/content/1290848995208939539/embed inside",
  89                      "#this is a text with an h5p url <iframe src=\"https://moodle.h5p.com/content/1290848995208939539/embed\"(.|\n)*> inside#"],
  90              ["https://generic.wordpress.soton.ac.uk/altc/wp-admin/admin-ajax.php?action=h5p_embed&amp;id=13",
  91                      "#<iframe src=\"https://generic.wordpress.soton.ac.uk/altc/wp-admin/admin-ajax.php\?action=h5p_embed\&amp\;id=13\"[^>]+?>#"],
  92              ["https://moodle.h5p.com/content/1290848995208939539/embed another content in the same page https://moodle.h5p.com/content/1290729733828858779/embed",
  93                      "#<iframe src=\"https://moodle.h5p.com/content/1290848995208939539/embed\"[^>]+?>((?!<iframe).)*".
  94                      "<iframe src=\"https://moodle.h5p.com/content/1290729733828858779/embed\"[^>]+?>#"],
  95              [$CFG->wwwroot."/pluginfile.php/5/user/private/interactive-video.h5p?export=1&embed=1",
  96                      "#<iframe src=\"{$CFG->wwwroot}/h5p/embed.php\?url=".rawurlencode("{$CFG->wwwroot}/pluginfile.php/5/user/private/interactive-video.h5p").
  97                      "&export=1&embed=1\"[^>]*?></iframe>#"],
  98              [$CFG->wwwroot."/pluginfile.php/5/user/private/accordion-6-7138%20%281%29.h5p.h5p",
  99                      "#<iframe src=\"{$CFG->wwwroot}/h5p/embed.php\?url=".rawurlencode("{$CFG->wwwroot}/pluginfile.php/5/user/private/accordion-6-7138%20%281%29.h5p.h5p").
 100                      "\"[^>]*?></iframe>#"]
 101          ];
 102      }
 103  }