From e7c8762353934dbf8d623a6d01fe4df211da598d Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sat, 11 Nov 2023 16:36:46 +0800 Subject: [PATCH] MDL-80079 core: Correct incorrect arg to format_text::$options --- lib/tests/weblib_format_text_test.php | 15 +++++++++++++++ lib/upgrade.txt | 2 +- lib/weblib.php | 11 +++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/tests/weblib_format_text_test.php b/lib/tests/weblib_format_text_test.php index 3593dcf20ba..53a6e908360 100644 --- a/lib/tests/weblib_format_text_test.php +++ b/lib/tests/weblib_format_text_test.php @@ -25,6 +25,7 @@ namespace core; * @category test * @copyright 2015 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + * @covers ::format_text */ class weblib_format_text_test extends \advanced_testcase { @@ -268,4 +269,18 @@ class weblib_format_text_test extends \advanced_testcase { ], ]; } + + public function test_with_context_as_options(): void { + $this->assertEquals( + '

Example

', + format_text('

Example

', FORMAT_HTML, \context_system::instance()), + ); + + $messages = $this->getDebuggingMessages(); + $this->assertdebuggingcalledcount(1); + $this->assertStringContainsString( + 'The options argument should not be a context object directly.', + $messages[0]->message, + ); + } } diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 93d909a54db..69c122f6882 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -8,7 +8,7 @@ information provided here is intended especially for developers. * Add a new method core_user::get_initials to get the initials of a user in a way compatible with internationalisation. * course_modinfo now has a purge_course_modules_cache() method, which takes a list of cmids and purges them all in a single cache set. -* The options for `format_string()` are now checked for incorrectly passed context objects. +* The options for `format_string()`, and `format_text()` are now checked for incorrectly passed context objects. Please note that this was never an accepted value but previously failed silently. === 4.2.3 === diff --git a/lib/weblib.php b/lib/weblib.php index db62be83442..3ff6738d52b 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1268,6 +1268,17 @@ function format_text($text, $format = FORMAT_MOODLE, $options = null, $courseidd return ''; } + if ($options instanceof \core\context) { + // A common mistake has been to call this function with a context object. + // This has never been expected, nor supported. + debugging( + 'The options argument should not be a context object directly. ' . + ' Please pass an array with a context key instead.', + DEBUG_DEVELOPER, + ); + $options = ['context' => $options]; + } + // Detach object, we can not modify it. $options = (array)$options;