diff --git a/lib/classes/formatting.php b/lib/classes/formatting.php index cc4bafa266c..a5c87aa7810 100644 --- a/lib/classes/formatting.php +++ b/lib/classes/formatting.php @@ -30,6 +30,9 @@ class formatting { /** @var bool Whether to apply striptags */ protected ?bool $striptags; + /** @var bool Whether to apply filters */ + protected ?bool $filterall; + /** * Given a simple string, this function returns the string * processed by enabled string filters if $CFG->filterall is enabled @@ -111,7 +114,7 @@ class formatting { // Regular expression moved to its own method for easier unit testing. $string = $options['escape'] ? replace_ampersands_not_followed_by_entity($string) : $string; - if (!empty($CFG->filterall) && $options['filter']) { + if (!empty($this->get_filterall()) && $options['filter']) { $filtermanager = \filter_manager::instance(); $filtermanager->setup_page_for_filters($PAGE, $options['context']); // Setup global stuff filters may have. $string = $filtermanager->filter_string($string, $options['context']); @@ -415,4 +418,33 @@ class formatting { return $CFG->formatstringstriptags; } + + /** + * Set the value of the filterall setting. + * + * @param bool $filterall + * @return formatting + */ + public function set_filterall(bool $filterall): self { + $this->filterall = $filterall; + + return $this; + } + + /** + * Get the current filterall value. + * + * Reverts to CFG->filterall if not set. + * + * @return bool + */ + public function get_filterall(): bool { + global $CFG; + + if (isset($this->filterall)) { + return $this->filterall; + } + + return $CFG->filterall; + } } diff --git a/lib/tests/formatting_test.php b/lib/tests/formatting_test.php index 887e465e5fd..1bc42a53f7b 100644 --- a/lib/tests/formatting_test.php +++ b/lib/tests/formatting_test.php @@ -148,15 +148,32 @@ class formatting_test extends \advanced_testcase { $this->assertEquals($expectednofilter, $nofilterresult); // Add the multilang filter. Make sure it's enabled globally. - $CFG->filterall = true; $CFG->stringfilters = 'multilang'; filter_set_global_state('multilang', TEXTFILTER_ON); filter_set_local_state('multilang', $context->id, TEXTFILTER_ON); - // This time we want to apply the filters. + + // Even after setting the filters, no filters are applied yet. + $nofilterresult = $formatting->format_string($rawstring, $striplinks, $options); + $this->assertEquals($expectednofilter, $nofilterresult); + + // Apply the filter as an option. $options['filter'] = true; $filterresult = $formatting->format_string($rawstring, $striplinks, $options); $this->assertMatchesRegularExpression("/$expectedfilter/", $filterresult); + // Apply it as a formatting setting. + unset($options['filter']); + $formatting->set_filterall(true); + $filterresult = $formatting->format_string($rawstring, $striplinks, $options); + $this->assertMatchesRegularExpression("/$expectedfilter/", $filterresult); + + // Unset it and we do not filter. + $formatting->set_filterall(false); + $nofilterresult = $formatting->format_string($rawstring, $striplinks, $options); + $this->assertEquals($expectednofilter, $nofilterresult); + + // Set it again. + $formatting->set_filterall(true); filter_set_local_state('multilang', $context->id, TEXTFILTER_OFF); // Confirm that we get back the cached string. The result should be