Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
17
namespace media_youtube;
18
 
19
use core_media_manager;
20
 
21
/**
22
 * Test script for media embedding.
23
 *
24
 * @package media_youtube
25
 * @copyright 2016 Marina Glancy
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class player_test extends \advanced_testcase {
29
 
30
    /**
31
     * Pre-test setup. Preserves $CFG.
32
     */
33
    public function setUp(): void {
34
        parent::setUp();
35
 
36
        // Reset $CFG and $SERVER.
37
        $this->resetAfterTest();
38
 
39
        // Consistent initial setup: all players disabled.
40
        \core\plugininfo\media::set_enabled_plugins('youtube');
41
 
42
        // Pretend to be using Firefox browser (must support ogg for tests to work).
43
        \core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 ');
44
    }
45
 
46
    /**
47
     * Test that plugin is returned as enabled media plugin.
48
     */
11 efrain 49
    public function test_is_installed(): void {
1 efrain 50
        $sortorder = \core\plugininfo\media::get_enabled_plugins();
51
        $this->assertEquals(['youtube' => 'youtube'], $sortorder);
52
    }
53
 
54
    /**
55
     * Test supported link types
56
     */
11 efrain 57
    public function test_supported(): void {
1 efrain 58
        $manager = core_media_manager::instance();
59
 
60
        // Format: youtube.
61
        $url = new \moodle_url('http://www.youtube.com/watch?v=vyrwMmsufJc');
62
        $t = $manager->embed_url($url);
63
        $this->assertStringContainsString('</iframe>', $t);
64
        $url = new \moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
65
        $t = $manager->embed_url($url);
66
        $this->assertStringContainsString('</iframe>', $t);
67
        $url = new \moodle_url('http://m.youtube.com/watch?v=vyrwMmsufJc');
68
        $t = $manager->embed_url($url);
69
        $this->assertStringContainsString('</iframe>', $t);
70
 
71
        // Format: youtube video within playlist.
72
        $url = new \moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0');
73
        $t = $manager->embed_url($url);
74
        $this->assertStringContainsString('</iframe>', $t);
75
        $this->assertStringContainsString('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
76
 
77
        // Format: youtube video with start time.
78
        $url = new \moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s');
79
        $t = $manager->embed_url($url);
80
        $this->assertStringContainsString('</iframe>', $t);
81
        $this->assertStringContainsString('start=3611', $t);
82
 
83
        // Format: youtube video within playlist with start time.
84
        $url = new \moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s');
85
        $t = $manager->embed_url($url);
86
        $this->assertStringContainsString('</iframe>', $t);
87
        $this->assertStringContainsString('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
88
        $this->assertStringContainsString('start=65', $t);
89
 
90
        // Format: youtube video with invalid parameter values (injection attempts).
91
        $url = new \moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_">');
92
        $t = $manager->embed_url($url);
93
        $this->assertStringContainsString('</iframe>', $t);
94
        $this->assertStringNotContainsString('list=PLxcO_', $t); // We shouldn't get a list param as input was invalid.
95
        $url = new \moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=">');
96
        $t = $manager->embed_url($url);
97
        $this->assertStringContainsString('</iframe>', $t);
98
        $this->assertStringNotContainsString('start=', $t); // We shouldn't get a start param as input was invalid.
99
 
100
        // Format: youtube playlist.
101
        $url = new \moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662');
102
        $t = $manager->embed_url($url);
103
        $this->assertStringContainsString('</iframe>', $t);
104
        $url = new \moodle_url('http://www.youtube.com/playlist?list=PL6E18E2927047B662');
105
        $t = $manager->embed_url($url);
106
        $this->assertStringContainsString('</iframe>', $t);
107
        $url = new \moodle_url('http://www.youtube.com/p/PL6E18E2927047B662');
108
        $t = $manager->embed_url($url);
109
        $this->assertStringContainsString('</iframe>', $t);
110
 
111
    }
112
 
113
    /**
114
     * Test embedding without media filter (for example for displaying URL resorce).
115
     */
11 efrain 116
    public function test_embed_url(): void {
1 efrain 117
        global $CFG;
118
 
119
        $url = new \moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
120
 
121
        $manager = core_media_manager::instance();
122
        $embedoptions = array(
123
            core_media_manager::OPTION_TRUSTED => true,
124
            core_media_manager::OPTION_BLOCK => true,
125
        );
126
 
127
        $this->assertTrue($manager->can_embed_url($url, $embedoptions));
128
        $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
129
 
130
        $this->assertMatchesRegularExpression('~mediaplugin_youtube~', $content);
131
        $this->assertMatchesRegularExpression('~</iframe>~', $content);
132
        $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
133
            $CFG->media_default_height . '"~', $content);
134
 
135
        // Repeat sending the specific size to the manager.
136
        $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions);
137
        $this->assertMatchesRegularExpression('~width="123" height="50"~', $content);
138
    }
139
 
140
    /**
141
     * Test that mediaplugin filter replaces a link to the supported file with media tag.
142
     *
143
     * filter_mediaplugin is enabled by default.
144
     */
11 efrain 145
    public function test_embed_link(): void {
1 efrain 146
        global $CFG;
147
        $url = new \moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
148
        $text = \html_writer::link($url, 'Watch this one');
149
        $content = format_text($text, FORMAT_HTML);
150
 
151
        $this->assertMatchesRegularExpression('~mediaplugin_youtube~', $content);
152
        $this->assertMatchesRegularExpression('~</iframe>~', $content);
153
        $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
154
            $CFG->media_default_height . '"~', $content);
155
    }
156
 
157
    /**
158
     * Test that mediaplugin filter adds player code on top of <video> tags.
159
     *
160
     * filter_mediaplugin is enabled by default.
161
     */
11 efrain 162
    public function test_embed_media(): void {
1 efrain 163
        global $CFG;
164
        $url = new \moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
165
        $trackurl = new \moodle_url('http://example.org/some_filename.vtt');
166
        $text = '<video controls="true"><source src="'.$url.'"/>' .
167
            '<track src="'.$trackurl.'">Unsupported text</video>';
168
        $content = format_text($text, FORMAT_HTML);
169
 
170
        $this->assertMatchesRegularExpression('~mediaplugin_youtube~', $content);
171
        $this->assertMatchesRegularExpression('~</iframe>~', $content);
172
        $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
173
            $CFG->media_default_height . '"~', $content);
174
        // Video tag, unsupported text and tracks are removed.
175
        $this->assertDoesNotMatchRegularExpression('~</video>~', $content);
176
        $this->assertDoesNotMatchRegularExpression('~<source\b~', $content);
177
        $this->assertDoesNotMatchRegularExpression('~Unsupported text~', $content);
178
        $this->assertDoesNotMatchRegularExpression('~<track\b~i', $content);
179
 
180
        // Video with dimensions and source specified as src attribute without <source> tag.
181
        $text = '<video controls="true" width="123" height="35" src="'.$url.'">Unsupported text</video>';
182
        $content = format_text($text, FORMAT_HTML);
183
        $this->assertMatchesRegularExpression('~mediaplugin_youtube~', $content);
184
        $this->assertMatchesRegularExpression('~</iframe>~', $content);
185
        $this->assertMatchesRegularExpression('~width="123" height="35"~', $content);
186
    }
187
 
188
    /**
189
     * Test that YouTube media plugin renders embed code correctly
190
     * when the "nocookie" config options is set to true.
191
     *
192
     * @covers \media_youtube_plugin::embed_external
193
     */
11 efrain 194
    public function test_youtube_nocookie(): void {
1 efrain 195
        // Turn on the no cookie option.
196
        set_config('nocookie', true, 'media_youtube');
197
 
198
        // Test that the embed code contains the no cookie domain.
199
        $url = new \moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
200
        $text = \html_writer::link($url, 'Watch this one');
201
        $content = format_text($text, FORMAT_HTML);
202
        $this->assertMatchesRegularExpression('~youtube-nocookie~', $content);
203
 
204
        // Next test for a playlist.
205
        $url = new \moodle_url('https://www.youtube.com/playlist?list=PL59FEE129ADFF2B12');
206
        $text = \html_writer::link($url, 'Great Playlist');
207
        $content = format_text($text, FORMAT_HTML);
208
        $this->assertMatchesRegularExpression('~youtube-nocookie~', $content);
209
    }
210
}