diff --git a/.gitignore b/.gitignore index f27ea9013c..71d8f61ce0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ tests/acceptance/AcceptanceTester.php tests/functional/FunctionalTester.php tests/unit/UnitTester.php pi.php +tests/_data/db.sqlite +tests/_data/dump.sql diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php index 177d96add4..c3fc2b17cb 100644 --- a/app/controllers/BudgetController.php +++ b/app/controllers/BudgetController.php @@ -146,7 +146,7 @@ class BudgetController extends BaseController public function show(Budget $budget, LimitRepetition $repetition = null) { if (!is_null($repetition) && $repetition->budgetLimit->budget->id != $budget->id) { - return View::make('error')->with('message','Invalid selection.'); + return View::make('error')->with('message', 'Invalid selection.'); } $hideBudget = true; // used in transaction list. @@ -162,7 +162,8 @@ class BudgetController extends BaseController */ public function store() { - $data = Input::except('_token'); + $data = Input::except('_token'); + $data['user_id'] = Auth::user()->id; // always validate: $messages = $this->_repository->validate($data); @@ -188,11 +189,7 @@ class BudgetController extends BaseController } // create another. - if ($data['post_submit_action'] == 'create_another') { - return Redirect::route('budgets.create')->withInput(); - } - - return Redirect::route('budgets.index'); + return Redirect::route('budgets.create')->withInput(); } @@ -204,7 +201,8 @@ class BudgetController extends BaseController public function update(Budget $budget) { - $data = Input::except('_token'); + $data = Input::except('_token'); + $data['user_id'] = Auth::user()->id; // always validate: $messages = $this->_repository->validate($data); @@ -230,12 +228,8 @@ class BudgetController extends BaseController if ($data['post_submit_action'] == 'update') { return Redirect::route('budgets.index'); } - // go back to update screen. - if ($data['post_submit_action'] == 'return_to_edit') { - return Redirect::route('budgets.edit', $budget->id)->withInput(['post_submit_action' => 'return_to_edit']); - } - return Redirect::route('budgets.index'); + return Redirect::route('budgets.edit', $budget->id)->withInput(['post_submit_action' => 'return_to_edit']); } /** diff --git a/app/lib/FireflyIII/Database/Budget/Budget.php b/app/lib/FireflyIII/Database/Budget/Budget.php index d69257a3e3..1fee659e8a 100644 --- a/app/lib/FireflyIII/Database/Budget/Budget.php +++ b/app/lib/FireflyIII/Database/Budget/Budget.php @@ -48,10 +48,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface */ public function store(array $data) { - $data['user_id'] = $this->getUser()->id; - $budget = new \Budget($data); - $budget->class = 'Budget'; if (!$budget->isValid()) { \Log::error('Could not store budget: ' . $budget->getErrors()->toJson()); @@ -88,8 +85,9 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface { $warnings = new MessageBag; $successes = new MessageBag; - $validator = \Validator::make($model, \Component::$rules); - $errors = $validator->errors(); + $budget = new \Budget($model); + $budget->isValid(); + $errors = $budget->getErrors(); if (!$errors->has('name')) { $successes->add('name', 'OK'); diff --git a/app/models/Budget.php b/app/models/Budget.php index 2a3e1254d6..e277b51ca6 100644 --- a/app/models/Budget.php +++ b/app/models/Budget.php @@ -2,13 +2,18 @@ use Illuminate\Database\Eloquent\SoftDeletingTrait; use \Illuminate\Database\Eloquent\Model as Eloquent; +use \Watson\Validating\ValidatingTrait; /** * Class Budget */ class Budget extends Eloquent { - use SoftDeletingTrait; + use SoftDeletingTrait, ValidatingTrait; protected $fillable = ['name', 'user_id']; + protected $rules = [ + 'user_id' => 'exists:users,id|required', + 'name' => 'required|between:1,100|alphabasic', + ]; /** * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough diff --git a/tests/functional/AccountControllerCest.php b/tests/functional/AccountControllerCest.php index 5eb4630822..6f63f66031 100644 --- a/tests/functional/AccountControllerCest.php +++ b/tests/functional/AccountControllerCest.php @@ -109,7 +109,7 @@ class AccountControllerCest public function storeValidateOnly(FunctionalTester $I) { $I->amOnPage('/accounts/create/asset'); - $I->wantTo('store a new asset account'); + $I->wantTo('validate a new asset account'); $I->see('Create a new asset account'); $I->submitForm( '#store', ['name' => 'New through tests.', 'what' => 'asset', 'account_role' => 'defaultExpense', 'post_submit_action' => 'validate_only'] diff --git a/tests/functional/BudgetControllerCest.php b/tests/functional/BudgetControllerCest.php index 1c8e793ff8..b36657d991 100644 --- a/tests/functional/BudgetControllerCest.php +++ b/tests/functional/BudgetControllerCest.php @@ -129,15 +129,108 @@ class BudgetControllerCest */ public function store(FunctionalTester $I) { - $I->wantTo('store a budget'); + $I->amOnPage('/budgets/create'); + $I->wantTo('store a new budget'); + $I->see('Create a new budget'); + $I->submitForm('#store', ['name' => 'New budget.', 'post_submit_action' => 'store']); + $I->seeRecord('budgets', ['name' => 'New budget.']); + resetToClean::clean(); } + /** + * @param FunctionalTester $I + */ + public function storeValidateOnly(FunctionalTester $I) + { + $I->amOnPage('/budgets/create'); + $I->wantTo('validate a new budget'); + $I->see('Create a new budget'); + $I->submitForm('#store', ['name' => 'New budget.', 'post_submit_action' => 'validate_only']); + $I->dontSeeRecord('budgets', ['name' => 'New budget.']); + resetToClean::clean(); + } + + /** + * @param FunctionalTester $I + */ + public function storeAndCreateAnother(FunctionalTester $I) + { + $I->amOnPage('/budgets/create'); + $I->wantTo('store a new budget and create another'); + $I->see('Create a new budget'); + $I->submitForm('#store', ['name' => 'New budget.', 'post_submit_action' => 'create_another']); + $I->seeRecord('budgets', ['name' => 'New budget.']); + resetToClean::clean(); + } + + /** + * @param FunctionalTester $I + */ + public function storeFail(FunctionalTester $I) + { + $I->amOnPage('/budgets/create'); + $I->wantTo('make storing a new budget fail.'); + $I->see('Create a new budget'); + $I->submitForm('#store', ['name' => null, 'post_submit_action' => 'validate_only']); + $I->dontSeeRecord('budgets', ['name' => 'New budget.']); + resetToClean::clean(); + } /** * @param FunctionalTester $I */ public function update(FunctionalTester $I) { $I->wantTo('update a budget'); + $I->amOnPage('/budgets/edit/3'); + $I->see('Edit budget "Delete me"'); + $I->submitForm('#update', ['name' => 'Update me', 'post_submit_action' => 'update']); + $I->seeRecord('budgets', ['name' => 'Update me']); + resetToClean::clean(); + + } + + /** + * @param FunctionalTester $I + */ + public function failUpdate(FunctionalTester $I) + { + $I->wantTo('update a budget and fail'); + $I->amOnPage('/budgets/edit/3'); + $I->see('Edit budget "Delete me"'); + $I->submitForm('#update', ['name' => '', 'post_submit_action' => 'update']); + $I->seeRecord('budgets', ['name' => 'Delete me']); + + } + + /** + * @param FunctionalTester $I + */ + public function validateUpdateOnly(FunctionalTester $I) + { + $I->wantTo('update a budget and validate only'); + $I->amOnPage('/budgets/edit/3'); + $I->see('Edit budget "Delete me"'); + $I->submitForm( + '#update', ['name' => 'Validate Only', 'post_submit_action' => 'validate_only'] + ); + $I->dontSeeRecord('budgets', ['name' => 'Savings accountXX']); + $I->seeRecord('budgets', ['name' => 'Delete me']); + + } + + /** + * @param FunctionalTester $I + */ + public function updateAndReturn(FunctionalTester $I) + { + $I->wantTo('update a budget and return to form'); + $I->amOnPage('/budgets/edit/3'); + $I->see('Edit budget "Delete me"'); + $I->submitForm( + '#update', ['name' => 'Savings accountXX', 'post_submit_action' => 'return_to_edit'] + ); + $I->seeRecord('budgets', ['name' => 'Savings accountXX']); + } /** @@ -145,6 +238,8 @@ class BudgetControllerCest */ public function updateIncome(FunctionalTester $I) { + $I->amOnPage('/budgets/income'); $I->wantTo('update my monthly income'); + $I->see('Update (expected) income for '); } } \ No newline at end of file