MDL-80079 core: Correct incorrect arg to format_text::$options

This commit is contained in:
Andrew Nicols
2023-11-14 22:28:55 +08:00
parent 79a42253c3
commit e7c8762353
3 changed files with 27 additions and 1 deletions
+15
View File
@@ -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(
'<p>Example</p>',
format_text('<p>Example</p>', 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,
);
}
}
+1 -1
View File
@@ -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 ===
+11
View File
@@ -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;