mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Added unit tests for Form->loadDataFrom()
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63761 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
0084e993cc
commit
1665fd5cf3
@ -5,6 +5,8 @@
|
||||
*/
|
||||
class FormTest extends FunctionalTest {
|
||||
|
||||
static $fixture_file = 'sapphire/tests/Forms/FormTest.yml';
|
||||
|
||||
public function testLoadDataFromRequest() {
|
||||
$form = new Form(
|
||||
new Controller(),
|
||||
@ -45,6 +47,61 @@ class FormTest extends FunctionalTest {
|
||||
$this->assertEquals($fields->fieldByName('othernamespace[key5][key6][key7]')->Value(), 'val7');
|
||||
}
|
||||
|
||||
public function testLoadDataFromObject() {
|
||||
$form = new Form(
|
||||
new Controller(),
|
||||
'Form',
|
||||
new FieldSet(
|
||||
new HeaderField('My Player'),
|
||||
new TextField('Name'), // appears in both Player and Team
|
||||
new TextareaField('Biography'),
|
||||
new DateField('Birthday'),
|
||||
new NumericField('BirthdayYear') // dynamic property
|
||||
//new CheckboxSetField('Teams') // relation editing
|
||||
),
|
||||
new FieldSet()
|
||||
);
|
||||
|
||||
$captain1 = $this->objFromFixture('FormTest_Player', 'captain1');
|
||||
$form->loadDataFrom($captain1);
|
||||
$this->assertEquals(
|
||||
$form->getData(),
|
||||
array(
|
||||
'Name' => 'Captain 1',
|
||||
'Biography' => 'Bio 1',
|
||||
'Birthday' => '1982-01-01',
|
||||
'BirthdayYear' => '1982',
|
||||
),
|
||||
'LoadDataFrom() loads simple fields and dynamic getters'
|
||||
);
|
||||
|
||||
$captain2 = $this->objFromFixture('FormTest_Player', 'captain2');
|
||||
$form->loadDataFrom($captain2);
|
||||
$this->assertEquals(
|
||||
$form->getData(),
|
||||
array(
|
||||
'Name' => 'Captain 2',
|
||||
'Biography' => 'Bio 1',
|
||||
'Birthday' => '1982-01-01',
|
||||
'BirthdayYear' => '1982',
|
||||
),
|
||||
'LoadNonBlankDataFrom() loads only fields with values, and doesnt overwrite existing values'
|
||||
);
|
||||
|
||||
$captain2 = $this->objFromFixture('FormTest_Player', 'captain2');
|
||||
$form->loadDataFrom($captain2, true);
|
||||
$this->assertEquals(
|
||||
$form->getData(),
|
||||
array(
|
||||
'Name' => 'Captain 2',
|
||||
'Biography' => '',
|
||||
'Birthday' => '',
|
||||
'BirthdayYear' => 0,
|
||||
),
|
||||
'LoadDataFrom() overwrites existing with blank values on subsequent calls'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFormMethodOverride() {
|
||||
$form = $this->getStubForm();
|
||||
$form->setFormMethod('GET');
|
||||
@ -79,4 +136,36 @@ class FormTest extends FunctionalTest {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class FormTest_Player extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
'Name' => 'Varchar',
|
||||
'Biography' => 'Text',
|
||||
'Birthday' => 'Date'
|
||||
);
|
||||
|
||||
static $belongs_many_many = array(
|
||||
'Teams' => 'FormTest_Team'
|
||||
);
|
||||
|
||||
static $has_one = array(
|
||||
'FavouriteTeam' => 'FormTest_Team',
|
||||
);
|
||||
|
||||
public function getBirthdayYear() {
|
||||
return ($this->Birthday) ? date('Y', strtotime($this->Birthday)) : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class FormTest_Team extends DataObject implements TestOnly {
|
||||
static $db = array(
|
||||
'Name' => 'Varchar',
|
||||
'Region' => 'Varchar',
|
||||
);
|
||||
|
||||
static $many_many = array(
|
||||
'Players' => 'FormTest_Player'
|
||||
);
|
||||
}
|
||||
?>
|
20
tests/forms/FormTest.yml
Normal file
20
tests/forms/FormTest.yml
Normal file
@ -0,0 +1,20 @@
|
||||
FormTest_Team:
|
||||
team1:
|
||||
Name: Team 1
|
||||
team2:
|
||||
Name: Team 2
|
||||
FormTest_Player:
|
||||
captain1:
|
||||
Name: Captain 1
|
||||
Birthday: 1982-01-01
|
||||
Biography: Bio 1
|
||||
FavouriteTeam: =>FormTest_Team.team1
|
||||
Teams: =>FormTest_Team.team1
|
||||
captain2:
|
||||
Name: Captain 2
|
||||
Teams: =>FormTest_Team.team2
|
||||
player1:
|
||||
Name: Player 1
|
||||
player2:
|
||||
Name: Player 2
|
||||
Teams: =>FormTest_Team.team1,=>FormTest_Team.team2
|
Loading…
Reference in New Issue
Block a user