diff --git a/lib/classes/formatting.php b/lib/classes/formatting.php index 1de6dc4210e..c46b1d49123 100644 --- a/lib/classes/formatting.php +++ b/lib/classes/formatting.php @@ -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; + } } diff --git a/lib/tests/formatting_test.php b/lib/tests/formatting_test.php index 33536468634..53f09c2195f 100644 --- a/lib/tests/formatting_test.php +++ b/lib/tests/formatting_test.php @@ -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); }