BUGFIX Fixed DataObject->merge() behaviour when merging grandchildren classes of DataObject - was using customDatabaseFields() to merge only the db fields on specific class. Changed to inheritedDatabaseFields() and added unit test.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69801 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-01-07 01:25:43 +00:00
parent c4250635bc
commit fcda174ea2
3 changed files with 57 additions and 3 deletions

View File

@ -489,8 +489,8 @@ class DataObject extends ViewableData implements DataObjectInterface,i18nEntityP
}
// makes sure we don't merge data like ID or ClassName
$leftData = $leftObj->customDatabaseFields();
$rightData = $rightObj->customDatabaseFields();
$leftData = $leftObj->inheritedDatabaseFields();
$rightData = $rightObj->inheritedDatabaseFields();
foreach($rightData as $key=>$rightVal) {
// don't merge conflicting values if priority is 'left'

View File

@ -543,6 +543,51 @@ class DataObjectTest extends SapphireTest {
$this->assertFalse(DataObject::has_own_table("ViewableData"));
$this->assertFalse(DataObject::has_own_table("ThisIsntADataObject"));
}
public function testMerge() {
// test right merge of subclasses
$left = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
$right = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam2_with_player_relation');
$leftOrigID = $left->ID;
$left->merge($right, 'right', false, false);
$this->assertEquals(
$left->Title,
'Subteam 2',
'merge() with "right" priority overwrites fields with existing values on subclasses'
);
$this->assertEquals(
$left->ID,
$leftOrigID,
'merge() with "right" priority doesnt overwrite database ID'
);
// test overwriteWithEmpty flag on existing left values
$left = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam2_with_player_relation');
$right = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam3_with_empty_fields');
$left->merge($right, 'right', false, true);
$this->assertEquals(
$left->Title,
'Subteam 3',
'merge() with $overwriteWithEmpty overwrites non-empty fields on left object'
);
// test overwriteWithEmpty flag on empty left values
$left = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam1');
$right = $this->objFromFixture('DataObjectTest_SubTeam', 'subteam2_with_player_relation'); // $SubclassDatabaseField is empty on here
$left->merge($right, 'right', false, true);
$this->assertEquals(
$left->SubclassDatabaseField,
NULL,
'merge() with $overwriteWithEmpty overwrites empty fields on left object'
);
// @todo test "left" priority flag
// @todo test includeRelations flag
// @todo test includeRelations in combination with overwriteWithEmpty
// @todo test has_one relations
// @todo test has_many and many_many relations
}
}
class DataObjectTest_Player extends Member implements TestOnly {

View File

@ -54,6 +54,15 @@ DataObjectTest_Player:
player2:
FirstName: Player 2
Teams: =>DataObjectTest_Team.team1,=>DataObjectTest_Team.team2
DataObjectTest_SubTeam:
subteam1:
Title: Subteam 1
Title: Subteam 1
SubclassDatabaseField: Subclassed 1
DecoratedDatabaseField: Decorated 1
subteam2_with_player_relation:
Title: Subteam 2
SubclassDatabaseField: Subclassed 2
DecoratedHasOneRelationship: => DataObjectTest_Player.player1
subteam3_with_empty_fields:
Title: Subteam 3