mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Fixed saving of TableField and added tests
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@61411 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
fb822a0924
commit
65604f4ab6
@ -34,6 +34,18 @@ class TableField extends TableListField {
|
||||
|
||||
protected $fieldList;
|
||||
|
||||
/**
|
||||
* A "Field = Value" filter can be specified by setting $this->filterField and $this->filterValue. This has the advantage of auto-populating
|
||||
* new records
|
||||
*/
|
||||
protected $filterField = null;
|
||||
|
||||
/**
|
||||
* A "Field = Value" filter can be specified by setting $this->filterField and $this->filterValue. This has the advantage of auto-populating
|
||||
* new records
|
||||
*/
|
||||
protected $filterValue = null;
|
||||
|
||||
/**
|
||||
* @var $fieldTypes FieldSet
|
||||
* Caution: Use {@setExtraData()} instead of manually adding HiddenFields if you want to
|
||||
@ -95,7 +107,12 @@ class TableField extends TableListField {
|
||||
|
||||
$this->editExisting = $editExisting;
|
||||
|
||||
parent::__construct($name, $sourceClass, $fieldList, $filterField ? "$filterField = $sourceFilter" : $sourceFilter, $sourceSort, $sourceJoin);
|
||||
// If we specify filterField, then an implicit source filter of "filterField = sourceFilter" is used.
|
||||
if($filterField) {
|
||||
$this->filterValue = $sourceFilter;
|
||||
$sourceFilter = "`$filterField` = '" . Convert::raw2sql($sourceFilter) . "'";
|
||||
}
|
||||
parent::__construct($name, $sourceClass, $fieldList, $sourceFilter, $sourceSort, $sourceJoin);
|
||||
|
||||
Requirements::javascript('sapphire/javascript/TableField.js');
|
||||
}
|
||||
@ -367,9 +384,9 @@ class TableField extends TableListField {
|
||||
}
|
||||
|
||||
// Legacy: Use the filter as a predefined relationship-ID
|
||||
if(!empty($this->filterField) && $this->sourceFilter) {
|
||||
if($this->filterField && $this->filterValue) {
|
||||
$filterField = $this->filterField;
|
||||
$obj->$filterField = $this->sourceFilter;
|
||||
$obj->$filterField = $this->filterValue;
|
||||
}
|
||||
|
||||
// Determine if there is changed data for saving
|
||||
|
82
tests/forms/TableFieldTest.php
Normal file
82
tests/forms/TableFieldTest.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
class TableFieldTest extends SapphireTest {
|
||||
static $fixture_file = 'sapphire/tests/forms/TableFieldTest.yml';
|
||||
|
||||
function testTableFieldSaving() {
|
||||
$group = $this->objFromFixture('Group','a');
|
||||
|
||||
$tableField = new TableField(
|
||||
"Permissions",
|
||||
"Permission",
|
||||
array(
|
||||
"Code" => _t('SecurityAdmin.CODE', 'Code'),
|
||||
"Arg" => _t('SecurityAdmin.OPTIONALID', 'Optional ID'),
|
||||
),
|
||||
array(
|
||||
"Code" => "PermissionDropdownField",
|
||||
"Arg" => "TextField",
|
||||
),
|
||||
"GroupID",
|
||||
$group->ID
|
||||
);
|
||||
$form = new Form(new TableFieldTest_Controller(), "Form", new FieldSet($tableField), new FieldSet());
|
||||
|
||||
/* The field starts emppty. Save some new data. We have replicated the array structure that the specific layout of the form generates. */
|
||||
$tableField->setValue(array(
|
||||
'new' => array(
|
||||
'Code' => array(
|
||||
'CMS_ACCESS_CMSMain',
|
||||
'CMS_ACCESS_AssetAdmin',
|
||||
),
|
||||
'Arg' => array(
|
||||
'1',
|
||||
'2'
|
||||
),
|
||||
),
|
||||
));
|
||||
$tableField->saveInto($group);
|
||||
|
||||
/* Let's check that the 2 permissions entries have been saved */
|
||||
$permissions = $group->Permissions()->toDropdownMap('Arg', 'Code');
|
||||
$this->assertEquals(array(
|
||||
1 => 'CMS_ACCESS_CMSMain',
|
||||
2 => 'CMS_ACCESS_AssetAdmin',
|
||||
), $permissions);
|
||||
|
||||
|
||||
/* Now let's perform an update query */
|
||||
$value = array();
|
||||
foreach($group->Permissions() as $permission) {
|
||||
$value[$permission->ID] = array("Code" => $permission->Code, "Arg" => $permission->Arg);
|
||||
}
|
||||
$value['new'] = array(
|
||||
'Code' => array(
|
||||
'CMS_ACCESS_NewsletterAdmin',
|
||||
),
|
||||
'Arg' => array(
|
||||
'3',
|
||||
),
|
||||
);
|
||||
$tableField->setValue($value);
|
||||
$tableField->saveInto($group);
|
||||
|
||||
/* Let's check that the 2 existing permissions entries, and the 1 new one, have been saved */
|
||||
$permissions = $group->Permissions()->toDropdownMap('Arg', 'Code');
|
||||
$this->assertEquals(array(
|
||||
1 => 'CMS_ACCESS_CMSMain',
|
||||
2 => 'CMS_ACCESS_AssetAdmin',
|
||||
3 => 'CMS_ACCESS_NewsletterAdmin',
|
||||
), $permissions);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stub controller
|
||||
*/
|
||||
class TableFieldTest_Controller extends Controller {
|
||||
function Link() {
|
||||
return 'TableFieldTest/';
|
||||
}
|
||||
}
|
3
tests/forms/TableFieldTest.yml
Normal file
3
tests/forms/TableFieldTest.yml
Normal file
@ -0,0 +1,3 @@
|
||||
Group:
|
||||
a:
|
||||
Title: Group A
|
Loading…
Reference in New Issue
Block a user