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]

   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 core;
  18  
  19  use media_test_native_plugin;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  require_once (__DIR__ . '/fixtures/testable_core_media_player_native.php');
  23  
  24  /**
  25   * Test for core_media_player_native.
  26   *
  27   * @package   core
  28   * @category  test
  29   * @covers    \core_media_player_native
  30   * @copyright 2019 Ruslan Kabalin
  31   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class core_media_player_native_test extends \advanced_testcase {
  34  
  35      /**
  36       * Pre-test setup.
  37       */
  38      public function setUp(): void {
  39          parent::setUp();
  40          $this->resetAfterTest();
  41      }
  42  
  43      /**
  44       * Test method get_supported_extensions
  45       */
  46      public function test_get_supported_extensions() {
  47          global $CFG;
  48          require_once($CFG->libdir . '/filelib.php');
  49          $nativeextensions = file_get_typegroup('extension', ['html_video', 'html_audio']);
  50  
  51          // Make sure that the list of extensions from the setting is exactly the same.
  52          $player = new media_test_native_plugin();
  53          $this->assertEmpty(array_diff($player->get_supported_extensions(), $nativeextensions));
  54          $this->assertEmpty(array_diff($nativeextensions, $player->get_supported_extensions()));
  55  
  56      }
  57  
  58      /**
  59       * Test method list_supported_urls
  60       */
  61      public function test_list_supported_urls() {
  62          global $CFG;
  63          require_once($CFG->libdir . '/filelib.php');
  64          $nativeextensions = file_get_typegroup('extension', ['html_video', 'html_audio']);
  65  
  66          // Create list of URLs for each extension.
  67          $urls = array_map(function($ext){
  68              return new \moodle_url('http://example.org/video.' . $ext);
  69          }, $nativeextensions);
  70  
  71          // Make sure that the list of supported URLs is not filtering permitted extensions.
  72          $player = new media_test_native_plugin();
  73          $this->assertCount(count($urls), $player->list_supported_urls($urls));
  74      }
  75  
  76      /**
  77       * Test method get_attribute
  78       */
  79      public function test_get_attribute() {
  80          $urls = [
  81              new \moodle_url('http://example.org/some_filename.mp4'),
  82              new \moodle_url('http://example.org/some_filename_hires.mp4'),
  83          ];
  84  
  85          $player = new media_test_native_plugin();
  86          // We are using fixture embed method directly as content generator.
  87          $title = 'Some Filename Video';
  88          $content = $player->embed($urls, $title, 0, 0, []);
  89  
  90          $this->assertMatchesRegularExpression('~title="' . $title . '"~', $content);
  91          $this->assertEquals($title, media_test_native_plugin::get_attribute($content, 'title'));
  92      }
  93  
  94      /**
  95       * Test methods add_attributes and remove_attributes
  96       */
  97      public function test_add_remove_attributes() {
  98          $urls = [
  99              new \moodle_url('http://example.org/some_filename.mp4'),
 100              new \moodle_url('http://example.org/some_filename_hires.mp4'),
 101          ];
 102  
 103          $player = new media_test_native_plugin();
 104          // We are using fixture embed method directly as content generator.
 105          $title = 'Some Filename Video';
 106          $content = $player->embed($urls, $title, 0, 0, []);
 107  
 108          // Add attributes.
 109          $content = media_test_native_plugin::add_attributes($content, ['preload' => 'none', 'controls' => 'true']);
 110          $this->assertMatchesRegularExpression('~title="' . $title . '"~', $content);
 111          $this->assertMatchesRegularExpression('~preload="none"~', $content);
 112          $this->assertMatchesRegularExpression('~controls="true"~', $content);
 113  
 114          // Change existing attribute.
 115          $content = media_test_native_plugin::add_attributes($content, ['controls' => 'false']);
 116          $this->assertMatchesRegularExpression('~title="' . $title . '"~', $content);
 117          $this->assertMatchesRegularExpression('~preload="none"~', $content);
 118          $this->assertMatchesRegularExpression('~controls="false"~', $content);
 119  
 120          // Remove attributes.
 121          $content = media_test_native_plugin::remove_attributes($content, ['title']);
 122          $this->assertDoesNotMatchRegularExpression('~title="' . $title . '"~', $content);
 123          $this->assertMatchesRegularExpression('~preload="none"~', $content);
 124          $this->assertMatchesRegularExpression('~controls="false"~', $content);
 125  
 126          // Remove another one.
 127          $content = media_test_native_plugin::remove_attributes($content, ['preload']);
 128          $this->assertDoesNotMatchRegularExpression('~title="' . $title . '"~', $content);
 129          $this->assertDoesNotMatchRegularExpression('~preload="none"~', $content);
 130          $this->assertMatchesRegularExpression('~controls="false"~', $content);
 131      }
 132  
 133      /**
 134       * Test method replace_sources
 135       */
 136      public function test_replace_sources() {
 137          $urls = [
 138              new \moodle_url('http://example.org/some_filename.mp4'),
 139              new \moodle_url('http://example.org/some_filename_hires.mp4'),
 140          ];
 141  
 142          $player = new media_test_native_plugin();
 143          // We are using fixture embed method directly as content generator.
 144          $title = 'Some Filename Video';
 145          $content = $player->embed($urls, $title, 0, 0, []);
 146  
 147          // Test sources present.
 148          $this->assertStringContainsString('<source src="http://example.org/some_filename.mp4" />', $content);
 149          $this->assertStringContainsString('<source src="http://example.org/some_filename_hires.mp4" />', $content);
 150  
 151          // Change sources.
 152          $newsource = '<source src="http://example.org/new_filename.mp4" />';
 153          $content = media_test_native_plugin::replace_sources($content, $newsource);
 154          $this->assertStringContainsString($newsource, $content);
 155          $this->assertStringNotContainsString('<source src="http://example.org/some_filename.mp4" />', $content);
 156          $this->assertStringNotContainsString('<source src="http://example.org/some_filename_hires.mp4" />', $content);
 157      }
 158  }