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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-04-19 23:48:14 +02:00
|
|
|
class GridFieldExportButtonTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected $list;
|
|
|
|
|
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
protected $form;
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'GridFieldExportButtonTest.yml';
|
2012-04-19 23:48:14 +02:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
2016-10-14 03:30:05 +02:00
|
|
|
Team::class,
|
|
|
|
NoView::class
|
2012-04-19 23:48:14 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->list = new DataList(Team::class);
|
2012-06-15 06:08:54 +02:00
|
|
|
$this->list = $this->list->sort('Name');
|
2012-04-19 23:48:14 +02:00
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
|
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
|
|
|
}
|
|
|
|
|
2015-02-03 04:21:37 +01:00
|
|
|
public function testCanView() {
|
2016-10-14 03:30:05 +02:00
|
|
|
$list = new DataList(NoView::class);
|
2015-02-03 04:21:37 +01:00
|
|
|
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array('Name' => 'My Name'));
|
|
|
|
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"\"My Name\"\n",
|
|
|
|
$button->generateExportFileData($gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-19 23:48:14 +02:00
|
|
|
public function testGenerateFileDataBasicFields() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array('Name' => 'My Name'));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-07-27 03:38:11 +02:00
|
|
|
'"My Name"'."\n".
|
|
|
|
'Test'."\n".
|
|
|
|
'Test2'."\n",
|
2012-04-19 23:48:14 +02:00
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateFileDataAnonymousFunctionField() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array(
|
|
|
|
'Name' => 'Name',
|
|
|
|
'City' => function($obj) {
|
|
|
|
return $obj->getValue() . ' city';
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-07-27 03:38:11 +02:00
|
|
|
'Name,City'."\n".
|
|
|
|
'Test,"City city"'."\n".
|
|
|
|
'Test2,"Quoted ""City"" 2 city"'."\n",
|
2012-04-19 23:48:14 +02:00
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuiltInFunctionNameCanBeUsedAsHeader() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array(
|
|
|
|
'Name' => 'Name',
|
|
|
|
'City' => 'strtolower'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-07-27 03:38:11 +02:00
|
|
|
'Name,strtolower'."\n".
|
|
|
|
'Test,City'."\n".
|
|
|
|
'Test2,"Quoted ""City"" 2"'."\n",
|
2012-04-19 23:48:14 +02:00
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoCsvHeaders() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array(
|
|
|
|
'Name' => 'Name',
|
|
|
|
'City' => 'City'
|
|
|
|
));
|
|
|
|
$button->setCsvHasHeader(false);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2015-07-27 03:38:11 +02:00
|
|
|
'Test,City'."\n".
|
|
|
|
'Test2,"Quoted ""City"" 2"'."\n",
|
2012-04-19 23:48:14 +02:00
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-10-17 05:22:30 +02:00
|
|
|
public function testArrayListInput() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$this->gridField->getConfig()->addComponent(new GridFieldPaginator());
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-10-17 05:22:30 +02:00
|
|
|
//Create an ArrayList 1 greater the Paginator's default 15 rows
|
|
|
|
$arrayList = new ArrayList();
|
|
|
|
for ($i = 1; $i <= 16; $i++) {
|
2016-01-06 00:34:58 +01:00
|
|
|
$dataobject = new DataObject(
|
2014-10-17 05:22:30 +02:00
|
|
|
array ( 'ID' => $i )
|
|
|
|
);
|
|
|
|
$arrayList->add($dataobject);
|
|
|
|
}
|
|
|
|
$this->gridField->setList($arrayList);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-10-17 05:22:30 +02:00
|
|
|
$this->assertEquals(
|
2015-07-27 03:38:11 +02:00
|
|
|
"ID\n".
|
|
|
|
"1\n".
|
|
|
|
"2\n".
|
|
|
|
"3\n".
|
|
|
|
"4\n".
|
|
|
|
"5\n".
|
|
|
|
"6\n".
|
|
|
|
"7\n".
|
|
|
|
"8\n".
|
|
|
|
"9\n".
|
|
|
|
"10\n".
|
|
|
|
"11\n".
|
|
|
|
"12\n".
|
|
|
|
"13\n".
|
|
|
|
"14\n".
|
|
|
|
"15\n".
|
|
|
|
"16\n",
|
2014-10-17 05:22:30 +02:00
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
2016-05-16 01:38:15 +02:00
|
|
|
|
|
|
|
public function testZeroValue() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array(
|
|
|
|
'RugbyTeamNumber' => 'Rugby Team Number'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2016-05-18 08:36:10 +02:00
|
|
|
"\"Rugby Team Number\"\n2\n0\n",
|
2016-05-16 01:38:15 +02:00
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
2012-04-19 23:48:14 +02:00
|
|
|
}
|
2015-02-03 04:21:37 +01:00
|
|
|
|