Skip to content

Commit

Permalink
Merge branch 'MDL-77612-400' of https://github.com/laurentdavid/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_400_STABLE
  • Loading branch information
sarjona committed Apr 11, 2023
2 parents 7d8f31d + dad1f86 commit 64038bd
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
26 changes: 26 additions & 0 deletions mod/label/db/upgrade.php
Expand Up @@ -75,5 +75,31 @@ function xmldb_label_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2022041901, 'label');
}

if ($oldversion < 2022041902) {
$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, 2022041902, 'label');
}

return true;
}
8 changes: 6 additions & 2 deletions mod/label/lib.php
Expand Up @@ -34,9 +34,13 @@
* @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)) {
Expand Down
89 changes: 89 additions & 0 deletions mod/label/tests/lib_test.php
Expand Up @@ -204,6 +204,95 @@ public function test_label_core_calendar_provide_event_action_already_completed_
$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' => 'Label'
],
'only html' => [
'content' => '<audio controls="controls"><source src=""></audio>',
'format' => FORMAT_HTML,
'expected' => 'Label'
]
];
}

/**
* Creates an action event.
*
Expand Down
2 changes: 1 addition & 1 deletion mod/label/version.php
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2022041901; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2022041902; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2022041200; // Requires this Moodle version.
$plugin->component = 'mod_label'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;

0 comments on commit 64038bd

Please sign in to comment.