MDL-72744 dataformat: Escape formulas when exporting spreadsheets

Co-authored-by: Huong Nguyen <[email protected]>
This commit is contained in:
David Woloszyn
2025-12-04 08:57:58 +07:00
committed by Huong Nguyen
co-authored by Huong Nguyen
parent 09a76fcb45
commit bcf67dd1cb
4 changed files with 145 additions and 1 deletions
+76
View File
@@ -122,4 +122,80 @@ final class dataformat_test extends \advanced_testcase {
$this->assertStringStartsWith($filerecord['filename'], $file->get_filename());
$this->assertGreaterThan(0, $file->get_filesize());
}
/**
* Data provider for test_escape_spreadsheet_formula.
*
* @return array
*/
public static function escape_spreadsheet_formula_provider(): array {
return [
'null stays null' => [
null,
null,
],
'empty string stays empty' => [
'',
'',
],
'Formula with tab' => [
'value' => "\t=SUM(1+1)",
'expected' => "'\t=SUM(1+1)",
],
'Formula with carriage return' => [
'value' => "\r=SUM(1+1)",
'expected' => "'\r=SUM(1+1)",
],
'Formula with new line' => [
'value' => "\n=SUM(1+1)",
'expected' => "'\n=SUM(1+1)",
],
'Formula starting with "="' => [
'value' => "=SUM(1+1)",
'expected' => "'=SUM(1+1)",
],
'Formula starting with "+"' => [
'value' => "+1+1",
'expected' => "'+1+1",
],
'Formula starting with "-"' => [
'value' => "-1+1",
'expected' => "'-1+1",
],
'Formula starting with "@"' => [
'value' => "@A5",
'expected' => "'@A5",
],
'Null placeholder' => [
'value' => "-",
'expected' => "-",
],
'dash with leading space is not placeholder, so escaped' => [
' -',
"' -",
],
'dash with trailing space is not placeholder, so escaped' => [
'- ',
"'- ",
],
'Non-formula' => [
'value' => "Hello there",
'expected' => "Hello there",
],
];
}
/**
* Test escape_spreadsheet_formula.
*
* @dataProvider escape_spreadsheet_formula_provider
* @param string|null $value The value to test.
* @param string|null $expected The expected value after escaping.
*/
public function test_escape_spreadsheet_formula(?string $value, ?string $expected): void {
$this->resetAfterTest();
$escapedvalue = dataformat::escape_spreadsheet_formula($value);
$this->assertEquals($expected, $escapedvalue);
}
}