Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 403]

   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_vimeo;
  18  
  19  use core_media_manager;
  20  
  21  /**
  22   * Test script for media embedding.
  23   *
  24   * @package media_vimeo
  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('vimeo');
  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       */
  49      public function test_is_installed() {
  50          $sortorder = \core\plugininfo\media::get_enabled_plugins();
  51          $this->assertEquals(['vimeo' => 'vimeo'], $sortorder);
  52      }
  53  
  54      /**
  55       * Test embedding without media filter (for example for displaying URL resorce).
  56       */
  57      public function test_embed_url() {
  58          global $CFG;
  59  
  60          $url = new \moodle_url('http://vimeo.com/1176321');
  61  
  62          $manager = core_media_manager::instance();
  63          $embedoptions = array(
  64              core_media_manager::OPTION_TRUSTED => true,
  65              core_media_manager::OPTION_BLOCK => true,
  66          );
  67  
  68          $this->assertTrue($manager->can_embed_url($url, $embedoptions));
  69          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
  70  
  71          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
  72          $this->assertMatchesRegularExpression('~</iframe>~', $content);
  73          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
  74              $CFG->media_default_height . '"~', $content);
  75  
  76          // Repeat sending the specific size to the manager.
  77          $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions);
  78          $this->assertMatchesRegularExpression('~width="123" height="50"~', $content);
  79      }
  80  
  81      /**
  82       * Test that mediaplugin filter replaces a link to the supported file with media tag.
  83       *
  84       * filter_mediaplugin is enabled by default.
  85       */
  86      public function test_embed_link() {
  87          global $CFG;
  88          $url = new \moodle_url('http://vimeo.com/1176321');
  89          $text = \html_writer::link($url, 'Watch this one');
  90          $content = format_text($text, FORMAT_HTML);
  91  
  92          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
  93          $this->assertMatchesRegularExpression('~</iframe>~', $content);
  94          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
  95              $CFG->media_default_height . '"~', $content);
  96      }
  97  
  98      /**
  99       * Test that mediaplugin filter adds player code on top of <video> tags.
 100       *
 101       * filter_mediaplugin is enabled by default.
 102       */
 103      public function test_embed_media() {
 104          global $CFG;
 105          $url = new \moodle_url('http://vimeo.com/1176321');
 106          $trackurl = new \moodle_url('http://example.org/some_filename.vtt');
 107          $text = '<video controls="true"><source src="'.$url.'"/>' .
 108              '<track src="'.$trackurl.'">Unsupported text</video>';
 109          $content = format_text($text, FORMAT_HTML);
 110  
 111          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
 112          $this->assertMatchesRegularExpression('~</iframe>~', $content);
 113          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
 114              $CFG->media_default_height . '"~', $content);
 115          // Video tag, unsupported text and tracks are removed.
 116          $this->assertDoesNotMatchRegularExpression('~</video>~', $content);
 117          $this->assertDoesNotMatchRegularExpression('~<source\b~', $content);
 118          $this->assertDoesNotMatchRegularExpression('~Unsupported text~', $content);
 119          $this->assertDoesNotMatchRegularExpression('~<track\b~i', $content);
 120  
 121          // Video with dimensions and source specified as src attribute without <source> tag.
 122          $text = '<video controls="true" width="123" height="35" src="'.$url.'">Unsupported text</video>';
 123          $content = format_text($text, FORMAT_HTML);
 124          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
 125          $this->assertMatchesRegularExpression('~</iframe>~', $content);
 126          $this->assertMatchesRegularExpression('~width="123" height="35"~', $content);
 127      }
 128  
 129      /**
 130       * Test embedding without media filter (for example for displaying URL resorce)
 131       * and test that player plugin is parsing the URL with the code.
 132       */
 133      public function test_embed_url_with_code() {
 134          global $CFG;
 135  
 136          $url = new \moodle_url('https://vimeo.com/1176321/abcdef12345');
 137  
 138          $manager = core_media_manager::instance();
 139          $embedoptions = array(
 140              core_media_manager::OPTION_TRUSTED => true,
 141              core_media_manager::OPTION_BLOCK => true,
 142          );
 143  
 144          $this->assertTrue($manager->can_embed_url($url, $embedoptions));
 145          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
 146  
 147          // Video source URL is contains the new vimeo embedded URL format.
 148          $this->assertStringContainsString('player.vimeo.com/video/1176321?h=abcdef12345', $content);
 149  
 150          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
 151          $this->assertMatchesRegularExpression('~</iframe>~', $content);
 152          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
 153                              $CFG->media_default_height . '"~', $content);
 154  
 155          // Repeat sending the specific size to the manager.
 156          $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions);
 157          $this->assertMatchesRegularExpression('~width="123" height="50"~', $content);
 158      }
 159  
 160      /**
 161       * Test that mediaplugin filter replaces a link to the supported file with media tag
 162       * and test that player plugin is parsing the URL with the code.
 163       *
 164       * filter_mediaplugin is enabled by default.
 165       */
 166      public function test_embed_link_with_code() {
 167          global $CFG;
 168          $url = new \moodle_url('https://vimeo.com/1176321/abcdef12345');
 169          $text = \html_writer::link($url, 'Watch this one');
 170          $content = format_text($text, FORMAT_HTML);
 171  
 172          // Video source URL is contains the new vimeo embedded URL format.
 173          $this->assertStringContainsString('player.vimeo.com/video/1176321?h=abcdef12345', $content);
 174  
 175          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
 176          $this->assertMatchesRegularExpression('~</iframe>~', $content);
 177          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
 178                              $CFG->media_default_height . '"~', $content);
 179      }
 180  
 181      /**
 182       * Test that mediaplugin filter adds player code on top of <video> tags
 183       * and test that player plugin is parse the URL with the code.
 184       *
 185       * filter_mediaplugin is enabled by default.
 186       */
 187      public function test_embed_media_with_code() {
 188          global $CFG;
 189          $url = new \moodle_url('https://vimeo.com/1176321/abcdef12345');
 190          $trackurl = new \moodle_url('http://example.org/some_filename.vtt');
 191          $text = '<video controls="true"><source src="'.$url.'"/>' .
 192              '<track src="'.$trackurl.'">Unsupported text</video>';
 193          $content = format_text($text, FORMAT_HTML);
 194  
 195          // Video source URL is contains the new vimeo embedded URL format.
 196          $this->assertStringContainsString('player.vimeo.com/video/1176321?h=abcdef12345', $content);
 197  
 198          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
 199          $this->assertMatchesRegularExpression('~</iframe>~', $content);
 200          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '" height="' .
 201                              $CFG->media_default_height . '"~', $content);
 202          // Video tag, unsupported text and tracks are removed.
 203          $this->assertDoesNotMatchRegularExpression('~</video>~', $content);
 204          $this->assertDoesNotMatchRegularExpression('~<source\b~', $content);
 205          $this->assertDoesNotMatchRegularExpression('~Unsupported text~', $content);
 206          $this->assertDoesNotMatchRegularExpression('~<track\b~i', $content);
 207  
 208          // Video with dimensions and source specified as src attribute without <source> tag.
 209          $text = '<video controls="true" width="123" height="35" src="'.$url.'">Unsupported text</video>';
 210          $content = format_text($text, FORMAT_HTML);
 211          $this->assertMatchesRegularExpression('~mediaplugin_vimeo~', $content);
 212          $this->assertMatchesRegularExpression('~</iframe>~', $content);
 213          $this->assertMatchesRegularExpression('~width="123" height="35"~', $content);
 214      }
 215  
 216      /**
 217       * Test that mediaplugin filter skip the process when the URL is invalid.
 218       */
 219      public function test_skip_invalid_url_format_with_code() {
 220          $url = new \moodle_url('https://vimeo.com/_________/abcdef12345s');
 221          $text = \html_writer::link($url, 'Invalid Vimeo URL');
 222          $content = format_text($text, FORMAT_HTML);
 223  
 224          $this->assertStringNotContainsString('player.vimeo.com/video/_________?h=abcdef12345s', $content);
 225          $this->assertDoesNotMatchRegularExpression('~mediaplugin_vimeo~', $content);
 226          $this->assertDoesNotMatchRegularExpression('~</iframe>~', $content);
 227      }
 228  }