. /** * Test classes for handling embedded media. * * @package media_videojs * @copyright 2016 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ defined('MOODLE_INTERNAL') || die(); /** * Test script for media embedding. * * @package media_videojs * @copyright 2016 Marina Glancy * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class media_videojs_testcase extends advanced_testcase { /** * Pre-test setup. Preserves $CFG. */ public function setUp() { parent::setUp(); // Reset $CFG and $SERVER. $this->resetAfterTest(); // Consistent initial setup: all players disabled. \core\plugininfo\media::set_enabled_plugins('videojs'); // Pretend to be using Firefox browser (must support ogg for tests to work). core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 '); } /** * Test that plugin is returned as enabled media plugin. */ public function test_is_installed() { $sortorder = \core\plugininfo\media::get_enabled_plugins(); $this->assertEquals(['videojs' => 'videojs'], $sortorder); } /** * Test method get_supported_extensions() */ public function test_supported_extensions() { $nativeextensions = array_merge(file_get_typegroup('extension', 'html_video'), file_get_typegroup('extension', 'html_audio')); set_config('useflash', 0, 'media_videojs'); // Make sure that the list of extensions from the setting is filtered to HTML5 natively supported extensions. $player = new media_videojs_plugin(); $this->assertNotEmpty($player->get_supported_extensions()); $this->assertTrue(in_array('.mp3', $player->get_supported_extensions())); $this->assertEmpty(array_diff($player->get_supported_extensions(), $nativeextensions)); // Try to set the audioextensions to something non-native (.ra) and make sure it is not returned as supported. set_config('audioextensions', '.mp3,.wav,.ra', 'media_videojs'); $player = new media_videojs_plugin(); $this->assertNotEmpty($player->get_supported_extensions()); $this->assertTrue(in_array('.mp3', $player->get_supported_extensions())); $this->assertFalse(in_array('.ra', $player->get_supported_extensions())); $this->assertEmpty(array_diff($player->get_supported_extensions(), $nativeextensions)); // Try to use flash extensions and make sure they are not returned as supported. set_config('videoextensions', '.flv,.f4v', 'media_videojs'); $player = new media_videojs_plugin(); $this->assertFalse(in_array('.flv', $player->get_supported_extensions())); $this->assertFalse(in_array('.f4v', $player->get_supported_extensions())); // Enable flash and test if flash extenstions are supported. set_config('useflash', 1, 'media_videojs'); set_config('videoextensions', '.flv,.f4v', 'media_videojs'); $player = new media_videojs_plugin(); $this->assertTrue(in_array('.flv', $player->get_supported_extensions())); $this->assertTrue(in_array('.f4v', $player->get_supported_extensions())); } /** * Test embedding without media filter (for example for displaying file resorce). */ public function test_embed_url() { global $CFG; $url = new moodle_url('http://example.org/1.webm'); $manager = core_media_manager::instance(); $embedoptions = array( core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true, ); $this->assertTrue($manager->can_embed_url($url, $embedoptions)); $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions); $this->assertRegExp('~mediaplugin_videojs~', $content); $this->assertRegExp('~~', $content); $this->assertRegExp('~title="Test & file"~', $content); $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content); // Repeat sending the specific size to the manager. $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions); $this->assertRegExp('~style="max-width:123px;~', $content); // Repeat without sending the size and with unchecked setting to limit the video size. set_config('limitsize', false, 'media_videojs'); $manager = core_media_manager::instance(); $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions); $this->assertNotRegExp('~style="max-width:~', $content); } /** * Test that mediaplugin filter replaces a link to the supported file with media tag. * * filter_mediaplugin is enabled by default. */ public function test_embed_link() { global $CFG; $url = new moodle_url('http://example.org/some_filename.mp4'); $text = html_writer::link($url, 'Watch this one'); $content = format_text($text, FORMAT_HTML); $this->assertRegExp('~mediaplugin_videojs~', $content); $this->assertRegExp('~~', $content); $this->assertRegExp('~title="Watch this one"~', $content); $this->assertNotRegExp('~assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content); } /** * Test that only supported URLs are listed as sources but all URLs are present in links fallbacks. */ public function test_fallback() { $urls = [ new moodle_url('http://example.org/1.rv'), // Not supported. new moodle_url('http://example.org/2.webm'), // Supported. new moodle_url('http://example.org/3.ogv'), // Supported. ]; $manager = core_media_manager::instance(); $content = $manager->embed_alternatives($urls, '', 0, 0, []); $this->assertRegExp('~mediaplugin_videojs~', $content); $this->assertRegExp('~~', $content); // Title is taken from the name of the first supported file. $this->assertRegExp('~title="2"~', $content); // Only supported files are in 's. $this->assertNotRegExp('~assertRegExp('~assertRegExp('~assertRegExp('~1.rv~', $content); $this->assertRegExp('~2.webm~', $content); $this->assertRegExp('~3.ogv~', $content); } /** * Assert other players do not apply after videojs was applied. */ public function test_prevent_other_players() { \core\plugininfo\media::set_enabled_plugins('videojs,html5video'); $url = new moodle_url('http://example.org/some_filename.webm'); $text = html_writer::link($url, 'Apply one player only'); $content = format_text($text, FORMAT_HTML); $this->assertRegExp('~mediaplugin_videojs~', $content); $this->assertEquals(1, substr_count($content, '')); $this->assertNotRegExp('~mediaplugin_html5video~', $content); $this->assertRegExp('~Apply one player only~', $content); } /** * Test that mediaplugin filter adds player code on top of ~', $content); $this->assertRegExp('~title="some_filename.mp4"~', $content); $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content); // Unsupported text and tracks are preserved. $this->assertRegExp('~Unsupported text~', $content); $this->assertRegExp('~assertNotRegExp('~somethinginvalid~i', $content); // Video with dimensions and source specified as src attribute without tag. $text = ''; $content = format_text($text, FORMAT_HTML); $this->assertRegExp('~mediaplugin_videojs~', $content); $this->assertRegExp('~~', $content); $this->assertRegExp('~assertRegExp('~style="max-width:123px;~', $content); $this->assertNotRegExp('~width="~', $content); $this->assertNotRegExp('~height="~', $content); // Audio tag. $url = new moodle_url('http://example.org/some_filename.mp3'); $trackurl = new moodle_url('http://example.org/some_filename.vtt'); $text = ''; $content = format_text($text, FORMAT_HTML); $this->assertRegExp('~mediaplugin_videojs~', $content); $this->assertNotRegExp('~~', $content); $this->assertRegExp('~~', $content); $this->assertRegExp('~title="some_filename.mp3"~', $content); $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content); // Unsupported text and tracks are preserved. $this->assertRegExp('~Unsupported text~', $content); $this->assertRegExp('~assertNotRegExp('~somethinginvalid~i', $content); } /** * Helper function for testing youtube videos embedding. * * @param string $t output of core_media_manager::embed_url. */ protected function youtube_plugin_engaged($t) { $this->assertContains('mediaplugin_videojs', $t); $this->assertContains('data-setup-lazy="{"techOrder": ["youtube"]', $t); } /** * Test that VideoJS can embed youtube videos. */ public function test_youtube() { set_config('youtube', 1, 'media_videojs'); set_config('useflash', 0, 'media_videojs'); $manager = core_media_manager::instance(); // Format: youtube. $url = new moodle_url('http://www.youtube.com/watch?v=vyrwMmsufJc'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $url = new moodle_url('http://www.youtube.com/v/vyrwMmsufJc'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); // Format: youtube video within playlist - this will be played by video.js but without tracks selection. $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t); // Format: youtube video with start time. $url = new moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $this->assertContains('t=1h11s', $t); // Format: youtube video within playlist with start time. $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t); $this->assertContains('t=1m5s', $t); // Format: youtube playlist - not supported. $url = new moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); $url = new moodle_url('http://www.youtube.com/playlist?list=PL6E18E2927047B662'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); $url = new moodle_url('http://www.youtube.com/p/PL6E18E2927047B662'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); } /** * Helper function for testing flash videos embedding. * * @param string $t output of core_media_manager::embed_url. */ protected function flash_plugin_engaged($t) { $this->assertContains('mediaplugin_videojs', $t); $this->assertContains('data-setup-lazy="{"techOrder": ["flash", "html5"]', $t); } /** * Test that VideoJS can embed flash videos. */ public function test_flash() { $manager = core_media_manager::instance(); // Flash enabled. set_config('useflash', 1, 'media_videojs'); $url = new moodle_url('http://example.org/some_filename.flv'); $t = $manager->embed_url($url); $this->flash_plugin_engaged($t); $this->assertRegExp('~~', $t); $this->assertRegExp('~assertRegExp('~some_filename.flv~', $t); // Flash disabled. set_config('useflash', 0, 'media_videojs'); $url = new moodle_url('http://example.org/some_filename.flv'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); $this->assertRegExp('~some_filename.flv~', $t); } /** * Test that VideoJS can embed RTMP streams. */ public function test_rtmp() { $manager = core_media_manager::instance(); // RTMP disabled, flash disabled. set_config('useflash', 0, 'media_videojs'); set_config('rtmp', 0, 'media_videojs'); $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); $this->assertRegExp('~file.mp4~', $t); // RTMP enabled, flash disabled. set_config('useflash', 0, 'media_videojs'); set_config('rtmp', 1, 'media_videojs'); $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); $this->assertRegExp('~file.mp4~', $t); // RTMP enabled, flash enabled, rtmp/mp4 type expected. set_config('useflash', 1, 'media_videojs'); set_config('rtmp', 1, 'media_videojs'); $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4'); $t = $manager->embed_url($url); $this->flash_plugin_engaged($t); $this->assertRegExp('~~', $t); $this->assertRegExp('~assertRegExp('~file.mp4~', $t); // RTMP enabled, flash enabled, rtmp/flv type expected. set_config('useflash', 1, 'media_videojs'); set_config('rtmp', 1, 'media_videojs'); $url = new moodle_url('rtmp://example.com/fms&flv:path/to/file.flv'); $t = $manager->embed_url($url); $this->flash_plugin_engaged($t); $this->assertRegExp('~~', $t); $this->assertRegExp('~assertRegExp('~file.flv~', $t); } }