Skip to content

Commit

Permalink
Merge pull request #11508 from creative-commoners/pulls/6.0/fix-unit-…
Browse files Browse the repository at this point in the history
…test

ENH Use updated league/csv api
  • Loading branch information
GuySartorelli authored Dec 12, 2024
2 parents 4cdd203 + d624e1b commit d3be004
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
11 changes: 4 additions & 7 deletions src/Forms/GridField/GridFieldExportButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Forms\GridField;

use League\Csv\Bom;
use League\Csv\Writer;
use LogicException;
use SilverStripe\Control\HTTPRequest;
Expand Down Expand Up @@ -180,8 +181,8 @@ public function generateExportFileData($gridField)
$csvWriter = Writer::createFromFileObject(new \SplTempFileObject());
$csvWriter->setDelimiter($this->getCsvSeparator());
$csvWriter->setEnclosure($this->getCsvEnclosure());
$csvWriter->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries
$csvWriter->setOutputBOM(Writer::BOM_UTF8);
$csvWriter->setEndOfLine("\r\n"); //use windows line endings for compatibility with some csv libraries
$csvWriter->setOutputBOM(Bom::Utf8);

if (!Config::inst()->get(get_class($this), 'xls_export_disabled')) {
$csvWriter->addFormatter(function (array $row) {
Expand Down Expand Up @@ -269,11 +270,7 @@ public function generateExportFileData($gridField)
}
}

if (method_exists($csvWriter, 'getContent')) {
return $csvWriter->getContent();
}

return (string)$csvWriter;
return $csvWriter->toString();
}

/**
Expand Down
19 changes: 10 additions & 9 deletions tests/php/Forms/GridField/GridFieldExportButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Forms\Tests\GridField;

use League\Csv\Bom;
use League\Csv\Reader;
use LogicException;
use ReflectionMethod;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function testCanView()

$this->assertEquals(
"$bom\"My Name\"\r\n",
(string) $csvReader
(string) $csvReader->toString()
);
}

Expand All @@ -78,7 +79,7 @@ public function testGenerateFileDataBasicFields()

$this->assertEquals(
$bom . '"My Name"' . "\r\n" . 'Test' . "\r\n" . 'Test2' . "\r\n",
(string) $csvReader
$csvReader->toString()
);
}

Expand All @@ -98,7 +99,7 @@ public function testXLSSanitisation()

$this->assertEquals(
"$bom\"My Name\"\r\n\"\t=SUM(1, 2)\"\r\nTest\r\nTest2\r\n",
(string) $csvReader
$csvReader->toString()
);
}

Expand All @@ -117,7 +118,7 @@ public function testGenerateFileDataAnonymousFunctionField()

$this->assertEquals(
$bom . 'Name,City' . "\r\n" . 'Test,"City city"' . "\r\n" . 'Test2,"Quoted ""City"" 2 city"' . "\r\n",
(string) $csvReader
$csvReader->toString()
);
}

Expand All @@ -134,7 +135,7 @@ public function testBuiltInFunctionNameCanBeUsedAsHeader()

$this->assertEquals(
$bom . 'Name,strtolower' . "\r\n" . 'Test,City' . "\r\n" . 'Test2,"Quoted ""City"" 2"' . "\r\n",
(string) $csvReader
$csvReader->toString()
);
}

Expand All @@ -152,7 +153,7 @@ public function testNoCsvHeaders()

$this->assertEquals(
$bom . 'Test,City' . "\r\n" . 'Test2,"Quoted ""City"" 2"' . "\r\n",
(string) $csvReader
$csvReader->toString()
);
}

Expand All @@ -179,7 +180,7 @@ public function testArrayListInput()

$this->assertEquals(
$bom . "ID\r\n" . "1\r\n" . "2\r\n" . "3\r\n" . "4\r\n" . "5\r\n" . "6\r\n" . "7\r\n" . "8\r\n" . "9\r\n" . "10\r\n" . "11\r\n" . "12\r\n" . "13\r\n" . "14\r\n" . "15\r\n" . "16\r\n",
(string) $csvReader
$csvReader->toString()
);
}

Expand All @@ -195,7 +196,7 @@ public function testZeroValue()

$this->assertEquals(
"$bom\"Rugby Team Number\"\r\n2\r\n0\r\n",
(string) $csvReader
$csvReader->toString()
);
}

Expand Down Expand Up @@ -224,7 +225,7 @@ protected function createReader($string)

// Explicitly set the output BOM in league/csv 9
if (method_exists($reader, 'getContent')) {
$reader->setOutputBOM(Reader::BOM_UTF8);
$reader->setOutputBOM(Bom::Utf8);
}

return $reader;
Expand Down

0 comments on commit d3be004

Please sign in to comment.