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_html5audio
  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_html5audio
  31   * @copyright 2016 Marina Glancy
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class media_html5audio_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('html5audio');
  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(['html5audio' => 'html5audio'], $sortorder);
  58      }
  59  
  60      /**
  61       * Test method get_supported_extensions()
  62       */
  63      public function test_get_supported_extensions() {
  64          global $CFG;
  65          require_once($CFG->libdir . '/filelib.php');
  66  
  67          $nativeextensions = file_get_typegroup('extension', 'html_audio');
  68  
  69          // Make sure that the list of extensions from the setting is exactly the same as html_audio group.
  70          $player = new media_html5audio_plugin();
  71          $this->assertEmpty(array_diff($player->get_supported_extensions(), $nativeextensions));
  72          $this->assertEmpty(array_diff($nativeextensions, $player->get_supported_extensions()));
  73      }
  74  
  75      /**
  76       * Test method list_supported_urls()
  77       */
  78      public function test_list_supported_urls() {
  79          global $CFG;
  80          require_once($CFG->libdir . '/filelib.php');
  81  
  82          $nativeextensions = file_get_typegroup('extension', 'html_audio');
  83  
  84          // Create list of URLs for each extension.
  85          $urls = array_map(function($ext){
  86              return new moodle_url('http://example.org/audio.' . $ext);
  87          }, $nativeextensions);
  88  
  89          // Make sure that the list of supported URLs is not filtering permitted extensions.
  90          $player = new media_html5audio_plugin();
  91          $this->assertCount(count($urls), $player->list_supported_urls($urls));
  92      }
  93  
  94      /**
  95       * Test embedding without media filter (for example for displaying file resorce).
  96       */
  97      public function test_embed_url() {
  98          global $CFG;
  99  
 100          $url = new moodle_url('http://example.org/1.wav');
 101  
 102          $manager = core_media_manager::instance();
 103          $embedoptions = array(
 104              core_media_manager::OPTION_TRUSTED => true,
 105              core_media_manager::OPTION_BLOCK => true,
 106          );
 107  
 108          $this->assertTrue($manager->can_embed_url($url, $embedoptions));
 109          $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions);
 110  
 111          $this->assertRegExp('~mediaplugin_html5audio~', $content);
 112          $this->assertRegExp('~</audio>~', $content);
 113          $this->assertRegExp('~title="Test &amp; file"~', $content);
 114          // Do not set default width/height (it's an audio after all).
 115          $this->assertNotRegExp('~width=~', $content);
 116          $this->assertNotRegExp('~height=~', $content);
 117  
 118          // This plugin ignores size settings.
 119          $this->assertNotRegExp('~width=~', $content);
 120          $this->assertNotRegExp('~height=~', $content);
 121      }
 122  
 123      /**
 124       * Test that mediaplugin filter replaces a link to the supported file with media tag.
 125       *
 126       * filter_mediaplugin is enabled by default.
 127       */
 128      public function test_embed_link() {
 129          $url = new moodle_url('http://example.org/some_filename.wav');
 130          $text = html_writer::link($url, 'Watch this one');
 131          $content = format_text($text, FORMAT_HTML);
 132  
 133          $this->assertRegExp('~mediaplugin_html5audio~', $content);
 134          $this->assertRegExp('~</audio>~', $content);
 135          $this->assertRegExp('~title="Watch this one"~', $content);
 136          $this->assertNotRegExp('~<track\b~i', $content);
 137      }
 138  
 139      /**
 140       * Test that mediaplugin filter does not work on <audio> tags.
 141       */
 142      public function test_embed_media() {
 143          $url = new moodle_url('http://example.org/some_filename.wav');
 144          $trackurl = new moodle_url('http://example.org/some_filename.vtt');
 145          $text = '<audio controls="true"><source src="'.$url.'"/><source src="somethinginvalid"/>' .
 146              '<track src="'.$trackurl.'">Unsupported text</audio>';
 147          $content = format_text($text, FORMAT_HTML);
 148  
 149          $this->assertNotRegExp('~mediaplugin_html5audio~', $content);
 150          $this->assertEquals(clean_text($text, FORMAT_HTML), $content);
 151      }
 152  }