Skip to content

Commit

Permalink
Merge pull request #42 from muskie9/pull/defaultSortValue
Browse files Browse the repository at this point in the history
BUGFIX default sort value not set when creating ingredient/direction
  • Loading branch information
muskie9 authored Sep 1, 2021
2 parents 0c41f92 + 65b0866 commit 90818c0
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Model/RecipeDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/Model/RecipeIngredient.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,28 @@ 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');
});

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
Expand Down
30 changes: 30 additions & 0 deletions tests/Model/RecipeDirectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
30 changes: 30 additions & 0 deletions tests/Model/RecipeIngredientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}

0 comments on commit 90818c0

Please sign in to comment.