Merge branch 'MDL-56549-master' of https://github.com/kabalin/moodle

This commit is contained in:
Jun Pataleta
2019-10-18 06:27:21 +08:00
7 changed files with 392 additions and 45 deletions
+6 -2
View File
@@ -1123,8 +1123,7 @@ class core_useragent {
$extension = strtolower($extension);
$supportedvideo = array('m4v', 'webm', 'ogv', 'mp4', 'mov');
$supportedaudio = array('ogg', 'oga', 'aac', 'm4a', 'mp3', 'wav');
// TODO MDL-56549 Flac will be supported in Firefox 51 in January 2017.
$supportedaudio = array('ogg', 'oga', 'aac', 'm4a', 'mp3', 'wav', 'flac');
// Basic extension support.
if (!in_array($extension, $supportedvideo) && !in_array($extension, $supportedaudio)) {
@@ -1158,6 +1157,11 @@ class core_useragent {
if ($isogg && (self::is_ie() || self::is_edge() || self::is_safari() || self::is_safari_ios())) {
return false;
}
// FLAC is not supported in IE and Edge (below 16.0).
if ($extension === 'flac' &&
(self::is_ie() || (self::is_edge() && !self::check_edge_version('16.0')))) {
return false;
}
// Wave is not supported in IE.
if ($extension === 'wav' && self::is_ie()) {
return false;
+163
View File
@@ -0,0 +1,163 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Test for core_media_player_native.
*
* @package core
* @category test
* @copyright 2019 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/testable_core_media_player_native.php');
/**
* Test for core_media_player_native.
*
* @package core
* @category test
* @covers core_media_player_native
* @copyright 2019 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_media_player_native_testcase extends advanced_testcase {
/**
* Pre-test setup.
*/
public function setUp() {
parent::setUp();
$this->resetAfterTest();
}
/**
* Test method get_supported_extensions
*/
public function test_get_supported_extensions() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$nativeextensions = file_get_typegroup('extension', ['html_video', 'html_audio']);
// Make sure that the list of extensions from the setting is exactly the same.
$player = new media_test_native_plugin();
$this->assertEmpty(array_diff($player->get_supported_extensions(), $nativeextensions));
$this->assertEmpty(array_diff($nativeextensions, $player->get_supported_extensions()));
}
/**
* Test method list_supported_urls
*/
public function test_list_supported_urls() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$nativeextensions = file_get_typegroup('extension', ['html_video', 'html_audio']);
// Create list of URLs for each extension.
$urls = array_map(function($ext){
return new moodle_url('http://example.org/video.' . $ext);
}, $nativeextensions);
// Make sure that the list of supported URLs is not filtering permitted extensions.
$player = new media_test_native_plugin();
$this->assertCount(count($urls), $player->list_supported_urls($urls));
}
/**
* Test method get_attribute
*/
public function test_get_attribute() {
$urls = [
new moodle_url('http://example.org/some_filename.mp4'),
new moodle_url('http://example.org/some_filename_hires.mp4'),
];
$player = new media_test_native_plugin();
// We are using fixture embed method directly as content generator.
$title = 'Some Filename Video';
$content = $player->embed($urls, $title, 0, 0, []);
$this->assertRegExp('~title="' . $title . '"~', $content);
$this->assertEquals($title, media_test_native_plugin::get_attribute($content, 'title'));
}
/**
* Test methods add_attributes and remove_attributes
*/
public function test_add_remove_attributes() {
$urls = [
new moodle_url('http://example.org/some_filename.mp4'),
new moodle_url('http://example.org/some_filename_hires.mp4'),
];
$player = new media_test_native_plugin();
// We are using fixture embed method directly as content generator.
$title = 'Some Filename Video';
$content = $player->embed($urls, $title, 0, 0, []);
// Add attributes.
$content = media_test_native_plugin::add_attributes($content, ['preload' => 'none', 'controls' => 'true']);
$this->assertRegExp('~title="' . $title . '"~', $content);
$this->assertRegExp('~preload="none"~', $content);
$this->assertRegExp('~controls="true"~', $content);
// Change existing attribute.
$content = media_test_native_plugin::add_attributes($content, ['controls' => 'false']);
$this->assertRegExp('~title="' . $title . '"~', $content);
$this->assertRegExp('~preload="none"~', $content);
$this->assertRegExp('~controls="false"~', $content);
// Remove attributes.
$content = media_test_native_plugin::remove_attributes($content, ['title']);
$this->assertNotRegExp('~title="' . $title . '"~', $content);
$this->assertRegExp('~preload="none"~', $content);
$this->assertRegExp('~controls="false"~', $content);
// Remove another one.
$content = media_test_native_plugin::remove_attributes($content, ['preload']);
$this->assertNotRegExp('~title="' . $title . '"~', $content);
$this->assertNotRegExp('~preload="none"~', $content);
$this->assertRegExp('~controls="false"~', $content);
}
/**
* Test method replace_sources
*/
public function test_replace_sources() {
$urls = [
new moodle_url('http://example.org/some_filename.mp4'),
new moodle_url('http://example.org/some_filename_hires.mp4'),
];
$player = new media_test_native_plugin();
// We are using fixture embed method directly as content generator.
$title = 'Some Filename Video';
$content = $player->embed($urls, $title, 0, 0, []);
// Test sources present.
$this->assertContains('<source src="http://example.org/some_filename.mp4" />', $content);
$this->assertContains('<source src="http://example.org/some_filename_hires.mp4" />', $content);
// Change sources.
$newsource = '<source src="http://example.org/new_filename.mp4" />';
$content = media_test_native_plugin::replace_sources($content, $newsource);
$this->assertContains($newsource, $content);
$this->assertNotContains('<source src="http://example.org/some_filename.mp4" />', $content);
$this->assertNotContains('<source src="http://example.org/some_filename_hires.mp4" />', $content);
}
}
+90
View File
@@ -0,0 +1,90 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Fixture for testing the functionality of core_media_player.
*
* @package core
* @subpackage fixtures
* @category test
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Media player stub for testing purposes.
*
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class media_test_plugin extends core_media_player {
/** @var array Array of supported extensions */
public $ext;
/** @var int Player rank */
public $rank;
/** @var int Arbitrary number */
public $num;
/**
* Constructor is used for tuning the fixture.
*
* @param int $num Number (used in output)
* @param int $rank Player rank
* @param array $ext Array of supported extensions
*/
public function __construct($num = 1, $rank = 13, $ext = array('mp3', 'flv', 'f4v', 'mp4')) {
$this->ext = $ext;
$this->rank = $rank;
$this->num = $num;
}
/**
* Generates code required to embed the player.
*
* @param array $urls URLs of media files
* @param string $name Display name; '' to use default
* @param int $width Optional width; 0 to use default
* @param int $height Optional height; 0 to use default
* @param array $options Options array
* @return string HTML code for embed
*/
public function embed($urls, $name, $width, $height, $options) {
self::pick_video_size($width, $height);
$contents = "\ntestsource=". join("\ntestsource=", $urls) .
"\ntestname=$name\ntestwidth=$width\ntestheight=$height\n<!--FALLBACK-->\n";
return html_writer::span($contents, 'mediaplugin mediaplugin_test');
}
/**
* Gets the list of file extensions supported by this media player.
*
* @return array Array of strings (extension not including dot e.g. '.mp3')
*/
public function get_supported_extensions() {
return $this->ext;
}
/**
* Gets the ranking of this player.
*
* @return int Rank
*/
public function get_rank() {
return 10;
}
}
@@ -0,0 +1,89 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Fixture for testing the functionality of core_media_player_native.
*
* @package core
* @subpackage fixtures
* @category test
* @copyright 2019 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Native media player stub for testing purposes.
*
* @copyright 2019 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class media_test_native_plugin extends core_media_player_native {
/** @var int Player rank */
public $rank;
/** @var int Arbitrary number */
public $num;
/**
* Constructor is used for tuning the fixture.
*
* @param int $num Number (used in output)
* @param int $rank Player rank
*/
public function __construct($num = 1, $rank = 13) {
$this->rank = $rank;
$this->num = $num;
}
/**
* Generates code required to embed the player.
*
* @param array $urls URLs of media files
* @param string $name Display name; '' to use default
* @param int $width Optional width; 0 to use default
* @param int $height Optional height; 0 to use default
* @param array $options Options array
* @return string HTML code for embed
*/
public function embed($urls, $name, $width, $height, $options) {
$sources = array();
foreach ($urls as $url) {
$params = ['src' => $url];
$sources[] = html_writer::empty_tag('source', $params);
}
$sources = implode("\n", $sources);
$title = $this->get_name($name, $urls);
// Escape title but prevent double escaping.
$title = s(preg_replace(['/&amp;/', '/&gt;/', '/&lt;/'], ['&', '>', '<'], $title));
return <<<OET
<video class="mediaplugin mediaplugin_test" title="$title">
$sources
</video>
OET;
}
/**
* Gets the ranking of this player.
*
* @return int Rank
*/
public function get_rank() {
return 10;
}
}
+4 -41
View File
@@ -17,13 +17,14 @@
/**
* Test classes for handling embedded media (audio/video).
*
* @package core_media
* @category phpunit
* @package core
* @category test
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/testable_core_media_player.php');
/**
* Test script for media embedding.
@@ -495,42 +496,4 @@ class core_medialib_testcase extends advanced_testcase {
}
return $out;
}
}
/**
* Media player stub for testing purposes.
*/
class media_test_plugin extends core_media_player {
/** @var array Array of supported extensions */
public $ext;
/** @var int Player rank */
public $rank;
/** @var int Arbitrary number */
public $num;
/**
* @param int $num Number (used in output)
* @param int $rank Player rank
* @param array $ext Array of supported extensions
*/
public function __construct($num = 1, $rank = 13, $ext = array('mp3', 'flv', 'f4v', 'mp4')) {
$this->ext = $ext;
$this->rank = $rank;
$this->num = $num;
}
public function embed($urls, $name, $width, $height, $options) {
self::pick_video_size($width, $height);
$contents = "\ntestsource=". join("\ntestsource=", $urls) .
"\ntestname=$name\ntestwidth=$width\ntestheight=$height\n<!--FALLBACK-->\n";
return html_writer::span($contents, 'mediaplugin mediaplugin_test');
}
public function get_supported_extensions() {
return $this->ext;
}
public function get_rank() {
return 10;
}
}
}
+20 -1
View File
@@ -60,7 +60,7 @@ class media_html5audio_testcase extends advanced_testcase {
/**
* Test method get_supported_extensions()
*/
public function test_supported_extensions() {
public function test_get_supported_extensions() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
@@ -72,6 +72,25 @@ class media_html5audio_testcase extends advanced_testcase {
$this->assertEmpty(array_diff($nativeextensions, $player->get_supported_extensions()));
}
/**
* Test method list_supported_urls()
*/
public function test_list_supported_urls() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$nativeextensions = file_get_typegroup('extension', 'html_audio');
// Create list of URLs for each extension.
$urls = array_map(function($ext){
return new moodle_url('http://example.org/audio.' . $ext);
}, $nativeextensions);
// Make sure that the list of supported URLs is not filtering permitted extensions.
$player = new media_html5audio_plugin();
$this->assertCount(count($urls), $player->list_supported_urls($urls));
}
/**
* Test embedding without media filter (for example for displaying file resorce).
*/
+20 -1
View File
@@ -60,7 +60,7 @@ class media_html5video_testcase extends advanced_testcase {
/**
* Test method get_supported_extensions()
*/
public function test_supported_extensions() {
public function test_get_supported_extensions() {
$nativeextensions = file_get_typegroup('extension', 'html_video');
// Make sure that the list of extensions from the setting is exactly the same as html_video group.
@@ -69,6 +69,25 @@ class media_html5video_testcase extends advanced_testcase {
$this->assertEmpty(array_diff($nativeextensions, $player->get_supported_extensions()));
}
/**
* Test method list_supported_urls()
*/
public function test_list_supported_urls() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$nativeextensions = file_get_typegroup('extension', 'html_video');
// Create list of URLs for each extension.
$urls = array_map(function($ext){
return new moodle_url('http://example.org/video.' . $ext);
}, $nativeextensions);
// Make sure that the list of supported URLs is not filtering permitted extensions.
$player = new media_html5video_plugin();
$this->assertCount(count($urls), $player->list_supported_urls($urls));
}
/**
* Test embedding without media filter (for example for displaying file resorce).
*/