Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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->assertMatchesRegularExpression('~mediaplugin_videojs~', $content);
 114          $this->assertMatchesRegularExpression('~</video>~', $content);
 115          $this->assertMatchesRegularExpression('~title="Test &amp; file"~', $content);
 116          $this->assertMatchesRegularExpression('~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->assertMatchesRegularExpression('~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->assertDoesNotMatchRegularExpression('~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->assertMatchesRegularExpression('~mediaplugin_videojs~', $content);
 142          $this->assertMatchesRegularExpression('~</video>~', $content);
 143          $this->assertMatchesRegularExpression('~title="Watch this one"~', $content);
 144          $this->assertDoesNotMatchRegularExpression('~<track\b~i', $content);
 145          $this->assertMatchesRegularExpression('~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->assertMatchesRegularExpression('~mediaplugin_videojs~', $content);
 163          $this->assertMatchesRegularExpression('~</video>~', $content);
 164          // Title is taken from the name of the first supported file.
 165          $this->assertMatchesRegularExpression('~title="2"~', $content);
 166          // Only supported files are in <source>'s.
 167          $this->assertDoesNotMatchRegularExpression('~<source src="http://example.org/1.rv"~', $content);
 168          $this->assertMatchesRegularExpression('~<source src="http://example.org/2.webm"~', $content);
 169          $this->assertMatchesRegularExpression('~<source src="http://example.org/3.ogv"~', $content);
 170          // Links to all files are included.
 171          $this->assertMatchesRegularExpression(
 172              '~<a class="mediafallbacklink" href="http://example.org/1.rv">1.rv</a>~', $content);
 173          $this->assertMatchesRegularExpression(
 174              '~<a class="mediafallbacklink" href="http://example.org/2.webm">2.webm</a>~', $content);
 175          $this->assertMatchesRegularExpression(
 176              '~<a class="mediafallbacklink" href="http://example.org/3.ogv">3.ogv</a>~', $content);
 177      }
 178  
 179      /**
 180       * Assert other players do not apply after videojs was applied.
 181       */
 182      public function test_prevent_other_players() {
 183          \core\plugininfo\media::set_enabled_plugins('videojs,html5video');
 184          $url = new moodle_url('http://example.org/some_filename.webm');
 185          $text = html_writer::link($url, 'Apply one player only');
 186          $content = format_text($text, FORMAT_HTML);
 187  
 188          $this->assertMatchesRegularExpression('~mediaplugin_videojs~', $content);
 189          $this->assertEquals(1, substr_count($content, '</video>'));
 190          $this->assertDoesNotMatchRegularExpression('~mediaplugin_html5video~', $content);
 191          $this->assertMatchesRegularExpression(
 192              '~<a class="mediafallbacklink" href="http://example.org/some_filename.webm">Apply one player only</a>~', $content);
 193      }
 194  
 195      /**
 196       * Test that mediaplugin filter adds player code on top of <video> tags.
 197       *
 198       * filter_mediaplugin is enabled by default.
 199       */
 200      public function test_embed_media() {
 201          global $CFG;
 202          $url = new moodle_url('http://example.org/some_filename.mp4');
 203          $trackurl = new moodle_url('http://example.org/some_filename.vtt');
 204          $text = '<video controls="true"><source src="'.$url.'"/><source src="somethinginvalid"/>' .
 205              '<track src="'.$trackurl.'">Unsupported text</video>';
 206          $content = format_text($text, FORMAT_HTML);
 207  
 208          $this->assertMatchesRegularExpression('~mediaplugin_videojs~', $content);
 209          $this->assertMatchesRegularExpression('~</video>~', $content);
 210          $this->assertMatchesRegularExpression('~title="some_filename.mp4"~', $content);
 211          $this->assertMatchesRegularExpression('~style="max-width:' . $CFG->media_default_width . 'px;~', $content);
 212          // Unsupported text and tracks are preserved.
 213          $this->assertMatchesRegularExpression('~Unsupported text~', $content);
 214          $this->assertMatchesRegularExpression('~<track\b~i', $content);
 215          // Invalid sources are removed.
 216          $this->assertDoesNotMatchRegularExpression('~somethinginvalid~i', $content);
 217  
 218          // Video with dimensions and source specified as src attribute without <source> tag.
 219          $text = '<video controls="true" width="123" height="35" src="'.$url.'">Unsupported text</video>';
 220          $content = format_text($text, FORMAT_HTML);
 221          $this->assertMatchesRegularExpression('~mediaplugin_videojs~', $content);
 222          $this->assertMatchesRegularExpression('~</video>~', $content);
 223          $this->assertMatchesRegularExpression('~<source\b~', $content);
 224          $this->assertMatchesRegularExpression('~style="max-width:123px;~', $content);
 225          $this->assertDoesNotMatchRegularExpression('~width="~', $content);
 226          $this->assertDoesNotMatchRegularExpression('~height="~', $content);
 227  
 228          // Audio tag.
 229          $url = new moodle_url('http://example.org/some_filename.mp3');
 230          $trackurl = new moodle_url('http://example.org/some_filename.vtt');
 231          $text = '<audio controls="true"><source src="'.$url.'"/><source src="somethinginvalid"/>' .
 232              '<track src="'.$trackurl.'">Unsupported text</audio>';
 233          $content = format_text($text, FORMAT_HTML);
 234  
 235          $this->assertMatchesRegularExpression('~mediaplugin_videojs~', $content);
 236          $this->assertDoesNotMatchRegularExpression('~</video>~', $content);
 237          $this->assertMatchesRegularExpression('~</audio>~', $content);
 238          $this->assertMatchesRegularExpression('~title="some_filename.mp3"~', $content);
 239          $this->assertMatchesRegularExpression('~style="max-width:' . $CFG->media_default_width . 'px;~', $content);
 240          // Unsupported text and tracks are preserved.
 241          $this->assertMatchesRegularExpression('~Unsupported text~', $content);
 242          $this->assertMatchesRegularExpression('~<track\b~i', $content);
 243          // Invalid sources are removed.
 244          $this->assertDoesNotMatchRegularExpression('~somethinginvalid~i', $content);
 245      }
 246  
 247      /**
 248       * Helper function for testing youtube videos embedding.
 249       *
 250       * @param string $t output of core_media_manager::embed_url.
 251       */
 252      protected function youtube_plugin_engaged($t) {
 253          $this->assertStringContainsString('mediaplugin_videojs', $t);
 254          $this->assertStringContainsString('data-setup-lazy="{&quot;techOrder&quot;: [&quot;youtube&quot;]', $t);
 255      }
 256  
 257      /**
 258       * Test that VideoJS can embed youtube videos.
 259       */
 260      public function test_youtube() {
 261          set_config('youtube', 1, 'media_videojs');
 262          set_config('useflash', 0, 'media_videojs');
 263  
 264          $manager = core_media_manager::instance();
 265  
 266          // Format: youtube.
 267          $url = new moodle_url('http://www.youtube.com/watch?v=vyrwMmsufJc');
 268          $t = $manager->embed_url($url);
 269          $this->youtube_plugin_engaged($t);
 270          $url = new moodle_url('http://www.youtube.com/v/vyrwMmsufJc');
 271          $t = $manager->embed_url($url);
 272          $this->youtube_plugin_engaged($t);
 273  
 274          // Format: youtube video within playlist - this will be played by video.js but without tracks selection.
 275          $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0');
 276          $t = $manager->embed_url($url);
 277          $this->youtube_plugin_engaged($t);
 278          $this->assertStringContainsString('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
 279  
 280          // Format: youtube playlist - not supported.
 281          $url = new moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662');
 282          $t = $manager->embed_url($url);
 283          $this->assertStringNotContainsString('mediaplugin_videojs', $t);
 284          $url = new moodle_url('http://www.youtube.com/playlist?list=PL6E18E2927047B662');
 285          $t = $manager->embed_url($url);
 286          $this->assertStringNotContainsString('mediaplugin_videojs', $t);
 287          $url = new moodle_url('http://www.youtube.com/p/PL6E18E2927047B662');
 288          $t = $manager->embed_url($url);
 289          $this->assertStringNotContainsString('mediaplugin_videojs', $t);
 290      }
 291  
 292      /**
 293       * Data provider for {@see test_youtube_start_time}
 294       *
 295       * @return array
 296       */
 297      public function youtube_start_time_provider(): array {
 298          return [
 299              ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s', 3611],
 300              ['https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s', 65],
 301              ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h10m30s', 4230],
 302              ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=3m', 180],
 303              ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=43s', 43],
 304              ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=1234', 1234],
 305              ['https://www.youtube.com/watch?v=JNJMF1l3udM&t=invalid', 0],
 306          ];
 307      }
 308  
 309      /**
 310       * Test Youtube video embedding with URL's containing start time interval
 311       *
 312       * @param string $url
 313       * @param int $expectedstart
 314       *
 315       * @dataProvider youtube_start_time_provider
 316       */
 317      public function test_youtube_start_time(string $url, int $expectedstart) {
 318          set_config('youtube', 1, 'media_videojs');
 319          set_config('useflash', 0, 'media_videojs');
 320  
 321          $embedcode = core_media_manager::instance()->embed_url(new moodle_url($url));
 322  
 323          $this->youtube_plugin_engaged($embedcode);
 324          $this->assertStringContainsString("&quot;youtube&quot;: {&quot;start&quot;: &quot;{$expectedstart}&quot;}", $embedcode);
 325      }
 326  
 327      /**
 328       * Helper function for testing flash videos embedding.
 329       *
 330       * @param string $t output of core_media_manager::embed_url.
 331       */
 332      protected function flash_plugin_engaged($t) {
 333          $this->assertStringContainsString('mediaplugin_videojs', $t);
 334          $this->assertStringContainsString('data-setup-lazy="{&quot;techOrder&quot;: [&quot;flash&quot;, &quot;html5&quot;]', $t);
 335      }
 336  
 337      /**
 338       * Test that VideoJS can embed flash videos.
 339       */
 340      public function test_flash() {
 341          $manager = core_media_manager::instance();
 342  
 343          // Flash enabled.
 344          set_config('useflash', 1, 'media_videojs');
 345          $url = new moodle_url('http://example.org/some_filename.flv');
 346          $t = $manager->embed_url($url);
 347          $this->flash_plugin_engaged($t);
 348          $this->assertMatchesRegularExpression('~</video>~', $t);
 349          $this->assertMatchesRegularExpression('~<source src="http://example.org/some_filename.flv"~', $t);
 350          $this->assertMatchesRegularExpression(
 351              '~<a class="mediafallbacklink" href="http://example.org/some_filename.flv">some_filename.flv</a>~', $t);
 352  
 353          // Flash disabled.
 354          set_config('useflash', 0, 'media_videojs');
 355          $url = new moodle_url('http://example.org/some_filename.flv');
 356          $t = $manager->embed_url($url);
 357          $this->assertStringNotContainsString('mediaplugin_videojs', $t);
 358          $this->assertMatchesRegularExpression(
 359              '~<a class="mediafallbacklink" href="http://example.org/some_filename.flv">some_filename.flv</a>~', $t);
 360      }
 361  
 362      /**
 363       * Test that VideoJS can embed RTMP streams.
 364       */
 365      public function test_rtmp() {
 366          $manager = core_media_manager::instance();
 367  
 368          // RTMP disabled, flash disabled.
 369          set_config('useflash', 0, 'media_videojs');
 370          set_config('rtmp', 0, 'media_videojs');
 371          $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4');
 372          $t = $manager->embed_url($url);
 373          $this->assertStringNotContainsString('mediaplugin_videojs', $t);
 374          $this->assertMatchesRegularExpression(
 375              '~<a class="mediafallbacklink" href="rtmp://example.com/fms&mp4:path/to/file.mp4">file.mp4</a>~', $t);
 376  
 377          // RTMP enabled, flash disabled.
 378          set_config('useflash', 0, 'media_videojs');
 379          set_config('rtmp', 1, 'media_videojs');
 380          $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4');
 381          $t = $manager->embed_url($url);
 382          $this->assertStringNotContainsString('mediaplugin_videojs', $t);
 383          $this->assertMatchesRegularExpression(
 384              '~<a class="mediafallbacklink" href="rtmp://example.com/fms&mp4:path/to/file.mp4">file.mp4</a>~', $t);
 385  
 386          // RTMP enabled, flash enabled, rtmp/mp4 type expected.
 387          set_config('useflash', 1, 'media_videojs');
 388          set_config('rtmp', 1, 'media_videojs');
 389          $url = new moodle_url('rtmp://example.com/fms&mp4:path/to/file.mp4');
 390          $t = $manager->embed_url($url);
 391          $this->flash_plugin_engaged($t);
 392          $this->assertMatchesRegularExpression('~</video>~', $t);
 393          $this->assertMatchesRegularExpression(
 394              '~<source src="rtmp://example.com/fms&mp4:path/to/file.mp4" type="rtmp/mp4"~', $t);
 395          $this->assertMatchesRegularExpression(
 396              '~<a class="mediafallbacklink" href="rtmp://example.com/fms&mp4:path/to/file.mp4">file.mp4</a>~', $t);
 397  
 398          // RTMP enabled, flash enabled, rtmp/flv type expected.
 399          set_config('useflash', 1, 'media_videojs');
 400          set_config('rtmp', 1, 'media_videojs');
 401          $url = new moodle_url('rtmp://example.com/fms&flv:path/to/file.flv');
 402          $t = $manager->embed_url($url);
 403          $this->flash_plugin_engaged($t);
 404          $this->assertMatchesRegularExpression('~</video>~', $t);
 405          $this->assertMatchesRegularExpression(
 406              '~<source src="rtmp://example.com/fms&flv:path/to/file.flv" type="rtmp/flv"~', $t);
 407          $this->assertMatchesRegularExpression(
 408              '~<a class="mediafallbacklink" href="rtmp://example.com/fms&flv:path/to/file.flv">file.flv</a>~', $t);
 409      }
 410  }