From 60c5570bf897304e52a9e58b1531a3974e3f703f Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Tue, 26 May 2009 02:19:50 +0000 Subject: [PATCH] Merged from branches/2.3 git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@77817 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- forms/ComplexTableField.php | 29 ++++++++++--------- tests/forms/ComplexTableFieldTest.php | 40 ++++----------------------- 2 files changed, 22 insertions(+), 47 deletions(-) diff --git a/forms/ComplexTableField.php b/forms/ComplexTableField.php index 28b2aaa89..c6da7b726 100755 --- a/forms/ComplexTableField.php +++ b/forms/ComplexTableField.php @@ -412,8 +412,8 @@ JS; /** * Returns the db-fieldname of the currently used has_one-relationship. */ - function getParentIdName( $parentClass, $childClass ) { - return $this->getParentIdNameRelation( $childClass, $parentClass, 'has_one' ); + function getParentIdName($parentClass, $childClass) { + return $this->getParentIdNameRelation($childClass, $parentClass, 'has_one'); } /** @@ -429,14 +429,15 @@ JS; /** * Returns the db-fieldname of the currently used relationship. */ - function getParentIdNameRelation( $parentClass, $childClass, $relation ){ - if($this->parentIdName) return $this->parentIdName; + function getParentIdNameRelation($parentClass, $childClass, $relation) { + if($this->parentIdName) return $this->parentIdName; - $relations = singleton( $parentClass )->$relation(); - $classes = ClassInfo::ancestry( $childClass ); - foreach( $relations as $k => $v ) { - if( $v == $childClass ) - return $k . 'ID'; + $relations = singleton($parentClass)->$relation(); + $classes = ClassInfo::ancestry($childClass); + if($relations) { + foreach($relations as $k => $v) { + if(array_key_exists($v, $classes)) return $k . 'ID'; + } } return false; } @@ -930,8 +931,8 @@ class ComplexTableField_ItemRequest extends RequestHandler { /** * Returns the db-fieldname of the currently used has_one-relationship. */ - function getParentIdName( $parentClass, $childClass ) { - return $this->getParentIdNameRelation( $childClass, $parentClass, 'has_one' ); + function getParentIdName($parentClass, $childClass) { + return $this->getParentIdNameRelation($childClass, $parentClass, 'has_one'); } /** @@ -952,8 +953,10 @@ class ComplexTableField_ItemRequest extends RequestHandler { $relations = singleton($parentClass)->$relation(); $classes = ClassInfo::ancestry($childClass); - foreach($relations as $k => $v) { - if(array_key_exists($v, $classes)) return $k . 'ID'; + if($relations) { + foreach($relations as $k => $v) { + if(array_key_exists($v, $classes)) return $k . 'ID'; + } } return false; } diff --git a/tests/forms/ComplexTableFieldTest.php b/tests/forms/ComplexTableFieldTest.php index 4c01a52f6..8c8e19986 100644 --- a/tests/forms/ComplexTableFieldTest.php +++ b/tests/forms/ComplexTableFieldTest.php @@ -15,14 +15,6 @@ class ComplexTableFieldTest extends FunctionalTest { */ protected $controller; - /** - * An instance of {@link Form} that is taken - * from the test controller, used for testing. - * - * @var Form object - */ - protected $form; - function setUp() { parent::setUp(); @@ -34,10 +26,7 @@ class ComplexTableFieldTest extends FunctionalTest { $field = $this->manyManyForm->dataFieldByName('Players'); $parser = new CSSContentParser($field->FieldHolder()); - /* There are 2 players (rows) in the table */ $this->assertEquals(count($parser->getBySelector('tbody tr')), 2, 'There are 2 players (rows) in the table'); - - /* There are 2 CTF items in the DataObjectSet */ $this->assertEquals($field->Items()->Count(), 2, 'There are 2 CTF items in the DataObjectSet'); } @@ -54,16 +43,9 @@ class ComplexTableFieldTest extends FunctionalTest { ) )); - /* Retrieve the new player record we created */ $newPlayer = DataObject::get_one('ComplexTableFieldTest_Player', "Name = 'Bobby Joe'"); - - /* A new ComplexTableFieldTest_Player record was created, Name = "Bobby Joe" */ $this->assertNotNull($newPlayer, 'A new ComplexTableFieldTest_Player record was created, Name = "Bobby Joe"'); - - /* Get the many-many related Teams to the new player that were automatically linked by CTF */ $teams = $newPlayer->getManyManyComponents('Teams'); - - /* Automatic many-many relation was set correctly on the new player */ $this->assertEquals($teams->Count(), 1, 'Automatic many-many relation was set correctly on the new player'); } @@ -80,26 +62,16 @@ class ComplexTableFieldTest extends FunctionalTest { ) )); - /* Retrieve the new sponsor record we created */ $newSponsor = DataObject::get_one('ComplexTableFieldTest_Sponsor', "Name = 'Jim Beam'"); - - /* A new ComplexTableFieldTest_Sponsor record was created, Name = "Jim Beam" */ $this->assertNotNull($newSponsor, 'A new ComplexTableFieldTest_Sponsor record was created, Name = "Jim Beam"'); + $this->assertEquals($newSponsor->TeamID, $team->ID, 'Automatic has-many/has-one relation was set correctly on the sponsor'); + $this->assertEquals($newSponsor->getComponent('Team')->ID, $team->ID, 'Automatic has-many/has-one relation was set correctly on the sponsor'); - /* Get the has-one related Team to the new sponsor that were automatically linked by CTF */ - $teamID = $newSponsor->TeamID; - - /* Automatic many-many relation was set correctly on the new player */ - $this->assertTrue($teamID > 0, 'Automatic has-many/has-one relation was set correctly on the sponsor'); - - /* The other side of the relation works as well */ - $team = DataObject::get_by_id('ComplexTableFieldTest_Team', $teamID); - - /* Let's get the Sponsors component */ - $sponsor = $team->getComponents('Sponsors')->First(); - - /* The sponsor is the same as the one we added */ + $team = DataObject::get_by_id('ComplexTableFieldTest_Team', $team->ID); + $sponsor = DataObject::get_by_id('ComplexTableFieldTest_Sponsor', $newSponsor->ID); $this->assertEquals($newSponsor->ID, $sponsor->ID, 'The sponsor is the same as the one we added'); + $foundTeam = $sponsor->getComponent('Team'); + $this->assertEquals($team->ID, $foundTeam->ID, 'The team ID matches on the other side of the relation'); } }