diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 59fcd1c132f..5a6ef0577e3 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8375,11 +8375,12 @@ function count_words($string) { * Letters are defined as chars not in tags and different from whitespace. * * @category string - * @param string $string The text to be searched for letters. + * @param string $string The text to be searched for letters. May be HTML. * @return int The count of letters in the specified text. */ function count_letters($string) { $string = strip_tags($string); // Tags are out now. + $string = html_entity_decode($string); $string = preg_replace('/[[:space:]]*/', '', $string); // Whitespace are out now. return core_text::strlen($string); diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 00ff10e1827..8c02d58b1b8 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -3822,6 +3822,31 @@ class core_moodlelib_testcase extends advanced_testcase { ]; } + /** + * Test function {@see count_letters()}. + * + * @dataProvider count_letters_testcases + * @param int $expectedcount number of characters in $string. + * @param string $string the test string to count the letters of. + */ + public function test_count_letters(int $expectedcount, string $string): void { + $this->assertEquals($expectedcount, count_letters($string)); + } + + /** + * Data provider for {@see count_letters_testcases}. + * + * @return array of test cases. + */ + public function count_letters_testcases(): array { + return [ + [0, ''], + [1, 'x'], + [1, '&'], + [4, '
frog
'], + ]; + } + /** * Tests the getremoteaddr() function. */