mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FEATURE Added initial commit of Datagrid with tests
This commit is contained in:
parent
75c0fcd052
commit
85cdb3771c
68
forms/Datagrid.php
Normal file
68
forms/Datagrid.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Description of Datagrid
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class Datagrid extends FormField {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var SS_list
|
||||||
|
*/
|
||||||
|
protected $datasource = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new field.
|
||||||
|
* @param name The internal field name, passed to forms.
|
||||||
|
* @param title The field label.
|
||||||
|
* @param value The value of the field.
|
||||||
|
* @param form Reference to the container form
|
||||||
|
* @param maxLength The Maximum length of the attribute
|
||||||
|
*/
|
||||||
|
function __construct($name, $title = null, SS_list $source = null, $form = null) {
|
||||||
|
parent::__construct($name, $title, null, $form);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the datasource
|
||||||
|
*
|
||||||
|
* @param SS_List $datasource
|
||||||
|
*/
|
||||||
|
public function setDatasource(SS_List $datasource ) {
|
||||||
|
$this->datasource = $datasource;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the datasource
|
||||||
|
*
|
||||||
|
* @return SS_list
|
||||||
|
*/
|
||||||
|
public function getDatasource() {
|
||||||
|
return $this->datasource;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the headers or column names for this grid
|
||||||
|
*
|
||||||
|
* The returning array will have the format of
|
||||||
|
* array(
|
||||||
|
* 'FirstName' => 'First name',
|
||||||
|
* 'Description' => 'A nice description'
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getHeaders() {
|
||||||
|
|
||||||
|
if($this->datasource instanceof DataList ) {
|
||||||
|
return singleton($this->datasource->dataClass)->summaryFields();
|
||||||
|
} else {
|
||||||
|
$firstItem = $this->datasource->first();
|
||||||
|
if(!$firstItem) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
return array_combine(array_keys($firstItem),array_keys($firstItem));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
tests/forms/DatagridFunctionalTest.php
Normal file
26
tests/forms/DatagridFunctionalTest.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a functional test for DatagridFunctionalTest
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class DatagridFunctionalTest extends FunctionalTest {
|
||||||
|
|
||||||
|
public function testGetInstance() {
|
||||||
|
$this->assertTrue(new Datagrid('testgrid') instanceof Datagrid, 'Trying to find an instance of Datagrid.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddToForm() {
|
||||||
|
$response = $this->get("DatagridFunctionalTest_Controller/");
|
||||||
|
$this->assertContains("form", $response->getBody());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DatagridFunctionalTest_Controller extends ContentController {
|
||||||
|
|
||||||
|
public function index() {
|
||||||
|
$grid = new Datagrid('testgrid');
|
||||||
|
$form = new Form($this, 'gridform', new FieldList($grid), new FieldList(new FormAction('rerender', 'rerender')));
|
||||||
|
return array('Form'=>$form);
|
||||||
|
}
|
||||||
|
}
|
66
tests/forms/DatagridTest.php
Normal file
66
tests/forms/DatagridTest.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a Unittest class for DatagridTest
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class DatagridTest extends SapphireTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
static $fixture_file = 'sapphire/tests/forms/DatagridTest.yml';
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $extraDataObjects = array(
|
||||||
|
'DatagridTest_Person',
|
||||||
|
);
|
||||||
|
|
||||||
|
public function testGetInstance() {
|
||||||
|
$this->assertTrue(new Datagrid('Testgrid') instanceof FormField, 'Datagrid should be a FormField');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSetDataSource() {
|
||||||
|
$grid = new Datagrid('Testgrid');
|
||||||
|
$source = new ArrayList();
|
||||||
|
$grid->setDatasource( $source );
|
||||||
|
$this->assertEquals($source, $grid->getDatasource());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDefaultHeadersFromEmptyArrayList() {
|
||||||
|
$grid = new Datagrid('Testgrid');
|
||||||
|
$source = new ArrayList();
|
||||||
|
$grid->setDatasource($source);
|
||||||
|
$this->assertEquals(array(), $grid->getHeaders());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDefaultHeadersFromArrayList() {
|
||||||
|
$grid = new Datagrid('Testgrid');
|
||||||
|
$source = new ArrayList(array(array('ID'=>1,'Name'=>'Aaron Aardwark')));
|
||||||
|
$grid->setDatasource($source);
|
||||||
|
$this->assertEquals(array('ID'=>'ID','Name'=>'Name'), $grid->getHeaders());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetDefaultHeadersFromDataList() {
|
||||||
|
$grid = new Datagrid('Testgrid');
|
||||||
|
$source = new DataList('DatagridTest_Person');
|
||||||
|
$grid->setDatasource($source);
|
||||||
|
$this->assertEquals(array('Name'=>'Name','ID'=>'ID'), $grid->getHeaders());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DatagridTest_Person extends Dataobject implements TestOnly {
|
||||||
|
|
||||||
|
public static $db = array(
|
||||||
|
'Name' => 'Varchar'
|
||||||
|
);
|
||||||
|
|
||||||
|
public static $summary_fields = array(
|
||||||
|
'Name',
|
||||||
|
'ID'
|
||||||
|
);
|
||||||
|
}
|
3
tests/forms/DatagridTest.yml
Normal file
3
tests/forms/DatagridTest.yml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
DatagridTest_Person:
|
||||||
|
first:
|
||||||
|
Name: First Person
|
Loading…
x
Reference in New Issue
Block a user