MDL-53175 file: Allow file_rewrite_pluginfile_urls to reverse

This commit is contained in:
Juan Leyva
2016-03-30 09:32:34 +01:00
parent 1912e615f7
commit 7cd6eefe28
2 changed files with 28 additions and 2 deletions
+8 -2
View File
@@ -445,6 +445,8 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $component, $fileare
/**
* Convert encoded URLs in $text from the @@PLUGINFILE@@/... form to an actual URL.
* Passing a new option reverse = true in the $options var will make the function to convert actual URLs in $text to encoded URLs
* in the @@PLUGINFILE@@ form.
*
* @category files
* @global stdClass $CFG
@@ -454,7 +456,7 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $component, $fileare
* @param string $component
* @param string $filearea helps identify the file area.
* @param int $itemid helps identify the file area.
* @param array $options text and file options ('forcehttps'=>false)
* @param array $options text and file options ('forcehttps'=>false), use reverse = true to reverse the behaviour of the function.
* @return string the processed text.
*/
function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid, array $options=null) {
@@ -479,7 +481,11 @@ function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $fil
$baseurl = str_replace('http://', 'https://', $baseurl);
}
return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
if (!empty($options['reverse'])) {
return str_replace($baseurl, '@@PLUGINFILE@@/', $text);
} else {
return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
}
}
/**
+20
View File
@@ -951,6 +951,26 @@ EOF;
$this->assertSame(0, $extcurl->get_errno());
$this->assertSame('', $contents);
}
/**
* Test file_rewrite_pluginfile_urls.
*/
public function test_file_rewrite_pluginfile_urls() {
$syscontext = context_system::instance();
$originaltext = 'Fake test with an image <img src="@@PLUGINFILE@@/image.png">';
// Do the rewrite.
$finaltext = file_rewrite_pluginfile_urls($originaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0);
$this->assertContains("pluginfile.php", $finaltext);
// Now undo.
$options = array('reverse' => true);
$finaltext = file_rewrite_pluginfile_urls($finaltext, 'pluginfile.php', $syscontext->id, 'user', 'private', 0, $options);
// Compare the final text is the same that the original.
$this->assertEquals($originaltext, $finaltext);
}
}
/**