diff --git a/filter/emailprotect/classes/text_filter.php b/filter/emailprotect/classes/text_filter.php index 42c58a3229d..ce7e74b761e 100644 --- a/filter/emailprotect/classes/text_filter.php +++ b/filter/emailprotect/classes/text_filter.php @@ -55,7 +55,7 @@ class text_filter extends \core_filters\text_filter { * @return string */ private function alter_email($matches) { - return $matches[1].obfuscate_text($matches[2]).$matches[3]; + return $matches[1] . obfuscate_text($matches[2]) . $matches[3]; } /** diff --git a/filter/emailprotect/tests/text_filter_test.php b/filter/emailprotect/tests/text_filter_test.php index 8bbd649b028..20fc52d8130 100644 --- a/filter/emailprotect/tests/text_filter_test.php +++ b/filter/emailprotect/tests/text_filter_test.php @@ -36,9 +36,18 @@ final class text_filter_test extends \advanced_testcase { public function test_filter( string $expression, string $text, + bool $exactmatch, ): void { $filter = new text_filter(\core\context\system::instance(), []); - $this->assertMatchesRegularExpression($expression, $text); + $result = $filter->filter($text); + + $this->assertMatchesRegularExpression($expression, $result); + + if ($exactmatch) { + $this->assertEquals($text, $result); + } else { + $this->assertNotEquals($text, $result); + } } /** @@ -50,11 +59,23 @@ final class text_filter_test extends \advanced_testcase { $email = 'chaise@example.com'; return [ // No email address found. - ['/Hello, world!/', 'Hello, world!'], + [ + '/Hello, world!/', + 'Hello, world!', + true, + ], // Email addresses present. // Note: The obfuscation randomly choose which chars to obfuscate. - ['/.*@.*/', $email], - ["~.*@.*~", "$email"], + [ + '/.*(@|@).*/', + $email, + false, + ], + [ + "~.*(@|@).*~", + "$email", + false, + ], ]; } }