MDL-77612 mod_label: Label name fix

* The @@PLUGINFILE@@ placeholder or URLs should not be displayed in the
course index for labels
This commit is contained in:
Laurent David 2023-03-16 09:14:03 +01:00
parent bd2de8edf6
commit 9f1f4de23b
4 changed files with 122 additions and 3 deletions

View File

@ -69,5 +69,31 @@ function xmldb_label_upgrade($oldversion) {
// Automatically generated Moodle v4.1.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2022112801) {
$prevlang = force_current_language($CFG->lang);
$labels = $DB->get_recordset('label');
foreach ($labels as $label) {
// Make sure that all labels have now the same name according to the new convention.
// Note this is the same (and duplicated) code as in get_label_name as we cannot call any API function
// during upgrade.
$name = html_to_text(format_string($label->intro, true));
$name = preg_replace('/@@PLUGINFILE@@\/[[:^space:]]+/i', '', $name);
// Remove double space and also nbsp; characters.
$name = preg_replace('/\s+/u', ' ', $name);
$name = trim($name);
if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) {
$name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH) . "...";
}
if (empty($name)) {
$name = get_string('modulename', 'label');
}
$label->name = $name;
$DB->update_record('label', $label);
}
$labels->close();
force_current_language($prevlang);
upgrade_mod_savepoint(true, 2022112801, 'label');
}
return true;
}

View File

@ -34,9 +34,13 @@ define("LABEL_MAX_NAME_LENGTH", 50);
* @return string
*/
function get_label_name($label) {
$name = strip_tags(format_string($label->intro,true));
$name = html_to_text(format_string($label->intro, true));
$name = preg_replace('/@@PLUGINFILE@@\/[[:^space:]]+/i', '', $name);
// Remove double space and also nbsp; characters.
$name = preg_replace('/\s+/u', ' ', $name);
$name = trim($name);
if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) {
$name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH)."...";
$name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH) . "...";
}
if (empty($name)) {

View File

@ -204,6 +204,95 @@ class lib_test extends \advanced_testcase {
$this->assertNull($actionevent);
}
/**
* Check label name with different content inserted in the label intro.
*
* @param string $labelcontent
* @param string $labelformat
* @param string $expectedlabelname
* @return void
* @covers \get_label_name
* @dataProvider label_get_name_data_provider
*/
public function test_label_get_label_name(string $labelcontent, string $labelformat, string $expectedlabelname): void {
$course = $this->getDataGenerator()->create_course();
// When creating the module, get_label_name is called and fills label->name.
$label = $this->getDataGenerator()->create_module('label', [
'course' => $course->id,
'intro' => $labelcontent,
'introformat' => $labelformat
]
);
$this->assertEquals($expectedlabelname, $label->name);
}
/**
* Dataprovider for test_label_get_label_name
*
* @return array
*/
public function label_get_name_data_provider(): array {
return [
'simple' => [
'content' => '<p>Simple textual content<p>',
'format' => FORMAT_HTML,
'expected' => 'Simple textual content'
],
'empty' => [
'content' => '',
'format' => FORMAT_HTML,
'expected' => 'Test label 1'
],
'withaudiocontent' => [
'content' => '<p>Test with audio</p>
<p>&nbsp; &nbsp;<audio controls="controls">
<source src="@@PLUGINFILE@@/moodle-hit-song.mp3">
@@PLUGINFILE@@/moodle-hit-song.mp3
</audio>&nbsp;</p>',
'format' => FORMAT_HTML,
'expected' => 'Test with audio'
],
'withvideo' => [
'content' => '<p>Test video</p>
<p>&nbsp;<video controls="controls">
<source src="https://www.youtube.com/watch?v=xxxyy">
https://www.youtube.com/watch?v=xxxyy
</video>&nbsp;</p>',
'format' => FORMAT_HTML,
'expected' => 'Test video https://www.youtube.com/watch?v=xxxyy'
],
'with video trimming' => [
'content' => '<p>Test with video to be trimmed</p>
<p>&nbsp;<video controls="controls">
<source src="https://www.youtube.com/watch?v=xxxyy">
https://www.youtube.com/watch?v=xxxyy
</video>&nbsp;</p>',
'format' => FORMAT_HTML,
'expected' => 'Test with video to be trimmed https://www.youtube....'
],
'with plain text' => [
'content' => 'Content with @@PLUGINFILE@@/moodle-hit-song.mp3 nothing',
'format' => FORMAT_HTML,
'expected' => 'Content with nothing'
],
'with several spaces' => [
'content' => "Content with @@PLUGINFILE@@/moodle-hit-song.mp3 \r &nbsp; several spaces",
'format' => FORMAT_HTML,
'expected' => 'Content with several spaces'
],
'empty spaces' => [
'content' => ' &nbsp; ',
'format' => FORMAT_HTML,
'expected' => 'Text and media area'
],
'only html' => [
'content' => '<audio controls="controls"><source src=""></audio>',
'format' => FORMAT_HTML,
'expected' => 'Text and media area'
]
];
}
/**
* Creates an action event.
*

View File

@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2022112800; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2022112801; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2022111800; // Requires this Moodle version.
$plugin->component = 'mod_label'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;