Permalink
Browse files

MDL-33116 Media filter broken with slasharguments off

  • Loading branch information...
sammarshallou committed May 21, 2012
1 parent 4f7f2a8 commit a3c17aed9ffca07c8ae653b80317c90c9f914c2a
Showing with 73 additions and 16 deletions.
  1. +9 −2 lib/medialib.php
  2. +47 −13 lib/tests/medialib_test.php
  3. +17 −1 lib/weblib.php
View
@@ -172,13 +172,20 @@ public static function get_extension(moodle_url $url) {
* @return string Filename only (not escaped)
*/
public static function get_filename(moodle_url $url) {
- $path = $url->get_path();
+ global $CFG;
+
+ // Use the 'file' parameter if provided (for links created when
+ // slasharguments was off). If not present, just use URL path.
+ $path = $url->get_param('file');
+ if (!$path) {
+ $path = $url->get_path();
+ }
+
// Remove everything before last / if present. Does not use textlib as / is UTF8-safe.
$slash = strrpos($path, '/');
if ($slash !== false) {
$path = substr($path, $slash + 1);
}
-
return $path;
}
@@ -42,7 +42,7 @@ public function setUp() {
global $CFG;
parent::setUp();
- // Reset CFG.
+ // Reset $CFG and $SERVER.
$this->resetAfterTest(true);
// Consistent initial setup: all players disabled.
@@ -81,18 +81,6 @@ private function pretend_to_be_firefox() {
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
}
- /**
- * Post-test cleanup. Replaces old $CFG.
- */
- public function tearDown() {
- // Replace original user agent.
- if (isset($this->realserver)) {
- $_SERVER = $this->realserver;
- }
-
- parent::tearDown();
- }
-
/**
* Test for the core_media_player is_enabled.
*/
@@ -108,6 +96,32 @@ public function test_is_enabled() {
$this->assertTrue($test->is_enabled());
}
+ /**
+ * Test for core_media::get_filename.
+ */
+ public function test_get_filename() {
+ $this->assertEquals('frog.mp4', core_media::get_filename(new moodle_url(
+ '/pluginfile.php/312/mod_page/content/7/frog.mp4')));
+ // This should work even though slasharguments is true, because we want
+ // it to support 'legacy' links if somebody toggles the option later.
+ $this->assertEquals('frog.mp4', core_media::get_filename(new moodle_url(
+ '/pluginfile.php?file=/312/mod_page/content/7/frog.mp4')));
+ }
+
+ /**
+ * Test for core_media::get_extension.
+ */
+ public function test_get_extension() {
+ $this->assertEquals('mp4', core_media::get_extension(new moodle_url(
+ '/pluginfile.php/312/mod_page/content/7/frog.mp4')));
+ $this->assertEquals('', core_media::get_extension(new moodle_url(
+ '/pluginfile.php/312/mod_page/content/7/frog')));
+ $this->assertEquals('mp4', core_media::get_extension(new moodle_url(
+ '/pluginfile.php?file=/312/mod_page/content/7/frog.mp4')));
+ $this->assertEquals('', core_media::get_extension(new moodle_url(
+ '/pluginfile.php?file=/312/mod_page/content/7/frog')));
+ }
+
/**
* Test for the core_media_player list_supported_urls.
*/
@@ -344,6 +358,26 @@ public function test_embed_url_other_formats() {
$this->assertTrue(self::str_contains($t, '</audio>'));
}
+ /**
+ * Same as test_embed_url MP3 test, but for slash arguments.
+ */
+ public function test_slash_arguments() {
+ global $CFG, $PAGE;
+
+ // Again we do not turn slasharguments actually on, because it has to
+ // work regardless of the setting of that variable in case of handling
+ // links created using previous setting.
+
+ // Enable MP3 and get renderer.
+ $CFG->core_media_enable_mp3 = true;
+ $renderer = new core_media_renderer_test($PAGE, '');
+
+ // Format: mp3.
+ $url = new moodle_url('http://example.org/pluginfile.php?file=x/y/z/test.mp3');
+ $t = $renderer->embed_url($url);
+ $this->assertTrue(self::str_contains($t, 'core_media_mp3_'));
+ }
+
/**
* Test for core_media_renderer embed_url.
* Checks the EMBED_OR_BLANK option.
View
@@ -763,14 +763,30 @@ public function out_as_local_url($escaped = true, array $overrideparams = null)
* By default the path includes slash-arguments (for example,
* '/myfile.php/extra/arguments') so it is what you would expect from a
* URL path. If you don't want this behaviour, you can opt to exclude the
- * slash arguments.
+ * slash arguments. (Be careful: if the $CFG variable slasharguments is
+ * disabled, these URLs will have a different format and you may need to
+ * look at the 'file' parameter too.)
*
* @param bool $includeslashargument If true, includes slash arguments
* @return string Path of URL
*/
public function get_path($includeslashargument = true) {
return $this->path . ($includeslashargument ? $this->slashargument : '');
}
+
+ /**
+ * Returns a given parameter value from the URL.
+ *
+ * @param string $name Name of parameter
+ * @return string Value of parameter or null if not set
+ */
+ public function get_param($name) {
+ if (array_key_exists($name, $this->params)) {
+ return $this->params[$name];
+ } else {
+ return null;
+ }
+ }
}
/**

0 comments on commit a3c17ae

Please sign in to comment.