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

This commit is contained in:
Andrew Nicols
2024-02-12 11:11:17 +08:00
parent 35a8e23034
commit e8eb163ff9
2 changed files with 52 additions and 3 deletions
+33 -1
View File
@@ -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;
}
}
+19 -2
View File
@@ -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