MDL-72762 backup: Accept empty and whitespace-only UI labels.

This was causing problems when sections or activity names where
set to be empty or whitespace-only, with the PARAM_CLEANHTML
leading to exception.

Now they are supported and handled like   (0xc2a0) to
allow the process to continue.

Added a few extra tests to confirm the behaviour.
This commit is contained in:
Eloy Lafuente (stronk7)
2022-02-11 23:50:34 +01:00
parent d24a4ab56f
commit 00b10bbb31
3 changed files with 26 additions and 9 deletions
+4
View File
@@ -1,6 +1,10 @@
This files describes API changes in /backup/*,
information provided here is intended especially for developers.
=== 4.0 ===
* Backup UI labels now accept empty/whitespace-only contents.
=== 3.11 ===
* New setting called "Include permission overrides" has been implemented. The default
+6 -1
View File
@@ -146,7 +146,12 @@ class base_setting_ui {
* @throws base_setting_ui_exception when the label is not valid.
* @param string $label
*/
public function set_label(string $label) :void {
public function set_label(string $label): void {
// Let's avoid empty/whitespace-only labels, so the html clean (that makes trim()) doesn't fail.
if (trim($label) === '') {
$label = ' '; // Will be converted to non-breaking utf-8 char 0xc2a0 by PARAM_CLEANHTML.
}
$label = clean_param($label, PARAM_CLEANHTML);
if ($label === '') {
+16 -8
View File
@@ -58,13 +58,21 @@ class base_setting_ui_test extends advanced_testcase {
$bsui->set_label(123);
$this->assertSame('123', $bsui->get_label());
// Should raise an exception when label is empty.
try {
$bsui->set_label('');
$this->assertTrue(false, 'base_setting_ui_exception');
} catch (Exception $exception) {
$this->assertTrue($exception instanceof base_setting_ui_exception);
$this->assertEquals($exception->errorcode, 'setting_invalid_ui_label');
}
// Should be converted to non-breaking space (U+00A0) when label is empty.
$bsui->set_label('');
$this->assertSame("\u{00A0}", $bsui->get_label());
// Should be converted to non-breaking space (U+00A0) when the trimmed label is empty.
$bsui->set_label(" \t\t\n\n\t\t ");
$this->assertSame("\u{00A0}", $bsui->get_label());
// Should clean partially the wrong bits.
$bsui->set_label('<b onmouseover=alert("test")>label</b>');
$this->assertSame('<b>label</b>', $bsui->get_label());
// Should raise an exception when cleaning ends with 100% empty.
$this->expectException(base_setting_ui_exception::class);
$this->expectExceptionMessage('error/setting_invalid_ui_label');
$bsui->set_label('<script>alert("test")</script>');
}
}