2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Test classes for handling embedded media.
20 * @package media_videojs
21 * @copyright 2016 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 * Test script for media embedding.
30 * @package media_videojs
31 * @copyright 2016 Marina Glancy
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class media_videojs_player_testcase extends advanced_testcase {
37 * Pre-test setup. Preserves $CFG.
39 public function setUp(): void {
42 // Reset $CFG and $SERVER.
43 $this->resetAfterTest();
45 // Consistent initial setup: all players disabled.
46 \core\plugininfo\media::set_enabled_plugins('videojs');
48 // Pretend to be using Firefox browser (must support ogg for tests to work).
49 core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 ');
53 * Test that plugin is returned as enabled media plugin.
55 public function test_is_installed() {
56 $sortorder = \core\plugininfo\media::get_enabled_plugins();
57 $this->assertEquals(['videojs' => 'videojs'], $sortorder);
61 * Test method get_supported_extensions()
63 public function test_supported_extensions() {
64 $supportedextensions = array_merge(file_get_typegroup('extension', 'html_video'),
65 file_get_typegroup('extension', 'html_audio'), file_get_typegroup('extension', 'media_source'));
67 set_config('useflash', 0, 'media_videojs');
69 // Make sure that the list of extensions from the setting is filtered to HTML5 natively supported extensions.
70 $player = new media_videojs_plugin();
71 $this->assertTrue(in_array('.mp3', $player->get_supported_extensions()));
72 $this->assertEmpty(array_diff($player->get_supported_extensions(), $supportedextensions));
74 // Try to set the audioextensions to something non-native (.ra) and make sure it is not returned as supported.
75 set_config('audioextensions', '.mp3,.wav,.ra', 'media_videojs');
76 $player = new media_videojs_plugin();
77 $this->assertNotEmpty($player->get_supported_extensions());
78 $this->assertTrue(in_array('.mp3', $player->get_supported_extensions()));
79 $this->assertFalse(in_array('.ra', $player->get_supported_extensions()));
80 $this->assertEmpty(array_diff($player->get_supported_extensions(), $supportedextensions));
82 // Try to use flash extensions and make sure they are not returned as supported.
83 set_config('videoextensions', '.flv,.f4v', 'media_videojs');
84 $player = new media_videojs_plugin();
85 $this->assertFalse(in_array('.flv', $player->get_supported_extensions()));
86 $this->assertFalse(in_array('.f4v', $player->get_supported_extensions()));
88 // Enable flash and test if flash extenstions are supported.
89 set_config('useflash', 1, 'media_videojs');
90 set_config('videoextensions', '.flv,.f4v', 'media_videojs');
91 $player = new media_videojs_plugin();
92 $this->assertTrue(in_array('.flv', $player->get_supported_extensions()));
93 $this->assertTrue(in_array('.f4v', $player->get_supported_extensions()));
97 * Test embedding without media filter (for example for displaying file resorce).
99 public function test_embed_url() {
102 $url = new moodle_url('http://example.org/1.webm');
104 $manager = core_media_manager::instance();
105 $embedoptions = array(
106 core_media_manager::OPTION_TRUSTED => true,
107 core_media_manager::OPTION_BLOCK => true,
110 $this->assertTrue($manager->can_embed_url($url, $embedoptions));
111 $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
113 $this->assertRegExp('~mediaplugin_videojs~', $content);
114 $this->assertRegExp('~</video>~', $content);
115 $this->assertRegExp('~title="Test & file"~', $content);
116 $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content);
118 // Repeat sending the specific size to the manager.
119 $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions);
120 $this->assertRegExp('~style="max-width:123px;~', $content);
122 // Repeat without sending the size and with unchecked setting to limit the video size.
123 set_config('limitsize', false, 'media_videojs');
125 $manager = core_media_manager::instance();
126 $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
127 $this->assertNotRegExp('~style="max-width:~', $content);
131 * Test that mediaplugin filter replaces a link to the supported file with media tag.
133 * filter_mediaplugin is enabled by default.
135 public function test_embed_link() {
137 $url = new moodle_url('http://example.org/some_filename.mp4');
138 $text = html_writer::link($url, 'Watch this one');
139 $content = format_text($text, FORMAT_HTML);
141 $this->assertRegExp('~mediaplugin_videojs~', $content);
142 $this->assertRegExp('~</video>~', $content);
143 $this->assertRegExp('~title="Watch this one"~', $content);
144 $this->assertNotRegExp('~<track\b~i', $content);
145 $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content);
149 * Test that only supported URLs are listed as sources but all URLs are present in links fallbacks.
151 public function test_fallback() {
154 new moodle_url('http://example.org/1.rv'), // Not supported.
155 new moodle_url('http://example.org/2.webm'), // Supported.
156 new moodle_url('http://example.org/3.ogv'), // Supported.
159 $manager = core_media_manager::instance();
160 $content = $manager->embed_alternatives($urls, '', 0, 0, []);
162 $this->assertRegExp('~mediaplugin_videojs~', $content);
163 $this->assertRegExp('~</video>~', $content);
164 // Title is taken from the name of the first supported file.
165 $this->assertRegExp('~title="2"~', $content);
166 // Only supported files are in <source>'s.
167 $this->assertNotRegExp('~<source src="http://example.org/1.rv"~', $content);
168 $this->assertRegExp('~<source src="http://example.org/2.webm"~', $content);
169 $this->assertRegExp('~<source src="http://example.org/3.ogv"~', $content);
170 // Links to all files are included.
171 $this->assertRegExp('~<a class="mediafallbacklink" href="http://example.org/1.rv">1.rv</a>~', $content);
172 $this->assertRegExp('~<a class="mediafallbacklink" href="http://example.org/2.webm">2.webm</a>~', $content);
173 $this->assertRegExp('~<a class="mediafallbacklink" href="http://example.org/3.ogv">3.ogv</a>~', $content);
177 * Assert other players do not apply after videojs was applied.
179 public function test_prevent_other_players() {
180 \core\plugininfo\media::set_enabled_plugins('videojs,html5video');
181 $url = new moodle_url('http://example.org/some_filename.webm');
182 $text = html_writer::link($url, 'Apply one player only');
183 $content = format_text($text, FORMAT_HTML);
185 $this->assertRegExp('~mediaplugin_videojs~', $content);
186 $this->assertEquals(1, substr_count($content, '</video>'));
187 $this->assertNotRegExp('~mediaplugin_html5video~', $content);
188 $this->assertRegExp('~<a class="mediafallbacklink" href="http://example.org/some_filename.webm">Apply one player only</a>~', $content);
192 * Test that mediaplugin filter adds player code on top of <video> tags.
194 * filter_mediaplugin is enabled by default.
196 public function test_embed_media() {
198 $url = new moodle_url('http://example.org/some_filename.mp4');
199 $trackurl = new moodle_url('http://example.org/some_filename.vtt');
200 $text = '<video controls="true"><source src="'.$url.'"/><source src="somethinginvalid"/>' .
201 '<track src="'.$trackurl.'">Unsupported text</video>';
202 $content = format_text($text, FORMAT_HTML);
204 $this->assertRegExp('~mediaplugin_videojs~', $content);
205 $this->assertRegExp('~</video>~', $content);
206 $this->assertRegExp('~title="some_filename.mp4"~', $content);
207 $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content);
208 // Unsupported text and tracks are preserved.
209 $this->assertRegExp('~Unsupported text~', $content);
210 $this->assertRegExp('~<track\b~i', $content);
211 // Invalid sources are removed.
212 $this->assertNotRegExp('~somethinginvalid~i', $content);
214 // Video with dimensions and source specified as src attribute without <source> tag.
215 $text = '<video controls="true" width="123" height="35" src="'.$url.'">Unsupported text</video>';
216 $content = format_text($text, FORMAT_HTML);
217 $this->assertRegExp('~mediaplugin_videojs~', $content);
218 $this->assertRegExp('~</video>~', $content);
219 $this->assertRegExp('~<source\b~', $content);
220 $this->assertRegExp('~style="max-width:123px;~', $content);
221 $this->assertNotRegExp('~width="~', $content);
222 $this->assertNotRegExp('~height="~', $content);
225 $url = new moodle_url('http://example.org/some_filename.mp3');
226 $trackurl = new moodle_url('http://example.org/some_filename.vtt');
227 $text = '<audio controls="true"><source src="'.$url.'"/><source src="somethinginvalid"/>' .
228 '<track src="'.$trackurl.'">Unsupported text</audio>';
229 $content = format_text($text, FORMAT_HTML);
231 $this->assertRegExp('~mediaplugin_videojs~', $content);
232 $this->assertNotRegExp('~</video>~', $content);
233 $this->assertRegExp('~</audio>~', $content);
234 $this->assertRegExp('~title="some_filename.mp3"~', $content);
235 $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content);
236 // Unsupported text and tracks are preserved.
237 $this->assertRegExp('~Unsupported text~', $content);
238 $this->assertRegExp('~<track\b~i', $content);
239 // Invalid sources are removed.
240 $this->assertNotRegExp('~somethinginvalid~i', $content);
244 * Helper function for testing youtube videos embedding.
246 * @param string $t output of core_media_manager::embed_url.
248 protected function youtube_plugin_engaged($t) {
249 $this->assertStringContainsString('mediaplugin_videojs', $t);
250 $this->assertStringContainsString('data-setup-lazy="{"techOrder": ["youtube"]', $t);
254 * Test that VideoJS can embed youtube videos.
256 public function test_youtube() {
257 set_config('youtube', 1, 'media_videojs');
258 set_config('useflash', 0, 'media_videojs');
260 $manager = core_media_manager::instance();
263 $url = new moodle_url('http://www.youtube.com/watch?v=vyrwMmsufJc');
264 $t = $manager->embed_url($url);
265 $this->youtube_plugin_engaged($t);
266 $url = new moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
267 $t = $manager->embed_url($url);
268 $this->youtube_plugin_engaged($t);
270 // Format: youtube video within playlist - this will be played by video.js but without tracks selection.
271 $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0');
272 $t = $manager->embed_url($url);
273 $this->youtube_plugin_engaged($t);
274 $this->assertStringContainsString('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
276 // Format: youtube playlist - not supported.
277 $url = new moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662');
278 $t = $manager->embed_url($url);
279 $this->assertStringNotContainsString('mediaplugin_videojs', $t);
280 $url = new moodle_url('http://www.youtube.com/playlist?list=PL6E18E2927047B662');
281 $t = $manager->embed_url($url);
282 $this->assertStringNotContainsString('mediaplugin_videojs', $t);
283 $url = new moodle_url('http://www.youtube.com/p/PL6E18E2927047B662');
284 $t = $manager->embed_url($url);
285 $this->assertStringNotContainsString('mediaplugin_videojs', $t);
289 * Data provider for {@see test_youtube_start_time}
293 public function youtube_start_time_provider(): array {
295 ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s', 3611],
296 ['https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s', 65],
297 ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h10m30s', 4230],
298 ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=3m', 180],
299 ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=43s', 43],
300 ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1234', 1234],
301 ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=invalid', 0],
306 * Test Youtube video embedding with URL's containing start time interval
309 * @param int $expectedstart
311 * @dataProvider youtube_start_time_provider
313 public function test_youtube_start_time(string $url, int $expectedstart) {
314 set_config('youtube', 1, 'media_videojs');
315 set_config('useflash', 0, 'media_videojs');
317 $embedcode = core_media_manager::instance()->embed_url(new moodle_url($url));
319 $this->youtube_plugin_engaged($embedcode);
320 $this->assertStringContainsString(""youtube": {"start": "{$expectedstart}"}", $embedcode);
324 * Helper function for testing flash videos embedding.
326 * @param string $t output of core_media_manager::embed_url.
328 protected function flash_plugin_engaged($t) {
329 $this->assertStringContainsString('mediaplugin_videojs', $t);
330 $this->assertStringContainsString('data-setup-lazy="{"techOrder": ["flash", "html5"]', $t);
334 * Test that VideoJS can embed flash videos.
336 public function test_flash() {
337 $manager = core_media_manager::instance();
340 set_config('useflash', 1, 'media_videojs');
341 $url = new moodle_url('http://example.org/some_filename.flv');
342 $t = $manager->embed_url($url);
343 $this->flash_plugin_engaged($t);
344 $this->assertRegExp('~</video>~', $t);
345 $this->assertRegExp('~<source src="http://example.org/some_filename.flv"~', $t);
346 $this->assertRegExp('~<a class="mediafallbacklink" href="http://example.org/some_filename.flv">some_filename.flv</a>~', $t);
349 set_config('useflash', 0, 'media_videojs');
350 $url = new moodle_url('http://example.org/some_filename.flv');
351 $t = $manager->embed_url($url);
352 $this->assertStringNotContainsString('mediaplugin_videojs', $t);
353 $this->assertRegExp('~<a class="mediafallbacklink" href="http://example.org/some_filename.flv">some_filename.flv</a>~', $t);
357 * Test that VideoJS can embed RTMP streams.
359 public function test_rtmp() {
360 $manager = core_media_manager::instance();
362 // RTMP disabled, flash disabled.
363 set_config('useflash', 0, 'media_videojs');
364 set_config('rtmp', 0, 'media_videojs');
365 $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4');
366 $t = $manager->embed_url($url);
367 $this->assertStringNotContainsString('mediaplugin_videojs', $t);
368 $this->assertRegExp('~<a class="mediafallbacklink" href="rtmp://example.com/fms&mp4:path/to/file.mp4">file.mp4</a>~', $t);
370 // RTMP enabled, flash disabled.
371 set_config('useflash', 0, 'media_videojs');
372 set_config('rtmp', 1, 'media_videojs');
373 $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4');
374 $t = $manager->embed_url($url);
375 $this->assertStringNotContainsString('mediaplugin_videojs', $t);
376 $this->assertRegExp('~<a class="mediafallbacklink" href="rtmp://example.com/fms&mp4:path/to/file.mp4">file.mp4</a>~', $t);
378 // RTMP enabled, flash enabled, rtmp/mp4 type expected.
379 set_config('useflash', 1, 'media_videojs');
380 set_config('rtmp', 1, 'media_videojs');
381 $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4');
382 $t = $manager->embed_url($url);
383 $this->flash_plugin_engaged($t);
384 $this->assertRegExp('~</video>~', $t);
385 $this->assertRegExp('~<source src="rtmp://example.com/fms&mp4:path/to/file.mp4" type="rtmp/mp4"~', $t);
386 $this->assertRegExp('~<a class="mediafallbacklink" href="rtmp://example.com/fms&mp4:path/to/file.mp4">file.mp4</a>~', $t);
388 // RTMP enabled, flash enabled, rtmp/flv type expected.
389 set_config('useflash', 1, 'media_videojs');
390 set_config('rtmp', 1, 'media_videojs');
391 $url = new moodle_url('rtmp://example.com/fms&flv:path/to/file.flv');
392 $t = $manager->embed_url($url);
393 $this->flash_plugin_engaged($t);
394 $this->assertRegExp('~</video>~', $t);
395 $this->assertRegExp('~<source src="rtmp://example.com/fms&flv:path/to/file.flv" type="rtmp/flv"~', $t);
396 $this->assertRegExp('~<a class="mediafallbacklink" href="rtmp://example.com/fms&flv:path/to/file.flv">file.flv</a>~', $t);