diff --git a/mod/label/db/upgrade.php b/mod/label/db/upgrade.php index 482149fb87f..d4623bd5890 100644 --- a/mod/label/db/upgrade.php +++ b/mod/label/db/upgrade.php @@ -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; } diff --git a/mod/label/lib.php b/mod/label/lib.php index 03c2f712584..1e7d194d1a2 100644 --- a/mod/label/lib.php +++ b/mod/label/lib.php @@ -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)) { diff --git a/mod/label/tests/lib_test.php b/mod/label/tests/lib_test.php index 36d58202954..78811743d0a 100644 --- a/mod/label/tests/lib_test.php +++ b/mod/label/tests/lib_test.php @@ -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' => '

Simple textual content

', + 'format' => FORMAT_HTML, + 'expected' => 'Simple textual content' + ], + 'empty' => [ + 'content' => '', + 'format' => FORMAT_HTML, + 'expected' => 'Test label 1' + ], + 'withaudiocontent' => [ + 'content' => '

Test with audio

+

    

', + 'format' => FORMAT_HTML, + 'expected' => 'Test with audio' + ], + 'withvideo' => [ + 'content' => '

Test video

+

  

', + 'format' => FORMAT_HTML, + 'expected' => 'Test video https://www.youtube.com/watch?v=xxxyy' + ], + 'with video trimming' => [ + 'content' => '

Test with video to be trimmed

+

  

', + '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   several spaces", + 'format' => FORMAT_HTML, + 'expected' => 'Content with several spaces' + ], + 'empty spaces' => [ + 'content' => '   ', + 'format' => FORMAT_HTML, + 'expected' => 'Text and media area' + ], + 'only html' => [ + 'content' => '', + 'format' => FORMAT_HTML, + 'expected' => 'Text and media area' + ] + ]; + } + /** * Creates an action event. * diff --git a/mod/label/version.php b/mod/label/version.php index 3772714ff3d..d974ea91f3e 100644 --- a/mod/label/version.php +++ b/mod/label/version.php @@ -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;