Adds missing Content-Type header to CSV-export (#3787)

Credit to @cleverer
This commit is contained in:
Nicolas Da Mutten 2018-09-25 16:55:14 +02:00 committed by Luke Towers
parent 5b7d9041fd
commit 2372f0eb23
2 changed files with 45 additions and 7 deletions

View File

@ -62,12 +62,7 @@ abstract class ExportModel extends Model
throw new ApplicationException(Lang::get('backend::lang.import_export.file_not_found_error')); throw new ApplicationException(Lang::get('backend::lang.import_export.file_not_found_error'));
} }
$headers = Response::download($csvPath, $outputName)->headers->all(); return Response::download($csvPath, $outputName)->deleteFileAfterSend(true);
$result = Response::make(File::get($csvPath), 200, $headers);
@unlink($csvPath);
return $result;
} }
/** /**

View File

@ -8,7 +8,18 @@ class ExampleExportModel extends ExportModel
{ {
public function exportData($columns, $sessionKey = null) public function exportData($columns, $sessionKey = null)
{ {
return []; return [
[
'foo' => 'bar',
'bar' => 'foo',
'foobar' => 'Hello World!',
],
[
'foo' => 'bar2',
'bar' => 'foo2',
'foobar' => 'Hello World2!',
],
];
} }
} }
@ -48,4 +59,36 @@ class ExportModelTest extends TestCase
$this->assertEquals('art direction-roman empire-sci\-fi', $result); $this->assertEquals('art direction-roman empire-sci\-fi', $result);
} }
public function testDownload()
{
$model = new ExampleExportModel;
$csvName = $model->export(['foo' => 'title', 'bar' => 'title2'], []);
$response = $model->download($csvName);
$request = new Illuminate\Http\Request();
$response->prepare($request);
$this->assertTrue($response->headers->has('Content-Type'), "Response is missing the Content-Type header!");
$this->assertTrue($response->headers->contains('Content-Type', 'text/plain'), "Content-Type is not \"text/plain\"!");
ob_start();
$response->send();
$output = ob_get_clean();
$utf8BOM = chr(239).chr(187).chr(191);
$this->assertEquals($utf8BOM."title,title2\nbar,foo\nbar2,foo2\n", $output, "CSV is not right!");
$filePath = temp_path($csvName);
$fileGotDeleted = !is_file($filePath);
$this->assertTrue($fileGotDeleted, "Export-CSV doesn't get deleted.");
if (!$fileGotDeleted) {
unlink($filePath);
}
}
} }