MDL-76362 core_privacy: Shortcircuit URL rewriting on empty content

This commit is contained in:
Andrew Nicols 2023-01-06 22:37:59 +08:00
parent a4ea607c24
commit 77a0a535b3
3 changed files with 19 additions and 12 deletions

View File

@ -14,17 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the moodle format implementation of the content writer.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_privacy\local\request;
defined('MOODLE_INTERNAL') || die();
/**
* The moodle_content_writer is the default Moodle implementation of a content writer.
*
@ -33,6 +24,7 @@ defined('MOODLE_INTERNAL') || die();
*
* Objects of data are stored as JSON.
*
* @package core_privacy
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -168,7 +160,10 @@ class moodle_content_writer implements content_writer {
* @param string $text The text to be processed
* @return string The processed string
*/
public function rewrite_pluginfile_urls(array $subcontext, $component, $filearea, $itemid, $text) : string {
public function rewrite_pluginfile_urls(array $subcontext, $component, $filearea, $itemid, $text): string {
if ($text === null || $text === '') {
return '';
}
// Need to take into consideration the subcontext to provide the full path to this file.
$subcontextpath = '';
if (!empty($subcontext)) {
@ -179,7 +174,7 @@ class moodle_content_writer implements content_writer {
$returnstring = $path . DIRECTORY_SEPARATOR . $this->get_files_target_url($component, $filearea, $itemid) . '/';
$returnstring = clean_param($returnstring, PARAM_PATH);
return str_replace('@@PLUGINFILE@@/', $returnstring, $text ?? '');
return str_replace('@@PLUGINFILE@@/', $returnstring, $text);
}
/**

View File

@ -381,7 +381,7 @@ class content_writer implements \core_privacy\local\request\content_writer {
* @param string $text The text to be processed
* @return string The processed string
*/
public function rewrite_pluginfile_urls(array $subcontext, $component, $filearea, $itemid, $text) : string {
public function rewrite_pluginfile_urls(array $subcontext, $component, $filearea, $itemid, $text): string {
return str_replace('@@PLUGINFILE@@/', 'files/', $text ?? '');
}

View File

@ -1292,6 +1292,18 @@ class moodle_content_writer_test extends advanced_testcase {
*/
public function rewrite_pluginfile_urls_provider() {
return [
'nullcontent' => [
'intro',
0,
null,
'',
],
'emptycontent' => [
'intro',
0,
'',
'',
],
'zeroitemid' => [
'intro',
0,