2012-04-19 23:48:14 +02:00
|
|
|
<?php
|
2015-02-03 04:21:37 +01:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests\GridField;
|
|
|
|
|
2018-02-21 21:22:37 +01:00
|
|
|
use League\Csv\Reader;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldExportButtonTest\NoView;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldExportButtonTest\Team;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator;
|
2017-11-29 04:07:30 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class GridFieldExportButtonTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
2017-11-29 04:07:30 +01:00
|
|
|
/**
|
|
|
|
* @var DataList
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $list;
|
|
|
|
|
2017-11-29 04:07:30 +01:00
|
|
|
/**
|
|
|
|
* @var GridField
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
protected static $fixture_file = 'GridFieldExportButtonTest.yml';
|
|
|
|
|
2017-11-29 04:07:30 +01:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
Team::class,
|
2017-03-24 12:17:26 +01:00
|
|
|
NoView::class,
|
2017-11-29 04:07:30 +01:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->list = new DataList(Team::class);
|
|
|
|
$this->list = $this->list->sort('Name');
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
|
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCanView()
|
|
|
|
{
|
|
|
|
$list = new DataList(NoView::class);
|
|
|
|
|
|
|
|
$button = new GridFieldExportButton();
|
2017-11-29 04:07:30 +01:00
|
|
|
$button->setExportColumns(['Name' => 'My Name']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($button->generateExportFileData($gridField));
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
"$bom\"My Name\"\r\n",
|
|
|
|
(string) $csvReader
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateFileDataBasicFields()
|
|
|
|
{
|
|
|
|
$button = new GridFieldExportButton();
|
2017-11-29 04:07:30 +01:00
|
|
|
$button->setExportColumns(['Name' => 'My Name']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($button->generateExportFileData($this->gridField));
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom . '"My Name"' . "\r\n" . 'Test' . "\r\n" . 'Test2' . "\r\n",
|
|
|
|
(string) $csvReader
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-29 04:07:30 +01:00
|
|
|
public function testXLSSanitisation()
|
|
|
|
{
|
|
|
|
// Create risky object
|
|
|
|
$object = new Team();
|
|
|
|
$object->Name = '=SUM(1, 2)';
|
|
|
|
$object->write();
|
|
|
|
|
|
|
|
// Export
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(['Name' => 'My Name']);
|
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($button->generateExportFileData($this->gridField));
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2017-11-29 04:07:30 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
"$bom\"My Name\"\r\n\"\t=SUM(1, 2)\"\r\nTest\r\nTest2\r\n",
|
|
|
|
(string) $csvReader
|
2017-11-29 04:07:30 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGenerateFileDataAnonymousFunctionField()
|
|
|
|
{
|
|
|
|
$button = new GridFieldExportButton();
|
2017-11-29 04:07:30 +01:00
|
|
|
$button->setExportColumns([
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Name',
|
2017-11-29 04:07:30 +01:00
|
|
|
'City' => function (DBField $obj) {
|
2016-12-16 05:34:21 +01:00
|
|
|
return $obj->getValue() . ' city';
|
|
|
|
}
|
2017-11-29 04:07:30 +01:00
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($button->generateExportFileData($this->gridField));
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom . 'Name,City' . "\r\n" . 'Test,"City city"' . "\r\n" . 'Test2,"Quoted ""City"" 2 city"' . "\r\n",
|
|
|
|
(string) $csvReader
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuiltInFunctionNameCanBeUsedAsHeader()
|
|
|
|
{
|
|
|
|
$button = new GridFieldExportButton();
|
2017-11-29 04:07:30 +01:00
|
|
|
$button->setExportColumns([
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Name',
|
2017-11-29 04:07:30 +01:00
|
|
|
'City' => 'strtolower',
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($button->generateExportFileData($this->gridField));
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom . 'Name,strtolower' . "\r\n" . 'Test,City' . "\r\n" . 'Test2,"Quoted ""City"" 2"' . "\r\n",
|
|
|
|
(string) $csvReader
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoCsvHeaders()
|
|
|
|
{
|
|
|
|
$button = new GridFieldExportButton();
|
2017-11-29 04:07:30 +01:00
|
|
|
$button->setExportColumns([
|
2016-12-16 05:34:21 +01:00
|
|
|
'Name' => 'Name',
|
2017-11-29 04:07:30 +01:00
|
|
|
'City' => 'City',
|
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$button->setCsvHasHeader(false);
|
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($button->generateExportFileData($this->gridField));
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom . 'Test,City' . "\r\n" . 'Test2,"Quoted ""City"" 2"' . "\r\n",
|
|
|
|
(string) $csvReader
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayListInput()
|
|
|
|
{
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$this->gridField->getConfig()->addComponent(new GridFieldPaginator());
|
|
|
|
|
|
|
|
//Create an ArrayList 1 greater the Paginator's default 15 rows
|
|
|
|
$arrayList = new ArrayList();
|
|
|
|
for ($i = 1; $i <= 16; $i++) {
|
2017-11-29 04:07:30 +01:00
|
|
|
$dataobject = new DataObject(['ID' => $i]);
|
2016-12-16 05:34:21 +01:00
|
|
|
$arrayList->add($dataobject);
|
|
|
|
}
|
|
|
|
$this->gridField->setList($arrayList);
|
|
|
|
|
2018-02-21 21:22:37 +01:00
|
|
|
$exportData = $button->generateExportFileData($this->gridField);
|
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($exportData);
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
$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
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testZeroValue()
|
|
|
|
{
|
|
|
|
$button = new GridFieldExportButton();
|
2017-11-29 04:07:30 +01:00
|
|
|
$button->setExportColumns([
|
2016-12-16 05:34:21 +01:00
|
|
|
'RugbyTeamNumber' => 'Rugby Team Number'
|
2017-11-29 04:07:30 +01:00
|
|
|
]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2020-08-30 06:01:27 +02:00
|
|
|
$csvReader = $this->createReader($button->generateExportFileData($this->gridField));
|
2018-02-21 21:22:37 +01:00
|
|
|
$bom = $csvReader->getInputBOM();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(
|
2018-02-21 21:22:37 +01:00
|
|
|
"$bom\"Rugby Team Number\"\r\n2\r\n0\r\n",
|
|
|
|
(string) $csvReader
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2020-08-30 06:01:27 +02:00
|
|
|
|
|
|
|
protected function createReader($string)
|
|
|
|
{
|
|
|
|
$reader = Reader::createFromString($string);
|
|
|
|
|
|
|
|
// Explicitly set the output BOM in league/csv 9
|
|
|
|
if (method_exists($reader, 'getContent')) {
|
|
|
|
$reader->setOutputBOM(Reader::BOM_UTF8);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $reader;
|
|
|
|
}
|
2012-04-19 23:48:14 +02:00
|
|
|
}
|