MDL-80072 core: Move CFG->forceclean to formatter property

This commit is contained in:
Andrew Nicols
2024-02-12 11:11:17 +08:00
parent 6bb0c91a73
commit f9dc48691d
2 changed files with 39 additions and 7 deletions
+35 -1
View File
@@ -24,6 +24,9 @@ namespace core;
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class formatting {
/** @var bool Whether to apply forceclean */
protected ?bool $forceclean;
/**
* Given a simple string, this function returns the string
* processed by enabled string filters if $CFG->filterall is enabled
@@ -195,7 +198,7 @@ class formatting {
$options['noclean'] = false;
}
}
if (!empty($CFG->forceclean)) {
if (!empty($this->get_forceclean())) {
// Whatever the caller claims, the admin wants all content cleaned anyway.
$options['noclean'] = false;
}
@@ -349,4 +352,35 @@ class formatting {
return $text;
}
/**
* Set the value of the forceclean setting.
*
* @param bool $forceclean
* @return self
*/
public function set_forceclean(bool $forceclean): self {
$this->forceclean = $forceclean;
return $this;
}
/**
* Get the current forceclean value.
*
* @return bool
*/
public function get_forceclean(): bool {
global $CFG;
if (isset($this->forceclean)) {
return $this->forceclean;
}
if (isset($CFG->forceclean)) {
return $CFG->forceclean;
}
return false;
}
}
+4 -6
View File
@@ -532,23 +532,21 @@ class formatting_test extends \advanced_testcase {
* @param string $cleaned Expected output of format_text() with noclean=false
*/
public function test_format_text_cleaning($input, $nocleaned, $cleaned): void {
global $CFG;
$this->resetAfterTest();
$formatter = new formatting();
$CFG->forceclean = false;
$formatter->set_forceclean(false);
$actual = $formatter->format_text($input, FORMAT_HTML, ['filter' => false, 'noclean' => false]);
$this->assertEquals($cleaned, $actual);
$CFG->forceclean = true;
$formatter->set_forceclean(true);
$actual = $formatter->format_text($input, FORMAT_HTML, ['filter' => false, 'noclean' => false]);
$this->assertEquals($cleaned, $actual);
$CFG->forceclean = false;
$formatter->set_forceclean(false);
$actual = $formatter->format_text($input, FORMAT_HTML, ['filter' => false, 'noclean' => true]);
$this->assertEquals($nocleaned, $actual);
$CFG->forceclean = true;
$formatter->set_forceclean(true);
$actual = $formatter->format_text($input, FORMAT_HTML, ['filter' => false, 'noclean' => true]);
$this->assertEquals($cleaned, $actual);
}