Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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