2012-04-19 23:48:14 +02:00
|
|
|
<?php
|
|
|
|
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(
|
|
|
|
'GridFieldExportButtonTest_Team'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->list = new DataList('GridFieldExportButtonTest_Team');
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateFileDataBasicFields() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array('Name' => 'My Name'));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"\"My Name\"\n\"Test\"\n\"Test2\"\n",
|
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateFileDataAnonymousFunctionField() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array(
|
|
|
|
'Name' => 'Name',
|
|
|
|
'City' => function($obj) {
|
|
|
|
return $obj->getValue() . ' city';
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"\"Name\",\"City\"\n\"Test\",\"City city\"\n\"Test2\",\"City2 city\"\n",
|
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuiltInFunctionNameCanBeUsedAsHeader() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array(
|
|
|
|
'Name' => 'Name',
|
|
|
|
'City' => 'strtolower'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"\"Name\",\"strtolower\"\n\"Test\",\"City\"\n\"Test2\",\"City2\"\n",
|
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNoCsvHeaders() {
|
|
|
|
$button = new GridFieldExportButton();
|
|
|
|
$button->setExportColumns(array(
|
|
|
|
'Name' => 'Name',
|
|
|
|
'City' => 'City'
|
|
|
|
));
|
|
|
|
$button->setCsvHasHeader(false);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"\"Test\",\"City\"\n\"Test2\",\"City2\"\n",
|
|
|
|
$button->generateExportFileData($this->gridField)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
class GridFieldExportButtonTest_Team extends DataObject implements TestOnly {
|
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-04-19 23:48:14 +02:00
|
|
|
'Name' => 'Varchar',
|
|
|
|
'City' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|