From c000eeeecf18c5fdb5890e1f1332270c703c4c5c Mon Sep 17 00:00:00 2001 From: FLIO Date: Thu, 16 Feb 2023 01:00:39 +0100 Subject: [PATCH] feature (#21426) edit ExportTest for resolve error and add test for utf8 --- test/phpunit/ExportTest.php | 102 ++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/test/phpunit/ExportTest.php b/test/phpunit/ExportTest.php index cf2f0520908c7..0a0a1c867ba4c 100644 --- a/test/phpunit/ExportTest.php +++ b/test/phpunit/ExportTest.php @@ -149,6 +149,94 @@ protected function tearDown(): void print __METHOD__."\n"; } + /** + * Other tests + * + * @return void + */ + public function testExportCsvUtf() + { + global $conf,$user,$langs,$db; + + $model='csvutf8'; + + $conf->global->EXPORT_CSV_SEPARATOR_TO_USE = ','; + print 'EXPORT_CSV_SEPARATOR_TO_USE = '.$conf->global->EXPORT_CSV_SEPARATOR_TO_USE; + + // Creation of class to export using model ExportXXX + $dir = DOL_DOCUMENT_ROOT . "/core/modules/export/"; + $file = "export_".$model.".modules.php"; + $classname = "Export".$model; + require_once $dir.$file; + $objmodel = new $classname($db); + + // First test without option USE_STRICT_CSV_RULES + unset($conf->global->USE_STRICT_CSV_RULES); + + $valtotest='A simple string'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, 'A simple string'); + + $valtotest='A string with , and ; inside'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, '"A string with , and ; inside"', 'Error in csvClean for '.$file); + + $valtotest='A string with " inside'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, '"A string with "" inside"'); + + $valtotest='A string with " inside and '."\r\n".' carriage returns'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, '"A string with "" inside and \n carriage returns"'); + + $valtotest='A string with html
content
inside
'."\n"; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, '"A string with html
content
inside"'); + + // Same tests with strict mode + $conf->global->USE_STRICT_CSV_RULES = 1; + + $valtotest='A simple string'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, 'A simple string'); + + $valtotest='A string with , and ; inside'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, '"A string with , and ; inside"'); + + $valtotest='A string with " inside'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, '"A string with "" inside"'); + + $valtotest='A string with " inside and '."\r\n".' carriage returns'; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, "\"A string with \"\" inside and \r\n carriage returns\""); + + $valtotest='A string with html
content
inside
'."\n"; + print __METHOD__." valtotest=".$valtotest."\n"; + $result = $objmodel->csvClean($valtotest, $langs->charset_output); + print __METHOD__." result=".$result."\n"; + $this->assertEquals($result, '"A string with html
content
inside"'); + } + /** * Other tests @@ -159,7 +247,7 @@ public function testExportOther() { global $conf,$user,$langs,$db; - $model='csv'; + $model='csviso'; $conf->global->EXPORT_CSV_SEPARATOR_TO_USE = ','; print 'EXPORT_CSV_SEPARATOR_TO_USE = '.$conf->global->EXPORT_CSV_SEPARATOR_TO_USE; @@ -263,7 +351,15 @@ public function testExportPersonalizedExport() dol_mkdir($conf->export->dir_temp); - $model='csv'; + $model='csviso'; + + // Build export file + print "Process build_file for model = ".$model."\n"; + $result=$objexport->build_file($user, $model, $datatoexport, $array_selected, array(), $sql); + $expectedresult = 1; + $this->assertEquals($expectedresult, $result, 'Error in CSV export'); + + $model='csvutf8'; // Build export file print "Process build_file for model = ".$model."\n"; @@ -353,7 +449,7 @@ public function testExportModulesDatasets() { global $conf,$user,$langs,$db; - $model='csv'; + $model='csviso'; $filterdatatoexport=''; //$filterdatatoexport='';