mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
MINOR Added tests for GridField with minor changes to support them
MINOR Added docblocks
This commit is contained in:
parent
ce795b6d8a
commit
ea4b9fe939
67
forms/gridfield/GridField.php
Normal file → Executable file
67
forms/gridfield/GridField.php
Normal file → Executable file
@ -90,8 +90,8 @@ class GridField extends FormField {
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
$this->config->addComponent(new GridState_Component());
|
||||
$this->setComponents($this->config);
|
||||
$this->components[] = new GridState_Component();
|
||||
$this->state = new GridState($this);
|
||||
|
||||
|
||||
@ -176,7 +176,7 @@ class GridField extends FormField {
|
||||
*
|
||||
* @param array $fields
|
||||
*/
|
||||
public function setDisplayFields(array $fields) {
|
||||
public function setDisplayFields($fields) {
|
||||
if(!is_array($fields)) {
|
||||
throw new InvalidArgumentException('Arguments passed to GridField::setDisplayFields() must be an array');
|
||||
}
|
||||
@ -185,7 +185,11 @@ class GridField extends FormField {
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify castings with fieldname as the key, and the desired casting as value.
|
||||
* Example: array("MyCustomDate"=>"Date","MyShortText"=>"Text->FirstSentence")
|
||||
*
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function setFieldCasting($casting) {
|
||||
$this->fieldCasting = $casting;
|
||||
@ -193,7 +197,12 @@ class GridField extends FormField {
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify custom formatting for fields, e.g. to render a link instead of pure text.
|
||||
* Caution: Make sure to escape special php-characters like in a normal php-statement.
|
||||
* Example: "myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'
|
||||
*
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function getFieldCasting() {
|
||||
return $this->fieldCasting;
|
||||
@ -201,6 +210,7 @@ class GridField extends FormField {
|
||||
|
||||
/**
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function setFieldFormatting($formatting) {
|
||||
$this->fieldFormatting = $formatting;
|
||||
@ -209,16 +219,18 @@ class GridField extends FormField {
|
||||
|
||||
/**
|
||||
* @param array $casting
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function getFieldFormatting() {
|
||||
return $this->fieldFormatting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Taken from TablelistField
|
||||
*
|
||||
* @param $value
|
||||
* Cast a arbitrary value with the help of a castingDefintion
|
||||
*
|
||||
* @param $value
|
||||
* @param $castingDefinition
|
||||
* @todo refactor this into GridFieldComponent
|
||||
*/
|
||||
public function getCastedValue($value, $castingDefinition) {
|
||||
if(is_array($castingDefinition)) {
|
||||
@ -266,12 +278,10 @@ class GridField extends FormField {
|
||||
/**
|
||||
* Get the current GridState
|
||||
*
|
||||
* @return GridState
|
||||
* @param bool $getData - flag for returning the GridState_Data or the GridState
|
||||
* @return GridState_data|GridState
|
||||
*/
|
||||
public function getState($getData=true) {
|
||||
if(!$this->state) {
|
||||
throw new LogicException('State has not been defined');
|
||||
}
|
||||
if($getData) {
|
||||
return $this->state->getData();
|
||||
}
|
||||
@ -357,7 +367,9 @@ class GridField extends FormField {
|
||||
|
||||
public function getColumnContent($record, $column) {
|
||||
// Build the column dispatch
|
||||
if(!$this->columnDispatch) $this->buildColumnDispatch();
|
||||
if(!$this->columnDispatch) {
|
||||
$this->buildColumnDispatch();
|
||||
}
|
||||
|
||||
$handler = $this->columnDispatch[$column];
|
||||
if($handler) {
|
||||
@ -369,14 +381,20 @@ class GridField extends FormField {
|
||||
|
||||
public function getColumnAttributes($record, $column) {
|
||||
// Build the column dispatch
|
||||
if(!$this->columnDispatch) $this->buildColumnDispatch();
|
||||
if(!$this->columnDispatch) {
|
||||
$this->buildColumnDispatch();
|
||||
}
|
||||
|
||||
$handler = $this->columnDispatch[$column];
|
||||
if($handler) {
|
||||
$attrs = $handler->getColumnAttributes($this, $record, $column);
|
||||
if(is_array($attrs)) return $attrs;
|
||||
else if($attrs) throw new LogicException("Non-array response from " . get_class($handler) . "::getColumnAttributes()");
|
||||
else return array();
|
||||
if(is_array($attrs)) {
|
||||
return $attrs;
|
||||
} elseif($attrs) {
|
||||
throw new LogicException("Non-array response from " . get_class($handler) . "::getColumnAttributes()");
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
} else {
|
||||
throw new InvalidArgumentException("Bad column '$column'");
|
||||
}
|
||||
@ -384,26 +402,28 @@ class GridField extends FormField {
|
||||
|
||||
public function getColumnMetadata($column) {
|
||||
// Build the column dispatch
|
||||
if(!$this->columnDispatch) $this->buildColumnDispatch();
|
||||
if(!$this->columnDispatch) {
|
||||
$this->buildColumnDispatch();
|
||||
}
|
||||
|
||||
$handler = $this->columnDispatch[$column];
|
||||
if($handler) {
|
||||
$metadata = $handler->getColumnMetadata($this, $column);
|
||||
if(is_array($metadata)) return $metadata;
|
||||
else if($metadata) throw new LogicException("Non-array response from " . get_class($handler) . "::getColumnMetadata()");
|
||||
else return array();
|
||||
} else {
|
||||
throw new InvalidArgumentException("Bad column '$column'");
|
||||
$metadata = $handler->getColumnMetadata($this, $column);
|
||||
if(is_array($metadata)) {
|
||||
return $metadata;
|
||||
} elseif($metadata) {
|
||||
throw new LogicException("Non-array response from " . get_class($handler) . "::getColumnMetadata()");
|
||||
}
|
||||
}
|
||||
throw new InvalidArgumentException("Bad column '$column'");
|
||||
}
|
||||
|
||||
public function getColumnCount() {
|
||||
// Build the column dispatch
|
||||
if(!$this->columnDispatch) $this->buildColumnDispatch();
|
||||
|
||||
return count($this->columnDispatch);
|
||||
|
||||
}
|
||||
|
||||
protected function buildColumnDispatch() {
|
||||
$this->columnDispatch = array();
|
||||
foreach($this->components as $item) {
|
||||
@ -449,7 +469,6 @@ class GridField extends FormField {
|
||||
return $form->forTemplate();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function handleAction($actionName, $args, $data) {
|
||||
|
359
tests/forms/GridFieldTest.php
Normal file
359
tests/forms/GridFieldTest.php
Normal file
@ -0,0 +1,359 @@
|
||||
<?php
|
||||
class GridFieldtest extends SapphireTest {
|
||||
|
||||
/**
|
||||
* @covers GridField::__construct
|
||||
* @covers GridFIeld::requireDefaultCSS
|
||||
*/
|
||||
public function testGridField() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$this->assertTrue($obj instanceof GridField, 'Test that the constructor arguments are valid');
|
||||
$css = Requirements::backend()->get_css();
|
||||
$this->assertTrue(isset($css['sapphire/css/GridField.css']), 'GridField.css should have been aquired');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::__construct
|
||||
* @covers GridField::getList
|
||||
*/
|
||||
public function testGridFieldSetList() {
|
||||
$list = ArrayList::create(array(1=>'hello', 2=>'goodbye'));
|
||||
$obj = new GridField('testfield', 'testfield', $list);
|
||||
$this->assertEquals($list, $obj->getList(), 'Testing getList');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::__construct
|
||||
* @covers GridField::getConfig
|
||||
* @covers GridField::setComponents
|
||||
* @covers GridField::getDefaultConfig
|
||||
*/
|
||||
public function testGridFieldDefaultConfig() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
|
||||
$expectedComponents = new ArrayList(array(
|
||||
0 => new GridFieldSortableHeader,
|
||||
1 => new GridFieldFilter,
|
||||
2 => new GridFieldDefaultColumns,
|
||||
3 => new GridFieldPaginator,
|
||||
4 => new GridState_Component,
|
||||
));
|
||||
|
||||
$this->assertEquals($expectedComponents, $obj->getConfig()->getComponents(), 'Testing default Config');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::__construct
|
||||
* @covers GridField::setComponents
|
||||
*/
|
||||
public function testGridFieldSetCustomConfig() {
|
||||
|
||||
$config = GridFieldConfig::create();
|
||||
$config->addComponent(new GridFieldSortableHeader());
|
||||
$config->addComponent(new GridFieldDefaultColumns());
|
||||
|
||||
$obj = new GridField('testfield', 'testfield', ArrayList::create(array()),$config);
|
||||
|
||||
$expectedComponents = new ArrayList(array(
|
||||
0 => new GridFieldSortableHeader,
|
||||
1 => new GridFieldDefaultColumns,
|
||||
2 => new GridState_Component,
|
||||
));
|
||||
|
||||
$this->assertEquals($expectedComponents, $obj->getConfig()->getComponents(), 'Testing default Config');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getModelClass
|
||||
* @covers GridField::setModelClass
|
||||
*/
|
||||
public function testGridFieldModelClass() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$this->assertEquals('Member', $obj->getModelClass(), 'Should return Member');
|
||||
$obj->setModelClass('DataModel');
|
||||
$this->assertEquals('DataModel', $obj->getModelClass(), 'Should return Member');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getModelClass
|
||||
*/
|
||||
public function testGridFieldModelClassThrowsException() {
|
||||
$this->setExpectedException('LogicException');
|
||||
$obj = new GridField('testfield', 'testfield', ArrayList::create());
|
||||
$obj->getModelClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldGetDefaultDisplayFields() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$expected = array(
|
||||
'FirstName' => 'First Name',
|
||||
'Surname' => 'Last Name',
|
||||
'Email' => 'Email',
|
||||
);
|
||||
$this->assertEquals($expected, $obj->getDisplayFields());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::setDisplayFields
|
||||
* @covers GridField::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldCustomDisplayFields() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$expected = array('Email' => 'Email');
|
||||
$obj->setDisplayFields($expected);
|
||||
$this->assertEquals($expected, $obj->getDisplayFields());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::setDisplayFields
|
||||
* @covers GridField::getDisplayFields
|
||||
*/
|
||||
public function testGridFieldDisplayFieldsWithBadArguments() {
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$obj->setDisplayFields(new stdClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::setList
|
||||
* @covers GridField::getList
|
||||
*/
|
||||
public function testSetAndGetList() {
|
||||
$list = DataList::create('Member');
|
||||
$arrayList = ArrayList::create(array(1,2,3));
|
||||
$obj = new GridField('testfield', 'testfield', $list);
|
||||
$this->assertEquals($list, $obj->getList());
|
||||
$obj->setList($arrayList);
|
||||
$this->assertEquals($arrayList, $obj->getList());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getState
|
||||
*/
|
||||
public function testGetState() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$this->assertTrue($obj->getState() instanceof GridState_Data);
|
||||
$this->assertTrue($obj->getState(false) instanceof GridState);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumns
|
||||
*/
|
||||
public function testGetColumns(){
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$expected = array (
|
||||
0 => 'FirstName',
|
||||
1 => 'Surname',
|
||||
2 => 'Email',
|
||||
);
|
||||
$this->assertEquals($expected, $obj->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnCount
|
||||
*/
|
||||
public function testGetColumnCount() {
|
||||
$obj = new GridField('testfield', 'testfield', DataList::create('Member'));
|
||||
$this->assertEquals(3, $obj->getColumnCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnContent
|
||||
*/
|
||||
public function testGetColumnContent() {
|
||||
$list = DataList::create('Member');
|
||||
$obj = new GridField('testfield', 'testfield', $list);
|
||||
$this->assertEquals('ADMIN@example.org', $obj->getColumnContent($list->first(), 'Email'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnContent
|
||||
*/
|
||||
public function testGetColumnContentBadArguments() {
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$list = DataList::create('Member');
|
||||
$obj = new GridField('testfield', 'testfield', $list);
|
||||
$obj->getColumnContent($list->first(), 'non-existing');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnAttributes
|
||||
*/
|
||||
public function testGetColumnAttributesEmptyArray() {
|
||||
$list = DataList::create('Member');
|
||||
$obj = new GridField('testfield', 'testfield', $list);
|
||||
$this->assertEquals(array(), $obj->getColumnAttributes($list->first(), 'Email'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnAttributes
|
||||
*/
|
||||
public function testGetColumnAttributes() {
|
||||
$list = DataList::create('Member');
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', $list, $config);
|
||||
$this->assertEquals(array('class'=>'css-class'), $obj->getColumnAttributes($list->first(), 'Email'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnAttributes
|
||||
*/
|
||||
public function testGetColumnAttributesBadArguments() {
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$list = DataList::create('Member');
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', $list, $config);
|
||||
$obj->getColumnAttributes($list->first(), 'Non-existing');
|
||||
}
|
||||
|
||||
public function testGetColumnAttributesBadResponseFromComponent() {
|
||||
$this->setExpectedException('LogicException');
|
||||
$list = DataList::create('Member');
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', $list, $config);
|
||||
$obj->getColumnAttributes($list->first(), 'Surname');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnMetadata
|
||||
*/
|
||||
public function testGetColumnMetadata() {
|
||||
$list = DataList::create('Member');
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', $list, $config);
|
||||
$this->assertEquals(array('metadata'=>'istrue'), $obj->getColumnMetadata('Email'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnMetadata
|
||||
*/
|
||||
public function testGetColumnMetadataBadResponseFromComponent() {
|
||||
$this->setExpectedException('LogicException');
|
||||
$list = DataList::create('Member');
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', $list, $config);
|
||||
$obj->getColumnMetadata('Surname');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getColumnMetadata
|
||||
*/
|
||||
public function testGetColumnMetadataBadArguments() {
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$list = ArrayList::create();
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', $list, $config);
|
||||
$obj->getColumnMetadata('non-exist-qweqweqwe');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::handleAction
|
||||
*/
|
||||
public function testHandleActionBadArgument() {
|
||||
$this->setExpectedException('InvalidArgumentException');
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$obj->handleAction('prft', array(), array());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::handleAction
|
||||
*/
|
||||
public function testHandleAction() {
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', ArrayList::create(), $config);
|
||||
$this->assertEquals('handledAction is executed', $obj->handleAction('jump', array(), array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getFieldCasting
|
||||
* @covers GridField::setFieldCasting
|
||||
*/
|
||||
public function testFieldCasting() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$this->assertEquals(array(), $obj->getFieldCasting());
|
||||
$obj->setFieldCasting(array("MyShortText"=>"Text->FirstSentence"));
|
||||
$this->assertEquals(array("MyShortText"=>"Text->FirstSentence"), $obj->getFieldCasting());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getFieldFormatting
|
||||
* @covers GridField::setFieldFormatting
|
||||
*/
|
||||
public function testFieldFormatting() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$this->assertEquals(array(), $obj->getFieldFormatting());
|
||||
$obj->setFieldFormatting(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'));
|
||||
$this->assertEquals(array("myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'), $obj->getFieldFormatting());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getCastedValue
|
||||
*/
|
||||
public function testGetCastedValue() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$value = $obj->getCastedValue('This is a sentance. This ia another.', array('Text->FirstSentence'));
|
||||
$this->assertEquals('This is a sentance.', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::getCastedValue
|
||||
*/
|
||||
public function testGetCastedValueObject() {
|
||||
$obj = new GridField('testfield', 'testfield');
|
||||
$value = $obj->getCastedValue('This is a sentance. This ia another.', 'Date');
|
||||
$this->assertEquals('1970-01-01', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers GridField::gridFieldAlterAction
|
||||
*/
|
||||
public function testGridFieldAlterAction() {
|
||||
$config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
|
||||
$obj = new GridField('testfield', 'testfield', ArrayList::create(), $config);
|
||||
$id = 'testGridStateActionField';
|
||||
Session::set($id, array('grid'=>'', 'actionName'=>'jump'));
|
||||
$form = new Form($this, 'mockform', new FieldList(array($obj)), new FieldList());
|
||||
$request = new SS_HTTPRequest('POST', 'url');
|
||||
$obj->gridFieldAlterAction(array('StateID'=>$id), $form, $request);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class GridFieldTest_Component implements GridField_ColumnProvider, GridField_ActionProvider, TestOnly{
|
||||
|
||||
public function augmentColumns($gridField, &$columns) {}
|
||||
|
||||
public function getColumnContent($gridField, $record, $columnName) {}
|
||||
|
||||
public function getColumnAttributes($gridField, $record, $columnName) {
|
||||
if($columnName=='Surname'){
|
||||
return 'shouldnotbestring';
|
||||
}
|
||||
return array('class'=>'css-class');
|
||||
}
|
||||
|
||||
public function getColumnMetadata($gridField, $columnName) {
|
||||
if($columnName=='Surname'){
|
||||
return 'shouldnotbestring';
|
||||
} elseif( $columnName == 'FirstName') {
|
||||
return array();
|
||||
}
|
||||
return array('metadata'=>'istrue');
|
||||
}
|
||||
public function getColumnsHandled($gridField) {
|
||||
return array('Email', 'Surname', 'FirstName');
|
||||
}
|
||||
|
||||
public function getActions($gridField) {
|
||||
return array('jump');
|
||||
}
|
||||
|
||||
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
||||
return 'handledAction is executed';
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user