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

This commit is contained in:
Andrew Nicols 2023-11-11 16:36:46 +08:00
parent dab4a2b66f
commit dde3333eaa
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
3 changed files with 27 additions and 1 deletions

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,
);
}
}

View File

@ -10,7 +10,7 @@ information provided here is intended especially for developers.
drag/drop, which can be used to add transition effects in calling code
* 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.3 ===

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;