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