BUGFIX: encode the csv file by hand, fputcsv is not parsing newlines properly

This commit is contained in:
Mateusz Uzdowski 2010-10-21 23:41:47 +00:00
parent 4e6a9ec412
commit 078043f189
3 changed files with 30 additions and 20 deletions

View File

@ -124,16 +124,12 @@ class SubmittedFormReportField extends FormField {
$csvRowItems = array();
foreach ($csvHeaders as $columnName=>$columnTitle) {
if (!isset($row[$columnName])) $csvRowItems[] = ""; // This submission did not have that column, insert blank
else $csvRowItems[] = $row[$columnName];
else $csvRowItems[] = str_replace('"', '""', $row[$columnName]);
}
$csvRowItems[] = $row['Submitted'];
// Encode the row
$fp = fopen('php://memory', 'r+');
fputcsv($fp, $csvRowItems);
rewind($fp);
$csvData .= fgets($fp);
fclose($fp);
// Encode the row by hand (fputcsv fails to encode newlines properly)
if (count($csvRowItems)) $csvData .= "\"".implode($csvRowItems, "\", \"")."\"\n";
}
} else {
user_error("No submissions to export.", E_USER_ERROR);

View File

@ -47,35 +47,38 @@ class SubmittedFormTest extends FunctionalTest {
function testCSVExport() {
$export = $this->field->export($this->page->ID);
// export it back to an array (rather than string)
$exportLines = explode("\n", $export);
array_pop($exportLines); // Remove trailing empty line
// Pretend we are opening via file
$fp = fopen('php://memory', 'w+');
fwrite($fp, $export);
rewind($fp);
$data = array();
foreach($exportLines as $line) {
$fp = fopen('php://memory', 'w+');
fputs($fp, $line);
rewind($fp);
$data[] = fgetcsv($fp);
}
while($data[] = fgetcsv($fp));
array_pop($data);
fclose($fp);
// check the headers are fine and include every legacy field. They should also be ordered
// Check the headers are fine and include every legacy field. They should also be ordered
// according to the latest form layout.
$this->assertEquals($data[0], array(
'First', 'Submitted Title 2', 'Submitted Title', 'Submitted'
));
// check the number of records in the export
// Check the number of records in the export
$this->assertEquals(count($data), 12);
// Make sure the number of columns matches
$this->assertEquals(count($data[1]), 4);
$this->assertEquals(count($data[2]), 4);
$this->assertEquals(count($data[3]), 4);
$this->assertEquals(count($data[11]), 4);
// Specific value tests
$this->assertEquals($data[1][1], 'quote " and comma , test');
$this->assertEquals($data[1][2], 'Value 1');
$this->assertEquals($data[2][1], 'Value 2');
$this->assertEquals($data[3][1], "multi\nline\ntest");
$this->assertEquals($data[11][0], 'First');
$this->assertEquals($data[11][1], 'Second');

View File

@ -24,6 +24,8 @@ SubmittedForm:
long-10:
long-11:
long-12:
UserDefinedForm:
@ -71,13 +73,22 @@ SubmittedFormField:
Title: Submitted Title 2
Parent: =>SubmittedForm.long-1
Value: 'quote " and comma , test'
long-submitted-2:
Parent: =>SubmittedForm.long-2
Name: Submitted Name 2
Title: Submitted Title 2
Value: Value 2
long-submitted-3:
Name: Submitted Name 2
Title: Submitted Title 2
Parent: =>SubmittedForm.long-3
Value: |
multi
line
test
long-submitted-11a:
Parent: =>SubmittedForm.long-11
Name: First