From d311cab0c0e8e7abb042bdc22766699362764269 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Sat, 26 Nov 2022 14:02:32 +0100 Subject: [PATCH] MDL-76493 core: make sure format_string(null) works on PHP 8.1 --- lib/tests/weblib_test.php | 4 ++++ lib/weblib.php | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php index a6c6aa9b3c3..0663cc84e67 100644 --- a/lib/tests/weblib_test.php +++ b/lib/tests/weblib_test.php @@ -48,6 +48,10 @@ class weblib_test extends advanced_testcase { // Unicode entities. $this->assertSame("ᅻ", format_string("ᅻ")); + // Nulls. + $this->assertSame('', format_string(null)); + $this->assertSame('', format_string(null, true, ['escape' => false])); + // < and > signs. $originalformatstringstriptags = $CFG->formatstringstriptags; diff --git a/lib/weblib.php b/lib/weblib.php index c3354e559af..374b162e161 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1430,6 +1430,11 @@ function reset_text_filters_cache($phpunitreset = false) { function format_string($string, $striplinks = true, $options = null) { global $CFG, $PAGE; + if ($string === '' || is_null($string)) { + // No need to do any filters and cleaning. + return ''; + } + // We'll use a in-memory cache here to speed up repeated strings. static $strcache = false;