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