Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * Test classes for handling embedded media.
  19   *
  20   * @package media_videojs
  21   * @copyright 2016 Marina Glancy
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Test script for media embedding.
  29   *
  30   * @package media_videojs
  31   * @copyright 2016 Marina Glancy
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class media_videojs_player_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Pre-test setup. Preserves $CFG.
  38       */
  39      public function setUp(): void {
  40          parent::setUp();
  41  
  42          // Reset $CFG and $SERVER.
  43          $this->resetAfterTest();
  44  
  45          // Consistent initial setup: all players disabled.
  46          \core\plugininfo\media::set_enabled_plugins('videojs');
  47  
  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 ');
  50      }
  51  
  52      /**
  53       * Test that plugin is returned as enabled media plugin.
  54       */
  55      public function test_is_installed() {
  56          $sortorder = \core\plugininfo\media::get_enabled_plugins();
  57          $this->assertEquals(['videojs' => 'videojs'], $sortorder);
  58      }
  59  
  60      /**
  61       * Test method get_supported_extensions()
  62       */
  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'));
  66  
  67          set_config('useflash', 0, 'media_videojs');
  68  
  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));
  73  
  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));
  81  
  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()));
  87  
  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()));
  94      }
  95  
  96      /**
  97       * Test embedding without media filter (for example for displaying file resorce).
  98       */
  99      public function test_embed_url() {
 100          global $CFG;
 101  
 102          $url = new moodle_url('http://example.org/1.webm');
 103  
 104          $manager = core_media_manager::instance();
 105          $embedoptions = array(
 106              core_media_manager::OPTION_TRUSTED => true,
 107              core_media_manager::OPTION_BLOCK => true,
 108          );
 109  
 110          $this->assertTrue($manager->can_embed_url($url, $embedoptions));
 111          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
 112  
 113          $this->assertRegExp('~mediaplugin_videojs~', $content);
 114          $this->assertRegExp('~</video>~', $content);
 115          $this->assertRegExp('~title="Test &amp; file"~', $content);
 116          $this->assertRegExp('~style="max-width:' . $CFG->media_default_width . 'px;~', $content);
 117  
 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);
 121  
 122          // Repeat without sending the size and with unchecked setting to limit the video size.
 123          set_config('limitsize', false, 'media_videojs');
 124  
 125          $manager = core_media_manager::instance();
 126          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
 127          $this->assertNotRegExp('~style="max-width:~', $content);
 128      }
 129  
 130      /**
 131       * Test that mediaplugin filter replaces a link to the supported file with media tag.
 132       *
 133       * filter_mediaplugin is enabled by default.
 134       */
 135      public function test_embed_link() {
 136          global $CFG;
 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);
 140  
 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);
 146      }
 147  
 148      /**
 149       * Test that only supported URLs are listed as sources but all URLs are present in links fallbacks.
 150       */
 151      public function test_fallback() {
 152  
 153          $urls = [
 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.
 157          ];
 158  
 159          $manager = core_media_manager::instance();
 160          $content = $manager->embed_alternatives($urls, '', 0, 0, []);
 161  
 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);
 174      }
 175  
 176      /**
 177       * Assert other players do not apply after videojs was applied.
 178       */
 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);
 184  
 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);
 189      }
 190  
 191      /**
 192       * Test that mediaplugin filter adds player code on top of <video> tags.
 193       *
 194       * filter_mediaplugin is enabled by default.
 195       */
 196      public function test_embed_media() {
 197          global $CFG;
 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);
 203  
 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);
 213  
 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);
 223  
 224          // Audio tag.
 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);
 230  
 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);
 241      }
 242  
 243      /**
 244       * Helper function for testing youtube videos embedding.
 245       *
 246       * @param string $t output of core_media_manager::embed_url.
 247       */
 248      protected function youtube_plugin_engaged($t) {
 249          $this->assertStringContainsString('mediaplugin_videojs', $t);
 250          $this->assertStringContainsString('data-setup-lazy="{&quot;techOrder&quot;: [&quot;youtube&quot;]', $t);
 251      }
 252  
 253      /**
 254       * Test that VideoJS can embed youtube videos.
 255       */
 256      public function test_youtube() {
 257          set_config('youtube', 1, 'media_videojs');
 258          set_config('useflash', 0, 'media_videojs');
 259  
 260          $manager = core_media_manager::instance();
 261  
 262          // Format: youtube.
 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);
 269  
 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);
 275  
 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);
 286      }
 287  
 288      /**
 289       * Data provider for {@see test_youtube_start_time}
 290       *
 291       * @return array
 292       */
 293      public function youtube_start_time_provider(): array {
 294          return [
 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],
 302          ];
 303      }
 304  
 305      /**
 306       * Test Youtube video embedding with URL's containing start time interval
 307       *
 308       * @param string $url
 309       * @param int $expectedstart
 310       *
 311       * @dataProvider youtube_start_time_provider
 312       */
 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');
 316  
 317          $embedcode = core_media_manager::instance()->embed_url(new moodle_url($url));
 318  
 319          $this->youtube_plugin_engaged($embedcode);
 320          $this->assertStringContainsString("&quot;youtube&quot;: {&quot;start&quot;: &quot;{$expectedstart}&quot;}", $embedcode);
 321      }
 322  
 323      /**
 324       * Helper function for testing flash videos embedding.
 325       *
 326       * @param string $t output of core_media_manager::embed_url.
 327       */
 328      protected function flash_plugin_engaged($t) {
 329          $this->assertStringContainsString('mediaplugin_videojs', $t);
 330          $this->assertStringContainsString('data-setup-lazy="{&quot;techOrder&quot;: [&quot;flash&quot;, &quot;html5&quot;]', $t);
 331      }
 332  
 333      /**
 334       * Test that VideoJS can embed flash videos.
 335       */
 336      public function test_flash() {
 337          $manager = core_media_manager::instance();
 338  
 339          // Flash enabled.
 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);
 347  
 348          // Flash disabled.
 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);
 354      }
 355  
 356      /**
 357       * Test that VideoJS can embed RTMP streams.
 358       */
 359      public function test_rtmp() {
 360          $manager = core_media_manager::instance();
 361  
 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);
 369  
 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);
 377  
 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);
 387  
 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);
 397      }
 398  }