MDL-79059 core: Use full name as alt text for user picture links
[moodle.git] / lib / tests / core_media_player_native_test.php
blob22a8bcf3d75cbd63bff70ec9e2b6ba76e0a7f530
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.
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/>.
17 namespace core;
19 use media_test_native_plugin;
21 defined('MOODLE_INTERNAL') || die();
22 require_once(__DIR__ . '/fixtures/testable_core_media_player_native.php');
24 /**
25 * Test for core_media_player_native.
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
33 class core_media_player_native_test extends \advanced_testcase {
35 /**
36 * Pre-test setup.
38 public function setUp(): void {
39 parent::setUp();
40 $this->resetAfterTest();
43 /**
44 * Test method get_supported_extensions
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']);
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()));
58 /**
59 * Test method list_supported_urls
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']);
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);
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));
76 /**
77 * Test method get_attribute
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'),
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, []);
90 $this->assertMatchesRegularExpression('~title="' . $title . '"~', $content);
91 $this->assertEquals($title, media_test_native_plugin::get_attribute($content, 'title'));
94 /**
95 * Test methods add_attributes and remove_attributes
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'),
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, []);
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);
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);
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);
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);
134 * Test method replace_sources
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'),
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, []);
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);
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);