Category controller covered.

This commit is contained in:
James Cole 2014-12-20 16:53:32 +01:00
parent 1484621300
commit 7b4703e4ff
10 changed files with 152 additions and 53 deletions

View File

@ -96,7 +96,8 @@ class CategoryController extends BaseController
*/ */
public function store() public function store()
{ {
$data = Input::except('_token'); $data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
// always validate: // always validate:
$messages = $this->_repository->validate($data); $messages = $this->_repository->validate($data);
@ -121,12 +122,7 @@ class CategoryController extends BaseController
return Redirect::route('categories.index'); return Redirect::route('categories.index');
} }
// create another. return Redirect::route('categories.create')->withInput();
if ($data['post_submit_action'] == 'create_another') {
return Redirect::route('categories.create')->withInput();
}
return Redirect::route('categories.index');
} }
/** /**
@ -137,7 +133,8 @@ class CategoryController extends BaseController
*/ */
public function update(Category $category) public function update(Category $category)
{ {
$data = Input::except('_token'); $data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
// always validate: // always validate:
$messages = $this->_repository->validate($data); $messages = $this->_repository->validate($data);
@ -163,12 +160,9 @@ class CategoryController extends BaseController
if ($data['post_submit_action'] == 'update') { if ($data['post_submit_action'] == 'update') {
return Redirect::route('categories.index'); 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']);
} }

View File

@ -48,13 +48,14 @@ class TestContentSeeder extends Seeder
// and because we have no filters, some repetitions: // 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' => $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]); $repTwo = 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]); $repThree = LimitRepetition::create(['budget_limit_id' => $limitThree->id, 'startdate' => '2014-01-01', 'enddate' => '2014-01-31', 'amount' => 200]);
// create two categories: // create two categories:
$dailyGroceries = Category::create(['user_id' => $user->id, 'name' => 'DailyGroceries']); $dailyGroceries = Category::create(['user_id' => $user->id, 'name' => 'DailyGroceries']);
$lunch = Category::create(['user_id' => $user->id, 'name' => 'Lunch']); $lunch = Category::create(['user_id' => $user->id, 'name' => 'Lunch']);
$house = Category::create(['user_id' => $user->id, 'name' => 'House']); $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 1', 'class' => 'Budget']);
Component::create(['user_id' => $user->id, 'name' => 'Some Component 2', 'class' => 'Budget']); Component::create(['user_id' => $user->id, 'name' => 'Some Component 2', 'class' => 'Budget']);

View File

@ -48,9 +48,8 @@ class Category implements CUD, CommonDatabaseCalls
*/ */
public function store(array $data) public function store(array $data)
{ {
$category = new \Category; $category = new \Category;
$category->name = $data['name']; $category->name = $data['name'];
$category->class = 'Category';
$category->user()->associate($this->getUser()); $category->user()->associate($this->getUser());
if (!$category->isValid()) { if (!$category->isValid()) {
\Log::error('Could not store category: ' . $category->getErrors()->toJson()); \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) public function update(Eloquent $model, array $data)
{ {
$model->name = $data['name']; $model->name = $data['name'];
if (!$model->isValid()) {
\Log::error('Could not store category: ' . $model->getErrors()->toJson());
throw new FireflyException($model->getErrors()->first());
}
$model->save(); $model->save();
return true; return true;
@ -94,8 +87,9 @@ class Category implements CUD, CommonDatabaseCalls
{ {
$warnings = new MessageBag; $warnings = new MessageBag;
$successes = new MessageBag; $successes = new MessageBag;
$validator = \Validator::make($model, \Component::$rules); $category = new \Category($model);
$errors = $validator->errors(); $category->isValid();
$errors = $category->getErrors();
if (!$errors->has('name')) { if (!$errors->has('name')) {
$successes->add('name', 'OK'); $successes->add('name', 'OK');

View File

@ -1,13 +1,35 @@
<?php <?php
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletingTrait; use Illuminate\Database\Eloquent\SoftDeletingTrait;
use \Illuminate\Database\Eloquent\Model as Eloquent; use Watson\Validating\ValidatingTrait;
/** /**
* Class Category * Class Category
*/ */
class Category extends Eloquent class Category extends Eloquent
{ {
use SoftDeletingTrait; use SoftDeletingTrait, ValidatingTrait;
protected $fillable = ['name', 'user_id']; protected $fillable = ['name', 'user_id'];
protected $rules
= [
'user_id' => '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 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
@ -24,19 +46,4 @@ class Category extends Eloquent
{ {
return $this->belongsTo('User'); 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;
}
} }

View File

@ -1,7 +1,7 @@
@extends('layouts.default') @extends('layouts.default')
@section('content') @section('content')
{{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName()) }} {{ 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')])}}
<div class="row"> <div class="row">
<div class="col-lg-6 col-md-6 col-sm-12"> <div class="col-lg-6 col-md-6 col-sm-12">

View File

@ -1,7 +1,7 @@
@extends('layouts.default') @extends('layouts.default')
@section('content') @section('content')
{{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName(), $category) }} {{ 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)])}}
<div class="row"> <div class="row">
<div class="col-lg-6 col-md-12 col-sm-12"> <div class="col-lg-6 col-md-12 col-sm-12">
<div class="panel panel-red"> <div class="panel panel-red">

View File

@ -1,7 +1,7 @@
@extends('layouts.default') @extends('layouts.default')
@section('content') @section('content')
{{ Breadcrumbs::renderIfExists(Route::getCurrentRoute()->getName(), $category) }} {{ 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)])}}
<div class="row"> <div class="row">
<div class="col-lg-6 col-md-6 col-sm-12"> <div class="col-lg-6 col-md-6 col-sm-12">
<div class="panel panel-primary"> <div class="panel panel-primary">

View File

@ -31,7 +31,7 @@
@section('scripts') @section('scripts')
<script type="text/javascript"> <script type="text/javascript">
var componentID = {{$category->id}}; var componentID = {{$category->id}};
var year = {{Session::get('start')->format('Y')}}; var year = {{Session::get('start',\Carbon\Carbon::now()->startOfMonth())->format('Y')}};
</script> </script>

View File

@ -70,8 +70,6 @@ class BudgetControllerCest
$I->see('Delete budget "Delete me"'); $I->see('Delete budget "Delete me"');
$I->submitForm('#destroy', []); $I->submitForm('#destroy', []);
$I->see('Budget &quot;Delete me&quot; was deleted.'); $I->see('Budget &quot;Delete me&quot; was deleted.');
#$I->dontSeeInDatabase('budgets', ['name' => 'Delete me','deleted_at' => null]);
resetToClean::clean();
} }
/** /**
@ -80,7 +78,8 @@ class BudgetControllerCest
public function edit(FunctionalTester $I) public function edit(FunctionalTester $I)
{ {
$I->wantTo('edit a budget'); $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) public function show(FunctionalTester $I)
{ {
$I->wantTo('show a budget'); $I->wantTo('show a budget');
$I->amOnPage('/budgets/show/1'); $I->amOnPage('/budgets/show/3');
$I->see('Delete me');
} }
/** /**

View File

@ -38,6 +38,8 @@ class CategoryControllerCest
public function delete(FunctionalTester $I) public function delete(FunctionalTester $I)
{ {
$I->wantTo('delete a category'); $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) public function destroy(FunctionalTester $I)
{ {
$I->wantTo('destroy a category'); $I->wantTo('destroy a category');
$I->amOnPage('/categories/delete/4');
$I->see('Delete category "Delete me"');
$I->submitForm('#destroy', []);
$I->see('Category &quot;Delete me&quot; was deleted.');
} }
@ -55,6 +61,8 @@ class CategoryControllerCest
public function edit(FunctionalTester $I) public function edit(FunctionalTester $I)
{ {
$I->wantTo('edit a category'); $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) public function show(FunctionalTester $I)
{ {
$I->wantTo('show a category'); $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) 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 * @param FunctionalTester $I
*/ */
public function update(FunctionalTester $I) public function update(FunctionalTester $I)
{ {
$I->wantTo('update a category'); $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']);
} }
} }