MDL-81284 mod_wiki: Add stricter cleaning of contentformat

* When restoring backup we clean the contentformat and defaultformat
to avoid issue with invalid value.
* Add tests for format and defaultformat so to check that we
 filter out invalid formats
* Remove private $basepath variable as it was used one and not always
initialised correctly.
This commit is contained in:
Laurent David 2024-03-20 11:49:26 +01:00 committed by Jenkins
parent 25b1b643c8
commit d5a99b1f33
4 changed files with 151 additions and 12 deletions

View File

@ -61,6 +61,7 @@ class restore_wiki_activity_structure_step extends restore_activity_structure_st
// See MDL-9367.
$data->editbegin = $this->apply_date_offset($data->editbegin);
$data->editend = $this->apply_date_offset($data->editend);
$data->defaultformat = clean_param($data->defaultformat, PARAM_ALPHA);
// insert the wiki record
$newitemid = $DB->insert_record('wiki', $data);
@ -120,7 +121,7 @@ class restore_wiki_activity_structure_step extends restore_activity_structure_st
$oldid = $data->id;
$data->pageid = $this->get_new_parentid('wiki_page');
$data->userid = $this->get_mappingid('user', $data->userid);
$data->contentformat = clean_param($data->contentformat, PARAM_ALPHA);
$newitemid = $DB->insert_record('wiki_versions', $data);
$this->set_mapping('wiki_version', $oldid, $newitemid);
}

View File

@ -44,16 +44,20 @@ defined('MOODLE_INTERNAL') || die();
* @package mod_wiki
*/
class wiki_parser_proxy {
private static $parsers = array();
private static $basepath = "";
public static function parse(&$string, $type, $options = array()) {
if (empty(self::$basepath)) {
global $CFG;
self::$basepath = $CFG->dirroot . '/mod/wiki/parser/';
}
/**
* @var array $parsers Array of parser instances
*/
private static $parsers = [];
/**
* Parse a string using a specific parser
*
* @param string $string The string to parse
* @param string $type The parser type
* @param array $options The parser options
* @return mixed The parsed string or false if the parser type is not found
*/
public static function parse(&$string, $type, $options = []) {
$type = strtolower($type);
self::$parsers[$type] = null; // Reset the current parser because it may have other options.
if (self::create_parser_instance($type)) {
@ -86,8 +90,14 @@ class wiki_parser_proxy {
}
private static function create_parser_instance($type) {
global $CFG;
$type = clean_param($type, PARAM_ALPHA);
if (empty(self::$parsers[$type])) {
include_once(self::$basepath . "markups/$type.php");
$path = "$CFG->dirroot/mod/wiki/parser/markups/$type.php";
if (!file_exists($path)) {
throw new moodle_exception("Parser type $type not found");
}
include_once($path);
$class = strtolower($type) . "_parser";
if (class_exists($class)) {
self::$parsers[$type] = new $class;

View File

@ -0,0 +1,80 @@
<?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/>.
namespace mod_wiki\backup;
/**
* Unit tests for wiki restoration process
*
* @package mod_wiki
* @copyright 2024 Laurent David <laurent.david@moodle.com>
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_format_test extends \advanced_testcase {
/**
* Data provider for test_duplicating_wiki_removes_unwanted_formats.
*
* @return array[]
*/
public static function restore_format_test_provider(): array {
return [
'creole' => [
'data' => 'creole',
'expected' => 'creole',
],
'html' => [
'data' => 'html',
'expected' => 'html',
],
'wikimarkup' => [
'data' => 'nwiki',
'expected' => 'nwiki',
],
'wrong format' => [
'data' => '../wrongformat123',
'expected' => 'wrongformat',
],
];
}
/**
* Test that duplicating a wiki removes unwanted / invalid format.
*
* @param string $format The format of the wiki.
* @param string $expected The expected format of the wiki after duplication.
*
* @covers \restore_wiki_activity_structure_step
* @dataProvider restore_format_test_provider
*/
public function test_duplicating_wiki_removes_unwanted_formats(string $format, string $expected): void {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Make a test course.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$wiki = $generator->create_module('wiki', array_merge(['course' => $course->id, 'defaultformat' => $format]));
// Duplicate the wiki.
$newwikicm = duplicate_module($course, get_fast_modinfo($course)->get_cm($wiki->cmid));
// Verify the settings of the duplicated activity.
$newwiki = $DB->get_record('wiki', ['id' => $newwikicm->instance]);
$this->assertEquals($expected, $newwiki->defaultformat);
}
}

View File

@ -39,7 +39,7 @@ require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class wikiparser_test extends \basic_testcase {
class wikiparser_test extends \advanced_testcase {
/**
* URL inside the clickable text of some link should not be turned into a new link via the url_tag_rule.
@ -332,4 +332,52 @@ class wikiparser_test extends \basic_testcase {
$this->assertNotEquals(false, $section);
}
/**
* Test that format that are not supported are raising an exception
*
* @param string $format
* @param string $expected
* @covers \wiki_parser_proxy::parse
* @dataProvider format_parser_provider
*/
public function test_format_parser(string $format, string $expected) {
$this->resetAfterTest();
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$wiki = $generator->create_module('wiki', array_merge(['course' => $course->id, 'defaultformat' => $format]));
$wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki');
if ($expected === 'exception') {
$this->expectException(\moodle_exception::class);
}
$page = $wikigenerator->create_page($wiki);
$version = wiki_get_current_version($page->id);
$this->assertEquals($expected, $version->contentformat);
}
/**
* Data provider for test_format_parser
*
* @return array[]
*/
public static function format_parser_provider(): array {
return [
'creole' => [
'data' => 'creole',
'expected' => 'creole',
],
'html' => [
'data' => 'html',
'expected' => 'html',
],
'wikimarkup' => [
'data' => 'nwiki',
'expected' => 'nwiki',
],
'wrong format' => [
'data' => '../wrongformat123',
'expected' => 'exception',
],
];
}
}