Merge branch 'MDL-79664' of https://github.com/paulholden/moodle
[moodle.git] / filter / displayh5p / tests / filter_test.php
blob9609c68c3e0e667b2ebc578c5484f3b12f60c05c
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.
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/>.
17 /**
18 * Unit tests for the filter_displayh5p
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
26 namespace filter_displayh5p;
28 use filter_displayh5p;
30 defined('MOODLE_INTERNAL') || die();
32 global $CFG;
33 require_once($CFG->dirroot.'/filter/displayh5p/filter.php');
35 /**
36 * Unit tests for the display H5P filter.
38 * @copyright 2019 Victor Deniz <victor@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class filter_test extends \advanced_testcase {
43 public function setUp(): void {
44 parent::setUp();
46 $this->resetAfterTest(true);
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');
54 /**
55 * Check that h5p tags with urls from allowed domains are filtered.
57 * @param string $text Original text
58 * @param string $filteredtextpattern Text pattern after display H5P filter
60 * @dataProvider texts_provider
62 public function test_filter_urls($text, $filteredtextpattern) {
64 $filterplugin = new filter_displayh5p(null, array());
66 $filteredtext = $filterplugin->filter($text);
67 $this->assertMatchesRegularExpression($filteredtextpattern, $filteredtext);
70 /**
71 * Provides texts to filter for the {@link self::test_filter_urls} method.
73 * @return array
75 public function texts_provider() {
76 global $CFG;
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>#"]