API Allow has_many fixtures to be declared with array format as well as many_many (#5944)

BUG Fix issue with parsing of extrafields in fixtures
BUG Fix issue in duplicate relation name, and ensure FixtureBlueprint fails on these
This commit is contained in:
Damian Mooyman 2016-09-05 16:58:36 +12:00 committed by Daniel Hensby
parent 5959419645
commit c6457c50e9
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E
4 changed files with 60 additions and 13 deletions

View File

@ -131,29 +131,48 @@ class FixtureBlueprint {
// Populate all relations // Populate all relations
if($data) foreach($data as $fieldName => $fieldVal) { if($data) foreach($data as $fieldName => $fieldVal) {
if($obj->manyManyComponent($fieldName) || $obj->hasManyComponent($fieldName)) { $isManyMany = $obj->manyManyComponent($fieldName);
$isHasMany = $obj->hasManyComponent($fieldName);
if ($isManyMany && $isHasMany) {
throw new InvalidArgumentException("$fieldName is both many_many and has_many");
}
if($isManyMany || $isHasMany) {
$obj->write(); $obj->write();
$parsedItems = array(); // Many many components need a little extra work to extract extrafields
if(is_array($fieldVal) && $isManyMany) {
if(is_array($fieldVal)) {
// handle lists of many_many relations. Each item can // handle lists of many_many relations. Each item can
// specify the many_many_extraFields against each // specify the many_many_extraFields against each
// related item. // related item.
foreach($fieldVal as $relVal) { foreach($fieldVal as $relVal) {
$item = key($relVal); // Check for many_many_extrafields
$extrafields = array();
if (is_array($relVal)) {
// Item is either first row, or key in yet another nested array
$item = key($relVal);
if (is_array($relVal[$item]) && count($relVal) === 1) {
// Extra fields from nested array
$extrafields = $relVal[$item];
} else {
// Extra fields from subsequent items
array_shift($relVal);
$extrafields = $relVal;
}
} else {
$item = $relVal;
}
$id = $this->parseValue($item, $fixtures); $id = $this->parseValue($item, $fixtures);
$parsedItems[] = $id;
array_shift($relVal);
$obj->getManyManyComponents($fieldName)->add( $obj->getManyManyComponents($fieldName)->add(
$id, $relVal $id, $extrafields
); );
} }
} else { } else {
$items = preg_split('/ *, */',trim($fieldVal)); $items = is_array($fieldVal)
? $fieldVal
: preg_split('/ *, */',trim($fieldVal));
$parsedItems = array();
foreach($items as $item) { foreach($items as $item) {
// Check for correct format: =><relationname>.<identifier>. // Check for correct format: =><relationname>.<identifier>.
// Ignore if the item has already been replaced with a numeric DB identifier // Ignore if the item has already been replaced with a numeric DB identifier
@ -169,9 +188,9 @@ class FixtureBlueprint {
$parsedItems[] = $this->parseValue($item, $fixtures); $parsedItems[] = $this->parseValue($item, $fixtures);
} }
if($obj->hasManyComponent($fieldName)) { if($isHasMany) {
$obj->getComponents($fieldName)->setByIDList($parsedItems); $obj->getComponents($fieldName)->setByIDList($parsedItems);
} elseif($obj->manyManyComponent($fieldName)) { } elseif($isManyMany) {
$obj->getManyManyComponents($fieldName)->setByIDList($parsedItems); $obj->getManyManyComponents($fieldName)->setByIDList($parsedItems);
} }
} }

View File

@ -109,6 +109,26 @@ class FixtureBlueprintTest extends SapphireTest {
$this->assertEquals(2, $obj->ManyManyRelation()->Count()); $this->assertEquals(2, $obj->ManyManyRelation()->Count());
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID)); $this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->ID));
$this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID)); $this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation2->ID));
$obj2 = $blueprint->createObject(
'two',
array(
// Note; using array format here, not comma separated
'HasManyRelation' => array(
'=>FixtureFactoryTest_DataObjectRelation.relation1',
'=>FixtureFactoryTest_DataObjectRelation.relation2'
)
),
array(
'FixtureFactoryTest_DataObjectRelation' => array(
'relation1' => $relation1->ID,
'relation2' => $relation2->ID
)
)
);
$this->assertEquals(2, $obj2->HasManyRelation()->Count());
$this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation1->ID));
$this->assertNotNull($obj2->HasManyRelation()->find('ID', $relation2->ID));
} }
/** /**

View File

@ -162,6 +162,10 @@ class FixtureFactoryTest_DataObject extends DataObject implements TestOnly {
"Name" => "Varchar" "Name" => "Varchar"
); );
private static $has_many = array(
"HasManyRelation" => "FixtureFactoryTest_DataObjectRelation"
);
private static $many_many = array( private static $many_many = array(
"ManyManyRelation" => "FixtureFactoryTest_DataObjectRelation" "ManyManyRelation" => "FixtureFactoryTest_DataObjectRelation"
); );
@ -186,4 +190,8 @@ class FixtureFactoryTest_DataObjectRelation extends DataObject implements TestOn
private static $belongs_many_many = array( private static $belongs_many_many = array(
"TestParent" => "FixtureFactoryTest_DataObject" "TestParent" => "FixtureFactoryTest_DataObject"
); );
private static $has_one = array(
'MyParent' => 'FixtureFactoryTest_DataObject'
);
} }

View File

@ -426,7 +426,7 @@ class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly
); );
private static $belongs_many_many = array( private static $belongs_many_many = array(
'People' => 'GridFieldDetailFormTest_Person' 'FavouritePeople' => 'GridFieldDetailFormTest_Person'
); );
private static $default_sort = '"Name"'; private static $default_sort = '"Name"';