mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
bb20587f01
value of a related object. Useful for handling has_many/many_many data in a single CSV column.
90 lines
2.1 KiB
PHP
90 lines
2.1 KiB
PHP
<?php
|
|
class GridFieldExportButtonTest extends SapphireTest {
|
|
|
|
protected $list;
|
|
|
|
protected $gridField;
|
|
|
|
protected $form;
|
|
|
|
public static $fixture_file = 'GridFieldExportButtonTest.yml';
|
|
|
|
protected $extraDataObjects = array(
|
|
'GridFieldExportButtonTest_Team'
|
|
);
|
|
|
|
public function setUp() {
|
|
parent::setUp();
|
|
|
|
$this->list = new DataList('GridFieldExportButtonTest_Team');
|
|
$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 {
|
|
|
|
static $db = array(
|
|
'Name' => 'Varchar',
|
|
'City' => 'Varchar'
|
|
);
|
|
|
|
public function canView($member = null) {
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|