1 |
efrain |
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 |
|
1441 |
ariadna |
17 |
namespace filter_mediaplugin;
|
|
|
18 |
|
1 |
efrain |
19 |
/**
|
|
|
20 |
* Unit test for the filter_mediaplugin
|
|
|
21 |
*
|
|
|
22 |
* @package filter_mediaplugin
|
1441 |
ariadna |
23 |
* @category test
|
1 |
efrain |
24 |
* @copyright 2011 Rossiani Wijaya <rwijaya@moodle.com>
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
1441 |
ariadna |
26 |
* @covers \filter_mediaplugin\text_filter
|
1 |
efrain |
27 |
*/
|
1441 |
ariadna |
28 |
final class filter_test extends \advanced_testcase {
|
|
|
29 |
public function test_text_filter_link(): void {
|
1 |
efrain |
30 |
$this->resetAfterTest(true);
|
|
|
31 |
|
|
|
32 |
// We need to enable the media plugins.
|
|
|
33 |
\core\plugininfo\media::set_enabled_plugins('vimeo,youtube,videojs,html5video,html5audio');
|
|
|
34 |
|
1441 |
ariadna |
35 |
$filterplugin = new text_filter(null, []);
|
1 |
efrain |
36 |
|
|
|
37 |
$longurl = '<a href="http://moodle/.mp4">my test file</a>';
|
|
|
38 |
$longhref = '';
|
|
|
39 |
|
|
|
40 |
do {
|
|
|
41 |
$longhref .= 'a';
|
1441 |
ariadna |
42 |
} while (strlen($longhref) + strlen($longurl) < 4095);
|
1 |
efrain |
43 |
|
|
|
44 |
$longurl = '<a href="http://moodle/' . $longhref . '.mp4">my test file</a>';
|
|
|
45 |
|
1441 |
ariadna |
46 |
$validtexts = [
|
1 |
efrain |
47 |
'<a href="http://moodle.org/testfile/test.mp3">test mp3</a>',
|
|
|
48 |
'<a href="http://moodle.org/testfile/test.ogg">test ogg</a>',
|
|
|
49 |
'<a id="movie player" class="center" href="http://moodle.org/testfile/test.mp4">test mp4</a>',
|
|
|
50 |
'<a href="http://moodle.org/testfile/test.webm">test</a>',
|
|
|
51 |
'<a href="http://www.youtube.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>',
|
|
|
52 |
'<a href="http://www.youtube-nocookie.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>',
|
|
|
53 |
'<a href="http://youtu.be/JghQgA2HMX8" class="href=css">test file</a>',
|
|
|
54 |
'<a href="http://y2u.be/JghQgA2HMX8" class="href=css">test file</a>',
|
|
|
55 |
'<a class="youtube" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>',
|
|
|
56 |
'<a class="hrefcss" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>',
|
|
|
57 |
'<a class="content" href="http://moodle.org/testfile/test.ogg">test ogg</a>',
|
|
|
58 |
'<a id="audio" href="http://moodle.org/testfile/test.mp3">test mp3</a>',
|
|
|
59 |
'<a href="http://moodle.org/testfile/test.mp3">test mp3</a>',
|
|
|
60 |
'<a href="http://moodle.org/testfile/test.mp3">test mp3</a>',
|
|
|
61 |
'<a href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200">youtube\'s</a>',
|
|
|
62 |
'<a
|
|
|
63 |
href="http://moodle.org/testfile/test.mp3">
|
|
|
64 |
test mp3</a>',
|
|
|
65 |
'<a class="content"
|
|
|
66 |
|
|
|
67 |
|
|
|
68 |
href="http://moodle.org/testfile/test.wav">test wav
|
|
|
69 |
</a>',
|
|
|
70 |
'<a href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200" >youtube\'s</a>',
|
|
|
71 |
// Test a long URL under 4096 characters.
|
1441 |
ariadna |
72 |
$longurl,
|
|
|
73 |
];
|
1 |
efrain |
74 |
|
1441 |
ariadna |
75 |
// Test for valid link.
|
1 |
efrain |
76 |
foreach ($validtexts as $text) {
|
1441 |
ariadna |
77 |
$msg = "Testing text: " . $text;
|
1 |
efrain |
78 |
$filter = $filterplugin->filter($text);
|
|
|
79 |
$this->assertNotEquals($text, $filter, $msg);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$insertpoint = strrpos($longurl, 'http://');
|
|
|
83 |
$longurl = substr_replace($longurl, 'http://pushover4096chars', $insertpoint, 0);
|
|
|
84 |
|
|
|
85 |
$originalurl = '<p>Some text.</p><pre style="color: rgb(0, 0, 0); line-height: normal;">' .
|
1441 |
ariadna |
86 |
'<a href="https://www.youtube.com/watch?v=uUhWl9Lm3OM">Valid link</a>' .
|
|
|
87 |
'</pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
|
1 |
efrain |
88 |
$paddedurl = str_pad($originalurl, 6000, 'z');
|
1441 |
ariadna |
89 |
$validpaddedurl = '<p>Some text.</p>' .
|
|
|
90 |
'<pre style="color: rgb(0, 0, 0); line-height: normal;">' .
|
|
|
91 |
'<span class="mediaplugin mediaplugin_youtube">
|
1 |
efrain |
92 |
<iframe title="Valid link" width="640" height="360" style="border:0;"
|
|
|
93 |
src="https://www.youtube.com/embed/uUhWl9Lm3OM?rel=0&wmode=transparent" allow="fullscreen" loading="lazy"></iframe>
|
|
|
94 |
</span></pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
|
|
|
95 |
$validpaddedurl = str_pad($validpaddedurl, 6000 + (strlen($validpaddedurl) - strlen($originalurl)), 'z');
|
|
|
96 |
|
1441 |
ariadna |
97 |
$invalidtexts = [
|
1 |
efrain |
98 |
'<a class="_blanktarget">href="http://moodle.org/testfile/test.mp3"</a>',
|
|
|
99 |
'<a>test test</a>',
|
|
|
100 |
'<a >test test</a>',
|
|
|
101 |
'<a >test test</a>',
|
|
|
102 |
'<a >test test</a>',
|
|
|
103 |
'<ahref="http://moodle.org/testfile/test.mp3">sample</a>',
|
|
|
104 |
'<a href="" test></a>',
|
|
|
105 |
'<a href="http://www.moodle.com/path/to?#param=29">test</a>',
|
|
|
106 |
'<a href="http://moodle.org/testfile/test.mp3">test mp3',
|
|
|
107 |
'<a href="http://moodle.org/testfile/test.mp3"test</a>',
|
|
|
108 |
'<a href="http://moodle.org/testfile/">test</a>',
|
|
|
109 |
'<href="http://moodle.org/testfile/test.avi">test</a>',
|
|
|
110 |
'<abbr href="http://moodle.org/testfile/test.mp3">test mp3</abbr>',
|
|
|
111 |
'<ahref="http://moodle.org/testfile/test.mp3">test mp3</a>',
|
|
|
112 |
'<aclass="content" href="http://moodle.org/testfile/test.mp3">test mp3</a>',
|
|
|
113 |
// Test a long URL over 4096 characters.
|
1441 |
ariadna |
114 |
$longurl,
|
|
|
115 |
];
|
1 |
efrain |
116 |
|
1441 |
ariadna |
117 |
// Test for invalid link.
|
1 |
efrain |
118 |
foreach ($invalidtexts as $text) {
|
1441 |
ariadna |
119 |
$msg = "Testing text: " . $text;
|
1 |
efrain |
120 |
$filter = $filterplugin->filter($text);
|
|
|
121 |
$this->assertEquals($text, $filter, $msg);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
// Valid mediaurl followed by a longurl.
|
1441 |
ariadna |
125 |
$precededlongurl = '<a href="http://moodle.org/testfile/test.mp3">test.mp3</a>' . $longurl;
|
1 |
efrain |
126 |
$filter = $filterplugin->filter($precededlongurl);
|
|
|
127 |
$this->assertEquals(1, substr_count($filter, '</audio>'));
|
|
|
128 |
$this->assertStringContainsString($longurl, $filter);
|
|
|
129 |
|
|
|
130 |
// Testing for cases where: to be filtered content has 6+ text afterwards.
|
|
|
131 |
$filter = $filterplugin->filter($paddedurl);
|
|
|
132 |
$this->assertEquals($validpaddedurl, $filter, $msg);
|
|
|
133 |
}
|
|
|
134 |
}
|