From 65b0866bd08c3f14b10be1ce98395e5a2fe16ae6 Mon Sep 17 00:00:00 2001 From: Nic Horstmeier Date: Tue, 31 Aug 2021 20:52:31 -0500 Subject: [PATCH] BUGFIX default sort value not set when creating ingredient/direction --- src/Model/RecipeDirection.php | 15 ++++++++++++++ src/Model/RecipeIngredient.php | 15 ++++++++++++++ tests/Model/RecipeDirectionTest.php | 30 ++++++++++++++++++++++++++++ tests/Model/RecipeIngredientTest.php | 30 ++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+) diff --git a/src/Model/RecipeDirection.php b/src/Model/RecipeDirection.php index 44be4fb..57725cc 100644 --- a/src/Model/RecipeDirection.php +++ b/src/Model/RecipeDirection.php @@ -49,6 +49,9 @@ class RecipeDirection extends DataObject public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { + $recipe = $fields->dataFieldByName('RecipeID'); + $fields->replaceField('RecipeID', $recipe->performReadonlyTransformation()); + $fields->addFieldToTab( 'Root.Main', HTMLEditorField::create('Title') @@ -62,6 +65,18 @@ public function getCMSFields() return parent::getCMSFields(); } + /** + * + */ + protected function onBeforeWrite() + { + parent::onBeforeWrite(); + + if (!$this->Sort) { + $this->Sort = static::get()->filter('RecipeID', $this->RecipeID)->max('Sort') + 1; + } + } + /** * @param null $member * @param array $context diff --git a/src/Model/RecipeIngredient.php b/src/Model/RecipeIngredient.php index d706e64..036dd02 100644 --- a/src/Model/RecipeIngredient.php +++ b/src/Model/RecipeIngredient.php @@ -64,6 +64,9 @@ class RecipeIngredient extends DataObject public function getCMSFields() { $this->beforeUpdateCMSFields(function (FieldList $fields) { + $recipe = $fields->dataFieldByName('RecipeID'); + $fields->replaceField('RecipeID', $recipe->performReadonlyTransformation()); + $fields->addFieldToTab('Root.Main', TextField::create('Title')->setTitle('Ingredient and measurement')); $fields->removeByName('Sort'); }); @@ -71,6 +74,18 @@ public function getCMSFields() return parent::getCMSFields(); } + /** + * + */ + protected function onBeforeWrite() + { + parent::onBeforeWrite(); + + if (!$this->Sort) { + $this->Sort = static::get()->filter('RecipeID', $this->RecipeID)->max('Sort') + 1; + } + } + /** * @param null $member * @param array $context diff --git a/tests/Model/RecipeDirectionTest.php b/tests/Model/RecipeDirectionTest.php index c32ebb1..7919b91 100644 --- a/tests/Model/RecipeDirectionTest.php +++ b/tests/Model/RecipeDirectionTest.php @@ -3,8 +3,10 @@ namespace Dynamic\RecipeBook\Test\Model; use Dynamic\RecipeBook\Model\RecipeDirection; +use Dynamic\RecipeBook\Page\RecipePage; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\FieldList; +use SilverStripe\ORM\ValidationException; class RecipeDirectionTest extends SapphireTest { @@ -22,4 +24,32 @@ public function testGetCMSFields() $fields = $object->getCMSFields(); $this->assertInstanceOf(FieldList::class, $fields); } + + /** + * @throws ValidationException + */ + public function testDefaultSortValue() + { + $recipe = $this->objFromFixture(RecipePage::class, 'two'); + + $direction = RecipeDirection::create(); + $direction->Title = 'My First Direction'; + $direction->RecipeID = $recipe->ID; + + $this->assertEquals(0, $direction->Sort); + + $direction->write(); + + $this->assertNotEquals(0, $direction->Sort); + + $direction2 = RecipeDirection::create(); + $direction2->Title = 'My Second Direction'; + $direction2->RecipeID = $recipe->ID; + + $this->assertEquals(0, $direction2->Sort); + + $direction2->write(); + + $this->assertGreaterThan($direction->Sort, $direction2->Sort); + } } diff --git a/tests/Model/RecipeIngredientTest.php b/tests/Model/RecipeIngredientTest.php index b15dbeb..c31aba1 100644 --- a/tests/Model/RecipeIngredientTest.php +++ b/tests/Model/RecipeIngredientTest.php @@ -3,8 +3,10 @@ namespace Dynamic\RecipeBook\Test\Model; use Dynamic\RecipeBook\Model\RecipeIngredient; +use Dynamic\RecipeBook\Page\RecipePage; use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\FieldList; +use SilverStripe\ORM\ValidationException; class RecipeIngredientTest extends SapphireTest { @@ -22,4 +24,32 @@ public function testGetCMSFields() $fields = $object->getCMSFields(); $this->assertInstanceOf(FieldList::class, $fields); } + + /** + * @throws ValidationException + */ + public function testDefaultSortValue() + { + $recipe = $this->objFromFixture(RecipePage::class, 'two'); + + $ingredient = RecipeIngredient::create(); + $ingredient->Title = 'My First Ingredient'; + $ingredient->RecipeID = $recipe->ID; + + $this->assertEquals(0, $ingredient->Sort); + + $ingredient->write(); + + $this->assertNotEquals(0, $ingredient->Sort); + + $ingredient2 = RecipeIngredient::create(); + $ingredient2->Title = 'My Second Ingredient'; + $ingredient2->RecipeID = $recipe->ID; + + $this->assertEquals(0, $ingredient2->Sort); + + $ingredient2->write(); + + $this->assertGreaterThan($ingredient->Sort, $ingredient2->Sort); + } }