Skip to content

Commit

Permalink
feature (Dolibarr#21426) edit ExportTest for resolve error and add te…
Browse files Browse the repository at this point in the history
…st for utf8
  • Loading branch information
FLIO committed Feb 16, 2023
1 parent c8c9980 commit c000eee
Showing 1 changed file with 99 additions and 3 deletions.
102 changes: 99 additions & 3 deletions test/phpunit/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="aaa"><strong>html<br>content</strong></a> inside<br>'."\n";
print __METHOD__." valtotest=".$valtotest."\n";
$result = $objmodel->csvClean($valtotest, $langs->charset_output);
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, '"A string with <a href=""aaa""><strong>html<br>content</strong></a> 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 <a href="aaa"><strong>html<br>content</strong></a> inside<br>'."\n";
print __METHOD__." valtotest=".$valtotest."\n";
$result = $objmodel->csvClean($valtotest, $langs->charset_output);
print __METHOD__." result=".$result."\n";
$this->assertEquals($result, '"A string with <a href=""aaa""><strong>html<br>content</strong></a> inside"');
}


/**
* Other tests
Expand All @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -353,7 +449,7 @@ public function testExportModulesDatasets()
{
global $conf,$user,$langs,$db;

$model='csv';
$model='csviso';

$filterdatatoexport='';
//$filterdatatoexport='';
Expand Down

0 comments on commit c000eee

Please sign in to comment.