diff --git a/app/controllers/CategoryController.php b/app/controllers/CategoryController.php
index 94e9c9409f..11c68ab7e6 100644
--- a/app/controllers/CategoryController.php
+++ b/app/controllers/CategoryController.php
@@ -96,7 +96,8 @@ class CategoryController 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);
@@ -121,12 +122,7 @@ class CategoryController extends BaseController
return Redirect::route('categories.index');
}
- // create another.
- if ($data['post_submit_action'] == 'create_another') {
- return Redirect::route('categories.create')->withInput();
- }
-
- return Redirect::route('categories.index');
+ return Redirect::route('categories.create')->withInput();
}
/**
@@ -137,7 +133,8 @@ class CategoryController extends BaseController
*/
public function update(Category $category)
{
- $data = Input::except('_token');
+ $data = Input::except('_token');
+ $data['user_id'] = Auth::user()->id;
// always validate:
$messages = $this->_repository->validate($data);
@@ -163,12 +160,9 @@ class CategoryController extends BaseController
if ($data['post_submit_action'] == 'update') {
return Redirect::route('categories.index');
}
- // go back to update screen.
- if ($data['post_submit_action'] == 'return_to_edit') {
- return Redirect::route('categories.edit', $category->id)->withInput(['post_submit_action' => 'return_to_edit']);
- }
- return Redirect::route('categories.index');
+ // go back to update screen.
+ return Redirect::route('categories.edit', $category->id)->withInput(['post_submit_action' => 'return_to_edit']);
}
diff --git a/app/database/seeds/TestContentSeeder.php b/app/database/seeds/TestContentSeeder.php
index 093e63d407..1b3bebb947 100644
--- a/app/database/seeds/TestContentSeeder.php
+++ b/app/database/seeds/TestContentSeeder.php
@@ -48,13 +48,14 @@ class TestContentSeeder extends Seeder
// and because we have no filters, some repetitions:
$repOne = LimitRepetition::create(['budget_limit_id' => $limitOne->id, 'startdate' => '2014-01-01', 'enddate' => '2014-01-31', 'amount' => 200]);
- $repOne = LimitRepetition::create(['budget_limit_id' => $limitTwo->id, 'startdate' => '2014-01-01', 'enddate' => '2014-01-31', 'amount' => 200]);
- $repOne = LimitRepetition::create(['budget_limit_id' => $limitThree->id, 'startdate' => '2014-01-01', 'enddate' => '2014-01-31', 'amount' => 200]);
+ $repTwo = LimitRepetition::create(['budget_limit_id' => $limitTwo->id, 'startdate' => '2014-01-01', 'enddate' => '2014-01-31', 'amount' => 200]);
+ $repThree = LimitRepetition::create(['budget_limit_id' => $limitThree->id, 'startdate' => '2014-01-01', 'enddate' => '2014-01-31', 'amount' => 200]);
// create two categories:
$dailyGroceries = Category::create(['user_id' => $user->id, 'name' => 'DailyGroceries']);
$lunch = Category::create(['user_id' => $user->id, 'name' => 'Lunch']);
$house = Category::create(['user_id' => $user->id, 'name' => 'House']);
+ $deleteMe= Category::create(['user_id' => $user->id, 'name' => 'Delete me']);
Component::create(['user_id' => $user->id, 'name' => 'Some Component 1', 'class' => 'Budget']);
Component::create(['user_id' => $user->id, 'name' => 'Some Component 2', 'class' => 'Budget']);
diff --git a/app/lib/FireflyIII/Database/Category/Category.php b/app/lib/FireflyIII/Database/Category/Category.php
index 3df8b608e3..dd0167aaf9 100644
--- a/app/lib/FireflyIII/Database/Category/Category.php
+++ b/app/lib/FireflyIII/Database/Category/Category.php
@@ -48,9 +48,8 @@ class Category implements CUD, CommonDatabaseCalls
*/
public function store(array $data)
{
- $category = new \Category;
- $category->name = $data['name'];
- $category->class = 'Category';
+ $category = new \Category;
+ $category->name = $data['name'];
$category->user()->associate($this->getUser());
if (!$category->isValid()) {
\Log::error('Could not store category: ' . $category->getErrors()->toJson());
@@ -71,12 +70,6 @@ class Category implements CUD, CommonDatabaseCalls
public function update(Eloquent $model, array $data)
{
$model->name = $data['name'];
- if (!$model->isValid()) {
- \Log::error('Could not store category: ' . $model->getErrors()->toJson());
- throw new FireflyException($model->getErrors()->first());
- }
-
-
$model->save();
return true;
@@ -94,8 +87,9 @@ class Category implements CUD, CommonDatabaseCalls
{
$warnings = new MessageBag;
$successes = new MessageBag;
- $validator = \Validator::make($model, \Component::$rules);
- $errors = $validator->errors();
+ $category = new \Category($model);
+ $category->isValid();
+ $errors = $category->getErrors();
if (!$errors->has('name')) {
$successes->add('name', 'OK');
diff --git a/app/models/Category.php b/app/models/Category.php
index 2dd934341e..782a394585 100644
--- a/app/models/Category.php
+++ b/app/models/Category.php
@@ -1,13 +1,35 @@
'exists:users,id|required',
+ 'name' => 'required|between:1,100|alphabasic',
+ ];
+
+ /**
+ * remove this method in favour of something in the FireflyIII libraries.
+ *
+ * @return Carbon
+ */
+ public function lastActionDate()
+ {
+ $transaction = $this->transactionjournals()->orderBy('updated_at', 'DESC')->first();
+ if (is_null($transaction)) {
+ return null;
+ }
+
+ return $transaction->date;
+ }
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
@@ -24,19 +46,4 @@ class Category extends Eloquent
{
return $this->belongsTo('User');
}
-
- /**
- * remove this method in favour of something in the FireflyIII libraries.
- *
- * @return Carbon
- */
- public function lastActionDate()
- {
- $transaction = $this->transactionjournals()->orderBy('updated_at', 'DESC')->first();
- if (is_null($transaction)) {
- return null;
- }
-
- return $transaction->date;
- }
}
\ No newline at end of file
diff --git a/app/views/categories/create.blade.php b/app/views/categories/create.blade.php
index f4c8111248..22faffe5f5 100644
--- a/app/views/categories/create.blade.php
+++ b/app/views/categories/create.blade.php
@@ -1,7 +1,7 @@
@extends('layouts.default')
@section('content')
{{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName()) }}
-{{Form::open(['class' => 'form-horizontal','url' => route('categories.store')])}}
+{{Form::open(['class' => 'form-horizontal','id' => 'store','url' => route('categories.store')])}}
diff --git a/app/views/categories/delete.blade.php b/app/views/categories/delete.blade.php
index b5e62748c1..399e2762fd 100644
--- a/app/views/categories/delete.blade.php
+++ b/app/views/categories/delete.blade.php
@@ -1,7 +1,7 @@
@extends('layouts.default')
@section('content')
{{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName(), $category) }}
-{{Form::open(['class' => 'form-horizontal','url' => route('categories.destroy',$category->id)])}}
+{{Form::open(['class' => 'form-horizontal','id' => 'destroy','url' => route('categories.destroy',$category->id)])}}
diff --git a/app/views/categories/edit.blade.php b/app/views/categories/edit.blade.php
index 6030c385b8..24123a7103 100644
--- a/app/views/categories/edit.blade.php
+++ b/app/views/categories/edit.blade.php
@@ -1,7 +1,7 @@
@extends('layouts.default')
@section('content')
{{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName(), $category) }}
-{{Form::model($category, ['class' => 'form-horizontal','url' => route('categories.update',$category->id)])}}
+{{Form::model($category, ['class' => 'form-horizontal','id' => 'update','url' => route('categories.update',$category->id)])}}
diff --git a/app/views/categories/show.blade.php b/app/views/categories/show.blade.php
index 16555e7a9e..34e1d4a265 100644
--- a/app/views/categories/show.blade.php
+++ b/app/views/categories/show.blade.php
@@ -31,7 +31,7 @@
@section('scripts')
diff --git a/tests/functional/BudgetControllerCest.php b/tests/functional/BudgetControllerCest.php
index b36657d991..13051fe5b6 100644
--- a/tests/functional/BudgetControllerCest.php
+++ b/tests/functional/BudgetControllerCest.php
@@ -70,8 +70,6 @@ class BudgetControllerCest
$I->see('Delete budget "Delete me"');
$I->submitForm('#destroy', []);
$I->see('Budget "Delete me" was deleted.');
- #$I->dontSeeInDatabase('budgets', ['name' => 'Delete me','deleted_at' => null]);
- resetToClean::clean();
}
/**
@@ -80,7 +78,8 @@ class BudgetControllerCest
public function edit(FunctionalTester $I)
{
$I->wantTo('edit a budget');
- $I->amOnPage('/budgets/edit/1');
+ $I->amOnPage('/budgets/edit/3');
+ $I->see('Edit budget "Delete me"');
}
/**
@@ -111,7 +110,8 @@ class BudgetControllerCest
public function show(FunctionalTester $I)
{
$I->wantTo('show a budget');
- $I->amOnPage('/budgets/show/1');
+ $I->amOnPage('/budgets/show/3');
+ $I->see('Delete me');
}
/**
diff --git a/tests/functional/CategoryControllerCest.php b/tests/functional/CategoryControllerCest.php
index 34057e1e90..0b2ea923e1 100644
--- a/tests/functional/CategoryControllerCest.php
+++ b/tests/functional/CategoryControllerCest.php
@@ -38,6 +38,8 @@ class CategoryControllerCest
public function delete(FunctionalTester $I)
{
$I->wantTo('delete a category');
+ $I->amOnPage('/categories/delete/4');
+ $I->see('Delete category "Delete me"');
}
/**
@@ -46,6 +48,10 @@ class CategoryControllerCest
public function destroy(FunctionalTester $I)
{
$I->wantTo('destroy a category');
+ $I->amOnPage('/categories/delete/4');
+ $I->see('Delete category "Delete me"');
+ $I->submitForm('#destroy', []);
+ $I->see('Category "Delete me" was deleted.');
}
@@ -55,6 +61,8 @@ class CategoryControllerCest
public function edit(FunctionalTester $I)
{
$I->wantTo('edit a category');
+ $I->amOnPage('/categories/edit/4');
+ $I->see('Edit category "Delete me"');
}
/**
@@ -72,6 +80,8 @@ class CategoryControllerCest
public function show(FunctionalTester $I)
{
$I->wantTo('show a category');
+ $I->amOnPage('/categories/show/3');
+ $I->see('Delete me');
}
/**
@@ -79,15 +89,108 @@ class CategoryControllerCest
*/
public function store(FunctionalTester $I)
{
- $I->wantTo('store a category');
+ $I->amOnPage('/categories/create');
+ $I->wantTo('store a new category');
+ $I->see('Create a new category');
+ $I->submitForm('#store', ['name' => 'New category.', 'post_submit_action' => 'store']);
+ $I->seeRecord('categories', ['name' => 'New category.']);
+ resetToClean::clean();
}
+ /**
+ * @param FunctionalTester $I
+ */
+ public function storeValidateOnly(FunctionalTester $I)
+ {
+ $I->amOnPage('/categories/create');
+ $I->wantTo('validate a new category');
+ $I->see('Create a new category');
+ $I->submitForm('#store', ['name' => 'New category.', 'post_submit_action' => 'validate_only']);
+ $I->dontSeeRecord('categories', ['name' => 'New category.']);
+ resetToClean::clean();
+ }
+
+ /**
+ * @param FunctionalTester $I
+ */
+ public function storeAndCreateAnother(FunctionalTester $I)
+ {
+ $I->amOnPage('/categories/create');
+ $I->wantTo('store a new category and create another');
+ $I->see('Create a new category');
+ $I->submitForm('#store', ['name' => 'New category.', 'post_submit_action' => 'create_another']);
+ $I->seeRecord('categories', ['name' => 'New category.']);
+ resetToClean::clean();
+ }
+
+ /**
+ * @param FunctionalTester $I
+ */
+ public function storeFail(FunctionalTester $I)
+ {
+ $I->amOnPage('/categories/create');
+ $I->wantTo('make storing a new category fail.');
+ $I->see('Create a new category');
+ $I->submitForm('#store', ['name' => null, 'post_submit_action' => 'validate_only']);
+ $I->dontSeeRecord('categories', ['name' => 'New category.']);
+ resetToClean::clean();
+ }
/**
* @param FunctionalTester $I
*/
public function update(FunctionalTester $I)
{
$I->wantTo('update a category');
+ $I->amOnPage('/categories/edit/4');
+ $I->see('Edit category "Delete me"');
+ $I->submitForm('#update', ['name' => 'Update me', 'post_submit_action' => 'update']);
+ $I->seeRecord('categories', ['name' => 'Update me']);
+ resetToClean::clean();
+
+ }
+
+ /**
+ * @param FunctionalTester $I
+ */
+ public function failUpdate(FunctionalTester $I)
+ {
+ $I->wantTo('update a category and fail');
+ $I->amOnPage('/categories/edit/4');
+ $I->see('Edit category "Delete me"');
+ $I->submitForm('#update', ['name' => '', 'post_submit_action' => 'update']);
+ $I->seeRecord('categories', ['name' => 'Delete me']);
+
+ }
+
+ /**
+ * @param FunctionalTester $I
+ */
+ public function validateUpdateOnly(FunctionalTester $I)
+ {
+ $I->wantTo('update a category and validate only');
+ $I->amOnPage('/categories/edit/4');
+ $I->see('Edit category "Delete me"');
+ $I->submitForm(
+ '#update', ['name' => 'Validate Only', 'post_submit_action' => 'validate_only']
+ );
+ $I->dontSeeRecord('categories', ['name' => 'Savings accountXX']);
+ $I->seeRecord('categories', ['name' => 'Delete me']);
+
+ }
+
+ /**
+ * @param FunctionalTester $I
+ */
+ public function updateAndReturn(FunctionalTester $I)
+ {
+ $I->wantTo('update a category and return to form');
+ $I->amOnPage('/categories/edit/4');
+ $I->see('Edit category "Delete me"');
+ $I->submitForm(
+ '#update', ['name' => 'Savings accountXX', 'post_submit_action' => 'return_to_edit']
+ );
+ $I->seeRecord('categories', ['name' => 'Savings accountXX']);
+
}
}
\ No newline at end of file