function testCsvExport() { $table = new TableListField("Tester", "TableListFieldTest_CsvExport", array("A" => "Col A", "B" => "Col B")); $form = new Form(new TableListFieldTest_TestController(), "TestForm", new FieldSet($table), new FieldSet()); $csvResponse = $table->export(); $csvOutput = $csvResponse->getBody(); $this->assertNotEquals($csvOutput, false); // Create a temporary file and write the CSV to it. $csvFileName = tempnam(TEMP_FOLDER, 'csv-export'); $csvFile = fopen($csvFileName, 'w'); fwrite($csvFile, $csvOutput); fclose($csvFile); $csvFile = fopen($csvFileName, 'r'); $csvRow = fgetcsv($csvFile); $this->assertEquals($csvRow, array('Col A', 'Col B')); $csvRow = fgetcsv($csvFile); $this->assertEquals($csvRow, array('"A field, with a comma"', 'A second field')); fclose($csvFile); unlink($csvFileName); }
function testCsvExport() { $table = new TableListField("Tester", "TableListFieldTest_CsvExport", array("A" => "Col A", "B" => "Col B")); $form = new Form(new TableListFieldTest_TestController(), "TestForm", new FieldSet($table), new FieldSet()); $csvResponse = $table->export(); $csvOutput = $csvResponse->getBody(); $this->assertNotEquals($csvOutput, false); // Create a temporary file and write the CSV to it. $csvFileName = tempnam(TEMP_FOLDER, 'csv-export'); $csvFile = fopen($csvFileName, 'wb'); fwrite($csvFile, $csvOutput); fclose($csvFile); $csvFile = fopen($csvFileName, 'rb'); $csvRow = fgetcsv($csvFile); $this->assertEquals($csvRow, array('Col A', 'Col B')); // fgetcsv doesn't handle escaped quotes in the string in PHP 5.2, so we're asserting the // raw string instead. $this->assertEquals('"\\"A field, with a comma\\"","A second field"', trim(fgets($csvFile))); fclose($csvFile); unlink($csvFileName); }