Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 39 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_html5video;
  18  
  19  use core_media_manager;
  20  use media_html5video_plugin;
  21  
  22  /**
  23   * Test script for media embedding.
  24   *
  25   * @package media_html5video
  26   * @copyright 2016 Marina Glancy
  27   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class player_test extends \advanced_testcase {
  30  
  31      /**
  32       * Pre-test setup. Preserves $CFG.
  33       */
  34      public function setUp(): void {
  35          parent::setUp();
  36  
  37          // Reset $CFG and $SERVER.
  38          $this->resetAfterTest();
  39  
  40          // Consistent initial setup: all players disabled.
  41          \core\plugininfo\media::set_enabled_plugins('html5video');
  42  
  43          // Pretend to be using Firefox browser (must support ogg for tests to work).
  44          \core_useragent::instance(true, 'Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0 ');
  45      }
  46  
  47      /**
  48       * Test that plugin is returned as enabled media plugin.
  49       */
  50      public function test_is_installed() {
  51          $sortorder = \core\plugininfo\media::get_enabled_plugins();
  52          $this->assertEquals(['html5video' => 'html5video'], $sortorder);
  53      }
  54  
  55      /**
  56       * Test method get_supported_extensions()
  57       */
  58      public function test_get_supported_extensions() {
  59          $nativeextensions = file_get_typegroup('extension', 'html_video');
  60  
  61          // Make sure that the list of extensions from the setting is exactly the same as html_video group.
  62          $player = new media_html5video_plugin();
  63          $this->assertEmpty(array_diff($player->get_supported_extensions(), $nativeextensions));
  64          $this->assertEmpty(array_diff($nativeextensions, $player->get_supported_extensions()));
  65      }
  66  
  67      /**
  68       * Test method list_supported_urls()
  69       */
  70      public function test_list_supported_urls() {
  71          global $CFG;
  72          require_once($CFG->libdir . '/filelib.php');
  73  
  74          $nativeextensions = file_get_typegroup('extension', 'html_video');
  75  
  76          // Create list of URLs for each extension.
  77          $urls = array_map(function($ext){
  78              return new \moodle_url('http://example.org/video.' . $ext);
  79          }, $nativeextensions);
  80  
  81          // Make sure that the list of supported URLs is not filtering permitted extensions.
  82          $player = new media_html5video_plugin();
  83          $this->assertCount(count($urls), $player->list_supported_urls($urls));
  84      }
  85  
  86      /**
  87       * Test embedding without media filter (for example for displaying file resorce).
  88       */
  89      public function test_embed_url() {
  90          global $CFG;
  91  
  92          $url = new \moodle_url('http://example.org/1.webm');
  93  
  94          $manager = core_media_manager::instance();
  95          $embedoptions = array(
  96              core_media_manager::OPTION_TRUSTED => true,
  97              core_media_manager::OPTION_BLOCK => true,
  98          );
  99  
 100          $this->assertTrue($manager->can_embed_url($url, $embedoptions));
 101          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
 102  
 103          $this->assertMatchesRegularExpression('~mediaplugin_html5video~', $content);
 104          $this->assertMatchesRegularExpression('~</video>~', $content);
 105          $this->assertMatchesRegularExpression('~title="Test &amp; file"~', $content);
 106          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '"~', $content);
 107          $this->assertDoesNotMatchRegularExpression('~height=~', $content); // Allow to set automatic height.
 108  
 109          // Repeat sending the specific size to the manager.
 110          $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions);
 111          $this->assertMatchesRegularExpression('~width="123" height="50"~', $content);
 112      }
 113  
 114      /**
 115       * Test that mediaplugin filter replaces a link to the supported file with media tag.
 116       *
 117       * filter_mediaplugin is enabled by default.
 118       */
 119      public function test_embed_link() {
 120          global $CFG;
 121          $url = new \moodle_url('http://example.org/some_filename.mp4');
 122          $text = \html_writer::link($url, 'Watch this one');
 123          $content = format_text($text, FORMAT_HTML);
 124  
 125          $this->assertMatchesRegularExpression('~mediaplugin_html5video~', $content);
 126          $this->assertMatchesRegularExpression('~</video>~', $content);
 127          $this->assertMatchesRegularExpression('~title="Watch this one"~', $content);
 128          $this->assertDoesNotMatchRegularExpression('~<track\b~i', $content);
 129          $this->assertMatchesRegularExpression('~width="' . $CFG->media_default_width . '"~', $content);
 130      }
 131  
 132      /**
 133       * Test that mediaplugin filter does not work on <video> tags.
 134       */
 135      public function test_embed_media() {
 136          $url = new \moodle_url('http://example.org/some_filename.mp4');
 137          $trackurl = new \moodle_url('http://example.org/some_filename.vtt');
 138          $text = '<video controls="true"><source src="'.$url.'"/><source src="somethinginvalid"/>' .
 139              '<track src="'.$trackurl.'">Unsupported text</video>';
 140          $content = format_text($text, FORMAT_HTML);
 141  
 142          $this->assertDoesNotMatchRegularExpression('~mediaplugin_html5video~', $content);
 143          $this->assertEquals(clean_text($text, FORMAT_HTML), $content);
 144      }
 145  }