From c6457c50e970654b43ff009933a80a1a493186fb Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 5 Sep 2016 16:58:36 +1200 Subject: [PATCH 1/3] 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 --- dev/FixtureBlueprint.php | 43 +++++++++++++------ tests/dev/FixtureBlueprintTest.php | 20 +++++++++ tests/dev/FixtureFactoryTest.php | 8 ++++ .../gridfield/GridFieldDetailFormTest.php | 2 +- 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/dev/FixtureBlueprint.php b/dev/FixtureBlueprint.php index 8497b99c5..7d8f8057d 100644 --- a/dev/FixtureBlueprint.php +++ b/dev/FixtureBlueprint.php @@ -131,29 +131,48 @@ class FixtureBlueprint { // Populate all relations 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(); - $parsedItems = array(); - - if(is_array($fieldVal)) { + // Many many components need a little extra work to extract extrafields + if(is_array($fieldVal) && $isManyMany) { // handle lists of many_many relations. Each item can // specify the many_many_extraFields against each // related item. 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); - $parsedItems[] = $id; - - array_shift($relVal); $obj->getManyManyComponents($fieldName)->add( - $id, $relVal + $id, $extrafields ); } } else { - $items = preg_split('/ *, */',trim($fieldVal)); + $items = is_array($fieldVal) + ? $fieldVal + : preg_split('/ *, */',trim($fieldVal)); + $parsedItems = array(); foreach($items as $item) { // Check for correct format: =>.. // Ignore if the item has already been replaced with a numeric DB identifier @@ -169,9 +188,9 @@ class FixtureBlueprint { $parsedItems[] = $this->parseValue($item, $fixtures); } - if($obj->hasManyComponent($fieldName)) { + if($isHasMany) { $obj->getComponents($fieldName)->setByIDList($parsedItems); - } elseif($obj->manyManyComponent($fieldName)) { + } elseif($isManyMany) { $obj->getManyManyComponents($fieldName)->setByIDList($parsedItems); } } diff --git a/tests/dev/FixtureBlueprintTest.php b/tests/dev/FixtureBlueprintTest.php index 5cf0268a4..20d0b3b22 100644 --- a/tests/dev/FixtureBlueprintTest.php +++ b/tests/dev/FixtureBlueprintTest.php @@ -109,6 +109,26 @@ class FixtureBlueprintTest extends SapphireTest { $this->assertEquals(2, $obj->ManyManyRelation()->Count()); $this->assertNotNull($obj->ManyManyRelation()->find('ID', $relation1->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)); } /** diff --git a/tests/dev/FixtureFactoryTest.php b/tests/dev/FixtureFactoryTest.php index 9d7013e2d..20b67304a 100644 --- a/tests/dev/FixtureFactoryTest.php +++ b/tests/dev/FixtureFactoryTest.php @@ -162,6 +162,10 @@ class FixtureFactoryTest_DataObject extends DataObject implements TestOnly { "Name" => "Varchar" ); + private static $has_many = array( + "HasManyRelation" => "FixtureFactoryTest_DataObjectRelation" + ); + private static $many_many = array( "ManyManyRelation" => "FixtureFactoryTest_DataObjectRelation" ); @@ -186,4 +190,8 @@ class FixtureFactoryTest_DataObjectRelation extends DataObject implements TestOn private static $belongs_many_many = array( "TestParent" => "FixtureFactoryTest_DataObject" ); + + private static $has_one = array( + 'MyParent' => 'FixtureFactoryTest_DataObject' + ); } diff --git a/tests/forms/gridfield/GridFieldDetailFormTest.php b/tests/forms/gridfield/GridFieldDetailFormTest.php index fd8d965fd..327b4c05d 100644 --- a/tests/forms/gridfield/GridFieldDetailFormTest.php +++ b/tests/forms/gridfield/GridFieldDetailFormTest.php @@ -426,7 +426,7 @@ class GridFieldDetailFormTest_PeopleGroup extends DataObject implements TestOnly ); private static $belongs_many_many = array( - 'People' => 'GridFieldDetailFormTest_Person' + 'FavouritePeople' => 'GridFieldDetailFormTest_Person' ); private static $default_sort = '"Name"'; From a14df0bc2d08f953ff7dd6f57899dbf260ab13a5 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 12 Sep 2016 14:15:07 +0100 Subject: [PATCH 2/3] FIX Force line endings to LF on sake file --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..5c2d3db9b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Line endings +sake text eol=lf From e3ac75febd76591881a46e6eead17e4133f99f8d Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 12 Sep 2016 17:01:50 +0100 Subject: [PATCH 3/3] DOCS Reference composer docs for installation instructions --- docs/en/00_Getting_Started/02_Composer.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/docs/en/00_Getting_Started/02_Composer.md b/docs/en/00_Getting_Started/02_Composer.md index 6eb9f83b1..3fae65ed9 100644 --- a/docs/en/00_Getting_Started/02_Composer.md +++ b/docs/en/00_Getting_Started/02_Composer.md @@ -11,15 +11,7 @@ We also have separate instructions for [installing modules with Composer](/devel Before installing Composer you should ensure your system has the version control system, [Git installed](http://git-scm.com/book/en/v2/Getting-Started-Installing-Git). Composer uses Git to check out the code dependancies you need to run your SilverStripe CMS website from the code repositories maintained on GitHub. -Next, to install Composer, run the following commands from your command-line. - - # Download composer.phar - curl -s https://getcomposer.org/installer | php - - # Move to your path - sudo mv composer.phar /usr/local/bin/composer - -Or [download composer.phar](http://getcomposer.org/composer.phar) manually, and rename `composer.phar` as `composer`, and put it in your path. On Windows, you should call the file `composer.bat`. +Next, [install composer](https://getcomposer.org/download/). For our documentation we assume composer is installed globally. You can then run Composer commands by calling `composer`. For example: